Shop OBEX P1 Docs P2 Docs Learn Events
Load Same Code into All Cogs — Parallax Forums

Load Same Code into All Cogs

HumanoidoHumanoido Posts: 5,770
edited 2010-08-18 12:34 in Propeller 1
I need to load the same code into all cogs, at the same time if possible. Is there a simple coginit extension to specify the initialization and loading of all cogs 0 through 7?

Humanoido

Comments

  • kuronekokuroneko Posts: 3,623
    edited 2010-08-17 17:32
    Humanoido wrote: »
    I need to load the same code into all cogs, at the same time if possible. Is there a simple coginit extension to specify the initialization and loading of all cogs 0 through 7?

    No there isn't. Besides, the load-from-hub magic is tied to the hub window anyway so you don't gain anything compared to running 8 coginitA commands in sequence (assuming PASM).

    A make that 7 cognew's and one coginit mumblesorrymumblephilmumble
  • Mike GreenMike Green Posts: 23,101
    edited 2010-08-17 18:09
    A COGNEW or COGINIT returns as soon as the copying of the code to the cog is initiated. The actual copy operation continues in the new cog for about 100us before the copied code is actually executed. It's easy to initiate further COGNEW or COGINIT operations while the first one (and subsequent ones) is still executing.
  • HumanoidoHumanoido Posts: 5,770
    edited 2010-08-18 04:27
    Mike Green wrote: »
    A COGNEW or COGINIT returns as soon as the copying of the code to the cog is initiated. The actual copy operation continues in the new cog for about 100us before the copied code is actually executed. It's easy to initiate further COGNEW or COGINIT operations while the first one (and subsequent ones) is still executing.
    Mike, I don't know how you know so much but this is the key answer to my question of loading up all cogs with the same code and running each. Thank you much!

    Humanoido
  • HumanoidoHumanoido Posts: 5,770
    edited 2010-08-18 04:29
    kuroneko wrote: »
    No there isn't. Besides, the load-from-hub magic is tied to the hub window anyway so you don't gain anything compared to running 8 coginitA commands in sequence (assuming PASM).
    A make that 7 cognew's and one coginit mumblesorrymumblephilmumble
    What would the code look like? At the top, first one coginit, immediately followed by 7 cognew's?

    Humanoido
  • Heater.Heater. Posts: 21,230
    edited 2010-08-18 05:00
    Mike Green:
    A COGNEW or COGINIT returns as soon as the copying of the code to the cog is initiated. The actual copy operation continues in the new cog for about 100us before the copied code is actually executed.

    That is something I had not realized before and could probably save a lot of debugging time one day. Generally it is not a problem as the code that is being loaded to COG sits around in memory permanently anyway. BUT if you start a COGINIT/NEW and then assume it is done when it returns you are going to get into a mess if you want to immediately recycle the COG code memory space for something else.

    Humanoido: I think it's the other way around:
    1) Let's say you have a loader starting in COG 0 at start up.
    2) The loader starts up some chunk of code on seven other COGs with COGNEW.
    3) The loader starts that same chunk of code on COG 0 using COGINIT specifying 0 as the COG. Thus replacing the running loader with the chunk.
    4) Now you have 8 COGs all running the same chunk of code.

    Now you should not assume that the loader is running on COG 0 in step 3). But that's OK a COG knows it's own ID from COGID. So use that instead.

    I have done something similar in the ZOG loader (run_zog.spin) where it starts an instance of the ZOG interpreter in the COG that is running the loader. In this way ZOG takes over the COG from the loader and poof, there is no more Spin running in the Prop anywhere.

    The loader in run_zog.spin is written PASM as it moves things around in memory prior to starting ZOG which might trample on any Spin code. But I guess in your case you can do it from Spin.
  • Heater.Heater. Posts: 21,230
    edited 2010-08-18 05:27
    Actually, if you are doing all this from Spin you only need 7 COGNEWs.

    Each of the seven COGNEWs starts a COG on some method in your Spin object.

    When they are up just call the same method. BINGO all COG's are running the same method.

    I seem to remember that there is an example of this in the Propeller manual.
  • kuronekokuroneko Posts: 3,623
    edited 2010-08-18 05:34
    Humanoido wrote: »
    What would the code look like?

    You didn't specify whether you use SPIN or PASM but in SPIN you want something like this
    cognew(fn, @stack0)
    cognew(fn, @stack1)
    cognew(fn, @stack2)
    cognew(fn, @stack3)
    cognew(fn, @stack4)
    cognew(fn, @stack5)
    cognew(fn, @stack6)
    coginit(cogid, fn, @stack7)
    

    and PASM (untested)
    cogid   zwei
    
            coginit eins
            coginit eins
            coginit eins
            coginit eins
            coginit eins
            coginit eins
            coginit eins
            or      eins, zwei
            xor     eins, #%1000
            coginit eins
    
    eins    long    param|addr|%1000   ' param and addr shifted appropriately
    zwei    res     1
    
  • Ron CzapalaRon Czapala Posts: 2,418
    edited 2010-08-18 06:27
    Here is an example from the Propeller Education Kit Labs
    ''This code example is from Propeller Education Kit Labs: Fundamentals, v1.1.
    ''A .pdf copy of the book is available from [URL="http://www.parallax.com"]www.parallax.com[/URL], and also through
    ''the Propeller Tool software's Help menu (v1.2.6 or newer).
    ''
    '' BlinkWithCogs.spin
    VAR
        long stack[30]
    PUB LaunchBlinkCogs 
        cognew(Blink(4, clkfreq/3, 9), @stack[0])
        cognew(Blink(5, clkfreq/7, 21), @stack[10])
        cognew(Blink(6, clkfreq/11, 39), @stack[20])
     
    PUB Blink( pin, rate, reps)
        dira[pin]~~
        outa[pin]~
     
        repeat reps * 2
           waitcnt(rate/2 + cnt)
           !outa[pin]
    
  • HumanoidoHumanoido Posts: 5,770
    edited 2010-08-18 12:34
    Thanks Ron, this will work because it has the bonus of parameter passing from cog to cog. :) I appreciate all the replies as they are very helpful.

    Humanoido
Sign In or Register to comment.