Shop OBEX P1 Docs P2 Docs Learn Events
Help passing data from a cog to hub ram — Parallax Forums

Help passing data from a cog to hub ram

Dr_AculaDr_Acula Posts: 5,484
edited 2011-08-13 05:37 in Propeller 1
I'm working on some experiments with video and external ram chips and the first step has been to get something working slow in Spin and then port it over to Pasm.

But I cannot seem to be able to pass data from pasm to spin.

The code is fairly standard. Define a block of variables (either in VAR or in DAT - both work fine)
VAR            long ramparam1
               long ramparam2

start up a cog
PUB Start(ramptr) : okay
  stop
  okay := cog := cognew(@entry, @ramptr) + 1  

PUB stop
'' Stop ram driver - frees a cog
  if cog
    cogstop(cog~ - 1)   

and change the first variable ramparam from 0 to 1 (it is initialized to zero)
        org

entry
        mov             p,par

loop
        mov             valreg,#1               ' change the value
        wrlong          valreg,p                'value,address
        jmp             #loop     

And then change some pixels on the screen depending on the value passed back
PUB RamStatus | i                                   ' read the status long of the ram driver
    i := ramparam1
   if i == 0
     blueline
   if i == 1
     greenline

This works fine.

But then I added some code to pass back values in a second variable, and everything has gone haywire. My first bit of code was this
        mov             Mem,par                            ' get start of the parameter list
        mov             statusptr1,Mem
        mov             statusptr2,Mem
        add             statusptr2,#4 ' next long hub ram location
loop         
        mov            ValReg,#1               ' temp variable = 1
        wrlong         ValReg,statusptr1                   ' store in hub       value, address
        wrlong         ValReg,statusptr2    ' blank screen, whole program crashes if add this line
     
        jmp             #loop  

Adding the second to last line means the screen goes blank. I've had a few dead ends - I tried declaring the parameters in the main program as a DAT array instead of a VAR array but no change.

The loop in spin that detects this is a tight spin loop, and maybe the prop can't handle this but I doubt this is the reason
  repeat
    ramstatus                                                       ' change line color based on status value

I've got myself very confused between variables and pointers, but I think I have it correct. It is copied out of the propeller manual and also from some other working code examples.

So out of desperation I started writing more code.
        org

entry
        mov             p,par
        rdlong          statusptr1,p
        add             p,#4
        rdlong          statusptr2,p
        mov             p,par ' restore old value

loop
        mov             valreg,#1               ' change the value
        wrlong          valreg,p                'value,address
        mov             mem,p
        nop
        'nop                  ' two nop and it works, three nops and there is a blank screen
        add             mem,#4
        wrlong          valreg,mem
        jmp             #loop 

There are some strange things here. There are three outcomes - the program changes the variable, the program does not change the variable or the whole program crashes and the screen goes blank.

The outcome is affected by the number of NOPs added. The outcome changes depending on the number of NOPs and the addition of other code even when the other code does not do anything. eg the 2nd to 5th lines don't do anything, yet adding them in changes the behaviour further down and the number of NOPs needed to get it to work.

Sometimes it works with 1 NOP, sometimes 3.

I suspect I am missing something really obvious here. Maybe I'm not stopping the cogs on a reboot. Maybe the hub variables are too near the top of ram (the video buffer is fairly large). Maybe something overwrites something critical.

Or most likely, I'm making a silly syntax error.

I've attached a copy that does work. If someone can spot the obvious error I'd be most grateful. If not, then this code is running with the TV on pins 24-26 and all the bits of code that read the external ram can be commented out as the issue here is why it works fine passing one variable but falls apart when passing two.

Any help would be most appreciated!

Comments

  • kuronekokuroneko Posts: 3,623
    edited 2011-08-13 05:09
    Dr_Acula wrote: »
    PUB Start(ramptr) : okay
      stop
      okay := cog := cognew(@entry, [COLOR="red"]@[/COLOR]ramptr) + 1  
    
    The Start method is called with @ramparam1 as the parameter. So you definitely don't want to change that address to some stack location (address of method parameter ramptr), i.e. just remove the offending @.
  • Heater.Heater. Posts: 21,230
    edited 2011-08-13 05:12
    I don't think your cognew call should have @ramptr. Remove @ because presumeably the ramptr passed to your start method is already an address obtained with an @.
  • Dr_AculaDr_Acula Posts: 5,484
    edited 2011-08-13 05:23
    Thanks guys - that fixed it.

    I really love this propeller forum!

    BTW how do you put "solved" next to a thread?
  • Heater.Heater. Posts: 21,230
    edited 2011-08-13 05:34
    Arn't you supposed to be asleep? It's mid afternoon here.
  • Dr_AculaDr_Acula Posts: 5,484
    edited 2011-08-13 05:37
    I'm high on coding...

    Mind you, I'm making some silly mistakes here.
    VAR
    
      long  cog
    
    PUB Start(ramptr) : okay
      stop
      okay := cog := cognew(@entry, ramptr) + 1  
    
    PUB stop
    
    '' Stop ram driver - frees a cog
    
      if cog
        cogstop(cog~ - 1)   
    
    DAT
    
    '*******************************
    '* Assembly language Ram driver *
    '*******************************
    
    ' note may be faster to read in 4 bytes and do a wrlong as a wrbyte x 4 will be longer
    
            org
    
    entry
            mov             p,par
            rdlong          screen_buf,p                 ' screen buffer location
            'mov             t1,p             ' uncommenting this line crashes the whole program
            add             p,#4
            rdlong          statusptr2,p
            mov             p,par ' restore old value
    
    loop
            mov             valreg,#1               ' change the value
            wrlong          valreg,p                'value,address
            mov             mem,p
            add             mem,#4
            wrlong          valreg,mem
            jmp             #loop        
    
            ' Uninitialized data
    
            t1              res 1
            status          res 1
            mem             res 1
            ValReg          res 1
            screen_buf      res 1
            statusptr2      res 1
            p               res 1
            
            fit 496
    

    This code works fine. But if I uncomment this line
            'mov             t1,p             ' uncommenting this line crashes the whole program and the screen goes blank
    

    The screen goes blank.

    But variable t1 is not used anywhere else.

    Adding a NOP in place of that line also crashes the program.

    I think I still have more to learn with PASM.
Sign In or Register to comment.