Shop OBEX P1 Docs P2 Docs Learn Events
Cognew questions... — Parallax Forums

Cognew questions...

RaymanRayman Posts: 14,162
edited 2008-01-29 20:19 in Propeller 1
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?

Comments

  • Paul BakerPaul Baker Posts: 6,351
    edited 2008-01-29 17:48
    1) no, cognew only works for methods within the same object.

    2) Never tried

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Paul Baker
    Propeller Applications Engineer

    Parallax, Inc.
  • hippyhippy Posts: 1,981
    edited 2008-01-29 18:36
    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[noparse][[/noparse] 1024 ]
    
    PRI Main
      CogNew( RunsInOwnCog, @stack+4 )
      repeat
        tv.Out( $01 )
        tv.Str( String( "Count : " )
        tv.Dec( long[noparse][[/noparse] @stack ] )
    
    PRI RunsInOwnGog | stackBase
      stackBase := @return - ?
      repeat
        long[noparse][[/noparse] stackBase ] := long[noparse][[/noparse] 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[noparse][[/noparse] $7FF0 ] := @return" in RunsInOwnCog then print "long[noparse][[/noparse] $7FF0 ] - @stack" in the main and you'll be able to determine the X.
  • deSilvadeSilva Posts: 2,967
    edited 2008-01-29 19:41
    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[noparse][[/noparse]100]
    
      cognew(aRoutine, @aStack)
    
    PUB aRoutine | theStack[noparse][[/noparse]90]
       theStack[noparse][[/noparse]0] := 4711
    


    Now 4711 can easily be retrieved in "aStack".

    Post Edited (deSilva) : 1/29/2008 7:55:10 PM GMT
  • RaymanRayman Posts: 14,162
    edited 2008-01-29 19:46
    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[noparse][[/noparse]0] := 4711
    

    just before the cog is stopped?
  • deSilvadeSilva Posts: 2,967
    edited 2008-01-29 19:52
    Never!
    Especially the FIRST elements will contain most relevant internal data.
  • RaymanRayman Posts: 14,162
    edited 2008-01-29 20:19
    Well, I just tried it a bit and it appears that stack[noparse][[/noparse]0] holds the cog# and stack[noparse][[/noparse]1] holds something else terribly important..

    But, it doesn't seem to mind me setting stack[noparse][[/noparse]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[noparse][[/noparse]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[noparse][[/noparse]2])
    PUB HelloWorld    'the first PUB routine is assumed the be the main one
      
    
      text.str(string("Hello World"))  'Output the string !
    
      stack[noparse][[/noparse]2]:=57
        'Note that the Spin interpreter will halt on this last line,
        '  but, the text driver will continue to display on screen
    
Sign In or Register to comment.