This post is a bit long by my standards. The TL;DR version of my questions are:
- Is my understanding of how the 6502 handles inequalities correct?
- Am I correct in understanding there is no bitwise NOT instruction? If so, what's the best way to simulate ~ .A?
- How does the V flag fit into the inequality matrix? I get that it's out-of-range for 2's compliment in a single byte.
- How does BCD mode affect any of this?
So, I'm still tying to understand a few basic things about how the 6502 checks for inequality and one logic question. In the abstract I "get" that from the Negative, Zero, and Cary flags — 2^3=8 possible combinations of bits — logical inequalities emerge. What is less clear is what combination of flags constitute which specific inequalities. I had to consult 2-3 different documents in order to pull together this chart.
Even then, I may well have made mistakes. This is part of a journey on my part to better grasp the Processor Status Register's effects on the system as a whole. I was encouraged to dig along these lines due to a comment on
another thread of mine.
It seems six possibilities should exist and that .P NZC drive the show:
Binary Inequality ChartCode:
Accumulator .P[NZC]
.A < M 100
.A > M 001
.A ≠ M 101
.A = M 010
.A ≤ M 110
.A ≥ M 011
.A ? M 000 (unsigned !=?)
.A ? M 111 (signed ==?)
Given 8 possible permutations of 3-bits, I thought that might suggest there are two combinations of the above three flags that don't fall neatly into symbolic logic. Of course, if someone casually suggested my ALU was "missing a few symbols" they might just be right.
Bruce Clark's article
Compare Instructions an Branching helped. The
Compare and Beyond article was instrumental. Without it, would not have the 3-bit logical pattern worked out the way that I did.
Jump Instructions: Unsigned Bytes- BCC (Branch on Cary Clear) IF unsigned .A is < M .PC will branch to target.
- BCC (Branch on Cary Clear) IF unsigned .A is ≤ M .PC will branch to target.
- BCS (Branch on Cary Set) IF unsigned .A is > M .PC will branch to target.
- BCS (Branch on Cary Set) IF unsigned .A is ≥ M .PC will branch to target.
- BEQ (Branch if Equal to Zero Flag Set) IF unsigned .A is ≤ M .PC will branch to target.
- BEQ (Branch if Equal to Zero Flag Set) IF unsigned .A is = M .PC will branch to target.
- BEQ (Branch if Equal to Zero Flag Set) IF unsigned .A is > M .PC will NOT branch to target.
- BVC (BVCBranch if oVerflow Clear) IF unsigned .A is ? .PC will do what?
Jump Instructions: Signed Bytes- BMI (Branch if Minus [negative] flag Set) IF signed .A is < M .PC will branch to target.
- BEQ (Branch if Equal to Zero Flag Set) IF signed .A is = M .PC will branch to target.
- BEQ (Branch if Equal to Zero Flag Set) IF signed .A is > M .PC will NOT branch to target.
- BPL (Branch if Minus [negative] flag clear) IF signed .A is > M .PC will branch to target.
- BMI (Branch if Minus [negative] flag set) IF signed .A is ≤ M .PC will branch to target.
- BEQ (Branch if Equal to Zero Flag Set) IF signed .A is ≤ M .PC will branch to target.
- BPL (Branch if Minus [negative] flag clear) IF signed .A is ≥ M .PC will branch to target.
- BVC (BVCBranch if oVerflow Clear) IF signed .A is ? .PC will do what?
Some assemblers, such as 64tass, aliases BCC as BLT (Branch Less Than) and BCS as BGE (Branch Greater Equal). Are these the most flexible greater than / less than instructions?
Bitwise LogicFrom the 6502 instruction set, I see most of what I expect for bitwise operations:
- The and instruction is bitwise AND (&) .A which interacts with .P: N+, Z+.
- The ora instruction is bitwise OR (|) .A which interacts with .P: N+, Z+.
- The eor instruction is bitwise XOR (^) .A which interacts with .P: N+, Z+.
- I'm not sure, but it seems bitwise NOT (~) .A does not exist?!
- The asl instruction is arithmetic shift left (<<) .A which interacts with .P: N+, Z+, C+.
- The lsr instruction is arithmetic shift right (>>) .A which interacts with .P: N-, Z+.
Am I missing something about the existence of bitwise NOT? Assuming there is no instruction to that effect, what combination of instructions would simulate bitwise NOT by flipping all 8 bits? I think it's a precise combination of OR with AND but I haven't quite got it. This seems like glaring omission.