6502bench

Odds & Ends

The rest of the code isn't really intended to do anything useful. It just exists to illustrate some odd situations.

t2-2078

Look at the code starting at $2078. It ends with a BRK at $2081, which as noted earlier is a bad sign. If you look two lines above the BRK, you'll see that it's loading the accumulator with zero, then doing a BNE, which should never be taken (note the cycle count for the BNE is 2). The trick is in the two lines before that, which use self-modifying code to change the LDA immediate operand from $00 to $ff. The BNE is actually a branch-always.

t2-override-status.png

We can fix this by correcting the status flags. Select line $207F, and then Actions > Override Status Flags. This lets us specify what the flags should be before the instruction is executed. For each flag, we can override the default behavior and specify that the flag is clear (0), set (1), or indeterminate (could be 0 or 1). In this case, we know that the self-modified code will be loading a non-zero value, so in the "Z" column click on the button in the "Zero" row. Click "OK".

t2-2078-done

The BNE is now an always-taken branch, and the code list rearranges itself appropriately (and the cycle count is now 3).

t2-2086

Continuing on, the code at $2086 touches a few consecutive locations that have auto-generated labels.

Edit the label on line $2081, setting it to STUFF. The operand label on line $2086 also changed. But what if we want to treat these as a single four-byte item?

t2-seek-nearby

Use Edit > Project Properties, then in the Analysis Parameters box check Seek nearby targets, and click OK.

t2-2081-stuff

You'll notice that the references to $2081 and later are now also references to STUFF. The nearby-target behavior is often useful, because it lets you avoid explicitly labeling every part of a multi-byte data item. References to the byte before a string or array are automatically resolved to LABEL-1. You can use Edit > Undo to turn it back off: changes to project properties are added to the undo/redo buffer just like any other change to the project.

(This feature used to be enabled by default, but is now disabled for new projects because it can be more distracting than helpful for some types of code.)

If you scroll up, you'll see that the references to PTR1+1 and PTR2+1 were not affected, because local variables use explicit widths rather than the "nearby target" logic.

t2-2092

While we're here, the code at $2092 looks a bit strange. LDX, then a BIT with a weird symbol, then another LDX. If you look at the "Bytes" column, you'll notice that the three-byte BIT instruction has only one byte on its line.

The trick here is that the LDX #$01 is embedded inside the BIT instruction. When the code runs through here, X is set to $00, then the BIT instruction sets some flags, then the following STA runs. Several lines down at $20A4 there's a BNE to $2095, which is in the middle of the BIT instruction. It loads X with $01, then also continues to the STA.

Embedded instructions are unusual but not unheard-of. (This trick is used extensively in Microsoft BASICs, such as Applesoft.) When you see the extra symbol in the opcode field, you need to look closely at what's going on.


This is the end of the basic tutorial (congratulations!). The next sections explore some advanced topics.

« Previous Next »