# Encodings and timing: what is known, and how the build closes the gap ## What Sharp's documents give - The instruction list and one-line semantics (DS p. 51-54; DB96 Table 1). - The 23 addressing modes with their ranges (DS Table 2, p. 9; DB96 Table 2). - Timing in general only (DS p. 15): the CPU **prefetches** the next opcode during the current instruction's execution; internal RAM reads and writes take **2 cycles**; external memory reads and writes take **2 cycles** (Fig. 5, 6: one RDB/WRB pulse of about one system-clock period, tSYS = 2/fCK at full speed); and one worked example: "the execution time for 2 bytes instructions (MOV R, r) of transferring the RAM contents to a register is **4 cycles**" (Fig. 4: opcode fetch, operand fetch, RAM read, register write, overlapped with the next opcode fetch). They give **no opcode map, no operand byte layouts, no per-instruction cycle counts, no flag table per instruction** (flags are described per flag, p. 12). Sharp's "SM85CPU user's manual", if it existed, is not known to be public (searched 2026-11-26: bitsavers' Sharp folder, datasheet archives; not found). ## What else exists | Source | Gives | Limits | | --- | --- | --- | | HDK `Hexcode instructions.txt` (MIT) | a first-byte map 00-FF: e.g. 00-0F one-operand ops on R (CLR NEG COM RR RL RRC RLC SRL INC DEC SRA SLL DA SWAP PUSH POP), 10-17 r,r ALU, 40-48 R,R ALU + MOV, 50-58 R,#imm, 60-6F word ALU, 70-77 DBNZ, 78-7F MOVW rr,#, 90-9F JMP cc, B0-BF MOV r,R / R,r, C0-C7 MOV r,#, D0-DF BR cc, E0-EF CALS 1x00, F0-FF STOP HALT RET IRET CLRC COMC SETC EI DI NOP | marked V1 with TODOs (BBC/BBS operands, 4E/4F); some rows visibly wrong (A8/A9 "BCLR #08/#09"); a `DM` mnemonic (2D, 3D) that is not in Sharp's list; no cycles; no flags | | MAME 0.285 binary | a complete decoder and a timing model, observable from outside: its debugger disassembles any byte string and its trace gives the cycle count between instructions | a model by its authors, not a measurement; its source (BSD-3) is not read here | | The firmware (FW) | 130 of 256 first opcode bytes executed in 2 s of the BIOS (OBSERVED, MAME) and every instruction real games use | only what it executes, only as MAME runs it | ## Agreement found so far (2026-11-26) DS's one worked example, `MOV R, r` = 4 cycles, against MAME's trace of the firmware: first byte B8-BF (HDK: `MOV R, r`) took **4 cycles** in all 26,329 executions; B0-B7 (`MOV r,R`, 1,250) 4; C0-C7 (`MOV r,#`, 224) 4; NOP (FF) 2; DI (FE) 2; HALT (F1) 6; RET (F8) 10; IRET (F9) 12; CALL (49) 12; JMP cc (98, 9E) 6; BR cc (D6) 4 not taken, 8 taken (`tests/detectors/mame-gamecom-bios-opcodes-2026-11-26.txt`). One tier-2 point agrees with the detector; everything else in the table is MAME's word alone. ## How the build closes the gap (proposed: decision 1 in the brief) 1. **Decode map by closed-box measurement.** Write each first byte (and for each, the operand patterns that matter: register fields, the `t` mode bits HDK identifies in 1A, 1B, 20-29, 30-3B) into RAM in a MAME session and record its disassembly and length with the debugger's `dasm`. The output (mnemonic, operands, length per pattern) is a closed-box measurement, tagged OBSERVED, cross-checked row by row against HDK and against DS's addressing-mode table. MAME's BSD-3 source stays unread. 2. **Semantics from DS**, one handler per instruction with DS's page in its docstring; flags per DS p. 12's definitions; where DS is silent (e.g. V on INC, H on word ops, B after BTST) the handler says HYPOTHESIS and the MAME lockstep decides only as a detector. 3. **Timing as data**: a table of cycles per (opcode, addressing mode), seeded from MAME's traces (OBSERVED), with DS's bus rules (2 cycles per memory access, prefetch overlap) as the model that explains each row. A row that DS's rules cannot explain is a contradiction to record, not to fit away. 4. **Lockstep with MAME** on the firmware and on cartridges (per instruction: PC, PS0, PS1, SP, RR0-RR14 as MAME names them, cycle deltas), every difference listed with the side DS supports. Modelling level (axis B): **instruction level with per-instruction cycle counts**, and the prefetch made visible where DS says software can see it (the MMU0 switch). A bus-cycle level model would claim an order of accesses no source gives.