# The TMS9900: programmer's model, instructions, interrupts, CRU Sources, by tag (pins in `scripts/pinned_files.json`): - **[DM]** TMS 9900 Microprocessor Data Manual, May 1976 (tentative data). Tier 2. - **[FSD]** 9900 Family Systems Design and Data Book, 1st edition, 1978. Tier 2. Chapter 4's "9900 Machine Cycle Sequences" (printed pp. 4-89..4-110) is the bus-level description of every instruction; [timing-and-bus.md](timing-and-bus.md) carries it. - **[CTD]** TI-99/4A Console and Peripheral Expansion System Technical Data (in ti99-python's pins): how the 99/4A wires the CPU. Tier 2. Every statement below is DOC unless marked. Page numbers are the printed ones; the OCR text of both books is in `reference/text/` after a fetch. ## The part - N-channel MOS, 64 pins, four non-overlapping clock phases φ1-φ4 (an external TIM9904 clock generator), 3 MHz nominal: tc(φ) = 0.333 µs [DM 1.2, 4.4]. The TI-99/4A runs it at exactly 3.000 MHz from a 48 MHz crystal through the TIM9904 ([CTD] figure A; MAME 0.285's `listxml ti99_4a` says 3,000,000 Hz, OBSERVED). - Address bus A0 (MSB)-A14: **15 bits of word address**. A15 does not leave the chip; the CPU reads and writes whole 16-bit words and picks bytes internally [DM 2; FSD ch. 5 "9900 architecture"]. A board that has an 8-bit bus makes its own A15 (the 99/4A's multiplexer does, [CTD] A.2). - Data bus D0 (MSB)-D15, three-state. Bus control MEMEN, DBIN, WE; READY in, WAIT out; IAQ out (high on instruction-fetch cycles); HOLD in, HOLDA out [DM table 2]. - CRU: CRUOUT, CRUIN, CRUCLK, with the CRU bit address on A3-A14 [DM 2.3]. - Interrupts: INTREQ and the 4-bit code IC0-IC3; LOAD; RESET [DM 2.2, 2.7, table 2]. ## Registers - **PC** (16 bits; bit 15 is never used, instructions are word aligned), **WP** (workspace pointer), **ST** (status) are the only registers on the chip that the program sees [DM 2.1]. - **Workspace registers R0-R15 are memory**: register n is the word at WP + 2n. Every register access is a memory cycle that the board sees and can stretch with READY [DM 2.1; FSD data derivation sequences]. A context switch exchanges only WP, PC and ST (three stores, three fetches). - Special register uses: R0 shift count (bits 12-15) when the instruction's count is 0; R11 return address of BL and the XOP source address; R12 CRU base (bits 3-14); R13, R14, R15 the saved WP, PC, ST of a context switch [DM 3.5; FSD ch. 6]. - R0 cannot be an index register: indexed mode with register 0 is symbolic (direct) mode [DM 3.5.1 notes]. ## Status register [DM 3.4] | Bit | Name | Set by | | --- | --- | --- | | ST0 | L> logical greater than | C, CB, CI (unsigned compare); ABS: operand ≠ 0; others: result ≠ 0 | | ST1 | A> arithmetic greater than | C, CB, CI (signed); ABS: MSB = 0 and ≠ 0; others: MSB = 0 and result ≠ 0 | | ST2 | EQ equal | C, CB, CI equal; COC/CZC match; TB: CRUIN = 1; ABS: operand = 0; others: result = 0 | | ST3 | C carry | A, AB, ABS, AI, DEC, DECT, INC, INCT, NEG, S, SB: carry out; shifts: last bit out | | ST4 | OV overflow | A, AB, AI, S, SB, DEC(T), INC(T), SLA (MSB changed during the shift), DIV (quotient would not fit), ABS and NEG (operand >8000) | | ST5 | OP odd parity | CB, MOVB: source byte; LDCR, STCR when 1 ≤ C ≤ 8; AB, SB, SOCB, SZCB: result byte | | ST6 | X | set by XOP | | ST7-ST11 | not used, read 0 | | | ST12-ST15 | interrupt mask | LIMI, RTWP (from WR15), interrupts (level - 1), RESET and LOAD (0), RSET (0) | The comparisons for C/CB/CI are written in [DM] as MSB tests on the operands and on (DA) - (SA); the table's wording for the subtraction is garbled in the OCR (`MSB of (DA) - (SA) = 1`) and must be read against [FSD] ch. 6 before it becomes code (U4 in the brief). ## Addressing modes [DM 3.2] | T field | Register | Mode | Extra words | Notes | | --- | --- | --- | --- | --- | | 00 | 0-15 | Rn | 0 | byte instructions use the register's left byte; the right byte is kept | | 01 | 0-15 | *Rn | 0 | | | 10 | 0 | @label (symbolic) | 1 | | | 10 | 1-15 | @table(Rn) (indexed) | 1 | | | 11 | 0-15 | *Rn+ | 0 | +1 for a byte operand, +2 for a word | With both operands symbolic or indexed the source's word comes first, then the destination's [DM 3.5.1 note 4]. Jumps use PC-relative addressing (signed byte × 2 added to the updated PC); SBO/SBZ/TB use CRU-relative addressing (signed byte added to R12's bits 3-14) [DM 3.2.7-3.2.8]. ## Instruction formats and opcodes [DM 3.5] Opcodes read from the bit patterns in [DM] tables 3.5.1-3.5.13 (hex is this repository's arithmetic from those patterns, to recheck against [FSD] ch. 6 when the decode table is generated). | Format | Instructions (first opcode) | | --- | --- | | I two general operands | SZC 4000, SZCB 5000, S 6000, SB 7000, C 8000, CB 9000, A A000, AB B000, MOV C000, MOVB D000, SOC E000, SOCB F000 | | III general source, register destination | COC 2000, CZC 2400, XOR 2800, MPY 3800, DIV 3C00 | | IX XOP and CRU multi-bit | XOP 2C00 (D = XOP number), LDCR 3000, STCR 3400 (D = bit count, 0 = 16) | | II jumps and CRU single-bit | JMP 1000, JLT 1100, JLE 1200, JEQ 1300, JHE 1400, JGT 1500, JNE 1600, JNC 1700, JOC 1800, JNO 1900, JL 1A00, JH 1B00, JOP 1C00, SBO 1D00, SBZ 1E00, TB 1F00 | | V shifts | SRA 0800, SRL 0900, SLA 0A00, SRC 0B00 (count in bits 8-11; 0 = R0 bits 12-15; both 0 = 16) | | VI one general operand | BLWP 0400, B 0440, X 0480, CLR 04C0, NEG 0500, INV 0540, INC 0580, INCT 05C0, DEC 0600, DECT 0640, BL 0680, SWPB 06C0, SETO 0700, ABS 0740 | | VIII immediate / internal | LI 0200, AI 0220, ANDI 0240, ORI 0260, CI 0280, STWP 02A0, STST 02C0, LWPI 02E0, LIMI 0300 | | VII control | IDLE 0340, RSET 0360, RTWP 0380, CKON 03A0, CKOF 03C0, LREX 03E0 | Bit 11 of the format VIII instructions is "N, don't care" in [DM]'s drawings, so LI answers to 0200-021F and so on; whether the chip really ignores those bits is U3. **Undefined opcodes.** [DM] table 3's last row (badly OCR'd) lists the illegal opcode ranges 0000-01FF, 0320-033F, 0C00-0FFF, 0780-07FF with 6 clock cycles and 1 memory access; IAQ "can be used to detect illegal op codes" [DM table 2]. What they do beyond costing 6 clocks (a no-op? any status change?) is not documented: U1. ## Semantics worth stating apart - **Read before write.** Every instruction that writes memory first reads the destination word, even MOV (whose read value is unused) and CLR/SETO; byte writes merge the new byte into the word read and store the whole word [FSD 4-93 notes 1-3, 4-95 note]. The 99/4A's technical data warns peripheral designers about exactly this, because its memory-mapped devices (VDP, GROM, speech) have auto-incrementing address registers and therefore separate read and write addresses [CTD B.1.2.1]. - **MPY** is unsigned 16 × 16 → 32 into D (high) and D+1 (low); with D = R15 the low word goes to the word after the workspace [DM 3.5.2]. No status bits. - **DIV** is unsigned 32 / 16. If the divisor ≤ the high word of the dividend, nothing is written and ST4 is set; otherwise quotient → D, remainder → D+1 [DM 3.5.2]. - **X** executes the instruction at its source address; that instruction's extra words come from the PC, and IAQ is not raised for the executed word [DM 3.5.4 note †]. - **BLWP** and **XOP** do not test INTREQ when they complete [DM 3.5.3, 3.5.4]; nor does the first instruction of an interrupt routine get interrupted [DM 2.2]. - **XOP n, src**: new WP from 0040 + 4n, new PC from 0042 + 4n; the source's *address* goes to the new R11; old WP, PC, ST to R13-R15; ST6 set [DM 3.5.3]. - **LDCR/STCR** move C bits (C = 0 means 16) LSB first between memory and consecutive CRU bits; C ≤ 8 addresses a byte, C ≥ 9 a word; STCR zero-fills [DM 2.5, 3.5.5]. STCR with C ≤ 8 fetches the destination word and merges the byte [FSD 4-96 note]. - **External instructions** IDLE, RSET, CKON, CKOF, LREX put a 3-bit code on A0-A2 and pulse CRUCLK; IDLE repeats the code and the pulse until an interrupt, LOAD or RESET [DM 2.6]. RSET also clears the mask. The 99/4A forbids all five: its CRUCLK decode ignores A0-A2, so they look like CRU writes to the console's devices [CTD B.1.3.1]. The board decides what they do; the core only reports them. ## Interrupts, LOAD and RESET [DM 2.2, 2.7; FSD 4-108..4-109] - Level 0 is RESET; levels 1-15 come in on IC0-IC3 with INTREQ. An interrupt is taken after the current instruction when its level ≤ ST12-15. The CPU fetches WP then PC from the vector (4 × level), stores old ST, PC, WP into the new R15, R14, R13, sets the mask to level - 1 and runs at least one instruction of the routine before testing again. INTREQ must stay active until the routine clears its source. - **LOAD** is a non-maskable trap through FFFC/FFFE after the current instruction; it also ends IDLE; the mask becomes 0. If LOAD is held while RESET is released, the LOAD trap follows the reset sequence [DM table 2]. - **RESET** inhibits WE and CRUCLK while low (≥ 3 clocks); on release the CPU runs the level-0 sequence through 0000/0002, clears ST and starts. The 99/4A holds RESET for at least five clocks [CTD A.2]. - On the 99/4A only level 1 is wired (from the TMS 9901), plus LOAD on the I/O port and RESET [CTD A.5]: ti99-python's concern; the core implements all sixteen. ## CRU [DM 2.3-2.5, 2.9.3] 4,096 input and 4,096 output bits, addressed on A3-A14. Software holds twice the bit number in R12 (bits 3-14), so CRU base >1100 is bit 880h. Single-bit operations add the signed displacement to R12's bit address; multi-bit ones increment the address per bit without changing R12. CRUOUT carries the output bit, sampled when CRUCLK pulses; CRUIN is sampled for TB and STCR. During a CRU cycle the address bus carries the CRU address, so a board that decodes memory carelessly sees it; on the 99/4A A15 doubles as CRUOUT on the I/O port [CTD A.2]. ## Variants (for the chip-rule table) | Part | Difference | Family users | | --- | --- | --- | | TMS9900 | this core | TI-99/4 and 99/4A, TI 990/4 and 990/5, Marinchip, many boards | | TMS9980A / 9981 | 8-bit data bus (two transfers per word, memory cycle 4 + 2W clocks), 14-bit address, 4 interrupt levels, on-chip clock [FSD 4-89] | TI's TM990/189 trainer board; none in the family plans | | TMS9995 | on-chip 256 B RAM and decrementer, 8-bit bus, faster microcode, MID trap | TI-99/8, Myarc Geneve 9640, Tomy Tutor / Pyuuta | | TMS99105A / 99110A | 9900 superset with macrostore, privileged mode | none | The 9995 and the 9980A have different machine-cycle sequences and are not configurations of this core; a later `tms9995-python` would be its own repository (open decision 3 in the brief).