# Memory, the MMU, the firmware and the two cartridge slots ## The CPU's 64 KB (DS p. 13, Fig. 9-1, 23, 24) | Range | What | Owner | | --- | --- | --- | | 0000-007F | register file: R0-R15 (via RP) and the control/peripheral registers (map below) | core (R0-R15, SYS, SP, PS0, PS1) + board (the rest) | | 0000-03FF | internal RAM, 1 KB | core | | 1000-1FFF | internal ROM (MMU0 disabled, at reset) or external memory through MMU0 | board (the core sees bytes) | | 2000-9FFF | external memory through MMU1-MMU4, 8 KB windows | board | | A000-BFFF | VRAM page A (VRAM0) | board | | C000-DFFF | VRAM page B (VRAM1) | board | | E000-FFFF | "Extend I/O / Extend RAM" | board | "Write only during CPU access" is DS's note on VRAM in Fig. 9-1 (**U4**: whether the CPU can read VRAM at all). DS p. 34: "Do not write data directly to VRAM while transferring data to LCD driver (MSB of LCC register is 1 and V-blank flag is 0)". The firmware never writes VRAM from the CPU in 300 frames (OBSERVED: 0 writes to A000-DFFF; all drawing is by DMA). ## The MMU (DS p. 45-46) - MMU0-MMU4 (0024-0028) map CPU 0000-1FFF, 2000-3FFF, 4000-5FFF, 6000-7FFF, 8000-9FFF to 8 KB pages of a **21-bit external space (2 MB)**: page number x 8 KB + the low 13 bits. - MMU0's lower 4 KB stay internal (RAM and register file); at reset MMU0 is disabled and the internal ROM is at 1000-1FFF; **the first write to MMU0** disables the internal ROM, with the prefetch rule (DS p. 46: "only one byte instruction can be followed"). - The DMA reaches ROM by its own bank register (DMBR) "irrespective of MMU". ## What the firmware does with it (OBSERVED, MAME, first 300 frames) SYS = 46h once (16-bit SP, MCNF = 110: 64 KB external mode); CKC = A3h once; MMU1 = 02h once; MMU2 written 264,613 times (mostly 02h and 05h); MMU3 62,107 times (00h, 05h-07h, 12h, 13h, 1Dh-1Fh, and 20h, 40h, 80h twice each); MMU4 37 times (03h-08h, 1Eh-20h). (These counts come from a Lua write tap; MAME also routes some of its CPU's internal updates through the same space, io.md U15, so large counts are not proof of firmware stores.) **MMU0 is never written**: the internal ROM stays at 1000-1FFF, so its vector table and trampolines (1000-1085h) serve as the console's interrupt entry for good. ## The external space: kernel ROM and two slots (U3) The SM8521 has 21 address lines (A0-A20: 2 MB), memory chip enables MCE0B and MCE1B and I/O chip enables IOE0B and IOE1B (DS pin list, p. 3). The Game.com has a 256 KB kernel ROM and two cartridge slots taking images up to 2 MB (census.md). How these three share one 2 MB space is not documented anywhere we can read: - HYPOTHESIS: MCE0B/MCE1B select between the kernel and the slots, with a port bit or the MMU page's high bits choosing the slot; the firmware's MMU values with only one high bit set (20h, 40h, 80h) look like probes of such a boundary. - The build answers it from the firmware, not from MAME's source: trace which MMU pages the firmware maps while reading a cartridge's header in slot 1 and in slot 2 (MAME can run both, `-cart1`/`-cart2`), and which port bits change around those reads. The header the firmware checks in a cartridge is **U5**: the first five bytes of the NI images vary (`0820402f11` Centipede, `0880402003` Henry/Jeopardy, `0440402003` Lights Out, `ffffffffff` or `0000000000` in seven 2 MB images: the header is not at offset 0 there). ## Register file (DS Fig. 9-2, 9-3) | Addr | Name | | Addr | Name | | --- | --- | --- | --- | --- | | 0010 | IE0 | | 0030 | LCC (LCD control/status) | | 0011 | IE1 | | 0031 | LCH (H timing) | | 0012 | IR0 | | 0032 | LCV (V timing; bit 7 V-blank, read only) | | 0013 | IR1 | | 0034 | DMC (DMA control) | | 0014-0017 | P0-P3 (PIO data) | | 0035-003A | DMX1, DMY1, DMDX, DMDY, DMX2, DMY2 | | 0019 | SYS (core) | | 003B | DMPL (palette) | | 001A | CKC (clock change) | | 003C | DMBR (ROM bank) | | 001C-001F | SPH, SPL, PS0, PS1 (core) | | 003D | DMVP (VRAM pages) | | 0020-0023 | P0C-P3C (PIO control) | | 0040-004E | sound (sound.md) | | 0024-0028 | MMU0-MMU4 | | 0050-0054 | TM0C, TM0D, TM1C, TM1D, CLKT | | 002B-002E | URTT, URTR, URTS, URTC | | 005E, 005F | WDT, WDTC (reset value 38h) | | | | | 0060-007F | SG0W0-15, SG1W0-15 (waveforms) |