# Instruction set and encodings Source: the Information Manual (tier 2), chapter 3 and appendix A (pages A-1 to A-7; the format diagrams are pictures: `pdftoppm -r 80 -f 174 -l 177 -png` on the PDF). Checked field by field against MAME 0.285's `dsp16core.ipp`, `dsp16.cpp` and `dsp16dis.cpp` (tier 5): **no disagreement in the encoding**. Semantics and registers are in [start-here.md](start-here.md), cycles in [timing.md](timing.md), the open points in [undocumented-behavior.md](undocumented-behavior.md). Every instruction is one 16-bit word except `R = N` (two). The type is the **T field, bits 15-11**. ## Formats (appendix A) | Format | Layout (bit 15 left) | Used by | | --- | --- | --- | | 1, 1a | `T(5) D|aT(1) S(1) F1(4) X(1) Y(4)` | multiply/ALU with a Y transfer (1a: `aT`, inverted: 0 = a1) | | 2, 2a | `T(5) D|aT(1) S(1) F1(4) X(1) Z(4)` | multiply/ALU with compound (Z) addressing | | 3 | `T(5) D(1) S(1) F2(4) CON(5)` | special functions `if CON F2`, `ifc CON F2` | | 4 | `T(4) JA(12)` | `goto JA` (T = 0000x), `call JA` (1000x) | | 5 | `T(5) B(3) 0(8)` | branch indirect: `return`, `ireturn`, `goto pt`, `call pt` | | 6 | `T(5) SI(1) 0(5) CON(5)` | conditional branch qualifier `if CON`; with SI = 1, `icall` | | 7, 7a | `T(5) D|aT(1) R(6) Y(4)` | data moves (`R = Y`, `Y = R`, `Z : R`, `R = aS`, `aT = R`) | | 8 | as 7, then a second word N | `R = N` | | 9 | `T(5) I(2) M(9)` | short immediate: `j k rb re` (T 00010) or `r0-r3` (T 00011), 9-bit value | | 10 | `T(5) NI(4) K(7)` | `do K` (NI 1-15) / `redo K` (NI = 0) | The X bit: in two-operand multiply/ALU forms it picks `*pt++` (0) or `*pt++i` (1); in one-operand forms it picks the low (0) or high (1) half of `aT` or `y`. ## T: the instruction type | T | Operation | Format | | T | Operation | Format | | --- | --- | --- | --- | --- | --- | --- | | 0000x | `goto JA` | 4 | | 1000x | `call JA` | 4 | | 00010 | short imm `j k rb re` | 9 | | 10010 | `ifc CON F2` | 3 | | 00011 | short imm `r0-r3` | 9 | | 10011 | `if CON F2` | 3 | | 00100 | F1; `Y = a1[l]` | 1 | | 10100 | F1; `Y = y[l]` | 1 | | 00101 | F1; `Z : aT[l]` | 2a | | 10101 | F1; `Z : y[l]` | 2 | | 00110 | F1; `Y` (postmodify only) | 1 | | 10110 | F1; `x = Y` | 1 | | 00111 | F1; `aT[l] = Y` | 1a | | 10111 | F1; `y[l] = Y` | 1 | | 01000 | `aT = R` | 7a | | 11000 | branch indirect (B) | 5 | | 01001 | `R = a0` | 7 | | 11001 | F1; `y = a0 x = X` | 1 | | 01010 | `R = N` | 8 | | 11010 | `if CON` qualifier / `icall` | 6 | | 01011 | `R = a1` | 7 | | 11011 | F1; `y = a1 x = X` | 1 | | 01100 | `Y = R` | 7 | | 11100 | F1; `Y = a0[l]` | 1 | | 01101 | `Z : R` | 7 | | 11101 | F1; `Z : y x = X` | 2 | | 01110 | `do K` / `redo K` | 10 | | 11110 | reserved | | | 01111 | `R = Y` | 7 | | 11111 | F1; `y = Y x = X` | 1 | ## Field tables (A-5 to A-7) **F1**, the multiply/ALU function (bits 8-5): | F1 | | F1 | | F1 | | F1 | | | --- | --- | --- | --- | --- | --- | --- | --- | | 0000 | `aD = p p = x*y` | 0100 | `aD = p` | 1000 | `aD = aS | y` | 1100 | `aD = y` | | 0001 | `aD = aS + p p = x*y` | 0101 | `aD = aS + p` | 1001 | `aD = aS ^ y` | 1101 | `aD = aS + y` | | 0010 | `p = x*y` | 0110 | `nop` | 1010 | `aS & y` (test, no store) | 1110 | `aD = aS & y` | | 0011 | `aD = aS - p p = x*y` | 0111 | `aD = aS - p` | 1011 | `aS - y` (compare, no store) | 1111 | `aD = aS - y` | **F2**, the special function (bits 8-5): 0000 `aD = aS >> 1`, 0001 `<< 1`, 0010 `>> 4`, 0011 `<< 4`, 0100 `>> 8`, 0101 `<< 8`, 0110 `>> 16`, 0111 `<< 16`, 1000 `aD = p`, 1001 `aDh = aSh + 1` (adds 0x0_0001_0000, clears the low half), 1010 reserved, 1011 `aD = rnd(aS)` (U2), 1100 `aD = y`, 1101 `aD = aS + 1`, 1110 `aD = aS`, 1111 `aD = -aS` (U4). Right shifts are arithmetic (sign from bit 35). **CON** (bits 4-0): 00000 `mi`, 00001 `pl`, 00010 `eq`, 00011 `ne`, 00100 `lvs`, 00101 `lvc`, 00110 `mvs`, 00111 `mvc`, 01000 `heads`, 01001 `tails` (U3), 01010 `c0ge`, 01011 `c0lt`, 01100 `c1ge`, 01101 `c1lt`, 01110 `true`, 01111 `false`, 10000 `gt`, 10001 `le`; 10010-11111 reserved. Testing `c0ge` / `c0lt` (`c1ge`/`c1lt`) **increments** `c0` (`c1`) (3-5). `ifc` first increments `c1`, then tests, and on a true condition does F2 and copies `c1` to `c2`; under `ifc`, a `c0` test does not increment `c0` and a `c1` test increments `c1` once (3-12, 3-38; U20). **Y** (bits 3-0): register `r(Y >> 2)`, postmodify by the low two bits: 00 none, 01 `++`, 10 `--`, 11 `++j`. **Z** (bits 3-0): register `r(Z >> 2)`, compound mode by the low two bits: 00 `zp` (read, then +1 at the write), 01 `pz` (+1 at the read, 0 at the write), 10 `m2` (-1, then +2), 11 `jk` (+j, then +k). A Z transfer reads the old memory word into the named register and writes the register's old value back (3-4). **R** (6 bits): 000000-000011 `r0-r3`, 000100 `j`, 000101 `k`, 000110 `rb`, 000111 `re`, 001000 `pt`, 001001 `pr`, 001010 `pi`, 001011 `i`, 010000 `x`, 010001 `y`, 010010 `yl`, 010011 `auc`, 010100 `psw`, 010101 `c0`, 010110 `c1`, 010111 `c2`, 011000 `sioc`, 011001 `srta`, 011010 `sdx`, 011011 `tdms`, 011100 `pioc`, 011101 `pdx0`, 011110 `pdx1`; others reserved. `sioc`, `srta` and `tdms` are write-only (2-11 footnote 3). Narrow registers read zero-extended except `j`, `k`, `i` (and `c0-c2`), which sign-extend (3-17). **B** (bits 10-8): 000 `return`, 001 `ireturn`, 010 `goto pt`, 011 `call pt`, 1xx reserved. **D**: 0 = a0, 1 = a1. **S**: 0 = a0, 1 = a1. **aT** is inverted: 0 = a1, 1 = a0. **I** (short immediate): 00 `r0`/`j`, 01 `r1`/`k`, 10 `r2`/`rb`, 11 `r3`/`re`. **SI**: 1 makes format 6 an `icall`. ## Statement rules worth a test each - Flags (LMI, LEQ, LLV, LMV) are set by F1 and F2 statements only, never by a transfer or a data move (3-4). `aD = y` as F1/F2 sets them; `a0 = y` as a data move does not. - In `aD = aS + p p = x*y`, the ALU uses the **old** `p` (3-6). - In an F1 instruction with a transfer, the F1 reads registers **before** the transfer writes them (the pipeline: MAME computes `dau_f1` first, then the transfer; the manual's example 3-6 `y = Y` then `aD = aS & y` on the next line shows the same order). - The conditional qualifier `if CON` makes the **next** instruction (which must be `goto`, `call`, `return`, `goto pt`, `call pt`) conditional; the pair is 3 cycles (3-13). - `icall` = `goto 2` taken as an interrupt (IACK, `pi` frozen), 3 cycles (4-6). - The assembler picks, for one source line that fits several, special function first, then multiply/ALU, then short immediate, then data move (4-3, 4-4); a disassembler prints what the word is, not what the assembler would pick. ## What the program uses Executed in the steady-state loop (two MAME traces, 608 distinct addresses, `scripts/cycle_audit.py`): T 00000, 00010, 00011, 00100, 00110, 00111, 01000, 01001, 01010, 01011, 01100, 01110, 01111, 10000, 10011, 10100, 10101, 10110, 10111, 11000, 11010, 11011, 11100, 11111. A linear walk of 0x0000-0x0D52 (`scripts/program_census.py`, an upper bound that also decodes zero padding and data as code) adds 11001 and 11101 in the start-up and filter-refresh code. **Never used by the program** (census, jtdsp16's README, the annotated disassembly): `Z : aT[l]` (00101), `Z : R` (01101), `ifc` (10010), `icall`, `heads`/`tails`, F2 1010 (reserved), the long `do` blocks with forbidden contents. These need directed tests (rung 1); the program cannot check them.