* bit0 - CF - Carry Flag: 1 on unsigned overflow
|| Flags are affected by most operations.
The BF is bit does not a physical flag implemented in a registeractually exist inside the 6502. It The BF bit only appears on exists in the status flag byte pushed to the stack when . When the P register flags are restored (via PLP or RTI), the BF bit is pushed to itdiscarded.
PHP (Push Processor Status) and PLP (Pull Processor Status) can be used to set or retrieve P directly via the stack.
== Decimal Mode ==
The 6502’s Decimal (BCD) mode automatically adjusts Mode allows ADC and SBC resultsinstructions to use Binary-Coded Decimal (BCD), while the Z80 requires a DAA instruction after where each BCD addition and subtractionnibble (4 bits) represents a decimal digit (0-9), instead of binary.
On NMOS, when Decimal Mode is on, the ADC and SBC instructions update NF, VF and ZF based on the binary result before the decimal correction is applied. Only CF is updated correctly. On CMOS, all the flags are updated correctly, at the cost of 1 additional cycle.
== Pipelining ==
The 6502 CPU uses some sort We have to dispel the myth of pipeliningin the 6502. If an instruction does not store data we analyze its operation in memory on its last cyclehalf-cycles, the processor can fetch the opcode of the next we see that instruction while executing the last cycle. This execution is very primitive as the 6502 does not have an instruction cache nor even a prefetch queue. It relies on RAM tightly bound to hold all program informationmemory operations without any overlap between different instructions.
As an exampleEach instruction follows a rigid sequence of steps, with no ability to fetch the next instruction EOR #$FF truly takes 3 cycles:* On while executing the first cycle, current one. This means that the opcode $49 will be fetched* During the second cycle the processor decodes the opcode and fetches the parameter #$FF* On the third cycle, the processor will perform the operation and store the result CPU cannot prefetch opcodes or operands ahead of time in register A, but simultaneously it fetches the opcode for the next instructionway a pipelined architecture would.
This is If we invert our perspective and consider ϕ2 as the first half-cycle and ϕ1 as the second, it becomes evident why pipelining does not exist on the EOR instruction effectively takes only 2 cycles6502.
However, this pipelining only makes sense when looking at full cycles. If we break it down into half-cycles, there's no actual overlap. In fact, it's the other way around. If the previous instruction ends with a memory write, the CPU has to wait for a half-cycle before fetching being able to fetch the next instruction on the next ϕ2 half-cycle.
<br>
== Adressing Modes ==
The 6502 uses only one addressing mode per instruction.
{| class="wikitable"
#Operand Fetch (if needed)
#Memory Read / I/O Read (if needed)
#ExecutionOperation
#Memory Write / I/O Write (if needed)
#At the end of every instruction, the IRQ (if the interrupt disable flag is clear) and NMI pins are checked.
As an example, let M[$42]=$80, M[$43]=$10 and Y=$F1. Then the instruction LDA ($42),Y will execute as follow, with ϕ2 as the first half-cycle and ϕ1 as the second half-cycle:
*T0: Fetch opcode $B1 (LDA (zp),Y) from memory then increment PC
*T1: Fetch operand byte $42 (zero page pointer address) then increment PC
*T2: Get low byte from zero page ($80) then increment the zero page address
*T3: Get high byte from next zero page location ($10) then add the Y register value ($F1) to $1080
*T4: Garbage fetch from memory address $1071 then handle page boundary crossing (since $1080 + $F1 crosses a page)
*T5: Read the value from memory address $1171 into the accumulator then no operation in the last half-cycle
<br>
| DEC || || || || CE (6) || DE (7) || || || C6 (5) || D6 (6) || || || || || N || - || - || - || - || Z || - || M - 1 → M || DECrement memory
|-
| DEX || CA (2) || || || || || || || || || || || || || N || - || - || - || - || Z || - || X - 1 → X || Decrement Index Register DEcrement X By One
|-
| DEY || 88 (2) || || || || || || || || || || || || || N || - || - || - || - || Z || - || Y - 1 → Y || Decrement Index Register DEcrement Y By One
|-
| EOR || || || 49 (2) || 4D (4) || 5D (4+p) || 59 (4+p) || || 45 (3) || 55 (4) || || 41 (6) || 51 (5+p) || || N || - || - || - || - || Z || - || A ⊻ M → A || bitwise Exclusive OR
| INC || || || || EE (6) || FE (7) || || || E6 (5) || F6 (6) || || || || || N || - || - || - || - || Z || - || M + 1 → M || INCrement memory
|-
| INX || E8 (2) || || || || || || || || || || || || || N || - || - || - || - || Z || - || X + 1 → X || Increment Index Register INcrement X By One
|-
| INY || C8 (2) || || || || || || || || || || || || || N || - || - || - || - || Z || - || Y + 1 → Y || Increment Index Register INcrement Y By One
|-
| LSR || || 4A (2) || || 4E (6) || 5E (7) || || || 46 (5) || 56 (6) || || || || || 0 || - || - || - || - || Z || C || 0 → /M<sub>7</sub>...M<sub>0</sub>/ → CF || Logical Shift Right
| STY || || || 8C (4) || || || || 84 (3) || 94 (4) || || || || || - || - || - || - || - || - || - || Y → M || STore Y register
|-
| TAX || AA (2) || || || || || || || || || || || || N || - || - || - || - || Z || - || A → X || Transfer Accumulator A to Index X
|-
| TAY || A8 (2) || || || || || || || || || || || || N || - || - || - || - || Z || - || A → Y || Transfer Accumulator A to Index Y
|-
| TSX || BA (2) || || || || || || || || || || || || N || - || - || - || - || Z || - || S → X || Transfer Stack pointer to X
|-
| TXA || 8A (2) || || || || || || || || || || || || N || - || - || - || - || Z || - || X → A || Transfer Index X to AccumulatorA
|-
| TXS || 9A (2) || || || || || || || || || || || || - || - || - || - || - || - || - || X → S || Transfer X to Stack pointer
|-
| TYA || 98 (2) || || || || || || || || || || || || N || - || - || - || - || Z || - || Y → A || Transfer Index Y to AccumulatorA
|}
| JMP || || || 4C (3) || || || 6C (5) || || || || || || || - || - || - || - || - || - || - || [PC + 1] → PCL, [PC + 2] → PCH || JuMP
|-
| JSR || || || 20 (6) || || || || || || || || || || - || - || - || - || - || - || - || PC + 2↓, [PC + 1] → PCL, [PC + 2] → PCH || Jump To to SubRoutine
|-
| NOP || EA (2) || || || || || || || || || || || || - || - || - || - || - || - || - || No operation || No OPeration
=== Illegal instructions ===
Opcodes in red are unstable. Only 2 A lot of those 7 opcodes ($8Bthese illegal instructions involve a bitwise AND operation, $AB) are actually unstable in the sense that they may produce which is a truly unpredictable resultside effect of the open-drain behavior of NMOS logic. The other 5 opcodes actually produce predictable results – but When two instructions put a value on the conditions under which they do that and bus at the produced results are same time, this creates a bit unexpectedbus conflict resulting effectively in an AND operation. The lower voltage wins because transistors can pull down stronger than resistors can pull up.
{| class="wikitable" style="white-space: nowrap;"
|-
! rowspan=2|Mnemonic !! rowspan=2|Combines !! colspan=10|Addressing Modes !! colspan=7|Flags !! rowspan=2|Operation !! rowspan=2|Description
|-
! ''No arg'' !! #$nn !! $nnnn !! $nnnn,X !! $nnnn,Y !! $nn !! $nn,X !! $nn,Y !! ($nn,X) !! ($nn),Y !! N !! V !! B !! D !! I !! Z !! C
|-
| ANC ALR (ANC2ASR) || AND + ASL/ROL || || 0B, 2B 4B (2) || || || || || || || || || N 0 || - || - || - || - || Z || C || A ∧ M → AAND oper, NF → 0 -> [76543210] -> CF || "AND" Memory with Accumulator then Move Negative Flag to Carry Flagoper + LSR
|-
| ARR || AND + ROR ANC || || 6B 0B (2) || || || || || || || || || N || V - || - || - || - || Z || C || A AND oper, bit(A ∧ M7) / 2 → A -> CF || "AND" Accumulator then Rotate Rightoper + set CF as ASL
|-
| ASR (ALR) || AND + LSR ANC2 || || 4B 2B (2) || || || || || || || || || 0 N || - || - || - || - || Z || C || A AND oper, bit(A ∧ M7) / 2 → A -> CF || "AND" then Logical Shift Rightoper + set CF as ROL
|-
| DCP ANE (DCMXAA) || DEC || style="color: #CC0000;"|'''8B''' (2) || || || || || || || || || N || - || - || - || - || Z || - || (A OR magic) AND X AND oper -> A || * AND X + CMP AND operhighly unstable: involves a 'magic' constant that depends on temperature, the chip series, and maybe other factors. Turrican 3 on C64 requires a different magic constant than $EE for ANE. $EF is recommended by Groepaz (VICE team)|-| ARR || || 6B (2) || || || || || || || || || N || V || - || - || - || Z || C || A AND oper, CF -> [76543210] -> CF || AND oper + ROR|-| DCP (DCM) || || || CF (6) || DF (7) || DB (7) || C7 (5) || D7 (6) || || C3 (8) || D3 (8) || N || - || - || - || - || Z || C || M - 1 → -> M, A - M || Decrement Memory By One then Compare with AccumulatorDEC oper + CMP oper
|-
| ISC (ISB, INS) || INC + SBC || || || EF (6) || FF (7) || FB (7) || E7 (5) || F7 (6) || || E3 (8) || F3 (8) || N || V || - || - || - || Z || C || M + 1 → -> M, A - M → - CF -> A || Increment Memory By One then INC oper + SBC then Subtract Memory from Accumulator with Borrowoper
|-
| JAM (KIL, HLT) || || 02, 12, 22,
32, 42, 52,
B2, D2, F2 (X)
|| || || || || || || || || || - || - || - || - || - || - || - || Stop execution || Halt the CPU. The processor will be trapped infinitely in T1 phase with $FF on the data bus. Reset required.
|-
| LAS (LAR) || STA/TXS + LDA/STX || || || || || BB (4+p) || || || || || || N || - || - || - || - || Z || - || M ∧ S → AND SP -> A, X, S SP || "AND" Memory with Stack PointerLDA/TSX oper
|-
| LAX (LXA) || LDA + LDX || || style="color: #CC0000;"|'''AB''' (2) || AF (4) || || BF (4+p) || A7 (3) || || B7 (4) || A3 (6) || B3 (5+p) || N || - || - || - || - || Z || - || M → -> A, -> X || Load Accumulator and Index Register X From MemoryLDA oper + LDX oper
|-
| LXA (LAX) || || style="color: #CC0000;"|'''AB''' (2) || || || || || || || || || N || - || - || - || - || Z || - || (A OR magic) AND oper -> A -> X || Store * AND oper in A and Xhighly unstable: involves a 'magic' constant that depends on temperature, the chip series, and maybe other factors. Wizball on C64 requires a $EE magic constant for LXA|-| NOP (DOP, TOP) || || 1A, 3A, 5A,
7A, DA, FA (2)
|| 80, 82, 89,
|| || || || - || - || - || - || - || - || - || No operation || No Operation
|-
| RLA || ROL + AND || || || 2F (6) || 3F (7) || 3B (7) || 27 (5) || 37 (6) || || 23 (8) || 33 (8) || N || - || - || - || - || Z || C || M = CF ← /M7...M0/ ← <- [76543210] <- CF, A ∧ AND M → -> A || Rotate Left then "ROL oper + AND" with Accumulatoroper
|-
| RRA || ROR + ADC || || || 6F (6) || 7F (7) || 7B (7) || 67 (5) || 77 (6) || || 63 (8) || 73 (8) || N || V || - || - || - || Z || C || M = CF → /M7...M0/ → -> [76543210] -> CF, A + M + CF → -> A , CF || Rotate Right and Add Memory to AccumulatorROR oper + ADC oper
|-
| SAX (AXS, AAX) || STA + STX || || || 8F (4) || || || 87 (3) || || 97 (4) || 83 (6) || || - || - || - || - || - || - || - || A ∧ AND X → -> M || Store Accumulator "Stores the bitwise AND" Index Register of A and X in Memory
|-
| SBC SBX (USBCAXS, SAX) || SBC + NOP || || EB CB (2) || || || || || || || || || N || V - || - || - || - || Z || C || (A AND X) - M oper - ~CF → A > X || Subtract Memory from Accumulator with BorrowCMP and DEX at once, sets flags like CMP
|-
| SBX SHA (AXSAHX, SAXAXA) || CMP + DEX || || CB (2) || || style="color: #CC0000;"|'''9F''' (5) | || || || || |style="color: #CC0000;"| '''93''' (6) || N - || - || - || - || - || Z - || C - || (A ∧ AND XAND (H+1) - > M → X || Subtract Memory from Accumulator "Stores A AND" Index Register XAND (high-byte of addr + 1) at addrunstable: sometimes 'AND (H+1)' is dropped, page boundary crossings may not work
|-
| SHA SHX (AHXA11, AXASXA, XAS) || STA/STX/STY || || || || || style="color: #CC0000;"|'''9F9E''' (5) || || || || || style="color: #CC0000;"|'''93''' (6) || - || - || - || - || - || - || - || A ∧ X ∧ V → AND (H+1) -> M || Store Accumulator "AND" Index Register Stores X "AND" Value(high-byte of addr + 1) at addrunstable: sometimes 'AND (H+1)' is dropped, page boundary crossings may not work
|-
| SHS SHY (TASSYA, XASSAY) || STA/TXS + LDA/TSX || || || || || style="color: #CC0000;"|'''9B9C''' (5) || || || || || || || - || - || - || - || - || - || - || A ∧ X → S, S ∧ Y AND (H + 1) → -> M || Transfer Accumulator "Stores Y AND" Index Register X to Stack Pointer then Store Stack Pointer "AND" Hi(high-Byte In Memorybyte of addr + 1) at addrunstable: sometimes 'AND (H+1)' is dropped, page boundary crossings may not work
|-
| SHX SLO (SXA, XASASO) || STA/STX/STY || || 0F (6) || 1F (7) || 1B (7) || style="color: #CC0000;"|'''9E''' 07 (5) || 17 (6) || || 03 (8) || 13 (8) || N || - || - || - || - || - Z || - C || M = CF <- || X ∧ (H + 1) → [76543210] <- 0, A OR M -> A || Store Index Register X "AND" ValueASL oper + ORA oper
|-
| SHY SRE (SYA, SAYLSE) || STA/STX/STY || || 4F (6) || 5F (7) || style="color: #CC0000;"5B (7) ||'''9C''' 47 (5) || 57 (6) || || 43 (8) || 53 (8) || || N || - || - || - || - || - Z || - C || M = 0 - || Y ∧ (H + 1) → > [76543210] -> CF, A EOR M -> A || Store Index Register Y "AND" ValueLSR oper + EOR oper
|-
| SLO TAS (ASOXAS, SHS) || ASL + ORA || || || 0F (6) || 1F (7) |style="color: #CC0000;"| 1B '''9B''' (75) || 07 (5) || 17 (6) || || 03 (8) || 13 (8) || N - || - || - || - || - || Z - || C - || M * 2 → MA AND X -> SP, A ∨ AND X AND (H+1) -> M → A || Arithmetic Shift Left then "OR" Memory with AccumulatorPuts A AND X in SP and stores A AND X AND (high-byte of addr + 1) at addrunstable: sometimes 'AND (H+1)' is dropped, page boundary crossings may not work
|-
| SRE USBC (LSESBC) || LSR + EOR || EB (2) || || 4F (6) || 5F (7) || 5B (7) || 47 (5) || 57 (6) || || 43 (8) || 53 (8) || N || - V || - || - || - || Z || C || M / 2 → M, A ⊻ - M → A || Logical Shift Right then "Exclusive OR" Memory with Accumulator|-| XAA (ANE) || TXA + AND || || style="color: #CC0000;"|'''8B''' (2) || || || || || || || || || N || ~CF - || - || - || - || Z || - || (A ∨ V) ∧ X ∧ M → > A || Non-deterministic Operation of Accumulator, Index Register X, Memory and Bus ContentsSBC oper + NOP
|}
Opcodes in red are unstable.
<br>
| 87 xx || '''SAX ''zpg'''''
|-
| 8B xx ||style="color: #CC0000;"|'''XAA ANE #'''
|-
| 8F xx xx || '''SAX ''abs'''''
| 97 xx || '''SAX ''zpg'',Y'''
|-
| 9B xx xx ||style="color: #CC0000;"|'''SHS TAS ''abs'',Y'''
|-
| 9F xx xx ||style="color: #CC0000;"|'''SHA ''abs'',Y'''
| E7 xx || '''ISC ''zpg'''''
|-
| EB xx || '''SBC USBC #'''
|-
| EF xx xx || '''ISC ''abs'''''
Any instruction xxxxxx11 will execute the instructions at xxxxxx01 and xxxxxx10 at once, using the address mode of the instruction at xxxxxx01.
For example:* , "SAX abs” ($8F) is the composite of “STA abs” ($8D) and “STX abs” ($8E)* "LAX X,ind” ($A3) is the composite of “LDA X,ind” ($A1) and “LDX imm” ($A2).
<br>
* On NMOS, an indirect JMP will behave unexpectedly when the indirect address crosses a page boundary, because the 6502 does not add the carry to calculate the address of the high byte. For example, JMP ($19FF) will use the contents of $19FF and $1900 for the JMP address. On CMOS, this issue was fixed, at the cost of 1 additional cycle. In our example, JMP ($19FF) will use the contents of $19FF and $2000 for the JMP address.
* Some instructions, particularly those involving branches or indexed addressing modes, incur an extra cycle if the processor has to cross a memory page boundary. This is problematic for time-sensitive code.
* Conditional jumps are only 8-bit relative. And unconditional jumps are only 16-bit absolute.
* The Z80 pre-decrements on PUSH and post-increments on POP, while the 6502 post-decrements on PHA and pre-increments on PLA, making them behave in opposite ways.
* ADC is the only command for addition. To perform an addition without carry, the carry flag must be cleared manually first. Same with SBC for subtract.
* The CLV (Clear Overflow Flag) instruction exist but not the SEV (Set Overflow Flag) instruction.
* The NOP instruction takes 2 full-cycles. This is the minimum amount of cycles an instruction can take. It is necessary because, while the instruction itself does nothing, it still has to increment the 16-bit PC register.
* The alternate NOPs are not created equal. Some have one- or two-byte operands (which they don't do anything with), and they take different amounts of time to execute.
<br> == Compared behaviour == * The alternate NOPs are not created equal6502’s Decimal (BCD) mode automatically adjusts ADC and SBC results, while the Z80 requires a DAA instruction after each BCD addition and subtraction. Some have * The 6502 uses only one- or addressing mode per instruction, while the Z80 can combine twodifferent addressing modes within a single instruction.* The 6502 post-byte operands (which they dondecrements on PHA and pre-increments on PLA, while the Z80 pre-decrements on PUSH and post-increments on POP.* The 6502 saves flags automatically during interrupts; while the Z80 requires PUSH AF and POP AF.* The 6502 only updates flags that are directly relevant to the operation's result. For example, EOR doesn't do anything with)conceptually involve a carry, and they take different amounts of time so the Carry flag is left untouched. On the Z80, XOR always clears the Carry flag to executeensure a clean flag state.
<br>
* The ROR instruction didn't exist in the very earliest (pre-1977) chips. See: [https://www.pagetable.com/?p=406 Measuring the ROR Bug in the Early MOS 6502]
* The 6502 core used inside the [[NES]] is missing the Decimal Mode feature. [https://archive.org/details/nes-programmers-reference-guide-by-electronic-arts-1989/ NES programmer's reference guide] [https://www.nesdev.org/NESDoc.pdf NESDoc] [https://www.nesdev.org/wiki/Mapper NES mappers] [https://problemkaputt.de/everynes.htm Noca$h's Everynes] [https://www.nesdev.org/wiki/Emulator_tests NES emulator tests] [https://tcrf.net/Category:Nintendo_Console_Testing_Software Official Nintendo testing software]
* The 6507 CPU, used in the [[Atari VCS]], has only 13 address lines. So it can only address 8KB instead of 64KB. It also lacks the IRQ and NMI interrupt lines. [https://youtu.be/qvpwf50a48E Atari VCS: The Ultimate Talk] [https://cdn.hackaday.io/files/1646277043401568/stella.pdf Stella programmer's guide] [https://www.atarimania.com/documents/stella_system_training_manual.pdf Stella system training manual] [https://problemkaputt.de/2k6specs.htm Noca$h's 2k6specs]
* The HuC6280, used in the [[PC-Engine]] gaming console, is an improved version of the CMOS 65C02. [[Media:HuC6280 - CMOS 8-bit Microprocessor Hardware Manual.pdf|HuC6280 hardware manual]] [[Media:HuC6260 - CMOS Video Color Encoder Manual.pdf|HuC6260 VCE manual]] [[Media:HuC6270 - CMOS Video Display Controller Manual.pdf|HuC6270 VDC manual]]
* The WDC 65C816, used in the [[SNES]] and the [[Apple IIGS]], is a 16-bit version of the 65C02 [https://www.westerndesigncenter.com/wdc/documentation/w65c816s.pdf 65C816 datasheet] [https://archive.org/details/SNESDevManual/ SNES development manual] [https://problemkaputt.de/fullsnes.htm Noca$h's fullsnes] [https://fabiensanglardsnes.netnesdev.org/wiki/ Fabien Sanglard's 2024 articlesSNESdev_Wiki SNESdev wiki]. The 65C816 contains a compatibillity mode, enabled by default upon reset, that makes it behave like a regular 65C02.
* The Sony SPC700 sound CPU used inside the SNES also behaves similarly to a 6502 with some extensions. [https://wiki.superfamicom.org/spc700-reference Source] [https://www.youtube.com/watch?v=zrn0QavLMyo&list=PLHQ0utQyFw5JD2wWda50J8XuzQ2cFr8RX SPC700 Series] [https://github.com/gilyon/snes-tests SNES-tests]
<br>
*[https://www.chibiakumas.com/6502/ Learn Assembly Programming with ChibiAkumas] Multi-platform 6502 tutorial
*[https://www.nesdev.org/obelisk-6502-guide/ Obelisk 6502 Guide]
*[https://www.masswerk.at/6502/6502_instruction_set.html 6502 Instruction Set] [https://www.masswerk.at/nowgobang/2021/6502-illegal-opcodes 6502 illegal opcodes demystified]*[[Media:NoMoreSecrets-NMOS6510UnintendedOpcodes-20212412.pdf|No more secrets - NMOS 6510 Unintended Opcodes]]
*[https://www.pagetable.com/c64ref/6502/?tab=2 6502 Family CPU Reference]
*[https://llx.com/Neil/a2/opcodes.html The 6502/65C02/65C816 Instruction Set Decoded]
*[https://www.nesdev.org/wiki/CPU_unofficial_opcodes Opcode matrix arrangement]
*[https://www.oxyron.de/html/opcodes.html Oxyron Opcode Matrices]
*[http://retro.hansotten.nl/uploads/books/Programming_the_6502.pdf Rodnay Zaks Programming the 6502]
*[http://www.6502.org/documents/datasheets/synertek/synertek_programming_manual.pdf Synertek SY650x programming manual (250 pages)]
*[http://www.6502.org/documents/datasheets/synertek/synertek_hardware_manual.pdf Synertek SY650x hardware manual (178 pages)]
*[[Media:SY6500 - SY65C02 datasheet.pdf]] - provides features a detailed breakdown of the machine cycles65C02 instructions*[https://xotmatrix.github.io/6502/ xotmatrix] 6502 instruction set and detailed cycle-by-cycle breakdown
*[[Media:6502 (65xx) Microprocessor Instant Reference Card.pdf]]
*[https://www.nesdev.org/wiki/CPU_interrupts 6502 Interrupts]
*[[Media:6502-dead-cycles.pdf]]
*[https://codebase64.org/doku.php?id=base:6510_instruction_timing 6510 Instruction Timing]
*[[Media:NoMoreSecrets-NMOS6510UnintendedOpcodes-20212412.pdf|No more secrets - NMOS 6510 Unintended Opcodes]]
*[https://www.masswerk.at/nowgobang/2021/6502-illegal-opcodes 6502 illegal opcodes demystified]
*[https://youtu.be/fWqBmmPQP40 Conference - Reverse Engineering the MOS 6502 CPU]
*[https://floooh.github.io/visual6502remix/ Visual6502remix] [https://www.nesdev.org/wiki/Visual6502wiki Visual6502wiki]