# The MCS-48 as this core will model it Every fact on this page names its source. The two sources are: - **Intel, *MCS-48 Family of Single Chip Microcomputers User's Manual*, order 9800270D, July 1978.** (396 pages, 18 MB, with an OCR text layer). Cited as **"UM §x.y, p. n-m"** using the manual's own section numbers and its printed page numbers (`2-6`, `4-5`, ...). Copyright Intel; cited, never redistributed. The 1980 edition and the *MCS-48 and UPI-41 Assembly Language Manual* (9800255D, December 1978) are in the same bitsavers directory and say the same things with different pagination; page citations here are to 9800270D only. - **MAME 0.285, `src/devices/cpu/mcs48/mcs48.cpp`, `mcs48.h` and `mcs48dsm.cpp`**, BSD-3-Clause, copyright Dan Boris, Mirko Buffoni, Aaron Giles and Couriersud. The BSD-3-Clause licence permits transcription with attribution, and this document does transcribe from it: the opcode table below, the per-opcode cycle counts, and every behaviour the manual leaves ambiguous. Read at tag `mame0285`, commit `f379cd060ac3b2bd567ebbd0957aed059d6895ff`; the three files are pinned by hash in [`scripts/fetch_sources.py`](../scripts/fetch_sources.py). Where the two disagree, or where only MAME has an answer, the text says so. Where neither has an answer, the claim is tagged `[unverified]`. ## The shape of the machine An MCS-48 part is a whole computer on one die: 8-bit accumulator, a small internal RAM that holds both the working registers and the stack, an internal program ROM, three I/O ports, two test pins, an interrupt pin and a timer/counter. There is no address bus in the 8080 sense; there is a multiplexed BUS port that carries an address then an instruction or a data byte, with ALE, PSEN, RD and WR to say which (UM §3.1.1, p. 3-1; §3.2.1, p. 3-4). What the **core** owns: | | | | --- | --- | | A | the 8-bit accumulator | | PSW | CY, AC, F0, BS and the 3-bit stack pointer (UM §2.1.7, p. 2-6) | | F1 | a second user flag, **not** part of PSW | | PC | 12 bits (UM §2.1.6, p. 2-5) | | DBF | the program-memory bank flip-flop set by `SEL MB1`, reset by `SEL MB0` (UM §3.1.2, p. 3-2) | | internal RAM | 64, 128 or 256 bytes depending on the part, containing R0-R7 of both banks and the 8-level stack | | T | the 8-bit timer/event counter, its 5-bit prescaler, its overflow flag and its overflow latch | | interrupt state | the external and timer enable flip-flops and the "interrupt in progress" flip-flop | What the **host** owns (and therefore what the host contract must expose): | Host call | Used by | Notes | | --- | --- | --- | | `read_program(addr12)` | every fetch, `MOVP`, `MOVP3` | the host decides whether an address lands in internal ROM or external ROM; an 8035 has no internal ROM and every fetch is external | | `read_ext(addr8)` / `write_ext(addr8)` | `MOVX A,@Rr` / `MOVX @Rr,A` | 256 bytes, addressed by R0 or R1 through BUS | | `read_port(p)` / `write_port(p)` | `IN A,Pp`, `OUTL Pp,A`, `ANL`/`ORL Pp,#data`, `INS A,BUS`, `OUTL BUS,A` | p = 1, 2 or BUS | | `read_expander(p)` / `write_expander(p, nibble)` | `MOVD`, `ANLD`, `ORLD` for P4-P7 | the 8243 expander, reached over P2.0-P2.3 with the PROG strobe | | `read_t0()` / `read_t1()` | `JT0`/`JNT0`, `JT1`/`JNT1`, `STRT CNT` | levels, sampled when the instruction executes | | `set_int(level)` | the external interrupt | **level triggered and active low** (UM §2.1.9, p. 2-7); the core samples it, the host does not push a vector | | `write_t0_clock(state)` | `ENT0 CLK` | optional; the host may ignore it | The core never owns a memory map. Donkey Kong's sound 8035 reads external program memory 0x000-0xFFF and uses `MOVX` against a banked sample ROM; the Sega Universal Sound Board's 8035 uses `MOVX` against a RAM and a timer chip. Both are host business. ## Accumulator, register banks, and BS Eight working registers R0-R7 are directly addressable, and they live in internal RAM, not in a separate file. `SEL RB0` designates RAM 0-7 as R0-R7; `SEL RB1` designates RAM 24-31 (UM §2.1.3, p. 2-3 and p. 2-4). The choice is PSW bit 4, **BS**, so it is saved on `CALL` and restored by `RETR`, which is the whole point: an interrupt routine executes `SEL RB1`, uses its own eight registers, and `RETR` puts the main program's bank back. R0 and R1 are special twice over: they are the only indirect pointers into internal RAM (`@R0`, `@R1`) and the only address source for external data memory (`MOVX`). Bank switching therefore gives you *four* pointers -- R0, R1, R0', R1' -- reaching four working areas (UM p. 2-4). If bank 1 is never selected, RAM 24-31 are ordinary memory (UM p. 2-4). MAME models the bank as a pointer swap: `m_regptr = &m_dataptr[(m_psw & B_FLAG) ? 24 : 0]` (`mcs48.cpp`, `update_regptr`), recomputed on `SEL RB0`, `SEL RB1`, `MOV PSW,A`, `RETR` and reset. A Python core should do the same thing -- compute a base index, not copy registers -- or it will get `MOV PSW,A` wrong. ## The program counter, 2 KB pages, and DBF The PC is a 12-bit counter (UM §2.1.6, p. 2-5). It behaves in three different ways depending on the instruction, and this is the part of the architecture most likely to be implemented wrongly: 1. **Sequential execution** increments all 12 bits. The manual's diagram labels the conventional counter as counting `000H` to `7FFH` and overflowing `7FFH` to `000H` (UM p. 2-6, p. 3-2) -- that is, **bit 11 is not affected by incrementing**. 2. **`JMP addr` and `CALL addr`** are two bytes: the opcode carries A10-A8 in bits 7-5, the second byte carries A7-A0, and **bit 11 is loaded from the DBF flip-flop** (UM §3.1.2, p. 3-2: "Bit 11 is not altered by normal incrementing of the program counter but is loaded with the contents of a special flip-flop each time a JMP or CALL instruction is executed"). So `SEL MB1` does nothing until the next `JMP` or `CALL`. MAME: `execute_jmp(address)` does `m_pc = address | a11` where `a11` is `m_a11` (`mcs48.cpp`). 3. **Conditional jumps, `DJNZ`, and `JMPP @A`** change only the low 8 bits, keeping the page of the *second byte* of the instruction. MAME: `execute_jcc` reads the offset byte first, then `m_pc = (m_pc & 0xf00) | offset`. A conditional jump whose second byte is the last byte of a 256-byte page therefore jumps into the *following* page, which is a real hazard and is why assemblers warn about it. `JMPP @A` is `m_pc = (m_pc & 0xf00) | program_r((m_pc & 0xf00) | m_a)`. `CALL` pushes all twelve bits, so a subroutine may live across the 2 KB boundary and the return restores the right bank -- but **the DBF flip-flop itself is not restored** (UM p. 3-2). Interrupts force bit 11 to 0 for the whole service routine, and executing `SEL MB0`/`SEL MB1` inside an interrupt routine changes the flip-flop without changing PC11 (UM p. 3-2, which calls it "not recommended"). MAME implements exactly that: `execute_jmp` uses `a11 = m_irq_in_progress ? 0 : m_a11`, and `pull_pc`/`pull_pc_psw` mask the restored PC to `0x7ff` while an interrupt is in progress. Internal program memory is 1 KB (8048), 2 KB (8049) or 4 KB (8050); fetches below that boundary are internal and generate no external signals except ALE, and everything above is fetched externally (UM §3.1, p. 3-1). Pulling `EA` high forces every fetch external; the 8035/8039 are permanently in that state (UM §3.1.1, p. 3-1). Three addresses are fixed (UM §2.1.2, p. 2-3): | Address | Entered on | | --- | --- | | 0 | reset | | 3 | external interrupt (`INT` low, enabled) | | 7 | timer/counter overflow interrupt (enabled) | ## The PSW `MOV A,PSW` and `MOV PSW,A` move the whole byte (UM §2.1.7, p. 2-6): | Bit | 7 | 6 | 5 | 4 | 3 | 2 | 1 | 0 | | --- | --- | --- | --- | --- | --- | --- | --- | --- | | Name | CY | AC | F0 | BS | — | S2 | S1 | S0 | - **CY** carry out of bit 7, set by `ADD`, `ADDC`, `DA A`, `RLC A`, `RRC A`, and written directly by `CLR C` and `CPL C`. - **AC** auxiliary carry, carry out of bit 3, set by `ADD` and `ADDC` and consumed by `DA A`. There is no instruction that writes AC alone. - **F0** a user flag, tested by `JF0`, written by `CLR F0` / `CPL F0`. - **BS** register bank select (above). - **Bit 3** is **"Not used ('1' level when read)"** (UM p. 2-6, verbatim). See [undocumented-behavior.md](undocumented-behavior.md). - **S2-S0** the stack pointer. **F1 is not in the PSW.** It is a second user flag tested by `JF1` and written by `CLR F1` / `CPL F1`; it is not saved on `CALL` and not restored by `RETR`. It survives nothing but reset, which clears it (UM §2.1.12, p. 2-12, item 10). MAME keeps it in a separate member `m_f1`. Only the **upper four bits** of PSW are pushed with the PC, and `RETR` restores them; `RET` does not (UM §2.1.7, p. 2-6). The stack pointer itself is not pushed -- it is decremented by both returns. ## Internal RAM and the stack | Addresses | Contents | | --- | --- | | 0-7 | R0-R7 of bank 0 | | 8-23 | the eight-level program counter stack, or general RAM if unused | | 24-31 | R0-R7 of bank 1, or general RAM if bank 1 is never selected | | 32-63 (-127, -255) | general RAM | (UM §2.1.3, p. 2-3, data memory map.) A `CALL` or an accepted interrupt writes two bytes at RAM `8 + 2*SP` and `9 + 2*SP`: the low byte of the PC, then PC11-PC8 in the low nibble and PSW7-PSW4 in the high nibble, and then increments SP (UM p. 2-6 diagram; MAME `push_pc_psw`). A return decrements SP and reads them back. The stack pointer is three bits, so **it wraps**: "If overflow does occur the deepest address stored (location 8 and 9) will be overwritten and lost since the stack pointer overflows from 111 to 000. It also underflows from 000 to 111" (UM §2.1.6, p. 2-5, verbatim). There is no trap and no error. A core that raises an exception on the ninth nested call is wrong. ## Ports, BUS and the expander - **P1 and P2** are 8-bit *quasi-bidirectional* ports: written data is latched and stays until rewritten, and a line written `1` is held by a weak pull-up so an external driver can pull it low and `IN A,Pp` will read the low (UM §2.1.4, p. 2-4). This is why `ANL P1,#data` and `ORL P1,#data` exist -- they are read-modify-write on the latch -- and why a program that wants to read a pin writes `1` to it first. Reset sets both ports to all ones, i.e. input mode (UM §2.1.12, p. 2-12, item 6). - **BUS** is a true bidirectional port with strobes. `INS A,BUS` and `OUTL BUS,A` use it as a latched port and pulse RD and WR; `MOVX` uses it as the address/data bus for external data memory; during an external program fetch it carries A7-A0 with P2.0-P2.3 carrying A11-A8 (UM §2.1.4, p. 2-5; §3.1.1, p. 3-1; §3.6, p. 3-11). The manual warns that "a previously latched output will be destroyed by executing a MOVX instruction and BUS will be left in the high impedance state" and that "OUTL should never be used in a system with external program memory" (UM §3.7, p. 3-11). - **P4-P7** are the four 4-bit ports of an external 8243 expander, addressed over P2.0-P2.3 with the PROG strobe. `MOVD A,Pp` reads a nibble into A's low half and **clears A's high half**; `MOVD Pp,A`, `ANLD Pp,A` and `ORLD Pp,A` write, and, or. MAME's `expander_operation` shows the sequence: drive the opcode/port nibble on P2.0-3, pulse PROG, then transfer the data nibble. Control signals, verbatim from UM §3.6, p. 3-11: | Signal | Active during | | --- | --- | | RD | `MOVX A,@R` or `INS A,BUS` | | WR | `MOVX @R,A` or `OUTL BUS,A` | | ALE | **every machine cycle** | | PSEN | fetch of external program memory (instruction or immediate data) | | PROG | `MOVD A,P`, `MOVD P,A`, `ANLD P,A`, `ORLD P,A` | ## T0, T1 and INT Three pins are testable by conditional jumps without going through a port (UM §2.1.5, p. 2-5): | Pin | Tested by | Second job | | --- | --- | --- | | T0 | `JT0`, `JNT0` | outputs the internal CLK if `ENT0 CLK` has been executed (UM §2.1.11, p. 2-10) | | T1 | `JT1`, `JNT1` | the event-counter input after `STRT CNT` | | INT | `JNI` | the external interrupt request | `JNI` jumps when INT is **low** (the active level), so a program can poll the pin as a third test input while interrupts are disabled (UM §2.1.9, p. 2-8). ## The timer / event counter One 8-bit up counter with two input sources (UM §2.1.10, p. 2-8 to p. 2-9): - `STRT T` connects the machine-cycle clock (the same clock that drives ALE) through a **divide-by-32 prescaler**, and **resets the prescaler**. At a 6 MHz crystal the machine cycle clock is 400 kHz, so the counter increments at 12.5 kHz, one tick every 80 µs, and a full 256-count overflow is 20 ms. - `STRT CNT` connects T1 and counts high-to-low transitions. Maximum rate is one transition per three instruction cycles; T1 must stay high at least 500 ns after each transition (at 6 MHz). - `STOP TCNT` stops either. - `MOV T,A` loads it, `MOV A,T` reads it. **Reset does not clear it**: "The counter content is not affected by Reset and is initialized solely by the MOV T,A instruction" (UM p. 2-8, verbatim). - Overflow (FF → 00) sets the **timer flag**, testable by `JTF`; `JTF` reads *and clears* the flag, and so does reset. Overflow also sets a separate **overflow latch** that requests the interrupt. While timer interrupts are disabled the latch does not store the overflow, though the flag is still set (UM p. 2-7, note 3, verbatim). MAME implements the prescaler as a 5-bit accumulator advanced by the cycle count of each instruction (`burn_cycles`), which is the right shape for a Python core: a `step()` that returns cycles should feed the same number into the timer. ## Interrupts The model is deliberately small, and every rule below is from UM §2.1.9, p. 2-7 and p. 2-8: 1. **Two sources.** External `INT`, vectoring to 3; timer/counter overflow, vectoring to 7. 2. **`INT` is level-triggered and active low**, "to allow WIRE OR'ing of several interrupt sources". 3. **Sampling.** "The Interrupt line is sampled every machine cycle during ALE and when detected causes a jump to subroutine at location 3 in program memory as soon as all cycles of the current instruction are complete." The figure's note 1 adds: "INT input is sampled by ALE every machine cycle except first cycle of double cycle instruction." 4. **It is a CALL.** The PC and the upper four PSW bits go on the stack, SP increments, and PC11 is forced to 0 for the routine. 5. **Priority and no nesting.** "If an internal timer/counter generated interrupt and an external interrupt are detected at the same time, the external source will be recognized." And note 2 of the figure: "When interrupt in progress f/f is set all further interrupts are locked out independent of state of either interrupt enable f/f." 6. **`RETR` ends it.** "The interrupt system is single level in that once an interrupt is detected all further interrupt requests are ignored until execution of an RETR re-enables the interrupt input logic. This occurs at the beginning of the second cycle of the RETR instruction." `RET` does *not* clear the in-progress flip-flop and does not restore PSW: using `RET` to leave an interrupt routine wedges the machine with interrupts locked out forever. 7. **The request must go away.** "An interrupt request must be removed before the RETR instruction is executed upon return from the service routine otherwise the processor will re-enter the service routine immediately." Because INT is a level, this is the host's problem and the core must not edge-latch it. 8. **Enables.** `EN I` / `DIS I` for external, `EN TCNTI` / `DIS TCNTI` for timer; both are disabled by reset. A pending timer interrupt is cleared by taking it, or by `DIS TCNTI`. MAME's `check_irqs` matches items 1, 4, 5 and 6 exactly, adds two machine cycles for the acknowledge, and clears the timer overflow latch when the timer interrupt is taken. Its known deviations from silicon are recorded in [undocumented-behavior.md](undocumented-behavior.md) -- the file's own header documents them. ## Reset UM §2.1.12, p. 2-12, lists eleven effects, verbatim: 1. Sets program counter to zero. 2. Sets stack pointer to zero. 3. Selects register bank 0. 4. Selects memory bank 0. 5. Sets BUS to high impedance state (except when EA = 5V). 6. Sets Ports 1 and 2 to input mode. 7. Disables interrupts (timer and external). 8. Stops timer. 9. Clears timer flag. 10. Clears F0 and F1. 11. Disables clock output from T0. Note what is **not** in the list: A, the timer/counter register, CY, AC and the contents of internal RAM. MAME does `m_psw = m_psw & (C_FLAG | A_FLAG)` on reset -- preserving CY and AC and clearing F0, BS and SP -- and does not touch `m_a` or `m_timer`, which is a faithful reading of the list. ## Conditional jumps Every conditional jump is two bytes, two cycles, and stays within the 256-byte page of its second byte (see "The program counter" above). | Instruction | Jumps when | | --- | --- | | `JC` / `JNC` | CY = 1 / CY = 0 | | `JZ` / `JNZ` | A = 0 / A ≠ 0 | | `JB0`-`JB7` | the numbered bit of A is 1 | | `JF0` | F0 = 1 (there is no `JNF0`) | | `JF1` | F1 = 1 | | `JTF` | the timer flag is 1; **the flag is cleared either way** | | `JT0` / `JNT0` | the T0 pin is high / low | | `JT1` / `JNT1` | the T1 pin is high / low | | `JNI` | the INT pin is low | | `DJNZ Rr` | `--Rr ≠ 0` | There is no jump on AC and no jump on the register bank. ## The instruction set **Provenance of this table.** The mnemonics and the opcode assignments are transcribed from MAME 0.285's `mcs48dsm.cpp` and the 256-entry dispatch table `s_mcs48_opcodes` in `mcs48.cpp` (BSD-3-Clause, attributed above). The byte and cycle counts were extracted mechanically from the same file (`burn_cycles(n)` and the presence of `argument_fetch()` in each handler) and then checked row by row against Intel's own summary table (UM §4.1, p. 4-5), which lists bytes and cycles for every instruction; they agree. The flag column is from the handler bodies, cross-read against UM §4.1. [`scripts/check_instruction_table.py`](../scripts/check_instruction_table.py) re-derives the table from the pinned MAME sources and diffs it against this document, so the table cannot drift without the check failing. ### Opcode map Rows are the high nibble, columns the low nibble. `—` is an undefined opcode (26 of them; see [undocumented-behavior.md](undocumented-behavior.md)). | | x0 | x1 | x2 | x3 | x4 | x5 | x6 | x7 | x8 | x9 | xA | xB | xC | xD | xE | xF | | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | | **0x** | NOP | — | OUTL BUS,A | ADD A,#d | JMP p0 | EN I | — | DEC A | INS A,BUS | IN A,P1 | IN A,P2 | — | MOVD A,P4 | MOVD A,P5 | MOVD A,P6 | MOVD A,P7 | | **1x** | INC @R0 | INC @R1 | JB0 | ADDC A,#d | CALL p0 | DIS I | JTF | INC A | INC R0 | INC R1 | INC R2 | INC R3 | INC R4 | INC R5 | INC R6 | INC R7 | | **2x** | XCH A,@R0 | XCH A,@R1 | — | MOV A,#d | JMP p1 | EN TCNTI | JNT0 | CLR A | XCH A,R0 | XCH A,R1 | XCH A,R2 | XCH A,R3 | XCH A,R4 | XCH A,R5 | XCH A,R6 | XCH A,R7 | | **3x** | XCHD A,@R0 | XCHD A,@R1 | JB1 | — | CALL p1 | DIS TCNTI | JT0 | CPL A | — | OUTL P1,A | OUTL P2,A | — | MOVD P4,A | MOVD P5,A | MOVD P6,A | MOVD P7,A | | **4x** | ORL A,@R0 | ORL A,@R1 | MOV A,T | ORL A,#d | JMP p2 | STRT CNT | JNT1 | SWAP A | ORL A,R0 | ORL A,R1 | ORL A,R2 | ORL A,R3 | ORL A,R4 | ORL A,R5 | ORL A,R6 | ORL A,R7 | | **5x** | ANL A,@R0 | ANL A,@R1 | JB2 | ANL A,#d | CALL p2 | STRT T | JT1 | DA A | ANL A,R0 | ANL A,R1 | ANL A,R2 | ANL A,R3 | ANL A,R4 | ANL A,R5 | ANL A,R6 | ANL A,R7 | | **6x** | ADD A,@R0 | ADD A,@R1 | MOV T,A | — | JMP p3 | STOP TCNT | — | RRC A | ADD A,R0 | ADD A,R1 | ADD A,R2 | ADD A,R3 | ADD A,R4 | ADD A,R5 | ADD A,R6 | ADD A,R7 | | **7x** | ADDC A,@R0 | ADDC A,@R1 | JB3 | — | CALL p3 | ENT0 CLK | JF1 | RR A | ADDC A,R0 | ADDC A,R1 | ADDC A,R2 | ADDC A,R3 | ADDC A,R4 | ADDC A,R5 | ADDC A,R6 | ADDC A,R7 | | **8x** | MOVX A,@R0 | MOVX A,@R1 | — | RET | JMP p4 | CLR F0 | JNI | — | ORL BUS,#d | ORL P1,#d | ORL P2,#d | — | ORLD P4,A | ORLD P5,A | ORLD P6,A | ORLD P7,A | | **9x** | MOVX @R0,A | MOVX @R1,A | JB4 | RETR | CALL p4 | CPL F0 | JNZ | CLR C | ANL BUS,#d | ANL P1,#d | ANL P2,#d | — | ANLD P4,A | ANLD P5,A | ANLD P6,A | ANLD P7,A | | **Ax** | MOV @R0,A | MOV @R1,A | — | MOVP A,@A | JMP p5 | CLR F1 | — | CPL C | MOV R0,A | MOV R1,A | MOV R2,A | MOV R3,A | MOV R4,A | MOV R5,A | MOV R6,A | MOV R7,A | | **Bx** | MOV @R0,#d | MOV @R1,#d | JB5 | JMPP @A | CALL p5 | CPL F1 | JF0 | — | MOV R0,#d | MOV R1,#d | MOV R2,#d | MOV R3,#d | MOV R4,#d | MOV R5,#d | MOV R6,#d | MOV R7,#d | | **Cx** | — | — | — | — | JMP p6 | SEL RB0 | JZ | MOV A,PSW | DEC R0 | DEC R1 | DEC R2 | DEC R3 | DEC R4 | DEC R5 | DEC R6 | DEC R7 | | **Dx** | XRL A,@R0 | XRL A,@R1 | JB6 | XRL A,#d | CALL p6 | SEL RB1 | — | MOV PSW,A | XRL A,R0 | XRL A,R1 | XRL A,R2 | XRL A,R3 | XRL A,R4 | XRL A,R5 | XRL A,R6 | XRL A,R7 | | **Ex** | — | — | — | MOVP3 A,@A | JMP p7 | SEL MB0 | JNC | RL A | DJNZ R0 | DJNZ R1 | DJNZ R2 | DJNZ R3 | DJNZ R4 | DJNZ R5 | DJNZ R6 | DJNZ R7 | | **Fx** | MOV A,@R0 | MOV A,@R1 | JB7 | — | CALL p7 | SEL MB1 | JC | RLC A | MOV A,R0 | MOV A,R1 | MOV A,R2 | MOV A,R3 | MOV A,R4 | MOV A,R5 | MOV A,R6 | MOV A,R7 | Regularities worth encoding in the dispatch rather than in 256 branches: - `JMP pN` is `N*32 + 0x04`, `CALL pN` is `N*32 + 0x14`: opcode bits 7-5 are A10-A8 of the destination, and bit 11 comes from DBF. - `JBb addr` is `b*32 + 0x12`: opcode bits 7-5 are the bit number. - Any opcode whose low three bits select a register (`0x18`-`0x1F`, `0x28`-`0x2F`, ...) uses bits 2-0 as `r`, and the `@R0`/`@R1` forms sit eight below with bit 0 as the pointer choice. - The expander instructions `MOVD`/`MOVD`/`ORLD`/`ANLD` use bits 1-0 as `p - 4`. ### Per-instruction table `b` = bytes, `c` = machine cycles. Flags not listed are unchanged. | Opcodes | Instruction | b | c | Effect on flags and state | | --- | --- | --- | --- | --- | | 68+r | `ADD A,Rr` | 1 | 1 | CY, AC | | 60, 61 | `ADD A,@Rr` | 1 | 1 | CY, AC | | 03 | `ADD A,#data` | 2 | 2 | CY, AC | | 78+r | `ADDC A,Rr` | 1 | 1 | CY, AC | | 70, 71 | `ADDC A,@Rr` | 1 | 1 | CY, AC | | 13 | `ADDC A,#data` | 2 | 2 | CY, AC | | 58+r | `ANL A,Rr` | 1 | 1 | — | | 50, 51 | `ANL A,@Rr` | 1 | 1 | — | | 53 | `ANL A,#data` | 2 | 2 | — | | 48+r | `ORL A,Rr` | 1 | 1 | — | | 40, 41 | `ORL A,@Rr` | 1 | 1 | — | | 43 | `ORL A,#data` | 2 | 2 | — | | D8+r | `XRL A,Rr` | 1 | 1 | — | | D0, D1 | `XRL A,@Rr` | 1 | 1 | — | | D3 | `XRL A,#data` | 2 | 2 | — | | 17 | `INC A` | 1 | 1 | — (no flag is touched) | | 07 | `DEC A` | 1 | 1 | — | | 27 | `CLR A` | 1 | 1 | — | | 37 | `CPL A` | 1 | 1 | — | | 57 | `DA A` | 1 | 1 | CY set by the adjust; AC is read, not written | | 47 | `SWAP A` | 1 | 1 | — | | E7 | `RL A` | 1 | 1 | — | | F7 | `RLC A` | 1 | 1 | CY ← old bit 7, bit 0 ← old CY | | 77 | `RR A` | 1 | 1 | — | | 67 | `RRC A` | 1 | 1 | CY ← old bit 0, bit 7 ← old CY | | F8+r | `MOV A,Rr` | 1 | 1 | — | | F0, F1 | `MOV A,@Rr` | 1 | 1 | — | | 23 | `MOV A,#data` | 2 | 2 | — | | A8+r | `MOV Rr,A` | 1 | 1 | — | | A0, A1 | `MOV @Rr,A` | 1 | 1 | — | | B8+r | `MOV Rr,#data` | 2 | 2 | — | | B0, B1 | `MOV @Rr,#data` | 2 | 2 | — | | C7 | `MOV A,PSW` | 1 | 1 | A ← PSW with bit 3 forced to 1 | | D7 | `MOV PSW,A` | 1 | 1 | CY, AC, F0, BS **and SP** all loaded | | 28+r | `XCH A,Rr` | 1 | 1 | — | | 20, 21 | `XCH A,@Rr` | 1 | 1 | — | | 30, 31 | `XCHD A,@Rr` | 1 | 1 | low nibbles only | | 18+r | `INC Rr` | 1 | 1 | — | | 10, 11 | `INC @Rr` | 1 | 1 | — | | C8+r | `DEC Rr` | 1 | 1 | — (there is no `DEC @Rr`) | | A3 | `MOVP A,@A` | 1 | 2 | A ← program[(PC & 0xF00) \| A], PC already past the opcode | | E3 | `MOVP3 A,@A` | 1 | 2 | A ← program[0x300 \| A] | | 80, 81 | `MOVX A,@Rr` | 1 | 2 | external data read, RD pulses | | 90, 91 | `MOVX @Rr,A` | 1 | 2 | external data write, WR pulses | | 09, 0A | `IN A,Pp` | 1 | 2 | p = 1, 2 | | 39, 3A | `OUTL Pp,A` | 1 | 2 | p = 1, 2 | | 99, 9A | `ANL Pp,#data` | 2 | 2 | read-modify-write on the latch | | 89, 8A | `ORL Pp,#data` | 2 | 2 | read-modify-write on the latch | | 08 | `INS A,BUS` | 1 | 2 | RD pulses | | 02 | `OUTL BUS,A` | 1 | 2 | WR pulses | | 98 | `ANL BUS,#data` | 2 | 2 | | | 88 | `ORL BUS,#data` | 2 | 2 | | | 0C-0F | `MOVD A,Pp` | 1 | 2 | p = 4-7; A's high nibble is cleared | | 3C-3F | `MOVD Pp,A` | 1 | 2 | | | 8C-8F | `ORLD Pp,A` | 1 | 2 | | | 9C-9F | `ANLD Pp,A` | 1 | 2 | | | 04+32N | `JMP addr` | 2 | 2 | PC10-8 from the opcode, PC11 from DBF | | B3 | `JMPP @A` | 1 | 2 | within the current 256-byte page | | 14+32N | `CALL addr` | 2 | 2 | pushes PC and PSW7-4; SP + 1 | | 83 | `RET` | 1 | 2 | SP − 1; PSW **not** restored | | 93 | `RETR` | 1 | 2 | SP − 1; CY AC F0 BS restored; clears interrupt-in-progress | | E8+r | `DJNZ Rr,addr` | 2 | 2 | page-local | | F6 | `JC addr` | 2 | 2 | | | E6 | `JNC addr` | 2 | 2 | | | C6 | `JZ addr` | 2 | 2 | | | 96 | `JNZ addr` | 2 | 2 | | | 12+32b | `JBb addr` | 2 | 2 | b = 0-7 | | B6 | `JF0 addr` | 2 | 2 | | | 76 | `JF1 addr` | 2 | 2 | | | 16 | `JTF addr` | 2 | 2 | **clears the timer flag whether or not it jumps** | | 36, 26 | `JT0` / `JNT0 addr` | 2 | 2 | | | 56, 46 | `JT1` / `JNT1 addr` | 2 | 2 | | | 86 | `JNI addr` | 2 | 2 | jumps when INT is low | | 42 | `MOV A,T` | 1 | 1 | — | | 62 | `MOV T,A` | 1 | 1 | — | | 55 | `STRT T` | 1 | 1 | starts the timer, **resets the prescaler** | | 45 | `STRT CNT` | 1 | 1 | starts the event counter on T1 | | 65 | `STOP TCNT` | 1 | 1 | | | 25 | `EN TCNTI` | 1 | 1 | | | 35 | `DIS TCNTI` | 1 | 1 | also clears a pending timer interrupt | | 05 | `EN I` | 1 | 1 | | | 15 | `DIS I` | 1 | 1 | | | C5 | `SEL RB0` | 1 | 1 | BS ← 0 | | D5 | `SEL RB1` | 1 | 1 | BS ← 1 | | E5 | `SEL MB0` | 1 | 1 | DBF ← 0 (takes effect at the next `JMP`/`CALL`) | | F5 | `SEL MB1` | 1 | 1 | DBF ← 1 | | 75 | `ENT0 CLK` | 1 | 1 | CLK appears on T0 until reset | | 95 | `CPL F0` | 1 | 1 | F0 | | 85 | `CLR F0` | 1 | 1 | F0 ← 0 | | B5 | `CPL F1` | 1 | 1 | F1 | | A5 | `CLR F1` | 1 | 1 | F1 ← 0 | | 97 | `CLR C` | 1 | 1 | CY ← 0 | | A7 | `CPL C` | 1 | 1 | CY | | 00 | `NOP` | 1 | 1 | — | Totals: 230 defined opcodes, 26 undefined; 159 one-cycle and 97 two-cycle opcodes; 190 one-byte and 66 two-byte. An accepted interrupt costs two cycles on top of the `CALL` it performs (MAME `check_irqs`); the manual does not give a figure, so that number is emulator-derived and is flagged as such in [timing.md](timing.md). ## The UPI-41/42 difference The 8041/8741/8042/8742 are the same CPU with a host interface bolted on: an 8-bit Data Bus Buffer (DBB in and DBB out), a status register STS with IBF and OBF bits, and `CS`/`A0`/`RD`/`WR` pins so a host processor reads and writes the DBB like a peripheral register. BUS is gone. Fourteen opcodes change (MAME `mcs48.cpp`, lines 74-91, transcribed): | Opcode | MCS-48 | UPI-41/42 | | --- | --- | --- | | 02 | `OUTL BUS,A` | `OUT DBB,A` | | 08 | `INS A,BUS` | undefined | | 22 | undefined | `IN A,DBB` | | 75 | `ENT0 CLK` | undefined | | 80 | `MOVX A,@R0` | undefined | | 81 | `MOVX A,@R1` | undefined | | 86 | `JNI addr` | `JOBF addr` | | 88 | `ORL BUS,#data` | undefined | | 90 | `MOVX @R0,A` | `MOV STS,A` | | 91 | `MOVX @R1,A` | undefined | | 98 | `ANL BUS,#data` | undefined | | D6 | undefined | `JNIBF addr` | | E5 | `SEL MB0` | `EN DMA` | | F5 | `SEL MB1` | `EN FLAGS` | The UPI's "external interrupt" is the host writing the DBB: MAME's `check_irqs` takes the external interrupt when `m_irq_state || (m_sts & STS_IBF)`. That is how DECO Cassette's 8041A and Taito's three 8041As in *Great Swordsman* talk to their main CPUs, and it is the whole mechanism behind "MCS-48 as a protection part". A core should implement the UPI as a second opcode table plus a DBB/STS block, not as a fork. The UPI-41A manual is ; this repository has not cited it by page yet. `[unverified]` for anything about the UPI beyond the table above.