# SPDX-License-Identifier: MIT AND BSD-3-Clause AND ISC """The Ricoh RF5C164 PCM sound source: eight channels reading 8-bit sign-magnitude samples from 64 KB of wave RAM. Sources. ``PCM p.N`` is Sega's *Mega-CD Hardware Manual: PCM Sound Source (RF5C164)*, ver. 1.0, 1991-10-14, read from the page images of Sega's 1999 HTML transcription (``sega-cd-manual-html-1999.zip``, ``MegaCD/PCM-Sound/pcm-NN.gif``) and, for p.14's figure, the scan (``mega-cd-hardware-manual-pcm-sound-source.pdf`` at 600 dpi); first read 2026-11-23. ``p.N`` alone is the hardware manual (p.15: the sub CPU reaches the chip at $FF0000-$FF3FFF on odd bytes; p.iv: "RF5C164 PCM", "64Kx8bit" RAM). DOC unless marked: - the map (PCM p.6): registers 0-8 written, the wave RAM window at $1000-$1FFF, each channel's wave address readable at $10-$1F (low byte even, high byte odd; PCM p.12: A15-A8 and A7-A0); "$0000 to $0007 are used as write only registers"; - the control register (PCM p.7-8): bit 7 ON (the chip sounds) / OFF (suspended), bit 6 MOD: 1 selects the channel in bits 2-0 for registers 0-6, 0 selects the 4 KB wave bank in bits 3-0; - ST, the start, "the high 8 bits" of the address with $00 below (PCM p.9); LSH/LSL, the loop address taken "when the stop data is read" (PCM p.9); FDH/FDL, the address step per sample, 2^4 down to 2^-11, so a 5.11 fixed-point step (PCM p.9-10; RAMAX, the ROM mode, is not used on the Mega-CD); PAN, the high nibble the right channel's coefficient and the low nibble the left's (PCM p.10); ENV multiplies the sample (PCM p.10); $FF is the loop stop data (PCM p.14); - the samples are sign-magnitude, bit 7 set for the positive half (PCM p.14's figure, read from the scan: levels 253-254 stored $FD-$FE, a level just below the centre stored as a small magnitude with bit 7 clear, a level near the bottom as $7C-$7F; the scan is poor, so DOC, partly legible); - the channels are worked 1 to 8, each sample times ENV times its PAN coefficient, added or subtracted by the sample's sign, summed over the eight, and limited at the ends of 16 bits (PCM p.13); clamped to signed 16 bits here; - 384 source clocks a sample: PCM p.1 gives a source clock "up to 12 MHz" and a sampling frequency of 31.x kHz (the last digit is unclear in the page image; 12 MHz / 384 = 31.25 kHz), and PCM p.11 a register write period of 384 source clocks while sounding; the Mega-CD clocks it at 12.5 MHz (p.iv), 32,552 Hz (sockets.md 7). DOC, the divider derived. OBSERVED (MAME f379cd0's ``rf5c68.cpp``, BSD-3-Clause, copyright-holders Olivier Galibert, Aaron Giles, notice in NOTICE; and ares 9408cb4's ``pcm.cpp``, ISC; both do it): the channel on/off register's polarity (a 0 bit sounds the channel: PCM p.10 says only that bit 0 is channel 1 and that the control register has priority) and a channel switched off returning to its start; the shift that brings the product into the 16-bit sum (``>> 5``: PCM p.13's diagram shows a 19-bit product and a 16-bit sum without saying which bits); reads of registers 0-8 giving $FF (the manual: "impossible"). Not modelled: the manual's access limits while sounding (wave RAM cannot be read, and writes need 16 source clocks between them, PCM p.11); a read here always gives the RAM. """ from __future__ import annotations #: Sub CPU clocks per PCM sample (12.5 MHz / 384). SAMPLE_CLOCKS = 384 class PCM: rate = 12_500_000 / SAMPLE_CLOCKS #: Each sample's weight in the console's mix: a labelled assumption (no recording of #: the Sega CD's mix exists, validation.md "What is not available"). gain = 0.25 def __init__(self) -> None: self.ram = bytearray(0x10000) self.enabled = 0 self.channel = 0 # the channel registers 0-6 write to self.bank = 0 # the 4 KB wave bank at $1000-$1FFF self.env = [0] * 8 self.pan = [0] * 8 self.step = [0] * 8 self.loop = [0] * 8 self.start = [0] * 8 self.on = [False] * 8 self.address = [0] * 8 # 16.11 fixed point self.clocks = 0 self.samples: list[tuple[float, float]] = [] # -- the sub CPU's window ($FF0000 + 2n + 1 is offset n) ---------------------------- def read(self, offset: int) -> int: offset &= 0x1FFF if offset >= 0x1000: return self.ram[(self.bank << 12) | (offset & 0xFFF)] if 0x10 <= offset < 0x20: address = self.address[(offset >> 1) & 7] >> 11 return (address >> 8) & 0xFF if offset & 1 else address & 0xFF return 0xFF def write(self, offset: int, value: int) -> None: offset &= 0x1FFF if offset >= 0x1000: self.ram[(self.bank << 12) | (offset & 0xFFF)] = value return c = self.channel if offset == 0: self.env[c] = value elif offset == 1: self.pan[c] = value elif offset == 2: self.step[c] = (self.step[c] & 0xFF00) | value elif offset == 3: self.step[c] = (self.step[c] & 0x00FF) | value << 8 elif offset == 4: self.loop[c] = (self.loop[c] & 0xFF00) | value elif offset == 5: self.loop[c] = (self.loop[c] & 0x00FF) | value << 8 elif offset == 6: self.start[c] = value if not self.on[c]: self.address[c] = value << 19 elif offset == 7: self.enabled = value >> 7 if value & 0x40: self.channel = value & 7 else: self.bank = value & 0xF elif offset == 8: for i in range(8): self.on[i] = not (value >> i) & 1 if not self.on[i]: self.address[i] = self.start[i] << 19 # -- sound ----------------------------------------------------------------------------- def run(self, clocks: int) -> None: self.clocks += clocks while self.clocks >= SAMPLE_CLOCKS: self.clocks -= SAMPLE_CLOCKS self.samples.append(self.sample()) def sample(self) -> tuple[float, float]: if not self.enabled: return (0.0, 0.0) ram = self.ram left = right = 0 for c in range(8): if not self.on[c]: continue address = self.address[c] data = ram[(address >> 11) & 0xFFFF] if data == 0xFF: address = self.loop[c] << 11 data = ram[self.loop[c]] if data == 0xFF: self.address[c] = address continue self.address[c] = (address + self.step[c]) & 0x7FFFFFF magnitude = data & 0x7F env, pan = self.env[c], self.pan[c] sign = 1 if data & 0x80 else -1 left += sign * ((magnitude * env * (pan & 0xF)) >> 5) right += sign * ((magnitude * env * (pan >> 4)) >> 5) return (float(max(-32768, min(32767, left))), float(max(-32768, min(32767, right)))) def take_samples(self, mclk: int) -> list[tuple[float, float]]: samples, self.samples = self.samples, [] return samples __all__ = ["PCM"]