From b7e8b634595445325d10f8fcddcb7d6cdaa8a922 Mon Sep 17 00:00:00 2001 From: Ian C Date: Tue, 3 Aug 2021 20:26:23 +0000 Subject: First attempt at sound --- source/spec.c | 49 +++++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 45 insertions(+), 4 deletions(-) (limited to 'source/spec.c') diff --git a/source/spec.c b/source/spec.c index 6cdc7c6..ef132d4 100644 --- a/source/spec.c +++ b/source/spec.c @@ -59,6 +59,7 @@ #define ED_LOAD 0xf1 #define SCAN_CYCLES 224 +#define FRAME_CYCLES 69888 /* The 3DS screen */ @@ -153,6 +154,17 @@ static struct {7,0x10}, {7,0x08}, {7,0x04}, {7,0x02}, {7,0x01} /* B - SPACE */ }; +/* Sound +*/ +#define SAMPLE_BUFF_SZ (SAMPLE_RATE/60) + +#define SAMPLE_CYCLE (FRAME_CYCLES/SAMPLE_BUFF_SZ) + +static u8 *sample[2]; +static int sound_buffer; +static u8 sound_level; +static int sound_ptr; +static ndspWaveBuf wave_buff[2]; /* ---------------------------------------- PRIVATE FUNCTIONS */ @@ -160,7 +172,7 @@ static void RomPatch(void) { static const Z80Byte save[]= { - 0xed, ED_SAVE, /* (SAVE) */ + 0xed, ED_SAVE, /* (SAVE) */ 0xc9, /* RET */ 0xff /* End of patch */ }; @@ -263,6 +275,13 @@ static int CheckTimers(Z80 *z80, Z80Val val) { int ret = TRUE; + if (DSSPEC_Config[DSSPEC_SOUND] && + Z80GetTimer(z80, Z80_TIMER_1) >= SAMPLE_CYCLE) + { + Z80SetTimer(z80, Z80_TIMER_1, 0); + sample[sound_buffer][sound_ptr++] = sound_level; + } + if (val > SCAN_CYCLES) { int y; @@ -287,6 +306,15 @@ static int CheckTimers(Z80 *z80, Z80Val val) Z80Interrupt(z80,0xff); + if (DSSPEC_Config[DSSPEC_SOUND]) + { + ndspChnWaveBufAdd(0, wave_buff + sound_buffer); + sound_buffer = !sound_buffer; + memset(sample[sound_buffer], 0, SAMPLE_BUFF_SZ); + sound_ptr = 0; + Z80SetTimer(z80, Z80_TIMER_1, 0); + } + ret = FALSE; } @@ -298,8 +326,6 @@ static int CheckTimers(Z80 *z80, Z80Val val) { DrawScanline(y, scanline); } - - /* TODO: Process sound emulation */ } return ret; @@ -361,6 +387,18 @@ void SPECInit(Z80 *z80) Z80LodgeCallback(z80,eZ80_EDHook,EDCallback); Z80LodgeCallback(z80,eZ80_Instruction,CheckTimers); + /* Create sound buffers and wave vars + */ + sample[0] = linearAlloc(SAMPLE_BUFF_SZ); + sample[1] = linearAlloc(SAMPLE_BUFF_SZ); + + wave_buff[0].data_vaddr = sample[0]; + wave_buff[0].nsamples = SAMPLE_BUFF_SZ; + wave_buff[1].data_vaddr = sample[1]; + wave_buff[1].nsamples = SAMPLE_BUFF_SZ; + + sound_buffer = 0; + SPECReset(z80); } @@ -465,6 +503,7 @@ void SPECWritePort(Z80 *z80, Z80Word port, Z80Byte val) { case 0xfe: /* ULA */ border = val & 0x07; + sound_level = val & 0x10 ? 128:0; break; case 0xfb: /* ZX Printer */ @@ -486,11 +525,13 @@ void SPECReset(Z80 *z80) matrix[f]=0x1f; Z80Reset(z80); - Z80ResetCycles(z80,0); border = 0; scanline = 0; + sound_buffer = 0; + sound_ptr = 0; + /* Set up screen */ c=0; -- cgit v1.2.3