# Undocumented, undefined and merely surprising behaviour Each entry says where it comes from and how well it is established: - **documented** — Intel's *MCS-48 Family User's Manual* 9800270D says it, in the words quoted. Cited as UM §x.y, p. n-m. - **MAME** — the behaviour of MAME 0.285's `mcs48` device (`src/devices/cpu/mcs48/mcs48.cpp`, BSD-3-Clause, commit `f379cd06`). This is an *emulator's opinion*, tier 3: it is what the most-exercised MCS-48 implementation does, which is evidence about what real programs rely on, not evidence about the die. - **hardware** — measured on a real chip. There are exactly two such facts in this document, and both come from MAME's own source header. - **[unverified]** — inferred, or asserted by nobody this repository can cite. A core may implement it, but no claim may rest on it. ## 1. The 26 undefined opcodes **documented** (by omission: they are absent from UM §4.1's alphabetic listing and summary) / **MAME** (for the behaviour). ``` 01 06 0B 22 33 38 3B 63 66 73 82 87 8B 9B A2 A6 B7 C0 C1 C2 C3 D6 E0 E1 E2 F3 ``` Three observations about the shape of that set: - They cluster in the low nibbles that carry the irregular instructions — `x0` 2, `x1` 3, `x2` 5, `x3` 5, `x6` 4, `x7` 2, `x8` 1, `xB` 4 — and never in `x4`, `x5`, `x9`, `xA`, or `xC`-`xF`, which are the fully regular columns (`JMP`, `CALL`, the control group, and the eight register forms). Row `Cx` is the emptiest with four, row `0x`, `3x`, `8x` and `Ex` have three each, and rows `1x`, `4x` and `5x` are completely full. - `C0`-`C3` and `E0`-`E2` are the only runs, and they sit where a `DEC @Rr` and a second `MOVP`-like form would have gone. There is no `DEC @Rr` in the instruction set; `DEC` exists only for A and for Rr. - `22` and `D6` are undefined on the MCS-48 and *defined* on the UPI-41 (`IN A,DBB` and `JNIBF`); `01` is defined on the CMOS parts (`IDL`). The other 23 are undefined on every family member MAME models. **What MAME does** (`mcs48.cpp`, `OPHANDLER(illegal)`), transcribed in full: ```cpp OPHANDLER( illegal ) { burn_cycles(1); logerror("Illegal opcode = %02x @ %04X\n", program_r(m_prevpc), m_prevpc); } ``` That is: one machine cycle, no operand byte consumed, no register or flag changed, PC already advanced past the single opcode byte by the fetch. In effect a one-cycle `NOP` with a log line. **The core should do the same and offer the host a hook**, because on a protection MCU an undefined opcode is usually a sign the emulation has gone off the rails, and a silent `NOP` hides it. **What real silicon does is `[unverified]`.** Nobody has published a capture. The plausible reading — an unassigned term in the decode PLA produces no control signals, so the instruction is a one-cycle no-op — is a guess about a 1976 PLA, not a measurement. The place to look, if it ever matters, is the T48 VHDL model ([validation.md](validation.md)), which is a from-the-manual reimplementation and therefore another opinion, not an answer. **Two opcodes in that list are defined on other family members**, and a core that means to grow should leave room: | Opcode | On | Meaning | | --- | --- | --- | | 01 | CMOS parts (80C48, 80C49, and NEC's and Toshiba's second sources) | `IDL` — halt until an interrupt or reset. MAME's disassembler prints `idl` for it (`mcs48dsm.cpp` line 40, with the comment "CMOS only; called HALT by NEC and Toshiba") but the **emulator does not implement it**: `mcs48.cpp`'s TODO list opens with "add CMOS devices, 1 new opcode (01 IDL)". So MAME will disassemble a CMOS program correctly and then execute `IDL` as a one-cycle no-op. | | 22 | UPI-41/42 | `IN A,DBB` | | D6 | UPI-41/42 | `JNIBF` | The 8021 and 8022 go the other way: they **reuse defined MCS-48 opcodes** rather than the undefined ones. `80` is `RAD` instead of `MOVX A,@R0`; `85` is `SEL AN0` instead of `CLR F0`; `95` is `SEL AN1` instead of `CPL F0`; `93` is `RETI` instead of `RETR` (`mcs48dsm.cpp` lines 214, 228, 283, 290). MAME's TODO admits it does not implement these ("add special 8022 opcodes (RAD, SEL AN0, SEL AN1, RETI)"). The 8021/8022 are out of scope for this core anyway: they have a reduced instruction set (UM §4.1, p. 4-6 lists the deletions) and a 30-clock machine cycle. ## 2. PSW bit 3 reads as 1 and cannot be written **documented.** UM §2.1.7, p. 2-6, verbatim: "Bit 3: Not used ('1' level when read)". **MAME** implements it as a pair of masks (`mcs48.cpp` lines 812 and 825): ```cpp OPHANDLER( mov_a_psw ) { burn_cycles(1); m_a = m_psw | 0x08; } OPHANDLER( mov_psw_a ) { burn_cycles(1); m_psw = m_a & ~0x08; update_regptr(); } ``` so the bit is forced to 1 on the way out and forced to 0 in the internal copy. Confirmed observationally: in the Donkey Kong trace of [mame-oracle.md](mame-oracle.md), the only PSW values that ever appear are `00`, `01`, `10` and `11` — bit 3 is never set in MAME's internal `m_psw`. Two consequences: - `MOV A,PSW` followed by `MOV PSW,A` is **not** the identity from the accumulator's point of view if you also inspect the pushed byte: the stack stores `PSW & 0xF0` (`push_pc_psw`), so bit 3 never reaches the stack either. - A core that stores bit 3 as 1 internally will produce a different `MOV A,PSW` result than one that stores 0 and ORs on read — **only if** it forgets the OR. Store 0, OR on read; that matches both the manual and MAME. Whether real silicon can be made to hold a 0 in some hidden latch is unanswerable from the outside, and irrelevant: the manual says reads return 1. `[unverified]` for anything finer. ## 3. The stack wraps at eight levels, silently **documented.** UM §2.1.6, p. 2-5, verbatim: > Nesting of subroutines within subroutines can continue up to 8 times > without overflowing the stack. 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. **MAME** does exactly the arithmetic (`push_pc_psw`, `pull_pc_psw`, `pull_pc`): ```cpp u8 sp = m_psw & 0x07; ram_w(8 + 2*sp, m_pc); ram_w(9 + 2*sp, ((m_pc >> 8) & 0x0f) | (m_psw & 0xf0)); m_psw = (m_psw & 0xf0) | ((sp + 1) & 0x07); ``` Three things a core must get right and one it must not do: - The stack lives **in internal RAM at 8-23**, so `@R0`/`@R1` can read and write it, and a program with fewer than eight nesting levels legitimately uses the top of it as data (UM p. 2-4 says so explicitly). - Only **PSW bits 7-4** are stored, in the high nibble of the odd byte. The stack pointer is not stored; it is reconstructed by decrementing. - The nine-level case overwrites locations 8 and 9 and **loses the outermost return address**. There is no trap, no flag, no wrap detection. - A core must **not** raise an exception on overflow or underflow. Real programs on protection MCUs do strange things with the stack deliberately. ## 4. `SEL MB` sets a flip-flop, not the PC **documented.** UM §3.1.2, p. 3-2, verbatim: "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... Therefore, the SEL MB instruction may be executed at any time prior to the actual bank switch which occurs during the next branch instruction encountered." Four corners follow, all documented and all easy to get wrong: 1. **The PC does not carry into bit 11.** Incrementing past 0x7FF wraps to 0x000, *not* to 0x800 (UM p. 2-6 diagram: "Counts 000H to 7FFH, Overflows 7FFH to 000H"). MAME's fetch is literally `m_pc = ((m_pc + 1) & 0x7ff) | (m_pc & 0x800)` — the low eleven bits wrap and bit 11 is preserved. On an 8049 with 2 KB of internal ROM this is the difference between running off the end into external memory and looping back to the reset vector. 2. **`CALL` saves all twelve bits**, so a call across the boundary returns to the right bank — but "the bank switch flipflop will not be altered on return" (UM p. 3-2). A subroutine in bank 1 that returns to bank 0 leaves DBF set, and the next unrelated `JMP` lands in bank 1. 3. **Interrupts force PC11 to 0** for the whole service routine. MAME: `execute_jmp` uses `a11 = m_irq_in_progress ? 0 : m_a11`, and both `pull_pc` and `pull_pc_psw` mask the restored PC with `0x7ff` while `m_irq_in_progress`. So a `JMP` inside an interrupt routine can only reach bank 0 no matter what DBF says. 4. **`SEL MB` inside an interrupt routine changes the flip-flop but not PC11**, which UM p. 3-2 calls "not recommended" — the effect lands after `RETR`, on some later branch, far from the instruction that caused it. `RETR` restores CY, AC, F0 and BS; it does **not** restore DBF. ## 5. Interrupt-in-progress locks everything out, and `RET` never clears it **documented.** UM §2.1.9, p. 2-7, figure note 2, verbatim: "When interrupt in progress f/f is set all further interrupts are locked out independent of state of either interrupt enable f/f." And p. 2-8: "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." The trap: **`RET` does not clear the flip-flop.** MAME's `ret` is `pull_pc()`, and only `retr` does `m_irq_in_progress = false`. A service routine that ends in `RET` returns correctly, does not restore PSW, and leaves the machine permanently deaf to both interrupt sources. There is no timeout and no error. This is a real failure mode of MCS-48 sound programs and a core must reproduce it rather than "helpfully" clearing the flag. A second, subtler consequence of MAME's implementation: `pull_pc` masks the restored PC with `0x7ff` **while an interrupt is in progress**, so a `RET` executed inside an interrupt routine truncates the return address to eleven bits even though the pushed value had twelve. That is MAME's reading of "bit 11 of the program counter is held at 0 during the interrupt service routine" (UM p. 3-2), and it is the reading this core should adopt — but it is **MAME**, not a measurement, and a program that calls across the 2 KB boundary from inside an interrupt would tell the two readings apart. `[unverified]` which is right on silicon. Also documented, and also a live failure mode: "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" (UM p. 2-8). `INT` is a **level**. A host that pulses it as an edge and a core that latches edges will both be wrong, and the bug will look like a hang. ## 6. Two hardware measurements say MAME's timer and interrupt timing is off **hardware.** These are the only measured MCS-48 facts this repository has been able to locate anywhere, and they are in MAME's own TODO block (`mcs48.cpp` lines 11-45), credited to the MAME contributor *bataais*: > IRQ and/or timer increment timing is wrong? See test below. After IRQ, > A = 0x20 on MAME, A = 0x22 on the real 8048 as tested by bataais. > > ``` > stop tcnt / mov a,0xff / mov t,a / inc a / en tcnti / strt t > inc a / inc a / inc a / (etc.) > ``` > > With the following test, on MAME, A = 0xff after 30 NOPs, A = 0 after 31 > NOPs. On the real 8048, A = 0xff after 31 NOPs, A = 0 after 32 NOPs. It can > mean that STRT T has a 1 cycle delay, or simply that MOV A,T gets the timer > value pre-increment. > > ``` > stop tcnt / mov a,0xff / mov t,a / strt t > nop / nop / nop / (etc.) / mov a,t > ``` And a third note, about a hack in the interrupt path: > IRQ timing is hacked due to WY-100 needing to take JNI branch before > servicing interrupt (see m_irq_polled), probably related to note above? The hack is visible in `check_irqs`: if the previous instruction was a `JNI` that did *not* branch (`m_irq_polled`), MAME rewinds the PC and forces the branch before vectoring. **What this means for the project.** Two things, and both belong in the README when a core exists: 1. These two experiments are **acceptance tests**, not trivia. A core that reproduces `A = 0x22` and the 31/32-NOP boundary is, on those two points, better than MAME. They are the only hardware-tier gate available. 2. A lockstep against MAME **will** diverge on timer-sensitive code, and the divergence is MAME's, not the core's. Any lockstep report must exclude these two regions explicitly or it will chase a known-bad reference. The second experiment does not settle the question it raises — "STRT T has a 1 cycle delay" and "MOV A,T gets the timer value pre-increment" produce the same measurement. Which of the two is true is `[unverified]`. ## 7. `JTF` clears the timer flag whether or not it jumps **documented.** UM §2.1.10, p. 2-8: "The state of the overflow flag is testable with the conditional jump instruction JTF. The flag is reset by executing a JTF or by Reset." **MAME**: `OPHANDLER( jtf ) { burn_cycles(2); execute_jcc(m_timer_flag); m_timer_flag = false; }` — the clear is unconditional and happens after the test. A polling loop `jtf tick / jmp $-2` therefore consumes the flag on the iteration that sees it, which is what makes the idiom work; a core that only clears on the taken branch will hang such a loop, and a core that clears before the test will never take it. Note that `JTF` clears only the **flag**. The separate overflow latch that requests the timer interrupt is cleared by taking the interrupt or by `DIS TCNTI` (MAME: `dis_tcnti` sets both `m_tirq_enabled = false` and `m_timer_overflow = false`), which matches UM p. 2-9: "The pending timer interrupt is reset by the Call to location 7 or may be removed by executing a DIS TCNTI instruction." And while timer interrupts are disabled, "timer overflow f/f will not store any overflow that occurs. Timer flag will be set, however" (UM p. 2-7, figure note 3) — two pieces of state, not one. ## 8. Page-relative jumps take the page of the *second* byte **documented**, in the sense that the manual describes the encoding, but it is stated nowhere as a warning and it bites. `JC`, `JNC`, `JZ`, `JNZ`, `JBb`, `JF0`, `JF1`, `JTF`, `JT0`, `JNT0`, `JT1`, `JNT1`, `JNI` and `DJNZ` replace only PC7-PC0. **MAME** fetches the operand first and then masks: ```cpp void mcs48_cpu_device::execute_jcc(bool result) { u16 pch = m_pc & 0xf00; // m_pc has already passed the operand byte u8 offset = argument_fetch(); if (result) m_pc = pch | offset; } ``` So a two-byte conditional jump whose **second byte is the last byte of a 256-byte page** jumps into the *following* page. Assemblers warn about this; emulators forget it. The same applies to `JMPP @A` (`m_pc &= 0xf00; m_pc |= program_r(m_pc | m_a);` — the table is in the page of the byte after the opcode) and to `MOVP A,@A` (`m_a = program_r((m_pc & 0xf00) | m_a);`, same rule). `MOVP3 A,@A` is the exception: it always reads page 3 (`0x300 | m_a`), which is why it exists. MAME's *disassembler* uses `(pc + 1) & 0x700` rather than `& 0xf00` — it drops bit 11 where the emulator keeps it. For a program that lives in bank 0 the two agree; for bank-1 code the disassembly prints a bank-0 address. That is a cosmetic MAME inconsistency, noted so nobody "fixes" the core to match the disassembly. ## 9. Port reads AND the latch; port read-modify-write does not **documented** in prose, **MAME** in detail. UM §2.1.4, p. 2-4 describes the quasi-bidirectional structure: a line written 1 is held by a ~50 kΩ pull-up and can be pulled low externally; a line written 0 is held by a ~300 Ω pull-down and cannot. So a program must write 1 to a pin before reading it. MAME encodes that as an AND with the latch (`mcs48.cpp` lines 750-751): ```cpp OPHANDLER( in_a_p1 ) { burn_cycles(2); m_a = port_r(1) & m_p1; } OPHANDLER( in_a_p2 ) { burn_cycles(2); m_a = port_r(2) & m_p2; } ``` but the read-modify-write forms work on the **latch alone**, never touching the pin (lines 670 and 881): ```cpp OPHANDLER( anl_p1_n ) { burn_cycles(2); port_w(1, m_p1 &= argument_fetch()); } OPHANDLER( orl_p1_n ) { burn_cycles(2); port_w(1, m_p1 |= argument_fetch()); } ``` while the BUS versions read the actual bus (lines 669 and 880): ```cpp OPHANDLER( anl_bus_n ) { burn_cycles(2); bus_w(bus_r() & argument_fetch()); } OPHANDLER( orl_bus_n ) { burn_cycles(2); bus_w(bus_r() | argument_fetch()); } ``` That asymmetry — P1/P2 read-modify-write on the latch, BUS read-modify-write on the pins — is not stated in the manual and is **MAME**'s reading. `[unverified]` on silicon, but it is the reading a core should adopt, because it is what every driver in MAME's collection has been debugged against. Related, and **documented** as a warning rather than a behaviour (UM §3.7, p. 3-11): "a previously latched output will be destroyed by executing a MOVX instruction and BUS will be left in the high impedance state", and "OUTL should never be used in a system with external program memory". A host that models the BUS latch must let `MOVX` clobber it. `MOVD A,Pp` clears the accumulator's **high** nibble as well as loading the low one (MAME `expander_operation`: `m_a = port_r(2) & 0x0f`). The manual describes the 8243 transfer but does not say this in so many words. ## 10. Reset leaves more alive than you expect **documented.** UM §2.1.12, p. 2-12 lists eleven effects ([start-here.md](start-here.md) reproduces them). What is **not** in the list matters as much: | Not reset | Source | | --- | --- | | The accumulator | absent from the list; MAME does not touch `m_a` | | The timer/counter register | UM §2.1.10, p. 2-8, verbatim: "The counter content is not affected by Reset and is initialized solely by the MOV T,A instruction" | | CY and AC | absent from the list; MAME does `m_psw = m_psw & (C_FLAG \| A_FLAG)`, preserving exactly those two | | Internal RAM | absent from the list | A core whose `reset()` zeroes A, T, CY, AC or RAM is wrong, and the error is invisible in normal use and fatal when a board pulses RESET during play. What a *power-on* leaves in RAM and in T is a separate question the manual does not answer; the host decides, and it should be told to decide. ## 11. Internal RAM beyond the part's size **[unverified].** `@R0` and `@R1` are eight bits, so a program on a 64-byte 8048 can point at address 100. What happens is not in the manual. **MAME** maps only `0 .. m_ram_size - 1` (`data_map`) and lets the unmapped-access machinery handle the rest, which is an address-space behaviour rather than a decision about the chip. A mirror (address masked to the RAM size) is the usual guess for a part with no decode above its array, and it is a guess. A core should make this a host-visible policy rather than a silent constant: `mask`, `mirror` or `open bus`, chosen by the host, defaulting to mirror, documented as unverified. ## 12. UPI-41 corners **MAME**, all of it; the UPI-41A manual has not been cited by page in this repository yet, so treat this section as a pointer rather than a reference. - `MOV STS,A` writes only the **upper nibble** of the status register: `m_sts = (m_sts & 0x0f) | (m_a & 0xf0)` (line 826). The low four bits are the hardware's own OBF, IBF, F0 and F1 flags. - The UPI takes the "external" interrupt when the host has written the data buffer: `check_irqs` fires on `m_irq_state || (m_sts & STS_IBF)`. There is no separate IBF vector — it is location 3, the same as `INT`. - `EN FLAGS` (F5, where the MCS-48 has `SEL MB1`) makes P2.4 and P2.5 mirror OBF and IBF; `EN DMA` (E5, where the MCS-48 has `SEL MB0`) turns P2.6/P2.7 into DRQ/DACK. Both immediately rewrite P2 in MAME (`port_w(2, m_p2)`), which is how the driver sees the pins change. - `IN A,DBB` acknowledges and clears IBF as a side effect of the read (line 753 onward). These four behaviours are the whole mechanism behind "MCS-48 as an arcade protection part": DECO Cassette's 8041A, Taito's three 8041As in *Great Swordsman*, and Tecmo's 8749 all work by the main CPU writing a byte, the MCU waking on IBF, and the main CPU reading a byte back.