What happens if you index past the end of 64k address space?

Discuss technical or other issues relating to programming the Nintendo Entertainment System, Famicom, or compatible systems. See the NESdev wiki for more information.

Moderator: Moderators

Post Reply
User avatar
GradualGames
Posts: 1106
Joined: Sun Nov 09, 2008 9:18 pm
Location: Pennsylvania, USA
Contact:

What happens if you index past the end of 64k address space?

Post by GradualGames »

If a game attempts to access a location past the end of 64k address space with indirect addressing, does this wrap all the way back to ZP, or only within the last page?
User avatar
dougeff
Posts: 3079
Joined: Fri May 08, 2015 7:17 pm

Re: What happens if you index past the end of 64k address sp

Post by dougeff »

WDC programming the 65816 manual, page 98
On the 6502, 65C02, and 65802, if an index plus its base would exceed $FFFF, it wraps to continue
from the beginning of the 64K bank zero; that is, when index is added to base, any carry out of the low-order
sixteen bits lost.
The next one past ffff is 0000, yes, zero page.
nesdoug.com -- blog/tutorial on programming for the NES
User avatar
GradualGames
Posts: 1106
Joined: Sun Nov 09, 2008 9:18 pm
Location: Pennsylvania, USA
Contact:

Re: What happens if you index past the end of 64k address sp

Post by GradualGames »

*warns the programmer that bad things are happening*
User avatar
rainwarrior
Posts: 8734
Joined: Sun Jan 22, 2012 12:03 pm
Location: Canada
Contact:

Re: What happens if you index past the end of 64k address sp

Post by rainwarrior »

The same thing happens with ZP indexing too, by the way. (The relevant instructions wrap within the ZP, they won't go into $1XX) I don't think this comes up very often though.

There's also a weird wrap with indirect pointers placed at $FF, or JMP ($XXFF) where the high byte doesn't get incremented to fetch the second half of the pointer. i.e. ($FF) is the pointer at $00:$FF, not $100:$FF. (Probably comes up even less often.)
User avatar
Bregalad
Posts: 8056
Joined: Fri Nov 12, 2004 2:49 pm
Location: Divonne-les-bains, France

Re: What happens if you index past the end of 64k address sp

Post by Bregalad »

GradualGames wrote:If a game attempts to access a location past the end of 64k address space with indirect addressing, does this wrap all the way back to ZP, or only within the last page?
I don't think there's any question to add there. The same principle of $ff + $01 = $00 applies everywhere in digital electronics, it would be totally weird to have it stick to $ff. Such a behaviour is totally unnatural and requires extra circuitery to work. Some advanced CPUs have instructions to do such calculation, which are useful for example when mixing audio. However it is always explicitely mentionned when such a saturation is implemented. There for your question is a non-question - obviously going past $ffff will roll back to $0000.
Post Reply