PDA

View Full Version : Cognew questions...



Rayman
01-30-2008, 01:42 AM
1. Can you COGNEW a routine in an included object? I tried it and it worked, but then rebooted!

I'm playing a wav file in the background... This code works as expected:


Pub PlayBackgroundWav(sFilename)
'play wav file in background using a cog
return cognew(PlayForegroundWav(sFilename),@stack)
'return cognew(wav.play(sFilename),@stack)

Pub PlayForegroundWav(sFilename):bOK
'play a wave and return status after playing
return wav.play(sFilename)


but, if I try the commented line in "PlayBackgroundWav", the file plays and then the system reboots...

I tried making the stack very big, but it didn't help...

2. Can I use the stack space to return some values by storing things there at the end of the proceedure that the CogNew calls? Anybody tried this?

Paul Baker
01-30-2008, 01:48 AM
1) no, cognew only works for methods within the same object.

2) Never tried

▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Paul Baker (mailto:pbaker@parallax.com)
Propeller Applications Engineer
[/url][url=http://www.parallax.com] (http://www.parallax.com)
Parallax, Inc. (http://www.parallax.com)

hippy
01-30-2008, 02:36 AM
Rayman said...
Can I use the stack space to return some values by storing things there at the end of the proceedure that the CogNew calls? Anybody tried this?


You can do this ...




VAR
long stack[ 1024 ]

PRI Main
CogNew( RunsInOwnCog, @stack+4 )
repeat
tv.Out( $01 )
tv.Str( String( "Count : " )
tv.Dec( long[ @stack ] )

PRI RunsInOwnGog | stackBase
stackBase := @return - ?
repeat
long[ stackBase ] := long[ stackBase ] + 1





The @stack+N in the CogNew allocates a common passing area for the separately running cog. The only thing I can't remember is how to determine the stackbase. I recall it's simply a matter of 'stackBase := @return - X' but cannot remember what X is !

Put a "long[ $7FF0 ] := @return" in RunsInOwnCog then print "long[ $7FF0 ] - @stack" in the main and you'll be able to determine the X.

deSilva
01-30-2008, 03:41 AM
hippy said...
The only thing I can't remember is how to determine the stackbase.

It's more or less @stackBase. I think the stack contains returnn address, parameters, then local variables. When calling without any parameters this offset can easily be determined by looking into the stack (I shall give you the offset in 10 minutes...)

As the stack is used for internal operations I do not see how to work with such things dangerlessly....

What you can do - but for what reason? - is



PUB main | aStack[100]

cognew(aRoutine, @aStack)

PUB aRoutine | theStack[90]
theStack[0] := 4711


Now 4711 can easily be retrieved in "aStack".

Post Edited (deSilva) : 1/29/2008 7:55:10 PM GMT

Rayman
01-30-2008, 03:46 AM
I was thinking that perhaps the stack was only used starting and during the cognew'd routine... At the very end of this routine, how about:


aStack[0] := 4711

just before the cog is stopped?

deSilva
01-30-2008, 03:52 AM
Never!
Especially the FIRST elements will contain most relevant internal data.

Rayman
01-30-2008, 04:19 AM
Well, I just tried it a bit and it appears that stack[0] holds the cog# and stack[1] holds something else terribly important..

But, it doesn't seem to mind me setting stack[2]...


CON 'Constants Section
_clkmode = xtal1 + pll16x 'using 5 MHz crystal in pll16x mode
_xinfreq = 5_000_000 'to achieve 80 Mhz clock frequency

OBJ 'Objects Section
'We need the VGA (or TV) text driver

text : "vga_text" 'For NTSC TV Video: Comment out this line...
'text1 : "tv_text" 'and un-comment this line (need to change pin parameter for text.start(pin) command below too).

VAR
long stack[100]
PUB Main
text.start(16) 'Start the VGA/TV text driver (uses another cog)
'The parameter (16) is the base pin used by demo and proto boards for VGA output
'Change (16) to (12) when using "tv_text" driver with demo board as it uses pin#12 for video
'text1.start(12)
CogNew(helloworld,@stack)
waitcnt(cnt+clkfreq)
text.out(13)
text.dec(stack[2])
PUB HelloWorld 'the first PUB routine is assumed the be the main one


text.str(string("Hello World")) 'Output the string !

stack[2]:=57
'Note that the Spin interpreter will halt on this last line,
' but, the text driver will continue to display on screen