Jump to content

Photo

Using PLA for sleeping


9 replies to this topic

#1 JeremiahK OFFLINE  

JeremiahK

    Chopper Commander

  • 187 posts
  • Location:Indiana, USA

Posted Tue May 15, 2018 10:12 AM

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.



#2 TheHoboInYourRoom OFFLINE  

TheHoboInYourRoom

    Moonsweeper

  • 402 posts
  • Whackadoo thingamajig
  • Location:Illinois

Posted Tue May 15, 2018 12:12 PM

The Atari 2600 Advanced Programming Guide on the MiniDig has a nice section "Wasting Cycles" that describes compact ways of wasting between 1 and 12 cycles. It might be useful for you in the future. :)



#3 JeremiahK OFFLINE  

JeremiahK

    Chopper Commander

  • Topic Starter
  • 187 posts
  • Location:Indiana, USA

Posted Tue May 15, 2018 12:31 PM

I love that guide, lots of handy stuff in there. It never mentions using PLA without a PHA, though, so I thought I would make a post about it.

#4 Thomas Jentzsch OFFLINE  

Thomas Jentzsch

    Thrust, Jammed, SWOOPS!, Boulder Dash, THREE·S, Star Castle

  • 23,544 posts
  • Always left from right here!
  • Location:Düsseldorf, Germany, Europe, Earth

Posted Tue May 15, 2018 2:13 PM

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

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

Edited by Thomas Jentzsch, Tue May 15, 2018 2:14 PM.


#5 DirtyHairy OFFLINE  

DirtyHairy

    Moonsweeper

  • 466 posts
  • Location:Germany

Posted Tue May 15, 2018 3:08 PM

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


Edited by DirtyHairy, Tue May 15, 2018 3:14 PM.


#6 JeremiahK OFFLINE  

JeremiahK

    Chopper Commander

  • Topic Starter
  • 187 posts
  • Location:Indiana, USA

Posted Tue May 15, 2018 11:04 PM

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, Tue May 15, 2018 11:07 PM.


#7 JeremiahK OFFLINE  

JeremiahK

    Chopper Commander

  • Topic Starter
  • 187 posts
  • Location:Indiana, USA

Posted Mon May 21, 2018 9:29 PM

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!"


#8 Kiwi ONLINE  

Kiwi

    Stargunner

  • 1,590 posts

Posted Mon May 21, 2018 11:51 PM

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.



#9 TheHoboInYourRoom OFFLINE  

TheHoboInYourRoom

    Moonsweeper

  • 402 posts
  • Whackadoo thingamajig
  • Location:Illinois

Posted Tue May 22, 2018 10:32 AM

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!



#10 Sheddy ONLINE  

Sheddy

    Dragonstomper

  • 735 posts
  • Location:UK

Posted Tue May 22, 2018 11:13 AM

If you're pla/plp ing the stack often enough for it to be worth it, you can always save the stack pointer first. It's not like the stack data dissappears by doing a pla/plp (but careful with any pha/php then!)




0 user(s) are browsing this forum

0 members, 0 guests, 0 anonymous users