Hide table access routines and use a higher level function for playing sounds on the Mockingboard.

This commit is contained in:
Jeremy Rand 2016-12-18 22:46:43 -05:00
parent ee5b55de46
commit 2a16923dbf
3 changed files with 57 additions and 37 deletions

View File

@ -88,13 +88,13 @@ int main(void)
printf("HELLO, WORLD!\n"); printf("HELLO, WORLD!\n");
while (!kbhit()) { while (!kbhit()) {
mockingBoardTableAccess(SOUND_CHIP_1, &soundData1); mockingBoardPlaySound(SPEAKER_LEFT, &soundData1);
delay(); delay();
mockingBoardReset(SOUND_CHIP_1); mockingBoardStopSound(SPEAKER_LEFT);
mockingBoardTableAccess(SOUND_CHIP_2, &soundData2); mockingBoardPlaySound(SPEAKER_RIGHT, &soundData2);
delay(); delay();
mockingBoardReset(SOUND_CHIP_2); mockingBoardStopSound(SPEAKER_RIGHT);
} }
cgetc(); cgetc();

View File

@ -22,6 +22,19 @@
#define RESET_COMMAND 0x0 #define RESET_COMMAND 0x0
#define THROUGH_PORT_B 0x4 #define THROUGH_PORT_B 0x4
#define MOCKINGBOARD_LATCH(soundChip) writeCommand((soundChip), LATCH_COMMAND)
#define MOCKINGBOARD_WRITE(soundChip) writeCommand((soundChip), WRITE_COMMAND)
#define MOCKINGBOARD_RESET(soundChip) writeCommand((soundChip), RESET_COMMAND)
// Typedefs
typedef enum {
SOUND_CHIP_1 = 0,
SOUND_CHIP_2 = 1,
NUM_SOUND_CHIPS = 2
} tMockingBoardSoundChip;
// Globals // Globals
@ -35,6 +48,8 @@ static uint8_t gMockingBoardInitialized = false;
static uint8_t gMockingBoardSpeechInitialized = false; static uint8_t gMockingBoardSpeechInitialized = false;
// Implementation
static uint8_t *mapIOPointer(tSlot slot, uint8_t *ptr) static uint8_t *mapIOPointer(tSlot slot, uint8_t *ptr)
{ {
uint16_t temp1 = (uint16_t)ptr; uint16_t temp1 = (uint16_t)ptr;
@ -51,7 +66,7 @@ static uint8_t *mapIOPointer(tSlot slot, uint8_t *ptr)
void mockingBoardInit(tSlot slot, bool hasSpeechChip) void mockingBoardInit(tSlot slot, bool hasSpeechChip)
{ {
tSoundChip soundChip; tMockingBoardSoundChip soundChip;
if (sizeof(tMockingSoundRegisters) != 16) { if (sizeof(tMockingSoundRegisters) != 16) {
printf("The sound registers must be 16 bytes long!\n"); printf("The sound registers must be 16 bytes long!\n");
@ -93,7 +108,7 @@ void mockingBoardShutdown(void)
} }
static void writeCommand(tSoundChip soundChip, uint8_t command) static void writeCommand(tMockingBoardSoundChip soundChip, uint8_t command)
{ {
volatile uint8_t *ptr = gMockPortB[soundChip]; volatile uint8_t *ptr = gMockPortB[soundChip];
@ -102,25 +117,7 @@ static void writeCommand(tSoundChip soundChip, uint8_t command)
} }
void mockingBoardLatch(tSoundChip soundChip) static void mockingBoardTableAccess(tMockingBoardSoundChip soundChip, tMockingSoundRegisters *registers)
{
writeCommand(soundChip, LATCH_COMMAND);
}
void mockingBoardWrite(tSoundChip soundChip)
{
writeCommand(soundChip, WRITE_COMMAND);
}
void mockingBoardReset(tSoundChip soundChip)
{
writeCommand(soundChip, RESET_COMMAND);
}
void mockingBoardTableAccess(tSoundChip soundChip, tMockingSoundRegisters *registers)
{ {
uint8_t *data = (uint8_t *)registers; uint8_t *data = (uint8_t *)registers;
volatile uint8_t *ptr = gMockPortA[soundChip]; volatile uint8_t *ptr = gMockPortA[soundChip];
@ -129,17 +126,41 @@ void mockingBoardTableAccess(tSoundChip soundChip, tMockingSoundRegisters *regis
if (!gMockingBoardInitialized) if (!gMockingBoardInitialized)
return; return;
mockingBoardReset(soundChip); MOCKINGBOARD_RESET(soundChip);
for (index = 0; index < 16; index++) { for (index = 0; index < 16; index++) {
*ptr = index; *ptr = index;
mockingBoardLatch(soundChip); MOCKINGBOARD_LATCH(soundChip);
*ptr = *data; *ptr = *data;
mockingBoardWrite(soundChip); MOCKINGBOARD_WRITE(soundChip);
data++; data++;
} }
} }
void mockingBoardPlaySound(tMockingBoardSpeaker speaker, tMockingSoundRegisters *registers)
{
if ((speaker & SPEAKER_LEFT) != 0) {
mockingBoardTableAccess(SOUND_CHIP_1, registers);
}
if ((speaker & SPEAKER_RIGHT) != 0) {
mockingBoardTableAccess(SOUND_CHIP_2, registers);
}
}
void mockingBoardStopSound(tMockingBoardSpeaker speaker)
{
if ((speaker & SPEAKER_LEFT) != 0) {
MOCKINGBOARD_RESET(SOUND_CHIP_1);
}
if ((speaker & SPEAKER_RIGHT) != 0) {
MOCKINGBOARD_RESET(SOUND_CHIP_2);
}
}
bool mockingBoardSpeechIsBusy(void) bool mockingBoardSpeechIsBusy(void)
{ {
return (mockingBoardSpeechBusy != 0); return (mockingBoardSpeechBusy != 0);

View File

@ -91,11 +91,13 @@
typedef uint8_t tSlot; typedef uint8_t tSlot;
typedef enum { typedef enum {
SOUND_CHIP_1 = 0, SPEAKER_NONE = 0,
SOUND_CHIP_2 = 1, SPEAKER_LEFT = (1 << 0),
NUM_SOUND_CHIPS = 2 SPEAKER_RIGHT = (1 << 1),
} tSoundChip; SPEAKER_BOTH = (1 << 0) | (1 << 1)
} tMockingBoardSpeaker;
typedef struct tMockingSoundRegisters { typedef struct tMockingSoundRegisters {
@ -115,11 +117,8 @@ typedef struct tMockingSoundRegisters {
extern void mockingBoardInit(tSlot slot, bool hasSpeechChip); extern void mockingBoardInit(tSlot slot, bool hasSpeechChip);
extern void mockingBoardShutdown(void); extern void mockingBoardShutdown(void);
extern void mockingBoardLatch(tSoundChip soundChip); extern void mockingBoardPlaySound(tMockingBoardSpeaker speaker, tMockingSoundRegisters *registers);
extern void mockingBoardWrite(tSoundChip soundChip); extern void mockingBoardStopSound(tMockingBoardSpeaker speaker);
extern void mockingBoardReset(tSoundChip soundChip);
extern void mockingBoardTableAccess(tSoundChip soundChip, tMockingSoundRegisters *registers);
extern bool mockingBoardSpeechIsBusy(void); extern bool mockingBoardSpeechIsBusy(void);
extern bool mockingBoardSpeechIsPlaying(void); extern bool mockingBoardSpeechIsPlaying(void);