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.

t2-2081-stuff

Edit the label on line $2081, setting it to STUFF. Notice how the references to $2081 through $2084 have changed from auto-generated labels to references to STUFF.

t2-seek-nearby

For some projects this may be undesirable. Use Edit > Project Properties, then in the Analysis Parameters box un-check Seek nearby targets, and click OK.

You'll notice that the references to $2081 and later have switched back to auto labels. 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" logic.

The nearby-target behavior is generally desirable, because it lets you avoid explicitly labeling every part of a multi-byte data item. For now, use Edit > Undo to switch it back on. (Changes to project properties are added to the undo/redo buffer just like any other change to the project.)

t2-2092

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 STA runs. Several lines down 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 »