Author Topic: Routines  (Read 296219 times)

0 Members and 1 Guest are viewing this topic.

SirCmpwn

  • Guest
Re: Routines
« Reply #135 on: March 21, 2010, 12:37:37 pm »
Let me see if i understand the concept behind the global data concept.  When the program is run, it is copied to RAM somewhere safe where the TiOS can run it.  After it is finished executing, it is NOT copied back, making the program effectively write protected :/. So all we need to do is write a little bit of code to find out where the program acualy located (the first hex code) and store it in a safe place (L4) until the end of the program where we use it.  Once we get to the end, we merely copy the program right back to where it started again, so that any changes we made are permanent.  We are not using any of the L1-L6 variables for anything more than a 4 byte address storage during the program execution, so there is no possibility that we could run out of memory due to large programs.

(did i get that correctly?)

Yeah, calc84 is right.  When you first run a program, TIOS stores the name of it to OP1 (a RAM area).  I read OP1 and store it to the end of L4.  That is what the first routine does.  Only 9 bytes.
« Last Edit: March 21, 2010, 12:39:19 pm by SirCmpwn »

Offline DJ Omnimaga

  • Clacualters are teh gr33t
  • CoT Emeritus
  • LV15 Omnimagician (Next: --)
  • *
  • Posts: 55942
  • Rating: +3154/-232
  • CodeWalrus founder & retired Omnimaga founder
    • View Profile
    • Dream of Omnimaga Music
Re: Routines
« Reply #136 on: March 21, 2010, 01:24:37 pm »
For some reasons, I am even more confused after reading Builderboy post. I think I'll just give up on the whole Self-modifying code concept and wait until Quigibo implements an axe feature that allows us to do so in an easier to understand manner (if it's even possible for those used to using SMC the CelticIII/Y= way), or wait until external variable storage/reading (appvar maybe?) is supported
Now active at https://discord.gg/cuZcfcF (CodeWalrus server)

SirCmpwn

  • Guest
Re: Routines
« Reply #137 on: March 21, 2010, 01:42:59 pm »
If you don't want to read a huge post, the juicy stuff is in the last two paragraphs, down at the bottom.

Here, let me do my best to explain it.
The memory is a series of bytes.  A very long series of bytes.  Each byte is represented by an offset from the first byte, called an address.  Addresses are almost always written in hex.  For example, 0A00h is 10 bytes from the first byte.  0000h is the first byte.  Notice that numbers in hex are ended with an 'h'.  Now, Axe creates program LOL and outputs it to a spot in memory.  For the purposes of this example, we will say it's at B2C4h.  When you run a program, there is another address in memory called OP1 that TIOS copies the name of the program to.  So, when you run prgmLOL, OP1 looks like this:
ProtProgObj (this represents a protected program), L, O, L, 0, 0, 0, 0, 0
You may have noticed that you can only have up 8 character names in TIOS.  In reality, all names have 9 characters - the identifier (ProtProgObj in this case), and the name, buffered by zeros.  So, if a name is less than 8 characters (like LOL), zeros are added to make it equal 8.
Now, when you run the first routine, it looks at OP1, and copies it to 8384h (tempSwapArea, or L4, plus 223).  This means that it copies the name of the program to the end of L4.
Additionally, programs are not run from where they are saved.  If a program is saved to B2C4h, TIOS copies it to 9D95h to run it.  When the program ends, however, it is not copied back to B2C4h, essentially making it write protected.  This is important, because any changes we make will not be copied.
When you run the second routine, it copies the end of L4 back to OP1.  This is because, during the course of the program, it is possible for OP1 to change, so we back up the name first.  There is a routine provided by TIOS for looking up variables by name.  It requires the name to be in OP1, and when we call it, it will return B2C4h (remember, that's where the program is saved?).  We already know the program is running at 9D95h.  This is the most up-to-date version, the one with the modified data.  We need to keep that modified data, and the only way to do that is to copy it back to B2C4h.  So, when TIOS tells us the program is at B2C4h, we copy all the data from 9D95h to B2C4h.  This saves the modified data for the next time.

If you have any questions about this, please feel free to ask.  I want to make sure that you are comfortable with this, because it is the only way right now to save external data.

Also, if you are concerned about speed and the time it would take to copy it, think of it this way.  TIOS copies the program to 9D95h before running it.  We copy it from 9D95h when you call the second routine.  So, if you want to get a general idea of how long it might take, watch how long it takes to start, because that is exactly how long it will take to run the second routine.

Offline DJ Omnimaga

  • Clacualters are teh gr33t
  • CoT Emeritus
  • LV15 Omnimagician (Next: --)
  • *
  • Posts: 55942
  • Rating: +3154/-232
  • CodeWalrus founder & retired Omnimaga founder
    • View Profile
    • Dream of Omnimaga Music
Re: Routines
« Reply #138 on: March 21, 2010, 02:03:54 pm »
OK so basically it's not the entire program content that is copied to OP1, just the name, right? No data from the program where your routine is ran is copied to OP1?

Also btw just so it is clear, since I thought people got the idea before: I am not concerned about the speed it takes to copy anything especially if it's just gonna be used for saving/loading.
« Last Edit: March 21, 2010, 02:05:05 pm by DJ Omnimaga »
Now active at https://discord.gg/cuZcfcF (CodeWalrus server)

SirCmpwn

  • Guest
Re: Routines
« Reply #139 on: March 21, 2010, 02:20:28 pm »
OK so basically it's not the entire program content that is copied to OP1, just the name, right? No data from the program where your routine is ran is copied to OP1?

Right.  Just 9 bytes.  Never enough to do damage.

Offline DJ Omnimaga

  • Clacualters are teh gr33t
  • CoT Emeritus
  • LV15 Omnimagician (Next: --)
  • *
  • Posts: 55942
  • Rating: +3154/-232
  • CodeWalrus founder & retired Omnimaga founder
    • View Profile
    • Dream of Omnimaga Music
Re: Routines
« Reply #140 on: March 21, 2010, 02:32:57 pm »
That's good then. I misunderstood yesterday, I was convinced the entire program data stored at its end had to be copied during writeback.
Now active at https://discord.gg/cuZcfcF (CodeWalrus server)

SirCmpwn

  • Guest
Re: Routines
« Reply #141 on: March 21, 2010, 03:38:36 pm »
I made another routine, here is is:
Axe Programs in MirageOS
The attached program will help you get your Axe programs to show up in MirageOS.  Draw a 15x15 icon in the top-left hand corner of the graph screen and run the attached program.  Copy the resulting code into the first line of your program.  If you are also using my writeback routine, make sure this code comes before the first writeback routine.

Offline DJ Omnimaga

  • Clacualters are teh gr33t
  • CoT Emeritus
  • LV15 Omnimagician (Next: --)
  • *
  • Posts: 55942
  • Rating: +3154/-232
  • CodeWalrus founder & retired Omnimaga founder
    • View Profile
    • Dream of Omnimaga Music
Re: Routines
« Reply #142 on: March 21, 2010, 03:45:11 pm »
:O

wow awesome! Thanks a lot for this ^^

Plus no more need for abusing the MirageOs 1.0/1.1 ungroup rename glitch to rename your program to something else than LOL.8xp now ^^, altough Quigibo is gonna add compiling under different name/shell in the future
Now active at https://discord.gg/cuZcfcF (CodeWalrus server)

SirCmpwn

  • Guest
Re: Routines
« Reply #143 on: March 21, 2010, 03:48:05 pm »
wow awesome! Thanks a lot for this ^^

Sure thing!

Offline Builderboy

  • Physics Guru
  • CoT Emeritus
  • LV13 Extreme Addict (Next: 9001)
  • *
  • Posts: 5673
  • Rating: +613/-9
  • Would you kindly?
    • View Profile
Re: Routines
« Reply #144 on: March 21, 2010, 03:51:46 pm »
Nice! Thats very useful until Quigibo can get out the official version.  Will this interfere with any of the L1-L6 ram areas?

SirCmpwn

  • Guest
Re: Routines
« Reply #145 on: March 21, 2010, 03:54:09 pm »
Nice! Thats very useful until Quigibo can get out the official version.  Will this interfere with any of the L1-L6 ram areas?

No.  The outputted code is never run.  It's a header, what it looks like is this:
ret      ; Return to the TIOS, because it shouldn't run a mirage program
01       ; MirageOS ID
Icon
Description
0
The icon is taken from the graph screen, converted to hex and copied in.  The description is converted to hex (supported characters are A-Z, a-z, and [space]), and copied in.  MirageOS skips past all this to the first line of your code.  All this does is places some data into your program for MirageOS to read.

Offline Builderboy

  • Physics Guru
  • CoT Emeritus
  • LV13 Extreme Addict (Next: 9001)
  • *
  • Posts: 5673
  • Rating: +613/-9
  • Would you kindly?
    • View Profile
Re: Routines
« Reply #146 on: March 21, 2010, 04:12:05 pm »
true, but does Mirage use any of the same safe areas that Axe uses?

Offline calc84maniac

  • eZ80 Guru
  • Coder Of Tomorrow
  • LV11 Super Veteran (Next: 3000)
  • ***********
  • Posts: 2912
  • Rating: +471/-17
    • View Profile
    • TI-Boy CE
Re: Routines
« Reply #147 on: March 21, 2010, 04:37:18 pm »
true, but does Mirage use any of the same safe areas that Axe uses?
I think StatVars holds Mirage's interrupt vector. To disable this, you might want to put Asm(ED56) after the header to make it use the TI-OS interrupt instead.
"Most people ask, 'What does a thing do?' Hackers ask, 'What can I make it do?'" - Pablos Holman

Offline ztrumpet

  • The Rarely Active One
  • CoT Emeritus
  • LV13 Extreme Addict (Next: 9001)
  • *
  • Posts: 5712
  • Rating: +364/-4
  • If you see this, send me a PM. Just for fun.
    • View Profile
Re: Routines
« Reply #148 on: March 21, 2010, 06:24:26 pm »
Excellent routine SirCmpwn! ;D

true, but does Mirage use any of the same safe areas that Axe uses?
I think StatVars holds Mirage's interrupt vector. To disable this, you might want to put Asm(ED56) after the header to make it use the TI-OS interrupt instead.
Really, that's all?  Then why do Celtic III and xLib mess with Mirage so much?

_player1537

  • Guest
Re: Routines
« Reply #149 on: March 21, 2010, 06:38:44 pm »
I might be mistaken, but I believe that MOS uses one of the safe ram spots to hold the program lists, for speed.