"""Behaviours no source settles: what the core does, so that a change is seen. Nothing here is evidence about hardware. Each test pins either a PROVISIONAL value (a decision in docs/handoff-brief.md, held as data on the variant so a measurement changes one value) or a CHOICE (nothing speaks to it). If a recording overturns one, the test changes with the value. """ from __future__ import annotations import dataclasses import itertools import pytest from helpers import SEGA_PARTS, bits, changes, left, rises, solo, variant_id, write_tone from sn76489_python import SEGA_VDP_PSG, SN76489, SN76489AN, YM7101_PSG ALL = (SN76489AN, *SEGA_PARTS) # -- provisional: decisions D3 to D7 --------------------------------------------- @pytest.mark.parametrize("variant", SEGA_PARTS, ids=variant_id) def test_tone_value_zero_behaves_as_one_on_segas_parts(variant): """PROVISIONAL (D3; spec 5.6, C2, U1). MAME's note: Sega's chips "act as if the frequency was set to 0", "Verified on SMS hardware", and its model then toggles every tick. Maxim says a constant +1 instead. Seed: `test_tone_register_zero_or_one_holds_the_output_high`, CORRECTED for value 0 by decision D3: the seed held the output at 1. """ assert variant.tone_zero_period == 1 zero, one = solo(variant, 0), solo(variant, 0) write_tone(zero, 0, 0) write_tone(one, 0, 1) stream = bits(zero, 200) assert stream == bits(one, 200) assert stream == [0, 1] * 100 @pytest.mark.parametrize("variant", SEGA_PARTS, ids=variant_id) def test_sample_playback_sits_at_half_the_channels_volume(variant): """What D3 does to what is heard (spec 5.6's observation): a channel at tone value 0 or 1 averages half its volume level, where the seed's and Maxim's held +1 gave the whole of it. Games that play samples by writing volumes are 6 dB quieter beside ordinary tones than with the seed. PROVISIONAL.""" psg = solo(variant, 0) levels = left(psg, 1_000) assert sum(levels) / len(levels) == 0.5 def test_tone_value_zero_is_a_period_of_1024_on_the_discrete_chip(): """PROVISIONAL (D4; spec 5.7, C3, U2): MAME's note, "All the TI-made PSG chips act as if the frequency was set to 0x400 if 0 is written", and what TI's description of a 10-bit down counter reloaded on borrow implies. Emulator tier. Seed: CORRECTED: the seed held a constant 1 on the discrete chip as on Sega's. """ assert SN76489AN.tone_zero_period == 1024 psg = solo(SN76489AN, 0) write_tone(psg, 0, 0) edges = changes(bits(psg, 1024 * 6)) assert {b - a for a, b in itertools.pairwise(edges)} == {1024} def test_noise_at_rate_three_with_the_third_tone_at_zero_on_the_discrete_chip(): """PROVISIONAL, following from D4 (spec 6.1.5): one shift per 2,048 ticks. MAME's change log says this case was "tested on hardware for noise".""" psg = solo(SN76489AN, 3) psg.write(0xE3) # The tone falls on the first tick and rises 1,024 ticks on; the set bit # reaches the output on the 14th shift, then every 15th. assert rises(bits(psg, 2048 * 15 * 2 + 8))[:2] == [1024 + 2048 * 13, 1024 + 2048 * 28] def test_the_discrete_chip_powers_on_at_full_volume_with_a_low_tone(): """PROVISIONAL (D5; spec 10.4, U13). That it sounds is tier 3 (S10); that it is all four channels at attenuation 0 and tone value 0 is MAME's rendering.""" psg = SN76489(SN76489AN) assert psg.volume == [0, 0, 0, 0] assert psg.tone == [0, 0, 0] levels = left(psg, 1024 * 4) assert levels[0] == 0.0 and levels[1024] == 3.0 # three tones in step, 109 Hz assert max(levels) == 4.0 # with the noise channel's periodic pulse on top @pytest.mark.parametrize("variant", ALL, ids=variant_id) def test_the_first_tone_value_is_latched_at_power_on(variant): """PROVISIONAL (D6; spec 10.5, C8, U14). Maxim's 2003 text: "The initially latched register is Tone0". MAME says the second tone's attenuation instead. Seen only by a program whose first write is a data byte.""" assert variant.power_on_latch == 0 psg = SN76489(variant) psg.write(0x3F) assert psg.tone == [0x3F0, 0, 0] assert psg.volume == [variant.power_on_attenuation] * 4 other = SN76489(dataclasses.replace(variant, power_on_latch=3)) # MAME's account other.write(0x05) assert other.volume[1] == 5 and other.tone == [0, 0, 0] def test_a_data_byte_reaches_every_register_on_the_discrete_chip_too(): """PROVISIONAL (U8; spec 3.6): only MAME says so for the TI chip. Held as variant data; with the flag off the byte is dropped.""" psg = SN76489(SN76489AN) psg.write(0x95) psg.write(0x0A) assert psg.volume[0] == 0x0A strict = SN76489(dataclasses.replace(SN76489AN, data_byte_reaches_all_registers=False)) strict.write(0x95) strict.write(0x0A) assert strict.volume[0] == 5 strict.write(0xE5) strict.lfsr = 0x1234 strict.write(0x02) assert strict.noise == 5 and strict.lfsr == 0x1234 strict.write(0x81) strict.write(0x02) assert strict.tone[0] == 0x021 # tone data bytes are TI's own two-byte load @pytest.mark.parametrize("source", range(4)) def test_the_mega_drives_test_override(source): """PROVISIONAL and untested (D7; spec section 11, U15). Tier 3, one paragraph on Plutiedev, with no word on what hardware it was tried on: "When bit 9 is clear, PSG works as normal. When it's set, however, all PSG channels get replaced with the volume level of one of them. Bits 11-10 pick which channel is the source of the volume level." Taken literally: four times the chosen channel's level, held, whatever the square waves do.""" psg = SN76489(YM7101_PSG) for channel, attenuation in enumerate((2, 5, 8, 11)): write_tone(psg, channel, 3 + channel) psg.write(0x90 | (channel << 5) | attenuation) normal = left(psg, 200) assert len(set(normal)) > 2 psg.write_test(0x0200 | (source << 10)) level = YM7101_PSG.volume_table[(2, 5, 8, 11)[source]] assert set(left(psg, 200)) == {level * 4} psg.write_test(source << 10) # bit 9 clear: "PSG works as normal" reference = SN76489(YM7101_PSG) for channel, attenuation in enumerate((2, 5, 8, 11)): write_tone(reference, channel, 3 + channel) reference.write(0x90 | (channel << 5) | attenuation) left(reference, 400) assert left(psg, 200) == left(reference, 200) # the generators ran on underneath def test_only_bits_11_to_9_of_the_test_value_are_the_psgs(): """Spec section 11: "Debug register 0. Bits 11-9: PSG override".""" psg = SN76489(YM7101_PSG) psg.write_test(0xF1FF) assert psg.test == 0 psg.write_test(0x0E00) assert psg.test == 7 # -- choices: nothing speaks to these (U6, U7, U10, U11) ------------------------- @pytest.mark.parametrize("variant", ALL, ids=variant_id) def test_a_new_tone_value_waits_for_the_next_reload(variant): """CHOICE that follows Maxim's wording of the counter (spec 5.4, U11): "the counter is reset to the value currently in the corresponding register" when it reaches zero. No source tests it.""" psg = solo(variant, 0) write_tone(psg, 0, 100) stream = bits(psg, 250) write_tone(psg, 0, 10) stream += bits(psg, 100) # Written at tick 250, mid-count: the edge at 300 still comes on the old value. assert changes(stream) == [100, 200, 300, 310, 320, 330, 340] @pytest.mark.parametrize("variant", ALL, ids=variant_id) def test_the_set_bit_reaches_the_output_after_a_plain_registers_shifts(variant): """CHOICE between accounts (spec 6.4.3, C5, U7): a plain register, 14 shifts on the discrete chip and 15 on Sega's. MAME's SN76489A carries two more stages. Until the bit arrives the output is 0, at once from the write (spec 6.4.4).""" psg = solo(variant, 3) write_tone(psg, 2, 1) psg.write(0xE7) bits(psg, 9_000) psg.write(0xE7) stream = bits(psg, 200) assert stream[0] == 0 # The third tone toggles every tick; the register shifts when it rises, which # is the first or second tick after the write, then every other tick. first_one = stream.index(1) assert first_one in (2 * (variant.lfsr_bits - 2), 2 * (variant.lfsr_bits - 2) + 1) @pytest.mark.parametrize("variant", ALL, ids=variant_id) def test_noise_at_rate_three_shifts_when_the_third_tone_rises(variant): """CHOICE (spec 6.1.4, U6): no source says which edge. Held as variant data.""" assert variant.noise_shift_level == 1 for level in (1, 0): psg = SN76489(dataclasses.replace(variant, noise_shift_level=level), silent_start=True) write_tone(psg, 2, 9) psg.write(0xE7) for _ in range(400): before = (psg.lfsr, psg.outputs[2]) psg.run(16) if psg.lfsr != before[0]: assert (before[1], psg.outputs[2]) == (1 - level, level) @pytest.mark.parametrize("variant", ALL, ids=variant_id) def test_a_noise_write_does_not_restart_the_rate_divider(variant): """CHOICE (spec 6.4.5, U7).""" psg = solo(variant, 3) psg.write(0xE0) bits(psg, 7) counter, flip_flop = psg.noise_counter, psg.noise_input psg.write(0xE0) assert (psg.noise_counter, psg.noise_input) == (counter, flip_flop) def test_the_starting_state_no_source_describes(): """CHOICE (spec 10.6, U10), the seed's: counters at zero and tone flip-flops set, so each tone falls on the first tick; the shift register as after a noise write; the divide-by-16 at the start of its cycle. The Nuked-PSG closed box starts in the same phase (docs/validation.md, rung 5), which is why the seed's value was kept over any other; it is not evidence about a console.""" psg = SN76489(SEGA_VDP_PSG) assert psg.counters == [0, 0, 0] and psg.outputs == [1, 1, 1] assert (psg.noise_counter, psg.noise_input, psg.lfsr) == (0, 0, 0x8000) assert psg.clock_remainder == 0 and psg.test == 0