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
sta SPENA
Lines 117-118 Shows the disabling of the sprite
ldy #$00
sty SPENA
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)
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
00000001
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
00000000
So if we were to send an $02, what would happen is we would be enabling the Sprite1, but, disabling sprite0.
00000010
If we wanted sprite0 active with sprite1, we would actually have to send an #03
00000011
Making it far worse, enabling sprites 0, 5, and 7 would require a hex value of $A1
10100001
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
sta SPENA
to
lda #% 00000001
sta SPENA
Then, lets change Lines 117-118 Shows the disabling of the sprite
lda #$00
sta SPENA
to
ldy #% 00000000
sty SPENA
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
sty SPENA
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
lda SPENA
eor #%11111111
sta SPENA
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
NEXT----->
Table of contents
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
sta SPENA
Lines 117-118 Shows the disabling of the sprite
ldy #$00
sty SPENA
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
00000001
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
00000000
So if we were to send an $02, what would happen is we would be enabling the Sprite1, but, disabling sprite0.
00000010
If we wanted sprite0 active with sprite1, we would actually have to send an #03
00000011
Making it far worse, enabling sprites 0, 5, and 7 would require a hex value of $A1
10100001
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
sta SPENA
to
lda #% 00000001
sta SPENA
Then, lets change Lines 117-118 Shows the disabling of the sprite
lda #$00
sta SPENA
to
ldy #% 00000000
sty SPENA
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
sty SPENA
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
lda SPENA
eor #%11111111
sta SPENA
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
NEXT----->
Table of contents
No comments:
Post a Comment