Skip to content

Commit

Permalink
Add more helper functions for YM2612 expectations
Browse files Browse the repository at this point in the history
  • Loading branch information
rhargreaves committed Feb 19, 2025
1 parent 0305ba2 commit f244a18
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 63 deletions.
42 changes: 26 additions & 16 deletions tests/mocks/mock_ym2612.c
Original file line number Diff line number Diff line change
Expand Up @@ -35,17 +35,6 @@ void __wrap_YM2612_write(const u16 port, const u8 data)
check_expected(data);
}

static u8 regOpIndex(u8 op)
{
if (op == 1) {
return 2;
} else if (op == 2) {
return 1;
} else {
return op;
}
}

void _expect_ym2612_write_reg_any_data(u8 part, u8 reg, const char* const file, const int line)
{
#ifdef DEBUG
Expand All @@ -71,8 +60,8 @@ void _expect_ym2612_write_operator_any_data(
will_return_with_pos(__wrap_Z80_getAndRequestBus, false, file, line);

expect_value_with_pos(__wrap_YM2612_writeReg, part, REG_PART(chan), file, line);
expect_value_with_pos(
__wrap_YM2612_writeReg, reg, baseReg + REG_OFFSET(chan) + (regOpIndex(op) * 4), file, line);
expect_value_with_pos(__wrap_YM2612_writeReg, reg,
baseReg + REG_OFFSET(chan) + (YM_OP_REG_INDEX(op) * 4), file, line);
expect_any_with_pos(__wrap_YM2612_writeReg, data, file, line);

expect_value_with_pos(__wrap_YM2612_write, port, 0, file, line);
Expand Down Expand Up @@ -103,7 +92,7 @@ void _expect_ym2612_write_operator(
u8 chan, u8 op, u8 baseReg, u8 data, const char* const file, const int line)
{
u8 part = REG_PART(chan);
u8 reg = baseReg + REG_OFFSET(chan) + (regOpIndex(op) * 4);
u8 reg = baseReg + REG_OFFSET(chan) + (YM_OP_REG_INDEX(op) * 4);

_expect_ym2612_write_reg(part, reg, data, file, line);
}
Expand All @@ -123,15 +112,15 @@ void _expect_ym2612_write_channel_any_data(
void _expect_ym2612_write_all_operators(
u8 chan, u8 baseReg, u8 data, const char* const file, const int line)
{
for (u8 op = 0; op < 4; op++) {
for (u8 op = YM_OP1; op <= YM_OP4; op++) {
_expect_ym2612_write_operator(chan, op, baseReg, data, file, line);
}
}

void _expect_ym2612_write_all_operators_any_data(
u8 chan, u8 baseReg, const char* const file, const int line)
{
for (u8 op = 0; op < 4; op++) {
for (u8 op = YM_OP1; op <= YM_OP4; op++) {
_expect_ym2612_write_operator_any_data(chan, op, baseReg, file, line);
}
}
Expand All @@ -145,3 +134,24 @@ void _expect_ym2612_note_off(u8 chan, const char* const file, const int line)
{
_expect_ym2612_write_reg(0, YM_KEY_ON_OFF, KEY_ON_OFF_CH_INDEX(chan), file, line);
}

void _expect_ym2612_write_frequency(
u8 chan, u16 msb, u16 lsb, const char* const file, const int line)
{
_expect_ym2612_write_channel(chan, YM_BASE_FREQ_MSB_BLK, msb, file, line);
_expect_ym2612_write_channel(chan, YM_BASE_FREQ_LSB, lsb, file, line);
}

void _expect_ym2612_write_frequency_any_data(u8 chan, const char* const file, const int line)
{
_expect_ym2612_write_channel_any_data(chan, YM_BASE_FREQ_MSB_BLK, file, line);
_expect_ym2612_write_channel_any_data(chan, YM_BASE_FREQ_LSB, file, line);
}

void _expect_ym2612_write_operator_volumes(
u8 chan, const u8* volumes, u8 count, const char* const file, const int line)
{
for (u8 op = 0; op < count; op++) {
_expect_ym2612_write_operator(chan, op, YM_BASE_TOTAL_LEVEL, volumes[op], file, line);
}
}
11 changes: 11 additions & 0 deletions tests/mocks/mock_ym2612.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,11 @@ void _expect_ym2612_write_all_operators_any_data(
u8 chan, u8 baseReg, const char* const file, const int line);
void _expect_ym2612_note_on(u8 chan, const char* const file, const int line);
void _expect_ym2612_note_off(u8 chan, const char* const file, const int line);
void _expect_ym2612_write_frequency(
u8 chan, u16 msb, u16 lsb, const char* const file, const int line);
void _expect_ym2612_write_frequency_any_data(u8 chan, const char* const file, const int line);
void _expect_ym2612_write_operator_volumes(
u8 chan, const u8* volumes, u8 count, const char* const file, const int line);

#define expect_ym2612_write_reg(part, reg, data) \
_expect_ym2612_write_reg(part, reg, data, __FILE__, __LINE__)
Expand All @@ -44,3 +49,9 @@ void _expect_ym2612_note_off(u8 chan, const char* const file, const int line);
_expect_ym2612_write_all_operators_any_data(chan, baseReg, __FILE__, __LINE__)
#define expect_ym2612_note_on(chan) _expect_ym2612_note_on(chan, __FILE__, __LINE__)
#define expect_ym2612_note_off(chan) _expect_ym2612_note_off(chan, __FILE__, __LINE__)
#define expect_ym2612_write_frequency(chan, msb, lsb) \
_expect_ym2612_write_frequency(chan, msb, lsb, __FILE__, __LINE__)
#define expect_ym2612_write_frequency_any_data(chan) \
_expect_ym2612_write_frequency_any_data(chan, __FILE__, __LINE__)
#define expect_ym2612_write_operator_volumes(chan, volumes, count) \
_expect_ym2612_write_operator_volumes(chan, volumes, count, __FILE__, __LINE__)
74 changes: 27 additions & 47 deletions tests/system/test_e2e.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
#include "mocks/mock_comm.h"
#include "mocks/mock_sgdk.h"
#include "mocks/mock_psg.h"
#include "ym2612_regs.h"
#include "ym2612_helper.h"

static const u8 TEST_CC_PAN = 10;
static const u8 TEST_CC_PORTAMENTO_TIME = 5;
Expand Down Expand Up @@ -55,8 +57,7 @@ void test_midi_note_on_event_sent_to_ym2612(void** state)
{
stub_everdrive_as_present();
stub_usb_receive_note_on(TEST_MIDI_CHANNEL_1, 48, 127);
expect_ym2612_write_channel(0, 0xA4, 0x1A);
expect_ym2612_write_channel(0, 0xA0, 0x84);
expect_ym2612_write_frequency(0, 0x1A, 0x84);
expect_ym2612_note_on(0);
midi_receiver_read();
}
Expand All @@ -68,14 +69,12 @@ void test_polyphonic_midi_sent_to_separate_ym2612_channels(void** state)
midi_receiver_read();

stub_usb_receive_note_on(TEST_MIDI_CHANNEL_1, 48, TEST_VELOCITY_MAX);
expect_ym2612_write_channel(0, 0xA4, 0x1A);
expect_ym2612_write_channel(0, 0xA0, 0x84);
expect_ym2612_write_frequency(0, 0x1A, 0x84);
expect_ym2612_note_on(0);
midi_receiver_read();

stub_usb_receive_note_on(TEST_MIDI_CHANNEL_1, 49, TEST_VELOCITY_MAX);
expect_ym2612_write_channel(1, 0xA4, 0x1A);
expect_ym2612_write_channel(1, 0xA0, 0xA9);
expect_ym2612_write_frequency(1, 0x1A, 0xA9);
expect_ym2612_note_on(1);
midi_receiver_read();
}
Expand Down Expand Up @@ -104,8 +103,7 @@ void test_general_midi_reset_sysex_stops_all_notes(void** state)

// FM note
stub_usb_receive_note_on(TEST_MIDI_CHANNEL_1, noteOnKey, TEST_VELOCITY_MAX);
expect_ym2612_write_channel(0, 0xA4, 0x1A);
expect_ym2612_write_channel(0, 0xA0, 0x84);
expect_ym2612_write_frequency(0, 0x1A, 0x84);
expect_ym2612_note_on(0);
midi_receiver_read();

Expand All @@ -120,12 +118,9 @@ void test_general_midi_reset_sysex_stops_all_notes(void** state)
for (u16 i = 0; i < sizeof(sysExGeneralMidiResetSequence); i++) {
stub_usb_receive_byte(sysExGeneralMidiResetSequence[i]);
}
expect_ym2612_note_off(0);
expect_ym2612_note_off(1);
expect_ym2612_note_off(2);
expect_ym2612_note_off(3);
expect_ym2612_note_off(4);
expect_ym2612_note_off(5);
for (u8 chan = 0; chan < MAX_FM_CHANS; chan++) {
expect_ym2612_note_off(chan);
}
expect_psg_attenuation(0, 0xF);
midi_receiver_read();
}
Expand Down Expand Up @@ -299,20 +294,17 @@ void test_midi_last_note_played_priority_respected_on_fm(void** state)
{
stub_everdrive_as_present();
stub_usb_receive_note_on(TEST_MIDI_CHANNEL_1, 48, 127);
expect_ym2612_write_channel(0, 0xA4, 0x1A);
expect_ym2612_write_channel(0, 0xA0, 0x84);
expect_ym2612_write_frequency(0, 0x1A, 0x84);
expect_ym2612_note_on(0);
midi_receiver_read();

stub_usb_receive_note_on(TEST_MIDI_CHANNEL_1, 50, 127);
expect_ym2612_write_channel(0, 0xA4, 0x1A);
expect_ym2612_write_channel(0, 0xA0, 0xD2);
expect_ym2612_write_frequency(0, 0x1A, 0xD2);
expect_ym2612_note_on(0);
midi_receiver_read();

stub_usb_receive_note_off(TEST_MIDI_CHANNEL_1, 50);
expect_ym2612_write_channel(0, 0xA4, 0x1A);
expect_ym2612_write_channel(0, 0xA0, 0x84);
expect_ym2612_write_frequency(0, 0x1A, 0x84);
expect_ym2612_note_on(0);
midi_receiver_read();
}
Expand All @@ -321,24 +313,19 @@ void test_midi_last_note_played_remembers_velocity_on_fm(void** state)
{
stub_everdrive_as_present();
stub_usb_receive_note_on(TEST_MIDI_CHANNEL_1, 48, 100);
expect_ym2612_write_operator(0, 0, 0x40, 0x27);
expect_ym2612_write_operator(0, 1, 0x40, 0x04);
expect_ym2612_write_operator(0, 2, 0x40, 0x24);
expect_ym2612_write_operator(0, 3, 0x40, 0x04);
expect_ym2612_write_channel(0, 0xA4, 0x1A);
expect_ym2612_write_channel(0, 0xA0, 0x84);
const u8 volumes[] = { 0x27, 0x04, 0x24, 0x04 };
expect_ym2612_write_operator_volumes(0, volumes, 4);
expect_ym2612_write_frequency(0, 0x1A, 0x84);
expect_ym2612_note_on(0);
midi_receiver_read();

stub_usb_receive_note_on(TEST_MIDI_CHANNEL_1, 50, 100);
expect_ym2612_write_channel(0, 0xA4, 0x1A);
expect_ym2612_write_channel(0, 0xA0, 0xD2);
expect_ym2612_write_frequency(0, 0x1A, 0xD2);
expect_ym2612_note_on(0);
midi_receiver_read();

stub_usb_receive_note_off(TEST_MIDI_CHANNEL_1, 50);
expect_ym2612_write_channel(0, 0xA4, 0x1A);
expect_ym2612_write_channel(0, 0xA0, 0x84);
expect_ym2612_write_frequency(0, 0x1A, 0x84);
expect_ym2612_note_on(0);
midi_receiver_read();
}
Expand All @@ -347,14 +334,12 @@ void test_midi_last_note_played_cleared_when_released_on_fm(void** state)
{
stub_everdrive_as_present();
stub_usb_receive_note_on(TEST_MIDI_CHANNEL_1, 48, 127);
expect_ym2612_write_channel(0, 0xA4, 0x1A);
expect_ym2612_write_channel(0, 0xA0, 0x84);
expect_ym2612_write_frequency(0, 0x1A, 0x84);
expect_ym2612_note_on(0);
midi_receiver_read();

stub_usb_receive_note_on(TEST_MIDI_CHANNEL_1, 50, 127);
expect_ym2612_write_channel(0, 0xA4, 0x1A);
expect_ym2612_write_channel(0, 0xA0, 0xD2);
expect_ym2612_write_frequency(0, 0x1A, 0xD2);
expect_ym2612_note_on(0);
midi_receiver_read();

Expand All @@ -370,10 +355,10 @@ void test_midi_changing_program_retains_pan(void** state)
{
stub_everdrive_as_present();

const u8 chan = 0;
const u8 chan = YM_CH1;

stub_usb_receive_cc(TEST_MIDI_CHANNEL_1, TEST_CC_PAN, 0); // left
expect_ym2612_write_channel(chan, 0xB4, 0x80); // pan, alg, fb
expect_ym2612_write_channel(chan, YM_BASE_STEREO_AMS_PMS, 0x80); // pan, alg, fb
midi_receiver_read();

stub_usb_receive_program(TEST_MIDI_CHANNEL_1, 1);
Expand Down Expand Up @@ -414,13 +399,11 @@ void test_midi_changing_program_retains_volume(void** state)
{
stub_everdrive_as_present();

const u8 chan = 0;
const u8 chan = YM_CH1;

stub_usb_receive_cc(TEST_MIDI_CHANNEL_1, TEST_CC_VOLUME, 0);
expect_ym2612_write_channel(chan, 0x40, 0x27);
expect_ym2612_write_channel(chan, 0x48, 0x04);
expect_ym2612_write_channel(chan, 0x44, 0x24);
expect_ym2612_write_channel(chan, 0x4C, 0x7F); // output operator mute
expect_ym2612_write_operator_volumes(
chan, ((u8[]) { 0x27, 0x04, 0x24, YM_TOTAL_LEVEL_SILENCE }), 4);
midi_receiver_read();

stub_usb_receive_program(TEST_MIDI_CHANNEL_1, 1);
Expand Down Expand Up @@ -467,21 +450,18 @@ void test_midi_portamento_glides_note(void** state)
midi_receiver_read();

stub_usb_receive_note_on(TEST_MIDI_CHANNEL_1, 48, 127);
expect_ym2612_write_channel(0, 0xA4, 0x1A);
expect_ym2612_write_channel(0, 0xA0, 0x84);
expect_ym2612_write_frequency(0, 0x1A, 0x84);
expect_ym2612_note_on(0);
midi_receiver_read();

stub_usb_receive_note_on(TEST_MIDI_CHANNEL_1, 58, 127);
midi_receiver_read();

expect_ym2612_write_channel(0, 0xA4, 0x1A);
expect_ym2612_write_channel(0, 0xA0, 0x87);
expect_ym2612_write_frequency(0, 0x1A, 0x87);
scheduler_vsync();
scheduler_tick();
for (u16 i = 0; i < 99; i++) {
expect_ym2612_write_channel_any_data(0, 0xA4);
expect_ym2612_write_channel_any_data(0, 0xA0);
expect_ym2612_write_frequency_any_data(0);
scheduler_vsync();
scheduler_tick();
}
Expand Down

0 comments on commit f244a18

Please sign in to comment.