# The SM85CPU: programmer's model, memory, interrupts DS = the SM8521 data sheet (tier 2), DB96 = the 1996 data book's SM8500-series pages (tier 2, a sibling chip). Page numbers are DS's printed page numbers. ## Registers (DS p. 10-12; DB96 p. 77-79) | Register | Where | Notes | | --- | --- | --- | | R0-R15 (RR0-RR14) | 16 bytes of internal RAM at `RP x 8` | "The eight 8-bit general purpose registers R0-R7 and all eight 16-bit ... RR0-RR14 are available as accumulator, index register and pointer registers. (R0 and RR0 cannot be used as index register.) The other eight 8-bit registers R8-R15 cannot be used as 8-bit general purpose register" (DS p. 10): only RR8-RR14 reach them | | PS0 | 001E | bits 7-3 RP (register pointer, 8-byte units over 0000-00FF), bits 2-0 IM (interrupt mask level) | | PS1 | 001F | bit 7 C, 6 Z, 5 S, 4 V, 3 D (last arithmetic was a subtraction: for DA), 2 H, 1 B (result of the last bit operation), 0 I (maskable interrupts enabled) | | SYS | 0019 | bit 6 SPC (0 = 8-bit SP, SPL only; 1 = 16-bit); bits 2-0 MCNF (000 no external memory; 110 64 KB external mode; others "do not use"); bit 4 AW "must clear 0" in DB96, absent from DS | | SPH, SPL | 001C, 001D | stack pointer, 8 or 16 bits by SPC | | PC | - | 1020h after reset | Reset values (DS Fig. 9-2): R0-R15, SP, PS0 undefined; PS1 `*******0` (I = 0); SYS `*0000000`. **C1:** DS p. 11 copies DB96's IM table (levels 1-12, 1-10 ... and two rows both labelled `111`), while DS p. 24 gives the SM8521's own eight-level table (`000`/`001` all, `010` levels 1-7, `011` 1-6, ... `111` 1-2). The SM8521 has eight maskable priorities (Table 3), so p. 24 is the one that applies; p. 11 is a leftover from the sibling's text. ## Address space (DS p. 13, Fig. 9-1, 9-2; MMU p. 45-46) | Range | What | | --- | --- | | 0000-000F | general registers (the 16 bytes RP selects; RP = 0 puts them here) | | 0000-007F | **register file**: control and peripheral registers (0010-007F) reachable by the short R/RR modes; the same bytes are RAM to the other modes ("can be selected by the addressing mode") | | 0080-00FF | RAM, also reachable by the R/RR modes and bit addressing | | 0000-03FF | internal RAM physically present (1 KB) | | 0400-0FFF | "reserved" in Fig. 9-1 | | 1000-101F | interrupt vectors (big-endian words) | | 1000-1FFF | internal ROM (4 KB) while MMU0 is disabled (at reset); external memory through MMU0 once MMU0 is written | | 2000-9FFF | external memory through MMU1-MMU4, 8 KB each, into a 2 MB (21-bit) space | | A000-DFFF | VRAM (external; the LCDC's) | | E000-FFFF | "Extend I/O / Extend RAM" (external) | | FF00-FFFF | also the DAp "direct special page" and bit-addressable memory | Which part of this the core owns: 0000-03FF's RAM, R0-R15 through RP, and the control registers 0019 and 001C-001F. Everything else is a callback into the board, including the peripheral registers at 0010-007F (decision 3 in the brief). The MMU belongs to the board: the CPU emits 16-bit addresses. A detail the core must honour (DS p. 46 note): changing to external ROM by writing MMU0 "use Immediate R in MOV instruction (MOV R,r or MOV R,R). Data in the next internal ROM address will be fetched at the same time of executing the instruction. Only one byte instruction can be followed when setting data to MMU0 register": the **prefetch** is visible, so the core must expose when it fetches the next opcode (decode-and-timing.md). ## Data formats (DS p. 13-14) Bit, 4-bit BCD, byte and word. Words in the register file must be at even addresses ("an odd address leads to unreliable results", **U1**: what actually happens); words in memory may be odd. Bit operations reach 0000-00FF (register file and RAM) and FF00-FFFF. ## Interrupts (DS p. 23-26, Table 3) | Vector | Source | Priority | | --- | --- | --- | | 1000 | DMA | 1 (highest maskable) | | 1002 | Timer 0 | 2 | | 1006 | external interrupt | 3 | | 1008 | UART transmit/receive complete | 4 | | 100E | LCD controller | 5 | | 1012 | Timer 1 | 6 | | 1016 | clock timer | 7 | | 101A | I/O port | 8 | | 101C | watchdog overflow | NMI | | 101E | NMI, illegal instruction | NMI | Acceptance: a source's IR bit set, its IE bit set, PS1.I = 1 and its level allowed by IM. On acceptance "the content of processor status PS1 and the value of program counter PC automatically are pushed onto stack" (DS p. 12); IRET pops PS1 (1 byte) then PC (2 bytes) (DS p. 52). The push order and the bytes' layout on the stack, whether the IR bit is cleared by hardware on acceptance, and whether I is cleared on entry are **U2-U4** (DS is silent; IRET's description implies PS1 at the lower address). The illegal-instruction trap: "When an error code is detected, a non-maskable interrupt (NMI) will be generated" (DS p. 8), sharing vector 101E with the NMI pin. Which encodings are illegal is **U5** (HDK lists 59-5B, F2-F7 and conditional cases). ## Standby (DS p. 27-28) and clock change (p. 19) - HALT: CPU stops; main clock, timers, UART, LCDC, sound keep running; any enabled interrupt (not the illegal trap) wakes it "immediately". - STOP: main clock stops (LCDC, sound, main-clock timers stop); woken by external interrupts and timers on external clocks; then a warm-up of 2^15-2^18 main-clock periods (CKC bits 1-0). - **Clock change**: CKC's FCPUS bits take effect only when STOP is executed with FCPUEN = 1 (DS p. 19; p. 27 note "The STOP instruction is also used for clock change function"). The firmware writes CKC = A3h (FCPUEN = 1, FCPUS = 100: fCK/2; OBSERVED in MAME, gamecom-python mame-oracle.md), so the console runs at 5.53 MHz after its first STOP. - DS p. 37: "After setting each parameter, set the DMA start bit to '1' and execute HALT instruction, then DMA transfer starts" — the LCDC's DMA runs while the CPU is halted. The firmware executes HALT 148 times in 2 s (OBSERVED, MAME trace). The board owns DMA; the core owns entering HALT and being woken. ## Contradictions inside the Sharp documents - C1 above (IM table). - **C2** DS p. 8 says "9 CPU control instructions" and Table 1 lists eight (COMC, CLRC, DI, EI, HALT, NOP, SETC, STOP), totalling 67; DS p. 53's Rotate and Shift summary omits RL, which Table 1 lists. Read as 67 instructions with RL present. - **C3** DS p. 52 gives RET's function the text "(Logical Exclusive OR Word)", a copy slip. - **C4** DS calls the clock register "CKKC" in the text (p. 15, 19) and "CKC" in the map.