"""Rung 3: community documents whose authors checked them on hardware (tier 3). - Maxim's SN76489 page on the SMS Power development wiki (phase0.md S6) and his 2003 text (S7): the only public account of most of what is particular to Sega's part. Its captures are unpublished, and the one file it offers as Sega's noise is BBC Micro data (spec C9). - Charles MacDonald's SC-3000H notes (S10). - SMS Power forum thread 17116 (S24). Passing these proves agreement with that account. It does not prove the account. """ from __future__ import annotations import pytest from helpers import SEGA_PARTS, bits, left, noise_bits, solo, variant_id, write_tone, write_volume from sn76489_python import SEGA_VDP_PSG, SN76489, SN76489AN ALL = (SN76489AN, *SEGA_PARTS) def test_maxims_worked_write_sequences(): """Tier 3 (S6, "SN76489 register writes"): its five worked examples.""" psg = SN76489(SEGA_VDP_PSG) psg.write(0b1_00_0_1110) psg.write(0b0_0_001111) assert psg.tone[0] == 0b0011111110 # "= 0xfe (440Hz @ 3579545Hz clock)" psg.write(0b1_01_1_1111) assert psg.volume[1] == 0xF # "silent" psg.write(0b1_10_1_1111) assert psg.volume[2] == 0xF psg.write(0b0_0_000000) assert psg.volume[2] == 0x0 # "THEN update it to %0000 = 0x0 (full)" psg.write(0b1_11_0_0101) assert psg.noise == 0b101 # "white noise, medium shift rate" psg.write(0b1_11_0_0101) psg.write(0b0_0_000100) assert psg.noise == 0b100 # "THEN update it to %100 (white noise, high shift rate)" def test_maxims_table_of_tone0_after_each_byte(): """Tier 3 (S6): "the tone registers update immediately when a byte is written; they do not wait until all 10 bits are written", with its four-row table.""" psg = SN76489(SEGA_VDP_PSG) psg.tone[0] = 0b1010101010 # "whatever was previously in the register" psg.write(0b1_00_0_0000) assert psg.tone[0] == 0b1010100000 psg.write(0b0_0_000000) assert psg.tone[0] == 0b0000000000 psg.write(0b1_00_0_1111) assert psg.tone[0] == 0b0000001111 psg.write(0b0_0_111111) assert psg.tone[0] == 0b1111111111 @pytest.mark.parametrize("variant", SEGA_PARTS, ids=variant_id) def test_a_latch_byte_alone_changes_the_pitch(variant): """Tier 3 (S6, as above; spec 3.2), heard at the output: after the latch byte and before any data byte, the tone already runs at the new value.""" psg = solo(variant, 0) write_tone(psg, 0, 0x123) psg.write(0x80 | 0x7) # low four bits only: the value is now $127 stream = bits(psg, 0x127 * 8) edges = [index for index in range(1, len(stream)) if stream[index] != stream[index - 1]] assert edges[-1] - edges[-2] == 0x127 @pytest.mark.parametrize("variant", SEGA_PARTS, ids=variant_id) def test_a_data_byte_reaches_the_attenuation_register(variant): """Tier 3 (S6): "The data byte is NOT ignored. If it is, you will hear a sustained tone while reading a message box in Alex Kidd in Miracle World." Seed: `test_data_byte_after_a_volume_latch_sets_the_volume`, kept. """ psg = SN76489(variant) write_tone(psg, 0, 4) psg.write(0x90 | 0x3) assert psg.volume[0] == 3 psg.write(0x07) assert psg.volume[0] == 7 assert max(left(psg, 16)) == variant.volume_table[7] psg.write(0x7F) # only the low four bits count assert psg.volume[0] == 15 @pytest.mark.parametrize("variant", SEGA_PARTS, ids=variant_id) def test_a_data_byte_reaches_the_noise_register(variant): """Tier 3 (S6): "The data byte is NOT ignored. If it is, some games (e.g. Micro Machines) produce the wrong sound on their noise channel." Seed: `test_data_byte_after_a_noise_latch_is_not_ignored`, kept. """ psg = SN76489(variant) psg.write(0xE5) assert psg.noise == 0b101 psg.lfsr = 0x1234 psg.write(0x04) assert psg.noise == 0b100 assert psg.lfsr == 0x8000 # and it restarts the register like any noise write @pytest.mark.parametrize("variant", SEGA_PARTS, ids=variant_id) def test_segas_white_noise_is_sixteen_stages_tapped_at_bits_0_and_3(variant): """Tier 3 (S6): "For the SMS (1 and 2), Genesis and Game Gear, the tapped bits are bits 0 and 3 ($0009), fed back into bit 15", reset to "all bits ... zero except for the highest bit", with its code: `Output=ShiftRegister&1`. The reference sequence is built here from that rule in the plainest way. No capture of a Sega part's noise is in hand (U3): this checks the rule, at tier 3, and nothing measured. """ register, expected = 0x8000, [] for _ in range(3_000): feedback = (register ^ (register >> 3)) & 1 register = (register >> 1) | (feedback << 15) expected.append(register & 1) assert noise_bits(variant, control=0x04, shifts=3_000) == expected def test_segas_white_noise_repeats_after_57337_shifts(): """Arithmetic on S6's rule, not a measurement (spec 6.2.4): the sequence is not the longest a 16-stage register can give. Seed: `test_white_noise_periods`, kept; the discrete half of it is now rung 1. """ stream = bytes(noise_bits(SEGA_VDP_PSG, control=0x04, shifts=2 * 57_337)) assert stream[57_337:] == stream[:57_337] assert (stream[:57_337] * 2).find(stream[:57_337], 1) == 57_337 @pytest.mark.parametrize("variant", SEGA_PARTS, ids=variant_id) def test_segas_periodic_noise_goes_round_sixteen_stages(variant): """Tier 3 (S6): "the output bit is also the input bit ... a loop of the same length (16 bits for the SMS (1 and 2), Genesis and Game Gear ...)". Seed: `test_periodic_noise_is_the_register_width`, kept. """ stream = noise_bits(variant, control=0x00, shifts=64) assert stream == ([0] * 14 + [1] + [0]) * 4 def test_the_discrete_chips_periodic_noise_goes_round_fifteen_stages(): """Tier 3 (S6: "15 bits for SG-1000, OMV, BBC Micro, SC-3000H ...") and tier 1 (Kortink, S9: "a cycle of 15 clocks").""" stream = noise_bits(SN76489AN, control=0x00, shifts=60) assert stream == ([0] * 13 + [1] + [0]) * 4 @pytest.mark.parametrize("variant", ALL, ids=variant_id) @pytest.mark.parametrize("control", [0xE4, 0xE0, 0xE5, 0xE7]) def test_rewriting_the_same_noise_value_restarts_the_register(variant, control): """Tier 3 (S6: "When the noise register is written to, the shift register is reset"; S24, Maxim: a ROM "writes to the PSG registers every frame instead of only when the values change, which affects the noise channel"). Spec 6.4.2.""" psg = solo(variant, 3) write_tone(psg, 2, 3) psg.write(control) bits(psg, 9_000) before = psg.lfsr psg.write(control) assert psg.noise == control & 7 assert psg.lfsr == 1 << (variant.lfsr_bits - 1) != before @pytest.mark.parametrize("variant", SEGA_PARTS, ids=variant_id) def test_segas_parts_start_silent_with_zeroed_registers(variant): """Tier 3 and hedged (S6): the integrated versions "seem to start initialised with zeroes in the tone/noise registers and ones in the volume registers (silence)". For the Mega Drive nothing specific exists (U12). Seed: `test_power_up_is_silent_with_zeroed_registers`, kept. """ psg = SN76489(variant) assert psg.tone == [0, 0, 0] assert psg.noise == 0 assert psg.volume == [15, 15, 15, 15] assert set(left(psg, 3_000)) == {0.0} def test_the_discrete_chip_is_not_silent_at_power_on(): """Tier 3 (S10, MacDonald on the SC-3000H): "The SN76489A is not reset when the SC-3000H is powered on ... from a cold boot the PSG emits a medium volume tone". Only that it sounds is checked here; what it sounds like is provisional (test_provisional.py).""" psg = SN76489(SN76489AN) levels = left(psg, 5_000) assert max(levels) > 0.0 assert len(set(levels)) > 1 # a tone, not a held level def test_a_board_may_ask_the_discrete_chip_to_start_silent(): """Decision D5: "a board may ask for a silent start".""" psg = SN76489(SN76489AN, silent_start=True) assert set(left(psg, 5_000)) == {0.0} psg.reset() assert max(left(psg, 5_000)) > 0.0 psg.reset(silent_start=True) assert set(left(psg, 5_000)) == {0.0} def test_all_four_at_full_volume_with_the_same_tone_reach_four(): """Tier 3 (S6, "Volume/attenuation"): the mixer "multiplies each channel's output by the corresponding volume ... and sums them"; a channel is 0 or +1.""" psg = SN76489(SEGA_VDP_PSG) for channel in range(4): write_volume(psg, channel, 0) assert set(left(psg, 64)) <= {0.0, 1.0, 3.0, 4.0}