# Start here: the MB88xx as this core will model it This page is for someone who has written a Z80 or 6502 interpreter and has never met a 4-bit Fujitsu MCU. Almost nothing transfers. There is no 8-bit accumulator, no 16-bit program counter, no condition-code branch, no general stack. What there is instead is set out below, and with it the opcode table at the bottom reads as a direct statement of the hardware. ## Sources, and what may be copied from them Two sources, and every fact on this page comes from one of them. Where they disagree, the disagreement is stated rather than resolved. | Tag | Source | License / status | | --- | --- | --- | | **DS** | Fujitsu, *MB8840/MB8840H Series NMOS Single-Chip 4-Bit Microcomputer*, document TM336-A871, January 1987, 39 pages. Tables 1 (pin description), 4 (mask options), 5 (instruction set summary), 7 (product lineup). Retrieved 2026-09-12 from `https://web.archive.org/web/2023/https://original.sharpmz.org/download/mb8843.pdf` (the live `sharpmz.org` copy 404s). | A 1987 Fujitsu datasheet. Cited by table and page; the scan is **not** redistributed here, and its OCR quality is poor enough that two readings are flagged below. | | **MAME** | MAME `src/devices/cpu/mb88xx/mb88xx.cpp`, `mb88xx.h`, `mb88dasm.cpp`, at `master` fetched 2026-09-12; `src/mame/namco/namco{06,50,51,52,53,54}.cpp`, `namco/galaga.cpp`, `namco/polepos.cpp`. | **BSD-3-Clause**, copyright-holders Ernesto Corvi (CPU core) and Aaron Giles / Mike Harris (Namco devices). The BSD-3-Clause licence permits redistribution of the source "in source or binary form, with or without modification", provided the copyright notice, the condition list and the disclaimer are retained. Transcribing the opcode semantics into prose and tables here, with the attribution above, is within that grant. This repository does **not** vendor the files. | Where this page says "MAME's choice" it means behaviour present in the emulator that the datasheet does not state. Those are the interesting places, and [validation.md](validation.md) collects them. ## The chip in one paragraph A single-chip 4-bit NMOS microcomputer with mask ROM and static RAM on board, introduced 1977, "same instruction set as the MB8850 series" (DS, front page). 69 or 70 instructions, 93 % of them one byte and one cycle, the rest two bytes and two cycles (DS, "Instruction Set Description"). One instruction cycle is six clock periods. Four input-only lines (K), eight latched output lines (O) behind a mask-programmed PLA, four latched output lines (P), sixteen bidirectional lines (R) in four nibble ports, an 8-bit timer/counter with two clock sources, a 4-bit serial port on the larger parts, one external interrupt pin, and a 4-level subroutine stack. ## The family DS Table 7, with MAME's device widths beside it. `program_width` and `data_width` are the constructor arguments in `mb88xx.cpp`. | Part | ROM | RAM | I/O lines | Serial | Instructions | Package | MAME device | prog / data width | | --- | --- | --- | --- | --- | --- | --- | --- | --- | | MB8841H (MB8845H) | 2 K × 8 | 128 × 4 | 37 | yes | 70 | 42-pin | `mb8841` | 11 / 7 | | MB8842 (MB8846) | 2 K × 8 | 128 × 4 | 25 | **no** | 69 | 28-pin | `mb8842` | 11 / 7 | | MB8843 (MB8847) | 1 K × 8 | 64 × 4 | 37 | yes | 70 | 42-pin | `mb8843` | 10 / 6 | | MB8844 (MB8848) | 1 K × 8 | 64 × 4 | 25 | **no** | 69 | 28-pin | `mb8844` | 10 / 6 | | MB88201 / MB88202 | 512 B / 1 K | 16 / 32 × 4 | 16-pin part | no | — | 16-pin | `mb88201/2` | 9 / 4, 10 / 5 | The four chips this project is aimed at are the middle four; the 16-pin MB8820x are in MAME's core for other hardware and cost nothing to support. Two unresolved points, both flagged here rather than guessed: - DS says the instruction count differs by exactly one between the 42-pin and 28-pin parts (70 vs 69) but never names the missing instruction. In the scan, note `*4` — "MB8841H/43/45H/47" — is printed against the **OUTO** row of Table 5, which cannot be right: DS Table 7 gives all four parts the O-port PLA, and Namco's 54xx is an MB8844 whose program executes OUTO several thousand times a second (measured, see [mame-oracle.md](mame-oracle.md)). The note most likely belongs to an adjacent serial row. **Unverified.** - DS Table 7 says "Process: CMOS" for parts the same document's title page calls NMOS. An editing error in the datasheet; immaterial here. MAME implements one instruction table for every variant and does not withhold the serial instructions from MB8842/MB8844. Since neither Namco 28-pin part binds SI or SO, nothing in this project's scope depends on it. ## Registers Every register is 4 bits unless stated. | Name | Width | Meaning | | --- | --- | --- | | A | 4 | Accumulator | | X | 4 | Data-memory page register (the high nibble of the RAM address) | | Y | 4 | Data-memory offset, **and** the port number for `IN`/`OUT`, **and** the bit number for `SETR`/`RSTR`/`TSTR`, **and** a loop counter with its own increment/decrement instructions | | PC | 6 | Program counter *within a page* | | PA | 4 (5 in the core) | Page address; see below | | SP[0..3] | 10 + 3 | Four-level stack; see below | | SI | 2 | Stack index (which of the four levels is next) | | TH, TL | 4 + 4 | Timer/counter, high and low nibble — one 8-bit counter | | TP | — | Internal timer prescale accumulator (not architecturally visible) | | SB | 4 | Serial buffer / shift register | | PIO | 8 | The enable byte that `EN` and `DIS` set and clear bits in | Y is the busiest register on the chip. Reading an opcode that mentions a port number, a bit number or a memory offset means reading Y. ## Data memory: `(X << 4) + Y` RAM is an array of nibbles addressed by the pair (X, Y): address `(X << 4) + Y`, which MAME writes as `GETEA()`. X selects one of up to 8 "pages" of 16 nibbles, Y the nibble within it. MB8843/MB8844 have 64 nibbles, so only X = 0..3 exist; MB8841/MB8842 have 128, X = 0..7. Every instruction written `M(X,Y)` below — `L`, `ST`, `ADC`, `SBC`, `AND`, `OR`, `EOR`, `C`, `ICM`, `DCM`, `X`, `STIC`, `STDC`, `SBIT`, `RBIT`, `TBIT`, `STS`, `LS` — uses that address and nothing else. There is no displacement, no immediate address, no indirection through RAM. Four opcode groups are the exception: `XD D` (0x50-0x53) and `XYD D` (0x54-0x57) address `M(0, D)` with D taken from the opcode — RAM addresses 0-3 and 4-7 — and touch neither X nor Y. They exist so a program can keep four scratch nibbles and four Y-saves reachable without spending X. DS Table 7 counts them as the "directly addressed locations (0-7)", and DS's abbreviation list writes them `M(0,D)`: "data memory location directly addressed by the D bits in the instruction code, in page #0 (X=0)". ## Program memory: 6-bit PC in a page, plus PA The program counter is **6 bits**. It counts within a 64-byte page. The page register PA supplies the rest of the address: ```text full address = (PA << 6) | PC ``` MAME's `GETPC()`, and the single most important thing to know when reading a MAME trace of one of these chips: the trace prints PA and PC as separate values and neither alone is the address. `INCPC()` is `PC++; if (PC >= 0x40) { PC = 0; PA++; }` — the page register carries. So straight-line code crosses pages freely; only the *branches* care about pages: | Form | Reach | | --- | --- | | `JMP addr` (0xC0-0xFF) | 6-bit target **in the current page**, one byte, one cycle. This is why the whole top quarter of the opcode map is one instruction. | | `JPL addr` (0x68-0x6F + byte) | 11-bit target anywhere: PA ← `((op & 7) << 2) | (arg >> 6)`, PC ← `arg & 0x3F` | | `CALL addr` (0x60-0x67 + byte) | same address arithmetic, and pushes | | `JPA addr` (0x3D + byte) | computed jump: PA ← `arg & 0x1F`, PC ← `A * 4`. Sixteen four-byte slots in a chosen page, indexed by the accumulator — the family's jump table | PA is 4 bits on the 1 KB parts and 5 on the 2 KB parts (32 pages × 64 bytes = 2048). MAME stores 5 bits unconditionally and masks the address with the memory map, so on an MB8843/MB8844 the top bit mirrors. A core should mask PA to the part's width and say so. Note that `INCPC` does not mask PA. A program that runs off the end of the last page has PA = 0x20 and an address of 0x800, outside every variant's ROM. MAME's address map mirrors; hardware presumably wraps. **Unverified**; no real program does it. ## ST: the skip flag, and why there are no conditional branches There is exactly one conditional mechanism on this chip. `CALL`, `JPL` and `JMP` execute **only if ST = 1**, and every one of them sets ST back to 1 afterwards whether it branched or not. ST is set to 1 by almost every instruction. The instructions that can clear it are the tests and the arithmetic: - A **test** (`TSTC`, `TSTZ`, `TSTI`, `TSTV`, `TSTS`, `TSTR`, `TSTD`, `TBIT`, `TBA`) sets `ST = !condition`. The condition *true* clears ST. - An **arithmetic** instruction with a carry or borrow out of bit 3 clears ST (`ADC`, `SBC`, `AI`, `ROL`, `ROR`, `DAA`, `DAS`, `ICY`, `ICM`, `DCY`, `DCM`, `STIC`, `STDC`). - A **comparison** (`C`, `CI`, `CYI`) and `NEG` and `EOR` clear ST when the result is **zero**. So the idiom is inverted from what a 6502 programmer expects: ```text TSTC ; ST = 0 if carry is set JMP carry_clear_path ; taken only when carry was clear ... ; falls through here when carry was set ``` ST is not a user flag: it lives one instruction, it cannot be read, and it is restored to 1 by the branch that consumed it. In MAME it is `m_st` and the macros that write it are `UPDATE_ST_C(v)` (`st = (v & 0x10) ? 0 : 1`) and `UPDATE_ST_Z(v)` (`st = (v == 0) ? 0 : 1`). Both are *inverting*: this is the single most common source of sign errors when writing one of these cores. ## The other flags | Flag | Width | Set by | Read by | | --- | --- | --- | --- | | **ST** | 1 | above | `CALL`, `JPL`, `JMP`; saved by an interrupt, restored by `RTI` | | **ZF** | 1 | result-is-zero for the instructions marked Z below. **ZF = 1 means the result was zero** (`UPDATE_ZF(v)` is `zf = (v != 0) ? 0 : 1`) | `TSTZ`; saved by an interrupt, restored by `RTI` | | **CF** | 1 | carry or borrow out of bit 3 for the arithmetic and rotate instructions; `SETC` / `RSTC` set and clear it | `ADC`, `SBC`, `ROL`, `ROR`, `DAA`, `DAS`, `OUTO` (selects the O-port nibble), `TSTC`; saved by an interrupt, restored by `RTI` | | **VF** | 1 | set when the 8-bit timer overflows out of TH | `TSTV`, which reads it **and clears it** | | **SF** | 1 | set when the serial buffer has taken four bits | `TSTS`, which reads it **and clears it** | | **IF** | 1 | tracks the /IRQ pin level: IF = 1 while /IRQ is low (DS, Table 1, IRQ row). Not an edge latch and not maskable | `TSTI` | Three of these — CF, ZF, ST — survive an interrupt because the interrupt sequence stashes them in the stack word. VF, SF and IF do not. `TSTV` and `TSTS` are destructive reads; a core that treats them as pure tests will hang a program that polls the timer. ## The stack: four levels, 10 bits, plus three flag bits `SP[0..3]`, indexed by the 2-bit `SI` which wraps. `CALL` writes `SP[SI] = (PA << 6) | PC` — the address of the instruction *after* the call, PC having already been advanced past the operand byte — then `SI = (SI + 1) & 3`. `RTS` does the reverse and sets ST = 1. DS gives the depth (Table 7: "Stack Depth 4 levels") and nothing about its contents. MAME stores each entry as a 16-bit word and uses three otherwise unused bits so that an interrupt can save the flags without a separate register: ```text bit: 15 14 13 12 11 10 9 ... 6 5 ... 0 CF ZF ST - - <---- PA ----> <- PC -> ``` `RTS` restores PC and PA only. `RTI` restores PC, PA, **and ST, ZF, CF** from bits 13-15. Where the real chip keeps those three bits during an interrupt is not documented; MAME's choice is observationally equivalent as long as a program does not nest an interrupt four deep (it cannot: DS says the interrupt is single level). Overflow is silent. Five nested calls overwrite the first return address. ## Interrupts Three sources, "single-level three priority source maskable interrupt" (DS, Features). Single level means an interrupt cannot interrupt an interrupt — MAME enforces that with an `m_in_irq` flag that `RTI` clears. Each source has an enable bit in the PIO byte, written by `EN` and `DIS`. The complete PIO byte, from DS Table 5 note `*3` — this is documentation, not a guess: | Bit | Meaning | | --- | --- | | 0 | enable/disable **serial buffer full/empty** interrupt | | 1 | enable/disable **timer/counter overflow** interrupt | | 2 | enable/disable **external (/IRQ)** interrupt | | 3 | enable/disable synchronous timing output (TO) on the SC/TO pin | | 4 | start/stop serial port, **external** clock (SC) | | 5 | start/stop serial port, **internal** clock | | 6 | start/stop counter, **external** clock (TC pin) | | 7 | start/stop timer, **internal** clock | `EN imm` sets the bits of `imm`; `DIS imm` clears them. Both are two bytes, two cycles, and set ST = 1. MAME reads exactly this map: `INT_CAUSE_SERIAL 0x01`, `INT_CAUSE_TIMER 0x02`, `INT_CAUSE_EXTERNAL 0x04`, `m_pio & 0x40` for the external counter, `m_pio & 0x80` for the internal timer, `m_pio & 0x30` for the serial clock source. It rejects `0x10` and `0x30` with a fatal error: only internal-clock serial (`0x20`) is implemented. Bit 3 (TO) is ignored entirely. ### Requesting - **External**: DS says "the falling edge of /IRQ sets the external interrupt request flag ... only if the external interrupt is enabled in advance by EN". MAME models the pin inverted (`execute_set_input` takes a logical level and latches on the *rising* logical edge, with a comment saying the pin is voltage low-triggered) and requires `m_pio & 0x04` at the moment of the edge — so an edge that arrives while the source is disabled is **lost**, not deferred. - **Timer**: the 8-bit counter overflowing out of TH sets VF and requests. - **Serial**: the fourth bit shifted in sets SF and requests. A request is a bit in a pending mask. The mask is cleared wholesale on entry, so two sources requesting between one instruction boundary and the next produce one interrupt and the lower-numbered source's vector. ### Entry sequence (MAME, `burn_cycles`) Checked after every instruction, at the instruction boundary, when `!in_irq && (pending & pio)`: 1. `SP[SI] = (PA << 6) | PC` — the address of the next instruction — with CF in bit 15, ZF in bit 14, ST in bit 13; `SI = (SI + 1) & 3`. 2. `PC` ← the vector; `PA` ← 0; `ST` ← 1; pending mask ← 0; `in_irq` ← true. 3. **3 cycles** are charged (and those 3 cycles also advance the timer). | Source | Vector (PA = 0) | Priority | | --- | --- | --- | | External /IRQ | `PC = 0x02`, address `0x002` | highest | | Timer overflow | `PC = 0x04`, address `0x004` | middle | | Serial | `PC = 0x06`, address `0x006` | lowest | **These three addresses and the 3-cycle charge are MAME's, not Fujitsu's.** The source says so in a comment: "the datasheet doesn't mention interrupt vectors but the Arabian MCU program expects the following", and the cycle count carries a bare `// ?`. The 39-page datasheet retrieved for this document indeed does not give them. See [validation.md](validation.md). They are, however, *consistent with what Namco's programs do*: a trace of Galaga's 54xx (an MB8844) shows the external interrupt entering at `0x002` exactly 3 cycles after the last instruction of the interrupted stream, and the 51xx (an MB8843) the same, 7,140 times in one run. Two independent programs agreeing with a vector inferred from a third is evidence, not proof — all three were disassembled against the same emulator. `RTI` (0x3C) is one byte, one cycle: pop PC, PA, ST, ZF, CF, clear `in_irq`. It does **not** re-enable anything; the PIO bits were never touched. ## Timer/counter One 8-bit up-counter presented as two nibbles, TH (high) and TL (low), both readable (`TTHA`, `TTLA`) and writable (`TATH`, `TATL`) through A. Incrementing TL past 15 carries into TH; TH past 15 sets VF and requests the timer interrupt. Two clock sources, selected independently by PIO bits 6 and 7, and both may be on at once: - **External (counter), PIO bit 6.** The falling edge of the /TC pin increments the counter by one (DS, Table 1, TC row). On the 28-pin parts /TC shares pin 22 with R9 and the choice is made in software. - **Internal (timer), PIO bit 7.** The counter increments every *N* instruction cycles. **MAME's N is 32, marked `/* guess */` in the source**, accumulated in `m_TP` inside `burn_cycles`. The datasheet retrieved here does not give a prescale ratio. See [validation.md](validation.md). Namco's 51xx uses the external source with vblank on /TC; no Namco part in scope uses the internal source, so the guessed 32 is not on this project's critical path — but a core must implement it, and cannot prove it. ## Serial port (MB8841/MB8843 only) A 4-bit shift register, SB, reachable as `TAS` (SB ← A), `TSA` (A ← SB), `STS` (M ← SB), `LS` (SB ← M), and tested by `TSTS`. Bits arrive on SI. MAME shifts right and inserts at bit 3: `SB = (SB >> 1) | (SI ? 8 : 0)`; after four bits SF is set and the serial interrupt requested. While SF is already set no bit is taken — a deliberate concession to the Namco 52xx, whose program writes SB and expects to read it back with the serial port enabled. Clock: DS offers an internal source (PIO bit 5) and an external SC pin (bit 4). MAME implements the internal one only, at `clock() / 6` — one bit per instruction cycle — with `SERIAL_PRESCALE 6` marked `/* guess */`, and throws a fatal error on the external setting. It also stops shifting after 1,000 unserviced bits and restarts on the next `TSTS`, which is an emulator housekeeping device with no hardware counterpart. Neither Namco chip in Galaga binds SI or SO. The 52xx (Bosconian, Pole Position) binds SI to a fixed level as a ROM-addressing mode select, which is a use of the pin, not of the serial port. ## The output PLA and the O port The O port is eight latched output lines driven by one instruction, `OUTO`, which presents a **5-bit index** — the accumulator in bits 0-3 and the carry flag in bit 4 — to a mask-programmed array. DS Table 1's O-port row gives the two mask options (Table 4, "Output PLA SPLA"): - **Dual 4-bit parallel output.** A goes out unconverted onto O3-O0 when CF = 0 and onto O7-O4 when CF = 1; the other nibble holds its previous value. This is the option Namco used: it is how the 54xx drives two independent 4-bit noise channels from one port, and how a trace tells the two channels apart. - **8-bit parallel PLA output.** The 5-bit index selects one of 32 customer-specified 8-bit patterns, all eight lines change together. DS gives LED seven-segment encoding as the motivating example. DS Table 7 counts this as "33 patterns": 32 PLA words plus the dual-4-bit option. MAME's spelling of the same choice is inverted and is a trap for the reader: `set_pla_bits(8)` — the default — selects the **dual 4-bit** mode (`write_pla` shifts the index into the half chosen by bit 4 and writes a half-width mask), and `set_pla_bits(4)` with `set_pla_data(ptr)` selects the **PLA-converted** mode, falling back to the identity map when no driver supplies the data. No Namco device in scope supplies PLA data. The mask the device callback receives says which nibble moved: `0x0f`, `0xf0` or `0xff`. A host that ignores the mask and latches all eight bits will corrupt the other channel. ## Reset DS, Table 1: "A low level on the /RESET pin forcedly stops the MCU's operations and initializes its internal state. After the /RESET pin returns high, the MCU restarts execution of program from **address #0 (of page #0)**. The /RESET pulse must be low for at least two instruction cycles (12 clock periods)." That parenthesis is the clean statement of the timing rule: two instruction cycles are twelve clock periods, so **one instruction cycle is six clock periods** — see [timing.md](timing.md). MAME's `device_reset` state, which a core should reproduce exactly: | Field | Value | | --- | --- | | PC, PA | 0, 0 | | SP[0..3], SI | 0, 0 | | A, X, Y | 0, 0, 0 | | **ST** | **1** | | ZF, CF, VF, SF | 0 | | PIO | 0 — every interrupt and both clock sources disabled | | TH, TL, TP | 0 | | SB, serial bit count | 0 | | pending interrupts, in-IRQ | none, false | | IF | *not* reset: it is the /IRQ pin level and is whatever the pin says | The output latches are a mask option (DS Table 4, "Output Port Level During Reset": low, or O3-O0 and R3-R0 fixed high). MAME does not model it; a host that cares must drive its own initial port values. There is also a factory test mode: forcing O0 low while /RESET is low enters it (DS, "Notes on Operation"). A core should ignore this; a host must simply not do it. ## The opcode map All 256 opcodes. Cycles are from DS Table 5, whose cycle column reads "1/1" for every one-byte instruction and "2/2" for the five two-byte ones — `EN`, `DIS`, `CALL`, `JPA`, `JPL` — and MAME charges the same. The flag columns use these conventions: - **ST** — `1` means set to 1 unconditionally; `C` means cleared by a carry or borrow out of bit 3 and set otherwise; `Z` means cleared by a zero result and set otherwise; `!x` means set to the negation of the test. - **ZF** — `Z` means set when the named value is zero, cleared otherwise. - **CF** — `C` means set by the carry or borrow; `1`/`0` unconditional. - A blank means untouched. `M` is `M(X,Y)`, the nibble at `(X << 4) + Y`. `R[n]` is R-port *n*. ### 0x00-0x1F: transfers, memory, arithmetic | Op | Mnemonic | Operation | ST | ZF | CF | | --- | --- | --- | --- | --- | --- | | 00 | `NOP` | — | 1 | | | | 01 | `OUTO` | O ← PLA(A, CF) | 1 | | | | 02 | `OUTP` | P ← A | 1 | | | | 03 | `OUT` | R[Y & 3] ← A | 1 | | | | 04 | `TAY` | Y ← A | 1 | | | | 05 | `TATH` | TH ← A | 1 | | | | 06 | `TATL` | TL ← A | 1 | | | | 07 | `TAS` | SB ← A | 1 | | | | 08 | `ICY` | Y ← Y + 1 | C | Z(Y) | | | 09 | `ICM` | M ← M + 1 | C | Z(M) | | | 0A | `STIC` | M ← A, Y ← Y + 1 | C | Z(Y) | | | 0B | `X` | A ↔ M | 1 | Z(A) | | | 0C | `ROL` | CF → A → CF, rotate left | C | Z(A) | C | | 0D | `L` | A ← M | 1 | Z(A) | | | 0E | `ADC` | A ← M + A + CF | C | Z(A) | C | | 0F | `AND` | A ← A & M | !Z | Z(A) | | | 10 | `DAA` | A ← A + 6 if CF or A > 9 | C | | C | | 11 | `DAS` | A ← A + 10 if CF or A > 9 | C | | C | | 12 | `INK` | A ← K | 1 | Z(A) | | | 13 | `IN` | A ← R[Y & 3] | 1 | Z(A) | | | 14 | `TYA` | A ← Y | 1 | Z(A) | | | 15 | `TTHA` | A ← TH | 1 | Z(A) | | | 16 | `TTLA` | A ← TL | 1 | Z(A) | | | 17 | `TSA` | A ← SB | 1 | Z(A) | | | 18 | `DCY` | Y ← Y − 1 | C | | | | 19 | `DCM` | M ← M − 1 | C | Z(M) | | | 1A | `STDC` | M ← A, Y ← Y − 1 | C | Z(Y) | | | 1B | `XX` | A ↔ X | 1 | Z(A) | | | 1C | `ROR` | CF → A → CF, rotate right | C | Z(A) | C | | 1D | `ST` | M ← A | 1 | | | | 1E | `SBC` | A ← M − A − CF | C | Z(A) | C | | 1F | `OR` | A ← A \| M | !Z | Z(A) | | `DCY` (0x18) is the one arithmetic instruction that leaves ZF alone; MAME and DS agree. `AND` and `OR` set ST from the *result being non-zero* (`st = zf ^ 1`), which is the opposite polarity from `EOR` — see below. ### 0x20-0x3F: ports, bits, tests, control | Op | Mnemonic | Operation | ST | ZF | CF | | --- | --- | --- | --- | --- | --- | | 20 | `SETR` | R line Y (of 16) ← 1 | 1 | | | | 21 | `SETC` | CF ← 1 | 1 | | 1 | | 22 | `RSTR` | R line Y (of 16) ← 0 | 1 | | | | 23 | `RSTC` | CF ← 0 | 1 | | 0 | | 24 | `TSTR` | test R line Y | !R | | | | 25 | `TSTI` | test IF (the /IRQ pin) | !IF | | | | 26 | `TSTV` | test VF, **then clear VF** | !VF | | | | 27 | `TSTS` | test SF, **then clear SF** | !SF | | | | 28 | `TSTC` | test CF | !CF | | | | 29 | `TSTZ` | test ZF | !ZF | | | | 2A | `STS` | M ← SB | 1 | Z(SB) | | | 2B | `LS` | SB ← M | 1 | Z(SB) | | | 2C | `RTS` | pop PC, PA | 1 | | | | 2D | `NEG` | A ← −A | Z | | | | 2E | `C` | compare M − A, result discarded | Z | !ST | C | | 2F | `EOR` | A ← A ^ M | Z | !ST | | | 30-33 | `SBIT b` | M bit *b* ← 1, b = op & 3 | 1 | | | | 34-37 | `RBIT b` | M bit *b* ← 0 | 1 | | | | 38-3B | `TBIT b` | test M bit *b* | !M.b | | | | 3C | `RTI` | pop PC, PA, ST, ZF, CF; leave interrupt | (popped) | (popped) | (popped) | | 3D | `JPA imm` | PA ← imm & 0x1F, PC ← A × 4 — **2 bytes, 2 cycles** | 1 | | | | 3E | `EN imm` | PIO ← PIO \| imm — **2 bytes, 2 cycles** | 1 | | | | 3F | `DIS imm` | PIO ← PIO & ~imm — **2 bytes, 2 cycles** | 1 | | | `C`, `NEG` and `EOR` write `ST = (result == 0) ? 0 : 1`, so a zero result *clears* ST; `C` and `EOR` then copy `ZF = ST ^ 1`. Line for line that is the opposite of `AND`/`OR`, which compute ZF first and derive ST from it. Both reach the same place; the order matters only when transcribing. `SETR`, `RSTR` and `TSTR` take a bit number 0-15 in Y across all four R ports: MAME uses `Y >> 2` for the port and `Y & 3` for the bit. All three read the port through the host's `read_r` callback, modify and write back — so a line configured as an input reads the *external* value, and writing it back is a real effect a host must expect. DS's caution that a line must be "set up to 1 (input mode)" before `TSTR`/`TSTD` is the same fact from the hardware side. ### 0x40-0x5F: direct R-port bits, direct RAM, X immediate | Op | Mnemonic | Operation | ST | ZF | CF | | --- | --- | --- | --- | --- | --- | | 40-43 | `SETD d` | R0 bit *d* ← 1, d = op & 3 (lines R3-R0) | 1 | | | | 44-47 | `RSTD d` | R0 bit *d* ← 0 | 1 | | | | 48-4B | `TSTD d` | test R2 bit (op & 3) — DS names these d = 8..11, lines R11-R8 | !R2.b | | | | 4C-4F | `TBA b` | test A bit *b*, b = op & 3 | !A.b | | | | 50-53 | `XD D` | A ↔ M(0, D), D = op & 3 → RAM 0-3 | 1 | Z(A) | | | 54-57 | `XYD D` | Y ↔ M(0, D), D = (op & 3) + 4 → RAM 4-7 | 1 | Z(Y) | | | 58-5F | `LXI imm` | X ← op & 7 | 1 | Z(X) | | `LXI` loads only 3 bits, so X is always 0-7 — the 8 RAM pages of the 128-nibble parts. On MB8843/MB8844 (64 nibbles) values 4-7 address memory the part does not have; MAME's data map mirrors. ### 0x60-0xBF: call, jump, and the immediates | Op | Mnemonic | Operation | Bytes / cycles | ST | ZF | CF | | --- | --- | --- | --- | --- | --- | --- | | 60-67 | `CALL addr` | if ST: push next address, PA ← ((op & 7) << 2) \| (arg >> 6), PC ← arg & 0x3F | 2 / 2 | 1 | | | | 68-6F | `JPL addr` | if ST: same target, no push | 2 / 2 | 1 | | | | 70-7F | `AI imm` | A ← A + (op & 0x0F) | 1 / 1 | C | Z(A) | C | | 80-8F | `LYI imm` | Y ← op & 0x0F | 1 / 1 | 1 | Z(Y) | | | 90-9F | `LI imm` | A ← op & 0x0F | 1 / 1 | 1 | Z(A) | | | A0-AF | `CYI imm` | compare (op & 0x0F) − Y | 1 / 1 | Z | !ST | C | | B0-BF | `CI imm` | compare (op & 0x0F) − A | 1 / 1 | Z | !ST | C | `CALL` and `JPL` read and skip their operand byte **whether or not ST is set**, so a not-taken call costs the same two bytes and two cycles as a taken one. `JPA` (0x3D) reads its operand without advancing PC, because PC is about to be overwritten regardless. Three assembler aliases DS lists as separate rows of Table 5 and MAME's disassembler does not: `CLA` = `LI 0` = 0x90, `ICA` = `AI 1` = 0x71, `DCA` = `AI 15` = 0x7F (A + 15 is A − 1 in 4 bits). ### 0xC0-0xFF: `JMP` One instruction filling 64 opcodes: `JMP addr`, one byte, one cycle. If ST = 1 then `PC ← op & 0x3F`, leaving PA alone; either way ST ← 1. The target is in the *current* page, and "current" means the page of the byte **after** the JMP — PC has already been incremented, so a `JMP` in the last byte of a page branches within the *next* page. MAME's disassembler prints `(pc & ~0x3f) + op - 0xc0` with `pc` already advanced, which encodes that. ## Cycle counts, complete There are only two values. | Cycles | Opcodes | | --- | --- | | 2 | `JPA` (3D), `EN` (3E), `DIS` (3F), `CALL` (60-67), `JPL` (68-6F) | | 1 | everything else, all 246 of them | | +3 | an accepted interrupt, charged after the instruction that was running (MAME's figure, marked a guess) | Not taken does not make a branch cheaper. `mb88xx.h` declares `execute_max_cycles() = 2 + 3`, which is that table. ## What to read next - [timing.md](timing.md) — the 6-clocks-per-cycle rule, the Namco clocks, and how to schedule one of these against a Z80. - [namco-customs.md](namco-customs.md) — which chip is which, its ROM hashes, its pin wiring and its protocol. - [validation.md](validation.md) — every oracle, its tier, and the four places a core cannot be proven right. - [mame-oracle.md](mame-oracle.md) — how to get a per-instruction trace of a real Namco MCU out of MAME, verified on this machine. - [handoff-brief.md](handoff-brief.md) — the brief for the session that writes the core.