Skip to content

Commit

Permalink
Use MIDI constants from midi.h
Browse files Browse the repository at this point in the history
We don't need to reproduce literal constants in E2E tests. A calculated define should probably be replicated though.
  • Loading branch information
rhargreaves committed Feb 19, 2025
1 parent 2de8546 commit 67c0d57
Showing 1 changed file with 54 additions and 63 deletions.
117 changes: 54 additions & 63 deletions tests/system/test_e2e.c
Original file line number Diff line number Diff line change
Expand Up @@ -13,27 +13,18 @@
#include "ym2612_regs.h"
#include "ym2612_helper.h"

static const u8 TEST_CC_PAN = 10;
static const u8 TEST_CC_PORTAMENTO_TIME = 5;
static const u8 TEST_CC_VOLUME = 7;
static const u8 TEST_CC_ALGORITHM = 14;
static const u8 TEST_CC_PORTAMENTO_ON = 65;
static const u8 TEST_CC_ENABLE_DAC = 78;
static const u8 TEST_CC_SPECIAL_MODE = 80;
static const u8 TEST_CC_POLYPHONIC = 84;
static const u8 TEST_CC_DEVICE_SELECT = 86;

static const u8 TEST_POLYPHONIC_ON = 0x7F;
static const u8 TEST_SPECIAL_MODE_ON = 64;
static const u8 TEST_DEVICE_SELECT_PSG = 64;
static const u8 TEST_MIDI_CHANNEL_PSG_1 = 6;
static const u8 TEST_MIDI_CHANNEL_1 = 0;
static const u8 TEST_MIDI_CHANNEL_3 = 2;
static const u8 TEST_MIDI_CHANNEL_6 = 5;
static const u8 TEST_MIDI_CHANNEL_11 = 10;

static const u8 TEST_VOLUME_MAX = 127;
static const u8 TEST_VELOCITY_MAX = 127;
static const u8 POLYPHONIC_ON = 0x7F;
static const u8 SPECIAL_MODE_ON = 64;
static const u8 DEVICE_SELECT_PSG = 64;

static const u8 MIDI_CHANNEL_PSG_1 = 6;
static const u8 MIDI_CHANNEL_1 = 0;
static const u8 MIDI_CHANNEL_3 = 2;
static const u8 MIDI_CHANNEL_6 = 5;
static const u8 MIDI_CHANNEL_11 = 10;

static const u8 MIDI_VOLUME_MAX = 127;
static const u8 MIDI_VELOCITY_MAX = 127;

int test_e2e_setup(void** state)
{
Expand All @@ -56,7 +47,7 @@ int test_e2e_setup(void** state)
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, TEST_VELOCITY_MAX);
stub_usb_receive_note_on(MIDI_CHANNEL_1, 48, MIDI_VELOCITY_MAX);
expect_ym2612_write_frequency(YM_CH1, 0x1A84);
expect_ym2612_note_on(YM_CH1);
midi_receiver_read();
Expand All @@ -65,15 +56,15 @@ void test_midi_note_on_event_sent_to_ym2612(void** state)
void test_polyphonic_midi_sent_to_separate_ym2612_channels(void** state)
{
stub_everdrive_as_present();
stub_usb_receive_cc(TEST_MIDI_CHANNEL_1, TEST_CC_POLYPHONIC, TEST_POLYPHONIC_ON);
stub_usb_receive_cc(MIDI_CHANNEL_1, CC_GENMDM_POLYPHONIC_MODE, POLYPHONIC_ON);
midi_receiver_read();

stub_usb_receive_note_on(TEST_MIDI_CHANNEL_1, 48, TEST_VELOCITY_MAX);
stub_usb_receive_note_on(MIDI_CHANNEL_1, 48, MIDI_VELOCITY_MAX);
expect_ym2612_write_frequency(YM_CH1, 0x1A84);
expect_ym2612_note_on(YM_CH1);
midi_receiver_read();

stub_usb_receive_note_on(TEST_MIDI_CHANNEL_1, 49, TEST_VELOCITY_MAX);
stub_usb_receive_note_on(MIDI_CHANNEL_1, 49, MIDI_VELOCITY_MAX);
expect_ym2612_write_frequency(YM_CH2, 0x1AA9);
expect_ym2612_note_on(YM_CH2);
midi_receiver_read();
Expand All @@ -82,7 +73,7 @@ void test_polyphonic_midi_sent_to_separate_ym2612_channels(void** state)
void test_psg_audible_if_note_on_event_triggered(void** state)
{
stub_everdrive_as_present();
stub_usb_receive_note_on(TEST_MIDI_CHANNEL_PSG_1, 60, TEST_VELOCITY_MAX);
stub_usb_receive_note_on(MIDI_CHANNEL_PSG_1, 60, MIDI_VELOCITY_MAX);
expect_any_psg_tone();
expect_any_psg_attenuation();
midi_receiver_read();
Expand All @@ -91,7 +82,7 @@ void test_psg_audible_if_note_on_event_triggered(void** state)
void test_psg_not_audible_if_midi_channel_volume_set_and_there_is_no_note_on_event(void** state)
{
stub_everdrive_as_present();
stub_usb_receive_cc(TEST_MIDI_CHANNEL_PSG_1, TEST_CC_VOLUME, TEST_VOLUME_MAX);
stub_usb_receive_cc(MIDI_CHANNEL_PSG_1, CC_VOLUME, MIDI_VOLUME_MAX);
midi_receiver_read();
}

Expand All @@ -102,13 +93,13 @@ void test_general_midi_reset_sysex_stops_all_notes(void** state)
const u8 noteOnKey = 48;

// FM note
stub_usb_receive_note_on(TEST_MIDI_CHANNEL_1, noteOnKey, TEST_VELOCITY_MAX);
stub_usb_receive_note_on(MIDI_CHANNEL_1, noteOnKey, MIDI_VELOCITY_MAX);
expect_ym2612_write_frequency(YM_CH1, 0x1A84);
expect_ym2612_note_on(YM_CH1);
midi_receiver_read();

// PSG note
stub_usb_receive_note_on(TEST_MIDI_CHANNEL_PSG_1, noteOnKey, TEST_VELOCITY_MAX);
stub_usb_receive_note_on(MIDI_CHANNEL_PSG_1, noteOnKey, MIDI_VELOCITY_MAX);
expect_psg_tone(PSG_CH1, 0x357);
expect_psg_attenuation(PSG_CH1, 0);
midi_receiver_read();
Expand Down Expand Up @@ -145,10 +136,10 @@ void test_remap_midi_channel_1_to_psg_channel_1()
remapChannel(MIDI_CHANNEL_UNASSIGNED, DEVICE_FM_1);
midi_receiver_read();

remapChannel(TEST_MIDI_CHANNEL_1, DEVICE_PSG_1);
remapChannel(MIDI_CHANNEL_1, DEVICE_PSG_1);
midi_receiver_read();

stub_usb_receive_note_on(TEST_MIDI_CHANNEL_1, 48, TEST_VELOCITY_MAX);
stub_usb_receive_note_on(MIDI_CHANNEL_1, 48, MIDI_VELOCITY_MAX);
expect_psg_tone(PSG_CH1, 0x357);
expect_psg_attenuation(PSG_CH1, 0);
midi_receiver_read();
Expand All @@ -157,13 +148,13 @@ void test_remap_midi_channel_1_to_psg_channel_1()
void test_set_device_for_midi_channel_1_to_psg()
{
stub_everdrive_as_present();
stub_usb_receive_cc(TEST_MIDI_CHANNEL_1, TEST_CC_POLYPHONIC, TEST_POLYPHONIC_ON);
stub_usb_receive_cc(MIDI_CHANNEL_1, CC_GENMDM_POLYPHONIC_MODE, POLYPHONIC_ON);
midi_receiver_read();

stub_usb_receive_cc(TEST_MIDI_CHANNEL_1, TEST_CC_DEVICE_SELECT, TEST_DEVICE_SELECT_PSG);
stub_usb_receive_cc(MIDI_CHANNEL_1, CC_DEVICE_SELECT, DEVICE_SELECT_PSG);
midi_receiver_read();

stub_usb_receive_note_on(TEST_MIDI_CHANNEL_1, 48, TEST_VELOCITY_MAX);
stub_usb_receive_note_on(MIDI_CHANNEL_1, 48, MIDI_VELOCITY_MAX);
expect_psg_tone(PSG_CH1, 0x357);
expect_psg_attenuation(PSG_CH1, 0);
midi_receiver_read();
Expand Down Expand Up @@ -201,7 +192,7 @@ void test_loads_psg_envelope()
}
midi_receiver_read();

stub_usb_receive_note_on(TEST_MIDI_CHANNEL_PSG_1, 60, TEST_VELOCITY_MAX);
stub_usb_receive_note_on(MIDI_CHANNEL_PSG_1, 60, MIDI_VELOCITY_MAX);
expect_psg_tone(PSG_CH1, 0x17c);
expect_psg_attenuation(PSG_CH1, 6);
midi_receiver_read();
Expand All @@ -210,19 +201,19 @@ void test_loads_psg_envelope()
void test_enables_ch3_special_mode(void** state)
{
stub_everdrive_as_present();
stub_usb_receive_cc(TEST_MIDI_CHANNEL_1, TEST_CC_SPECIAL_MODE, TEST_SPECIAL_MODE_ON);
stub_usb_receive_cc(MIDI_CHANNEL_1, CC_GENMDM_CH3_SPECIAL_MODE, SPECIAL_MODE_ON);
expect_ym2612_write_reg(0, YM_CH3_MODE, 0x40);
midi_receiver_read();
}

void test_sets_separate_ch3_operator_frequencies(void** state)
{
stub_everdrive_as_present();
stub_usb_receive_cc(TEST_MIDI_CHANNEL_1, TEST_CC_SPECIAL_MODE, TEST_SPECIAL_MODE_ON);
stub_usb_receive_cc(MIDI_CHANNEL_1, CC_GENMDM_CH3_SPECIAL_MODE, SPECIAL_MODE_ON);
expect_ym2612_write_reg(0, YM_CH3_MODE, 0x40);
midi_receiver_read();

stub_usb_receive_cc(TEST_MIDI_CHANNEL_3, TEST_CC_ALGORITHM, 0x7F); // alg 7
stub_usb_receive_cc(MIDI_CHANNEL_3, CC_GENMDM_FM_ALGORITHM, 0x7F); // alg 7
expect_ym2612_write_reg(0, 0xB2, 0x7);
midi_receiver_read();

Expand All @@ -231,7 +222,7 @@ void test_sets_separate_ch3_operator_frequencies(void** state)
const u8 tlValues[] = { 0x27, 0x04, 0x24 };

for (u8 op = 0; op < 3; op++) {
stub_usb_receive_note_on(TEST_MIDI_CHANNEL_11 + op, 60, 120);
stub_usb_receive_note_on(MIDI_CHANNEL_11 + op, 60, 120);
expect_ym2612_write_reg(0, upperRegs[op], 0x22);
expect_ym2612_write_reg(0, lowerRegs[op], 0x84);
expect_ym2612_write_operator(CH3_SPECIAL_MODE, op, 0x40, tlValues[op]);
Expand All @@ -242,21 +233,21 @@ void test_sets_separate_ch3_operator_frequencies(void** state)
void test_pitch_bends_ch3_special_mode_operators(void** state)
{
stub_everdrive_as_present();
stub_usb_receive_cc(TEST_MIDI_CHANNEL_1, TEST_CC_SPECIAL_MODE, TEST_SPECIAL_MODE_ON);
stub_usb_receive_cc(MIDI_CHANNEL_1, CC_GENMDM_CH3_SPECIAL_MODE, SPECIAL_MODE_ON);
expect_ym2612_write_reg(0, YM_CH3_MODE, 0x40);
midi_receiver_read();

stub_usb_receive_cc(TEST_MIDI_CHANNEL_3, TEST_CC_ALGORITHM, 0x7F); // alg 7
stub_usb_receive_cc(MIDI_CHANNEL_3, CC_GENMDM_FM_ALGORITHM, 0x7F); // alg 7
expect_ym2612_write_reg(0, YM_REG(YM_BASE_ALGORITHM_FEEDBACK, YM_CH3), 0x7);
midi_receiver_read();

stub_usb_receive_note_on(TEST_MIDI_CHANNEL_11, 60, 120);
stub_usb_receive_note_on(MIDI_CHANNEL_11, 60, 120);
expect_ym2612_write_reg(0, YM_CH3SM_OP1_FREQ_MSB_BLK, 0x22);
expect_ym2612_write_reg(0, YM_CH3SM_OP1_FREQ_LSB, 0x84);
expect_ym2612_write_operator_any_data(CH3_SPECIAL_MODE, 0, 0x40);
midi_receiver_read();

stub_usb_receive_pitch_bend(TEST_MIDI_CHANNEL_11, 0x4000);
stub_usb_receive_pitch_bend(MIDI_CHANNEL_11, 0x4000);
expect_ym2612_write_reg(0, YM_CH3SM_OP1_FREQ_MSB_BLK, 0x22);
expect_ym2612_write_reg(0, YM_CH3SM_OP1_FREQ_LSB, 0xD2);
midi_receiver_read();
Expand All @@ -277,11 +268,11 @@ void test_write_directly_to_ym2612_regs_via_sysex(void** state)
void test_plays_pcm_sample(void** state)
{
stub_everdrive_as_present();
stub_usb_receive_cc(TEST_MIDI_CHANNEL_1, TEST_CC_ENABLE_DAC, 127);
stub_usb_receive_cc(MIDI_CHANNEL_1, CC_GENMDM_ENABLE_DAC, 127);
expect_ym2612_write_reg(0, YM_DAC_ENABLE, 0x80);
midi_receiver_read();

stub_usb_receive_note_on(TEST_MIDI_CHANNEL_6, 60, TEST_VOLUME_MAX);
stub_usb_receive_note_on(MIDI_CHANNEL_6, 60, MIDI_VOLUME_MAX);
expect_any(__wrap_SND_PCM_startPlay, sample);
expect_any(__wrap_SND_PCM_startPlay, len);
expect_value(__wrap_SND_PCM_startPlay, rate, 1);
Expand All @@ -293,17 +284,17 @@ void test_plays_pcm_sample(void** state)
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, TEST_VELOCITY_MAX);
stub_usb_receive_note_on(MIDI_CHANNEL_1, 48, MIDI_VELOCITY_MAX);
expect_ym2612_write_frequency(YM_CH1, 0x1A84);
expect_ym2612_note_on(YM_CH1);
midi_receiver_read();

stub_usb_receive_note_on(TEST_MIDI_CHANNEL_1, 50, TEST_VELOCITY_MAX);
stub_usb_receive_note_on(MIDI_CHANNEL_1, 50, MIDI_VELOCITY_MAX);
expect_ym2612_write_frequency(YM_CH1, 0x1AD2);
expect_ym2612_note_on(YM_CH1);
midi_receiver_read();

stub_usb_receive_note_off(TEST_MIDI_CHANNEL_1, 50);
stub_usb_receive_note_off(MIDI_CHANNEL_1, 50);
expect_ym2612_write_frequency(YM_CH1, 0x1A84);
expect_ym2612_note_on(YM_CH1);
midi_receiver_read();
Expand All @@ -312,18 +303,18 @@ void test_midi_last_note_played_priority_respected_on_fm(void** state)
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);
stub_usb_receive_note_on(MIDI_CHANNEL_1, 48, 100);
expect_ym2612_write_operator_volumes(0, ((u8[]) { 0x27, 0x04, 0x24, 0x04 }), 4);
expect_ym2612_write_frequency(YM_CH1, 0x1A84);
expect_ym2612_note_on(YM_CH1);
midi_receiver_read();

stub_usb_receive_note_on(TEST_MIDI_CHANNEL_1, 50, 100);
stub_usb_receive_note_on(MIDI_CHANNEL_1, 50, 100);
expect_ym2612_write_frequency(YM_CH1, 0x1AD2);
expect_ym2612_note_on(YM_CH1);
midi_receiver_read();

stub_usb_receive_note_off(TEST_MIDI_CHANNEL_1, 50);
stub_usb_receive_note_off(MIDI_CHANNEL_1, 50);
expect_ym2612_write_frequency(YM_CH1, 0x1A84);
expect_ym2612_note_on(YM_CH1);
midi_receiver_read();
Expand All @@ -332,20 +323,20 @@ void test_midi_last_note_played_remembers_velocity_on_fm(void** state)
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, TEST_VELOCITY_MAX);
stub_usb_receive_note_on(MIDI_CHANNEL_1, 48, MIDI_VELOCITY_MAX);
expect_ym2612_write_frequency(YM_CH1, 0x1A84);
expect_ym2612_note_on(YM_CH1);
midi_receiver_read();

stub_usb_receive_note_on(TEST_MIDI_CHANNEL_1, 50, TEST_VELOCITY_MAX);
stub_usb_receive_note_on(MIDI_CHANNEL_1, 50, MIDI_VELOCITY_MAX);
expect_ym2612_write_frequency(YM_CH1, 0x1AD2);
expect_ym2612_note_on(YM_CH1);
midi_receiver_read();

stub_usb_receive_note_off(TEST_MIDI_CHANNEL_1, 48);
stub_usb_receive_note_off(MIDI_CHANNEL_1, 48);
midi_receiver_read();

stub_usb_receive_note_off(TEST_MIDI_CHANNEL_1, 50);
stub_usb_receive_note_off(MIDI_CHANNEL_1, 50);
expect_ym2612_note_off(YM_CH1);
midi_receiver_read();
}
Expand All @@ -356,11 +347,11 @@ void test_midi_changing_program_retains_pan(void** state)

const u8 chan = YM_CH1;

stub_usb_receive_cc(TEST_MIDI_CHANNEL_1, TEST_CC_PAN, 0); // left
stub_usb_receive_cc(MIDI_CHANNEL_1, CC_PAN, 0); // left
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);
stub_usb_receive_program(MIDI_CHANNEL_1, 1);
expect_ym2612_write_channel_any_data(chan, 0xB0);
expect_ym2612_write_channel(chan, 0xB4, 0x80); // pan, alg, fb
expect_ym2612_write_channel_any_data(chan, 0x30);
Expand Down Expand Up @@ -400,12 +391,12 @@ void test_midi_changing_program_retains_volume(void** state)

const u8 chan = YM_CH1;

stub_usb_receive_cc(TEST_MIDI_CHANNEL_1, TEST_CC_VOLUME, 0);
stub_usb_receive_cc(MIDI_CHANNEL_1, CC_VOLUME, 0);
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);
stub_usb_receive_program(MIDI_CHANNEL_1, 1);
expect_ym2612_write_channel_any_data(chan, 0xB0);
expect_ym2612_write_channel_any_data(chan, 0xB4);
expect_ym2612_write_channel_any_data(chan, 0x30);
Expand Down Expand Up @@ -443,17 +434,17 @@ void test_midi_portamento_glides_note(void** state)
{
stub_everdrive_as_present();

stub_usb_receive_cc(TEST_MIDI_CHANNEL_1, TEST_CC_PORTAMENTO_TIME, 95);
stub_usb_receive_cc(MIDI_CHANNEL_1, CC_PORTAMENTO_TIME_MSB, 95);
midi_receiver_read();
stub_usb_receive_cc(TEST_MIDI_CHANNEL_1, TEST_CC_PORTAMENTO_ON, 0x7F);
stub_usb_receive_cc(MIDI_CHANNEL_1, CC_PORTAMENTO_ENABLE, 0x7F);
midi_receiver_read();

stub_usb_receive_note_on(TEST_MIDI_CHANNEL_1, 48, TEST_VELOCITY_MAX);
stub_usb_receive_note_on(MIDI_CHANNEL_1, 48, MIDI_VELOCITY_MAX);
expect_ym2612_write_frequency(YM_CH1, 0x1A84);
expect_ym2612_note_on(YM_CH1);
midi_receiver_read();

stub_usb_receive_note_on(TEST_MIDI_CHANNEL_1, 58, TEST_VELOCITY_MAX);
stub_usb_receive_note_on(MIDI_CHANNEL_1, 58, MIDI_VELOCITY_MAX);
midi_receiver_read();

expect_ym2612_write_frequency(YM_CH1, 0x1A87);
Expand Down

0 comments on commit 67c0d57

Please sign in to comment.