"""Rung 4: community statements checked on hardware (docs/validation.md). Prose rules without a printed value; each test pins the rule and gives its tier. Tier 1: the author measured it on a chip (or read it off the die: "die"). Tier 3: a community author says so without describing a test. """ from __future__ import annotations from itertools import pairwise 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 from ym2612_python.tables import vibrato_offset def test_one_address_latch_holds_the_part_and_either_data_port_serves(): """Spec 2.5, tier 1 (Nemesis, p7673): "you can ... write an address to $A00000, then write the data to $A00003, and the data will in fact be written to the part 1 register block".""" fm = chip() fm.write(0, 0x40) fm.write(3, 0x55) assert fm.channels[0].operators[0].total_level == 0x55 assert fm.channels[3].operators[0].total_level == 0 fm.write(2, 0x40) fm.write(1, 0x33) assert fm.channels[3].operators[0].total_level == 0x33 assert fm.channels[0].operators[0].total_level == 0x55 def test_an_address_stays_latched_for_several_data_bytes(): """Spec 2.4, documented (S1, S2 page 7).""" fm = chip() fm.write(0, 0x2A) for data in (1, 2, 3): fm.write(1, data) assert fm.dac_data == data def test_key_on_does_not_reset_the_attenuation(): """Spec 7.4.3, tier 3 (Snake, p7999: "the level continues from where it was"; Nemesis, p5724).""" fm = chip() voice(fm, attack=14) op_reg(fm, 0, 3, 0x80, 0x02) # a slow release key(fm, 0) step(fm, 4000) key(fm, 0, 0) step(fm, 300) op = fm.channels[0].operators[3] level = op.attenuation assert 0 < level < 0x3FF key(fm, 0) step(fm, 1) assert op.envelope_phase == ATTACK and op.attenuation == level def test_total_level_feedback_and_algorithm_are_heard_at_the_next_sample(): """Spec 7.1.2, 9.5: tier 1 for feedback (Shiru, p5720: "it changes sound immediately"), reasoned for total level (Nemesis, p8020).""" fm = chip() voice(fm, f_number=0x400, block=6) key(fm, 0) step(fm, 9) twin = chip() voice(twin, f_number=0x400, block=6) key(twin, 0) step(twin, 9) op_reg(fm, 0, 3, 0x40, 16) assert abs(values(fm, 1)[0]) == abs(values(twin, 1)[0]) >> 2 def test_tremolo_is_still_applied_with_the_lfo_off(): """Spec 8.3, tier 1 (Nemesis, p7490): "Even when the LFO is disabled, amplitude ... modulation are still applied ... it applies a large amount of attenuation". That it rests at the maximum is provisional (test_provisional.py).""" loud, quiet = chip(), chip() for fm in (loud, quiet): voice(fm) reg(fm, 0xB4, 0xF0) key(fm, 0) op_reg(quiet, 0, 3, 0x60, 0x80) assert max(values(quiet, 400)) < max(values(loud, 400)) // 2 def test_vibrato_is_applied_to_the_f_number_before_the_block(): """Spec 8.5, tier 1 (Nemesis, p8905, p8908: "applied directly to fnum, before block data is applied, and this is an 11-bit calculation"; "just mask the result ... with an 11-bit mask").""" fm = chip() voice(fm, f_number=0x7FF, block=3) reg(fm, 0xB4, 0xC7) reg(fm, 0x22, 0x0F) op = fm.channels[0].operators[3] key(fm, 0) increments = set() last = 0 for _ in range(128 * 6): step(fm, 1) increments.add((op.phase - last) & 0xFFFFF) last = op.phase expected = { (((0x7FF + vibrato_offset(0x7FF, 7, counter)) & 0x7FF) << 3) >> 1 for counter in range(128) } assert increments - {(-last) & 0xFFFFF} <= expected | {0} assert min(expected) < 0x100 # the positive swing wraps to a very small F-number assert op.key_code == (3 << 2) | 3 # untouched by the LFO (die, p31049) def test_vibrato_cannot_take_a_small_f_number_below_zero(): """Spec 8.6, tier 1 (Nemesis, p8908): "If all upper six bits of fnum are 0, phase modulation won't adjust fnum at all"; "fnum will always be larger than the amount that would be subtracted".""" for f_number in range(0x20): assert all(vibrato_offset(f_number, 7, counter) == 0 for counter in range(128)) for f_number in (0x20, 0x155, 0x400, 0x7FF): assert min(vibrato_offset(f_number, 7, counter) for counter in range(128)) > -f_number def test_modulation_is_the_output_shifted_right_once(): """Spec 9.3, tier 1 (GManiac, p18814: the "sum shifts RIGHT for ONE bit and this value is added to input phase of modulated operator. No exceptions").""" fm = chip() voice(fm, algorithm=4, total_levels=(0, 0, 127, 127), f_number=0, block=0) op1, op2 = fm.channels[0].operators[0], fm.channels[0].operators[1] key(fm, 0, 0b0011) step(fm, 1) op1.phase = 0x40000 # a quarter turn: operator 1's peak step(fm, 1) out1 = fm.channels[0].feedback_newer assert out1 > 8000 angle = (out1 >> 1) & 0x3FF probe = chip() voice(probe, algorithm=7, total_levels=(127, 0, 127, 127), f_number=0, block=0) key(probe, 0, 0b0010) step(probe, 1) probe.channels[0].operators[1].phase = angle << 10 step(probe, 1) assert fm.channels[0].operator2_previous == probe.channels[0].operator2_previous != 0 assert op2.phase == 0 def test_feedback_uses_operator_1s_last_two_outputs(): """Spec 9.4, tier 1 (GManiac, p18814: "there are really two feedback registers, I made it sure by special tests"), die (Sauraen, p27319: a 15-bit sum).""" fm = chip() voice(fm, algorithm=7, total_levels=(0, 127, 127, 127), feedback=7, f_number=0, block=0) key(fm, 0, 0b0001) step(fm, 1) channel = fm.channels[0] channel.feedback_older, channel.feedback_newer = 4000, 0 step(fm, 1) from_older = channel.feedback_newer channel.feedback_older, channel.feedback_newer = 0, 4000 step(fm, 1) from_newer = channel.feedback_newer assert from_older == from_newer != 0 assert channel.feedback_older == 4000 # the newer became the older def test_the_delayed_modulation_paths_of_the_die_reading(): """Spec 9.6, die (Sauraen, p29471: operator 3 "is evaluated with the ... operator 1 value" of the previous round and "operator 4 is evaluated with the result of operator 2 which was calculated 5 phases ago"; operator 2 and the 3-to-4 path use the most recent). Decision T4, provisional.""" # Algorithm 5: operator 1 feeds 2, 3 and 4. Operators 2 and 4 hear operator 1's # output of this sample; operator 3 hears the one before. fm = chip() voice(fm, algorithm=5, total_levels=(0, 0, 0, 0), f_number=0x300, block=5) key(fm, 0) step(fm, 50) ops = fm.channels[0].operators for op in ops[1:]: op.phase = 0 before = fm.channels[0].feedback_newer step(fm, 1) now = fm.channels[0].feedback_newer assert before != now probe = chip() voice(probe, algorithm=7, total_levels=(127, 0, 0, 0), f_number=0x300, block=5) key(probe, 0) step(probe, 5) outputs = {} for name, modulator in (("current", now), ("previous", before)): op = probe.channels[0].operators[1] op.phase = (-op.phase_increment(0x300)) & 0xFFFFF angle = ((op.phase_increment(0x300) >> 10) + (modulator >> 1)) & 0x3FF op.phase = ((angle << 10) - op.phase_increment(0x300)) & 0xFFFFF step(probe, 1) outputs[name] = probe.channels[0].operator2_previous assert fm.channels[0].operator2_previous == outputs["current"] assert outputs["current"] != outputs["previous"] def test_special_mode_gives_each_operator_its_own_key_code(): """Spec 10.2, die (Sauraen, p31049: the key code is from "the FNUM for the current operator, including the cases of the operators of voice 3 with their own frequencies").""" fm = chip() reg(fm, 0x27, 0x40) frequency(fm, 2, 0x100, 1) reg(fm, 0xAD, (6 << 3) | 0x07) reg(fm, 0xA9, 0xFF) ops = fm.channels[2].operators assert ops[0].key_code == (6 << 2) | 3 and ops[3].key_code == 1 << 2 reg(fm, 0x27, 0x00) assert ops[0].key_code == 1 << 2 and ops[0].f_number == 0x100 reg(fm, 0x27, 0x80) assert ops[0].f_number == 0x7FF def test_channel_3s_own_frequencies_exist_in_part_i_only(): """Spec 3.3 (documented), 2.3.""" fm = chip() reg(fm, 0x27, 0x40) reg(fm, 0xAD, 0x3F, part=1) reg(fm, 0xA9, 0xFF, part=1) assert fm.channel3_f_number == [0, 0, 0] assert all(op.f_number == 0 for op in fm.channels[5].operators) def test_dac_ninth_bit_is_2c_bit_3(): """Spec 13.3, die (Sauraen, p26996: "$2C:3 - Ch6 DAC ninth (least significant) bit").""" fm = chip() reg(fm, 0x2B, 0x80) reg(fm, 0x2A, 0x90) assert step(fm, 1)[0][0] == 32 reg(fm, 0x2C, 0x08) assert step(fm, 1)[0][0] == 33 reg(fm, 0x2C, 0xF7) assert step(fm, 1)[0][0] == 32 def test_every_dac_write_one_sample_apart_is_heard(): """Spec 13.5, tier 1 (TmEE, p31027, p34262: "I could get unique samples out every 144 cycles").""" fm = chip() reg(fm, 0x2B, 0x80) heard = [] for data in range(0x80, 0x90): reg(fm, 0x2A, data) heard.append(step(fm, 1)[0][0]) assert heard == [2 * (data - 0x80) for data in range(0x80, 0x90)] def test_dac_level_is_about_one_fm_channels(): """Spec 13.3, contradiction C10, tier 1 (Nemesis, p5958: "the maximum DAC output is slightly lower than that of an fm channel"). GManiac's factor of 4 (p14109) would make it twice an FM channel's.""" fm = chip() reg(fm, 0x2B, 0x80) reg(fm, 0x2A, 0xFF) assert 200 < step(fm, 1)[0][0] <= 255 def test_the_quiet_sine_of_gmaniacs_test(): """Spec 15.3.2, tier 1 (TmEE, p12981, on a model 2: "There seems to be -2, -1, 0 and +1 here"; p12991, GManiac, on a model 1: "the step -1 to 0 will be HUGE"). One operator at a total level that leaves two bits of swing.""" seen = {} for variant in (YM3438_ASIC, YM2612_DISCRETE): fm = chip(variant) voice(fm, total_levels=(127, 127, 127, 0x3C), f_number=0x100, block=2) key(fm, 0) frames = step(fm, 6000) assert set(fm.channel_values()) <= {-2, -1, 0, 1} idle = 5 * (variant.ladder_idle + 0) seen[variant.name] = sorted({frame[0] - idle for frame in frames}) assert seen["YM3438"] == [-2, -1, 0, 1] levels = seen["YM2612"] gaps = [b - a for a, b in pairwise(levels)] assert len(levels) == 4 and max(gaps) > 3 and gaps.index(max(gaps)) == 1 def test_six_slots_are_summed_not_wrapped(): """Spec 15.1, tier 1 (Nemesis, p7516: each channel has its own time slot). The core gives the sum of the six slots (decision T11): six channels at their limit give six times one.""" fm = chip() for channel in range(6): voice(fm, channel=channel, algorithm=7, total_levels=(0, 0, 0, 0)) key(fm, channel) frames = step(fm, 2000) assert max(frame[0] for frame in frames) == 6 * 255 assert min(frame[0] for frame in frames) == 6 * -256 def test_psg_balance_is_the_boards_business(): """Spec 15.4 (Nemesis, p5960): "All 4 PSG channels at max combined is equivalent to the maximum output of a single YM2612 channel at max". The core exports the unit: one channel's maximum is 255 steps.""" assert YM3438_ASIC.accumulator_max == 255