Re: Blue April - RC6502 Project Space
Posted: Mon Jul 11, 2022 8:46 pm
This is from approximately the middle of the programming-tips page of the 6502 primer:
If you want to just check bit 7 of PORTA (PA), you can use BIT PORTA and not involve or disturb the accumulator. Then do your BMI.
- An automatic compare-to-zero instruction is built into the following 65c02 instructions: LDA, LDX, LDY, INC, INX, INY, DEC, DEX, DEY, INA, DEA, AND, ORA, EOR, ASL, LSR, ROL, ROR, PLA, PLX, PLY, SBC, ADC, TAX, TXA, TAY, TYA, and TSX. This means that, for example, a CMP #0 after an LDA is redundant, a wasted instruction. A kitten somewhere dies every time you do that!
The only time a 65c02 (CMOS) needs a compare-to-zero instruction after one of these is if you want to compare a register that was not involved in the previous instruction; for example,
(Note the Y and the X are not the same register.) If you can spare a register to which you can transfer the one you want to test, you can save a byte with the transfer instead of a compare instruction. The example above, if the contents of A don't need to be kept, could be changed to:Code: Select all
DEY CPX #0
and then you can branch on the N or Z flag which tell if X was negative or zero. The TXA isn't any faster (both TXA and CPX# take two clocks); but TXA takes only one byte, whereas the CPX #0 takes two bytes.Code: Select all
DEY TXA
The NMOS 6502 did have a bug in that the flags weren't always correct after a decimal-mode operation like ADC; so then you might have to follow it with the CMP #0 to get the N and Z flags right. It's best to just use the CMOS processor for all new builds.
If you want to just check bit 7 of PORTA (PA), you can use BIT PORTA and not involve or disturb the accumulator. Then do your BMI.