6502.org Forum  Projects  Code  Documents  Tools  Forum
It is currently Wed Jul 03, 2024 7:57 am

All times are UTC




Post new topic Reply to topic  [ 5 posts ] 
Author Message
PostPosted: Sun Nov 14, 2021 1:05 pm 
Offline

Joined: Sun Nov 14, 2021 12:53 pm
Posts: 7
Hi, I am programming on Atari 2600. There is a problem is driving me crazy.
When I use JSR instruction, the return addres is written in memory location assigned for other things and not in the SP.
This create bugs.
Is there some reason for this?
Thanks.


Top
 Profile  
Reply with quote  
 Post subject: Re: Atari 2600
PostPosted: Sun Nov 14, 2021 1:28 pm 
Offline
User avatar

Joined: Thu Dec 11, 2008 1:28 pm
Posts: 10838
Location: England
Hi, and welcome!

The Atari 2600 is very unusual in having only 128 bytes of RAM. So, although the CPU will expect a full 256 bytes of page 0 and a full 256 bytes of page 1 (for the stack) in fact these 512 addresses will be folded on top of each other.

    So address 00 and 80 and 100 and 180 are all the same location, the same byte.
    And so on, up to address 7F and FF and 17F and 1FF which are all the same.

So, to keep all your variables separate, and also to keep your stack in a different place, you need to be extra careful with your choice of which addresses to use.

It's normal to initialise S, the stack pointer, to FF. And so, if you allow a generous 32 bytes for stack usage - which is to say, you are careful never to exceed 32 bytes - that leaves you 96 bytes for zero page use. You could consider that as
    locations 00 to 5F are your zero page
    locations 1E0 to 1FF are your stack.
And you see that these two ranges do not conflict, even though everything appears four times.

In fact, it might be less strain on the brain to use locations
    80 to DF in zero page
    1E0 to 1FF in page one for the stack
because it's more obvious how these don't overlap.

I hope this helps!

Edit: It's important to note that S, the stack pointer, is a single byte, which points to page one for the stack. That is, the stack is not on-chip, it is off-chip. Only the stack pointer is on chip, and it is combined with 01 to make up an address. When S is FF, the next stack location is at 01FF, and the stack grows downwards. So the first JSR you use would store the return address in locations 01FF and 01FE, then S would become FD, and 01FD is the next free location.


Top
 Profile  
Reply with quote  
PostPosted: Sun Nov 14, 2021 1:44 pm 
Offline

Joined: Sun Nov 14, 2021 12:53 pm
Posts: 7
Thanks for the reply. I'll try to understand all that you write me.
For now, I am watching Stella in debugger mode. Yes the SP address, uses address used for store other things.
Must I set the SP to $FF? The $FF memory address is not the default address for it?


Top
 Profile  
Reply with quote  
PostPosted: Sun Nov 14, 2021 1:47 pm 
Offline
User avatar

Joined: Thu Dec 11, 2008 1:28 pm
Posts: 10838
Location: England
I think with all the early NMOS 6502s, you do need to initialise the stack pointer. Typical reset code will do
LDX #$FF
TXS

If your emulator happens to initialise SP, but if real hardware does not, you will probably have trouble when you run on real hardware.


Top
 Profile  
Reply with quote  
PostPosted: Sun Nov 14, 2021 2:04 pm 
Offline

Joined: Sun Nov 14, 2021 12:53 pm
Posts: 7
Ok I have understood the problem. In previous code I used TXS / TSX.
Thanks for help me to understand.


Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 5 posts ] 

All times are UTC


Who is online

Users browsing this forum: No registered users and 4 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to: