# Super NES Programming/65c816 reference
## Internal Registers

| bits | Register | Description |
| 8 or 16 | A | The accumulator. This is the math register. It stores one of two operands or the result of most arithmetic and logical operations. |
| 8 or 16 | X, Y | The index registers. These can be used to reference memory, to pass data to memory, or as counters for loops. |
| 16 | S | The stack pointer, points to the next available(unused) location on the stack. |
| 8 | DBR | Data bank register, holds the default bank for memory transfers. |
| 16 | D | Direct page register, used for direct page addressing modes. |
| 8 | PBR | Program Bank, holds the bank address of all instruction fetches. |
| 8 | P | Processor Status, holds various important flags, see below. |
| 16 | PC | Program counter |

##### Flags stored in P register

| Mnemonic | Value | Binary Value | Description | Description |
| N | #$80 | 10000000 | Negative | Condition codes used for branch instructions. |
| V | #$40 | 01000000 | Overflow | Condition codes used for branch instructions. |
| Z | #$02 | 00000010 | Zero | Condition codes used for branch instructions. |
| C | #$01 | 00000001 | Carry | Condition codes used for branch instructions. |
| D | #$08 | 00001000 | Decimal | Decimal |
| I | #$04 | 00000100 | IRQ disable | IRQ disable |
| X | #$10 | 00010000 | Index register size (native mode only) (0 = 16-bit, 1 = 8-bit) | Index register size (native mode only) (0 = 16-bit, 1 = 8-bit) |
| M | #$20 | 00100000 | Accumulator register size (native mode only) (0 = 16-bit, 1 = 8-bit) | Accumulator register size (native mode only) (0 = 16-bit, 1 = 8-bit) |
| E | not in P | | 6502 emulation mode | 6502 emulation mode |
| B | #$10 | 00010000 | Break (emulation mode only) | Break (emulation mode only) |

## Instructions
### Arithmetic and Logical Instructions

| Instruction | Description | Arguments | Flags set |
| ADC | Add A with something and carry bit. Result is put in A. | Immediate value or address | n,v,z,c | Arithmetic instructions |
| SBC | Subtract something and the carry bit. | | n,v,z,c | Arithmetic instructions |
| AND | AND A with memory, storing result in A. | Immediate value or address | n,z | Logical instructions |
| EOR | Exclusive or | | n,z | Logical instructions |
| ORA | OR A with memory, storing result in A. | Immediate value or address | n,z | Logical instructions |
| TSB | Test and set bits | | z | Logical instructions |
| TRB | Test and reset bits | | z | Logical instructions |
| ASL | Arithmetic shift left | A or address | n,z,c | Shift instructions |
| LSR | Logical shift right | A or address | n,z,c | Shift instructions |
| ROL | Rotate left | A or address | n,z,c | Shift instructions |
| ROR | Rotate right | A or address | n,z,c | Shift instructions |
| BIT | test bits, setting | immediate value or address | n,v,z (only z if in immediate mode) | Test instructions |
| CMP | Compare accumulator with memory | | n,z,c | Test instructions |
| CPX | Compare register X with memory | | n,z,c | Test instructions |
| CPY | Compare register Y with memory | | n,z,c | Test instructions |
| DEA | Decrement Accumulator | | n,z |
| DEC | Decrement, see INC | | n,z |
| DEX | Decrement X register | | n,z |
| DEY | Decrement Y register | | n,z |
| INA | Increment Accumulator | | n,z |
| INC | Increment, see DEC | | n,z |
| INX | Increment X register | | n,z |
| INY | Increment Y register | | n,z |
| NOP | No operation | | none |
| XBA | Exchange bytes of accumulator | | n,z |

### Load/Store Instructions

| Instruction | Description |
| LDA | Load accumulator from memory |
| LDX | Load register X from memory |
| LDY | Load register Y from memory |
| STA | Store accumulator in memory |
| STX | Store register X in memory |
| STY | Store register Y in memory |
| STZ | Store zero in memory |

### Transfer Instructions

| Instruction | Description | Flags affected |
| TAX | Transfer Accumulator to index register X | n,z |
| TAY | Transfer Accumulator to index register Y | n,z |
| TCD | Transfer 16-bit Accumulator to Direct Page register | n,z |
| TCS | Transfer 16-bit Accumulator to Stack Pointer | none |
| TDC | Transfer Direct Page register to 16-bit Accumulator | n,z |
| TSC | Transfer Stack Pointer to 16-bit Accumulator | n,z |
| TSX | Transfer Stack Pointer to index register X | n,z |
| TXA | Transfer index register X to Accumulator | n,z |
| TXS | Transfer index register X to Stack Pointer | none |
| TXY | Transfer index register X to index register Y | n,z |
| TYA | Transfer index register Y to Accumulator | n,z |
| TYX | Transfer index register Y to index register X | n,z |

### Branch Instructions

| Instruction | Description |
| BCC | Branch if Carry flag is clear (C=0) |
| BCS | Branch if Carry flag is set (C=1) |
| BNE | Branch if not equal (Z=0) |
| BEQ | Branch if equal (Z=1) |
| BPL | Branch if plus (N=0) |
| BMI | Branch if minus (N=1) |
| BVC | Branch if overflow flag is clear (V=0) |
| BVS | Branch if overflow flag is set (V=1) |
| BRA | Branch Always (unconditional) |
| BRL | Branch Always Long (unconditional) |

### Jump and call instructions

| Instruction | Description |
| JMP | Jump |
| JML | Jump long |
| JSR | Jump and save return address |
| JSL | Jump long and save return address |
| RTS | Return from subroutine |
| RTL | Return long from subroutine |

### Interrupt instructions

| Instruction | Description |
| BRK | Generate software interrupt |
| COP | Generate coprocessor interrupt |
| RTI | Return from interrupt |
| STP | Stop processor until RESET |
| WAI | Wait for hardware interrupt |

### P Flag instructions

| Instruction | Description |
| CLC | Clear carry flag |
| CLD | Clear decimal flag (binary arithmetic) |
| CLI | Enable interrupt requests |
| CLV | Clear overflow flag |
| REP | Reset status bits (for example REP #%00100000 clears the M flag) |
| SEC | Set carry flag |
| SED | Set decimal flag (decimal arithmetic) |
| SEP | Set status bits (for example SEP #%00010000 sets the X flag) |
| SEI | Disable interrupt requests |
| XCE | Exchange carry flag with emulation flag |

### Stack Instructions

| Instruction | Description |
| PHA | Push Accumulator | Push instructions |
| PHX | Push index register X | Push instructions |
| PHY | Push index register Y | Push instructions |
| PHD | Push direct page register | Push instructions |
| PHB | Push data bank register | Push instructions |
| PHK | Push Program Bank Register | Push instructions |
| PHP | Push processor status | Push instructions |
| PEA | Push effective address | Push instructions |
| PEI | Push effective indirect address | Push instructions |
| PER | Push effective relative address | Push instructions |
| PLA | Pull Accumulator | Pull instructions |
| PLX | Pull index register X | Pull instructions |
| PLY | Pull index register Y | Pull instructions |
| PLP | Pull processor status | Pull instructions |
| PLD | Pull direct page register | Pull instructions |
| PLB | Pull data bank register | Pull instructions |
* 65816 Opcode Table.

## Addressing modes

| Mode | Example |
| Implied | PHB |
| Immediate[MemoryFlag] | AND #1 or 2 bytes |
| Immediate[IndexFlag] | LDX #1 or 2 bytes |
| Immediate[8-Bit] | SEP #byte |
| Relative | BEQ byte (signed) |
| Relative long | BRL 2bytes (signed) |
| Direct | AND byte |
| Direct indexed (with X) | AND byte, x |
| Direct indexed (with Y) | AND byte, y |
| Direct indirect | AND (byte) |
| Direct indexed indirect | AND (byte, x) |
| Direct indirect indexed | AND (byte), y |
| Direct indirect long | AND [byte] |
| Direct indirect indexed long | AND [byte], y |
| Absolute | AND 2bytes |
| Absolute indexed (with X) | AND 2bytes, x |
| Absolute indexed (with Y) | AND 2bytes, y |
| Absolute long | AND 3bytes |
| Absolute indexed long | AND 3bytes, x |
| Stack relative | AND byte, s |
| Stack relative indirect indexed | AND (byte, s), y |
| Absolute indirect | JMP (2bytes) |
| Absolute indirect long | JML [2bytes] |
| Absolute indexed indirect | JMP/JSR (2bytes,x) |
| Implied accumulator | INC |
| Block move | MVN/MVP byte, byte |

## External links

* A 6502 Programmer's Introduction to the 65816
* Programming the 65816
* 65816 Primer