Skip to content

Commit

Permalink
Latch DAC data address for PCM driver when releasing Z80 bus
Browse files Browse the repository at this point in the history
Prevents the PCM driver from sending junk to the data port of other YM2612 registers
  • Loading branch information
rhargreaves committed Aug 30, 2024
1 parent 4419f71 commit c2d6e8e
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 5 deletions.
13 changes: 10 additions & 3 deletions src/synth.c
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,11 @@ static u8 volumeAdjustedTotalLevel(u8 channel, u8 totalLevel);
static void channelParameterUpdated(u8 channel);
static void otherParameterUpdated(u8 channel, ParameterUpdated parameterUpdated);
static void writeRegSafe(u8 part, u8 reg, u8 data);
static void releaseZ80Bus(void);

void synth_init(const FmChannel* initialPreset)
{
// Z80_loadDriver(Z80_DRIVER_PCM, true);
Z80_loadDriver(Z80_DRIVER_PCM, true);
Z80_requestBus(TRUE);
writeSpecialModeReg();
for (u8 chan = 0; chan < MAX_FM_CHANS; chan++) {
Expand All @@ -57,7 +58,7 @@ void synth_init(const FmChannel* initialPreset)
updateChannel(chan);
}
writeGlobalLfo();
Z80_releaseBus();
releaseZ80Bus();
}

static void updateChannel(u8 chan)
Expand Down Expand Up @@ -454,6 +455,12 @@ static void writeRegSafe(u8 part, u8 reg, u8 data)
bool takenAlready = Z80_getAndRequestBus(TRUE);
YM2612_writeReg(part, reg, data);
if (!takenAlready) {
Z80_releaseBus();
releaseZ80Bus();
}
}

static void releaseZ80Bus(void)
{
YM2612_write(0, 0x2A); // Latch reg address for PCM driver
Z80_releaseBus();
}
1 change: 1 addition & 0 deletions tests/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@ MOCKS=midi_process \
MD_MOCKS=SYS_setVIntCallback \
VDP_setTextPalette \
YM2612_writeReg \
YM2612_write \
VDP_drawText \
VDP_clearText \
VDP_setBackgroundColor \
Expand Down
13 changes: 13 additions & 0 deletions tests/asserts.c
Original file line number Diff line number Diff line change
Expand Up @@ -81,9 +81,14 @@ void _expect_ym2612_write_reg_any_data(u8 part, u8 reg, const char* const file,
#endif
expect_value_with_pos(__wrap_Z80_getAndRequestBus, wait, TRUE, file, line);
will_return_with_pos(__wrap_Z80_getAndRequestBus, false, file, line);

expect_any_with_pos(__wrap_YM2612_writeReg, part, file, line);
expect_any_with_pos(__wrap_YM2612_writeReg, reg, file, line);
expect_any_with_pos(__wrap_YM2612_writeReg, data, file, line);

expect_value_with_pos(__wrap_YM2612_write, port, 0, file, line);
expect_value_with_pos(__wrap_YM2612_write, data, 0x2A, file, line);

expect_function_call(__wrap_Z80_releaseBus);
}

Expand All @@ -96,6 +101,9 @@ void expect_ym2612_write_operator_any_data(u8 chan, u8 op, u8 baseReg)
expect_value(__wrap_YM2612_writeReg, reg, baseReg + REG_OFFSET(chan) + (regOpIndex(op) * 4));
expect_any(__wrap_YM2612_writeReg, data);

expect_value(__wrap_YM2612_write, port, 0);
expect_value(__wrap_YM2612_write, data, 0x2A);

expect_function_call(__wrap_Z80_releaseBus);
}

Expand Down Expand Up @@ -138,9 +146,14 @@ void _expect_ym2612_write_reg(u8 part, u8 reg, u8 data, const char* const file,
#endif
expect_value(__wrap_Z80_getAndRequestBus, wait, TRUE);
will_return(__wrap_Z80_getAndRequestBus, false);

expect_value_with_pos(__wrap_YM2612_writeReg, part, part, file, line);
expect_value_with_pos(__wrap_YM2612_writeReg, reg, reg, file, line);
expect_value_with_pos(__wrap_YM2612_writeReg, data, data, file, line);

expect_value_with_pos(__wrap_YM2612_write, port, 0, file, line);
expect_value_with_pos(__wrap_YM2612_write, data, 0x2A, file, line);

expect_function_call(__wrap_Z80_releaseBus);
}

Expand Down
8 changes: 6 additions & 2 deletions tests/unit/test_synth.c
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ static void set_initial_registers(void)
expect_any(__wrap_YM2612_writeReg, data);
}

expect_value(__wrap_YM2612_write, port, 0);
expect_value(__wrap_YM2612_write, data, 0x2A);
expect_function_call(__wrap_Z80_releaseBus);

const FmChannel M_BANK_0_INST_0_GRANDPIANO = { 0, 0, 3, 0, 0, 0, 0,
Expand All @@ -37,14 +39,14 @@ static void loads_pcm_driver(void)
static int test_synth_setup(UNUSED void** state)
{
updated = false;
// loads_pcm_driver();
loads_pcm_driver();
set_initial_registers();
return 0;
}

static void test_synth_init_sets_initial_registers(UNUSED void** state)
{
// loads_pcm_driver();
loads_pcm_driver();
set_initial_registers();
}

Expand Down Expand Up @@ -744,6 +746,8 @@ static void test_requests_Z80_bus_if_not_already_taken(UNUSED void** state)
expect_value(__wrap_YM2612_writeReg, reg, 0x2B);
expect_value(__wrap_YM2612_writeReg, data, 0);

expect_value(__wrap_YM2612_write, port, 0);
expect_value(__wrap_YM2612_write, data, 0x2A);
expect_function_call(__wrap_Z80_releaseBus);

__real_synth_directWriteYm2612(0, 0x2B, 0);
Expand Down
8 changes: 8 additions & 0 deletions tests/wraps.c
Original file line number Diff line number Diff line change
Expand Up @@ -406,6 +406,14 @@ void __wrap_YM2612_writeReg(const u16 part, const u8 reg, const u8 data)
check_expected(data);
}

void __wrap_YM2612_write(const u16 port, const u8 data)
{
if (disableChecks)
return;
check_expected(port);
check_expected(data);
}

void __wrap_VDP_drawText(const char* str, u16 x, u16 y)
{
}
Expand Down
1 change: 1 addition & 0 deletions tests/wraps.h
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,7 @@ extern void __real_scheduler_addFrameHandler(HandlerFunc* onFrame);

/* SDGK wraps */
void __wrap_YM2612_writeReg(const u16 part, const u8 reg, const u8 data);
void __wrap_YM2612_write(const u16 port, const u8 data);
void __wrap_VDP_drawText(const char* str, u16 x, u16 y);
void __wrap_SYS_setVIntCallback(VoidCallback* CB);
void __wrap_VDP_setTextPalette(u16 palette);
Expand Down

0 comments on commit c2d6e8e

Please sign in to comment.