Running Native Code with ImageCraft C
jazzed
Posts: 11,803
Being able to run native pasm in ImageCraft C should allow us to get existing asm drivers up in short order.
Below is one way to run native pasm on a cog in ImageCraft C. Don't know if there is another way. Something more elegant would be desirable.·ImageCraft will be adding a parameter variable to cognew_native so we can communicate with the pasm.
Creating the pasm array will require dumping the .binary file, removing the header, and formatting as a C array then stuffing it into a file to be included in·a cog loader .c module. Hopefully someone can write a utility that does the conversion.·Being able to·preserve code in it's existing form by adding a cookie like·0xbabeface in the asm would be useful. This would be in lieu of hacking out spin methods, etc.
·Added missing toggleStack in·undef USE_NATIVE case·4-22.
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
jazzed·... about·living in·http://en.wikipedia.org/wiki/Silicon_Valley
Traffic is slow at times, but Parallax orders·always get here fast 8)
Post Edited (jazzed) : 4/22/2008 4:11:20 PM GMT
Below is one way to run native pasm on a cog in ImageCraft C. Don't know if there is another way. Something more elegant would be desirable.·ImageCraft will be adding a parameter variable to cognew_native so we can communicate with the pasm.
Creating the pasm array will require dumping the .binary file, removing the header, and formatting as a C array then stuffing it into a file to be included in·a cog loader .c module. Hopefully someone can write a utility that does the conversion.·Being able to·preserve code in it's existing form by adding a cookie like·0xbabeface in the asm would be useful. This would be in lieu of hacking out spin methods, etc.
[color=#008000]/** * @file native.c * Demonstrates running native propeller opcodes defined as an array from C. */ [/color][color=#000000]#include <propeller.h> [/color][color=#008000]//undef USE_NATIVE to use LMM toggleBit function in a cog[/color] [color=#000000]#define USE_NATIVE #ifdef USE_NATIVE [b]long[/b] longArr[noparse][[/noparse]] = { 0xa0ffecff, [/color][color=#008000]// mov dira, #$ff[/color] [color=#000000] 0x6cffe8ff, [/color][color=#008000]// loop xor outa, #$ff[/color] [color=#000000] 0x5c7c0001 [/color][color=#008000]// jmp #loop[/color] [color=#000000]}; #else [b]long[/b] toggleStack[b][noparse][[/noparse]10];[/b][/color] [color=#000000][b]void[/b] toggleBit([b]void[/b]) { asm([i]"mov dira, #$ff"[/i]); [b] while[/b](1) { asm([i]"xor outa, #$ff"[/i]); } } #endif [b]void[/b] main([b]void[/b]) { #ifdef USE_NATIVE _coginit_native(4, ([b]void[/b]*)&longArr); #else cognew(toggleBit, toggleStack); #endif [b] while[/b](1); [color=#008000]// don't let program terminate [/color] [color=#000000]}[/color] [/color]
·Added missing toggleStack in·undef USE_NATIVE case·4-22.
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
jazzed·... about·living in·http://en.wikipedia.org/wiki/Silicon_Valley
Traffic is slow at times, but Parallax orders·always get here fast 8)
Post Edited (jazzed) : 4/22/2008 4:11:20 PM GMT
Comments
First of all, at the production release, you can add an assembly .s file to your project that contains pure COG code and you can launch that. So you don't have to do it the hard way.
Second, if you want to do this before the facility is available, google for things like bin2hex and you should find some utility out there.
So instead of:
You could do something sort of like:
I guess this is more of a shortcut if anything, but it could help when writting large blocks of ASM code into your C programs.
Thanks everyone!
--Andrew Arsenault.
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Share the knowledge: propeller.wikispaces.com
Lets make some music: www.andrewarsenault.com/hss
It's not what you want exactly, but something like this appears to work:
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
jazzed·... about·living in·http://en.wikipedia.org/wiki/Silicon_Valley
Traffic is slow at times, but Parallax orders·always get here fast 8)
I did have to use an intermediate cog so I could access the coginit instruction directly (so I could pass a par to the new cog). This is a bit messy, but it seems to work well enough for now.
Harrison
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
jazzed·... about·living in·http://en.wikipedia.org/wiki/Silicon_Valley
Traffic is slow at times, but Parallax orders·always get here fast 8)
Probably no need to use another cog just as a middle-person, you can add your own function:
// function prototype, add to the file that references the function
int my_cognew_native(void (*func)(int), int par);
Add a .s file to the project file list, and write something like:
.area text(rom, rel)
_my_cognew_native::
shr R0,#2
shl R0,#4 ; get function address into the right place
shr R1,#2 ; get par into the the right place
shl R1,#18
or R0,R1
COGINIT R0 WR WC WZ
IF_C neg R0,#1
@FRET
not tested, but something like it should work....
Just change the ledpin variable to use other Pins.
I also tried it with a .s file and the assembly source in the coments above. But the jmp #blink does not jump to the right location, because an ORG 0 is not possible. Then I tried jmp #8, but this produce a jmp #0 in the binary, and I tried jmp #blink-asmentry (with an asmentry label at begin), but that is not accepted by the Compiler.
I think we need first a detailed description of the ICC Prop Assembler and it's possibilities.
The code for starting a new cog is a bit different from what ImageCraft has suggested in the previous post, but I have done it before I have read that.
Andy
EDIT: The code above seams not do work with more then 1 cog, I don't know why !
Post Edited (Ariba) : 4/23/2008 2:19:41 AM GMT
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
jazzed·... about·living in·http://en.wikipedia.org/wiki/Silicon_Valley
Traffic is slow at times, but Parallax orders·always get here fast 8)