Author Topic: Using APD in your program  (Read 6575 times)

0 Members and 1 Guest are viewing this topic.

Offline E37

  • LV6 Super Member (Next: 500)
  • ******
  • Posts: 358
  • Rating: +23/-0
  • Trial and error is the best teacher
    • View Profile
Using APD in your program
« on: June 27, 2019, 08:19:37 pm »
I did some research and experimentation and I found out how to use the built in Auto Power Down (APD) feature in an assembly program. I couldn't really find any kind of relevant documentation on WikiTi so I just looked at the memory areas that looked related and tried some things until I got something that works. Since I found this all out by experimentation, there is a chance that I could be totally wrong and that the calc doesn't go into low power mode or something but I'm pretty sure that I am correct. I guess the only way to tell for sure is for me to stick fresh batteries in a calc and run it in an assembly program until it dies and then to do the same thing for my APD but that could take weeks to a month.

The routiene in Axe (but it is very easy to translate to assembly if you want):
Code: [Select]
:GOff() ;If you are using graylib or any custom interrupt, turn it off here. If not, skip this step
:*save 0x8000* ;The APD destroys 256 bytes starting at 0x8000 so if you use that area for storage, you will need to back it up. If not, you don't have to do anything.
:0x180C->{0x89F8}r ;Set some flags so that it works
:0x0101->(0x8448)r ;Set the APD timer to right about to shut off
:Asm(FF) ;Call the OS interrupt (This is 'rst 38h' in assembly)
:FnOn ;Interrupts should be on at this point because of the last line but it doesn't hurt to be very sure. (This is 'ei' in assembly)
:Stop ;Wait until the OS interrupt fires. It is at this point that the calc actually turns off (This is 'halt' in assembly)
:0x7469->{0x8448}r ;Set the APD timer to what it normally is so the calc doesn't instantly APD when you quit the program
:0x0804->{0x89F8}r ;Restore some flags
:*restore 0x8000* ;Restore the values you saved. If you didn't save anything you of course don't need to restore it
:GOn() ;If you are using graylib or any custom interrupt, turn it back on here

This routiene turns the calc off as soon as you call it which means if you want your program to turn off after no keys have been pressed for a while, you will have to do that yourself. It works fine if the On key is being held when it starts (which means if you have the user press On to turn the calc off you don't have to wait for them to release On to call this) It doesn't wait for the On key to be released when it is finished so I would put a loop at the end of it to wait until the On key is released.

In my experiments with this it worked fine with all sorts of memory screwed up so as long as you save 0x8000 then you shouldn't have to do any other kind of setup. I don't know exactly what flags I am messing with (since I am too lazy to look up what they are - something like iy + onflags I think) in the two flags steps but I know they are nessicary and that there doesn't seem to be any kind of ill effect from setting them in that way.
I'm still around... kind of.

Offline Xeda112358

  • they/them
  • Moderator
  • LV12 Extreme Poster (Next: 5000)
  • ************
  • Posts: 4704
  • Rating: +719/-6
  • Calc-u-lator, do doo doo do do do.
    • View Profile
Re: Using APD in your program
« Reply #1 on: June 27, 2019, 08:41:02 pm »
This is clever, nice work!

Offline E37

  • LV6 Super Member (Next: 500)
  • ******
  • Posts: 358
  • Rating: +23/-0
  • Trial and error is the best teacher
    • View Profile
Re: Using APD in your program
« Reply #2 on: October 18, 2019, 08:41:17 am »
When you use this to shut the calc off and pull the batteries while it is off, when you put the batteries back in and turn it on, the calc won't crash but just go to the homescreen. (as opposed to normally when you pull a battery it definitely crashes when you turn on)
Is there some RAM area that if you set it to a specific value it will crash on the homescreen? It needs to be easily undone since in most cases the user isn't going to pull a battery and is just going to turn it back on normally. I know that you can just start writing garbage to the VAT but that isn't reversible. I guess I'm looking for something like "just set the size of prgm! to some impossibly large number and that will crash"
I'm still around... kind of.

Offline Xeda112358

  • they/them
  • Moderator
  • LV12 Extreme Poster (Next: 5000)
  • ************
  • Posts: 4704
  • Rating: +719/-6
  • Calc-u-lator, do doo doo do do do.
    • View Profile
Re: Using APD in your program
« Reply #3 on: October 18, 2019, 10:36:34 am »
As long as the user doesn't disable the RAM execution limit, you could `jp $C000` and that automatically crashes the calc.

Offline Sue Doenim

  • LV2 Member (Next: 40)
  • **
  • Posts: 27
  • Rating: +0/-0
    • View Profile
Re: Using APD in your program
« Reply #4 on: October 18, 2019, 11:08:21 am »
Doesn't RST $00 trigger a reset?

Offline E37

  • LV6 Super Member (Next: 500)
  • ******
  • Posts: 358
  • Rating: +23/-0
  • Trial and error is the best teacher
    • View Profile
Re: Using APD in your program
« Reply #5 on: October 18, 2019, 11:08:43 am »
As long as the user doesn't disable the RAM execution limit, you could `jp $C000` and that automatically crashes the calc.
I have no problems resetting ram. Im trying to set up memory so that IF the os regains control, the calc will crash but if the programs gets control I can recover and easily undo whatever I did. Like setting the floating point stack to somewhere random. That wouldn't result in an instant crash for a program (and is easy to set back to the right value) but if the OS got control with the pointer to the floating point stack set to some garbage location it would crash. (Im not entirely sure that is how the floating point stack works since I have never really messed with it)
I'm still around... kind of.

Offline Xeda112358

  • they/them
  • Moderator
  • LV12 Extreme Poster (Next: 5000)
  • ************
  • Posts: 4704
  • Rating: +719/-6
  • Calc-u-lator, do doo doo do do do.
    • View Profile
Re: Using APD in your program
« Reply #6 on: October 18, 2019, 11:45:40 am »
Oh, I see what you mean. I know there must be a better way, but maybe set up a context hook that is just
Code: [Select]
.db $83
rst 0
This way it always crashes if the "context" changes.
http://wikiti.brandonw.net/index.php?title=83Plus:BCALLs:502C

The "apps" that it refers to are, for example, the program editor, homescreen, and others. You can find them in the ti83plus.inc file in the section of equates that start with "cx" (not "_cx" !)