6502


Operators

$

Prefix denoting hexadecimal

#

Prefix denoting immediate data value, when absent implies an address

%

Prefix denoting binary

Instructions

ADC

Add with carry; Adds a value to the accumulator and accounting for carry

AND

Applies logical AND operator

ASL

Arithmetic shift left; Shifts all bits in accumulator left one position, with the bit being pushed out falling into the carry

BCC

Branch on carry clear; Jumps to a subroutine if the carry flag is cleared

BCS

Branch on carry set; Jumps to a subroutine if the carry flag is set

BEQ

Branch on equal; Jumps to a subroutine if the zero flag is set

BIT

Performs a logical AND on an address and accumulator and sets the Z flag if the result is 0

BMI

Branch on minus; Jumps to a subroutine if the negative flag is set

BNE

Branch on not equal; Jumps to a subroutine if the zero flag is cleared

BPL

Branch on plus; Jumps to a subroutine if the negative bit is cleared

BRK

Break; Executes a non-maskable interrupt (NMI)

BVC

Branch on overflow clear; Jumps to a subroutine if the overflow bit is cleared

BVS

Branch on overflow set; Jumps to a subroutine if the overflow bit is set

CLC

Clear carry; clears the carry bit

CLD

Clear decimal; clears the decimal bit

CLI

Clear interrupt; clears the interrupt bit

CLV

Clear overflow; Clears the overflow bit

CMP

Compare; Minuses a value with the accumulator, but doesn't store the result. Mainly used with branch instructions.

CMX

Compare X; Minuses a value with the X register, but doesn't store the result. Mainly used with branch instructions.

CMY

Compare Y; Minuses a value with the Y register, but doesn't store the result. Mainly used with branch instructions.

DEC

Decrement; Decreases value in an address by 1

DEX

Decrement X; Decreases value in the X register by 1

DEY

Decrement Y; Decreases value in the Y register by 1

EOR

Exclusive OR; Applies XOR logic with a value and accumulator

INC

Increment; Increment value in an address by 1

INX

Increment X; Increases value in the X register by 1

INY

Increment Y; Increases value in the Y register by 1

JMP

Jump; Jumps to an address to execute instructions from

JSR

Jump to subroutine

LDA

Load A

LDX

Load X

LDY

Load Y

LSR

Logical shift right

NOP

No operation

ORA

OR A

PHA

Push A

PHP

Push processor status

PLA

Pull A

PLP

Pull processor status

ROL

Rotate left

ROR

Rotate right

RTI

Return from interrupt

RTS

Return from subroutine

SBC

Subtract with carry

SEC

Set clear

SED

Set decimal

SEI

Set interrupt

STA

Store A

STX

Store X

STY

Store Y

TAX

Transfer A to X

TAY

Transfer A to Y

TSX

Transfer stack to X

TXA

Transfer X to A

TXS

Transfer X to stack

TXS

Transfer Y to A

Addressing modes

Implicit

Parsing no address since the data is implied from context

TAX

Immediate

Parsing data value

LDA #$40 #Loads 0x40 (64) into A

Zero page

Addresses an address within the first 256 address spaces

LDA $2A #Loads value at address 0x2A (42) into A

Absolute

Addresses an address

LDA $1996 #Loads value at address 0x1996 (6502) into A

Indirect

Addresses a dereferenced pointer stored in two addresses in little endian format. The lower byte of the pointer is the input in this addressing mode.

LDA #$40 STA $1966 #Loads 64 into address 6502 LDA #$00 STA $1967 #Loads 0 into address 6503 LDA ($1966) #Loads value of $0040 into A

X

Addresses register X

ADC #$64,X

Y

Addresses register Y

ADC #$64,Y

X indirect

Indirectly addresses X addresses (where X is the value in register X) above the input address

LDA #$40 STA $1966 #Loads 64 into address 6502 LDA #$00 STA $1967 #Loads 0 into address 6503 STX #$40 LDA ($1926,X) #Loads value of $0040 into A

Indirect Y

Indirectly addresses Y addresses (where X is the value in register X) above the dereferenced address

LDA #$40 STA $1966 #Loads 64 into address 6502 LDA #$00 STA $1967 #Loads 0 into address 6503 STY #$40 LDA ($1966),Y #Loads value of $0080 into A

Registers

Accumulator

Denoted as A, 8 bit register that is used for input and output of Arithmetic Logic Unit (ALU; the CPU component that performs operands)

X

8 bit register that is used for indexing operations

Y

8 bit register that is used for indexing operations

Status register

Denoted as SR, 8 bit register that describe the status of the program

Program counter

Denoted as PC, 16 bit little endian register that represents the current operand being executed

Stack pointer

Denoted as SP, 8 bit register that points to the top of the stack

Status bits

Carry Flag

Toggled when: ADC overclocks, no borrow with SBC or CMP, or manually set or cleared with SEC or CLC respectively. It also takes the form of the bit shifted out in a ASL, LSR, ROL or ROR.

Zero Flag

Set when the result of an instruction is zero

Interrupt Disable Flag

When set, interrupts other than the NMI are prohibited.

Decimal

On some machines, initiates binary-coded decimal representation for easier decimal representation.

B Flag

Unknown

Overflow Flag

Is set with ADC and SBC when a value is added or subtracted in a way that changes its sign (like adding #$7F and #$01, or #$FF and #$01).

Negative Flag

Represents the seventh bit of the variable in context (as the seventh bit states whether a signed bit is negative).

Interrupt

A hardware event which switches the program flow to some specific block of code that is to be executed whenever this event occurs

Vertical Blanking Interval

Aslo known as a Vblank, the time interval between the end of the final line of a frame and the beggining of the first line of the next frame, in other words, when no graphics are being produced. The notion of "vertical" in the term derives from the CRT electron gun vertically traversing from the bottom right of a monitor to the top left to start the new frame.

Non-Maskable Interrupt (NMI)

An interrupt that cannot be disabled (masked in this sense meand disabled) due to its crucial functionality. In the context of the Famicom, there is an NMI for Vblank intervals since this is the only safe time to write to the PPU.

Interrupt Request (IRQ)

A maskable interrupt that can be called by setting a CPU flag. In the Famicom context, this is used for bank switching

Object Attribute Memory (OAM)

  1. Y coordinate
  2. Tile
  3. Attributes
  4. X coordinate

Direct Memory Access

Act of parsing a block of memory to the registers of some processor

Palette

Collection of 4 colours that can be assigned to a sprite or background tiles. Every 16x16 pixel space is bound to one palette

Tile

8x8 matrix of pixels

Nametable

30x32 matrix of tiles in RAM to represent background

Decimal handling

Mapper

Hardware configuration used for a ROM with specific mappings for different size PRG and CHR ROMs and external modules such as Audio chips and WRAM