LDA STA LDX ... ; load / store A, X, Y
TAX TXA ... ; transfer A to X
PHA PLA ; push A / pull A
CMP ; compare with A
ADC SBC ; add/subtract with carry
CLC SEC ; clear / set carry
JMP ; jump
BEQ BNE BCC BCS ; branch ==, !=, carry set/clear
BMI BPL ... ; <0, >=0
JSR ; jump to subroutine
RTS ; return from subroutine
Addressing modes
a9 20 LDA #$20 ; A = 0x20
a5 70 LDA $70 ; A = readmem(0x70)
ad 34 12 LDA $1234 ; A = readmem(0x1234)
Addressing modes
bd 34 12 LDA $1234, X ; A = readmem(0x1234 + X)
b9 34 12 LDA $1234, Y ; A = readmem(0x1234 + Y)
b1 70 LDA ($70), Y ; t1 = readmem(0x70)
; t2 = readmem(0x71)
; addr = t1 | (t2<<8)
; A = readmem(addr + Y)
b5 70 LDA $70, X ; A = readmem(0x70 + X)
Emulating a 6502
Fetch next instruction
Decode it
Execute it
Emulation
var a = 0, x = 0, y = 0, pc = readword(0xfffe);
while (true) {
switch (readmem(pc++)) {
case 0xa9: // LDA #imm
a = readmem(pc++); break;
case 0xad: // LDA $addr
var addr = readword(pc); pc += 2;
a = readmem(addr);
break;
// and so on for all other instructions...
}
}
case 0x2e: // ROL abs
addr = readword(pc); pc += 2;
hardware.run(3);
val = readbyte(addr);
hardware.run(1);
writebyte(addr, val); // unmodified value
hardware.run(1);
val = (val << 1) | (carry ? 1);
carry = val & 0x100;
writebyte(addr, val & 0xff); // modified value
hardware.run(1);
break;