Re: [stella] Stella sequencing

Subject: Re: [stella] Stella sequencing
From: Lord Spambraticus of Borg <lord-of-hell@xxxxxxxxxxxxxxxx>
Date: Mon, 04 Jan 1999 22:11:15 +1000
On Sun, 03 Jan 1999 23:51:36 -0600 jkharvey@xxxxxxxxxxxxxxxxx (John Harvey)
wrote:

>Note: I could be wrong about some of this.  If so, please correct me.
>
>An interesting thought entered my mind when writing some code yesterday.
>Basically, I was writing a subroutine within a subroutine (JSR's) and it
>compiled and actually ran on the PCAE emulator.  

Sure, why not? :)

> I was under the impression that in the event of a subroutine, the return 
> address of the subroutine is saved in the program counter (I could be wrong,
> I read this a while ago), until the event of an RTS.  

No, what happens is the current PC (note, each fetch increments the PC, so the
PC DURING the execution of JSR $1234 is actually at the next instruction) is
copied to the (SP) and the SP is decremented by 2.  Note the S-register is the
low 8 bits of the stack pointer address, and on the VCS2600, it's wired to $00
at the high-byte.  So the stack pointer is: $00SS, with the caveat that only
$80-$FF have RAM.  

> Is this just a fluke?  There is no system stack that I know of, unless, $FF 
> thu $80 are automatically called, 

Bingo, baby.  Yes, $80->$FF is the stack, with $ff being the "beginning" or
"top" of the stack.  While I imagine the CPU starts S off at $FF, I'd imagine a
properly written RESET routine to initialise the register (LDX #$FF, TXS)
appropriately.

> and the stack pointer incremented, but I do not think this is so.

It's decremented.  This is why you allocate your own scratch variables
ascendingly ($80 and up), whilst you have the stack "crawling" downwards from
$FF, and as the programming guide mentions "hope they don't meet" :)

On *ALL* 6502-derived CPUs (aside from the 16 and 32 bit evolved 65816 etc), the
stack register is only 8-bits wide.  On the Apple ][, it was located at
$100...$1ff if memory serves.

> If it is possible to have a nested loop, what is the limit?  

Well, assuming no other scratch RAM usage (not a very useful program)... you
could have 64 nested subroutine calls. (128 bytes RAM / 2 bytes per return
address).

>RTS's on the'r own, would the program get confused and ignore the command,
>or keep going, somehow finding the corresponding JSR?

Um, if you're asking what would happen if you kept calling deeper subroutines?
The stack would "wrap" and your program would behave VERY strangely, or crash.

>Also, (if anyone knows), does an emulator "fix" programming mistakes?  Are
>there things that I could program on an emulator that would not work on a
>real VCS?  

Well, in the aspect of this question of yours, since emulators probably provide
"unlimited" resources, such as RAM (if need be), it may obscure (or even cover
up) blatant programming mistakes that would seize fast on a real VCS.  (I
actually don't have the data to say for certain, however, since so many
incarnations of VCS runtime envrionments exist, I imagine Stella and other
emulators "can provide" tons more than 128 bytes of RAM and 4k of ROM, etc.

> it seems that I am one of the "young
>'uns" on the list, as my knowledge is not yet so immense.  Thank you to all
>for putting up with me.

Nobody is born with VCS2600 knowledge - we all were in your place at some point,
and many of us still are. :)

=Rob=


--
Archives (includes files) at http://www.biglist.com/lists/stella/archives/
Unsub & more at http://www.biglist.com/lists/stella/

Current Thread