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.
JSR problem on Atari 2600
Re: Atari 2600
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.
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
In fact, it might be less strain on the brain to use locations
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.
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.
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.
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
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.
-
boriskarloff
- Posts: 7
- Joined: 14 Nov 2021
Re: JSR problem on Atari 2600
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?
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?
Re: JSR problem on Atari 2600
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.
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.
-
boriskarloff
- Posts: 7
- Joined: 14 Nov 2021
Re: JSR problem on Atari 2600
Ok I have understood the problem. In previous code I used TXS / TSX.
Thanks for help me to understand.
Thanks for help me to understand.