Shop OBEX P1 Docs P2 Docs Learn Events
Different executables on different cogs — Parallax Forums

Different executables on different cogs

Mercury1964Mercury1964 Posts: 15
edited 2015-01-04 23:12 in Propeller 1
I'm trying to create a smartwatch using the Propeller. Ideally, I'd like to be able to run custom programs (games, demos, etc.) from an SD card in separate cogs, while keeping certain system utilities (like the RTC manager, display handler, etc.) running in other cogs. The only SD card loaders I've seen so far *seem* to load a new program into cog 0 and stop all other cogs, effectively overwriting whatever they were running before. Is there a way of loading external programs into a specific cog while keeping other cogs running?

Comments

  • Duane DegnDuane Degn Posts: 10,588
    edited 2015-01-03 13:46
    Is there a way of loading external programs into a specific cog while keeping other cogs running?

    Sort of.

    This idea as been discussed several times on the forum. Post #34 of this thread has a program which should allow one to replace one method with another.

    Are you running out of hub RAM? You don't need to resort of overlays unless you're running short on memory. You can start and stop cogs all you want with more traditional program techniques.

    I'm assuming you're using Spin? I believe using large programs in C is easier than when using Spin.
  • localrogerlocalroger Posts: 3,451
    edited 2015-01-03 14:25
    What you need to do is load the PASM image for the cog you want to start into a temporary buffer. This requires you to have the filesystem and a 2K buffer available. You can load the image into the buffer, launch the cog from the buffer with COGNEW, then reuse the buffer for other stuff once the cog is running. You can only do this with PASM or C COG mode code -- Spin and C bytecode and LMM are actually executed from Hub RAM which is common to all the cogs.
  • Mercury1964Mercury1964 Posts: 15
    edited 2015-01-03 14:47
    Duane Degn wrote: »
    Post #34 of this thread has a program which should allow one to replace one method with another.

    This seems to be what I was looking for. Thanks!
    Duane Degn wrote: »
    Are you running out of hub RAM? You don't need to resort of overlays unless you're running short on memory. You can start and stop cogs all you want with more traditional program techniques.

    I'm not worried about RAM usage (at least, not yet). From my limited understanding of multi-core programming on the Propeller, using cog_run would require any applications to be compiled and linked as methods in the system software, which is somewhat limiting for my purposes.
  • Duane DegnDuane Degn Posts: 10,588
    edited 2015-01-03 16:08
    I'm not worried about RAM usage (at least, not yet). From my limited understanding of multi-core programming on the Propeller, using cog_run would require any applications to be compiled and linked as methods in the system software, which is somewhat limiting for my purposes.

    Are you using Spin or C?

    The link I provided was to a thread about using overlays in Spin. I don't know enough about C on the Propeller to know how to do something similar in C. I believe creating a large program (larger than 32K) using C is easier than creating a large program in Spin.

    Again, if you're not running out of RAM, there isn't a need to use overlays.
  • Cluso99Cluso99 Posts: 18,069
    edited 2015-01-03 17:39
    You might also look at my Prop OS. See my signature for a link.

    I have also written an overlay loader. It's used in ZiCog (Z80 emulator) which runs CPM2.2 (and can be launched by Prop OS).
  • Mercury1964Mercury1964 Posts: 15
    edited 2015-01-03 20:53
    Duane Degn wrote: »
    Are you using Spin or C?
    Again, if you're not running out of RAM, there isn't a need to use overlays.

    I'm using Spin. I'm still confused - is there another way to load methods to individual cogs without them being integrated at compile-time?

    I'll take a close look at PropOS in the morning - it seems very interesting.
  • Duane DegnDuane Degn Posts: 10,588
    edited 2015-01-03 21:26
    I'm using Spin. I'm still confused - is there another way to load methods to individual cogs without them being integrated at compile-time?

    Technically methods aren't loaded into cogs. PASM code gets loaded into cogs but Spin code remains in the hub.

    Launching a new cog to run another instance of the Spin interpreter occurs at run time not compile time. You could have a conditional statement indicating which method to launch.

    For example say the variable "character" is the input from the terminal.

    The value of "character" could be used to select which method will be used by the newly launched cog.
      if characterCog
        cogstop(characterCog - 1) ' optional, stop previously launched cog.
    
      case character
        "a":
          characterCog := cognew(MethodA, @stack) + 1
        "b":
          characterCog := cognew(MethodB, @stack) + 1
        ' etc.
    

    I this example the same cog could be used to run different methods depending on which one the user wanted active.

    Before the new cog is launched the previously launched cog is stopped.

    The "+ 1" allows allows any cog (even cog #0) to satisfy the "if characterCog" check.

    Edit: I remember when I was first learning Spin statements like "if characterCog" confused me. "if characterCog" is the same as "if characterCog <> 0".
  • cavelambcavelamb Posts: 720
    edited 2015-01-03 22:46
    Back @ the OP?

    So the real problem here is that the SD driver takes all the cogs?
  • Mercury1964Mercury1964 Posts: 15
    edited 2015-01-04 12:01
    Thanks for the help so far, guys. I'm not great at explaining things, so I'll start back at the beginning.

    My watch will need to have certain cogs running certain things all the time. Things like the SD card handler, the RTC manager, or the Bluetooth controller always need to be running. These tasks will be written as methods in the system software, something like this pseudocode:
        PUB bluetooth:
            //code goes here
    
        PUB rtc:
            //code goes here
    
        PUB sdcard:
            //code goes here
    
        PUB main:
            cog_run(1, bluetooth)   
            cog_run(2, rtc) 
            cog_run(3, sdcard)
    

    As I understand it, using this method of starting new cogs would require any applications I develop to be built into the system software, and any updates to those applications would require me to reflash the entire system software to the Propeller. Furthermore, I would only be able to have as many applications as I could fit into the 20K or so left of memory after all the system code. I was wondering if there was any way to load these applications from an SD card instead of from the EEPROM and without having linked them when I compiled the system software.
  • Duane DegnDuane Degn Posts: 10,588
    edited 2015-01-04 12:33
    As I understand it, using this method of starting new cogs would require any applications I develop to be built into the system software, and any updates to those applications would require me to reflash the entire system software to the Propeller. Furthermore, I would only be able to have as many applications as I could fit into the 20K or so left of memory after all the system code. I was wondering if there was any way to load these applications from an SD card instead of from the EEPROM and without having linked them when I compiled the system software.

    I think your main limit is the RAM. Rebooting the Prop to install new firmware shouldn't be a big deal. As you've noted, this can be done from a SD card.

    If you run out of RAM then you'll need to use the technique linked to in post #2 or Cluso's Prop OS.
  • ElectrodudeElectrodude Posts: 1,658
    edited 2015-01-04 23:12
    I never realized you could mess with pbase and friends through pnut in the way the Spin Task Swapper in the other thread and MethodPointer do it... I thought WriteOps messed with the stack frame directly underneath itself and then returned, but apparently not. Now I have no excuse not to write my run-time spin linker (and loader). It will probably start out as just a normal spin library, but will eventually be part of the spin++ (got a better name?) language I've been designing for a while.
Sign In or Register to comment.