"""Rung 1: digital values published from real chips (docs/validation.md). Each test is one fact someone read out of a chip, or saw at its pin, and stated in the prose of a post of SpritesMind thread 386 ("p7967" is a post number). The provenance audit (docs/provenance-audit.md) read each post cited here. """ from __future__ import annotations from itertools import pairwise import pytest from helpers import chip, frequency, key, op_reg, reg, step, values, voice from ym2612_python import YM2612_DISCRETE, YM3438_ASIC from ym2612_python.operator import ATTACK, DECAY, RELEASE, SUSTAIN, Operator from ym2612_python.tables import ENVELOPE_INCREMENTS, EXPONENT, LOG_SINE def operator(f_number=0, block=0, detune=0, multiple=1): fm = chip() op = fm.channels[0].operators[0] fm._set_frequency(op, f_number, block) op.detune, op.multiple = detune, multiple return op # -- phase generator: Nemesis, p6177 (spec 5) ------------------------------------ @pytest.mark.parametrize( ("f_number", "block", "detune", "multiple", "expected"), [ (0x7FF, 7, 0, 1, 0x1FFC0), # "0x7FF<<6=0x1FFC0" (0x7FF, 0, 0, 1, 0x3FF), # "0x7FF>>1=0x3FF" (0x100, 5, 3, 1, 0x100B), # key code $14, detune 3 adds 11 (0x100, 5, 7, 1, 0xFF5), # detune -3 (0x1FF, 4, 0, 8, 0x7FC0), (0x1FF, 4, 0, 0, 0x7FC), # multiple 0 halves ], ) def test_phase_increments_nemesis_works_through(f_number, block, detune, multiple, expected): """Spec 5.3, 5.6, 5.8: the worked examples of p6177.""" op = operator(f_number, block, detune, multiple) assert op.phase_increment(f_number) == expected def test_negative_detune_on_f_number_0_wraps_to_1ffff(): """Spec 5.7 (p6177): "if detune attempted to subtract a value of 1 from an fnum of 0, the result would be 0x1FFFF". Key code 4 gives a detune of 1.""" op = operator(0, 1, detune=5) assert op.phase_increment(0) == 0x1FFFF def test_detune_overflow_is_a_continuous_tone_with_every_f_number_0(): """Spec 5.7 (p5618): the test ROM "generates a continuous tone on the real system ... even though all slots ... have a frequency number of 0".""" fm = chip() voice(fm, f_number=0, block=1) op_reg(fm, 0, 3, 0x30, 0x51) key(fm, 0) heard = values(fm, 200) assert max(heard) > 200 and min(heard) < -200 fm = chip() voice(fm, f_number=0, block=1) key(fm, 0) assert set(values(fm, 200)) == {0} def test_multiple_keeps_20_bits(): """Spec 5.8 (p6177): $1FFFF times 15 is $1DFFF1, "equivalent to 0xDFFF1".""" op = operator(0, 1, detune=5, multiple=15) assert op.phase_increment(0) == 0xDFFF1 def test_block_0_loses_the_f_numbers_low_bit(): """Spec 5.3 (p6177): "when block is set to 0, an fnum value of 1 = 0, 3 = 2".""" op = operator(0, 0) assert op.phase_increment(1) == op.phase_increment(0) assert op.phase_increment(3) == op.phase_increment(2) def test_key_on_clears_the_phase_counter(): """Spec 5.9 (p7967 item 4).""" fm = chip() voice(fm) key(fm, 0) step(fm, 100) op = fm.channels[0].operators[3] assert op.phase != 0 key(fm, 0, 0) step(fm, 1) key(fm, 0) step(fm, 1) assert op.phase == op.phase_increment(0x269) # one increment after the clear def test_high_f_number_byte_waits_for_the_low_byte(): """Spec 3.3.5 (p5621): after $A6 = $1E, $A2 = $FF, $A6 = $24 "the effective settings for the channel will remain as 0xA6 = 0x1E, 0xA2 = 0xFF".""" fm = chip() reg(fm, 0xA6, 0x1E) reg(fm, 0xA2, 0xFF) reg(fm, 0xA6, 0x24) channel = fm.channels[2] assert (channel.block, channel.f_number) == (3, 0x6FF) reg(fm, 0xA2, 0xFF) assert (channel.block, channel.f_number) == (4, 0x4FF) # -- operator unit (spec 6) ------------------------------------------------------ def test_the_tables_ends_as_nemesis_gives_them(): """Spec 6.5 (p6741): the power table's entries "effectively run from 0x7FA-0x400"; the ROM, without bit 10, "from 0x3FA-0x0".""" assert (EXPONENT[0], EXPONENT[255]) == (0x7FA, 0x400) assert len(LOG_SINE) == len(EXPONENT) == 256 assert LOG_SINE[255] == 0 and max(LOG_SINE) == LOG_SINE[0] < 4096 # 12 bits def full_level_operator(): op = Operator() op.attenuation = 0 op.envelope_phase = SUSTAIN return op def test_one_operator_spans_minus_8168_to_plus_8168(): """Spec 6.1 (GManiac, p11876): "operator's output is -8168..+8168".""" op = full_level_operator() outputs = [op.clock(1 << 10, 0, 0) for _ in range(1024)] assert (min(outputs), max(outputs)) == (-8168, 8168) def test_8168_cut_to_nine_bits_is_plus_255_and_minus_256(): """Spec 14.1 (p11876): "+8168 asr 5 = +255", "-8168 asr 5 = -256".""" fm = chip() voice(fm) key(fm, 0) heard = values(fm, 2000) assert (min(heard), max(heard)) == (-256, 255) def test_eight_total_level_steps_halve_the_output(): """Spec 6.8 (p11876): "each 8 steps of TL are equal to 6 dB (division by 2)".""" op = full_level_operator() op.phase = 0xFF << 10 loud = op.clock(0, 0, 0) op.total_level = 8 assert op.clock(0, 0, 0) == loud >> 1 == 4084 def test_output_is_zero_past_about_40_db(): """Spec 6.6 (p6114): past "around 40db attenuation ... just outputs 0".""" op = full_level_operator() op.phase = 0xFF << 10 op.total_level = 0x7F assert op.clock(0, 0, 0) == 0 op.total_level = 0x60 # $300: 36 of the envelope's 48 dB (72 by the manual's) assert op.clock(0, 0, 0) != 0 # -- envelope generator: Nemesis, p5716, p7967 (spec 7) -------------------------- def envelope_operator(phase, attenuation, **registers): op = Operator() op.envelope_phase, op.attenuation = phase, attenuation for name, value in registers.items(): setattr(op, name, value) return op def test_attack_from_3ff_at_increment_8_reaches_0_in_ten_updates(): """Spec 7.4.2 (p7967 item 8): at an 8x increment "from a starting attenuation level of 0x3FF, it would take 10 update steps to achieve minimum attenuation". Rate 60 is the fastest attack that is not instant.""" op = envelope_operator(ATTACK, 0x3FF, attack_rate=30) assert op.rate(op.attack_rate) == 60 updates = 0 while op.attenuation: updates += 1 op.envelope_step(updates) assert updates == 10 def test_first_step_of_a_slow_attack_from_200_gives_1df(): """Spec 7.4.2 (p8037): the attenuation "sits on 0x200 until the attack phase advances. When it does advance, it will drop the attenuation to 0x1DF".""" op = envelope_operator(ATTACK, 0x200, attack_rate=10) counter = 1 while op.attenuation == 0x200: op.envelope_step(counter) counter += 1 assert op.attenuation == 0x1DF @pytest.mark.parametrize("rate_register", [31]) def test_attack_rate_62_or_63_at_key_on_is_instant(rate_register): """Spec 7.4.4 (p7967 item 8): "special case handling to force the attenuation level directly to 0 ... immediately when the key-on event is processed".""" fm = chip() voice(fm, attack=rate_register) op = fm.channels[0].operators[3] assert op.attenuation == 0x3FF and op.rate(op.attack_rate) >= 62 key(fm, 0) step(fm, 1) assert op.attenuation == 0 def test_switching_to_rate_62_during_an_attack_stalls_it(): """Spec 7.4.5 (p7967 item 8): "the attack phase will NOT finish ... it won't advance at all ... until the attack rate is modified again to something less than 62 ... or until key-off occurs".""" fm = chip() voice(fm, attack=12) key(fm, 0) step(fm, 600) op = fm.channels[0].operators[3] assert op.envelope_phase == ATTACK and 0 < op.attenuation < 0x3FF stalled_at = op.attenuation op_reg(fm, 0, 3, 0x50, 31) step(fm, 3000) assert (op.envelope_phase, op.attenuation) == (ATTACK, stalled_at) op_reg(fm, 0, 3, 0x50, 12) step(fm, 600) assert op.attenuation < stalled_at def test_decay_that_passes_the_sustain_level_is_not_cut_back(): """Spec 7.4.6 (p7967 item 6): "the sustain phase begins on the next cycle, but the attenuation value is not clamped to SL".""" op = envelope_operator(DECAY, 0x1C, decay_rate=30, sustain_level=1, sustain_rate=0) op.envelope_step(1) assert (op.envelope_phase, op.attenuation) == (DECAY, 0x24) # past $20 op.envelope_step(2) assert (op.envelope_phase, op.attenuation) == (SUSTAIN, 0x24) def test_a_sustain_level_write_after_decay_ended_does_nothing(): """Spec 7.4.6 (p7967 item 5): "the operator does NOT re-enter the decay phase".""" op = envelope_operator(DECAY, 0x40, decay_rate=30, sustain_level=2, sustain_rate=0) op.envelope_step(1) assert op.envelope_phase == SUSTAIN op.sustain_level = 8 for counter in range(2, 50): op.envelope_step(counter) assert (op.envelope_phase, op.attenuation) == (SUSTAIN, 0x40) def test_the_rate_is_worked_out_at_every_update(): """Spec 7.2.3 (p7967 item 5): rate changes "can cause an immediate change in the effective rate for the current phase".""" op = envelope_operator(DECAY, 0, decay_rate=0, sustain_level=15) op.envelope_step(1) assert op.attenuation == 0 op.decay_rate = 30 op.envelope_step(2) assert op.attenuation == 8 def test_release_rate_is_never_0(): """Spec 7.2.1 (p5724): RR is "a 5-bit value with the LSB set to 1 ... so Rate can never equal 0 in the release phase".""" op = envelope_operator(RELEASE, 0, release_rate=0) for counter in range(1, 4096): op.envelope_step(counter) assert op.attenuation > 0 def test_rate_7_steps_0_1_1_1_0_1_1_1(): """Spec 7.3.3 (p5716): "a calculated rate value of 7 ... gives it an update cycle pattern of 0,1,1,1,0,1,1,1".""" assert ENVELOPE_INCREMENTS[7] == (0, 1, 1, 1, 0, 1, 1, 1) def test_low_rates_pair_up(): """Spec 7.3.3 (p5716): "rate values of 2 and 3, 4 and 5, 6 and 7 do produce an identical output. That's not an error".""" for low in (2, 4, 6): assert ENVELOPE_INCREMENTS[low] == ENVELOPE_INCREMENTS[low + 1] def test_the_largest_increment_is_8_and_the_top_rates_use_it(): """Spec 7.3.3 (p7967 items 6, 8): "the largest increment value which can be applied ... in a single update cycle is 8"; rates 62 and 63 "would be the same as rate values 60 and 61, namely ... an 8x increment".""" assert max(max(row) for row in ENVELOPE_INCREMENTS) == 8 assert all(ENVELOPE_INCREMENTS[rate] == (8,) * 8 for rate in range(60, 64)) def test_the_slowest_and_fastest_decays_last_what_nemesis_measured(): """Spec 7.3.2 (p5716, restated in envelope updates): rate 63 decays in 128 updates and rate 2 in 2,048 times 2,046 or so.""" op = envelope_operator(DECAY, 0, decay_rate=31, sustain_rate=31, sustain_level=15) updates = 0 while op.attenuation < 0x3FF and updates < 1000: updates += 1 op.envelope_step(updates) assert updates == 128 op = envelope_operator(DECAY, 0, decay_rate=1, sustain_level=15) assert op.rate(1) == 2 for counter in range(1, 4096): op.envelope_step(counter) assert op.attenuation == 1 # one step in 4,095 updates: every other 2,048th def test_the_envelope_advances_every_third_sample(): """Spec 4.5 (p6224, p7967 item 1): "the envelope generator is updated once every 3 fm update cycles".""" fm = chip() voice(fm) op_reg(fm, 0, 3, 0x60, 31) # decay rate 62: 8 per update op_reg(fm, 0, 3, 0x80, 0xFF) key(fm, 0) op = fm.channels[0].operators[3] seen = [] for _ in range(30): step(fm, 1) seen.append(op.attenuation) changes = [index for index in range(1, 30) if seen[index] != seen[index - 1]] assert all(b - a == 3 for a, b in pairwise(changes)) assert len(changes) >= 8 def test_keys_are_acted_on_every_sample(): """Spec 7.4.9 (p7967 item 7): "key-on changes are processed every fm update cycle".""" fm = chip() voice(fm) op = fm.channels[0].operators[3] for _ in range(3): # whichever sample of the three it is key(fm, 0) step(fm, 1) assert op.envelope_phase != RELEASE key(fm, 0, 0) step(fm, 1) assert op.envelope_phase == RELEASE # -- SSG-EG: Nemesis, p7967, p8025, p8037 (spec 7.6) ----------------------------- @pytest.mark.parametrize(("level", "inverted"), [(0x001, 0x1FF), (0x110, 0x0F0), (0x250, 0x3B0)]) def test_ssg_eg_inversion_is_centred_on_200(level, inverted): """Spec 7.6.2 (p7967): "0x001 should invert to 0x1FF, 0x110 should invert to 0xF0, 0x250 should invert to 0x3B0".""" op = envelope_operator(ATTACK, level, ssg=0x0C) assert op.level(0) == inverted def test_a_write_to_the_attack_bit_inverts_the_output_at_once(): """Spec 7.6.2 (p8025): "A change to the attack bit always causes an immediate inversion of the output".""" op = envelope_operator(DECAY, 0x040, ssg=0x08) assert op.level(0) == 0x040 op.ssg = 0x0C assert op.level(0) == 0x1C0 op.ssg_flag = 1 # toggled once: the attack bit now gives a plain output assert op.level(0) == 0x040 def test_no_inversion_in_release_whatever_the_bits(): """Spec 7.6.2 (p8025): "Output inversion is always disabled during the release phase"; "toggling the ATT bit during the release phase doesn't cause output inversion to kick in".""" op = envelope_operator(RELEASE, 0x040, ssg=0x0C) assert op.level(0) == 0x040 def test_ssg_eg_steps_four_times_as_far_in_decay_sustain_and_release(): """Spec 7.6.1 (p7967, SSG-EG correction 1): "only multiplied by a factor of 4", and "should indeed be applied during the release phase".""" for phase in (DECAY, SUSTAIN, RELEASE): plain = envelope_operator(phase, 0, decay_rate=31, sustain_rate=31, release_rate=15) ssg = envelope_operator(phase, 0, decay_rate=31, sustain_rate=31, release_rate=15, ssg=8) plain.sustain_level = ssg.sustain_level = 15 plain.envelope_step(1) ssg.envelope_step(1) assert (plain.attenuation, ssg.attenuation) == (8, 32) def test_one_sample_is_output_at_200_before_the_pattern_repeats(): """Spec 7.6.4 (p7967): "a single output sample is generated at this attenuation level before the envelope loops back to the attack phase".""" op = envelope_operator(DECAY, 0x1E0, decay_rate=31, attack_rate=31, sustain_level=15, ssg=8) op.ssg_step() op.envelope_step(1) assert op.level(0) == 0x200 op.ssg_step() assert (op.envelope_phase, op.attenuation) == (ATTACK, 0) def test_looping_with_a_slow_attack_rests_at_200_and_toggles_every_sample(): """Spec 7.6.6 (p7967, p8037): "the output inversion state is toggled each sample where the alternate bit is set, and the hold bit is unset" while the attenuation stays at or above $200.""" op = envelope_operator(DECAY, 0x200, decay_rate=31, attack_rate=4, sustain_level=15, ssg=0x0A) flags = [] for _ in range(6): op.ssg_step() flags.append(op.ssg_flag) assert op.envelope_phase == ATTACK and op.attenuation == 0x200 assert flags == [1, 0, 1, 0, 1, 0] def test_release_reaching_200_under_ssg_eg_goes_silent(): """Spec 7.6.3 (p8025, p8032): "the attenuation level is still forced directly to 0x3FF when the release phase reaches 0x200".""" op = envelope_operator(RELEASE, 0x200, release_rate=15, ssg=8) op.ssg_step() assert op.attenuation == 0x3FF def test_phase_is_held_in_patterns_without_alternate_or_hold(): """Spec 5.10 (p7967 item 4): the phase counter is reset "in cases where both the HLD and ALT bits are unset".""" for ssg, held in ((0x08, True), (0x0C, True), (0x0A, False), (0x09, False)): op = envelope_operator(DECAY, 0x200, attack_rate=4, ssg=ssg) op.phase = 0x12345 op.ssg_step() assert (op.phase == 0) is held def test_upper_bits_of_the_ssg_register_do_nothing(): """Spec 3.2.3 (p8037, p14196): "The upper 4 bits had no observable effect".""" fm = chip() op_reg(fm, 0, 0, 0x90, 0xF5) assert fm.channels[0].operators[0].ssg == 0x05 # -- timers: Nemesis, p5680, p5687 (spec 11) ------------------------------------- def samples_until_flag(fm, mask, limit=20000): for count in range(1, limit): step(fm, 1) if fm.read(0) & mask: return count raise AssertionError("no overflow") def test_timer_a_at_3ff_overflows_every_sample(): """Spec 11.1 (p7967): "timer A runs at the same rate as the phase generator update cycle, setting the timer A period to the shortest possible value (0x3FF)".""" fm = chip() reg(fm, 0x24, 0xFF) reg(fm, 0x25, 0x03) reg(fm, 0x27, 0x05) for _ in range(5): assert samples_until_flag(fm, 1) == 1 reg(fm, 0x27, 0x15) def test_timer_periods_are_twice_the_ym2608_manuals(): """Spec 11.1, contradiction C1 (p5680): "the YM2612 timers take twice as long to count down as the timers in the YM2608": 144 and 2,304 clocks a step.""" fm = chip() reg(fm, 0x24, 0xF0) # NA = $3C0: 64 samples reg(fm, 0x27, 0x05) assert samples_until_flag(fm, 1) == 64 fm = chip() reg(fm, 0x26, 0xFC) # NB = 252: 4 steps of 16 samples reg(fm, 0x27, 0x0A) assert samples_until_flag(fm, 2) in range(4 * 16 - 15, 4 * 16 + 1) def test_timer_reload_rules_one_by_one(): """Spec 11.2 (p5687): reloaded on underflow and when the load bit goes 0 to 1; NOT when 1 is written over 1, when reset or enable change, or when the timer registers are written.""" fm = chip() reg(fm, 0x24, 0xF0) reg(fm, 0x27, 0x05) step(fm, 40) reg(fm, 0x27, 0x05) # load written 1 over 1 reg(fm, 0x27, 0x15) # reset bit reg(fm, 0x27, 0x01) # enable off reg(fm, 0x27, 0x05) # and on reg(fm, 0x24, 0xF8) # a new value: used at the next reload assert samples_until_flag(fm, 1) == 24 reg(fm, 0x27, 0x15) assert samples_until_flag(fm, 1) == 32 # $3E0: the new value reg(fm, 0x27, 0x10) # load 0: stopped step(fm, 500) assert fm.read(0) & 1 == 0 reg(fm, 0x27, 0x05) # 0 to 1: restarts from the register value assert samples_until_flag(fm, 1) == 32 def test_timer_a_bytes_are_not_ganged_like_the_f_number(): """Spec 11.2 (p5680): "they are NOT 'ganged together' like the frequency registers are".""" fm = chip() reg(fm, 0x25, 0x02) assert fm.timer_a_register == 0x002 reg(fm, 0x24, 0x80) assert fm.timer_a_register == 0x202 def test_the_interrupt_line_is_the_or_of_the_two_flags(): """Spec 11.4 (p7967): "generated in real-time by a logical OR operation between the overflow bits of the two timers", until "the reset bit is flagged".""" fm = chip() reg(fm, 0x24, 0xFF) reg(fm, 0x25, 0x03) reg(fm, 0x27, 0x05) assert not fm.irq step(fm, 1) assert fm.irq reg(fm, 0x27, 0x10) assert not fm.irq # -- CSM: Nemesis, p5650, p7967 (spec 10) ---------------------------------------- def csm_chip(mode_bits, timer_a): fm = chip() voice(fm, channel=2) reg(fm, 0x24, timer_a >> 2) reg(fm, 0x25, timer_a & 3) reg(fm, 0x27, (mode_bits << 6) | 0x01) return fm, fm.channels[2].operators[3] def test_csm_acts_in_mode_10_only(): """Spec 10.1 (p5650, p7967): "CSM mode is only active when the mode bits are set to 10, NOT when they are set to 11".""" for mode, active in ((0, False), (1, False), (2, True), (3, False)): fm, op = csm_chip(mode, 0x3F0) phases = set() for _ in range(40): step(fm, 1) phases.add(op.envelope_phase) assert (phases != {RELEASE}) is active, mode def test_csm_needs_only_timer_as_load_bit(): """Spec 10.3 (p5650): "None of the other timer-related flags ... have any impact on CSM mode".""" fm, op = csm_chip(2, 0x3F0) assert fm.timer_enable == 0 keyed = 0 for _ in range(64): step(fm, 1) keyed += op.key_state assert keyed == 4 and fm.read(0) == 0 def test_a_lone_overflow_keys_on_for_one_sample(): """Spec 10.4 (p7967): the flag "remains set until the next ... update cycle"; "If timer A does not overflow again before the next update, the CSM key-on flag will appear negated, which would key-off the operator".""" fm, op = csm_chip(2, 0x3F0) states = [] for _ in range(48): step(fm, 1) states.append(op.key_state) assert states.count(1) == 3 and "1, 1" not in str(states) def test_overflow_on_every_sample_holds_the_key(): """Spec 10.4 (p7967): "If timer A overflows before every update cycle ... the CSM key-on flag appears to be permanently set".""" fm, op = csm_chip(2, 0x3FF) step(fm, 1) states = [] for _ in range(30): step(fm, 1) states.append(op.key_state) assert set(states) == {1} def test_a_key_held_through_28_is_not_disturbed_and_can_be_handed_over(): """Spec 10.4 (p7967): the two flags "are OR'd together"; you can "key on an operator manually, trigger a CSM key-on event in a 'held' state (timer A at max rate), key-off the operator manually, and find that the operator does not actually key-off".""" fm, op = csm_chip(2, 0x3F0) key(fm, 2) step(fm, 100) assert op.envelope_phase != RELEASE fm, op = csm_chip(2, 0x3FF) key(fm, 2) step(fm, 10) key(fm, 2, 0) step(fm, 10) assert op.key_state == 1 and op.envelope_phase != RELEASE def test_leaving_mode_10_releases_a_held_csm_key(): """Spec 10.4 (p7967): "if the channel 3 mode doesn't equal 10 ... the CSM key-on state is forced to 0. This causes a CSM key-off event".""" fm, op = csm_chip(2, 0x3FF) step(fm, 10) assert op.key_state == 1 reg(fm, 0x27, 0x41) step(fm, 2) assert op.key_state == 0 and op.envelope_phase == RELEASE def test_csm_key_on_clears_the_phase_too(): """Spec 5.9 (p7967 item 4): "This of course also includes key-on events triggered by CSM mode".""" fm, op = csm_chip(2, 0x3F0) op.phase = 0x54321 for _ in range(20): step(fm, 1) if op.key_state: break assert op.phase == op.phase_increment(op.f_number) # -- accumulator, DAC, status (spec 12 to 14) ------------------------------------ def test_carriers_are_cut_to_nine_bits_before_they_are_summed(): """Spec 14.1 (GManiac, p11862, p11876): "operators are shifted first, then are summed"; "shifted arithmetically (not logically) by 5 bits". Four carriers each at -1 of 14 bits sum to -4, not to (-4 >> 5) = -1.""" fm = chip() voice(fm, algorithm=7, total_levels=(0x48, 0x48, 0x48, 0x48), f_number=0x400, block=4) key(fm, 0) seen = set(values(fm, 3000)) assert min(seen) == -4 and max(seen) == 0 # each carrier swings -1 to 0 def test_the_sum_is_limited_not_wrapped(): """Spec 14.2 (p11876; p5958: "Clipping DOES occur when operators are combined").""" fm = chip() voice(fm, algorithm=7, total_levels=(0, 0, 0, 0)) key(fm, 0) heard = values(fm, 2000) assert (min(heard), max(heard)) == (-256, 255) assert heard.count(255) > 400 and heard.count(-256) > 400 def test_channel_6_keeps_running_under_the_dac(): """Spec 13.2 (GManiac, p14109): "FM Channel 6 and Reg $2A are independent of each other. 'DAC mode' (Reg $2B) only chose output of one of them".""" with_dac, without = chip(), chip() for fm in (with_dac, without): voice(fm, channel=5) key(fm, 5) reg(with_dac, 0x2B, 0x80) step(with_dac, 777) step(without, 777) assert with_dac.channel_values()[5] == 0 # $2A is $80 after reset: mid-scale reg(with_dac, 0x2B, 0x00) assert values(with_dac, 50, 5) == values(without, 50, 5) def test_busy_is_set_by_a_data_write_only_and_lasts_192_clocks(): """Spec 12.2 (Eke, p31026): "BUSY flag is only set on DATA port writes"; "duration seems to be 32 internal clocks (32*6 68k clocks)"; "does not depend on the written register".""" for address in (0x22, 0x30, 0xA0, 0x28): fm = chip() fm.write(0, address) assert fm.read(0) & 0x80 == 0 fm.write(1, 0) assert fm.read(0) & 0x80 fm.run(191) assert fm.read(0) & 0x80 fm.run(1) assert fm.read(0) & 0x80 == 0 def test_absent_registers_do_nothing(): """Spec 3.1.2 (p5680): $2D to $2F "had no noticeable effect"; the "interrupt control flags ... in register $29 do not exist". Spec 2.3 (Shiru, p5648): special mode cannot be set through part II.""" fm, untouched = chip(), chip() for address in (0x29, 0x2D, 0x2E, 0x2F): reg(fm, address, 0xFF) reg(fm, 0x27, 0x40, part=1) reg(fm, 0x22, 0x0F, part=1) assert fm.channel3_mode == 0 and fm.lfo_enabled == 0 fm.busy_clocks = 0 latch = ("channels", "address", "address_part") assert {k: v for k, v in vars(fm).items() if k not in latch} == { k: v for k, v in vars(untouched).items() if k not in latch } def test_channel_3s_operators_take_the_registers_shiru_found(): """Spec 3.3.4 (p5605, Shiru's hardware test): operator 4 is $A6/$A2, operator 3 $AC/$A8, operator 2 $AE/$AA, operator 1 $AD/$A9.""" fm = chip() reg(fm, 0x27, 0x40) for high, low, f_number in ((0xA6, 0xA2, 0x104), (0xAC, 0xA8, 0x103), (0xAE, 0xAA, 0x102), (0xAD, 0xA9, 0x101)): # fmt: skip reg(fm, high, 0x08 | (f_number >> 8)) reg(fm, low, f_number & 0xFF) assert [op.f_number for op in fm.channels[2].operators] == [0x101, 0x102, 0x103, 0x104] def test_status_is_the_same_whatever_register_is_latched(): """Spec 12.1 (p5680): the status is returned "regardless of ... the register that is latched at the time"; there is no chip ID at $FF.""" fm = chip() fm.write(0, 0xFF) assert fm.read(0) == 0 @pytest.mark.parametrize("variant", [YM2612_DISCRETE, YM3438_ASIC]) def test_after_reset_the_channel_outputs_read_0(variant): """Spec 16.2 (Sauraen, p30088, on a YM3438): "if there's nothing playing (or after power-up), the values is 0".""" fm = chip(variant) step(fm, 10) assert fm.channel_values() == [0] * 6 def test_frequency_changes_are_used_from_the_next_sample(): """Spec 5.9 (p5655): "the new frequency is used for the very next sample".""" fm = chip() voice(fm) key(fm, 0) step(fm, 10) op = fm.channels[0].operators[3] before = op.phase frequency(fm, 0, 0x100, 2) step(fm, 1) assert (op.phase - before) & 0xFFFFF == 0x100 << 1