Metadata-Version: 2.5 Name: sn76489-python Version: 0.1.0.dev0 Summary: The SN76489 sound generator and its Sega relatives, written from documents License-Expression: MIT License-File: LICENSE Requires-Python: >=3.11 Provides-Extra: dev Requires-Dist: pytest; extra == 'dev' Requires-Dist: ruff==0.16.8; extra == 'dev' Description-Content-Type: text/markdown # sn76489-python The SN76489 programmable sound generator and its Sega relatives, as a readable pure-Python core for the family's boards (sms-python, megadrive-python). No runtime dependencies. PyPy 3.11 is the supported interpreter. ```python from sn76489_python import SN76489, SEGA_VDP_PSG, CLOCKS_PER_SAMPLE psg = SN76489(SEGA_VDP_PSG) psg.write(0x8E) # latch tone 0, low four bits psg.write(0x0F) # high six bits: n = 254, 440 Hz psg.write(0x90) # tone 0 at full volume psg.run(3_579_545) # one second of the Z80's clock samples = psg.take_samples() # one (left, right) pair per 16 clocks, 0.0 to 4.0 ``` Four parts, as data on one frozen `PSGVariant` record: `SN76489AN` (the discrete TI chip of the SG-1000 and SC-3000), `SEGA_VDP_PSG` (Mark III, Master System), `GAME_GEAR_PSG` (with the stereo register) and `YM7101_PSG` (Mega Drive, with `write_test`). [docs/api.md](docs/api.md) is the whole surface. ## Status (2026-11-12) **A restart, built from documents.** An earlier core under this name was a translation of a GPL model and was withdrawn; [docs/restart.md](docs/restart.md) has the rules this one is written under. The code is written from [docs/spec.md](docs/spec.md), which is written from the sources listed in [docs/phase0.md](docs/phase0.md). Every rule in the code names its spec section, or says CHOICE or PROVISIONAL. What it supports, and no more: *the core does what Texas Instruments' and Sega's manuals and SMS Power's research say, and its discrete-chip noise matches one capture from a BBC Micro.* **No recording of any Sega console's PSG is in hand, so nothing here is claimed exact to a console.** [docs/claims.md](docs/claims.md) lists each claim with its evidence; [docs/validation.md](docs/validation.md) says what was run and what it showed. - 233 tests pass on PyPy 3.11 (rungs 0 to 3 and the provisional behaviours). - `validation/faults.py` breaks the core in 22 ways; every fault is noticed. By rung: the contract tests notice 5, Kortink's capture 5, the manuals 13, the community documents 13, the provisional tests 16. - Not on PyPI. Not adopted by a board yet. ## What is known, and how *documented*: a document says so (tier 2 is the manufacturer, tier 3 a community author who tested on hardware). *measured*: a capture from hardware says so. *provisional*: no source settles it; the value is a decision, held as data on the variant. *unsettled*: sources disagree or are silent, and the core's behaviour is a choice. Spec section numbers in brackets. | Behaviour | `SN76489AN` | `SEGA_VDP_PSG`, `GAME_GEAR_PSG` | `YM7101_PSG` | |---|---|---|---| | Register map, latch and data bytes [2, 3] | documented, tier 2 (TI) | documented, tier 2 (Sega) | documented, tier 2 (Sega) | | A data byte reaches attenuation and noise [3.6] | provisional (only MAME says so) | documented, tier 3 (Maxim) | documented, tier 3 | | Divide by 16; f = N / 32n [4, 5] | documented, tier 2; measured at one value on a BBC Micro | documented, tier 2: all 51 rows of the Game Gear note table | documented, tier 2 | | Tone value 1 is the top note, a square wave [5.5] | measured on a BBC Micro (read by summary) | documented, tier 2 (Sega), against one sentence of Maxim's | as left | | Tone value 0 [5.6, 5.7] | **provisional**: a period of 1,024 (MAME's note; D4) | **provisional**: the same as value 1 (D3) | **provisional**: as left | | A new tone value waits for the next reload [5.4] | unsettled: Maxim's wording, untested | unsettled | unsettled; the closed box does otherwise | | Fixed noise rates N/512, N/1024, N/2048 [6.1] | documented, tier 2 | documented, tier 2 | documented, tier 2 | | Noise rate 3 is clocked by the third tone's output [6.1.3] | documented, tier 2; measured (Kortink) | documented, tier 2 | documented, tier 2 | | Which edge of that tone shifts the register [6.1.4] | unsettled (rising: a choice) | unsettled | unsettled | | Shift register length [6.2.1] | **measured**: 15 (Kortink's capture) | documented, tier 2 and 3: 16 | documented, tier 3: 16 | | White-noise sequence [6.2.3, 6.2.4] | **measured**: 32,767 of 32,767 bits of Kortink's capture | documented, tier 3 only: taps 0 and 3. No capture exists | as left | | Periodic noise, one pulse per trip [6.2.2] | measured (Kortink): 1 in 15 | documented, tier 2: 1 in 16, all 33 rows of the manual's table | documented, tier 3 | | A noise write restarts the register [6.4] | documented, tier 2; start value measured | documented, tier 2 and 3 | documented, tier 3 | | Timing just after a noise write [6.4.3 to 6.4.5] | unsettled (a plain register: a choice) | unsettled | unsettled; the closed box differs by a tick | | 2 dB attenuation steps, 15 is off [7] | documented, tier 2 (nominal; TI gives tolerances) | documented, tier 2. No measured curve | documented, tier 2; the closed box's curve is steeper | | Output polarity [8] | unsettled: levels are unsigned | unsettled | unsettled | | Stereo register [9] | none | Game Gear: documented, tier 2 | none | | Power-on [10] | **provisional**: not reset (tier 3); full volume and a 109 Hz tone is MAME's rendering (D5) | silent: documented, tier 2 (Game Gear), tier 3 (Master System) | silent: unsettled, taken as Sega's other parts | | Register latched at power-on [10.5] | **provisional**: tone 0 (D6) | **provisional** | **provisional** | | Counters, flip-flops, divider at power-on [10.6] | unsettled (a choice) | unsettled | unsettled | | When a write lands within the 16-clock tick [3.9] | unsettled: the next tick (a choice) | unsettled | unsettled | | Test override, `write_test` [11] | none | none | **provisional, untested**: one paragraph on Plutiedev (D7) | ### Provisional behaviours, in one place Each is a field of `PSGVariant` or one clearly marked rule, so a measurement changes a value and not the structure. 1. **Tone value 0 on Sega's parts behaves as value 1** (`tone_zero_period = 1`). 2. **Tone value 0 on the discrete chip is a period of 1,024** (`tone_zero_period = 1024`). 3. **The discrete chip powers on at full volume with tone values 0**, a 109 Hz tone until the program writes (`power_on_attenuation = 0`). A board that does not want it passes `SN76489(SN76489AN, silent_start=True)`. 4. **Tone 0's value is the register latched at power-on** (`power_on_latch = 0`). 5. **A data byte reaches the attenuation and noise registers on the discrete chip too** (`data_byte_reaches_all_registers`). 6. **`write_test`** exists only on `YM7101_PSG` and takes the one published paragraph literally: with bit 9 set, every channel's output is replaced by the volume level of the channel bits 11-10 choose, held, whatever the square waves do. Untested on hardware. ### Sample playback is quieter than with the earlier PSG Games play samples by holding a tone value at 0 or 1 and writing the volume. Here such a channel is a square wave at 111.9 kHz, far above hearing, which through any audio path averages **half** the channel's volume level. The PSG sms-python carried before (the seed, below) and Maxim's page hold a constant +1 instead, the **whole** level. So sampled speech and drums come out 6 dB quieter beside ordinary tones than they did. This follows Sega's manual for value 1 ("Maximum frequency is n = 1") and decision D3 for value 0. It is **provisional**: one recording from a console settles it. ## Reference models that were run, and how - **Nuked-PSG** (Nuke.YKT, GPL-2.0-or-later), a model of the Mega Drive's PSG, was **run as a closed box and its source was not read**. `scripts/fetch_references.py` fetches it at a pinned commit into the git-ignored `reference/`; `validation/nuked_harness.c` drives it through the calls its public header declares; `validation/closed_box.py` compares its output with `YM7101_PSG` at every tick. Its header, licence and README were opened; `ympsg.c` was compiled and never opened, searched or quoted, including when comparisons failed. Nothing built from it is committed. It is a **detector**: where it differs from the core, the difference is recorded, and the core follows the documents. Result: 19 of 51 streams identical at every tick; the rest differ in ways listed in [docs/validation.md](docs/validation.md) (two timing behaviours of tone and noise writes, one tick at noise rate 3, the volume curve, the test bits, and one noise-rate transition that was not characterised). - **MAME's `sn76496` device** (BSD-3-Clause) was **read**, as Phase 0 had, and **not run**: it could not be started outside a full machine in the time allowed (two tries of `mame sg1000` with a hand-made cartridge stopped on a slot error). The differences between it and the core, from its source, are listed in docs/validation.md. - Genesis Plus GX, Gearsystem, BlastEm, openMSX and the earlier core were neither run nor read. ## Rung 4 is empty Rung 4 of the ladder is recordings from a real Sega console. **There are none.** The owner's early Genesis has a broken controller port, and no recordings will be made until it is repaired (decision D8). Until then every statement about Sega's parts rests on manuals that leave out the contested points and on community accounts whose captures are not public. docs/validation.md lists the nine probes that would settle most of the unknowns in an afternoon. ## Credits and sources - **John Kortink** captured the white-noise sequence of the SN76489 in a BBC Micro and worked out the rule that produces it ("SN76489 sound chip details", 2002). The discrete variant's noise is his finding, and it is the only hardware capture this core is judged against. - **Maxim** (SMS Power) wrote the account of Sega's part that most of this rests on, with captures by **Charles MacDonald**. - Texas Instruments' data sheets and Sega's Mark III, Game Gear and Genesis manuals are cited and quoted a sentence at a time; none is reproduced. - The core grew from the PSG that sms-python carried before it adopted a core (sms-python `db518bd`, 2026-09-18), written from Maxim's page and TI's data sheet. It lived in `seed/` during the build and remains in this repository's history. docs/validation.md says which of its tests were kept and which were corrected, and why. No code was ported from anywhere. ## Working on it ```sh ~/.local/bin/pypy3.11 -m venv .venv-pypy .venv-pypy/bin/pip install -e ".[dev]" .venv-pypy/bin/python -m pytest -q .venv-pypy/bin/ruff check . && .venv-pypy/bin/ruff format --check . python scripts/fetch_references.py # pinned references into reference/ .venv-pypy/bin/python validation/closed_box.py --sweeps # rung 5, about 30 s .venv-pypy/bin/python validation/faults.py # about 3 minutes ``` The rung 1 test that reads Kortink's file skips when `reference/` has not been fetched; the same comparison by SHA-256 always runs. MIT.