"""PWM (milestone 5, ``pwm.py``): the counter, the FIFOs, routing, the output level, the timer interrupt, DREQ1 and socket 8's samples. Each test names the quirk (``sega32x_python.machine.Quirks``) or the source it pins. The counter is driven directly (``PWM.run`` in SH-2 states) where only PWM is under test, and through the SH-2 side's catch-up with stand-in boot ROMs where the interrupt and DREQ1 need the SH-2s; none needs the user's files.""" from __future__ import annotations from conftest import SYNTHETIC_VECTORS, image from megadrive_python.cartridge import Cartridge from test_sh2side import _start, _system from sega32x_python import MACHINES, Sega32X from sega32x_python.pwm import NEVER CONTROL, CYCLE, LEFT, RIGHT, MONO = 0x30, 0x32, 0x34, 0x36, 0x38 def _pwm(cycle: int = 11, control: int = 0x0001): """A PWM with ADEN set, no SH-2s, the cycle and the control (SH-2 side) written at 0.""" device = Sega32X(Cartridge(image("60fe")), vector_rom=SYNTHETIC_VECTORS) device.adapter |= 1 pwm = device.pwm pwm.write(CYCLE, cycle, 0xFFFF, sh2=True) pwm.write(CONTROL, control, 0xFFFF, sh2=True) return pwm def test_quirks_are_recorded() -> None: """Every PWM choice is a Quirk with its source and tier; the gain is an assumption.""" q = MACHINES["genesis-32x"].quirks for name in ("pwm_period", "pwm_fifo", "pwm_routing", "pwm_timer_restart", "pwm_cycle_write", "pwm_dreq1", "pwm_byte_writes", "pwm_needs_aden", "pwm_output_level", "pwm_gain"): # fmt: skip quirk = getattr(q, name) assert quirk.source and quirk.tier and quirk.milestone == 5, name assert q.pwm_gain.tier == "assumption" assert q.pwm_output_level.tier == "assumption" def test_period_is_cycle_minus_one() -> None: """Quirks.pwm_period: cycle - 1 SH-2 states (PicoDrive, ares, jgenesis, BlastEm); a FIFO gives a word at each period's end, which plays through the next.""" pwm = _pwm(cycle=11) assert pwm.period() == 10 and pwm.next == 10 for width in (2, 4, 6): pwm.write(LEFT, width, 0xFFFF, sh2=True) pwm.run(9) assert pwm.periods == 0 and pwm.levels == (0.0, 0.0) pwm.run(10) assert pwm.periods == 1 and pwm.fifos[0] == [4, 6] assert pwm.levels == (0.2, 0.0) # 2 of 10: L routed, R off pwm.run(30) assert pwm.periods == 3 and pwm.levels == (0.6, 0.0) and pwm.next == 40 def test_cycle_one_stops_and_zero_is_4095() -> None: """Quirks.pwm_period: cycle 1 gives no period (ares: 'PWM will no longer operate'); cycle 0 wraps to 4,095.""" pwm = _pwm(cycle=1) assert pwm.period() == 0 and not pwm.counting() and pwm.next == NEVER pwm.write(CYCLE, 0, 0xFFFF, sh2=True) assert pwm.period() == 4095 and pwm.counting() def test_empty_fifo_holds_and_the_off_side_drains() -> None: """Quirks.pwm_fifo: an empty FIFO leaves its last word playing; the side routed off drains too (Manual 3.2.1: 'data within FIFO will disappear').""" pwm = _pwm(cycle=11, control=0x0001) # L only pwm.write(LEFT, 5, 0xFFFF, sh2=True) pwm.write(RIGHT, 7, 0xFFFF, sh2=True) pwm.write(RIGHT, 8, 0xFFFF, sh2=True) pwm.run(20) assert pwm.fifos == ([], []) assert pwm.levels == (0.5, 0.0) pwm.run(50) assert pwm.levels == (0.5, 0.0) # held assert pwm.read(LEFT, sh2=True) == 0x4000 # EMPTY def test_routing() -> None: """Quirks.pwm_routing: LMD 2 plays R on the left; RMD 2 plays L on the right; 3 is silent but runs the counter (Manual 3.2.1's table).""" pwm = _pwm(cycle=11, control=0b1010) # both swapped pwm.write(LEFT, 2, 0xFFFF, sh2=True) pwm.write(RIGHT, 8, 0xFFFF, sh2=True) pwm.run(10) assert pwm.levels == (0.8, 0.2) pwm.write(CONTROL, 0b0101, 0xFFFF, sh2=True) assert pwm.levels == (0.2, 0.8) pwm.write(CONTROL, 0b0011, 0xFFFF, sh2=True) assert pwm.counting() and pwm.levels == (0.0, 0.0) def test_level_is_the_duty_cycle_clamped_at_the_period() -> None: """Quirks.pwm_output_level: min(width, period) / period, unipolar.""" pwm = _pwm(cycle=101, control=0b0101) pwm.write(MONO, 0, 0xFFFF, sh2=True) pwm.write(MONO, 4095, 0xFFFF, sh2=True) pwm.run(100) assert pwm.levels == (0.0, 0.0) pwm.run(200) assert pwm.levels == (1.0, 1.0) def test_byte_writes_queue_on_the_odd_byte() -> None: """Quirks.pwm_byte_writes: the Z80's high-then-low byte pair is one entry.""" pwm = _pwm() pwm.write(MONO, 0x0200, 0xFF00, sh2=False) assert pwm.fifos == ([], []) pwm.write(MONO, 0x0009, 0x00FF, sh2=False) assert pwm.fifos == ([0x209], [0x209]) pwm.write(LEFT, 0x0105, 0xFFFF, sh2=False) assert pwm.fifos[0] == [0x209, 0x105] def test_the_68000_writes_only_the_routing() -> None: """Quirks.pwm_registers: TM and RTP are the SH-2s' (Manual 3.2.1).""" pwm = _pwm(control=0) pwm.write(CONTROL, 0x0F8F, 0xFFFF, sh2=False) assert pwm.control == 0x000F pwm.write(CONTROL, 0x0F8F, 0xFFFF, sh2=True) assert pwm.read(CONTROL, sh2=False) == 0x0F8F def test_counter_needs_aden() -> None: """Quirks.pwm_needs_aden: with ADEN = 0 nothing drains.""" pwm = _pwm(cycle=11) pwm.device.adapter &= ~1 pwm.write(LEFT, 3, 0xFFFF, sh2=True) pwm.run(1000) assert pwm.fifos[0] == [3] and pwm.periods == 0 def test_cycle_write_keeps_the_phase() -> None: """Quirks.pwm_cycle_write: the period under way keeps its start; the new length applies from it.""" pwm = _pwm(cycle=11) pwm.run(25) # periods end at 10, 20; the third began at 20 pwm.write(CYCLE, 21, 0xFFFF, sh2=True) assert pwm.next == 40 pwm.write(CYCLE, 3, 0xFFFF, sh2=True) # 2 states: that period ended at 22, past pwm.run(26) assert pwm.periods == 3 + 2 and sum(run[0] for run in pwm.runs) == 26 def test_take_samples_one_per_period_at_its_rate() -> None: """Socket 8: one (left, right) per period, at MCLK x 3/7 over the period; nothing before the counter first runs (a passive 32X adds nothing to the mix).""" device = Sega32X(Cartridge(image("60fe")), vector_rom=SYNTHETIC_VECTORS) pwm = device.pwm assert pwm.take_samples(10_000) == [] # ADEN = 0: never started device.adapter |= 1 pwm.write(CYCLE, 101, 0xFFFF, sh2=True) pwm.write(CONTROL, 0b0101, 0xFFFF, sh2=True) # starts at state 10,000 * 3 // 7 start = pwm.period_start for width in (25, 50, 75): pwm.write(MONO, width, 0xFFFF, sh2=True) samples = pwm.take_samples((start + 400) * 7 // 3 + 1) assert samples == [(0.0, 0.0), (0.25, 0.25), (0.5, 0.5), (0.75, 0.75)] assert pwm.rate == 53_693_175 * 3 / 7 / 100 def test_first_samples_start_at_the_previous_take() -> None: """Socket 8: megadrive-python's mixer lines a source's first samples up with the start of the stretch it mixes, so a counter that starts between two takes hands over silence back to the first take (a short first list put the whole stream that far ahead, and each later call skipped and held as much).""" device = Sega32X(Cartridge(image("60fe")), vector_rom=SYNTHETIC_VECTORS) device.adapter |= 1 pwm = device.pwm assert pwm.take_samples(700) == [] # state 300, nothing running pwm.run(500) pwm.write(CYCLE, 101, 0xFFFF, sh2=True) pwm.write(MONO, 50, 0xFFFF, sh2=True) pwm.write(CONTROL, 0b0101, 0xFFFF, sh2=True) # starts at state 500 # 300 states on a grid of 100: 200 of lead, then the first period (the width queued # plays from its end, 600). assert pwm.take_samples(1400) == [(0.0, 0.0)] * 3 # state 600 assert pwm.take_samples(1634) == [(0.5, 0.5)] # state 700 def test_take_samples_across_a_cycle_change() -> None: """Socket 8: a stretch played at another period is read on the new grid by zero-order hold, so time is kept exactly.""" pwm = _pwm(cycle=11, control=0b0101) pwm.write(MONO, 10, 0xFFFF, sh2=True) pwm.run(20) # two periods of 10: the second plays 10/10 pwm.write(CYCLE, 21, 0xFFFF, sh2=True) # from 20, periods of 20 pwm.run(60) pwm.device.clock = 60 * 7 // 3 samples = pwm.take_samples(60 * 7 // 3) assert pwm.grid == 20 # 60 states on a grid of 20: three samples, at 20 (1.0, the second period of 10), # 40 and 60 (10 of 20: 0.5). assert samples == [(1.0, 1.0), (0.5, 0.5), (0.5, 0.5)] def test_timer_interrupt_every_tm_periods_and_restart_on_control() -> None: """Quirks.pwm_timer_restart and the TM interval (Manual 3.2.1: TM = 1 every period, 0 every 16): the PWM interrupt on the SH-2s that enable it; a control write restarts the count.""" system, device = _system() _start(system, device) device.sh2_register_write(0, 0x4000, 0x0001, 0x00FF) # PWM mask, master only device.sh2_register_write(0, 0x4032, 21, 0xFFFF) device.sh2_register_write(0, 0x4030, 0x0301, 0xFFFF) # TM 3, L on pwm = device.pwm t0 = pwm.period_start pwm.run(t0 + 59) assert device.sh2.pending == [0, 0] # type: ignore[union-attr] pwm.write(CONTROL, 0x0301, 0xFFFF, sh2=True) # restarts the count at 2 periods pwm.run(t0 + 60) assert device.sh2.pending == [0, 0] # type: ignore[union-attr] pwm.run(t0 + 120) assert device.sh2.pending == [1, 0] # type: ignore[union-attr] def test_dreq1_paces_dma_channel_1() -> None: """Quirks.pwm_dreq1: with RTP each timer event is one DMA channel 1 transfer, set up as the Manual's chapter 5 says (external request, DS = 1, DL = 1, longwords from SDRAM to $20004034: the L and R FIFOs at once).""" system, device = _system() _start(system, device) sh2 = device.sh2 assert sh2 is not None for i in range(8): sh2.sdram[i * 4 : i * 4 + 4] = (((100 + i) << 16) | (200 + i)).to_bytes(4, "big") master = sh2.chips[0] dmac = master.dmac dmac.write(0xFFFFFF90, 32, 0x26000000) # SAR1: SDRAM, cache-through dmac.write(0xFFFFFF94, 32, 0x20004034) # DAR1: L then R dmac.write(0xFFFFFF98, 32, 8) # TCR1 dmac.write(0xFFFFFF9C, 32, 0b0001_1000_1110_0001) # CHCR1 as Mars Check's 152 dmac.write(0xFFFFFFB0, 32, 1) # DME device.sh2_register_write(0, 0x4032, 521, 0xFFFF) device.sh2_register_write(0, 0x4030, 0x0185, 0xFFFF) # TM 1, RTP, L and R pwm = device.pwm # Each timer event (every period, TM = 1) is one longword: the FIFOs never hold more # than one word, which the next period's end takes. while pwm.periods < 9: system.step() assert len(pwm.fifos[0]) <= 1 and pwm.fifos[0] == [w - 100 for w in pwm.fifos[1]] assert pwm.dreq1_count == 9 assert dmac.channels[1].tcr == 0 and dmac.channels[1].chcr & 2 # TE: all 8 moved assert pwm.widths == [107, 207]