# Start here: the 8086 as this core will model it This page is for someone who has written a Z80 or 6502 interpreter and has never touched an 8086. It gives the register file, the flag word, the way an instruction's bytes are split into fields, the segment model, the prefix rules, the interrupt model, and the additions the NEC V20/V30 make. It is written before the code, so where z80-python's primer says "see `_alu.py`", this one names the page of the manual the rule comes from. When the code exists, the code is the reference and this page is the key to reading it. Two manuals are cited throughout, both hosted by bitsavers (scans of Intel and NEC publications; cited, not redistributed): - **Intel**: *The 8086 Family User's Manual*, October 1979, order 9800722-03, . Page labels like "2-24" are the manual's own. Chapter 2 is the CPU, chapter 4 is hardware reference including instruction encoding. The 1981 *iAPX 86,88 User's Manual* () is the same text reorganised; the 1979 edition is cited because its page numbers were checked against the PDF's text layer for this document. - **NEC**: *µPD70108 (V20) / µPD70116 (V30) User's Manual*, October 1986, stock no. 500350, . Sections 3 (functional description), 6 (interrupts), 8 (emulation mode), 11 (addressing modes), 12 (instruction set, one page per form), appendix A (instruction index). ## Register file | Name | Width | Notes | | --- | --- | --- | | AX, BX, CX, DX | 16 | General registers, each addressable as a high and low byte: AH/AL, BH/BL, CH/CL, DH/DL. Implicit uses: AX/AL accumulator for MUL/DIV/IN/OUT/string ops; BX base for XLAT and addressing; CX count for LOOP, REP, and shifts (CL); DX port for IN/OUT DX and the high word of MUL/DIV. (Intel p. 2-6) | | SI, DI | 16 | Index registers; string source (DS:SI) and destination (ES:DI). | | BP, SP | 16 | Base and stack pointers. Both default to the SS segment when used in an effective address (BP) or by PUSH/POP/CALL/RET/INT (SP). PUSH decrements SP by 2 then writes the word. | | CS, DS, SS, ES | 16 | Segment registers: code, data, stack, extra. (Intel p. 2-7) | | IP | 16 | Instruction pointer, an offset in CS. Programs cannot read it directly; it is pushed by CALL and INT. (Intel p. 2-7) | | FLAGS | 16 | Below. | The NEC manual names the same registers AW, BW, CW, DW, IX, IY, BP, SP, PS (CS), DS0 (DS), SS, DS1 (ES), PC (IP), PSW (FLAGS) (NEC p. 3-1, 3-4, Table 12-3 on p. 12-2). MAME's V30 debugger uses the NEC names; the mapping table is in [mame-oracle.md](mame-oracle.md). ## The FLAGS word ```text bit: 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0 1 1 1 1 OF DF IF TF SF ZF 0 AF 0 PF 1 CF ``` - **CF** carry/borrow out of the high bit. **PF** even parity of the low byte of the result. **AF** carry out of bit 3 (for the decimal adjust instructions). **ZF** zero. **SF** bit 7 or 15 of the result. **OF** signed overflow. (Intel p. 2-7 to 2-8, figure 2-9.) - **TF** trap: an INT 1 after every instruction. **IF** enables INTR (not NMI, not internal interrupts). **DF** string direction: 0 increments SI/DI, 1 decrements. (Intel p. 2-8.) - **Fixed bits.** Bits 1, 12, 13, 14, 15 read as 1 and bits 3, 5 read as 0 when FLAGS is stored by PUSHF, and the SingleStepTests 8088 corpus shows exactly that: every initial and final `flags` value has bits 12-15 set and bit 1 set (checked across 10,000 cases of `00.json`). POPF cannot clear them. Intel documents only the nine named flags; the fixed values are the hardware's and are what the corpus judges. - **On the V20/V30** bit 15 is the **MD** (mode) flag, 1 in native mode and 0 in 8080 emulation mode; bits 12-14 are undefined in NEC's figure and read as 1 on the V20 corpus (every case of the V20 `00.json` has bits 12-15 set, as on the 8088). NEC's names are V (OF), DIR (DF), IE (IF), BRK (TF), S, Z, AC (AF), P, CY (CF). (NEC p. 3-3, figure "PSW".) ## Segmentation and effective addresses Memory is 1 MiB of bytes, addressed by a 20-bit physical address. A logical address is a 16-bit segment base and a 16-bit offset; the physical address is `(segment << 4) + offset`, truncated to 20 bits, so `FFFF:0010` wraps to `00000` (Intel p. 2-10 to 2-12; NEC p. 10-1). Words are stored little-endian at any alignment (Intel p. 2-8). Every memory reference has a default segment register, and most can be overridden by a prefix byte (Intel p. 2-14, table 2-1; NEC p. 12-176): | Reference | Default | Override | | --- | --- | --- | | Instruction fetch | CS | no | | Stack (PUSH, POP, CALL, RET, INT, IRET, and BP-based EAs) | SS | yes for BP-based data references only | | Data (everything else, including SI-based string source) | DS | yes | | String destination (DI in MOVS/CMPS/SCAS/STOS/INS/OUTS) | ES | no | The **effective address** is computed from the ModR/M byte and displacement (Intel table 4-10, p. 4-20; NEC table 12-5, p. 12-3): | r/m | mod=00 | mod=01 | mod=10 | Default segment | | --- | --- | --- | --- | --- | | 000 | BX+SI | BX+SI+disp8 | BX+SI+disp16 | DS | | 001 | BX+DI | BX+DI+disp8 | BX+DI+disp16 | DS | | 010 | BP+SI | BP+SI+disp8 | BP+SI+disp16 | SS | | 011 | BP+DI | BP+DI+disp8 | BP+DI+disp16 | SS | | 100 | SI | SI+disp8 | SI+disp16 | DS | | 101 | DI | DI+disp8 | DI+disp16 | DS | | 110 | **disp16 (direct)** | BP+disp8 | BP+disp16 | DS for direct, SS for BP | | 111 | BX | BX+disp8 | BX+disp16 | DS | `disp8` is sign-extended to 16 bits (Intel p. 4-20). The sum is truncated to 16 bits before the segment base is added, so an EA never carries into the segment. `mod=11` selects a register instead of memory. ## Instruction encoding Instructions are one to six bytes: optional prefixes, an opcode byte, usually a ModR/M byte, zero to two displacement bytes, zero to two immediate bytes (Intel p. 4-18 to 4-20, figure 4-20, tables 4-7 to 4-10). The opcode's low bits carry single-bit fields for most two-operand instructions: ```text opcode = oooooo d w w: 0 = byte, 1 = word d: 1 = REG field is the destination, 0 = source ModR/M = mm rrr xxx mm = mod, rrr = reg (or opcode extension), xxx = r/m ``` The `s` bit (in 0x80-0x83) sign-extends an 8-bit immediate to 16 when `w = 1`; the `v` bit (0xD0-0xD3) chooses a count of 1 or CL for shifts; the `z` bit distinguishes REPE from REPNE (Intel table 4-7). The REG field indexes AL CL DL BL AH CH DH BH for byte operations and AX CX DX BX SP BP SI DI for word operations (table 4-9); the two-bit segment field is ES CS SS DS (0-3). The *group* opcodes use REG as an opcode extension: 0x80-0x83 (ADD OR ADC SBB AND SUB XOR CMP), 0xD0-0xD3 (ROL ROR RCL RCR SHL SHR *undocumented 6* SAR), 0xF6/0xF7 (TEST *alias 1* NOT NEG MUL IMUL DIV IDIV), 0xFE (INC DEC, 2-7 undefined), 0xFF (INC DEC CALL CALLF JMP JMPF PUSH *alias 7*), 0x8F (POP, 1-7 undefined), 0xC6/0xC7 (MOV imm, 1-7 undefined but treated as 0). The italicised entries are the subject of [undocumented-behavior.md](undocumented-behavior.md). Intel's table 4-12 (p. 4-20 onward) gives every encoding; table 4-13 (p. 4-27 onward) is the decoding guide by first byte. Appendix A of the NEC manual indexes the same forms by NEC mnemonic with the page holding each encoding. ## Prefixes | Byte | Prefix | Effect | | --- | --- | --- | | 26 2E 36 3E | ES: CS: SS: DS: | Segment override for the instruction's overridable data reference. | | F2 | REPNE/REPNZ | Repeat a string instruction while CX ≠ 0 and (for CMPS/SCAS) ZF = 0. | | F3 | REP/REPE/REPZ | Repeat while CX ≠ 0 and (for CMPS/SCAS) ZF = 1; plain REP for MOVS/STOS/LODS. | | F0 | LOCK | Asserts the bus lock pin for the instruction; no architectural effect on a single-processor core. | Prefixes are "part of" the instruction they precede: no interrupt is recognised between a prefix and its instruction (Intel p. 2-24 footnote). A repeated string instruction checks CX *before* each iteration, so CX = 0 executes nothing; CX is decremented after each element; and the repetition is interruptible between elements, the return address pointing back at the prefix so the operation resumes (Intel p. 2-42). The trap in that mechanism: **an 8086/8088 remembers only the one prefix immediately before the string opcode.** If a REP-prefixed string instruction with a segment override (or LOCK) is interrupted, execution resumes with only the last prefix in effect (Intel p. 2-42; the same caveat is repeated in Intel Application Note AP-67 bound into the manual, p. A-16). The V20/V30 fix this by retaining up to three prefixes and subtracting one from the pushed return address per retained prefix (NEC p. 6-4). ## Interrupts An interrupt of type *n* (0-255) pushes FLAGS, clears IF and TF, pushes CS then IP (the address of the next instruction), and loads IP from the word at physical `4n` and CS from `4n + 2` (Intel p. 2-25 to 2-26). IRET pops IP, CS, FLAGS in that order (Intel p. 2-28). Sources: | Source | Type | How it arrives | | --- | --- | --- | | Divide error | 0 | Generated by DIV/IDIV when the quotient does not fit, including division by zero; on the 8086/8088 the pushed return address is the **next** instruction, as for every other interrupt (SingleStepTests 8088 README; later Intel parts point at the faulting instruction). AAM with an immediate of 0 also raises it (SingleStepTests 8088 README, per-instruction note on D4). | | Single step | 1 | After every instruction while TF = 1. The INT sequence clears TF, so the handler runs normally; setting TF via POPF/IRET takes effect after the *next* instruction (Intel p. 2-28). | | NMI | 2 | Edge-triggered pin, latched, not maskable, no INTA cycles (Intel p. 2-24). | | INT 3 | 3 | One-byte breakpoint opcode 0xCC. | | INTO | 4 | Only if OF = 1 (Intel p. 2-25). | | INT n | n | 0xCD followed by the type byte. | | INTR | from the bus | Level-triggered, honoured only when IF = 1, at the end of the current instruction. The CPU runs two INTA bus cycles and reads the type byte on the second one; on the arcade boards a µPD71059C/8259 supplies it (Intel p. 2-24; NEC p. 6-2 to 6-3). | | BOUND (V20/V30 CHKIND) | 5 | Index outside the bounds (NEC table 6-1, p. 6-1). | **Priority** when several are pending at one boundary: divide error, INT n, INTO (highest); NMI; INTR; single step (lowest) (Intel table 2-3, p. 2-26). The manual's figure 2-31 (p. 2-27) walks through a divide error, an INTR and single-stepping arriving together. **The one-instruction shadow.** No interrupt is recognised until after the instruction following a `MOV` to a segment register or a `POP` of a segment register (Intel p. 2-24 footnote; NEC p. 6-4 lists MOV sreg,reg16 / MOV sreg,mem16 / POP sreg and adds the reverse MOVs). The stated reason is loading SS then SP without an interrupt landing between them. STI and IRET likewise re-enable interrupts only after the end of the following instruction (Intel p. 2-24; NEC p. 6-4 says the same for EI, INT only). NMI requests that arrive during a shadow are held and taken after it (NEC p. 6-4). Neither corpus exercises interrupts, so the shadow is documentation-tier; MAME 0.285's `nec` core applies it after POP SS, after every MOV to a segment register, and after a LOCK prefix (`necinstr.hxx`, `m_no_interrupt`). **HLT** stops until an interrupt (INTR with IF set, or NMI) and resumes at the instruction after HLT (Intel p. 2-29). ## String instructions MOVS, CMPS, SCAS, LODS, STOS, in byte and word forms (opcodes A4-A7, AA-AF), plus INS/OUTS (6C-6F) on the V20/V30. Source is DS:SI (overridable), destination ES:DI (not overridable); each element advances SI and/or DI by 1 or 2 in the direction DF selects; a REP prefix uses CX as the count (Intel p. 2-40 to 2-43, tables 2-12 and 2-13). CMPS computes source minus destination and sets all six status flags; SCAS compares AL/AX with the destination; neither writes memory. Two corpus notes worth knowing before writing the runner: CMPS accesses its two operands in the opposite order when prefixed by REPE/REPNE (SingleStepTests v20 README), and the corpora mask CX to seven bits for REP tests. ## Multiply and divide | Instruction | Operation | CF and OF | Other flags | | --- | --- | --- | --- | | MUL r/m8, r/m16 | AX = AL × src; DX:AX = AX × src (unsigned) | set iff the high half (AH or DX) is nonzero | SF ZF AF PF undefined (Intel p. 2-37) | | IMUL r/m8, r/m16 | same, signed | set iff the high half is not the sign extension of the low half | SF ZF AF PF undefined | | DIV r/m8, r/m16 | AL = AX ÷ src, AH = remainder; AX = DX:AX ÷ src, DX = remainder (unsigned) | undefined | all six undefined; type-0 interrupt if the quotient overflows or src = 0 (Intel p. 2-37) | | IDIV r/m8, r/m16 | same, signed; quotient truncated toward zero, remainder takes the dividend's sign; byte quotient range −127..+127 (0x81..0x7F) and word −32767..+32767 per the manual, overflow otherwise | undefined | all six undefined; type 0 on overflow | | AAM imm8 (0xD4) | AH = AL ÷ imm, AL = AL mod imm (documented only for imm = 10) | undefined | SF ZF PF from AL; AF undefined (Intel p. 2-37) | | AAD imm8 (0xD5) | AL = AH × imm + AL, AH = 0 | undefined | SF ZF PF from AL; AF undefined (Intel p. 2-38) | | CBW, CWD | sign-extend AL into AH, AX into DX | unchanged | none | "Undefined" here is Intel's word. The hardware produces deterministic values, the 8088 corpus captured them, and reproducing them is a later milestone; [undocumented-behavior.md](undocumented-behavior.md) has the rules that are known and the masks for the ones that are not. The V30 has a hardware multiplier: MULU reg8 takes 21-22 clocks against the 8086's 70-77 (NEC p. 12-50; Intel table 2-21). See [timing.md](timing.md). ## The instruction set The complete table with encoding, flags, and 8086 clocks is Intel table 2-21, p. 2-51 to 2-67, and the decoding guide is table 4-13, p. 4-27 to 4-32. This page does not reproduce 60 pages of tables; it gives the map by opcode byte so a reader can find any instruction, and the flag conventions. Flag notation: **all** means OF SF ZF AF PF CF from the result; **logic** means OF = CF = 0, SF ZF PF from the result, AF undefined (Intel p. 2-38). | Opcodes | Instructions | Flags | | --- | --- | --- | | 00-05, 08-0D, 10-15, 18-1D, 20-25, 28-2D, 30-35, 38-3D | ADD, OR, ADC, SBB, AND, SUB, XOR, CMP in the six forms r/m,r; r,r/m; AL,imm8; AX,imm16 (Intel p. 2-35 to 2-36, 2-38) | all (arithmetic) or logic | | 06 07 0E 16 17 1E 1F | PUSH/POP ES, CS (0x0F = POP CS, undocumented), SS, DS | none | | 26 2E 36 3E | segment override prefixes | none | | 27 2F 37 3F | DAA DAS AAA AAS (Intel p. 2-35 to 2-36) | DAA/DAS: OF undefined, rest set; AAA/AAS: AF CF set, OF SF ZF PF undefined | | 40-47, 48-4F | INC r16, DEC r16 | all except CF (Intel p. 2-35, 2-36) | | 50-57, 58-5F | PUSH r16, POP r16 (PUSH SP pushes the decremented value on the 8086/8088) | none | | 60-6F | **not instructions on the 8086/8088**: aliases of 70-7F. V20/V30: PUSHA POPA BOUND *63 undefined* REPNC REPC *66-67 FPO2* PUSH imm16 IMUL imm16 PUSH imm8 IMUL imm8 INSB INSW OUTSB OUTSW | see [undocumented-behavior.md](undocumented-behavior.md) | | 70-7F | Jcc rel8: JO JNO JB JNB JZ JNZ JBE JA JS JNS JP JNP JL JGE JLE JG | none | | 80-83 | group 1: op r/m, imm (83 sign-extends imm8 to a word; 82 is an alias of 80) | as the op | | 84 85 | TEST r/m, r | logic | | 86 87 | XCHG r/m, r | none | | 88-8B | MOV r/m,r / MOV r,r/m | none | | 8C 8E | MOV r/m16, sreg / MOV sreg, r/m16 (only two bits of REG are decoded, so REG 4-7 alias 0-3; 8E with CS is legal on the 8086 and does not flush the queue) | none | | 8D | LEA r16, m | none | | 8F /0 | POP r/m16 | none | | 90-97 | XCHG AX, r16 (90 = NOP) | none | | 98 99 | CBW CWD | none | | 9A | CALL far imm | none | | 9B | WAIT | none | | 9C 9D 9E 9F | PUSHF POPF SAHF LAHF (SAHF loads SF ZF AF PF CF from AH bits 7 6 4 2 0) | POPF: all; SAHF: those five | | A0-A3 | MOV AL/AX, [disp16] and back | none | | A4-A7, AA-AF | MOVS CMPS STOS LODS SCAS | CMPS/SCAS: all | | A8 A9 | TEST AL/AX, imm | logic | | B0-B7, B8-BF | MOV r8, imm8; MOV r16, imm16 | none | | C2 C3 CA CB | RET imm16 / RET / RETF imm16 / RETF (C0 C1 C8 C9 alias these on the 8086; on the V20/V30 they are shift-by-imm8, ENTER, LEAVE) | none | | C4 C5 | LES r16,m32 / LDS r16,m32 | none | | C6 C7 | MOV r/m, imm | none | | CC CD CE CF | INT 3, INT imm8, INTO, IRET | INT clears IF TF; IRET restores all | | D0-D3 | group 2: ROL ROR RCL RCR SHL SHR (6: undocumented SETMO) SAR by 1 or by CL | CF = last bit out; OF defined only for single-bit forms; AF undefined for shifts; rotates leave SF ZF AF PF alone (Intel p. 2-39) | | D4 D5 | AAM imm8, AAD imm8 | above | | D6 | undocumented SALC (AL = CF ? 0xFF : 0x00) | none | | D7 | XLAT: AL = [DS:BX + AL] | none | | D8-DF | ESC (8087 coprocessor); on a board without one, an EA is computed and a memory operand may be read, nothing else | none | | E0 E1 E2 E3 | LOOPNE LOOPE LOOP JCXZ | none | | E4 E5 E6 E7, EC ED EE EF | IN AL/AX, imm8 ; OUT imm8, AL/AX ; IN AL/AX, DX ; OUT DX, AL/AX. A word port access on the 8086 is two byte ports at n and n+1. | none | | E8 E9 EA EB | CALL rel16, JMP rel16, JMP far imm, JMP rel8 | none | | F0 (F1) | LOCK (F1 is decoded as LOCK too) | none | | F2 F3 | REPNE, REP/REPE | none | | F4 F5 | HLT CMC | CMC: CF | | F6 F7 | group 3: TEST imm (1 aliases 0), NOT, NEG, MUL, IMUL, DIV, IDIV | NEG: all, CF = (operand ≠ 0); NOT: none | | F8-FD | CLC STC CLI STI CLD STD | the named flag | | FE | group 4: INC DEC r/m8 (REG 2-7 undefined) | all except CF | | FF | group 5: INC DEC CALL CALLF JMP JMPF PUSH r/m16 (7 aliases 6) | INC/DEC as above | Shift and rotate semantics (Intel p. 2-39): SHL/SAL shift zeros in from the right; SHR shifts zeros in from the left; SAR copies the sign bit in; ROL/ROR wrap; RCL/RCR rotate through CF as a ninth or seventeenth bit. On the 8086/8088 the count in CL is **not masked**, so a count of 255 does 255 shifts (the 80186 and later mask to 5 bits); the corpora mask CL to six bits for test length and say so precisely to catch a 5-bit mask. ## The V20/V30 additions Everything the 8086 does, plus (NEC p. 1-1; instruction pages in section 12; opcode statuses and `arch` tags from the SingleStepTests v20 `metadata.json`): | Opcode | NEC name (Intel-style name) | Notes | | --- | --- | --- | | 60, 61 | PUSH R, POP R (PUSHA, POPA) | Push AW CW DW BW SP BP IX IY; SP pushed is the pre-push value (MAME `necinstr.hxx` and the corpus decide the detail). | | 62 | CHKIND (BOUND) | Type 5 if the register is outside [low, high]; the register form is invalid and halts the V20 (v20 README). | | 64, 65 | REPNC, REPC | Repeat while CY = 0 / CY = 1, for CMPBK/CMPM (CMPS/SCAS) (NEC p. 12-14); on INS/OUTS they act as plain REP (v20 README). | | 68, 6A | PUSH imm16, PUSH imm8 (sign-extended) | | | 69, 6B | MUL reg16, r/m16, imm16 / imm8 (IMUL three-operand) | CY and V set on signed overflow; other flags undefined (metadata). | | 6C-6F | INM, OUTM (INS, OUTS) | Port DW to ES:IY / DS:IX to port DW. | | C0, C1 | shift/rotate r/m, imm8 | Count not masked to 5 bits per the corpus note. | | C8, C9 | PREPARE, DISPOSE (ENTER, LEAVE) | The V20 does not take the nesting level modulo 32 (v20 README). | | 0F 10-1F | TEST1, CLR1, SET1, NOT1 with a CL or immediate bit number | Bit manipulation on r/m8 (bit & 7) or r/m16 (bit & 15). | | 0F 20, 22, 26 | ADD4S, SUB4S, CMP4S | Packed-BCD string add/subtract/compare, CL digits, DS:IX and ES:IY. | | 0F 28, 2A | ROL4, ROR4 | Rotate a nibble through AL. | | 0F 31, 39 | INS (bitfield insert) | Register operand only; memory forms run for >130,000 cycles (v20 README). | | 0F 33, 3B | EXT (bitfield extract) | Same restriction. | | 0F FF | BRKEM imm8 | Enter 8080 emulation mode via an interrupt vector. **Out of scope** together with RETEM and CALLN (NEC section 8): the arcade boards never leave native mode. | | D6 | SETALC (SALC) | Same undocumented instruction as the 8086. | **Interrupt differences** (NEC table 6-1, p. 6-1): the vectors match the 8086 (0 divide, 1 single step, 2 NMI, 3 BRK 3, 4 BRKV, 5 CHKIND, 32-255 general). The clock counts differ and are given per part (V20/V30): NMI 58/38, INT 68/49, BRK imm8 58/38, BRKV 60/40, divide error 65-75/45-55, CHKIND 81-84/53-56. NEC's interrupt-disable list (p. 6-4) is broader than Intel's and is quoted in the interrupt section above. **Undefined-flag differences.** NEC's manual marks undefined flags "U" in each instruction's flag table (table 12-4, p. 12-3), and the pattern matches Intel's for MUL (p. 12-50: only CY and V defined). The *values* the two families produce for undefined flags differ, which is why MAME's core says "flag settings are also correct for the NEC processors rather than the I86 versions" (`nec.cpp` header), and why the V20 corpus's `metadata.json` marks different flags undefined for some opcodes than the 8088 corpus's does (TEST1 leaves S Z AC P undefined and clears CY V; D2/D3 on the V20 mark CY undefined for counted SHL/SHR/SAR where the 8086 corpus does not; D0/D1 REG 6 on the V20 leaves only AC undefined). [undocumented-behavior.md](undocumented-behavior.md) lists them side by side. **Invalid opcodes** behave differently: the 8086 executes something for every byte, while the V20 treats 63, FE.3/FE.5 and FF.3/FF.5 register forms, and BOUND with a register as invalid, halting with no HALT bus status (v20 README). 66 and 67 are FPO2 (coprocessor) on the V20 and Jcc aliases on the 8086. F1 is decoded as a prefix on all three parts (metadata). ## Suggested reading order 1. This page. 2. [timing.md](timing.md): what `step()` will return and why cycle exactness is not a first-core goal. 3. [undocumented-behavior.md](undocumented-behavior.md). 4. [validation.md](validation.md), then the corpus README inside `tests/8088_test_vectors/` once fetched. 5. [handoff-brief.md](handoff-brief.md).