From 2a16923dbff55ba94a72322cd3d28934dec6fdfb Mon Sep 17 00:00:00 2001 From: Jeremy Rand Date: Sun, 18 Dec 2016 22:46:43 -0500 Subject: [PATCH] Hide table access routines and use a higher level function for playing sounds on the Mockingboard. --- mocktest/main.c | 8 ++--- mocktest/mockingboard.c | 69 +++++++++++++++++++++++++++-------------- mocktest/mockingboard.h | 17 +++++----- 3 files changed, 57 insertions(+), 37 deletions(-) diff --git a/mocktest/main.c b/mocktest/main.c index 64d0c73..68ca05d 100644 --- a/mocktest/main.c +++ b/mocktest/main.c @@ -88,13 +88,13 @@ int main(void) printf("HELLO, WORLD!\n"); while (!kbhit()) { - mockingBoardTableAccess(SOUND_CHIP_1, &soundData1); + mockingBoardPlaySound(SPEAKER_LEFT, &soundData1); delay(); - mockingBoardReset(SOUND_CHIP_1); + mockingBoardStopSound(SPEAKER_LEFT); - mockingBoardTableAccess(SOUND_CHIP_2, &soundData2); + mockingBoardPlaySound(SPEAKER_RIGHT, &soundData2); delay(); - mockingBoardReset(SOUND_CHIP_2); + mockingBoardStopSound(SPEAKER_RIGHT); } cgetc(); diff --git a/mocktest/mockingboard.c b/mocktest/mockingboard.c index a485316..cff3358 100644 --- a/mocktest/mockingboard.c +++ b/mocktest/mockingboard.c @@ -22,6 +22,19 @@ #define RESET_COMMAND 0x0 #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 @@ -35,6 +48,8 @@ static uint8_t gMockingBoardInitialized = false; static uint8_t gMockingBoardSpeechInitialized = false; +// Implementation + static uint8_t *mapIOPointer(tSlot slot, uint8_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) { - tSoundChip soundChip; + tMockingBoardSoundChip soundChip; if (sizeof(tMockingSoundRegisters) != 16) { 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]; @@ -102,25 +117,7 @@ static void writeCommand(tSoundChip soundChip, uint8_t command) } -void mockingBoardLatch(tSoundChip soundChip) -{ - 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) +static void mockingBoardTableAccess(tMockingBoardSoundChip soundChip, tMockingSoundRegisters *registers) { uint8_t *data = (uint8_t *)registers; volatile uint8_t *ptr = gMockPortA[soundChip]; @@ -129,17 +126,41 @@ void mockingBoardTableAccess(tSoundChip soundChip, tMockingSoundRegisters *regis if (!gMockingBoardInitialized) return; - mockingBoardReset(soundChip); + MOCKINGBOARD_RESET(soundChip); for (index = 0; index < 16; index++) { *ptr = index; - mockingBoardLatch(soundChip); + MOCKINGBOARD_LATCH(soundChip); *ptr = *data; - mockingBoardWrite(soundChip); + MOCKINGBOARD_WRITE(soundChip); 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) { return (mockingBoardSpeechBusy != 0); diff --git a/mocktest/mockingboard.h b/mocktest/mockingboard.h index dc31b77..d7b57f5 100644 --- a/mocktest/mockingboard.h +++ b/mocktest/mockingboard.h @@ -91,11 +91,13 @@ typedef uint8_t tSlot; + typedef enum { - SOUND_CHIP_1 = 0, - SOUND_CHIP_2 = 1, - NUM_SOUND_CHIPS = 2 -} tSoundChip; + SPEAKER_NONE = 0, + SPEAKER_LEFT = (1 << 0), + SPEAKER_RIGHT = (1 << 1), + SPEAKER_BOTH = (1 << 0) | (1 << 1) +} tMockingBoardSpeaker; typedef struct tMockingSoundRegisters { @@ -115,11 +117,8 @@ typedef struct tMockingSoundRegisters { extern void mockingBoardInit(tSlot slot, bool hasSpeechChip); extern void mockingBoardShutdown(void); -extern void mockingBoardLatch(tSoundChip soundChip); -extern void mockingBoardWrite(tSoundChip soundChip); -extern void mockingBoardReset(tSoundChip soundChip); - -extern void mockingBoardTableAccess(tSoundChip soundChip, tMockingSoundRegisters *registers); +extern void mockingBoardPlaySound(tMockingBoardSpeaker speaker, tMockingSoundRegisters *registers); +extern void mockingBoardStopSound(tMockingBoardSpeaker speaker); extern bool mockingBoardSpeechIsBusy(void); extern bool mockingBoardSpeechIsPlaying(void);