6502
Introduction
The MOS Technology 6502 was first released in 1975 and was the dominant CPU in home computers for a decade, appearing in the Atari 2600, Apple II, NES, Commodore PET/VIC20/C64. It is an 8-bit processor with 16 bit address space. It included 3 registers (Accumulator and two general purpose X, Y registers) and a status register.
The instruction sets allowed for loading, storing, and transferring data between the A, X, and Y registers, arithmetic, shifting and rotating, logic AND and OR, comparison, branching, jumping and subroutines, interrupts, and stack operations.
Memory to the 6502 spans its entire 16-bit address space and allows for up to 64 KB of memory to be addressed. Though, this space may also be used for ROM, screen memory, or hardware peripherals. The 6502 organizes memory in the following mapping.
| Address | Purpose |
|---|---|
| 0000 - 00FF | First page is known as the Zero page and used for variables.
Instructions can reference a single-byte address which references the zero page. |
| 0100 - 01FF | Second page reserved for the stack. Initializes at 0x1FF. |
Instruction Set
| Description | Instructions | Examples and notes |
|---|---|---|
| Load from memory | LDA, LDX, LDY | LDA #$22 to load 0x22 to accumulator
|
| Store to memory | STA, STX, STY | |
| Transfer from Accumulator | TAX, TAY | |
| Transfer to Accumulator | TXA, TYA | Note: No way to directly transfer between registers X and Y |
| Arithmetic on accumulator | ADC add
SBC - subtract |
ADC #$02 add 2 to accumulator
|
| Increment and Decrement | INC, INX, INY
DEC, DEX, DEY |
Note: May set carry, overflow, or zero flags. |
| Rotate | ROL, ROR | Rotate left or right |
| Shifting | ASL, ASR
LSL, LSR |
Arithmetic shift will set carry bit, Logical uses carry bit (?)
|
| Set Flags | SEC, SED, SEI | Sets carry, decimal, interrupt |
| Clear Flags | CLC, CLV, CLI, CLD | Clear carry, overflow, interrupt, decimal |
| Jumping | JMP | |
| Branching | BCC - carry clear
BCS - carry set BNE - zero clear BEQ - zero set BPL - negative clear BMI - negative set BVC - overflow clear BVS - overflow set |
|
| Stack | PUSH, PULL
PHP, PLP - push/pull flags PHA, PLA - push/pull accumulator |
|
| Subroutine | JSR - jump to subroutine
RTS - return |
JSR pushes the current address to the stack before jumping. RTS pops and jumps back to this location and adds one. |
Flags
- Carry set if add/inc >255 or sub/dec <0.
- Overflow if numbers >255
- Zero: operation results in 0
- Negative: bit 7 is set
- Break
- Interrupt
- Decimal
See Also
- A good tutorial and JS emulator - http://skilldrick.github.io/easy6502