# SPDX-License-Identifier: MIT """The Sega CD's memories: PRG RAM, word RAM (2M and 1M, with the 1M views), backup RAM. Sources (docs/manual-registers.md is this repository's reading of Sega's *Mega-CD Hardware Manual*, 1991, cited ``p.N``): - PRG RAM is 512 KB ("4Mbit", p.12-14); the main CPU sees one 128 KB bank of it at $020000 through BK1-0 while SBRQ=1 or SRES=0 (p.5, p.57). DOC. - Word RAM is two 1 Mbit RAMs, WORD-RAM 0 and WORD-RAM 1. In 2M mode they are interleaved word by word: address 0 is WORD-RAM 0's word 0, address 2 WORD-RAM 1's word 0, address 4 WORD-RAM 0's word 1 (p.11). DOC. The bytes are stored here in that 2M order, so a 1M bank's word ``w`` is physical word ``2w + bank``. - 1M: the sub CPU's dot image at $080000-$0BFFFF shows its bank one pixel per byte, the upper four bits reading 0; dot byte $080000+2n is bits 7-4 of bank byte n and +2n+1 is bits 3-0 (p.14, derived from the p.54 example). DOC. Writes there follow PM1-0 (p.23, p.54): overwrite writes a pixel only when the new one is non-zero, underwrite only where the old one is zero. DOC. - 1M: the main CPU's cell image at $220000-$23FFFF ("VRAM image") is its bank's dot map in five regions rearranged as cells stacked in columns (p.13, p.16-18); the formula is derived from the manual's address table (manual-registers.md section 4). DOC (derived). Writes through it go to the same bytes: HYPOTHESIS, the manual says nothing about writes there; ares 9408cb4 (ISC) maps writes the same way (OBSERVED). - Backup RAM is 8 KB on odd bytes at sub $FE0000-$FE3FFF, its image to $FEFFFF (p.15). DOC. """ from __future__ import annotations PRG_RAM_SIZE = 0x80000 WORD_RAM_SIZE = 0x40000 BACKUP_RAM_SIZE = 0x2000 #: The five regions of the 1M cell image: (cell offset, dot offset, size, V lines), #: offsets within the main CPU's 128 KB bank (p.16-18, manual-registers.md section 4). CELL_REGIONS = ( (0x20000, 0x00000, 0x10000, 256), (0x30000, 0x10000, 0x08000, 128), (0x38000, 0x18000, 0x04000, 64), (0x3C000, 0x1C000, 0x02000, 32), (0x3E000, 0x1E000, 0x02000, 32), ) #: PM1-0 (p.23): off, underwrite, overwrite; 3 is "Prohibited". PRIORITY_OFF = 0 PRIORITY_UNDERWRITE = 1 PRIORITY_OVERWRITE = 2 def bank_byte(offset: int, bank: int) -> int: """Where byte ``offset`` (0-$1FFFF) of 1M bank ``bank`` lives in the 2M layout: word ``offset >> 1`` of the bank is physical word ``2w + bank`` (p.11).""" return ((offset & 0x1FFFE) << 1) | (bank << 1) | (offset & 1) def cell_to_dot(offset: int) -> int: """A byte offset in the main CPU's 1M bank seen through the cell image ($20000-$3FFFF of the bank's window) to the dot-map byte it shows (p.16-18). With V lines in the region, a cell column of V/8 cells takes V*4 bytes, a cell is eight lines of four bytes: cell offset C holds dot byte ``y * $100 + xb`` with ``xb = (C // (V*4)) * 4 + (C & 3)`` and ``y = (C % (V*4)) >> 2``. DOC (derived from the manual's table of address pairs).""" for cell, dot, size, lines in CELL_REGIONS: if cell <= offset < cell + size: c = offset - cell column = lines * 4 xb = (c // column) * 4 + (c & 3) y = (c % column) >> 2 return dot + y * 0x100 + xb return offset # not reached for 0x20000-0x3FFFF class WordRAM: """256 KB in 2M order. The owner bookkeeping lives in the gate array (:class:`~segacd_python.gatearray.GateArray`); this holds the bytes and the views.""" def __init__(self) -> None: self.data = bytearray(WORD_RAM_SIZE) # -- 2M: plain RAM, either CPU ------------------------------------------------ def read_word_2m(self, offset: int) -> int: offset &= 0x3FFFE data = self.data return (data[offset] << 8) | data[offset + 1] def write_word_2m(self, offset: int, value: int) -> None: offset &= 0x3FFFE self.data[offset] = value >> 8 self.data[offset + 1] = value & 0xFF # -- 1M: one bank ------------------------------------------------------------- def read_byte_1m(self, offset: int, bank: int) -> int: return self.data[bank_byte(offset, bank)] def write_byte_1m(self, offset: int, bank: int, value: int) -> None: self.data[bank_byte(offset, bank)] = value def read_word_1m(self, offset: int, bank: int) -> int: at = bank_byte(offset & 0x1FFFE, bank) return (self.data[at] << 8) | self.data[at + 1] def write_word_1m(self, offset: int, bank: int, value: int) -> None: at = bank_byte(offset & 0x1FFFE, bank) self.data[at] = value >> 8 self.data[at + 1] = value & 0xFF # -- 1M: the sub CPU's dot image ($080000-$0BFFFF) ---------------------------- def read_dot(self, offset: int, bank: int) -> int: """Dot-image byte ``offset`` (0-$3FFFF): one pixel, upper four bits 0 (p.14).""" byte = self.data[bank_byte(offset >> 1, bank)] return byte >> 4 if not offset & 1 else byte & 0x0F def write_dot(self, offset: int, bank: int, pixel: int, priority: int) -> None: """One pixel into the dot image under PM1-0 (p.23, p.54).""" pixel &= 0x0F at = bank_byte(offset >> 1, bank) byte = self.data[at] old = byte >> 4 if not offset & 1 else byte & 0x0F if priority == PRIORITY_OVERWRITE and not pixel: return if priority == PRIORITY_UNDERWRITE and old: return if not offset & 1: self.data[at] = (pixel << 4) | (byte & 0x0F) else: self.data[at] = (byte & 0xF0) | pixel # -- 2M: the graphics unit's 4-bit pixels -------------------------------------- def read_nibble(self, address: int) -> int: """Pixel ``address`` of the 2M RAM as the graphics unit addresses it: four pixels a word, the first in bits 15-12 (p.36-37: stamps and the image buffer are MD cells, two pixels a byte, high nibble first).""" byte = self.data[(address >> 1) & 0x3FFFF] return byte >> 4 if not address & 1 else byte & 0x0F def write_nibble(self, address: int, pixel: int) -> None: at = (address >> 1) & 0x3FFFF byte = self.data[at] if not address & 1: self.data[at] = (pixel << 4) | (byte & 0x0F) else: self.data[at] = (byte & 0xF0) | pixel def formatted_backup_ram() -> bytes: """The internal backup RAM's starting contents when no file is given. The backup RAM's format is the BIOS's (its BRMFORMAT call, BIOS manual p.41), and no document here describes its layout, so this board does not write one: it returns an erased (zero) RAM, which the BIOS reports as "not formatted" (BIOS manual p.38) and which its own control panel formats. A formatted image made that way by your own BIOS can be passed with ``--backup-ram``. HYPOTHESIS: the RAM's power-on contents on a console are whatever the battery kept; zero is a documented default.""" return bytes(BACKUP_RAM_SIZE) __all__ = [ "BACKUP_RAM_SIZE", "PRG_RAM_SIZE", "WORD_RAM_SIZE", "WordRAM", "bank_byte", "cell_to_dot", "formatted_backup_ram", ]