Ø_________________ _ _ _______ _______ ________ _______ Ø ÆÆÆ ÆÆÆ ÆÆÆ ÆÆÆ ÆÆ_ ÆÆÆ ÆÆÆ ÆÆÆ ___ ÆÆÆ ÆÆÆ ÆÆÆ Est. 1984 Æ¥Æ Æ±Æ Æ±Æ ÷ Æ±Æ Æ¥Æ ÷ Æ±Æ Æ¥Æ ÷ Æ¥Æ Æ¥Æ ÷ Æ±Æ Æ¥Æ ÷ Æ±Æ Æ¥Æ Æ÷Æ Æ÷Æ ÷ Æ÷Æ Æ±Æ ÷ Æ÷Æ Æ±Æ ÷ Æ±Æ Æ±Æ ÷ Æ÷Æ Æ±Æ ÷ Æ÷Æ Æ±Æ ™™™ Æ Æ ÷ Æ Æ Æ÷Æ ÷ Æ Æ Æ÷Æ ÷ Æ÷Æ Æ÷Æ ÷ Æ Æ Æ÷Æ ÷ ™™™ Æ±Æ Æ Æ Æ Æ Æ Æ___Æ™™ Æ Æ ^ Æ Æ Æ Æ___ÆÆ™ Æ Æ Æ÷Æ ™ÆÆÆ™ÆÆÆ™ Æ Æ™™™Æ__ Æ Æ v Æ Æ Æ Æ Æ Æ Æ Æ ™™ÆÆÆ Æ÷Æ Æ Æ Æ÷Æ ÷ Æ Æ Æ÷Æ ÷ Æ÷Æ Æ÷Æ Æ Æ Æ÷Æ ÷ Æ Æ Æ÷Æ Æ÷Æ Æ±Æ ÷ Æ÷Æ Æ±Æ ÷ Æ±Æ Æ±Æ Æ÷Æ Æ±Æ ÷ Æ÷Æ Æ Æ Æ±Æ Æ¥Æ ÷ Æ±Æ Æ¥Æ ÷ Æ¥Æ Æ¥Æ Æ±Æ Æ¥Æ ÷ Æ±Æ Æ1Æ ÆÆÆ ÆÆÆ ÆÆÆ ÆÆÆ ÆÆÆ ÆÆÆ ÆÆÆ ÆÆ™ ÆÆÆ Æ3Æ ™™™™™ ™™™™™™™ ™™™™™™™ ™™™ ™™™ ™™™™™™™ Æ5Æ _______ _ _ _______ _________ _______ _______ _______ Æ8Æ ÆÆ_ ÆÆÆ ÆÆÆ ÆÆÆ ÆÆ_ ÆÆÆ ÆÆ™ÆÆÆ™ÆÆ ÆÆ_ ÆÆÆÆÆ ÆÆÆ Æ ÆÆÆ ÆÆ_ ÆÆÆ Æ Æ Æ¥Æ ÷ Æ±Æ Æ±Æ ÷ Æ±Æ Æ¥Æ ÷ Æ±Æ Æ¥Æ Æ¥Æ Æ¥Æ Æ Æ¥Æ Æ¥Æ ÷ Æ±Æ Æ÷Æ Æ±Æ ÷ Æ÷Æ Æ÷Æ ÷ Æ÷Æ Æ±Æ ÷ Æ÷Æ Æ±Æ Æ±Æ Æ±Æ Æ Æ±Æ Æ±Æ ÷ Æ÷Æ Æ÷Æ Æ÷Æ ™™™ Æ Æ ÷ Æ Æ Æ÷Æ ™™™ Æ÷Æ Æ÷Æ Æ÷Æ Æ Æ÷Æ Æ÷Æ ™™™ Æ÷Æ ™ÆÆ____ Æ Æ Æ Æ ™ÆÆ____ Æ Æ Æ Æ__Æ™™ Æ Æ ¥ Æ Æ ™ÆÆ____ Æ±Æ ™™™™ÆÆ_ ™ÆÆÆ™ÆÆÆ™ ™™™™ÆÆ_ Æ Æ Æ Æ™™Æ__ Æ Æ ± Æ Æ ™™™™ÆÆ_ Æ±Æ ÆÆÆ Æ Æ Æ Æ ÆÆÆ Æ Æ Æ÷Æ Æ÷Æ Æ÷Æ ÷ Æ÷Æ ÆÆÆ Æ Æ Æ¥Æ _ Æ÷Æ ÷ Æ÷Æ Æ÷Æ Æ÷Æ ÷ Æ÷Æ Æ±Æ Æ±Æ Æ±Æ Æ±Æ Æ÷Æ ÷ Æ÷Æ Æ¥Æ Æ Æ±Æ ÷ Æ±Æ Æ±Æ Æ±Æ ÷ Æ±Æ Æ¥Æ Æ¥Æ Æ¥Æ Æ¥Æ Æ±Æ ÷ Æ±Æ ÆÆÆ Æ ÆÆÆ ™ÆÆ ÆÆÆ ÆÆÆ ™ÆÆ ÆÆÆ ÆÆ™ ÆÆÆÆÆ ÆÆÆ ÆÆÆ ÆÆÆ ™ÆÆ Ø ™™™™™™™™ ™™™™™™™ ™™™™™ ™™™™™™™ ™™™ ™™™™™™™ ™™™ ™™™ ™™™™™™™ Ø | |||
Çybôrg |
|
Sÿstem$ | |
Authored by: Bluechip |
Instruction set Lookup Table |
|
|
|||||||||||||||
The opcode for any instruction may be composed by reading the row and column it is in. | ||||||||||||||||
By example: | ||||||||||||||||
| ||||||||||||||||
Some instructions (or more specifically Addressing Modes) require either one or two bytes of data as a parameter. | ||||||||||||||||
Under these circumstances, this data immediately follows the instruction itself. | ||||||||||||||||
By Example: | ||||||||||||||||
|
-0 | -1 | -2 | -3 | -4 | -5 | -6 | -7 | -8 | -9 | -A | -B | -C | -D | -E | -F | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
0- |
|
|
|
|
|
|
|
|
|
0- | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
1- |
|
|
|
|
|
|
|
|
1- | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
2- |
|
|
|
|
|
|
|
|
|
|
|
2- | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
3- |
|
|
|
|
|
|
|
|
3- | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
4- |
|
|
|
|
|
|
|
|
|
|
4- | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
5- |
|
|
|
|
|
|
|
|
5- | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
6- |
|
|
|
|
|
|
|
|
|
|
6- | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
7- |
|
|
|
|
|
|
|
|
7- | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
8- |
|
|
|
|
|
|
|
|
|
8- | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
9- |
|
|
|
|
|
|
|
|
|
9- | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
A- |
|
|
|
|
|
|
|
|
|
|
|
|
A- | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
B- |
|
|
|
|
|
|
|
|
|
|
|
B- | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
C- |
|
|
|
|
|
|
|
|
|
|
|
C- | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
D- |
|
|
|
|
|
|
|
|
D- | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
E- |
|
|
|
|
|
|
|
|
|
|
|
E- | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
F- |
|
|
|
|
|
|
|
|
F- | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
-0 | -1 | -2 | -3 | -4 | -5 | -6 | -7 | -8 | -9 | -A | -B | -C | -D | -E | -F |
Detailed Instruction Reference |
ADC | Address Mode Syntax Opcode I-Len T-Cnt |
|||||||||||||
Add Memory to A with Carry | ||||||||||||||
Flags Affected: |
|
|||||||||||||
Logic: t = A + M + P.C P.V = (A.7!=t.7) ? 1:0 P.N = A.7 P.Z = (t==0) ? 1:0 IF (P.D) t = bcd(A) + bcd(M) + P.C P.C = (t>99) ? 1:0 ELSE P.C = (t>255) ? 1:0 A = t & 0xFF | ||||||||||||||
|
AND | Address Mode Syntax Opcode I-Len T-Cnt |
||||||||||||||||||||||||
Bitwise-AND A with Memory | |||||||||||||||||||||||||
Flags Affected: |
|
||||||||||||||||||||||||
Logic: A = A & M P.N = A.7 P.Z = (A==0) ? 1:0 | |||||||||||||||||||||||||
|
ASL | Address Mode Syntax Opcode I-Len T-Cnt |
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Arithmetic Shift Left | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Flags Affected: |
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Logic: P.C = B.7 B = (B << 1) & $FE P.N = B.7 P.Z = (B==0) ? 1:0 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
BCC | Address Mode Syntax Opcode I-Len T-Cnt
|
||||||||
Branch iff P.C is CLEAR | |||||||||
|
|||||||||
Logic: if (P.C == 0) GOTO (PC+M) | |||||||||
|
BCS | Address Mode Syntax Opcode I-Len T-Cnt
|
||||||||
Branch iff P.C is SET | |||||||||
|
|||||||||
Logic: if (P.C == 1) GOTO (PC+M) | |||||||||
|
BEQ | Address Mode Syntax Opcode I-Len T-Cnt
|
||||||||
Branch iff P.Z is SET | |||||||||
|
|||||||||
Logic: if (P.Z == 1) GOTO (PC+M) | |||||||||
|
BIT | Address Mode Syntax Opcode I-Len T-Cnt |
|||||||||||||||||||||||
Test bits in A with M | ||||||||||||||||||||||||
Flags Affected: |
|
|||||||||||||||||||||||
Logic: t = A & M P.N = t.7 P.V = t.6 P.Z = (t==0) ? 1:0 | ||||||||||||||||||||||||
|
BMI | Address Mode Syntax Opcode I-Len T-Cnt
|
||||||||
Branch iff P.N is SET | |||||||||
|
|||||||||
Logic: if (P.N == 1) GOTO (PC+M) | |||||||||
|
BNE | Address Mode Syntax Opcode I-Len T-Cnt
|
||||||||
Branch iff P.Z is CLEAR | |||||||||
|
|||||||||
Logic: if (P.Z == 0) GOTO (PC+M) | |||||||||
|
BPL | Address Mode Syntax Opcode I-Len T-Cnt
|
||||||||
Branch iff P.N is CLEAR | |||||||||
|
|||||||||
Logic: if (P.N == 0) GOTO (PC+M) | |||||||||
|
BRK | Address Mode Syntax Opcode I-Len T-Cnt |
|||||||||||||
Simulate Interrupt ReQuest (IRQ) | ||||||||||||||
|
||||||||||||||
Logic: PC = PC + 1 bPoke(SP,PC.h) SP = SP - 1 bPoke(SP,PC.l) SP = SP - 1 bPoke(SP, (P|$10) ) SP = SP - 1 l = bPeek($FFFE) h = bPeek($FFFF)<<8 PC = h|l | ||||||||||||||
|
BVC | Address Mode Syntax Opcode I-Len T-Cnt
|
||||||||
Branch iff P.V is CLEAR | |||||||||
|
|||||||||
Logic: if (P.V == 0) GOTO (PC+M) | |||||||||
|
BVS | Address Mode Syntax Opcode I-Len T-Cnt
|
||||||||
Branch iff P.V is SET | |||||||||
|
|||||||||
Logic: if (P.V == 1) GOTO (PC+M) | |||||||||
|
CLC | Address Mode Syntax Opcode I-Len T-Cnt |
||||||||
Clear Carry Flag (P.C) | |||||||||
Flags Affected: |
|
||||||||
Logic: P.C = 0 | |||||||||
|
CLD | Address Mode Syntax Opcode I-Len T-Cnt |
|||||||||
Clear Decimal Flag (P.D) | ||||||||||
Flags Affected: |
|
|||||||||
Logic: P.D = 0 | ||||||||||
|
CLI | Address Mode Syntax Opcode I-Len T-Cnt |
|||||||||
Clear Interrupt (disable) Flag (P.I) | ||||||||||
Flags Affected: |
|
|||||||||
Logic: P.I = 0 | ||||||||||
|
CLV | Address Mode Syntax Opcode I-Len T-Cnt |
|||||||||
Clear oVerflow Flag (P.V) | ||||||||||
Flags Affected: |
|
|||||||||
Logic: P.V = 0 | ||||||||||
|
CMP | Address Mode Syntax Opcode I-Len T-Cnt |
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Compare A with Memory | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Flags Affected: |
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Logic: t = A - M P.N = t.7 P.C = (A>=M) ? 1:0 P.Z = (t==0) ? 1:0 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
CPX | Address Mode Syntax Opcode I-Len T-Cnt |
||||||||
Compare X with Memory | |||||||||
Flags Affected: |
|
||||||||
Logic: t = X - M P.N = t.7 P.C = (X>=M) ? 1:0 P.Z = (t==0) ? 1:0 | |||||||||
|
CPY | Address Mode Syntax Opcode I-Len T-Cnt |
||||||||
Compare Y with Memory | |||||||||
Flags Affected: |
|
||||||||
Logic: t = Y - M P.N = t.7 P.C = (Y>=M) ? 1:0 P.Z = (t==0) ? 1:0 | |||||||||
|
DEC | Address Mode Syntax Opcode I-Len T-Cnt |
||||||||
Decrement Memory by one | |||||||||
Flags Affected: |
|
||||||||
Logic: M = (M - 1) & $FF P.N = M.7 P.Z = (M==0) ? 1:0 | |||||||||
|
DEX | Address Mode Syntax Opcode I-Len T-Cnt |
||||||||
Decrement X by one | |||||||||
Flags Affected: |
|
||||||||
Logic: X = X - 1 P.Z = (X==0) ? 1:0 P.N = X.7 | |||||||||
|
DEY | Address Mode Syntax Opcode I-Len T-Cnt |
||||||||
Decrement Y by one | |||||||||
Flags Affected: |
|
||||||||
Logic: Y = Y - 1 P.Z = (Y==0) ? 1:0 P.N = Y.7 | |||||||||
|
EOR | Address Mode Syntax Opcode I-Len T-Cnt |
|||||||||||||||||||||||||||||
Bitwise-EXclusive-OR A with Memory | ||||||||||||||||||||||||||||||
Flags Affected: |
|
|||||||||||||||||||||||||||||
Logic: A = A ^ M P.N = A.7 P.Z = (A==0) ? 1:0 | ||||||||||||||||||||||||||||||
|
INC | Address Mode Syntax Opcode I-Len T-Cnt |
||||||||
Increment Memory by one | |||||||||
Flags Affected: |
|
||||||||
Logic: M = (M + 1) & $FF P.N = M.7 P.Z = (M==0) ? 1:0 | |||||||||
|
INX | Address Mode Syntax Opcode I-Len T-Cnt |
||||||||
Increment X by one | |||||||||
Flags Affected: |
|
||||||||
Logic: X = X + 1 P.Z = (X==0) ? 1:0 P.N = X.7 | |||||||||
|
INY | Address Mode Syntax Opcode I-Len T-Cnt |
||||||||
Increment Y by one | |||||||||
Flags Affected: |
|
||||||||
Logic: Y = Y + 1 P.Z = (Y==0) ? 1:0 P.N = Y.7 | |||||||||
|
JMP | Address Mode Syntax Opcode I-Len T-Cnt |
||||||||
GOTO Address | |||||||||
|
|||||||||
Logic: PC = M | |||||||||
|
JSR | Address Mode Syntax Opcode I-Len T-Cnt |
||||||||
Jump to SubRoutine | |||||||||
|
|||||||||
Logic: t = PC - 1 bPoke(SP,t.h) SP = SP - 1 bPoke(SP,t.l) SP = SP - 1 PC = $A5B6 | |||||||||
|
LDA | Address Mode Syntax Opcode I-Len T-Cnt |
||||||||
Load A with Memory | |||||||||
Flags Affected: |
|
||||||||
Logic: A = M P.N = A.7 P.Z = (A==0) ? 1:0 | |||||||||
|
LDX | Address Mode Syntax Opcode I-Len T-Cnt |
||||||||
Load X with Memory | |||||||||
Flags Affected: |
|
||||||||
Logic: X = M P.N = X.7 P.Z = (X==0) ? 1:0 | |||||||||
|
LDY | Address Mode Syntax Opcode I-Len T-Cnt |
||||||||
Load Y with Memory | |||||||||
Flags Affected: |
|
||||||||
Logic: Y = M P.N = Y.7 P.Z = (Y==0) ? 1:0 | |||||||||
|
LSR | Address Mode Syntax Opcode I-Len T-Cnt |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Logical Shift Right | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Flags Affected: |
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Logic: P.N = 0 P.C = B.0 B = (B >> 1) & $7F P.Z = (B==0) ? 1:0 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
NOP | Address Mode Syntax Opcode I-Len T-Cnt |
|||||||||
No OPeration | ||||||||||
|
||||||||||
Logic: ~none~ | ||||||||||
|
ORA | Address Mode Syntax Opcode I-Len T-Cnt |
||||||||||||||||||||||||
Bitwise-OR A with Memory | |||||||||||||||||||||||||
Flags Affected: |
|
||||||||||||||||||||||||
Logic: A = A | M P.N = A.7 P.Z = (A==0) ? 1:0 | |||||||||||||||||||||||||
|
PHA | Address Mode Syntax Opcode I-Len T-Cnt |
||||||||
PusH A onto Stack | |||||||||
|
|||||||||
Logic: bPoke(SP,A) SP = SP - 1 | |||||||||
|
PHP | Address Mode Syntax Opcode I-Len T-Cnt |
||||||||||||
PusH P onto Stack | |||||||||||||
|
|||||||||||||
Logic: bPoke(SP,P) SP = SP - 1 | |||||||||||||
|
PLA | Address Mode Syntax Opcode I-Len T-Cnt |
||||||||
PulL from Stack to A | |||||||||
Flags Affected: |
|
||||||||
Logic: SP = SP + 1 A = bPeek(SP) P.N = A.7 P.Z = (A==0) ? 1:0 | |||||||||
|
PLP | Address Mode Syntax Opcode I-Len T-Cnt |
|||||||||||||||||||
PulL from Stack to P | ||||||||||||||||||||
Flags Affected: |
|
|||||||||||||||||||
Logic: SP = SP + 1 P = bPeek(SP) | ||||||||||||||||||||
|
ROL | Address Mode Syntax Opcode I-Len T-Cnt |
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
ROtate Left | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Flags Affected: |
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Logic: t = B.7 B = (B << 1) & $FE B = B | P.C P.C = t P.Z = (B==0) ? 1:0 P.N = B.7 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
ROR | Address Mode Syntax Opcode I-Len T-Cnt |
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
ROtate Right | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Flags Affected: |
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Logic: t = B.0 B = (B >> 1) & $7F B = B | ((P.C) ? $80:$00) P.C = t P.Z = (B==0) ? 1:0 P.N = B.7 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
RTI | Address Mode Syntax Opcode I-Len T-Cnt |
|||||||||
ReTurn from Interrupt | ||||||||||
Flags Affected: |
|
|||||||||
Logic: SP = SP - 1 P = bPeek(SP) SP = SP - 1 l = bPeek(SP) SP = SP - 1 h = bPeek(SP)<<8 PC = h|l | ||||||||||
|
RTS | Address Mode Syntax Opcode I-Len T-Cnt |
||||||||
ReTurn from Subroutine | |||||||||
|
|||||||||
Logic: SP = SP + 1 l = bPeek(SP) SP = SP + 1 h = bPeek(SP)<<8 PC = (h|l) +1 | |||||||||
|
SBC | Address Mode Syntax Opcode I-Len T-Cnt |
|||||||||||
Subtract Memory from A with Borrow | ||||||||||||
Flags Affected: |
|
|||||||||||
Logic: IF (P.D) t = bcd(A) - bcd(M) - !P.C P.V = (t>99 OR t<0) ? 1:0 ELSE t = A - M - !P.C P.V = (t>127 OR t<-128) ? 1:0 P.C = (t>=0) ? 1:0 P.N = t.7 P.Z = (t==0) ? 1:0 A = t & 0xFF | ||||||||||||
|
SEC | Address Mode Syntax Opcode I-Len T-Cnt |
||||||||
Set Carry flag (P.C) | |||||||||
Flags Affected: |
|
||||||||
Logic: P.C = 1 | |||||||||
|
SED | Address Mode Syntax Opcode I-Len T-Cnt |
|||||||||
Set Binary Coded Decimal Flag (P.D) | ||||||||||
Flags Affected: |
|
|||||||||
Logic: P.D = 1 | ||||||||||
|
SEI | Address Mode Syntax Opcode I-Len T-Cnt |
|||||||||
Set Interrupt (disable) Flag (P.I) | ||||||||||
Flags Affected: |
|
|||||||||
Logic: P.I = 1 | ||||||||||
|
STA | Address Mode Syntax Opcode I-Len T-Cnt |
||||||||
Store A in Memory | |||||||||
|
|||||||||
Logic: M = A | |||||||||
|
STX | Address Mode Syntax Opcode I-Len T-Cnt |
||||||||
Store X in Memory | |||||||||
|
|||||||||
Logic: M = X | |||||||||
|
STY | Address Mode Syntax Opcode I-Len T-Cnt |
||||||||
Store Y in Memory | |||||||||
|
|||||||||
Logic: M = Y | |||||||||
|
TAX | Address Mode Syntax Opcode I-Len T-Cnt |
||||||||
Transfer A to X | |||||||||
Flags Affected: |
|
||||||||
Logic: X = A P.N = X.7 P.Z = (X==0) ? 1:0 | |||||||||
|
TAY | Address Mode Syntax Opcode I-Len T-Cnt |
||||||||
Transfer A to Y | |||||||||
Flags Affected: |
|
||||||||
Logic: Y = A P.N = Y.7 P.Z = (Y==0) ? 1:0 | |||||||||
|
TSX | Address Mode Syntax Opcode I-Len T-Cnt |
||||||||
Transfer Stack Pointer to X | |||||||||
Flags Affected: |
|
||||||||
Logic: X = SP P.N = X.7 P.Z = (X==0) ? 1:0 | |||||||||
|
TXA | Address Mode Syntax Opcode I-Len T-Cnt |
||||||||
Transfer X to A | |||||||||
Flags Affected: |
|
||||||||
Logic: A = X P.N = A.7 P.Z = (A==0) ? 1:0 | |||||||||
|
TXS | Address Mode Syntax Opcode I-Len T-Cnt |
||||||||
Transfer X to Stack Pointer | |||||||||
|
|||||||||
Logic: SP = X | |||||||||
|
TYA | Address Mode Syntax Opcode I-Len T-Cnt |
||||||||
Transfer Y to A | |||||||||
Flags Affected: |
|
||||||||
Logic: A = Y P.N = A.7 P.Z = (A==0) ? 1:0 | |||||||||
|
6502 Instruction set Quick Reference |
ADC AND ASL BCC BCS BEQ BIT BMI BNE BPL BRK BVC BVS CLC CLD CLI CLV CMP CPX CPY DEC DEX DEY EOR INC INX INY JMP JSR LDA LDX LDY LSR NOP ORA PHA PHP PLA PLP ROL ROR RTI RTS SBC SEC SED SEI STA STX STY TAX TAY TSX TXA TXS TYA |
Instruction set by Purpose |
ACCUMULATOR ARITHMETIC STACK Arithmetic Accumulator Setup Memory Storage Index X Implied Interaction Register Storage Index Y Direct Interaction Bitwise Logic Memory INDEX REGISTERS BITWISE LOGIC FLOW CONTROL Index X Shifts & Rotates Comparisons Arithmetic Comparisons Conditional Branches Memory Storage Processor Status Flags GOTO & GOSUB Register Storage Index Y MISCELLANEOUS Arithmetic Memory Storage Register Storage |
ACCUMULATOR Arithmetic ADC - Add Memory to A with Carry SBC - Subtract Memory from A with Borrow Memory Storage LDA - Load A with Memory STA - Store A in Memory PHA - PusH A onto stack (push ->) PLA - PulL A from stack (pull <-) Register Storage TXA - Transfer X to A TAX - Transfer A to X TYA - Transfer Y to A TAY - Transfer A to Y Bitwise Logic AND - Bitwise-and A with Memory EOR - Bitwise-exclusive-or A with Memory ORA - Bitwise-or A with Memory ASL - Arithmetic Shift Left LSR - Logical Shift Right ROL - ROtate Left ROR - ROtate Right INDEX REGISTERS Index X Arithmetic INX - Increment X by one DEX - Decrement X by one Memory Storage LDX - Load X with Memory STX - Store X in Memory Register Storage TAX - Transfer A to X TXA - Transfer X to A TSX - Transfer Stack Pointer to X TXS - Transfer X to Stack Pointer Index Y Arithmetic INY - Increment Y by one DEY - Decrement Y by one Memory Storage LDY - Load Y with Memory STY - Store Y in Memory Register Storage TAY - Transfer A to Y TYA - Transfer Y to A ARITHMETIC Accumulator ADC - Add Memory to A with Carry SBC - Subtract Memory from A with Borrow ASL - Arithmetic Shift Left LSR - Logical Shift Right Index X INX - Increment X by one DEX - Decrement X by one Index Y INY - Increment Y by one DEY - Decrement Y by one Memory DEC - Decrement Memory by one INC - Increment Memory by one ASL - Arithmetic Shift Left LSR - Logical Shift Right BITWISE LOGIC Shifts & Rotates ASL - Arithmetic Shift Left LSR - Logical Shift Right ROL - ROtate Left ROR - ROtate Right Comparisons BIT - Test bits in A with M Processor Status Word (Flags) (P.?) P.N : sigN Flag (a.k.a. "S" Sign Flag) There are no instructions to directly SET or CLEAR P.N P.V : oVerflow Flag There is no instruction to directly SET P.V CLV - Clear oVerflow flag (P.V) P.B : Break Flag BRK - Simulate IRQ (push ->) P.D : binary coded Decimal Flag SED - Set Decimal flag (P.D) CLD - Clear Decimal flag (P.D) P.I : Interrupt (disable) Flag SEI - Set Interrupt flag (P.I) CLI - Clear Interrupt flag (P.D) P.Z : Zero Flag There are no instructions to directly affect P.Z P.C : Carry Flag SEC - Set Carry flag (P.C) CLC - Clear Carry flag (P.C) STACK Setup TSX - Transfer Stack Pointer to X TXS - Transfer X to Stack Pointer Implied Interaction JSR - Jump to SubRoutine (push ->) RTS - ReTurn from Subroutine (pull <-) BRK - Simulate IRQ (push ->) RTI - ReTurn from Interrupt (pull <-) Direct Interaction PHA - PusH A onto stack (push ->) PHP - PusH P onto stack (push ->) PLA - PulL A from stack (pull <-) PLP - PulL P from stack (pull <-) FLOW CONTROL Comparisons BIT - Test bits in A with M CMP - Compare A with Memory CPX - Compare X with Memory CPY - Compare Y with Memory Conditional Branches The is no unconditional Branch instruction. BCC - Branch iff Carry flag (P.C) is CLEAR BCS - Branch iff Carry flag (P.C) is SET BEQ - Branch iff Carry flag (P.Z) is SET BMI - Branch iff sigN flag (P.N) is SET BNE - Branch iff Carry flag (P.Z) is CLEAR BPL - Branch iff sigN flag (P.N) is CLEAR BVC - Branch iff oVerflow flag (P.V) is CLEAR BVS - Branch iff oVerflow flag (P.V) is SET GOTO & GOSUB JMP - GOTO Address JSR - Jump to SubRoutine RTS - ReTurn from Subroutine BRK - Simulate IRQ (push ->) RTI - ReTurn from Interrupt (pull <-) MISCELLANEOUS NOP - No OPeration
Processor Status Word (Flags) |
bit 7 P.N Negative* bit 6 P.V oVerflow bit 5 - unused bit 4 P.B BRK was executed bit 3 P.D enable binary coded Decimal bit 2 P.I block IRQ Interrupts bit 1 P.Z Zero bit 0 P.C Carry |
Bit no.: 7 6 5 4 3 2 1 0 Flag ID: N V - B D I Z C | | | | | | | | | | | | | +-- Carry | | | | | +-- Zero | | | | +-- block IRQ Interrupts | | | +-- enable binary coded Decimal | | +-- BRK was executed | | | +-- oVerflow +-- Negative* | |
* The Negative flag is often referred to as P.S the "S"ign flag |
P.N : sigN Flag P.V : oVerflow Flag P.B : Break Flag P.D : Decimal Flag Binary Coded Decimal (BCD) P.I : Interrupt (disable) Flag P.Z : Zero Flag P.C : Carry Flag
sigN Flag (P.N) | ||||||||||||||||||||
# | Also known as P.S (Sign flag) in many documents. | |||||||||||||||||||
# | The following instructions can affect P.N: | |||||||||||||||||||
ADC, SBC, INC, INX, INY, DEC, DEX, DEY AND, EOR, ORA, BIT, CMP, CPX, CPY ASL, LSR, ROL, ROR LDA, LDX, LDY TAX, TAY, TSX, TXA, TYA PLA, PLP, RTI | ||||||||||||||||||||
# | The following instructions depend on P.N: | |||||||||||||||||||
BMI, BPL | ||||||||||||||||||||
# | There are NO instructions to directly SET or CLEAR P.N | |||||||||||||||||||
# |
|
oVerflow Flag (P.V) | ||||||||||||||||||||
# |
The following instructions can affect P.V: | |||||||||||||||||||
CLV, BIT ADC, SBC PLP, RTI | ||||||||||||||||||||
# |
The following instructions depend on P.V: | |||||||||||||||||||
BVC, BVS | ||||||||||||||||||||
# | There is NO instruction to directly SET P.V | |||||||||||||||||||
# | P.V can be CLEARed by the user by means of the CLV instruction | |||||||||||||||||||
# |
|
Break Flag (P.B) | |||||||||||
# |
P.B and the BRK instruction seem to me to be one very badly thought out bodge. If you do not plan to use the BRK instruction I would just ignore it! | ||||||||||
# | P.B is never actually set in the Flags register! | ||||||||||
# |
When a BRK instruction occurs the Flags are PUSHed onto the Stack
along with a return address* It is only this copy of the Flags (the one on the Stack) that has P.B set! | ||||||||||
* |
| ||||||||||
# | For further information, see Interrupts. | ||||||||||
Decimal Flag (P.D) | |
# |
The following instructions can affect P.D: |
CLD, SED PLP, RTI | |
# |
The following instructions depend on P.D: |
ADC, SBC | |
# | P.D is "unknown" at boot. Therefore the boot code should initialise this flag |
# | P.D can be SET by the user by means of the SED instruction |
# | P.D can be CLEARed by the user by means of the CLD instruction |
# | P.D dictates whether Addition (ADC) and Subtraction (SBC) operate in the classic Binary or the more obscure Binary Coded Decimal (BCD) mode. |
Binary Coded Decimal (BCD) |
|
BCD is whereby the upper and lower nibbles (4-bits) of a byte (8-bits) are treated as two digits in a decimal number; The upper nibble contains the number from the 'tens column'; and the lower nibble, the number from the 'units column' |
|
By Example:
10010011 (or 1001'0011) represents 93 (or 9'3) Thus restricting the range of values that can be held in a single byte to the positive integers 0 through 99. |
|
The upshot is that any routine "PrintHex" will effectively become "PrintDecimal" ...Other than that I personally have never worked out why! And as such I do not intend to cover it in any more detail here. My suggestion is perform a CLD during boot up and forget about it! |
Interrupt (disable) Flag (P.I) | |
# |
The following instructions can affect P.I: |
CLI, SEI BRK, RTI, PLP | |
# |
The following instructions depend on P.I: ~None~ |
# | P.I can be CLEARed by the user by means of the CLI instruction |
# | P.I can be SET by the user by means of the SEI instruction |
# | When P.I is SET, Interrupt ReQuest signals (IRQs) to the IRQ pin (classically pin-4) are IGNORED |
# | When P.I is CLEAR, signals to the IRQ pin are acknowledged. |
Full details can be found under Interrupts and Interrupt ReQuests (IRQ's) | |
# |
When the processor is switched on or reset, P.I is SET. Thus Interrupts are disabled on startup. The intention here is to give the boot code an opportunity to initialise all external hardware without Interruption from that hardware. |
Zero Flag (P.Z) | |
# |
The following instructions affect P.Z: |
ADC, SBC, INC, INX, INY, DEC, DEX, DEY AND, EOR, ORA, BIT, CMP, CPX, CPY ASL, LSR, ROL, ROR LDA, LDX, LDY PLA, PLP, RTI TAX, TAY, TSX, TXA, TYA | |
# |
The following instructions depend on P.Z: |
BEQ, BNE | |
# | There are no instructions to directly SET or CLEAR P.Z |
# | P.Z is SET when a zero value is placed in a register |
# | P.Z is CLEARed when a non-zero value is placed in a register |
Carry Flag (P.C) | |
# |
The following instructions affect P.C: |
CLC, SEC ADC, SBC CMP, CPX, CPY ASL, LSR, ROL, ROR PLP, RTI | |
# |
The following instructions depend on P.C: |
BCC, BCS ADC, SBC ROL, ROR | |
# | P.C is "unknown" at boot. Therefore the boot code should initialise this flag |
# | P.C can be SET by the user by means of the SEC instruction |
# | P.C can be CLEARed by the user by means of the CLC instruction |
# | P.C can be considered to be the 9th bit of an arithmetic operation. |
Addressing Modes |
# | This mode is whereby no address or value is specified by the programmer. Some of these instructions perform no memory access at all: | ||||
| |||||
# | Others require memory addresses, but these are either known or calculated by the CPU at run time. | ||||
| |||||
| |||||
# | Implied Addressing is available for the following instructions: | ||||
CLC, CLD, CLI, CLV, DEX, DEY, INX, INY, NOP, PHA, PHP, PLA, PLP, RTI, RTS, SEC, CLD, SEI, TAX, TAY, TSX, TXA, TXS, TYA |
# | There are a number of "atomic read/modify/write" instructions which can address EITHER Memory OR the Accumulator (A) | ||||
# | In this case one cannot 'infer' A (as above), it MUST be stated. | ||||
| |||||
# | Accumulator Addressing is available for the following instructions: | ||||
ASL, LSR, ROL, ROR |
# | A better name for this mode might be Immediate Value as no "addressing" actually takes place. | ||||
| |||||
# | Immediate Addressing is available for the following instructions: | ||||
ADC, AND, CMP, CPX, XPY, EOR, LDA, LDX, LDY, ORA, SBC |
# | This mode is used exclusively by the Branch instructions. For further information see Conditional Branches |
# | Immediate Addressing is available for the following instructions: |
BCC, BCS, BEQ, BMI, BNE, BPL, BVC, BCS |
# | Read a value from a 16-bit address | ||||
# | Remember without special external hardware for paging, the 6502 only has a maximum of 64K of address space available - so 16-bits is enough to address ANY byte of memory. | ||||
| |||||
# | Absolute Addressing is available for the following instructions: | ||||
ADC, AND, ASL, BIT, CMP, CPX, CPY, DEC, EOR, INC, JMP, JSR, LDA, LDX, LDY, LSR, ORA, ROL, ROR, SBC, STA, STX, STY |
# | Much like Absolute Addressing, but can only address the first 256 (0..255) bytes of memory. | ||||
# | The benefit is a saving of 1 T-State :) | ||||
| |||||
# | Zero-Page Addressing is available for the following instructions: | ||||
ADC, AND, ASL, BIT, CMP, CPX, CPY, DEC, EOR, INC, LDA, LDX, LDY, LSR, ORA, ROL, ROR, SBC, STA, STX, STY |
# | In Absolute Addressing the destination address is fixed by the programmer (or assembler) at assembly time. | ||||||||
# |
By using the hard-coded address as a base, and X or Y as an Index, a more dynamic addressing
system can be implemented. | ||||||||
# |
If the result of Base+Index is greater than $FFFF, wrapping will occur. | ||||||||
| |||||||||
# |
Absolute Indexed Addressing with X is available for the following instructions: ADC, AND, ASL, CMP, DEC, EOR, INC, LDA, LDY, LSR, ORA, ROL, ROR, SBC, STA | ||||||||
# |
Absolute Indexed Addressing with Y is available for the following instructions: ADC, AND, CMP, EOR, LDA, LDX, ORA, SBC, STA |
# | In Zero-Page Addressing the destination address is fixed by the programmer (or assembler) at assembly time. | ||||||||
# | By using the hard-coded address as a base, and X or Y as an Index, a more dynamic addressing system can be implemented. | ||||||||
# | With Zero-Page, only the first 256 (0..255) bytes of memory may be addressed. So if the result of Base+Index is greater than $FF, wrapping will occur. | ||||||||
# |
The benefit is a saving of 1 T-State :) | ||||||||
| |||||||||
# | Zero-Page Indexed Addressing with X is available for the following instructions: | ||||||||
ADC, AND, ASL, CMP, DEC, EOR, INC, LDA, LDY, LSR, ORA, ROL, ROR, SBC, STA, STY | |||||||||
# | Zero-Page Indexed Addressing with Y is available for the following instructions: | ||||||||
LDX, STX |
# | With this instruction, the 8-but address (location) supplied by the programmer is considered to be a Zero-Page address, that is, an address in the first 256 (0..255) bytes of memory. | ||||
# | The content of this Zero-Page address must contain the low 8-bits of a memory address | ||||
# | The following byte (the contents of address+1) must contain the upper 8-bits of a memory address | ||||
# | Once this memory address has been read from the Zero-Page location (specified by the programmer), this calculated memory address is then examined, and it's contents are returned. | ||||
# | Although tricky to explain, the concept is actually quite simple. The upshot is that you do not need to hard-code all address value, this system allows addresses to be calculated at run time. | ||||
| |||||
# | Indirect Addressing is available for the following instructions: | ||||
JMP |
# | This addressing mode is only available with X. | ||||
# | Much like Indirect Addressing, but the contents of the index register is added to the Zero-Page address (location) | ||||
# | If Base_Location+Index is greater than $FF, wrapping will occur. | ||||
| |||||
# | Indexed Indirect Addressing is available for the following instructions: | ||||
ADC, AND, CMP, EOR, LDA, ORA, SBC, STA |
# | This addressing mode is only available with Y. | ||||
# | Much like Indexed Addressing, but the contents of the index register is added to the Base_Location after it is read from Zero-Page memory. | ||||
# | If Base_Location+Index is greater than $FFFF, wrapping will occur. | ||||
| |||||
# | Indirect Indexed Addressing is available for the following instructions: | ||||
ADC, AND, CMP, EOR, LDA, ORA, SBC, STA |
6502 Memory Map |
0000-00FF - RAM for Zero-Page & Indirect-Memory Addressing 0100-01FF - RAM for Stack Space & Absolute Addressing 0200-3FFF - RAM for programmer use 4000-7FFF - Memory mapped I/O 8000-FFF9 - ROM for programmer useage FFFA - Vector address for NMI (low byte) FFFB - Vector address for NMI (high byte) FFFC - Vector address for RESET (low byte) FFFD - Vector address for RESET (high byte) FFFE - Vector address for IRQ & BRK (low byte) FFFF - Vector address for IRQ & BRK (high byte) |
The Stack |
# | The Stack Pointer is standardly abbreviated to "SP" or "S" depending on many factors, not least of all the author of the document you are reading. | ||||||
# | For the duration of this section of the document, the Processor Status Word (or "Flags") will be referred to as PSW | ||||||
# | The following instructions affect the Stack: BRK, JSR, PHA, PHP, PLA, PLP, RTI, RTS, TXS | ||||||
# | TXS is used by the programmer to initialise the Stack Pointer. An example of this can be found in the System Startup code. | ||||||
# | The Stack can only live in Page 1 of memory; That is, addresses in the range $01'00...$01'FF | ||||||
# | Therefore SP is only an 8-bit value | ||||||
# | This means that the Stack will wrap around within Page 1 if care is not taken. | ||||||
# | PHA PusHes a single byte onto the Stack; | ||||||
# | This byte will contain a copy of the value currently in register A | ||||||
# | PHP PusHes a single byte onto the Stack; | ||||||
# | This byte will contain a copy of the value currently in the PSW | ||||||
# | PLA PulLs (or "POPs") a single byte from the stack; | ||||||
# | This value retrieved will be stored in A | ||||||
# | PLP PulLs (or "POPs") a single byte from the stack; | ||||||
# | This value retrieved will be stored in the PSW | ||||||
# | The easiest way to set the Flags to a "known value" is: | ||||||
| |||||||
# | JSR and RTS are the 6502 equivalents of GOSUB and RETURN; | ||||||
# | A comprehensive explanation of their use of the Stack is under GOSUB and RETURN | ||||||
# | BRK simulates an interrupt, but behaves most perculiarly. | ||||||
# |
BRK places THREE bytes on the stack. | ||||||
| |||||||
# |
The effects and uses of BRK are also discussed at the following locations:
| ||||||
# |
RTI is used to terminate an Interrupt or to return from a BRK instruction.
RTI removes THREE bytes from the Stack
|
The Stack - What Is It? |
# | Imagine a cardboard box that can hold 8-bits of information. |
# | Now Imagine a STACK of these boxes. |
# | Next to this STACK, is a guard to stands watch over it. |
# | His main guard duty is to POINT to the space where the next box will go. |
# | Due to the nature of physics, this gap will always be just above the the top-most box! |
# | If anyone PUSHes another box onto the STACK, it will be added to the STACK at precisely where the guard is POINTing. After this has happened the guard will automatically move his finger and POINT at the new space. |
# | If anyone PULLs (or POPs) a box from the stack; that is, should anyone as the guard for the top box. He will move his finger to point at the last box, and then pass it to you. |
# | The result is that as he passes you the box, his finger is left POINTing at the newly formed space. |
# | This creates a "last-in, first-out" system - because you can only ever access the TOP of the stack. |
# |
...oh yeah, one more thing... This system was designed by an Australian; As we all know, the laws of physics invert when you are on the bottom of the globe. So the stack actually builds from the ceiling down - deal with it! |
# |
Realise that there is a limited amount of room an the STACK. If the STACK is full and you attempt to PUSH another box STACK a "Stack Overflow" condition will occur. |
# |
On many modern system architecture you will receive a warning when this occurs. This is NOT true of the 6502 STACK POINTer So it is the concern of the programmer to be careful that this does not occur. |
# | There are a couple of more subtle things that are for the advanced programmer. |
# | When a box is removed, a ghost of it remains. |
# | This ghost is a perfect copy of the last box which occupied that space. |
# | But be warned, a light breeze that passes that area of the warehouse will destroy the ghost, so do NOT depend on the presence of this ghost too heavily. |
# | In actual fact you do not retrieve the value on the TOP of the stack, but actually the value which is indicated by the STACK POINTER |
# | For a bribe, (the currency is T-states in this world), the guard will point anywhere you want him to, including (but not restricted to) the middle of the stack. |
# | Bear in mind that during this process, the stack will NOT be guarded. BE WARNED it may not be there when you get back! |
# | This is a very dodgy process and considered to be very bad coding etiquette, but a is also a damn fine way to confuse hackers and has found many other weird and wonderful uses in it's life. |
# | ...what this security guard will do for a small bribe is certainly perverted, but great fun to watch :) |
# | Question: What colour uniform was the guard wearing? |
Program Flow Control |
# | The "Fetch-Execute cycle" is what drives a processor |
# | The processor fetches an instruction and then executes it. |
# | The Program Counter is incremented AFTER the Fetch phase and BEFORE the Execute phase. |
# | Therefore... during Execution of an instruction, the Program Counter is pointing at the NEXT instruction to be executed; NOT this one! |
# | This is particularly important when considering the destination of a Branch instructions. |
# | And makes perfect sense when considered in relation to the JSR and RTS instructions. |
# |
The following Conditional Branch instructions are available: | ||||||||
# | It should be noted that there is NO unconditional branch! ...but one can be simlated by setting a Processor Status Word Flag to a known state and then Branching on that condition flag. | ||||||||
# | The operand to a Branch instruction is a single byte offset (8 bits, giving a range of -128 to 127) which specifies the new Program Counter (PC) address realtive to it's current position. Hence the name Relative Addressing | ||||||||
# | All branch commands are two bytes long - one byte for the Branch instruction; another for the relative offset. | ||||||||
| |||||||||
# | The advantage of Relative Addressing is that it geneates fully relocatable code; That is, the program will execute normally no matter where it is loaded into memory. | ||||||||
# | The down-side Relative Addressing is that compromises must be made if you wish to change PC by more than {-128 to +127}. |
Two solutions are available to long branches: | |||||||||||||||||||
1) | Use a "complimented branch and jump pair" | ||||||||||||||||||
# | Consider this example where "label" is 'out of range' | ||||||||||||||||||
|
|||||||||||||||||||
# | This code can be replaced with | ||||||||||||||||||
|
|||||||||||||||||||
# | The advantage of this method is that the memory overhead is only 3 bytes | ||||||||||||||||||
# | And although it may cause several passes for an assembler, the process can be automated simply and still produce readable code. | ||||||||||||||||||
# | The disadvantage is that the code will no longer be relocatable. | ||||||||||||||||||
2) | "skip" or "leap-frog" through the code. | ||||||||||||||||||
|
# | The following table shows how to assemble standard C comparisons to their 6502 equivalents. | ||||||||||||||||||||||||||||||||
# | It should be noted that two of the conditions require TWO Branch instructions to resolve, whereas the other four only require ONE. | ||||||||||||||||||||||||||||||||
| |||||||||||||||||||||||||||||||||
|
JMP - a.k.a GOTO | |||||||||||||||||||||||||||||||||
# | The simplest way to change the execution flow of the processor is with the JMP instruction. | ||||||||||||||||||||||||||||||||
# | This will immediately and unconditionally modify the Program Counter. And effectively behaves just like the classic GOTO command in most higher level languages. | ||||||||||||||||||||||||||||||||
# | JMP has TWO addressing modes, Absolute and Indirect. | ||||||||||||||||||||||||||||||||
| |||||||||||||||||||||||||||||||||
JSR & RTS - GOSUB & RETURN | |||||||||||||||||||||||||||||||||
# |
JSR is normally used to call SubRoutines; That is, small pieces of code which are used repetetively by the main code. A typical example might be "print". | ||||||||||||||||||||||||||||||||
# | JSR behaves exactly like JMP in Absolute Addressing Mode with one excpetion... | ||||||||||||||||||||||||||||||||
# | Before PC is changed, a copy of PC is placed on the Stack for safe keeping. | ||||||||||||||||||||||||||||||||
# | When the Subroutine is finished, it simply issues an RTS and the return address is retrieved from the Stack and placed back into PC. | ||||||||||||||||||||||||||||||||
# | The net result is that execution will then continue from exactly where it left off. | ||||||||||||||||||||||||||||||||
| |||||||||||||||||||||||||||||||||
| |||||||||||||||||||||||||||||||||
# | As can be seen from the Logic above, JSR actually stores PC-1 on the Stack | ||||||||||||||||||||||||||||||||
# |
| ||||||||||||||||||||||||||||||||
# | RTS is sensible enough to know this, and corrects the error before making the return. | ||||||||||||||||||||||||||||||||
# |
Although it is not normally necessary to know this. There are occasions when you may wish to Push an address on the Stack and then use the RTS instruction to initiate the jump. | ||||||||||||||||||||||||||||||||
# |
This is very unusual, but if you wish to use this method of Flow Control you must remember
to Push the address of the destination instruction MINUS ONE. | ||||||||||||||||||||||||||||||||
| |||||||||||||||||||||||||||||||||
# | If you plan to use this method, you should take time to read the section on The Stack. |
Interrupts and Interrupt ReQuests (IRQ's) |
What is an Interrupt? | ||||||
# |
An Interrupt is, at it's most primitive level, a signal that comes into the CPU from
one of it's pins. Specifically, the one labelled "/IRQ". | |||||
# | When this pin is "pulled low" (Eg. has 0V applied to it), a signal is sent to the CPU core. | |||||
# | The CPU halts the current program and instead runs a special routine called the Interrupt Service Routine. | |||||
How can I ENable Interrupts? | ||||||
# | When the 6502 starts. Interrupts are disabled. | |||||
# | The intention here is to give the boot code an opportunity to initialise all external hardware without interruption (from that hardware.) | |||||
# | Interrupts can be enabled by CLEARing the Interrupt (disable) Flag (P.I) with the CLI instruction. | |||||
How can I DISable Interrupts? | ||||||
# | Sometimes, when you are executing time critical code, you may wish to ensure that you are not interrupted. | |||||
# | The process of disabling Interrupts is known as "Masking" or "Disabling" or "Inhibiting" or "Ignoring". | |||||
# | That is, you don't stop the interrupt from occuring, you just ignore it! ...you "Mask" it. | |||||
# | To make the 6502 Mask interrupts you need to SET the Interrupt (disable) Flag (P.I) | |||||
# | This is done with the STI instruction. | |||||
What happens when an Interrupt occurs? | ||||||
# | First you must consider if Interrupts are disabled | |||||
# | If they are (disabled). We do nothing! | |||||
# | If Interrupts are enabled (I.e. Interrupt (disable) Flag (P.I) is CLEAR) | |||||
# | The following process occurs: | |||||
# | The current instruction is allowed to finish. | |||||
# | The current PC is Pushed onto the Stack. | |||||
(This is the address of the next instruction). | ||||||
# | A copy of the Processor Status Word (Flags) (P.?) is Pushed onto the Stack. | |||||
# | The Interrupt (disable) Flag (P.I) is SET | |||||
(This ensures that the interrupt does not get interrupted) | ||||||
# | A 16-bit address is read from $FFFE...$FFFF in memory and placed in Program Counter (PC). | |||||
# | You must supply a special piece of code called The Interrupt Service Routine which will start at this address (or "Vector") | |||||
# | An Example/Template Interrupt Service Routine is supplied below | |||||
What is a "Non-Maskable Interrupt"? | ||||||
# | An NMI is almost identical to an IRQ, but it: | |||||
# | Uses a different pin on the chip - Namely "/NMI" | |||||
# | Is triggered by a Level Change (Eg. from 0V-to-5V or vice versa) | |||||
# | Calls a different Routine - Specified by the 16-bit Vector at $FFFA...$FFFB | |||||
# | Cannot be Masked, Disabled, Inhibited or Ignored | |||||
How can I disable Non-Maskable Interrupts? | ||||||
# | You can't - They're NON-Maskable! | |||||
# | The Interrupt (disable) Flag (P.I) has NO effect on NON-Maskable Interrupts | |||||
# | The purpose of the NMI is to alert the CPU to a critical event. | |||||
# | You can, of course, design/modify your hardware such that no signals are ever sent to the /NMI pin. That'll stop it working! | |||||
# | You might instead write an NMI-Handler which does nothing except RTI. Not so much "ignoring it", more akin to saying "yeah, whatever" everytime it tries to interrupt you. | |||||
# | In very simple computers, NMI is often left unused. | |||||
# | If the NMI wants your attention - whatever it is, is more important that what you're currently doing. | |||||
# | If an NMI happens for a trivial task, lean over and slap your hardware engineer sqaure between the eyes and when he asks why you just did that, say "oh nothing important" ...he'll soon get the message! | |||||
# | My Girlfriend is a non-maskable-interrupt. The telephone isn't! | |||||
What happens when a Non-Maskable Interrupt occurs? | ||||||
# | Pretty much the same as a normal Interrupt except the Vector (address of Interrupt Routine) is read from $FFFA...$FFFB | |||||
# | The current instruction is allowed to finish. | |||||
# |
The current PC is Pushed onto the Stack. | |||||
(This is the address of the next instruction). | ||||||
# | A copy of the Processor Status Word (Flags) (P.?) is Pushed onto the Stack. | |||||
# |
The Interrupt (disable) Flag (P.I) is SET | |||||
(This ensures that the interrupt does not get interrupted) Also note that the Flag is SET after the Processor Status Word (Flags) (P.?) is Pushed onto the Stack | ||||||
# | A 16-bit address is read from $FFFA...$FFFB in memory and placed in Program Counter (PC). | |||||
When does an Interrupt or Non-Maskable Interrupt finish? | ||||||
# | Quite simply: When the CPU finds an RTI - ReTurn from Interrupt instruction. | |||||
# |
It is important to note that an Interrupt will Push "PC" on the Stack ...Unlike JSR which Pushes "PC-1" | |||||
# |
Therefore when the RTI instruction is executed it does NOT need to perform the error correction that is used by RTS | |||||
# |
This is only relevant if you are playing silly buggers with the Stack ...but worth noting, in case you do. | |||||
# |
RTI does not actually reset the Interrupt (disable) Flag (P.I) ...but it DOES Pull (Pop) the a copy of the Processor Status Word (Flags) (P.?) from the stack as it returns. | |||||
# |
Due to the way that the Interrupt occurs; The action of Pulling (POPing) the Processor Status Word (Flags) (P.?) off the stack means that the Interrupt (disable) Flag (P.I) is implicitly CLEARed. | |||||
# | The upshot is that under normal conditions RTI will CLEAR the Interrupt (disable) Flag (P.I) | |||||
How can I simulate an Interrupt. | ||||||
# |
The BRK instruction does precisely this. In fact, an IRQ triggers the same ciruit in the chip as BRK. | |||||
# | It MUST be noted that when BRK Pushes the return address on the Stack, it actually Pushes "PC+1". | |||||
# | ...Nobody seems to know why, but it does, and you have to deal with it! | |||||
# | The Interrupt Service Routine suggested below takes cares of this correction. | |||||
# | Alternatively, you can just put a NOP after each BRK instruction | |||||
How do I write an Interrupt Service Routine? | ||||||
# | The following code is an example of an Interrupt Service Routine | |||||
# |
This code does not realy tackle the issue of Device I/O. It is presumed that each device will set bit-7 of a specified memory address to indicate that it requires servicing. This is NOT always true, you will need to read the respective Device Manual to know precisely how each device works. | |||||
# |
There is a LOT more to know about Interrupts and Interrupt Service Routines, but it is not considered "in scope" for this document. (I've been at this a week and I'm getting bored.) | |||||
| ||||||
** |
If a device is a LOW priority device, you may choose to allow that device to interrupted
by placing and CLI instruction at the top of it's private handler code. | |||||
# |
If ZERO Device Handlers contain a CLI instruction then you will be coding
an "Interrupt Queue". | |||||
# |
If EVERY Device Handlers contain a CLI instruction then you will be coding
an "Interrupt Stack". | |||||
# |
If SOME Device Handlers contain a CLI instruction then you will be coding
an "Hybrid Interrupt Handler". | |||||
System Startup sequence (boot-strap) |
# |
At startup the processor will SET the Interrupt (disable) Flag (P.I) and
place the RESET Vector Address in the Program Counter (PC). ...and start executing whatever it finds there. | |||||||||||||||||
# | The programmer should now: | |||||||||||||||||
| ||||||||||||||||||
# | Each Input/Output (I/O) Device will normally trap reads and writes to block of addresses in the range $4000 to $7FFF. | |||||||||||||||||
# | Therefore, each I/O Device is normally initialised by writing a specific value to a specific memory address. | |||||||||||||||||
|
Bibliography |
"Synertek Programming Manual" | DOWNLOAD |
Publication Number 6500-50 | |
Written, 1975 | |
Revised, August 1976 | |
Published, May 1978 | |
SynerTek, 3050 Colorado Drive, Santa Clara, Cal. 95051 |
Mastering Machine Code on Your ZX Spectrum | |
Toni Baker | |
October 1983 | |
Publisher: Interface | |
ISBN: 0907563236 |
Rockwell 650x : 651x Data Sheet | DOWNLOAD |
Document Number 29000D39 | |
Order No. D39 | |
Rev B, June 1987 |
6502 Opcodes | VIEW |
John Pickens |
Compare Instructions | VIEW |
John Pickens |
|
|||
Authored by: Bluechip |