How do I cognew a Spin binary?
Dave Hein
Posts: 6,347
I want to include a Spin binary in a program, and then run it in another cog.· It seems like the way to do this is to set up the stack with the correct stuff, and then do a cognew that loads the Spin interpreter·from ROM.
Could one of the Spin gurus look at my code and tell me if I'm on the right track, and if so, what would I need to change to actually make it work?
Thanks,
Dave
Could one of the Spin gurus look at my code and tell me if I'm on the right track, and if so, what would I need to change to actually make it work?
Thanks,
Dave
CON SPIN_INTERP_ADDR = $f002 ' NOTE: This should be long-aligned OBJECT_BASE = 0 OBJECT_VAR_BASE = 1 STACK_FRAME = 2 RETURN_ADDRESS = 3 PARM1_OFFSET = 2 PARM2_OFFSET = 3 VAR long stack[noparse][[/noparse]20] long varspace[noparse][[/noparse]100] PUB main InitializeStack(@stack, 2, 4, 1, 2) cognew(SPIN_INTERP_ADDR, @stack) PUB InitializeStack(stackptr, numparms, numlocals, parm1, parm2) word[noparse][[/noparse]stackptr][noparse][[/noparse]OBJECT_BASE] := @testcode ' NOTE: Need to add flags word[noparse][[/noparse]stackptr][noparse][[/noparse]OBJECT_VAR_BASE] := @varspace word[noparse][[/noparse]stackptr][noparse][[/noparse]STACK_FRAME] := stackptr + 8 + (numparms + numlocals) * 4 word[noparse][[/noparse]stackptr][noparse][[/noparse]RETURN_ADDRESS] := 0 ' NOTE: Not sure what this should be long[noparse][[/noparse]stackptr][noparse][[/noparse]PARM1_OFFSET] := parm1 long[noparse][[/noparse]stackptr][noparse][[/noparse]PARM2_OFFSET] := parm2 DAT testcode FILE "test.binary" ' This is a Spin binary
Comments
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
"I mean, if I went around sayin' I was an emperor just because some moistened bint had lobbed a scimitar at me they'd put me away!"
Below is one way to do it.
You could also load/fix-up in a COG and run the binary with coginit ....
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Propeller Pages: Propeller JVM
What I want to do is something like this;
The difference is that I want to load Cog 1 code from a binary file instead of including the source.· Cog 0 will continue to run while Cog 1 runs.· Basically, I want to trick the Spin interpreter into doing a Spin cognew rather than a PASM cognew.
Dave
They are loading an unmodified propeller binary and launching the top object in a spin cog while leaving the existing cogs running, which appears to be precisely what you are trying to do.
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
"I mean, if I went around sayin' I was an emperor just because some moistened bint had lobbed a scimitar at me they'd put me away!"
I tried out the code and it work great!· I was able to run a spin binary file·in a new cog.
Thanks,
Dave
What's the best way to handle the stack size issue?· Maybe additional information can be added to the end of the spin binary file to define the stack size needed.· If it is not included, then an arbitrarily large stack would be allocated, such a 1 or 2 kbytes.
Dave
CON
_stacksize = nnn
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
www.mikronauts.com E-mail: mikronauts _at_ gmail _dot_ com
My products: Morpheus / Mem+ / PropCade / FlexMem / VMCOG / Propteus / Proteus / SerPlug
and 6.250MHz Crystals to run Propellers at 100MHz & 5.0" OEM TFT VGA LCD modules
Las - Large model assembler Largos - upcoming nano operating system
Where would you store it? (as a long between VAR and Stack ?)
Even then, it's still guesswork on the part of the original binaries author.
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
"I mean, if I went around sayin' I was an emperor just because some moistened bint had lobbed a scimitar at me they'd put me away!"
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
"I mean, if I went around sayin' I was an emperor just because some moistened bint had lobbed a scimitar at me they'd put me away!"
The loader would know that the stack size in being specified because the first long contains the magic workd $1ead5afe.· If the magic word is not present, then the loader would have to use some reasonable value for the stack size.· Perhaps other information could be included with the stack size, such as whether the program must run as the only app in the system.
The author should be able to detemine the stack size needed for his program.· He might be able to figure it out from the worst case call depth in the program, or he could run a stack analyzer to determine it.
Dave
But I agree, it can be done externally.
I now have stripped down and modified PropCMD, leaving 24KB free in the hub, and using unix-style commands.
I think I can get it to the point were there will be almost 28KB is free in the hub, allowing mailbox-aware spin programs to run without having to reboot... and since cog images won't have to be part of the spin code, it will actually leave more memory for the programs!
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
www.mikronauts.com E-mail: mikronauts _at_ gmail _dot_ com
My products: Morpheus / Mem+ / PropCade / FlexMem / VMCOG / Propteus / Proteus / SerPlug
and 6.250MHz Crystals to run Propellers at 100MHz & 5.0" OEM TFT VGA LCD modules
Las - Large model assembler Largos - upcoming nano operating system
Your problem there is while there is a stack start entry, there is nothing to specify where it ends or how big it might get. The stack just goes from DBASE until it hits ROM (and I can tell you weird stuff happens when it bumps into ROM)
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
"I mean, if I went around sayin' I was an emperor just because some moistened bint had lobbed a scimitar at me they'd put me away!"
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
www.mikronauts.com E-mail: mikronauts _at_ gmail _dot_ com
My products: Morpheus / Mem+ / PropCade / FlexMem / VMCOG / Propteus / Proteus / SerPlug
and 6.250MHz Crystals to run Propellers at 100MHz & 5.0" OEM TFT VGA LCD modules
Las - Large model assembler Largos - upcoming nano operating system
In this example, the programmer knows (or should know) that a stack of 20 longs is sufficient for the program in cog 1.· There are various techniques for determining the stack usage, and ensuring that enough memory is allocated for the stack.
When loading a Spin binary file it would be useful to provide the stack size to the loader.· That is why I am suggesting including it in the top object's DAT space.· It doesn't require any changes to the compiler.
The other is issue is cleaning up after the program is done.· I need to free up the cog and also free up the memory the program used.· What normally happens when the entry method reaches the end?· Do it automatically free up the cog or does it spin in an infinite loop?
Dave
Why not use the first 2 words in the image to do that (they are usually the clock speed). You could then have your image locate the method table and work back from there to find the pointers.
No, once the end of the code is reached, the implicit return causes the cog to stop.
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
"I mean, if I went around sayin' I was an emperor just because some moistened bint had lobbed a scimitar at me they'd put me away!"
Maybe I'm just hallucinating, but would this work?
If you can do that, you should also be able to leave the return code in the stack also, but it depends on how you recover after exit. Might have to leave the return code in a predefined location ... I don't know exactly. All pure speculation at this point, so this could be worthless pondering.
Cheers,
--Steve
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Propeller Pages: Propeller JVM
Program cleanup after completion will require a little work.· A well behaved program should close all its files and free any memory it allocated.· The OS will just have to free up the memory that the program was loaded in.· A poorly behaving program may leave a memory mess when it exits.· The OS would have to close all the open files and free up memory, which would require tagging them with the COG ID.· I'll see if I can get the well behaved case working first.
Dave
Thanks,
Dave
Delighted!
--Steve
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Propeller Pages: Propeller JVM