P2 PASM 4xCOG Syncronize
pic18f2550
Posts: 400
I would like to know how to get 4 COGs in lockstep.
My idea is to use GETCT() to load the counter value and wait in the PASM part until it expires.
PUB main() | mode
sync := GETCT() {{ Start all COGs (16sec to overflow}}
sync := sync+256_000_000 * 4 {{ wait 4 sec }}
cog[1] := pha1.ini(@sync, @p_array1, @s_array1, 64, 64)
cog[2] := pha2.ini(@sync, @p_array2, @s_array2, 64, 64)
...
DAT
sync
mov t0, ptra
addct1 t0, #0 {{ 2 }}
sync1
{{ ==== SYNC }}
waitct1 {{ 2+ }}
...
Translated with www.DeepL.com/Translator (free version)

Comments
Using waitatn ?
The 4 cogs starts with waitatn, the spin code issues a cogatn(mask) with the 4-cogs bit set (after waiting a bit to allow all 4 to load and start).
Just wondering.
I still have to make some preparations in some COGs.
The syncronization comes only a little later in code.
(this is not present in the example above).
You can use a variable to signal when the cogs are waiting, same technique used with P1.
Try this:
CON _clkfreq = 160_000_000 VAR byte ready[8] PUB main() | c1, c2, c3, c4 c1 := coginit(16, @driver, @ready) c2 := coginit(16, @driver, @ready) c3 := coginit(16, @driver, @ready) c4 := coginit(16, @driver, @ready) repeat while ready[c1]==0 || ready[c2]==0 || ready[c3]==0 || ready[c4]==0 cogatn((1 << c1) | (1 << c2) | (1 << c3) | (1 << c4)) repeat DAT org $000 driver cogid a ' get our id and add a, ptra ' use as offset into the table wrbyte #$FF, a ' signal we are ready waitatn ' patiently wait... getct ct debug(UDEC(ct)) jmp #$ a res 1 ct res 1That's pretty neat, @macca.
I used the cnt to sync all 8 cogs in a p1 logging program. For that program i needed each cog to start precisely 1 clock after each other.
PASM
Keeps the COG #2 running?
Does this also work with multiple COGs at the same time?
Yes, it's a bit-mask rather than an ID number. Value #2 is bit#1 so cog#1 gets the event. If value was #3 then cog#0 and cog#1 both get an attention event.