Atari 2600 instructions

TranslationShow original
An overview of all 56 official MOS 6507 instructions for retro developers.
TranslationShow original

The MOS 6507 instruction set (Atari 2600)

The Atari 2600 uses the MOS 6507, a simplified version of the MOS 6502 with a narrower address bus and no external IRQ or NMI interrupt inputs. Its instruction set is unchanged, however: it has 56 official instructions. In assembly language, these are written as three-letter mnemonics such as LDA or JMP.

For the story of how this code was written, tested and eventually put into a game cartridge, see From scratch to cartridge.

Moving data: loads, stores and transfers

These instructions move data between memory and the processor’s registers. A is the accumulator; X and Y are the index registers.
  • LDA – Loads a value into the accumulator (Load Accumulator)
  • LDX – Loads a value into the X register (Load X Register)
  • LDY – Loads a value into the Y register (Load Y Register)
  • STA – Stores the accumulator in memory (Store Accumulator)
  • STX – Stores the X register in memory (Store X Register)
  • STY – Stores the Y register in memory (Store Y Register)
  • TAX – Copies A to X (Transfer A to X)
  • TAY – Copies A to Y (Transfer A to Y)
  • TSX – Copies the stack pointer to X (Transfer Stack Pointer to X)
  • TXA – Copies X to A (Transfer X to A)
  • TXS – Copies X to the stack pointer (Transfer X to Stack Pointer)
  • TYA – Copies Y to A (Transfer Y to A)

Working with the stack

The stack is an area of memory reserved for temporarily holding data and return addresses. The last value pushed onto it is the first one pulled off.
  • PHA – Pushes the accumulator onto the stack (Push Accumulator)
  • PHP – Pushes the processor status onto the stack (Push Processor Status)
  • PLA – Pulls a value from the stack into the accumulator (Pull Accumulator)
  • PLP – Restores the processor status from the stack (Pull Processor Status)

Arithmetic operations

The processor supports addition and subtraction directly. Multiplication and division have to be built from sequences of instructions.
  • ADC – Adds a value and the current carry flag C to the accumulator (Add with Carry)
  • SBC – Subtracts a value from the accumulator, including the borrow; C = 0 represents a borrow (Subtract with Carry)
  • INC – Increments a value in memory by 1 (Increment Memory)
  • INX – Increments X by 1 (Increment X)
  • INY – Increments Y by 1 (Increment Y)
  • DEC – Decrements a value in memory by 1 (Decrement Memory)
  • DEX – Decrements X by 1 (Decrement X)
  • DEY – Decrements Y by 1 (Decrement Y)

Logical operations and bit shifts

These instructions manipulate individual bits, an important part of tasks such as drawing graphics on the Atari.
  • AND – Performs a bitwise AND between a value and the accumulator, storing the result in A (Logical AND)
  • ORA – Performs a bitwise inclusive OR between a value and the accumulator, storing the result in A (Logical Inclusive OR)
  • EOR – Performs a bitwise exclusive OR between a value and the accumulator, storing the result in A (Exclusive OR)
  • ASL – Shifts bits left, inserts a zero into the lowest bit and moves the highest bit into C (Arithmetic Shift Left)
  • LSR – Shifts bits right, inserts a zero into the highest bit and moves the lowest bit into C (Logical Shift Right)
  • ROL – Rotates bits left through the carry flag C (Rotate Left)
  • ROR – Rotates bits right through the carry flag C (Rotate Right)
  • BIT – Tests bits in a memory value and updates Z, N and V without changing A (Bit Test)

Comparisons and conditional branches

The program makes decisions by testing flags set by earlier operations. Comparisons update those flags without changing the register being compared.
  • CMP – Compares the accumulator with a value (Compare Accumulator)
  • CPX – Compares X with a value (Compare X Register)
  • CPY – Compares Y with a value (Compare Y Register)
  • BCC – Branches if carry is clear (C = 0) (Branch if Carry Clear)
  • BCS – Branches if carry is set (C = 1) (Branch if Carry Set)
  • BEQ – Branches if the zero flag is set (Z = 1); after a comparison, this means equal (Branch if Equal)
  • BNE – Branches if the zero flag is clear (Z = 0); after a comparison, this means not equal (Branch if Not Equal)
  • BMI – Branches if the negative flag is set (N = 1) (Branch if Minus)
  • BPL – Branches if the negative flag is clear (N = 0), including after a zero result (Branch if Plus)
  • BVC – Branches if the overflow flag is clear (V = 0) (Branch if Overflow Clear)
  • BVS – Branches if the overflow flag is set (V = 1) (Branch if Overflow Set)

Jumps and subroutines

Instructions for moving to another part of the code and returning from a subroutine or interrupt handler.
  • JMP – Jumps unconditionally to an address (Jump)
  • JSR – Calls a subroutine and pushes the return address onto the stack (Jump to Subroutine)
  • RTS – Returns from a subroutine (Return from Subroutine)
  • RTI – Returns from an interrupt handler and restores the status flags (Return from Interrupt)

Status flags and system instructions

These instructions control the processor’s status flags or its execution.
  • CLC – Clears the carry flag C (Clear Carry)
  • CLD – Turns decimal mode off (D = 0) (Clear Decimal Mode)
  • CLI – Clears the interrupt-disable flag I (Clear Interrupt Disable)
  • CLV – Clears the overflow flag V (Clear Overflow)
  • SEC – Sets the carry flag C (Set Carry)
  • SED – Turns decimal mode on (D = 1) (Set Decimal Mode)
  • SEI – Sets the interrupt-disable flag I (Set Interrupt Disable)
  • BRK – Triggers a software interrupt (Force Break)
  • NOP – Performs no data operation; the official NOP instruction takes 2 CPU cycles (No Operation)
An Atari 2600 detail: the MOS 6507 has no external IRQ or NMI inputs. CLI and SEI still change the I flag, but they cannot enable or disable external interrupts that the chip cannot receive. The software interrupt instruction BRK and the return instruction RTI nevertheless remain part of its instruction set.

Why an instruction that does “nothing” can be useful

The Atari 2600 has no framebuffer holding a complete image. Its TIA chip generates the graphics on the fly, and the program must update the chip’s registers at the right moments as each line is drawn on the CRT television.

The NOP instruction helps with precise timing by taking up two CPU cycles. Carefully chosen BIT or LDA instructions can fill time too, but they only do “nothing” in terms of the program’s intended result: they change flags, and LDA also changes the accumulator. The programmer must account for those effects. It is all about arranging the cycles so that a write to a TIA register happens at exactly the right moment.

For more on the TIA, limited memory and flickering objects, continue with The extreme challenges of developing for the Atari 2600.

Documentation and further reading

Share
Share Facebook WhatsApp X Telegram Reddit E-mail

Sign in to add a post.

Herbert Sobel

TranslationShow original
I can't wait until I finally have time to sit down and try programming something myself. These old machines offer so many challenges... Thanks for the informative article 👍 This really interests me.