Hi,
So here is the issue ... ask a dozen programmers, get a bakers dozen answers...
A few things here and one is picking a different way to do it.
So the issue is to fill a section of RAM with a fixed value. For your range: $200 through $5FF, personally, I'd un-wind the loop and not use a zero page pointer at all. This is at the expense of code size, but the advantage is speed. Much faster, so:
Code:
; fill:
; Value passed in A
fill:
ldy #0
fill1:
sta $0200,y
sta $0300,y
sta $0400,y
sta $0500,y
iny
bne fill1
rts
Actually, looking at that now, it might even be smaller than a conventional pointer based fill + increment + compare type loop.
Tackling the original scenario, break it down, so the increment:
Code:
; inc16:
; Add 1 to a 16-bit pointer in zero page
inc16:
inc ptr
bne :+
inc ptr+1
: rts
You can see there that there is no need to do a compare to handle the wrap round because the 6502 explicitly sets condition flags based on the last operation. In this scenario, when we increment 'ptr' (the low byte) when it increments from $FF through to $00, then the Zero flag will be set, so we can simply test for it.
The compare part (in the original scenario) can simply be to test against $600 - this is easy (a single byte compare) but needs the loop to work like:
Code:
mainFill:
ldy #$02
sty ptr+1 ; High byte
ldy #$00
sty ptr ; Low byte (leaves Y=0)
mainFill1:
sta (ptr),y
jsr inc16
lda ptr+1 ; High byte
cmp #$06
bne mainFill1
rts
If you are using as 65C02 then this can be further optimised, but on simple way would be to in-line the call to inc16 - that'll save a jsr/rts, but will add a few more bytes to the code. Also look at Garths example above where he uses Y as an index in conjunction with the pointer. (Told you there was more than one way to do it
It may not be as fast as the original one I write though, but might give you some ideas of different ways to accomplish the same thing.
Finally - do note that if you're looking at a lot of old 6502 code, then it's often been written to optimise for space rather than speed. Mask programmable ROMs were expensive back then...
Cheers,
-Gordon
_________________
--
Gordon Henderson.
See my
Ruby 6502 and 65816 SBC projects here:
https://projects.drogon.net/ruby/