Stack Suff

Building your first 6502-based project? We'll help you get started here.
Post Reply
NESguy
Posts: 2
Joined: 20 Aug 2017

Stack Suff

Post by NESguy »

So I'm relatively new at this whole "controlling the processor on a miniscule level where you can change things integral to the thing running properly" thing, and have a small question. Tutorials I've found never seem to have any problems with just shoving things into the stack, and I seem to be missing something since to me it seems absolutely absurd to even touch something so integral for functioning subroutines/JSR as that. Any idea what I might be missing here?
User avatar
BigEd
Posts: 11463
Joined: 11 Dec 2008
Location: England
Contact:

Re: Stack Suff

Post by BigEd »

Welcome! You do have to be pretty careful with the stack: in between calls and returns you need to push and pop exactly the same number of bytes. But so long as you do that, you're fine - the stack is just some memory with automatic addressing, it doesn't know anything about what the data means.

Sometimes it's useful to be just a bit more involved: knowing what RTS is going to do, you can push two bytes and then RTS, and you've just something rather like a computed jump.

Hope this helps.
User avatar
GARTHWILSON
Forum Moderator
Posts: 8773
Joined: 30 Aug 2002
Location: Southern California
Contact:

Re: Stack Suff

Post by GARTHWILSON »

There's a lot more power in the stack than meets the eye. You can do a load of things with it, and have lots of things pending, and as Ed said, as long you know what you're doing, nothing gets messed up. I have a treatise on 6502 stacks at http://wilsonminesco.com/stacks/ since forum posts showed the need for it. It addresses the range of subjects regarding stacks, starting with the definition and gradually reaching a stage a little bit past intermediate use, mostly ignoring the loftier subjects like multi-user, multithreading, and multitasking systems (which the 6502 is not very well suited for anyway).

Here's a list of chapters:
  • definition and very basics
  • subroutine return addresses and nesting
  • interrupts
  • virtual stacks and various ways to implement them
  • stack addressing, both hardware and virtual
  • passing parameters, and comparison of methods
  • having a subroutine find inlined data, using the return address
  • doing math and other operations by stacks in RPN
  • RPN efficiency
  • 65c02's added instructions that are useful in stacks
  • using RTS, RTI, and JSR to synthesized other instructions
  • where-am-I routines, for self-relocatable code
  • a peek at the 65816's new instructions and capabilities that are relevant to stacks, and 65c02 code which partially synthesizes some of them
  • local variables and environments
  • recursion
  • enough stack space?
  • compiling or assembling program structures
  • stack potpourri
  • for further reading
plus appendices.

Start at the beginning and go as far as you're comfortable.

Happy programming!
http://WilsonMinesCo.com/ lots of 6502 resources
The "second front page" is http://wilsonminesco.com/links.html .
What's an additional VIA among friends, anyhow?
NESguy
Posts: 2
Joined: 20 Aug 2017

Re: Stack Suff

Post by NESguy »

The "virtual stacks" section and the advice from both of you helped, thanks! I was trying to use this as a graphics data buffer with the NES's VBLANK and NMI, and the tutorials I saw out there... aren't the best about this
User avatar
commodorejohn
Posts: 299
Joined: 21 Jan 2016
Location: Placerville, CA
Contact:

Re: Stack Suff

Post by commodorejohn »

NESguy wrote:
I was trying to use this as a graphics data buffer with the NES's VBLANK and NMI, and the tutorials I saw out there... aren't the best about this
Yeah, I remember puzzling over that when I was dabbling in NES development as well. The reasoning behind that approach is that it allows you to use non-VBlank time to queue up a pile of data for the PPU ahead of time, so that you can maximize throughput during the limited VBlank time. Since PLA is one of the quickest instructions on the 6502, it means you can pair up PLA/STA portaddress instructions to mass-transfer a bunch more data than a more typical LDA source, X/STA portaddress approach would achieve.

Still, it's a bit counter-intuitive and I'm not sure how much your typical NES game really needs to transfer in a single frame (especially since it's really only tilemap data and non-ROM tile definitions that are stored in main PPU RAM - the sprite table is separate and has its own DMA facility.)
Post Reply