Some rarely used 6502 aspects
I am by no means any professional programmer, but I have coded 6502 since the late 80s and can safely say that I have a lot of experience. My main focus throughout all these years has been to reverse other peoples work, so I have also seen a lot of examples of how others use it.
I will take this time to look at two rather nice, but rather underutilised ways to do things.
The BIT operation
The most used aspect of BIT is seems to be to allow different entry values in a set of code, like you see here:
It’s a very handy way to achieve a result that is difficult to get good in other ways. The $2C (BIT of a word) is used as a way to have the CPU skip the following two bytes. (Basically like the illegal opcode $0C, that is a three byte NOP)
But have you used BIT for what is actually there to do? In a situation where you have data in all three registers (A, X, and Y) and you still need to make a compare, BIT might be your friend.
When you do a BIT operation, bit 7 from the argument is copied to the N flag and bit 6 is copied to the V flag which means that you can now use BPL/BMI to branch based on bit 7 and use BVS/BVC to branch based on bit 6.
The command also sets the Z flag but that is a lot trickier to use, as the flag is set as a result of an AND between the A and the memory location given as an argument. So it’s not independent of the registers as the N and V flags are.
Decimal mode
“Hexadecimal is the humanly readable form of binary” (Pontus Berg, 2021). Whereas hex is logical and easy to manage, many people in the real world tend to prefer decimal. Given our number of fingers, it does make sense to use that. And no 6502 programmer can get around the problem that you need to bridge the two paradigms from time to time. Be it counting lives and other resources in a game to printing line numbers in a wordprocessor, presentation of decimal values is sometimes requested.
So how do you convert between the two? Well, have you ever considered that you don’t have to? Store the value in decimal form and let the 6502 do its work, using decimal when accessing these values. Looking at the example on the right, what is the value of the A register when you read it from $1000?
Normally incrementing 09 with one would be 0A. But if you set decimal mode (SED), increasing 09 with one results in 10. It handles the nibbels in the byte as only being able to hold values up to 9. Very handy indeed.
Just remember to close decimal mode again (CLD) or your code will for sure turn haywire.
Decimal mode only affects ADC and SBC operations, not INC or other ones. Recommend a modified example, otherwise the answer in A register is $0A which is not limited to decimal digits.
sed
lda #$09
clc
adc #$01
sta $1000
cld