# The F3850 CPU: programmer's model, instruction set, timing What the CPU chip holds and does. Where the program counter and data counter live, and how the memory chips follow the CPU, is in [romc-and-the-psus.md](romc-and-the-psus.md). Every statement carries its source; "Guide" is *F8 Guide to Programming* (Fairchild, 1977), "UG" the *F8 User's Guide* 67095665 (11/77), "DB" the 1982 *Microprocessor Products Data Book* (all pinned in `scripts/pinned_files.json`). ## What is on the chip (DOC: DB F3850 p. 3-17ff; Guide 2.4) | Part | Width | Notes | | --- | --- | --- | | Accumulator A | 8 | | | Scratchpad r0-r63 | 64 x 8 | r9 = J (W's save slot), r10/11 = H (HU, HL), r12/13 = K (KU, KL), r14/15 = Q (QU, QL); r12-r15 also have direct `LR` forms (opcodes 00-07) | | ISAR | 6 | indirect scratchpad address; two octal digits ISARU (bits 5-3) and ISARL (bits 2-0) | | W (status) | 5 | bit 4 ICB (interrupt control), 3 O (overflow), 2 Z, 1 C, 0 S | | I/O ports 0 and 1 | 8 + 8 | on the CPU itself; the only ports an `INS`/`OUTS` reaches in short cycles | | Instruction register, control unit | | decodes the opcode and drives ROMC0-4 every machine cycle | | Clock | | crystal, external or RC (3850-1) modes; outputs phi and WRITE to the memory chips | **Not on the chip:** PC0, PC1, DC0, DC1. They are in each memory chip ([romc-and-the-psus.md](romc-and-the-psus.md)). The CPU reaches them only through ROMC states and the data bus (e.g. `LR Q,DC` makes the chip that owns DC0's address drive DC0's bytes onto the bus). ## Scratchpad addressing (DOC: Guide 2.4.2, 6.22-6.23; DB Table 3) - A 4-bit register field `r` in opcodes 3x, 4x, 5x, Cx, Dx, Ex, Fx: 0-11 name r0-r11 directly; **12 = (IS)**, the byte ISAR points at; **13 = (IS)+**, then increment ISAR; **14 = (IS)-**, then decrement ISAR. So r12-r14 cannot be named directly by these opcodes. 15 is not a defined operand (U, handoff brief). - **Increment and decrement change ISARL only** and wrap inside the octal group: 0'27' + 1 = 0'20', and 0'20' - 1 = 0'27' (Guide 2.4.2, DOC). ISARU is unchanged. - `LISU e` / `LISL e` load one octal digit; `LR IS,A` loads 6 bits; `LR A,IS` reads ISAR (the upper two bits of A: Guide table 6-7 shows `00XXXXXX`, DOC). - `BR7` branches when ISARL != 7: the loop idiom for 8-byte buffers. ## Status flags (DOC: Guide 2.4.3, Table 2-1) - **S = not ALU bit 7** (set when the result is positive). This inversion is the F8's best-known trap: `SR 1` always sets S. - **C = carry out of bit 7.** - **Z = result is zero.** - **O = carry out of bit 7 XOR carry out of bit 6.** - Logical operations (NI, OI, XI, NM, OM, XM, NS, XS, COM, shifts) clear O and C and set Z and S from the result (DB Table 3 columns). - `CI aa` and `CM` compute `aa + not(A) + 1` (i.e. `aa - A`, operand minus accumulator) and set all four flags without storing (DB Table 3). Note the order: the immediate minus A, not A minus the immediate. - `LNK` adds C to A and sets all flags; `INC` adds 1. - `DS r` decrements a scratchpad byte by adding FFh, so C is set unless the byte was 0 (DB Table 3 "r <- (r) + H'FF'"; the carry rule follows from the add, HYPOTHESIS until a judge shows it). - Decimal: `AMD` / `ASD` are the third step of a three-step BCD add: the program first adds 66h in binary (`AI H'66'`); the instruction adds the operand in binary, records the carry C and the intermediate carry IC (out of bit 3), then adds AAh (C=0, IC=0), A0h (C=0, IC=1), 0Ah (C=1, IC=0) or 00h (C=1, IC=1) **with the carry from the low digit to the high digit suppressed** (Guide 6.4, DOC). Which flags the result leaves is not spelled out beyond Table 3's "all four set" (U). ## Instruction set and timing The whole of DB Table 3 is transcribed as data in [table3.tsv](table3.tsv): for every opcode, the machine cycles in order, each cycle's length (S = 4 phi clocks, L = 6) and the ROMC state the CPU drives in it, the status effects, and whether an interrupt can follow. That table is the specification's timing source ("timing as data", accepted JIT proposal J2); the decode table should be generated from it, not typed again. Shape of the set (DOC: DB Table 3; Guide section 6): - 1-byte register operations (LR forms, INC, COM, LNK, shifts, LIS, LISU/LISL, AS, XS, NS, DS, ASD): one short cycle (DS: one long cycle; ASD: two short cycles). - Immediate operations (LI, NI, OI, XI, AI, CI): L(03, operand fetch) + S(00, next fetch). - Memory operations through DC0 (LM, ST, AM, AMD, NM, OM, XM, CM): L(02 or 05) + S(00). - 16-bit register moves between the scratchpad and PC0/PC1/DC0 (LR K,P; LR P,K; PK; LR P0,Q; LR Q,DC; LR DC,Q; LR DC,H; LR H,DC): two long cycles + S. The CPU drives a scratchpad byte on the bus (ROMC 14-19) or the owning memory chip drives a register byte (ROMC 06, 07, 09, 0B). - Branches: relative, 8-bit signed displacement added to the address of the displacement byte (ROMC 01). `BT e` tests W bits 2-0 against a mask ("true" if any selected bit is 1); `BF t` tests W bits 3-0 ("false": branch if all selected bits are 0). 80h (BT 0) never branches; 90h (BF 0, spelled BR) always does. Taken and not-taken paths differ in cycle shape (table3.tsv). - Absolute control transfer (JMP, PI): the high address byte goes through **A**, which is left holding it (DB Table 3: "A <- H'ii'" then "PC0U <- (A)"). Programs that keep data in A across a `PI` lose it: an observable a core must reproduce. - I/O: `INS p` / `OUTS p` (p = 0-15, short form) and `IN pp` / `OUT pp` (pp = 0-255). Ports 0 and 1 are the CPU's own: two short cycles, no bus transaction (DB Figure 11). Ports 4-255 belong to memory chips or I/O chips: the port address goes onto the data bus in one cycle (ROMC 1C or 03), and in the next the owning chip latches A (ROMC 1A) or drives its port (ROMC 1B). Ports 2 and 3 are reserved (UG; U for what an `INS 2` does). - `IN`/`INS` set flags from the value read (O=0, C=0, Z, S). Instruction lengths in phi clocks run from 4 (one short cycle) to 26 (`PI`: L S L L S). At the Channel F's 1.7897725 MHz phi (NTSC; 2.0 MHz PAL, OBSERVED MAME `-listxml`), one short cycle is 2.235 us and `PI` takes 14.5 us. ## Interrupts (DOC: Guide 8.2; DB Table 3) - One request line INT REQ (active low) into the CPU; the CPU takes it at the end of an instruction when ICB = 1, except after the instructions listed as "x" in Table 3. - **The two sources disagree on that list (C1 in the brief):** the Guide (8.2.1) lists PK, PI, POP, JMP, OUTS (if not port 0 or 1), OUT (if not port 0 or 1), EI and LR W,J; Table 3 marks PK, LR P0,Q, OUT (unconditionally), PI, JMP, OUTS 4-15, EI, POP and LR W,J. LR P0,Q and OUT to ports 0/1 are in one list and not the other. - The acknowledge sequence (Table 3 "INTRPT"): L(1C, idle) L(0F: the interrupting chip drives its vector's low byte, every chip copies PC0 to PC1 and loads PC0's low byte) L(13: high byte to PC0's high byte; the interrupting chip clears its request; ICB is cleared) S(00: fetch at the vector). Priority between requesting chips is a daisy chain (PRI IN / PRI OUT) through the memory chips, not a CPU function. - ROMC 10 ("inhibit any modification to the interrupt priority logic") is not used by any instruction in Table 3 (U: when does the CPU drive it?). - Power-on: interrupts disabled (Guide 8.2.1). ## Reset (DOC: DB Table 3 "RESET"; Table 5 ROMC 08) S(1C) L(08: every chip copies PC0 to PC1; the CPU drives 00h and every chip loads it into both bytes of PC0) S(00: fetch from 0000h). ICB is cleared. What else reset clears in the CPU (A, W's flags, ISAR, ports 0/1) is not stated in the DB pages read (U). The MAME trace from reset shows A=00, W=00, IS=00 at the first instruction (OBSERVED, closed box; not a judge). ## I/O port pins (DOC: DB F3851 "I/O Ports"; Guide 6.17 IN) Ports are latched outputs with inverted pins ("when a logic 1 is output to port A or B, it places a 0 V level on the output pin"; the same inversion applies to input) and a pin used for input must first be written with 0 so the latch does not hold it low (Guide, IN/INS notes). The pin model (open-drain with pull-up, the read value as latch OR pin, or as pin only) decides what a program reads back from a port it wrote; the Channel F's controller reads depend on it (channelf-python docs/input.md). The 3850 data sheet's I/O pin options are the source to settle it (U).