Skip to content

Commit

Permalink
More magic number banishment ✨
Browse files Browse the repository at this point in the history
  • Loading branch information
rhargreaves committed Feb 19, 2025
1 parent e5273aa commit 4956a0e
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 120 deletions.
25 changes: 25 additions & 0 deletions tests/test_helpers.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,21 @@
#define MIDI_VOLUME_MAX 127
#define MIDI_VELOCITY_MAX 127

#define MIDI_PITCH_A2 45
#define MIDI_PITCH_C3 48
#define MIDI_PITCH_C4 60
#define MIDI_PITCH_CS4 61
#define MIDI_PITCH_AS6 94
#define MIDI_PITCH_B6 95

#define SYSEX_DYNAMIC_AUTO 0x02
#define SYSEX_DYNAMIC_ENABLED 0x01
#define SYSEX_DYNAMIC_DISABLED 0x00
#define SYSEX_NON_GENERAL_MIDI_CCS_ENABLED 0x01
#define SYSEX_NON_GENERAL_MIDI_CCS_DISABLED 0x00

#define UNASSIGNED_MIDI_CHANNEL 0x7F

// --- YM2612 ---
#define YM_OP_REG_INDEX(op) ((op) == 1 ? 2 : ((op) == 2 ? 1 : (op)))
#define YM_REG(baseReg, channel) (baseReg + (channel % 3))
Expand Down Expand Up @@ -45,8 +60,18 @@
#define YM_OP3 2
#define YM_OP4 3

#define SYNTH_NTSC_C 0x284
#define SYNTH_NTSC_AS 1146
#define SYNTH_PAL_C 649

// --- PSG ---
#define PSG_CH1 0
#define PSG_CH2 1
#define PSG_CH3 2
#define PSG_NOISE_CH4 3

#define TONE_NTSC_AS4 479
#define TONE_NTSC_C4 427
#define TONE_NTSC_CS4 403
#define TONE_NTSC_DS4 380
#define TONE_NTSC_A2 1016
40 changes: 17 additions & 23 deletions tests/unit/test_midi.c
Original file line number Diff line number Diff line change
Expand Up @@ -68,25 +68,25 @@ int test_midi_setup(UNUSED void** state)

void test_midi_polyphonic_mode_returns_state(UNUSED void** state)
{
__real_midi_cc(0, CC_GENMDM_POLYPHONIC_MODE, 127);
__real_midi_cc(MIDI_CHANNEL_1, CC_GENMDM_POLYPHONIC_MODE, 127);

assert_true(__real_midi_dynamic_mode());

__real_midi_cc(0, CC_GENMDM_POLYPHONIC_MODE, 0);
__real_midi_cc(MIDI_CHANNEL_1, CC_GENMDM_POLYPHONIC_MODE, 0);
}

void test_midi_sets_all_notes_off(UNUSED void** state)
{
expect_value(__wrap_synth_noteOff, channel, 0);
expect_synth_noteOff(0);

__real_midi_cc(0, CC_ALL_NOTES_OFF, 0);
__real_midi_cc(MIDI_CHANNEL_1, CC_ALL_NOTES_OFF, 0);
}

void test_midi_sets_all_sound_off(UNUSED void** state)
{
expect_value(__wrap_synth_noteOff, channel, 0);
expect_synth_noteOff(0);

__real_midi_cc(0, CC_ALL_SOUND_OFF, 0);
__real_midi_cc(MIDI_CHANNEL_1, CC_ALL_SOUND_OFF, 0);
}

void test_midi_sets_unknown_CC(UNUSED void** state)
Expand All @@ -97,37 +97,33 @@ void test_midi_sets_unknown_CC(UNUSED void** state)
u8 expectedValue = 0x50;

expect_log_warn("Ch %d: CC 0x%02X 0x%02X?");
__real_midi_cc(0, expectedController, expectedValue);
__real_midi_cc(MIDI_CHANNEL_1, expectedController, expectedValue);
}

void test_midi_ignores_sustain_pedal_cc(UNUSED void** state)
{
mock_log_enable_checks();

u8 cc = 64;

__real_midi_cc(0, cc, 1);
__real_midi_cc(MIDI_CHANNEL_1, CC_SUSTAIN_PEDAL, 1);
}

void test_midi_ignores_expression_cc(UNUSED void** state)
{
mock_log_enable_checks();

u8 cc = 11;

__real_midi_cc(0, cc, 1);
__real_midi_cc(MIDI_CHANNEL_1, CC_EXPRESSION, 1);
}

void test_midi_ignores_sysex_nrpn_ccs(UNUSED void** state)
{
mock_log_enable_checks();

__real_midi_cc(0, 6, 1);
__real_midi_cc(0, 38, 1);
__real_midi_cc(0, 98, 1);
__real_midi_cc(0, 99, 1);
__real_midi_cc(0, 100, 1);
__real_midi_cc(0, 101, 1);
__real_midi_cc(MIDI_CHANNEL_1, CC_DATA_ENTRY_MSB, 1);
__real_midi_cc(MIDI_CHANNEL_1, CC_DATA_ENTRY_LSB, 1);
__real_midi_cc(MIDI_CHANNEL_1, CC_NRPN_LSB, 1);
__real_midi_cc(MIDI_CHANNEL_1, CC_NRPN_MSB, 1);
__real_midi_cc(MIDI_CHANNEL_1, CC_RPN_LSB, 1);
__real_midi_cc(MIDI_CHANNEL_1, CC_RPN_MSB, 1);
}

void test_midi_shows_fm_parameter_ui(UNUSED void** state)
Expand Down Expand Up @@ -157,8 +153,7 @@ void test_midi_hides_fm_parameter_ui(UNUSED void** state)
void test_midi_resets_fm_values_to_defaults(UNUSED void** state)
{
for (u8 ch = 0; ch < 6; ch++) {
expect_value(__wrap_synth_volume, channel, ch);
expect_value(__wrap_synth_volume, volume, 60);
expect_synth_volume(ch, 60);

__real_midi_cc(ch, CC_VOLUME, 60);

Expand All @@ -169,8 +164,7 @@ void test_midi_resets_fm_values_to_defaults(UNUSED void** state)
expect_any(__wrap_synth_init, defaultPreset);

for (u8 ch = 0; ch < 6; ch++) {
expect_value(__wrap_synth_volume, channel, ch);
expect_value(__wrap_synth_volume, volume, 127);
expect_synth_volume(ch, 127);
}

__real_midi_reset();
Expand Down
27 changes: 1 addition & 26 deletions tests/unit/test_midi.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,32 +11,7 @@
#include "mocks/mock_log.h"
#include "mocks/mock_comm.h"
#include "mocks/mock_psg.h"

#define MIDI_PITCH_A2 45
#define MIDI_PITCH_C3 48
#define MIDI_PITCH_C4 60
#define MIDI_PITCH_CS4 61
#define MIDI_PITCH_AS6 94
#define MIDI_PITCH_B6 95

#define TONE_NTSC_AS4 479
#define TONE_NTSC_C4 427
#define TONE_NTSC_CS4 403
#define TONE_NTSC_DS4 380
#define TONE_NTSC_A2 1016

#define SYNTH_NTSC_C 0x284
#define SYNTH_NTSC_AS 1146
#define SYNTH_PAL_C 649

#define SYSEX_DYNAMIC_AUTO 0x02
#define SYSEX_DYNAMIC_ENABLED 0x01
#define SYSEX_DYNAMIC_DISABLED 0x00
#define SYSEX_NON_GENERAL_MIDI_CCS_ENABLED 0x01
#define SYSEX_NON_GENERAL_MIDI_CCS_DISABLED 0x00

#define UNASSIGNED_MIDI_CHANNEL 0x7F

#include "test_helpers.h"
int test_midi_setup(UNUSED void** state);
void test_midi_polyphonic_mode_returns_state(UNUSED void** state);
void test_midi_sets_all_sound_off(UNUSED void** state);
Expand Down
107 changes: 36 additions & 71 deletions tests/unit/test_synth.c
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ static void set_initial_registers(void)
}

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

const FmChannel M_BANK_0_INST_0_GRANDPIANO = { 0, 0, 3, 0, 0, 0, 0,
Expand Down Expand Up @@ -285,36 +285,18 @@ void test_synth_sets_preset(UNUSED void** state)
{ { 1, 1, 31, 2, 0, 1, 0, 1, 6, 28, 0 }, { 1, 3, 31, 2, 0, 0, 0, 1, 7, 33, 0 },
{ 1, 5, 31, 3, 0, 1, 0, 0, 2, 30, 0 }, { 1, 7, 31, 0, 6, 0, 4, 6, 7, 6, 0 } } };

expect_ym2612_write_channel_any_data(chan, 0xB0);
expect_ym2612_write_channel_any_data(chan, 0xB4);
expect_ym2612_write_channel_any_data(chan, 0x30);
expect_ym2612_write_channel_any_data(chan, 0x50);
expect_ym2612_write_channel_any_data(chan, 0x60);
expect_ym2612_write_channel_any_data(chan, 0x70);
expect_ym2612_write_channel_any_data(chan, 0x80);
expect_ym2612_write_channel_any_data(chan, 0x40);
expect_ym2612_write_channel_any_data(chan, 0x90);
expect_ym2612_write_channel_any_data(chan, 0x38);
expect_ym2612_write_channel_any_data(chan, 0x58);
expect_ym2612_write_channel_any_data(chan, 0x68);
expect_ym2612_write_channel_any_data(chan, 0x78);
expect_ym2612_write_channel_any_data(chan, 0x88);
expect_ym2612_write_channel_any_data(chan, 0x48);
expect_ym2612_write_channel_any_data(chan, 0x98);
expect_ym2612_write_channel_any_data(chan, 0x34);
expect_ym2612_write_channel_any_data(chan, 0x54);
expect_ym2612_write_channel_any_data(chan, 0x64);
expect_ym2612_write_channel_any_data(chan, 0x74);
expect_ym2612_write_channel_any_data(chan, 0x84);
expect_ym2612_write_channel_any_data(chan, 0x44);
expect_ym2612_write_channel_any_data(chan, 0x94);
expect_ym2612_write_channel_any_data(chan, 0x3C);
expect_ym2612_write_channel_any_data(chan, 0x5C);
expect_ym2612_write_channel_any_data(chan, 0x6C);
expect_ym2612_write_channel_any_data(chan, 0x7C);
expect_ym2612_write_channel_any_data(chan, 0x8C);
expect_ym2612_write_channel_any_data(chan, 0x4C);
expect_ym2612_write_channel_any_data(chan, 0x9C);
expect_ym2612_write_channel_any_data(chan, YM_BASE_ALGORITHM_FEEDBACK);
expect_ym2612_write_channel_any_data(chan, YM_BASE_STEREO_AMS_PMS);

for (u8 op = YM_OP1; op <= YM_OP4; op++) {
expect_ym2612_write_channel_any_data(chan, YM_REG3(YM_BASE_MULTIPLE_DETUNE, op));
expect_ym2612_write_channel_any_data(chan, YM_REG3(YM_BASE_ATTACK_RATE_SCALING_RATE, op));
expect_ym2612_write_channel_any_data(chan, YM_REG3(YM_BASE_DECAY_RATE_AM_ENABLE, op));
expect_ym2612_write_channel_any_data(chan, YM_REG3(YM_BASE_SUSTAIN_RATE, op));
expect_ym2612_write_channel_any_data(chan, YM_REG3(YM_BASE_RELEASE_RATE_SUSTAIN_LEVEL, op));
expect_ym2612_write_channel_any_data(chan, YM_REG3(YM_BASE_TOTAL_LEVEL, op));
expect_ym2612_write_channel_any_data(chan, YM_REG3(YM_BASE_SSG_EG, op));
}

__real_synth_preset(chan, &M_BANK_0_INST_7_CLAVINET);
}
Expand All @@ -323,43 +305,25 @@ void test_synth_sets_preset_retaining_pan(UNUSED void** state)
{
const u8 chan = 0;

expect_ym2612_write_channel(chan, 0xB4, 0x80);
expect_ym2612_write_channel(chan, YM_BASE_STEREO_AMS_PMS, 0x80);
__real_synth_stereo(chan, STEREO_MODE_LEFT);

const FmChannel M_BANK_0_INST_7_CLAVINET = { 1, 7, 3, 0, 0, 0, 0,
{ { 1, 1, 31, 2, 0, 1, 0, 1, 6, 28, 0 }, { 1, 3, 31, 2, 0, 0, 0, 1, 7, 33, 0 },
{ 1, 5, 31, 3, 0, 1, 0, 0, 2, 30, 0 }, { 1, 7, 31, 0, 6, 0, 4, 6, 7, 6, 0 } } };

expect_ym2612_write_channel_any_data(chan, 0xB0);
expect_ym2612_write_channel(chan, 0xB4, 0x80);
expect_ym2612_write_channel_any_data(chan, 0x30);
expect_ym2612_write_channel_any_data(chan, 0x50);
expect_ym2612_write_channel_any_data(chan, 0x60);
expect_ym2612_write_channel_any_data(chan, 0x70);
expect_ym2612_write_channel_any_data(chan, 0x80);
expect_ym2612_write_channel_any_data(chan, 0x40);
expect_ym2612_write_channel_any_data(chan, 0x90);
expect_ym2612_write_channel_any_data(chan, 0x38);
expect_ym2612_write_channel_any_data(chan, 0x58);
expect_ym2612_write_channel_any_data(chan, 0x68);
expect_ym2612_write_channel_any_data(chan, 0x78);
expect_ym2612_write_channel_any_data(chan, 0x88);
expect_ym2612_write_channel_any_data(chan, 0x48);
expect_ym2612_write_channel_any_data(chan, 0x98);
expect_ym2612_write_channel_any_data(chan, 0x34);
expect_ym2612_write_channel_any_data(chan, 0x54);
expect_ym2612_write_channel_any_data(chan, 0x64);
expect_ym2612_write_channel_any_data(chan, 0x74);
expect_ym2612_write_channel_any_data(chan, 0x84);
expect_ym2612_write_channel_any_data(chan, 0x44);
expect_ym2612_write_channel_any_data(chan, 0x94);
expect_ym2612_write_channel_any_data(chan, 0x3C);
expect_ym2612_write_channel_any_data(chan, 0x5C);
expect_ym2612_write_channel_any_data(chan, 0x6C);
expect_ym2612_write_channel_any_data(chan, 0x7C);
expect_ym2612_write_channel_any_data(chan, 0x8C);
expect_ym2612_write_channel_any_data(chan, 0x4C);
expect_ym2612_write_channel_any_data(chan, 0x9C);
expect_ym2612_write_channel_any_data(chan, YM_BASE_ALGORITHM_FEEDBACK);
expect_ym2612_write_channel(chan, YM_BASE_STEREO_AMS_PMS, 0x80);

for (u8 op = YM_OP1; op <= YM_OP4; op++) {
expect_ym2612_write_channel_any_data(chan, YM_REG3(YM_BASE_MULTIPLE_DETUNE, op));
expect_ym2612_write_channel_any_data(chan, YM_REG3(YM_BASE_ATTACK_RATE_SCALING_RATE, op));
expect_ym2612_write_channel_any_data(chan, YM_REG3(YM_BASE_DECAY_RATE_AM_ENABLE, op));
expect_ym2612_write_channel_any_data(chan, YM_REG3(YM_BASE_SUSTAIN_RATE, op));
expect_ym2612_write_channel_any_data(chan, YM_REG3(YM_BASE_RELEASE_RATE_SUSTAIN_LEVEL, op));
expect_ym2612_write_channel_any_data(chan, YM_REG3(YM_BASE_TOTAL_LEVEL, op));
expect_ym2612_write_channel_any_data(chan, YM_REG3(YM_BASE_SSG_EG, op));
}
__real_synth_preset(chan, &M_BANK_0_INST_7_CLAVINET);
}

Expand Down Expand Up @@ -562,8 +526,9 @@ void test_synth_disables_ch3_special_mode(UNUSED void** state)

void test_synth_sets_ch3_special_mode_operator_pitches(UNUSED void** state)
{
const u8 upperRegs[] = { 0xAD, 0xAE, 0xAC };
const u8 lowerRegs[] = { 0xA9, 0xAA, 0xA8 };
const u8 upperRegs[]
= { YM_CH3SM_OP1_FREQ_MSB_BLK, YM_CH3SM_OP3_FREQ_MSB_BLK, YM_CH3SM_OP2_FREQ_MSB_BLK };
const u8 lowerRegs[] = { YM_CH3SM_OP1_FREQ_LSB, YM_CH3SM_OP3_FREQ_LSB, YM_CH3SM_OP2_FREQ_LSB };

for (u8 op = 0; op < 3; op++) {
expect_ym2612_write_reg(0, upperRegs[op], 0x22);
Expand All @@ -576,8 +541,8 @@ void test_synth_sets_ch3_special_mode_operator_pitches(UNUSED void** state)
void test_synth_handles_out_of_range_ch3_special_mode_operator(UNUSED void** state)
{
const u8 op = 3; // invalid op
expect_ym2612_write_reg(0, 0xAD, 0x22); // safely wrap to valid reg
expect_ym2612_write_reg(0, 0xA9, 0x84);
expect_ym2612_write_reg(0, YM_CH3SM_OP1_FREQ_MSB_BLK, 0x22); // safely wrap to valid reg
expect_ym2612_write_reg(0, YM_CH3SM_OP1_FREQ_LSB, 0x84);

__real_synth_specialModePitch(op, 4, SYNTH_NTSC_C);
}
Expand Down Expand Up @@ -656,14 +621,14 @@ void test_requests_Z80_bus_if_not_already_taken(UNUSED void** state)
will_return(__wrap_Z80_getAndRequestBus, false);

expect_value(__wrap_YM2612_writeReg, part, 0);
expect_value(__wrap_YM2612_writeReg, reg, 0x2B);
expect_value(__wrap_YM2612_writeReg, reg, YM_DAC_ENABLE);
expect_value(__wrap_YM2612_writeReg, data, 0);

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

__real_synth_directWriteYm2612(0, 0x2B, 0);
__real_synth_directWriteYm2612(0, YM_DAC_ENABLE, 0);
}

void test_does_not_release_Z80_bus_when_taken_prior_to_call(UNUSED void** state)
Expand All @@ -672,8 +637,8 @@ void test_does_not_release_Z80_bus_when_taken_prior_to_call(UNUSED void** state)
will_return(__wrap_Z80_getAndRequestBus, true);

expect_value(__wrap_YM2612_writeReg, part, 0);
expect_value(__wrap_YM2612_writeReg, reg, 0x2B);
expect_value(__wrap_YM2612_writeReg, reg, YM_DAC_ENABLE);
expect_value(__wrap_YM2612_writeReg, data, 0);

__real_synth_directWriteYm2612(0, 0x2B, 0);
__real_synth_directWriteYm2612(0, YM_DAC_ENABLE, 0);
}

0 comments on commit 4956a0e

Please sign in to comment.