* 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 ==
Decimal Mode allows arithmetic operations ADC and SBC instructions to use Binary-Coded Decimal (BCD), where each nibble (4 bits) represents a decimal digit (0-9), instead of binary. When this mode is enabled with the SED instruction, ADC and SBC instructions are automatically adjusted.
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>
=== Illegal instructions ===
Opcodes A lot of these illegal instructions involve a bitwise AND operation, which is a side effect of the open-drain behavior of NMOS logic. When two instructions put a value on the bus at the same time, this creates a bus conflict resulting effectively in red are unstablean AND operation. The lower voltage wins because transistors can pull down stronger than resistors can pull up.
{| class="wikitable" style="white-space: nowrap;"
! ''No arg'' !! #$nn !! $nnnn !! $nnnn,X !! $nnnn,Y !! $nn !! $nn,X !! $nn,Y !! ($nn,X) !! ($nn),Y !! N !! V !! B !! D !! I !! Z !! C
|-
| ALR (ASR) || || 4B (2) || || || || || || || || || 0 || - || - || - || - || Z || C || A AND oper, 0 -> [76543210] -> C CF || AND oper + LSR
|-
| ANC || || 0B (2) || || || || || || || || || N || - || - || - || - || Z || C || A AND oper, bit(7) -> C CF || AND oper + set C CF as ASL
|-
| ANC2 || || 2B (2) || || || || || || || || || N || - || - || - || - || Z || C || A AND oper, bit(7) -> C CF || AND oper + set C CF as ROL
|-
| ANE (XAA) || || style="color: #CC0000;"|'''8B''' (2) || || || || || || || || || N || - || - || - || - || Z || - || (A OR magic) AND X AND oper -> A || * AND X + AND oper
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, C CF -> [76543210] -> C 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 || DEC oper + CMP oper
|-
| ISC (ISB, INS) || || || EF (6) || FF (7) || FB (7) || E7 (5) || F7 (6) || || E3 (8) || F3 (8) || N || V || - || - || - || Z || C || M + 1 -> M, A - M - C CF -> A || INC oper + SBC oper
|-
| JAM (KIL, HLT) || 02, 12, 22,
|| || || || - || - || - || - || - || - || - || No operation || No Operation
|-
| RLA || || || 2F (6) || 3F (7) || 3B (7) || 27 (5) || 37 (6) || || 23 (8) || 33 (8) || N || - || - || - || - || Z || C || M = C CF <- [76543210] <- CCF, A AND M -> A || ROL oper + AND oper
|-
| RRA || || || 6F (6) || 7F (7) || 7B (7) || 67 (5) || 77 (6) || || 63 (8) || 73 (8) || N || V || - || - || - || Z || C || M = C CF -> [76543210] -> CCF, A + M + C CF -> A, C CF || ROR oper + ADC oper
|-
| SAX (AXS, AAX) || || || 8F (4) || || || 87 (3) || || 97 (4) || 83 (6) || || - || - || - || - || - || - || - || A AND X -> M || Stores the bitwise AND of A and X
unstable: sometimes 'AND (H+1)' is dropped, page boundary crossings may not work
|-
| SLO (ASO) || || || 0F (6) || 1F (7) || 1B (7) || 07 (5) || 17 (6) || || 03 (8) || 13 (8) || N || - || - || - || - || Z || C || M = C CF <- [76543210] <- 0, A OR M -> A || ASL oper + ORA oper
|-
| SRE (LSE) || || || 4F (6) || 5F (7) || 5B (7) || 47 (5) || 57 (6) || || 43 (8) || 53 (8) || N || - || - || - || - || Z || C || M = 0 -> [76543210] -> CCF, A EOR M -> A || LSR oper + EOR oper
|-
| TAS (XAS, SHS) || || || || || style="color: #CC0000;"|'''9B''' (5) || || || || || || - || - || - || - || - || - || - || A AND X -> SP, A AND X AND (H+1) -> M || Puts A AND X in SP and stores A AND X AND (high-byte of addr + 1) at addr
unstable: sometimes 'AND (H+1)' is dropped, page boundary crossings may not work
|-
| USBC (SBC) || || EB (2) || || || || || || || || || N || V || - || - || - || Z || C || A - M - ~C CF -> A || SBC oper + NOP
|}
A lot of these illegal instructions involve a bitwise AND operation, which is a side effect of the open-drain behavior of NMOS logic. When two instructions put a value on the bus at the same time, this creates a bus conflict resulting effectively Opcodes in an AND operation. The lower voltage wins because transistors can pull down stronger than resistors can pull upred are unstable.
<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 6502 post-decrements on PHA and pre-increments on PLA, while the Z80 pre-decrements on PUSH and post-increments on POP, making them behave in opposite ways.
* The 6502 saves flags automatically during interrupts; while the Z80 requires PUSH AF and POP AF.
* The 6502’s Decimal (BCD) mode automatically adjusts ADC and SBC results, while the Z80 requires a DAA instruction after each BCD addition and subtraction.
* 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.
* The 6502’s Decimal (BCD) mode automatically adjusts ADC and SBC results, while the Z80 requires a DAA instruction after each BCD addition and subtraction.
* The 6502 uses only one addressing mode per instruction, while the Z80 can combine two different addressing modes within a single instruction.
* The 6502 post-decrements 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.
<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]