Understanding 6502 assembly on the Commodore 64 - (9) The Hexadecimal Conundrum

Its time, now to take a step back and look at some very important things in out last program.  It would seem that the simplicity of this particular program allowed us to overlook what is an inevitable coding issue.  This of course, was done intentionally, as I've stated, to make the easiest to read program, whist sacrificing other things in return.

To show our issue, we need only to cite one example that appears twice in our code.

; Sprite Enable Register
SPENA = $D015

We used this to enable our sprite in the beginning of the program.  And used it again to disable the sprite in our END routine

Lines 68-69 shows the enabling of the sprite

lda #$01

Lines 117-118 Shows the disabling of the sprite

ldy #$00


As it would turn out lines 68-69 happened to turn on Sprite 0, and lines 117-118 happened to turn off Sprite 0.  But they did not actually do that!!!!!!!!

In reality, Sending $01 to SPENA said "TURN OFF ALL SPRITES EXCEPT FOR SPRITE 0"
and sending it the $00 said "TURN OFF ALL 8 SPRITES"

To understand why, lets have a look at our memory map for SPENA ($D015)

Sprite Enable Register

Bit 0:  Enable Sprite 0 (1=sprite is on, 0=sprite is off)
Bit 1:  Enable Sprite 1 (1=sprite is on, 0=sprite is off)
Bit 2:  Enable Sprite 2 (1=sprite is on, 0=sprite is off)
Bit 3:  Enable Sprite 3 (1=sprite is on, 0=sprite is off)
Bit 4:  Enable Sprite 4 (1=sprite is on, 0=sprite is off)
Bit 5:  Enable Sprite 5 (1=sprite is on, 0=sprite is off)
Bit 6:  Enable Sprite 6 (1=sprite is on, 0=sprite is off)
Bit 7:  Enable Sprite 7 (1=sprite is on, 0=sprite is off)

You see, the hex value is a summation of individual bits turned on and off to reflect which sprites are on or off.  In sending our HEX value $01 we essentially set this memory address as


Bit 0 the LSB was turned on, thus enabling the sprite. and that is why it worked.  $00 set all bits to off and Sprite0 was disabled


So if we were to send an $02, what would happen is we would be enabling the Sprite1, but, disabling sprite0.


If we wanted sprite0 active with sprite1, we would actually have to send an #03


 Making it far worse, enabling sprites 0, 5, and 7 would require a hex value of $A1


If you haven't figured it out by now, hex seems like a poor choice for bit manipulation.   In contrast, it seems that binary is much easier to read and work with in these instances.  Luckily, we can use binary in our code instead of hex, in these areas where binary would be a better fit.  And its easy to implement.

Lets change lines 68-69 shows the enabling of the sprite using the % sign instead of the #

DISREGARD THE SPACE AFTER %, the browser won't format it correctly without a space

lda #$01


lda #% 00000001

Then, lets change Lines 117-118 Shows the disabling of the sprite

lda #$00



ldy #% 00000000

The compiled machine code will look exactly the same, the readability within the code would be greatly improved.  But there are even more advantages.  For instance, lets turn on sprites 7 through 4, while shutting off 3 through 0.

ldy #%11110000

Done!!!   That was much easier than hex.   A quick, more advanced demonstration could be something like, turn off all sprites that are on, and turn on all sprites that are off


eor #%11111111

While, I don't want to get into logical operations yet, I thought it would be appropriate to cite it as an example, since much of the code we want displayed in binary will be used in logical operations.

We used EOR Exclusive-OR Memory with Accumulator to accomplish this with a mask of FF or in binary as 11111111.   With this mask, EOR turns any 0 to a 1 and any 1 to a zero. All logical operations have to be done in the A register

Understanding 6502 assembly on the Commodore 64 - (10) Disappearing, Reappearing BASIC

Table of contents

No comments:

Post a Comment