81 lines
2.8 KiB
C
81 lines
2.8 KiB
C
// SPDX-License-Identifier: GPL-3.0-or-later
|
|
#include <vcc/ssc.h>
|
|
#include "internal.h"
|
|
|
|
static unsigned char play_slot(unsigned char command, unsigned char slot,
|
|
unsigned int poll_limit)
|
|
{
|
|
if (slot > 7)
|
|
return SSC_INVALID;
|
|
command += slot;
|
|
return ssc_write_bytes(&command, 1, poll_limit);
|
|
}
|
|
|
|
unsigned char ssc_play_phrase(unsigned char slot, unsigned int poll_limit)
|
|
{
|
|
return play_slot(0xD0, slot, poll_limit);
|
|
}
|
|
|
|
unsigned char ssc_store_allophones(unsigned char slot, const unsigned char *codes,
|
|
unsigned int count, unsigned int poll_limit)
|
|
{
|
|
static const unsigned char end[] = {SSC_AL_PA1, 0xFF};
|
|
unsigned char command;
|
|
unsigned int i;
|
|
if (slot > 7 || !codes || !count || count > SSC_MAX_ALLOPHONES)
|
|
return SSC_INVALID;
|
|
for (i = 0; i < count; ++i)
|
|
if (codes[i] > SSC_AL_BB2)
|
|
return SSC_INVALID;
|
|
|
|
command = 0xB0 + slot;
|
|
if (ssc_write_bytes(&command, 1, poll_limit) != SSC_OK ||
|
|
ssc_write_bytes(codes, count, poll_limit) != SSC_OK)
|
|
return SSC_TIMEOUT;
|
|
return ssc_write_bytes(end, sizeof(end), poll_limit);
|
|
}
|
|
|
|
unsigned char ssc_play_allophones(unsigned char slot, unsigned int poll_limit)
|
|
{
|
|
return play_slot(0xF0, slot, poll_limit);
|
|
}
|
|
|
|
unsigned char ssc_store_enveloped_tone(unsigned char slot, unsigned char channel,
|
|
unsigned int tone_period, unsigned char shape,
|
|
unsigned int envelope_period, unsigned char duration,
|
|
unsigned int poll_limit)
|
|
{
|
|
unsigned char packet[18];
|
|
if (slot > 7 || channel > 2 || !tone_period || tone_period > 4095 ||
|
|
shape > 15 || !envelope_period)
|
|
return SSC_INVALID;
|
|
|
|
packet[0] = 0x98 + slot;
|
|
// The PIC writes the shape before the period. On a cold cartridge a
|
|
// one-shot can finish at the old period before the PIC enables the tone.
|
|
// Prime the generator and retrigger with the tone gate already open.
|
|
packet[1] = 0x60 | shape;
|
|
packet[2] = (unsigned char)(envelope_period >> 8);
|
|
packet[3] = (unsigned char)envelope_period;
|
|
packet[4] = 0;
|
|
packet[5] = (channel << 5) | 0x10;
|
|
packet[6] = (unsigned char)(tone_period >> 8);
|
|
packet[7] = (unsigned char)tone_period;
|
|
packet[8] = duration; // The tone's timer schedules the silence.
|
|
packet[9] = packet[1];
|
|
packet[10] = packet[2];
|
|
packet[11] = packet[3];
|
|
packet[12] = 0;
|
|
packet[13] = channel << 5; // Final event selects fixed amplitude zero.
|
|
packet[14] = packet[6];
|
|
packet[15] = packet[7];
|
|
packet[16] = 0;
|
|
packet[17] = 0xFF;
|
|
return ssc_write_bytes(packet, sizeof(packet), poll_limit);
|
|
}
|
|
|
|
unsigned char ssc_play_stored_effect(unsigned char slot, unsigned int poll_limit)
|
|
{
|
|
return play_slot(0xD8, slot, poll_limit);
|
|
}
|