Shop OBEX P1 Docs P2 Docs Learn Events
Calling Spin from C — Parallax Forums

Calling Spin from C

Keith DKeith D Posts: 15
edited 2008-06-28 04:46 in Propeller 1
Hi

I was wondering if it would be possible to now run a spin object from within C code?

I know that the Spin Interpreter source has been released, and I was wondering about the practicalities of converting the interpreter into a C library so that all the spin libraries that have been developed could be used.

Is anyone working on a project like this?

Cheers
Keith

Comments

  • Ron SutcliffeRon Sutcliffe Posts: 420
    edited 2008-06-27 09:04
    I think it is unlikely.

    Imagecraft clearly state in their readme file that SPIN is never likely to be supported in any version of ICC for Prop.


    Remember·your code runs within the constraints of LMM.

    Ron
  • ImageCraftImageCraft Posts: 348
    edited 2008-06-27 09:08
    Well, I talked to Keith a little about it. The main thing is that I did/do not see the ROI in it. I am not sure whether there is any technical reason why it cannot be done. We have to be carefully how C and Spin share the HUB RAM of course, but C's use of the HUB RAM is not hardcoded and can leave space for Spin. But there are too many unknowns to me, so I welcome any knowledgeable Spin hackers and users to share some insight.
  • Ron SutcliffeRon Sutcliffe Posts: 420
    edited 2008-06-27 09:20
    Well I did wonder about that.

    Only have to look at the work Hippy has done with Pnut.

    But LMM would slow Spin down even further would it not ?

    Ron
  • AleAle Posts: 2,363
    edited 2008-06-27 09:45
    Ron Sutcliffe said...
    But LMM would slow Spin down even further would it not ?

    NO. Spin will run in its own COG. Multitasking with more than 1 processor and NO delay (almost) from access to shared memory wink.gif
  • Ron SutcliffeRon Sutcliffe Posts: 420
    edited 2008-06-27 10:18
    Ok Ale, its the process that I don't understand.

    I assume the long Pnut_array is loaded into a new cog by ICC.

    ICC starts cog..... code for Pnut is read from some HUB memory reserved for SPIN code.

    I need to think on this one............

    ??

    Ron
  • hippyhippy Posts: 1,981
    edited 2008-06-27 11:03
    It's entirely feasible for C to launch another Cog running the ROM interpreter and to execute Spin bytecode. Two problems to overcome though ...

    1) How to get compiled Spin bytecode to where it needs to be to suit the C memory layout.

    2) How to interact between the two beyond peeking and poking shared RAM locations.

    The ImageCraft development process is far more open and accessible than Parallax's Propeller Tool so it currently makes more sense to take ImageCraft output and launch C from within Spin. That's still not easy though and would need external tools writing to enable it.

    With the demo and non-commercial C compilers not supporting command line invocation that makes it hard to create an elegant tool chain except for commercial compiler users. It is also IMO going to limit the incentive for people working towards such tools.

    Being able to intermix code in C and Spin and casually call from Spin to C and vice-versa is possible but would need a whole new development environment adding and be a more complicated proposition.

    Apart from doing it for the fun of it or to prove it can be done I don't really see the need or usefulness. The main argument in support is so C programmers can take existing Spin objects for use. A better approach IMO would be to convert Spin objects to C. Effort spent in doing that and automating the process would be far more useful IMO.

    Such efforts are I believe most likely going to have to come from ImageCraft, C users or Spin programmers wanting to migrate to C. No offence intended to ImageCraft or C users, but I personally don't see any motivation for undertaking conversion or developing tools which would benefit commercial C compiler users but have almost no benefit for the wider Spin community, a similar ROI issue ImageCraft has identified. So, while it's possible, in the short term, I don't think C plus Spin is likely to happen. Others may however have entirely different ideas.
  • ImageCraftImageCraft Posts: 348
    edited 2008-06-27 11:28
    @hippy
    Yes, I think from your several postings on the subject that you don't care for the commercial C tools too much. I am just trying to make a living here...
  • AleAle Posts: 2,363
    edited 2008-06-27 11:29
    Did I ever suggested open source development tools for the propeller ?
  • Ron SutcliffeRon Sutcliffe Posts: 420
    edited 2008-06-27 12:05
    I can see no reason for wanting to run Spin alongside C either Hippy.

    Makes no sense at all IMO.

    Ron
  • Keith DKeith D Posts: 15
    edited 2008-06-27 12:18
    My (personal) main reason for wanting to call spin from C is to utilise the Object exchange for objects that have already been written. I am a professional developer (currently writting in Java, but I used to write in C), and prefer the C language over spin, but I don't want to have to port any of the existing spin objects and have to maintain them especially if they already exist in spin and are being maintained!

    I like the idea of having well written/well documented shared libraries and objects but I feel that being able to call a spin object from C code (and vise-versa) would benefit everyone developing code as we could still share code without having to duplicate it in two languages!
  • RaymanRayman Posts: 14,162
    edited 2008-06-27 12:51
    I think calling Spin from C would be useful just because the Spin interpreter comes for free and we've seen that Spin code can be much smaller than C. So, if space gets tight, putting some objects that doen't need to fast in Spin would seem to make sense...
  • stevenmess2004stevenmess2004 Posts: 1,102
    edited 2008-06-27 13:04
    A lot of the objects that use an assembly cog for the driver should be fairly easy to port. I believe that all you need to do is add the assembly part of the driver binary to the c file and then start a cog to run it and some c functions to call it. Since these will just be playing around with the memory at some address it shouldn't be too hard but I haven't done any yet.

    A better thing to do than run spin is to make a spin to c pre-processor. It should be a fair bit easier and should make the spin code run a bit faster. However, the c version will probably use more space than the spin version.
  • Ron SutcliffeRon Sutcliffe Posts: 420
    edited 2008-06-27 14:14
    Steve,
    The asm part is made easy be using the px.bat file. Just look at the C objects posted by jazzed. There is PX.bat which takes the binary output file and generates a long array Object_array.h ready to be loaded under C.

    The tedious part is porting the SPIN component.

    Ron
  • jazzedjazzed Posts: 11,803
    edited 2008-06-27 14:28
    The px.bat file relies on Cygwin tools which is a windows version of linux utilities and gnu development tools.
    You can install Cygwin using http://www.cygwin.com/setup.exe . Or I could rent out my linux server [noparse]:)[/noparse]

    The file makearray.bat let's you compile spin and run px.bat using one command. It requires the Propellent tool.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔


    Post Edited (jazzed) : 6/27/2008 2:39:46 PM GMT
  • hippyhippy Posts: 1,981
    edited 2008-06-27 18:12
    If you don't fancy installing Cygwin, this Windows command line utility will convert PASM in a Spin .eeprom or .binary into a C array.

    Strip the Spin source to just the PASM in the DAT, prefix with a PUB Main, F8 save the file as .binary or .eeprom, run Pasm2C. The included PasmCode.spin is the example to follow.
  • RaymanRayman Posts: 14,162
    edited 2008-06-27 18:15
    That nice Hippy! Thanks.
  • jazzedjazzed Posts: 11,803
    edited 2008-06-27 18:40
    Very good Hippy [noparse]:)[/noparse]
    Do you use a fixed number to get the offset? I've found that sometimes a spin .binary file can be pulled in without any source modification if you allow selecting the start address. It can be tricky though.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
  • hippyhippy Posts: 1,981
    edited 2008-06-27 19:04
    Yes, fixed offset. DAT starts at $0018, byte after last is pointed to by the contents of $000C.

    More than one method and the start address gets pushed upwards. As there's no way to tell how many methods there are from the image it's a guess as to how far upwards and I think length might get harder to determine.
  • jazzedjazzed Posts: 11,803
    edited 2008-06-27 20:03
    I've had good luck so far with looking for instructions with most significant bits set or not at or around location $18. Relying entirely on that as part of a detection algorithm may not be perfect as corner cases may eventually show up, but one could provide an experimental or hidden auto-detect command line parameter [noparse]:)[/noparse] I'll look at it more after golf today.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
  • hippyhippy Posts: 1,981
    edited 2008-06-27 20:44
    I've been thinking about C calling Spin methods and it is doable ...

    1) Adjust the C compiler so stack start is moved down from top of memory, that space above will be used for the Spin program ( bytecode, vars and stack ). It will be a smaller version of Hub memory.

    2) Put the entire Spin program above the C stack base.

    3) Write a Spin Interpreter. Not as hard as it seems.

    4) Create a syntax and implementation for a C program to call a Spin program in the C source. Because there are no names for Spin methods they'll have to be enumerated calls. This effectively compiles to a call into the C LMM.

    5) Add the 'Call Spin' handler to the C LMM. This pushes a stack frame as a Spin method would expect when running plus any arguments etc. All the necessary information is in the Spin image to do this. Then jump to the Spin Interpreter to execute the selected Spin method.

    6) Spin interpreter runs as it would be expected to, executes the method, launches new Cog's, calls other Spin methods etc etc. When the return from the first called method occurs the Spin interpreter jumps back to the C LMM and C carries on as normal.

    Bingo - inline, same Cog, totally transparent Spin called by C.

    C and Spin would be entirely isolated from each other only communicating through method call parameters so would need getters and setters as calls into objects do. It's pretty much how I'd run C from within Spin.

    Off the top of my head, I'd guesstimate a week's work if able to jiggle the C compiler but depends on the tweaks needed to get the Spin interpreter working.
  • hippyhippy Posts: 1,981
    edited 2008-06-27 20:48
    @ Jazzzed : Probably the easiest thing to do is to take a .spin file, parse that and only keep CON and DAT sections, create a temporary.spin file, compile that via Propellent and then everything is in fixed locations.
  • ImageCraftImageCraft Posts: 348
    edited 2008-06-27 21:52
    The C compiler works seems fairly minimal: adjusting the HRSP is easy, you can pretty much do it by hand even without any changes from the compiler:
    asm("@ldcnst\n"
    ".long 0x????\n"
    "mov HRSP,TEMP0");
    will put whatever you want to the HUB RAM stack. Of course, we can add a checkbox to the IDE for initial SP placement.
  • jazzedjazzed Posts: 11,803
    edited 2008-06-27 22:18
    hippy said...
    @ Jazzzed : Probably the easiest thing to do is to take a .spin file, parse that and only keep CON and DAT sections, create a temporary.spin file, compile that via Propellent and then everything is in fixed locations.
    Yeah I guess so [noparse]:)[/noparse]· What VB version syntax is being used in that .bas file?· VB6 chokes on it.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
  • hippyhippy Posts: 1,981
    edited 2008-06-27 23:33
    @ ImageCraft : That looks easy enough, I thought there'd be a way.

    @ Jazzed : That's PowerBasic Console Compiler (PB/CC) source. I can knock out a VB6 version if you'd like.
  • jazzedjazzed Posts: 11,803
    edited 2008-06-28 00:52
    It's ok Hippy, I'll C something if I have time; meanwhile existing tools are fine. Thanks for the offer though.
    I'm much more interested in seeing your Cspin integration progress. Knowing you, it will be done by tomorrow [noparse]:)[/noparse]

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
  • hippyhippy Posts: 1,981
    edited 2008-06-28 03:35
    Unfortunately integrating the two isn't a project I'm currently planning on, which is why I was saying it would have to be down to others.
  • Cluso99Cluso99 Posts: 18,069
    edited 2008-06-28 04:46
    Hi All,

    I'm currently rewriting the spin interpreter (modifying) to run faster and it is looking good. As the bytecodes are being moved to table driven access, it should then be possible to call routines from any language (such as C). Also it should be possible to extend the spin interpreter using the unused bytecode.

    My version still runs in a cog and uses hub ram tables to gain execution speed. I probably will need to use small overlays (or maybe LMM) for a few routines, but it is still too early to know how many routines will be done this way. My overlay routine hits the sweet spot.

    see Spin Interpreter - Faster?? posting
Sign In or Register to comment.