Shop OBEX P1 Docs P2 Docs Learn Events
coginit vs cognew — Parallax Forums

coginit vs cognew

edited 2013-10-12 11:41 in Propeller 1
Is there a reason to call coginit rather than cognew? That was a question asked in another thread about some PASM code that someone was having problems with. Rather than hijacking that thread I thought I would start a new one since the coginit vs cognew subject has come up before and there's potential for discussion.

Coginit is the only way I've found to have PASM code running in all 8 cogs.
coginit(7, @PASM, pParameters7)
coginit(6, @PASM, pParameters6)
coginit(5, @PASM, pParameters5)
coginit(4, @PASM, pParameters4)
coginit(3, @PASM, pParameters3)
coginit(2, @PASM, pParameters2)
coginit(1, @PASM, pParameters1)
coginit(0, @PASM, pParameters0)

You have to load cog 0 last or you'll overwrite the spin code that's starting the cogs.

I remember other threads where people were advised that it wasn't possible to run PASM in all eight cogs since there had to be some spin code to kick the whole thing off. That's true but it can be replaced when it has completed it's task.

Coginit also gives you more control over what goes where so you can reassign cogs to other tasks on the fly. I can't think of a scenario where that would be necessary but I'm sure that someone will be able to think of something.

Sandy

Comments

  • Mike GreenMike Green Posts: 23,101
    edited 2013-10-11 08:26
    On the other hand, the hub already has a way to keep track of which cogs are available for use and can automatically (with COGNEW) assign an idle cog. The main reason for using COGINIT is when you want to replace existing code in the current cog with new code reinitializing the cog ( COGINIT( COGID, ..., ... ) ). Everything else you can do with COGNEW. Note that this question also comes up with replacing a current PASM program with a fresh copy of the Spin interpreter.
  • edited 2013-10-11 15:45
    Cognew will allow up to 7 cogs to run PASM because it always looks for a free cog but if you want 8 cogs you're stuck unless you use coginit.

    Currently I have a project going that uses PASM for everything except display. Once I get the whole thing finished I'll write a PASM LCD driver to replace the current spin driver. Why? Because I can. I think.

    Back in the early 1980s I remember writing very simple programs on my Apple II using just hex codes. The programs didn't do much but what they did do they did really fast. I found the whole process fascinating.

    Besides, if you use Spin you have to fiddle around and calculate the amount of stack space required.

    Sandy
  • Heater.Heater. Posts: 21,230
    edited 2013-10-12 01:19
    Alexander (Sandy) Hapgood,
    You have to load cog 0 last or you'll overwrite the spin code that's starting the cogs.
    That is true, most of the time.
    In general a piece of Spin code that intends to replace itself with a PASM program in the same COG should find out which COG it is running on (COGID) and use that ID to load the PASM into with COGINT.
  • ratronicratronic Posts: 1,451
    edited 2013-10-12 11:25
    I had never tried running assembly on all 8 cogs. So I experimented and came up with this that starts the same assembly routine in all 8 cogs and blinks ports 8 - 15 at a slightly different rate.
    Con            'blinking leds in all cogs in assembly                                              
                                                             
      _CLKMODE = XTAL1 + PLL16X                              
      _XINFREQ = 5_000_000
      
    Var
      long param[8]
      
    Obj
         
    Pub Main | i
      repeat i from 8 to 15
        param[i - 8] := i
        
      repeat i from 0 to 6
        cognew(@entry, @param[i])
        
      coginit(cogid, @entry, @param[7])
      
    dat
                            org 0
                            
    entry                   rdlong  t1, par
                            mov     pin, #1
                            shl     pin, t1
                            shl     t1, #16
                            
                            or      dira, pin
                            mov     ctr, cnt
                            add     ctr, t1
                            
    :loop                   xor     outa, pin
                            waitcnt ctr, delay
                            jmp #:loop
                        
    delay   long  9000000
    ctr     res   1
    t1      res   1
    pin     res   1
    
  • edited 2013-10-12 11:41
    That code is very similar to the code I came up with. I used CogID in the PASM routine to select the output pin. That way I could be sure which cog was blinking which LED.

    Sandy
Sign In or Register to comment.