Page 1 of 1

ca65 range error when calculating address

Posted: Wed Sep 01, 2021 5:57 pm
by reesey-spoon
I am using calculated addresses to push onto the stack and return from, but when I use

Code: Select all

addressTableH
    .byte >address00, etc
addressTableL
    .byte <address00-1, etc
I get a range error (not within 255) whenever the lowbyte of an address is 0 and causes an underflow. I am sure there is a simple way around this, I can't find it though.

Re: ca65 range error when calculating address

Posted: Wed Sep 01, 2021 6:07 pm
by hoglet
reesey-spoon wrote:
I get a range error (not within 255) whenever the lowbyte of an address is 0 and causes an underflow. I am sure there is a simple way around this, I can't find it though.
Try this:

Code: Select all

addressTableH
    .byte >(address00-1), etc
addressTableL
    .byte <(address00-1), etc

Re: ca65 range error when calculating address

Posted: Wed Sep 01, 2021 6:10 pm
by reesey-spoon
hoglet wrote:
Try this:

Code: Select all

addressTableH
    .byte >(address00-1), etc
addressTableL
    .byte <(address00-1), etc
Thank you! I'll try it tonight

Re: ca65 range error when calculating address

Posted: Wed Sep 01, 2021 7:08 pm
by BigDumbDinosaur
hoglet wrote:
Try this:

Code: Select all

addressTableH
    .byte >(address00-1), etc
addressTableL
    .byte <(address00-1), etc

Yuh beat me to it! :D

Re: ca65 range error when calculating address

Posted: Sun Sep 26, 2021 6:25 pm
by reesey-spoon
hoglet wrote:
Try this:

Code: Select all

addressTableH
    .byte >(address00-1), etc
addressTableL
    .byte <(address00-1), etc

I'm guessing the -1 NEEDS to be on both the High and Low address. I have been hunting down a bug for the past few days and I think its because, after reading your advice, I only but it on the low byte side.

Re: ca65 range error when calculating address

Posted: Sun Sep 26, 2021 8:11 pm
by drogon
reesey-spoon wrote:
hoglet wrote:
Try this:

Code: Select all

addressTableH
    .byte >(address00-1), etc
addressTableL
    .byte <(address00-1), etc

I'm guessing the -1 NEEDS to be on both the High and Low address. I have been hunting down a bug for the past few days and I think its because, after reading your advice, I only but it on the low byte side.
Indeed - so say for example, the target address is $8000, then >$8000 is $80 and <$8000 is $00, but by just taking 1 from the low byte you get: >$8000 = $80 and <$7FFF = $FF, so the resulting address is $80FF which isn't what you want, so >$7FFF = $7F and <$7FFF is $FF which is what you want.

So you have an address that lands on a page boundary $xx00 and it crashes. This is a tricky thing to fix as simply adding in debug code can change the target address which may then cause it to work! it's only when address00 is $xx00 that you'll have issues.


-Gordon

Re: ca65 range error when calculating address

Posted: Sun Sep 26, 2021 11:01 pm
by reesey-spoon
drogon wrote:

Indeed - so say for example, the target address is $8000, then >$8000 is $80 and <$8000 is $00, but by just taking 1 from the low byte you get: >$8000 = $80 and <$7FFF = $FF, so the resulting address is $80FF which isn't what you want, so >$7FFF = $7F and <$7FFF is $FF which is what you want.

So you have an address that lands on a page boundary $xx00 and it crashes. This is a tricky thing to fix as simply adding in debug code can change the target address which may then cause it to work! it's only when address00 is $xx00 that you'll have issues.


-Gordon
Yes I found this out the hard way. My program was hanging at "random" intervals. Took me quite some time to realize it was unrelated to what I was working on at the moment. Thank you for all your help