Jump to content
IGNORED

Using PLA for sleeping


Recommended Posts

I'm sure somebody else has been doing this already, but I just thought of it yesterday and wanted to share!

 

I am currently working on the menu kernel for my game, and I had a "SLEEP 19" in the kernel. (I honestly can't believe I have that much wiggle room, but that is during the simplest point of the kernel!). Since here, SLEEP just inserts 8 NOP's and a "NOP 0" or "BIT VSYNC", that is 10 bytes of ROM. Of course, for longer sleep durations, you could set up a BNE loop, but I am using both index registers at this point. Instead, I am just using PLA, since I am not using the stack pointer. Since you are not writing to the stack, there is no danger of corrupting your TIA registers or any RAM. It uses 4 cycles, and only 1 byte of ROM, allowing me to save 4 bytes of ROM. Plus I am using it again later to save another single byte of ROM.

 

Of course, when I have to reset the stack, it will take another 3 bytes, so it doesn't save much in this case, but it can surely be helpful if you need it.

Link to comment
Share on other sites

Yea, most programmers like to keep the stack pointer intact. :)

 

Yep. As I said, this only works if the situation allows, and you would need to reset the stack pointer afterwards. It's one of those ugly methods that is a terrible idea, but works well if you know what you're doing, which fits right in to the craziness of 2600 programming. :D

 

Provided you have a RTS sticking around somewhere, you can use JSR to waste 12 cycles with three bytes; that’s four cycles per byte, too, but without messing up your stack :)

 

:thumbsup: :thumbsup: :thumbsup:

 

Another idea I've had that I've never actually used would be something like this:

Sleep48
    jsr Sleep12    ; sleep 12 cycles, then continue
Sleep36
    jsr Sleep12    ; sleep 12 cycles, then continue
Sleep24
    jsr Sleep12    ; sleep 12 cycles, then continue
Sleep12
    rts

This would have to be its own subroutine, rather than just using the end of another one. It would probably almost never be worth using in actual code, unless you were writing a kernel that needed a ton of long SLEEP durations that couldn't be done with loops. But that seems unlikely, since usually you have the opposite problem of not having enough cycles.

Edited by JeremiahK
Link to comment
Share on other sites

It never mentions using PLA without a PHA, though, so I thought I would make a post about it.

 

Took a closer look, turns out I was wrong, it does mention it:

"You can use PHA/PHP (1 byte 3 cycles) or PLA/PLP (1 byte 4 cycles) alone but you have to be carefull not to mess up your stack (PLP/PHA would be usefull if you have no stack!"
Link to comment
Share on other sites

Yeah, if you have subroutines. It automatically push and pull from the stack. I have used 2 pla and a rts to avoid going back to the original sender, but to go back to the main game loop when it was 2 level deep.

Oh my gosh, that never occurred to me. That cracks the puzzle of one of my projects. Thank you!

  • Like 1
Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...