# SPDX-License-Identifier: MIT """The PWM unit (``pwm.py``) against Sega's 32X Hardware Manual (DOC) and, where the Manual is silent, ares 9408cb4's behaviour (OBSERVED, provisional) as the quirk records describe it. Written 2026-11 from those sources alone, on a stand-in device: the timer's interrupts and DREQ1 are counted on stubs.""" from __future__ import annotations from types import SimpleNamespace import pytest from sega32x_python.machine import MACHINES from sega32x_python.pwm import PWM, period_length CONTROL, CYCLE, LEFT, RIGHT, MONO = 0x30, 0x32, 0x34, 0x36, 0x38 WORD = 0xFFFF FULL, EMPTY = 0x8000, 0x4000 class StubSH2: def __init__(self) -> None: self.raised = 0 self.chips = [SimpleNamespace(dreq1=0), SimpleNamespace(dreq1=0)] def raise_pwm(self) -> None: self.raised += 1 def set_dreq1(self, level: int) -> None: for chip in self.chips: chip.dreq1 = level def make(*, aden: bool = True) -> tuple[PWM, SimpleNamespace]: device = SimpleNamespace(machine=MACHINES["genesis-32x"], board=None, sh2=StubSH2(), aden=aden) return PWM(device), device # type: ignore[arg-type] def sh2_write(pwm: PWM, offset: int, value: int, lanes: int = WORD) -> None: pwm.write(offset, value, lanes, sh2=True) def start(pwm: PWM, cycle: int, control: int = 0x0001) -> None: sh2_write(pwm, CYCLE, cycle) sh2_write(pwm, CONTROL, control) def test_period_is_cycle_minus_one() -> None: """DOC, Manual 3.4: "the set value - 1 as a multiple of the base clock cycle"; 0 is 4,095; 1 "PWM will no longer operate"; 1047 at 23.01 MHz is "22 (kHz)".""" assert period_length(0) == 4095 assert period_length(1) == 0 assert period_length(2) == 1 assert period_length(1047) == 1046 pwm, _ = make() start(pwm, 1047) pwm.take_samples(10_000 * 7 // 3) assert pwm.rate == pytest.approx(22_000, rel=0.001) def test_registers_read_back() -> None: """DOC, Manual 3.2.1: the 68000 writes LMD and RMD only (TM "Read only from MD side", RTP "SH2 side only"); the SH-2 all of them; cycle is 12 bits.""" pwm, _ = make(aden=False) pwm.write(CONTROL, 0xFFFF, WORD, sh2=False) assert pwm.read(CONTROL, sh2=False) == 0x000F sh2_write(pwm, CONTROL, 0xFFFF) assert pwm.read(CONTROL, sh2=True) == 0x0F8F sh2_write(pwm, CYCLE, 0xFFFF) assert pwm.read(CYCLE, sh2=True) == 0x0FFF def test_fifos_three_deep_oldest_discarded_mono_both() -> None: """DOC, Manual 3.2.1: FULL and EMPTY; "three separate FIFO steps"; "If writing when FIFO is FULL, the oldest data is discarded"; a mono write goes to both.""" pwm, _ = make(aden=False) assert pwm.read(LEFT, sh2=True) == EMPTY for width in (1, 2, 3): sh2_write(pwm, LEFT, width) assert pwm.read(LEFT, sh2=True) == FULL sh2_write(pwm, LEFT, 4) assert pwm.fifos[0] == [2, 3, 4] sh2_write(pwm, MONO, 5) assert pwm.fifos == ([3, 4, 5], [5]) assert pwm.read(RIGHT, sh2=True) == 0 # neither FULL nor EMPTY def test_counter_stopped_with_both_channels_off() -> None: """DOC, Manual 3.2.1: "When both L and R channels are off, because the cycle counter does not operate, once the FULL bit is set to 1, it will not become 0".""" pwm, device = make() sh2_write(pwm, CYCLE, 101) sh2_write(pwm, CONTROL, 0x0F00) # TM only, LMD = RMD = 0 for width in (1, 2, 3): sh2_write(pwm, LEFT, width) pwm.run(100_000) assert pwm.read(LEFT, sh2=True) == FULL assert device.sh2.raised == 0 def test_cycle_one_stops_the_counter() -> None: """DOC, Manual 3.4: with 1 set, "PWM will no longer operate".""" pwm, device = make() for width in (1, 2, 3): sh2_write(pwm, LEFT, width) start(pwm, 1, control=0x0101) pwm.run(100_000) assert pwm.read(LEFT, sh2=True) == FULL assert device.sh2.raised == 0 def test_the_off_side_drains_too() -> None: """DOC, Manual 3.2.1: "When either L or R channel is on, because the OFF side FIFO is also operating, no sound will be output; however, data within FIFO will disappear".""" pwm, _ = make() for width in (1, 2, 3): sh2_write(pwm, RIGHT, width) start(pwm, 101, control=0x0001) # L on, R off pwm.run(pwm.time + 3 * 100) assert pwm.read(RIGHT, sh2=True) == EMPTY assert pwm.take_samples(10**6)[-1][1] == 0.0 # the R output stays silent def test_a_period_pops_as_it_begins() -> None: """``pwm_fifo`` (OBSERVED ares): a width written while stopped plays as the counter starts, and each later period start takes the next.""" pwm, _ = make() sh2_write(pwm, CYCLE, 101) # 100 states a period sh2_write(pwm, LEFT, 11) sh2_write(pwm, LEFT, 21) sh2_write(pwm, CONTROL, 0x0001) assert pwm.fifos[0] == [21] assert pwm.heights[0] == 10 pwm.run(99) assert pwm.fifos[0] == [21] pwm.run(100) assert pwm.fifos[0] == [] assert pwm.heights[0] == 20 def test_empty_fifo_holds_the_width_and_height_is_width_minus_one() -> None: """DOC, Manual 3.4: "When FIFO is empty, the previous pulse width is held"; "Because the set value - 1 is the height, when 1 is set ... 0; and when 0 is set ... 4095" (clamped at the period: ``pwm_output_level``); "Immediately after reset, FIFO is empty and the pulse width is 0".""" pwm, _ = make() start(pwm, 101) assert pwm.take_samples(50 * 7 // 3 + 7) == [(0.0, 0.0)] # after reset: 0 pwm, _ = make() sh2_write(pwm, LEFT, 51) start(pwm, 101) samples = pwm.take_samples(1000 * 7 // 3) assert len(samples) == 10 assert all(sample == (0.5, 0.0) for sample in samples) # held: 50 of 100 states for width, level in ((1, 0.0), (0, 1.0), (101, 1.0)): sh2_write(pwm, LEFT, width) pwm.run(pwm.next) assert pwm.take_samples(pwm.next * 7 // 3)[-1][0] == level def test_routing_table() -> None: """DOC, Manual 3.2.1's table: LMD 01 L, 10 R; RMD 01 R, 10 L; 11 not allowed (the board: silent, ``pwm_routing``).""" for control, expected in ( (0x1, (0.25, 0.0)), (0x2, (0.75, 0.0)), (0x4, (0.0, 0.75)), (0x8, (0.0, 0.25)), (0x9, (0.25, 0.25)), (0x6, (0.75, 0.75)), (0x3, (0.0, 0.0)), (0xC, (0.0, 0.0)), ): pwm, _ = make() sh2_write(pwm, LEFT, 26) sh2_write(pwm, RIGHT, 76) start(pwm, 101, control=control) assert pwm.take_samples(200 * 7 // 3)[-1] == expected, hex(control) def test_timer_interrupt_every_tm_periods() -> None: """DOC, Manual 3.2.2: "Interrupt occurs by cycle register set value x TM cycle. When TM = 1, the interval is the same as the cycle register. When TM = 0, the interval is 16 times the cycle register".""" pwm, device = make() start(pwm, 101, control=0x0101) pwm.run(pwm.time + 10 * 100 - 1) assert device.sh2.raised == 10 pwm, device = make() start(pwm, 101, control=0x0001) # TM = 0 pwm.run(pwm.time + 16 * 100 - 1) assert device.sh2.raised == 1 pwm.run(pwm.time + 16 * 100) assert device.sh2.raised == 2 pwm, device = make() start(pwm, 101, control=0x0301) pwm.run(pwm.time + 9 * 100 - 1) assert device.sh2.raised == 3 def test_dreq1_with_rtp_at_each_timer_event() -> None: """``pwm_dreq1``: with RTP the timer's signal is also DREQ1 (DOC, Manual 3.4), on both SH-2s; an SH-2 write of RTP = 0 drops it (OBSERVED ares). The 68000 cannot set RTP (DOC, 3.2.1).""" pwm, device = make() pwm.write(CONTROL, 0x0081, WORD, sh2=False) sh2_write(pwm, CYCLE, 101) pwm.run(10_000) assert [chip.dreq1 for chip in device.sh2.chips] == [0, 0] pwm, device = make() start(pwm, 101, control=0x0281) # TM = 2, RTP pwm.run(pwm.time + 99) assert [chip.dreq1 for chip in device.sh2.chips] == [0, 0] pwm.run(pwm.time + 100) assert [chip.dreq1 for chip in device.sh2.chips] == [1, 1] sh2_write(pwm, CONTROL, 0x0001, lanes=0x00FF) assert [chip.dreq1 for chip in device.sh2.chips] == [0, 0] def test_an_sh2_tm_write_restarts_the_count() -> None: """``pwm_timer_restart`` (OBSERVED ares): an SH-2 write of TM clears the periods counted; the low byte alone and the 68000 do not.""" pwm, device = make() start(pwm, 101, control=0x0401) # TM = 4: the 4th period start interrupts pwm.run(pwm.time + 250) # three starts counted pwm.write(CONTROL, 0x0F01, WORD, sh2=False) # the 68000: TM untouched sh2_write(pwm, CONTROL, 0x0001, lanes=0x00FF) # the SH-2's low byte only pwm.run(pwm.next) assert device.sh2.raised == 1 pwm, device = make() start(pwm, 101, control=0x0401) pwm.run(pwm.time + 250) sh2_write(pwm, CONTROL, 0x0401) # TM written: the count starts again pwm.run(pwm.time + 349) # starts at +300, +400, +500 counted again from 1 assert device.sh2.raised == 0 pwm.run(pwm.time + 1) # the 4th, at +600 assert device.sh2.raised == 1 def test_a_cycle_write_keeps_the_period_under_way() -> None: """``pwm_cycle_write`` (OBSERVED ares): the new length applies from the next period.""" pwm, _ = make() start(pwm, 101) begun = pwm.next - 100 pwm.run(begun + 30) sh2_write(pwm, CYCLE, 11) assert pwm.next == begun + 100 pwm.run(pwm.next) assert pwm.next == begun + 110 def test_width_byte_writes() -> None: """``pwm_byte_writes`` (OBSERVED ares): the even byte is held, the odd byte queues it with the held high bits, a word queues once, and the held byte is kept.""" pwm, _ = make(aden=False) sh2_write(pwm, LEFT, 0x0300, lanes=0xFF00) assert pwm.fifos[0] == [] sh2_write(pwm, LEFT, 0x0045, lanes=0x00FF) assert pwm.fifos[0] == [0x345] sh2_write(pwm, LEFT, 0x0067, lanes=0x00FF) assert pwm.fifos[0] == [0x345, 0x367] pwm.write(MONO, 0x0100, 0xFF00, sh2=False) pwm.write(MONO, 0x0002, 0x00FF, sh2=False) assert pwm.fifos == ([0x345, 0x367, 0x102], [0x102]) def test_counter_needs_aden() -> None: """``pwm_needs_aden`` (CHOICE): with ADEN = 0 nothing drains.""" pwm, device = make(aden=False) sh2_write(pwm, LEFT, 1) start(pwm, 101) pwm.run(10_000) assert pwm.fifos[0] == [1] device.aden = True pwm.run(10_001) assert pwm.fifos[0] == [] def test_samples_cover_each_stretch_at_the_period_rate() -> None: """Socket 8: one sample per period's length over the whole stretch (silence too), at the SH-2 clock (MCLK x 3/7, Manual 3.3) over that length; ``gain`` from ``Quirks.pwm_gain``.""" pwm, _ = make() silent = pwm.take_samples(4095 * 10 * 7 // 3) assert len(silent) == 10 and set(silent) == {(0.0, 0.0)} sh2_write(pwm, LEFT, 501) start(pwm, 1001) mclk = pwm.time * 7 // 3 + 1000 * 100 * 7 // 3 samples = pwm.take_samples(mclk) assert len(samples) in (100, 101) assert samples[-1] == (0.5, 0.0) assert pwm.rate == pytest.approx(53_693_175 * 3 / 7 / 1000) assert pwm.gain == MACHINES["genesis-32x"].quirks.pwm_gain.value