Propeller Manual

Table Of Contents
2: Spin Language Reference – COGINIT
Propeller Manual v1.1 · Page 77
frame in the specified stack space before launching the cog. Relaunching a cog with a
COGINIT command specifying the same stack space that it is currently using will likely cause
the new stack frame image to be clobbered before the cog is restarted. To prevent this from
happening, first perform a
T
COGSTOP on that cog, followed by the desired COGINITT.
Spin Code (Syntax 1)
To run a Spin method in a specific cog, the
COGINIT command needs the cog ID, the method
name, its parameters, and a pointer to some stack space. For example:
coginit(1, Square(@X), @SqStack) 'Launch Square in Cog 1
This example launches the Square method into Cog 1, passing the address of X into Square
and the address of
SqStack as COGINIT’s stack pointer. See COGNEW, page 78, for more
information.
Propeller Assembly Code (Syntax 2)
To run Propeller Assembly code in a specific cog, the
COGINIT command needs the cog ID,
the address of the assembly routine, and a value that can optionally be used by the assembly
routine. For example:
coginit(2, @Toggle, 0)
This example launches the Propeller Assembly routine, Toggle, into Cog 2 with a PAR
parameter of 0. See the example in the assembly syntax description of
COGNEW, page 78, for a
more detailed example keeping in mind that the above
COGINIT command can be used in
place of
T
COGNEW in that example.
The Parameter Field
It’s important to note that the Parameter field is intended to pass a long address, so only 14
bits (bits 2 through 15) are passed into the cog’s
PAR register; the lower two bits are always
cleared to zero to ensure it’s a long-aligned address. A value other than a long address can
still be passed via the Parameter field, but will have to be limited to 14 bits, must be shifted
left by two bits (for the
COGNEW/COGINIT command), and will have to be shifted right by two
bits by the assembly program that receives it.