# SPDX-License-Identifier: MIT """PROVISIONAL behaviours, pinned so that a change is seen. They prove nothing. Each is a decision (T numbers: docs/provenance.md) or an emulator-tier default the provenance audit had to take (U21 to U26: docs/provenance.md). The value is data or one marked rule, so a measurement later changes a value, not the structure. """ from __future__ import annotations from helpers import chip, key, op_reg, reg, step, values, voice from ym2612_python import YM2612_DISCRETE, YM3438_ASIC from ym2612_python import chip as chip_module from ym2612_python.operator import DECAY, RELEASE, SUSTAIN, SUSTAIN_LEVEL_15, Operator from ym2612_python.tables import ( ENVELOPE_INCREMENTS, ENVELOPE_SHIFT, LFO_DIVIDERS, TREMOLO_SHIFT, VIBRATO, vibrato_offset, ) def test_t1_vibrato_sums_each_bits_truncated_amount_in_11_bits(): """PROVISIONAL (T1, C16): Nemesis's method. The die reading suggests 12 bits.""" assert vibrato_offset(0x7E0, 7, 0x1C) == 48 + 24 + 12 + 6 + 3 + 1 assert vibrato_offset(0x7E0, 7, 0x5C) == -(48 + 24 + 12 + 6 + 3 + 1) assert vibrato_offset(0x01F, 7, 0x1C) == 0 def test_u24_vibrato_tables_lower_columns_are_ymfms(): """PROVISIONAL, emulator tier (U24): only the last column is documented.""" assert [row[7] for row in VIBRATO] == [0, 1, 2, 3, 4, 6, 12, 24] # documented assert VIBRATO[4] == (0, 0, 1, 2, 2, 2, 3, 4) assert VIBRATO[7] == (0, 0, 8, 12, 16, 16, 20, 24) assert all(list(row) == sorted(row) for row in VIBRATO) def test_t2_lfo_divider_is_the_manuals(): """PROVISIONAL (T2, C14): 109, 78, ... samples a step; emulators use one fewer.""" assert LFO_DIVIDERS == (109, 78, 72, 68, 63, 45, 9, 6) fm = chip() reg(fm, 0x22, 0x0F) step(fm, 5) assert fm.lfo_counter == 0 step(fm, 1) assert fm.lfo_counter == 1 def test_t3_status_addresses(): """PROVISIONAL (T3, C4): the discrete chip at address 0 only, the ASIC at all.""" assert YM2612_DISCRETE.status_ports == (True, False, False, False) assert YM3438_ASIC.status_ports == (True, True, True, True) def test_t4_delayed_modulation_is_on_and_one_constant_away(): """PROVISIONAL (T4, C11).""" assert chip_module.DELAYED_MODULATION is True def heard(): fm = chip() voice(fm, algorithm=0, total_levels=(10, 10, 10, 0), f_number=0x300, block=5) key(fm, 0) return values(fm, 300) delayed = heard() chip_module.DELAYED_MODULATION = False try: assert heard() != delayed finally: chip_module.DELAYED_MODULATION = True def test_t5_accumulator_limit_is_plus_255_on_both(): """PROVISIONAL (T5, C8): GManiac measured +256, "AFAIR".""" assert YM2612_DISCRETE.accumulator_max == YM3438_ASIC.accumulator_max == 255 def test_t6_sustain_level_15_is_3e0(): """PROVISIONAL (T6, C9): the manual's 93 dB; Nemesis measured $3FF.""" assert SUSTAIN_LEVEL_15 == 0x3E0 op = Operator() op.envelope_phase, op.attenuation = DECAY, 0x3E0 op.sustain_level = 15 op.envelope_step(1) assert op.envelope_phase == SUSTAIN def test_t7_one_shared_f_number_latch_per_part(): """PROVISIONAL, emulator tier (T7, C12): a high byte written for one channel is taken by the next low byte written in that part, whatever its channel.""" assert chip_module.FNUM_LATCH_SHARED is True fm = chip() reg(fm, 0xA4, 0x22) # high byte for channel 1 reg(fm, 0xA1, 0x10) # low byte for channel 2 takes it assert (fm.channels[1].block, fm.channels[1].f_number) == (4, 0x210) reg(fm, 0xA0, 0x10) # and channel 1 still gets it: the latch is not cleared assert (fm.channels[0].block, fm.channels[0].f_number) == (4, 0x210) reg(fm, 0xA4, 0x09, part=1) assert fm.fnum_latch[0] == 0x22 # part I's latch is its own def test_t8_test_registers_are_stored_and_only_2c_bit_3_acts(): """PROVISIONAL (T8).""" fm = chip() voice(fm) key(fm, 0) reg(fm, 0x21, 0xFF) reg(fm, 0x2C, 0xF7) twin = chip() voice(twin) key(twin, 0) assert step(fm, 200) == step(twin, 200) assert (fm.test_21, fm.test_2c) == (0xFF, 0xF7) def test_t9_a_write_is_heard_from_the_next_sample_wherever_it_falls(): """PROVISIONAL (T9, U1): no position inside the sample.""" early, late = chip(), chip() for fm, clocks in ((early, 1), (late, 143)): voice(fm) fm.run(clocks) key(fm, 0) fm.run(144 * 20 - clocks) assert early.take_samples() == late.take_samples() def test_t10_ladder_constants_are_ymfms_and_the_asic_has_none(): """PROVISIONAL, emulator tier (T10, U12): +4, -3, and +4 for a slot panned off. Not measured and not fitted. Two hardware reports of the mid-scale step alone are smaller.""" discrete = YM2612_DISCRETE assert (discrete.ladder_positive, discrete.ladder_negative, discrete.ladder_idle) == (4, -3, 4) assert not YM3438_ASIC.ladder assert step(chip(discrete), 1) == [(24, 24)] # six silent slots assert step(chip(YM3438_ASIC), 1) == [(0, 0)] fm = chip(discrete) reg(fm, 0x2B, 0x80) for data, expected in ((0x80, 4), (0x7F, -2 - 3), (0x81, 2 + 4)): reg(fm, 0x2A, data) assert step(fm, 1)[0][0] - 20 == expected reg(fm, 0xB6, 0x00, part=1) assert step(fm, 1) == [(24, 24)] def test_t12_u26_lfo_off_holds_the_counter_at_0_so_tremolo_rests_at_its_maximum(): """PROVISIONAL, emulator tier (T12, U26, C15).""" assert TREMOLO_SHIFT == (8, 3, 1, 0) fm = chip() voice(fm) reg(fm, 0x22, 0x0F) step(fm, 300) assert fm.lfo_counter != 0 reg(fm, 0x22, 0x07) step(fm, 1) assert fm.lfo_counter == 0 op_reg(fm, 0, 3, 0x60, 0x80) key(fm, 0) quiet = Operator() quiet.envelope_phase, quiet.attenuation = SUSTAIN, 0 for depth, rest in ((0, 0), (1, 15), (2, 63), (3, 126)): reg(fm, 0xB4, 0xC0 | (depth << 4)) # What the chip plays is one operator attenuated by the resting amount. quiet.phase = 0 expected = [quiet.clock(0x269 << 3, 0, 0) for _ in range(2000)] quiet.attenuation = rest expected = [quiet.clock(0x269 << 3, 0, 0) for _ in range(2000)] heard = values(fm, 2000) assert max(heard) == max(expected) >> 5, depth def test_t13_envelope_counter_is_12_bits_and_skips_0(): """PROVISIONAL (T13, U3, U4): and every operator updates on the same sample.""" fm = chip() fm.envelope_counter = 4094 step(fm, 3) assert fm.envelope_counter == 4095 step(fm, 3) assert fm.envelope_counter == 1 def test_t14_reset_state(): """PROVISIONAL (T14, U14): registers 0, panning on, release at $3FF.""" fm = chip() voice(fm, feedback=5) reg(fm, 0x22, 0x0F) reg(fm, 0x27, 0xFF) reg(fm, 0x2B, 0x80) key(fm, 0) step(fm, 500) fm.reset_line(0) fm.reset_line(1) fresh = chip() assert fm.envelope_counter == 0 and fm.lfo_counter == 0 and fm.status_flags == 0 for mine, new in zip(fm.channels, fresh.channels, strict=True): assert (mine.left, mine.right, mine.feedback) == (1, 1, 0) for a, b in zip(mine.operators, new.operators, strict=True): assert all(getattr(a, name) == getattr(b, name) for name in a.__slots__) assert (a.envelope_phase, a.attenuation) == (RELEASE, 0x3FF) def test_t15_key_on_channel_values_3_and_7_are_ignored(): """PROVISIONAL (T15, U2).""" fm = chip() reg(fm, 0x28, 0xF3) reg(fm, 0x28, 0xF7) assert all(op.key_request == 0 for channel in fm.channels for op in channel.operators) def test_t16_timer_bs_prescaler_runs_free_of_the_load_bit(): """PROVISIONAL, emulator tier (T16, U9): a first period can be up to 15 samples short.""" fm = chip() step(fm, 10) # the prescaler is at 10 of 16 reg(fm, 0x26, 0xFF) reg(fm, 0x27, 0x0A) count = 0 while not fm.read(0) & 2: step(fm, 1) count += 1 assert count == 6 def test_u21_envelope_table_is_ymfms(): """PROVISIONAL, emulator tier (U21): pinned whole by its sums. The prose facts it agrees with are in tests/test_measured.py.""" assert [sum(row) for row in ENVELOPE_INCREMENTS[:8]] == [0, 0, 4, 4, 4, 4, 6, 6] for rate in range(8, 48): assert sum(ENVELOPE_INCREMENTS[rate]) == 4 + (rate & 3) for rate in range(48, 60): assert sum(ENVELOPE_INCREMENTS[rate]) == (8 + 2 * (rate & 3)) << ((rate - 48) >> 2) assert ENVELOPE_INCREMENTS[9] == (0, 1, 0, 1, 1, 1, 0, 1) assert ENVELOPE_INCREMENTS[49] == (1, 1, 1, 2, 1, 1, 1, 2) assert ENVELOPE_SHIFT[:8] == (11, 11, 11, 11, 10, 10, 10, 10) assert ENVELOPE_SHIFT[44:] == (0,) * 20 def test_u22_attack_curve_is_ymfms(): """PROVISIONAL, emulator tier (U22): attenuation += (~attenuation * step) >> 4.""" op = Operator() op.envelope_phase, op.attenuation, op.attack_rate = 0, 0x3FF, 26 # rate 52: steps of 2 seen = [] for counter in range(1, 6): op.envelope_step(counter) seen.append(op.attenuation) assert seen == [0x37F, 0x30F, 0x2AD, 0x257, 0x20C] def test_u23_key_off_while_inverted_carries_the_heard_level_into_release(): """PROVISIONAL, emulator tier (U23).""" op = Operator() op.ssg, op.envelope_phase, op.attenuation, op.key_state = 0x0C, DECAY, 0x040, 1 heard = op.level(0) op.set_key(0) assert op.envelope_phase == RELEASE and op.attenuation == heard == 0x1C0 assert op.level(0) == heard def test_u25_channel_3_mode_11_gives_separate_frequencies_without_csm(): """PROVISIONAL, emulator tier (U25).""" fm = chip() reg(fm, 0xAD, 0x12) reg(fm, 0xA9, 0x34) reg(fm, 0x27, 0xC0) assert fm.channels[2].operators[0].f_number == 0x234 def test_choice_data_written_through_the_other_part_reaches_global_registers(): """PROVISIONAL (C13): Nemesis's one latch is taken for the $2x registers too.""" fm = chip() fm.write(0, 0x2B) fm.write(3, 0x80) assert fm.dac_enabled == 1