6502.org Forum  Projects  Code  Documents  Tools  Forum
It is currently Fri Sep 20, 2024 4:13 am

All times are UTC




Post new topic Reply to topic  [ 5 posts ] 
Author Message
 Post subject: Stack Suff
PostPosted: Sun Aug 20, 2017 8:45 pm 
Offline

Joined: Sun Aug 20, 2017 8:28 pm
Posts: 2
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?


Top
 Profile  
Reply with quote  
 Post subject: Re: Stack Suff
PostPosted: Sun Aug 20, 2017 9:03 pm 
Offline
User avatar

Joined: Thu Dec 11, 2008 1:28 pm
Posts: 10938
Location: England
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.


Top
 Profile  
Reply with quote  
 Post subject: Re: Stack Suff
PostPosted: Sun Aug 20, 2017 9:34 pm 
Offline
User avatar

Joined: Fri Aug 30, 2002 1:09 am
Posts: 8510
Location: Southern California
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?


Top
 Profile  
Reply with quote  
 Post subject: Re: Stack Suff
PostPosted: Sun Aug 20, 2017 11:33 pm 
Offline

Joined: Sun Aug 20, 2017 8:28 pm
Posts: 2
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


Top
 Profile  
Reply with quote  
 Post subject: Re: Stack Suff
PostPosted: Wed Aug 23, 2017 12:03 am 
Offline

Joined: Thu Jan 21, 2016 7:33 pm
Posts: 275
Location: Placerville, CA
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.)


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 23 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: