"""The four parts this core models, as data. Section numbers ("spec 6.2") are those of ``docs/spec.md``; S, C and U numbers are its sources, contradictions and unknowns. Each field says where its values come from and how far they can be trusted: - *documented*: a manufacturer's or community document says so; - *measured*: a capture from hardware says so; - *provisional*: no source settles it; the value is the hand-off brief's decision (D numbers, ``docs/handoff-brief.md``) and a measurement would change the value, not the code; - *choice*: nothing speaks to it and this core had to pick. No logic anywhere asks which variant it is running: it reads these fields. """ from __future__ import annotations from dataclasses import dataclass #: Amplitude for each 4-bit attenuation: 2 dB a step (a voltage ratio of 10^-0.1), #: value 0 loudest, value 15 off whatever the step before it was. Documented: spec #: 7.1 and 7.2 (TI's data sheet, Table 1; Sega's manuals print all sixteen rows). #: It is the nominal curve: TI allows each weight a tolerance (spec 7.3) and no #: measured curve of any Sega part is in hand (U4). IDEAL_VOLUME_TABLE: tuple[float, ...] = (*(10 ** (-0.1 * step) for step in range(15)), 0.0) @dataclass(frozen=True) class PSGVariant: """What differs between the parts. Configuration: nothing here changes at run time.""" #: A label for messages and reports. Nothing compares against it. name: str #: Stages in the noise shift register. 15 on the TI chip: measured (spec 6.2.1, #: Kortink's BBC Micro capture repeats after 32,767 bits). 16 on Sega's parts: #: documented (Sega's Game Gear manual, tier 2; Maxim's page, tier 3). lfsr_bits: int #: Stages exclusive-ORed to make the new bit in white noise, as a mask with the #: output end at bit 0. $0003 on the TI chip: measured (spec 6.2.3, the rule #: reproduces Kortink's capture). $0009 on Sega's parts: documented at tier 3 #: only (spec 6.2.4, Maxim's page); no capture of a Sega part is in hand (U3). white_noise_taps: int #: Whether the Game Gear's stereo register exists. Documented (spec section 9). stereo: bool = False #: The period, in ticks between toggles, that a tone value of 0 stands for. #: Sega's parts: 1, the same as value 1. PROVISIONAL (D3; spec 5.6, C2, U1: #: Maxim says a constant +1, MAME's note says "verified on SMS hardware"). #: TI chip: 1,024, what a 10-bit down counter loaded with 0 would count. #: PROVISIONAL (D4; spec 5.7, C3, U2: MAME's note plus an inference from TI's #: description of the counter; emulator tier). tone_zero_period: int = 1 #: The attenuation all four channels hold at power-on. Sega's parts: 15, silent. #: Documented for the Game Gear (spec 10.1, Sega's manual), tier 3 and hedged #: for the Master System (spec 10.2), unsettled for the Mega Drive (U12). #: TI chip: 0, full volume: the chip has no reset and "from a cold boot the PSG #: emits a medium volume tone" (spec 10.4, MacDonald's SC-3000H notes); the #: exact value is MAME's rendering. PROVISIONAL (D5, U13). power_on_attenuation: int = 0 #: The register code (0 to 7, spec 2.2) latched at power-on, which matters only #: to a program whose first write is a data byte. 0, the first tone value, as #: Maxim's 2003 text has it. PROVISIONAL (D6; spec 10.5, C8, U14: MAME says 3). power_on_latch: int = 0 #: Whether a data byte (bit 7 clear) reaches the attenuation and noise registers. #: Documented for Sega's part at tier 3 (spec 3.6, Maxim: "The data byte is NOT #: ignored"). On the TI chip only MAME says so: PROVISIONAL (U8). data_byte_reaches_all_registers: bool = True #: At noise rate 3 the shift register is clocked by the third tone generator's #: output (documented: spec 6.1.3, TI's block diagram and Sega's manual). This is #: the level that output must change *to* for a shift. No source says which edge #: (U6). 1 is a CHOICE, by analogy with Maxim's "when its input changes from 0 #: to 1" for the fixed rates. noise_shift_level: int = 1 #: Whether the Mega Drive's test override exists (spec section 11). #: PROVISIONAL and untested (D7, U15): one paragraph on Plutiedev is the only #: source. test_override: bool = False #: Input clocks for which READY is low after a write: about 32 on the TI chip #: (documented: spec 3.8, TI's data sheet), none on Sega's parts ("there is no #: need for a wait", Sega's Game Gear manual). The core does nothing with it: a #: board that wires READY to the CPU's WAIT, as the SC-3000 does, reads it here. ready_clocks: int = 0 #: Amplitude per attenuation value. The ideal table unless a board passes a #: measured one (D11): ``dataclasses.replace(variant, volume_table=...)``. volume_table: tuple[float, ...] = IDEAL_VOLUME_TABLE #: The discrete Texas Instruments chip of the SG-1000 and SC-3000 (spec section 1). SN76489AN = PSGVariant( "SN76489AN", lfsr_bits=15, white_noise_taps=0x0003, tone_zero_period=1024, power_on_attenuation=0, ready_clocks=32, ) #: The PSG inside the Mark III's and Master System's VDP. SEGA_VDP_PSG = PSGVariant("Sega VDP PSG", lfsr_bits=16, white_noise_taps=0x0009) #: The Game Gear's: the same with a stereo register. GAME_GEAR_PSG = PSGVariant("Game Gear PSG", lfsr_bits=16, white_noise_taps=0x0009, stereo=True) #: The PSG inside the Mega Drive's VDP (YM7101). That its noise is the Master #: System's rests on one line of Maxim's history and on MAME (spec 6.3). YM7101_PSG = PSGVariant("YM7101 PSG", lfsr_bits=16, white_noise_taps=0x0009, test_override=True)