# Start here: the 6809 as this core will model it This page is for someone who knows what a CPU is, has perhaps written a 6502 or Z80 interpreter, and has never touched a 6809. It gives the register file, the condition-code byte, the way an opcode byte splits into an operation and an addressing mode, the postbyte that drives indexed addressing, the two prefix pages, and the complete instruction table with bytes and cycles. There is no core code yet; when there is, every handler should read as a direct statement of what is written here, and this page should be corrected wherever an oracle disagrees with it. Everything on this page is taken from Motorola's own documents unless a line says otherwise: - **MC6809 datasheet** (Motorola, 1983 data-book reprint, "MC6809 8-bit microprocessing unit"): - **MC6809E datasheet**: - **MC6809-MC6809E Microprocessor Programming Manual**, M6809PM(AD), rev 0, May 1983 (13 MB): - **Motorola application note AN-825**, "An Interactive Graphic System Using the MC6809" (the addressing-mode and position-independent-code examples): - Terry Ritter and Joel Boney, "A Microprocessor for the Revolution: The 6809", *Byte*, January to March 1979 (the designers' own account of the addressing modes, the postbyte and the two stacks): Those documents are copyright Motorola (now NXP) and Byte respectively; they are cited, not reproduced. The cycle and byte counts below are the datasheet's "Programming Aid" table, cross-checked line by line against MAME 0.285's `src/devices/cpu/m6809/m6809.lst` and `base6x09.lst` (BSD-3-Clause, Nathan Woods), which model the same counts; every disagreement found is noted where it occurs. Where a behaviour rests on a hardware measurement rather than the datasheet, the line says so and [undocumented-behavior](undocumented-behavior.md) gives the source and its tier. ## Register file | Name | Width | Role | | --- | --- | --- | | A, B | 8 | Accumulators. Together they are **D** (A is the high byte). Most 8-bit operations exist in an A form and a B form; `D` has its own load/store/add/sub/compare | | X, Y | 16 | Index registers. Any indexed operand may use X, Y, U or S as its base | | U | 16 | User stack pointer: `PSHU`/`PULU` push and pull through it, and it is otherwise an ordinary index register the hardware never touches | | S | 16 | Hardware (system) stack pointer: subroutine calls, `PSHS`/`PULS`, interrupts and `SWI` stack through it | | PC | 16 | Program counter. Also usable as an index base (`n,PCR`), which is how position-independent code is written | | DP | 8 | Direct page: the high byte of every direct-mode address. Reset clears it to 0 | | CC | 8 | Condition codes, below | Both stacks grow downward and are **pre-decrement on push, post-increment on pull**: `PSHS A` stores A at S-1 and leaves S pointing at it. A 16-bit value is stored high byte at the lower address (big-endian), and pushed low byte first so it lands that way. There is no separate status register, no alternate register set and no refresh counter. What the host sees is exactly A, B, X, Y, U, S, PC, DP and CC, plus the three interrupt lines and RESET. MAME exposes the same set plus `D` as a view of A:B (`docs/mame-oracle.md`). ## The CC byte ```text bit: 7 6 5 4 3 2 1 0 flag: E F H I N Z V C ``` - **C** carry (or borrow) out of bit 7; for `MUL` it is bit 7 of B, so that `ADCA #0` rounds the high byte. Shifts and rotates go through it. - **V** two's-complement overflow. Logic instructions clear it. Its value after `DAA`, `SEX` and `MUL` is discussed on the undocumented page. - **Z** result was zero. **N** bit 7 (or bit 15) of the result. - **I** inhibits IRQ. **F** inhibits FIRQ. Both are set by RESET and by the interrupt entry sequence; NMI ignores both. - **H** half-carry from bit 3 to bit 4 of an 8-bit add. Only `ADDA/ADDB` and `ADCA/ADCB` define it; it exists for `DAA`. The datasheet marks it "undefined" after subtractions and shifts; hardware leaves it unchanged there (undocumented page, hoglet67 finding). - **E** entire-state flag. Set by the interrupt entry sequence when all registers were stacked (IRQ, NMI, SWI, SWI2, SWI3, CWAI), cleared when only PC and CC were stacked (FIRQ). `RTI` reads it from the stacked CC to know how much to pull back. Conditions for branches are built from these: `BHI` is C=0 and Z=0, `BGE` is N=V, `BGT` is N=V and Z=0, and so on; the full list is in the branch table. ## How an opcode byte is decoded The 6809 opcode map is column-regular in a way a reader should exploit. Write the opcode as two hex digits `hl`: | `h` | Group | `l` selects | Notes | | --- | --- | --- | --- | | 0 | Read-modify-write on memory, **direct** | operation (NEG, COM, LSR, ROR, ASR, ASL, ROL, DEC, INC, TST, JMP, CLR at l = 0,3,4,6,7,8,9,A,C,D,E,F) | 1,2,5,B are undocumented aliases | | 4 | The same operations on **A** (inherent) | same `l` | `4E` is undocumented | | 5 | The same operations on **B** (inherent) | same `l` | | | 6 | The same operations, **indexed** | same `l` | | | 7 | The same operations, **extended** | same `l` | | | 8 | Accumulator A (and X, D) ops, **immediate** | SUB, CMP, SBC, SUBD, AND, BIT, LD, (ST), EOR, ADC, OR, ADD, CMPX, BSR/JSR, LDX, (STX) for l = 0..F | `87`, `8F` undocumented; `8D` is `BSR` | | 9 | the same, **direct** | same `l` | `9D` is `JSR` | | A | the same, **indexed** | | | | B | the same, **extended** | | | | C | Accumulator B (and U, D) ops, **immediate** | SUB, CMP, SBC, ADDD, AND, BIT, LD, (ST), EOR, ADC, OR, ADD, LDD, (STD), LDU, (STU) | `C7`, `CF`, `CD` undocumented | | D, E, F | the same, direct / indexed / extended | | | | 1 | Miscellany: page prefixes `10`/`11`, NOP, SYNC, LBRA, LBSR, DAA, ORCC, ANDCC, SEX, EXG, TFR | | `14`,`15`,`18`,`1B` undocumented | | 2 | Short branches, `l` = condition | BRA, BRN, BHI, BLS, BCC, BCS, BNE, BEQ, BVC, BVS, BPL, BMI, BGE, BLT, BGT, BLE | | | 3 | LEA (X,Y,S,U), PSHS, PULS, PSHU, PULU, RTS, ABX, RTI, CWAI, MUL, SWI | | `38`, `3E` undocumented | So the low digit names the operation and the high digit names the operand form, for the whole of `0x`/`4x`-`7x` and `8x`-`Fx`. The addressing mode of `8x`-`Fx` is `(h - 8) & 3`: 0 immediate, 1 direct, 2 indexed, 3 extended. The `8x`/`Cx` columns differ only in which accumulator (A or B) and which 16-bit register (X or U; D shares) they name. ## The two prefix pages Opcodes `10` and `11` are one-byte prefixes that open a second and third map. Each prefix adds one byte and one cycle. | Prefix | Contents | | --- | --- | | `10` | Long conditional branches `10 21`..`10 2F` (`LBRN`..`LBLE`); `10 3F` `SWI2`; `CMPD` (`10 83/93/A3/B3`), `CMPY` (`10 8C/9C/AC/BC`), `LDY`/`STY` (`10 8E/9E/AE/BE`, `10 9F/AF/BF`), `LDS`/`STS` (`10 CE/DE/EE/FE`, `10 DF/EF/FF`) | | `11` | `11 3F` `SWI3`; `CMPU` (`11 83/93/A3/B3`); `CMPS` (`11 8C/9C/AC/BC`) | Every other byte after a prefix is undocumented; the general rule found on hardware is that the prefix is discarded and the page-1 opcode runs, one cycle longer. A chain of prefixes (`10 10 11 ...`) keeps only the first and charges one cycle for each extra one; the whole chain is one uninterruptible instruction. Both rules are hoglet67's hardware findings, not datasheet text; see the undocumented page. ## Addressing modes | Mode | Syntax | Operand bytes | What is addressed | | --- | --- | --- | --- | | Inherent | `ABX`, `NEGA` | 0 | Named in the opcode | | Immediate | `LDA #$12`, `LDX #$1234` | 1 or 2 | The bytes following the opcode | | Direct | `LDA <$12` | 1 | `DP:$12`, one 256-byte page selected by DP | | Extended | `LDA $1234` | 2 | The full 16-bit address, high byte first | | Relative | `BNE label`, `LBNE label` | 1 or 2 | PC (already past the instruction) plus a signed offset | | Indexed | `LDA ,X`, `LDA $10,Y`, `LDA [$1234]` | 1 postbyte + 0..2 | Computed from a base register, below | ### The indexed postbyte Every indexed instruction is followed by one postbyte. Bits 6-5 (`RR`) pick the base register: 00 = X, 01 = Y, 10 = U, 11 = S. Bit 7 chooses between the short form and the long form; in the long form bit 4 selects **indirection** (the computed address holds the 16-bit address of the operand) and bits 3-0 select the mode. The extra cycles are added to the instruction's base indexed count (the "+" in the table below); the extra bytes are added to its base length. | Postbyte | Assembler | Effective address | + cycles | + bytes | Indirect form | + cycles | | --- | --- | --- | ---: | ---: | --- | ---: | | `0RRnnnnn` | `n,R` | R + 5-bit signed n (-16..15) | 1 | 0 | none (bit 4 is part of n) | | | `1RR00000` | `,R+` | R; then R += 1 | 2 | 0 | **invalid** (see below) | | | `1RR00001` | `,R++` | R; then R += 2 | 3 | 0 | `[,R++]` | 6 | | `1RR00010` | `,-R` | R -= 1; then R | 2 | 0 | **invalid** | | | `1RR00011` | `,--R` | R -= 2; then R | 3 | 0 | `[,--R]` | 6 | | `1RR00100` | `,R` | R | 0 | 0 | `[,R]` | 3 | | `1RR00101` | `B,R` | R + sign-extended B | 1 | 0 | `[B,R]` | 4 | | `1RR00110` | `A,R` | R + sign-extended A | 1 | 0 | `[A,R]` | 4 | | `1RR01000` | `n,R` | R + 8-bit signed n | 1 | 1 | `[n,R]` | 4 | | `1RR01001` | `n,R` | R + 16-bit signed n | 4 | 2 | `[n,R]` | 7 | | `1RR01011` | `D,R` | R + D (signed 16-bit) | 4 | 0 | `[D,R]` | 7 | | `1xx01100` | `n,PCR` | PC (after the operand bytes) + 8-bit signed n | 1 | 1 | `[n,PCR]` | 4 | | `1xx01101` | `n,PCR` | PC (after the operand bytes) + 16-bit signed n | 5 | 2 | `[n,PCR]` | 8 | | `10011111` | `[n]` | extended indirect: the 16-bit address n holds the operand address | 5 | 2 | (this *is* the indirect form) | | Rules the table implies: - Indirection always costs 3 more cycles than the direct form (two reads of the pointer plus one internal cycle) and never changes the byte count. - `,R+` and `,-R` have no indirect forms: postbytes `1RR10000` and `1RR10010` are undefined. So are `1RR00111`, `1RR01010`, `1RR01110` and `1RR11111` with RR != 00. The datasheet does not say what they do; MAME 0.285 computes an effective address of 0 with no extra cycles for them (`m6809.lst`, the `default:` arm of the postbyte switch), which is an implementation choice and is marked `[unverified]` on the undocumented page. - Auto-increment happens after the address is used; auto-decrement before. With `,R++` on the same register the instruction loads (`LDX ,X++`) the increment is applied to the old X and then overwritten by the load: the programming manual documents this ordering. - The 5-bit offset form has no indirect variant because bit 4 is part of the offset. - `,PCR` offsets are relative to the address of the *next* instruction (the assembler computes them); for the 8-bit form that is the postbyte address + 2. ## The complete instruction table Cycle counts are E-clock cycles on both the MC6809 and the MC6809E: the two datasheets print the same Programming Aid table, and MAME runs both parts from one instruction list with only the input-clock divider differing (the 6809 divides its EXTAL by 4; the 6809E is fed E and Q directly). "+" means add the indexed-postbyte cost from the table above; "n" in `PSHS`/`PULS` is the number of bytes moved. Bytes for indexed forms are the base count plus the postbyte's extra bytes. ### Inherent | Mnemonic | Op | Cycles | Bytes | Effect and flags | | --- | --- | ---: | ---: | --- | | ABX | 3A | 3 | 1 | X = X + B (unsigned). No flags | | ASLA / ASLB (LSLA / LSLB) | 48 / 58 | 2 | 1 | Shift left through C. N Z V C; H undefined (hardware: unchanged) | | ASRA / ASRB | 47 / 57 | 2 | 1 | Arithmetic shift right, bit 7 kept. N Z C; H undefined (unchanged) | | CLRA / CLRB | 4F / 5F | 2 | 1 | 0. N=0 Z=1 V=0 C=0 | | COMA / COMB | 43 / 53 | 2 | 1 | Ones' complement. N Z, V=0, C=1 | | CWAI #n | 3C | 20 | 2 | CC &= n; set E; push all; wait for an interrupt, then vector (below) | | DAA | 19 | 2 | 1 | Decimal adjust A after an add. N Z C; V "undefined" (hardware: C xor new bit 7) | | DECA / DECB | 4A / 5A | 2 | 1 | N Z V (V set only when operand was $80); C unchanged | | EXG r1,r2 | 1E | 8 | 2 | Exchange, postbyte below. No flags unless CC is named | | INCA / INCB | 4C / 5C | 2 | 1 | N Z V (V set only when operand was $7F); C unchanged | | LSRA / LSRB | 44 / 54 | 2 | 1 | Logical shift right. N=0 Z C | | MUL | 3D | 11 | 1 | D = A x B unsigned. Z (D == 0), C = bit 7 of B; N V unchanged | | NEGA / NEGB | 40 / 50 | 2 | 1 | Two's complement. N Z V (operand $80) C (operand != 0); H undefined (unchanged) | | NOP | 12 | 2 | 1 | | | PSHS regs | 34 | 5 + n | 2 | Push onto S, mask below | | PSHU regs | 36 | 5 + n | 2 | Push onto U | | PULS regs | 35 | 5 + n | 2 | Pull from S | | PULU regs | 37 | 5 + n | 2 | Pull from U | | ROLA / ROLB | 49 / 59 | 2 | 1 | Rotate left through C. N Z V C | | RORA / RORB | 46 / 56 | 2 | 1 | Rotate right through C. N Z C | | RTI | 3B | 6 or 15 | 1 | Pull CC; if its E is set pull A B DP X Y U PC (15), else PC only (6) | | RTS | 39 | 5 | 1 | Pull PC from S | | SEX | 1D | 2 | 1 | A = sign of B (D = sign-extended B). N Z; V per datasheet cleared, hardware unchanged | | SWI | 3F | 19 | 1 | Set E, push all on S, set I and F, PC = [$FFFA] | | SWI2 | 10 3F | 20 | 2 | Set E, push all, PC = [$FFF4]. I and F **not** set | | SWI3 | 11 3F | 20 | 2 | Set E, push all, PC = [$FFF2]. I and F not set | | SYNC | 13 | >= 4 | 1 | Stop until any interrupt line is asserted (below) | | TFR r1,r2 | 1F | 6 | 2 | Copy, postbyte below. No flags unless CC is the destination | | TSTA / TSTB | 4D / 5D | 2 | 1 | N Z, V=0; C unchanged | ### Memory read-modify-write (direct / indexed / extended) All twelve take the same shape: direct 6 cycles / 2 bytes, indexed 6+ / 2+, extended 7 / 3, except `JMP` and `TST` as marked. | Mnemonic | Direct | Indexed | Extended | Flags | | --- | --- | --- | --- | --- | | NEG | 00 | 60 | 70 | as NEGA | | COM | 03 | 63 | 73 | as COMA | | LSR | 04 | 64 | 74 | as LSRA | | ROR | 06 | 66 | 76 | as RORA | | ASR | 07 | 67 | 77 | as ASRA | | ASL / LSL | 08 | 68 | 78 | as ASLA | | ROL | 09 | 69 | 79 | as ROLA | | DEC | 0A | 6A | 7A | as DECA | | INC | 0C | 6C | 7C | as INCA | | TST | 0D (6) | 6D (6+) | 7D (7) | N Z V=0; memory not written | | JMP | 0E (**3**) | 6E (**3+**) | 7E (**4**) | none | | CLR | 0F | 6F | 7F | as CLRA; the operand is read before it is written | ### Accumulator and register operations (immediate / direct / indexed / extended) Base cycles and bytes by operand width. 8-bit register ops: immediate 2 / 2, direct 4 / 2, indexed 4+ / 2+, extended 5 / 3. 16-bit ops one cycle and (for immediate) one byte more: immediate 3 / 3, direct 5 / 2, indexed 5+ / 2+, extended 6 / 3; the compares and `ADDD`/`SUBD` are one more again (4 / 6 / 6+ / 7). Page-2/3 forms add one cycle and one byte to each. | Mnemonic | Imm | Dir | Idx | Ext | Cycles (imm/dir/idx/ext) | Flags | | --- | --- | --- | --- | --- | --- | --- | | ADCA / ADCB | 89 / C9 | 99 / D9 | A9 / E9 | B9 / F9 | 2 / 4 / 4+ / 5 | H N Z V C | | ADDA / ADDB | 8B / CB | 9B / DB | AB / EB | BB / FB | 2 / 4 / 4+ / 5 | H N Z V C | | ADDD | C3 | D3 | E3 | F3 | 4 / 6 / 6+ / 7 | N Z V C | | ANDA / ANDB | 84 / C4 | 94 / D4 | A4 / E4 | B4 / F4 | 2 / 4 / 4+ / 5 | N Z V=0 | | ANDCC #n | 1C | | | | 3 | CC &= n | | BITA / BITB | 85 / C5 | 95 / D5 | A5 / E5 | B5 / F5 | 2 / 4 / 4+ / 5 | N Z V=0 (A & M, A unchanged) | | CMPA / CMPB | 81 / C1 | 91 / D1 | A1 / E1 | B1 / F1 | 2 / 4 / 4+ / 5 | N Z V C; H undefined (unchanged) | | CMPD | 10 83 | 10 93 | 10 A3 | 10 B3 | 5 / 7 / 7+ / 8 | N Z V C | | CMPS | 11 8C | 11 9C | 11 AC | 11 BC | 5 / 7 / 7+ / 8 | N Z V C | | CMPU | 11 83 | 11 93 | 11 A3 | 11 B3 | 5 / 7 / 7+ / 8 | N Z V C | | CMPX | 8C | 9C | AC | BC | 4 / 6 / 6+ / 7 | N Z V C | | CMPY | 10 8C | 10 9C | 10 AC | 10 BC | 5 / 7 / 7+ / 8 | N Z V C | | EORA / EORB | 88 / C8 | 98 / D8 | A8 / E8 | B8 / F8 | 2 / 4 / 4+ / 5 | N Z V=0 | | JSR | | 9D | AD | BD | - / 7 / 7+ / 8 | push PC on S, jump | | LDA / LDB | 86 / C6 | 96 / D6 | A6 / E6 | B6 / F6 | 2 / 4 / 4+ / 5 | N Z V=0 | | LDD | CC | DC | EC | FC | 3 / 5 / 5+ / 6 | N Z V=0 | | LDS | 10 CE | 10 DE | 10 EE | 10 FE | 4 / 6 / 6+ / 7 | N Z V=0; enables NMI after reset | | LDU | CE | DE | EE | FE | 3 / 5 / 5+ / 6 | N Z V=0 | | LDX | 8E | 9E | AE | BE | 3 / 5 / 5+ / 6 | N Z V=0 | | LDY | 10 8E | 10 9E | 10 AE | 10 BE | 4 / 6 / 6+ / 7 | N Z V=0 | | LEAS / LEAU | | | 32 / 33 | | 4+ | none | | LEAX / LEAY | | | 30 / 31 | | 4+ | Z only | | ORA / ORB | 8A / CA | 9A / DA | AA / EA | BA / FA | 2 / 4 / 4+ / 5 | N Z V=0 | | ORCC #n | 1A | | | | 3 | CC \|= n | | SBCA / SBCB | 82 / C2 | 92 / D2 | A2 / E2 | B2 / F2 | 2 / 4 / 4+ / 5 | N Z V C; H undefined (unchanged) | | STA / STB | | 97 / D7 | A7 / E7 | B7 / F7 | - / 4 / 4+ / 5 | N Z V=0 | | STD | | DD | ED | FD | - / 5 / 5+ / 6 | N Z V=0 | | STS | | 10 DF | 10 EF | 10 FF | - / 6 / 6+ / 7 | N Z V=0 | | STU | | DF | EF | FF | - / 5 / 5+ / 6 | N Z V=0 | | STX | | 9F | AF | BF | - / 5 / 5+ / 6 | N Z V=0 | | STY | | 10 9F | 10 AF | 10 BF | - / 6 / 6+ / 7 | N Z V=0 | | SUBA / SUBB | 80 / C0 | 90 / D0 | A0 / E0 | B0 / F0 | 2 / 4 / 4+ / 5 | N Z V C; H undefined (unchanged) | | SUBD | 83 | 93 | A3 | B3 | 4 / 6 / 6+ / 7 | N Z V C | The immediate column of the store instructions (`87`, `8F`, `C7`, `CF`, and their page-2/3 forms) is where the undocumented "store immediate" opcodes live; they are on the undocumented page. ### Branches Short branches: 2 bytes, 3 cycles whether or not taken. Long branches: 4 bytes (prefix `10`), 5 cycles not taken, 6 taken; `LBRA` (`16`) and `LBSR` (`17`) are unprefixed 3-byte forms. | Mnemonic | Short | Long | Taken when | | --- | --- | --- | --- | | BRA | 20 | 16 `LBRA` (5 cycles, 3 bytes) | always | | BRN | 21 | 10 21 | never (a 2- or 4-byte NOP) | | BHI | 22 | 10 22 | C=0 and Z=0 | | BLS | 23 | 10 23 | C=1 or Z=1 | | BCC / BHS | 24 | 10 24 | C=0 | | BCS / BLO | 25 | 10 25 | C=1 | | BNE | 26 | 10 26 | Z=0 | | BEQ | 27 | 10 27 | Z=1 | | BVC | 28 | 10 28 | V=0 | | BVS | 29 | 10 29 | V=1 | | BPL | 2A | 10 2A | N=0 | | BMI | 2B | 10 2B | N=1 | | BGE | 2C | 10 2C | N=V | | BLT | 2D | 10 2D | N!=V | | BGT | 2E | 10 2E | Z=0 and N=V | | BLE | 2F | 10 2F | Z=1 or N!=V | | BSR | 8D (7 cycles, 2 bytes) | 17 `LBSR` (9 cycles, 3 bytes) | always; pushes PC on S | The offset is relative to the address following the branch instruction. ## PSHS / PULS / PSHU / PULU The postbyte is a register mask: ```text bit: 7 6 5 4 3 2 1 0 reg: PC U/S Y X DP B A CC ``` Bit 6 names the *other* stack pointer: U for `PSHS`/`PULS`, S for `PSHU`/`PULU`. A push stores registers in the order PC, U/S, Y, X, DP, B, A, CC (high bit first), each 16-bit register low byte then high byte, so that the stack afterwards reads, from the lowest address upward: CC A B DP X Y U/S PC. That is exactly the frame the interrupt sequence builds, which is why `PULS CC,A,B,DP,X,Y,U,PC` and `RTI` (with E set) read the same frame. Pulls go in the reverse order, CC first. `PULS PC` is a return (`RTS` is `PULS PC` in 5 cycles). Cycles: 5 plus one per byte moved. MAME's model of the push has one dummy read of the stack top before the first write (`PSHS:` in `base6x09.lst`), and of the pull one dummy read after the last; the `neetandev/m6809` corpus records those bus cycles. ## TFR and EXG Postbyte: source register in the high nibble, destination in the low nibble (`TFR X,Y` is `1F 12`). | Code | Register | Width | | --- | --- | --- | | 0 | D | 16 | | 1 | X | 16 | | 2 | Y | 16 | | 3 | U | 16 | | 4 | S | 16 | | 5 | PC | 16 | | 8 | A | 8 | | 9 | B | 8 | | A | CC | 8 | | B | DP | 8 | | 6, 7, C, D, E, F | undefined | | The datasheet allows only same-width pairs. Mixed 8/16-bit pairs and the undefined codes do something on hardware, which several sources have measured and do not entirely agree on; the undocumented page has them. `TFR r,S` and `EXG` naming S count as "loading S" for the NMI rule in the next section (MAME models `TFR` to S and `LEAS`/`LDS` that way; `EXG` is not marked in MAME, `[unverified]` on hardware). ## Reset, interrupts, SWI, CWAI, SYNC **Reset** (level-sensitive RESET pin): DP = 0, I = 1, F = 1, PC = the word at `$FFFE`. Every other register is undefined. NMI is not recognised until the program has loaded S (`LDS`, `LEAS`, `TFR` or `EXG` into S), so that a stray NMI cannot stack through an uninitialised pointer; the datasheet states this rule and MAME implements it as `m_lds_encountered`. **Vectors** (all fetched high byte first): | Address | Vector | | --- | --- | | $FFF0 | reserved (the 6309 uses it for the illegal-opcode/division trap) | | $FFF2 | SWI3 | | $FFF4 | SWI2 | | $FFF6 | FIRQ | | $FFF8 | IRQ | | $FFFA | SWI | | $FFFC | NMI | | $FFFE | RESET | **Hardware interrupt entry.** The lines are sampled at instruction boundaries. NMI is edge-triggered and always taken; FIRQ is level-sensitive and masked by F; IRQ is level-sensitive and masked by I. Priority is NMI, then FIRQ, then IRQ. Entry then does, in this order: | Line | Stacked on S (lowest address last) | E | Then sets | Vector | Cycles (MAME model) | | --- | --- | --- | --- | --- | ---: | | NMI | PC U Y X DP B A CC | 1 | I and F | $FFFC | 19 | | FIRQ | PC CC | 0 | I and F | $FFF6 | 10 | | IRQ | PC U Y X DP B A CC | 1 | I only | $FFF8 | 19 | The counts are: two dummy instruction-stream reads and one internal cycle, the stack writes (12 or 3), one internal cycle, two vector reads, and one internal cycle. The `docs/mame-oracle.md` trace of Robotron shows the IRQ entry as exactly 19 cycles between the last instruction of the interrupted code and the first at `$DC56`. The datasheet's interrupt timing diagram shows the same sequence. **RTI** pulls CC first; if E is set it then pulls A B DP X Y U PC (15 cycles in all), otherwise only PC (6 cycles). It is the same instruction whichever of NMI, FIRQ, IRQ, SWI or CWAI built the frame. **SWI** stacks everything with E set and, unlike SWI2/SWI3, also sets I and F. SWI2 and SWI3 stack everything with E set and leave I and F alone (which is why OS-9 uses SWI2 for system calls: they remain interruptible). **CWAI #n** ANDs CC with n (typically to clear I or F), sets E, stacks the entire state, and then waits with the bus released for any interrupt line. When one arrives it does not stack again; it sets I (and F unless the interrupt was IRQ) and takes the vector. Total 20 cycles plus the wait. This is the only way to get a full-state frame from a FIRQ. **SYNC** stops the processor until any of the three lines is asserted. If that interrupt is enabled it is taken normally; if it is masked, execution simply continues with the next instruction. The datasheet gives 4 cycles for the instruction itself; MAME charges 3 plus the wait (`SYNC:` in `base6x09.lst`), a one-cycle disagreement that a bus-captured oracle would settle (`[unverified]`). ## DAA, MUL, SEX, ABX - **DAA** adds `$06`, `$60` or `$66` to A according to H, C and the two nibbles, exactly as the programming manual's table, and sets C if the correction produced a carry or C was already set. N and Z follow the result. V is "undefined" in the datasheet; hardware sets it to the new C xor the new bit 7 of A (hoglet67, hardware). - **MUL** is the only 8x8 multiply: unsigned, D = A x B, 11 cycles. Z reflects the 16-bit result; C is bit 7 of B so that `ADCA #0` rounds D to the nearest high byte. N and V are not touched. - **SEX** sign-extends B into A. N and Z follow D. The datasheet says V is cleared; hardware leaves it unchanged (hoglet67). - **ABX** adds B, zero-extended, to X and sets no flags. It is the only unsigned-index add; every indexed offset is signed. ## 6809, 6809E, 68B09 and what is out of scope | Part | Clocking | Difference for an instruction core | | --- | --- | --- | | MC6809 | On-chip oscillator; EXTAL/XTAL at 4x the bus rate, generating E and Q outputs | none | | MC6809E | E and Q are inputs; adds LIC, AVMA, BUSY and TSC pins for external bus control | none: same instruction set, same cycle counts. Williams, Konami and Namco boards use this part with a board-generated clock | | MC68A09(E), MC68B09(E) | 1.5 MHz and 2 MHz speed grades | none | | Hitachi HD6309 / HD63C09 | Pin-compatible; in "native" mode adds registers (E, F, W, V, MD), instructions and faster timings; in emulation mode is a 6809 that also traps illegal opcodes | **not in the first core**, but a mode to add here afterwards rather than a separate project: see [hd6309.md](hd6309.md) | | Konami-1 (Konami 052001 / "KONAMI" custom) | 6809-derived custom with a re-mapped opcode set, used on Konami boards from about 1988 (Ajax, Gradius III, TMNT) | **out of scope**; MAME models it as `konami.lst`, a separate map. Not to be confused with Konami's *encrypted* 6809 programs on earlier boards (Gyruss's sound CPU is a plain 6809 with the program in clear; the main CPU is a Z80) | MAME's `m6809.cpp` header records that the MC6809's on-chip divider and the 6809E's lack of one were once assigned the wrong way round in MAME's device tree and are being straightened out; a host that copies a clock value from a MAME driver should check which device type it is attached to ([timing](timing.md)). ## Suggested reading order 1. This page. 2. [undocumented-behavior](undocumented-behavior.md): the illegal opcodes and undefined flags, with the tier of each source. 3. [timing](timing.md): cycle rules and the arcade host contract. 4. [validation](validation.md) and [mame-oracle](mame-oracle.md): what can be checked against what. 5. [handoff-brief](handoff-brief.md): the plan a build session follows.