Shop OBEX P1 Docs P2 Docs Learn Events
Multiple dat sections and variables — Parallax Forums

Multiple dat sections and variables

CncjerryCncjerry Posts: 64
edited 2012-06-11 22:28 in Propeller 1
If I have a number of assembler routines in one module that will each be loaded in their own cog, can each separate dat section have the same variable names? It doesn't look like it to me. For instance I have a mask for the LEDs on the QuickStart and would like to access them across three cogs by the same name. I also want each cog to have the led variable stacked the same way in the dat section at the end of the program not unlike a data block.

I thought about using separate modules but I thought I read that you can't cognew a routine from another module.

Jerry

Comments

  • msrobotsmsrobots Posts: 3,709
    edited 2012-06-09 23:46
    Hi Cncjerry,

    allmost right. You can't do a cognew to a routine in a module from outside.

    But you can cognew that routine inside a PUB xxx in the module and call it there from outside with module.xxx ...

    Enjoy!

    Mike
  • kuronekokuroneko Posts: 3,623
    edited 2012-06-10 00:13
    Cncjerry wrote: »
    I thought about using separate modules but I thought I read that you can't cognew a routine from another module.
    That only applies to SPIN methods. PASM is more relaxed in that respect. All you need is a hub address and a(n optional) parameter. I'd suggest you go for the one-cog-image-per-file approach (unless you can re-use the code and control its behaviour with configuration settings).
  • pik33pik33 Posts: 2,397
    edited 2012-06-10 01:30
    Cncjerry wrote: »
    can each separate dat section have the same variable names? It doesn't look like it to me.
    Jerry

    No, it can not. All names are unique across all spin file. And it even didn't warn if you use name defined in one .dat module in another .dat module, which can cause strange and hard to find errors.
  • Heater.Heater. Posts: 21,230
    edited 2012-06-10 02:26
    Cncjerry,
    No you canot have the same labels in different DAT sections of the same object file.
    Worse than that, if you have two sections of PASM in a single object that you wish to load into two cogs they cannot share a single DAT definition like a bit mask patern. It will compile but the patern will not be in the right place for one of the cogs and it will fail.
    Also if you have multiple PASM for multiple cogs in a single file you will have to be sure to use org 0 at the start of each one.
    All in all, I'd suggest keeping each cogs code in it's own file.
  • CncjerryCncjerry Posts: 64
    edited 2012-06-10 07:43
    So when you say file, do you mean object module? This is mostly assembly. If I have multiple files (object modules?) then do I need a start or init routine at the top that then cognews the dat assembler routines?
  • Heater.Heater. Posts: 21,230
    edited 2012-06-10 09:03
    I don't like to talk of object files or object modules because traditionally these terms refer to the binary files that result from compiling a source file. Many object files may then be linked toggether to make the final executable program.

    In Spin an object is a single source file. So yes the simple thing to do is have one Spin object (a source file) containing each piece of PASM you want to start in it's COG. Together with a little start method, in Spin, for it.
  • Duane DegnDuane Degn Posts: 10,588
    edited 2012-06-10 09:05
    Cncjerry wrote: »
    So when you say file, do you mean object module? This is mostly assembly. If I have multiple files (object modules?) then do I need a start or init routine at the top that then cognews the dat assembler routines?

    Usually a separate object (file) would have a "Start" method with a cognew statement. But instead of a "Start" method, you could have a method that returns the location of your PASM code to the parent object and the parent object could then use a cognew on that address. I'm having a hard time thinking of when it would be better for the parent object to start the new cog rather than just having the child object start it.

    If you try to start a Spin method in a child object using cognew in the parent object, you'll get unexpected results (assuming you were expecting normal results). You should only start Spin methods from within the object(file) containing the method.

    BTW, The convention is call a method "Start" if it starts a new cog. If it just sets I/O pins and some variables you're supposed to call it "Init". I'm not sure how much this convention is followed (I know I read about this convention in one of Parallax's documents about the Prop). I see a lot of "Start" methods that don't start a new cog.
  • Phil Pilgrim (PhiPi)Phil Pilgrim (PhiPi) Posts: 23,514
    edited 2012-06-10 09:16
    There actually is a way you can reuse DAT label names. Give the name of each entry point (the first instruction of each PASM cog, following its own org 0 -- don't forget that) a unique name. The precede the names of all labels within the cog with a colon, like this:
             org     0
    cog1     mov     :ptr,par
             rdlong  :led_msk,:ptr
    
    :main_lp ...
             ...
             jmp     #:main_lp
    
    :ptr     res     1
    :led_msk res     1
    
             org     0
    cog2     mov     :ptr,par
             rdlong  :led_msk,:ptr
     
    :main_lp ...
             ...
             jmp     #:main_lp
    
    :ptr     res     1
    :led_msk res     1
    

    It's a big PITA to have to do it this way, but it does work. And it prevents errors due to referencing a label across cog boundaries.

    -Phil
  • CncjerryCncjerry Posts: 64
    edited 2012-06-10 16:19
    So I had my code split with org 0 statements and it gave me an error so I must have done something wrong. Could it have had something to do with res vs long variables? I have gone back to separate files so don't spend much time on it.
  • Phil Pilgrim (PhiPi)Phil Pilgrim (PhiPi) Posts: 23,514
    edited 2012-06-10 16:28
    Well, just for jollies, post your code anyway. It may still be instructive for others.

    -Phil
  • CncjerryCncjerry Posts: 64
    edited 2012-06-11 11:08
    sorry, code is gone. This might help though, does org without a 0 default to 0? I might not have had a 0 on the second cog routine.
  • Duane DegnDuane Degn Posts: 10,588
    edited 2012-06-11 11:10
    Cncjerry wrote: »
    does org without a 0 default to 0?

    Yes, it does default to 0.
  • CncjerryCncjerry Posts: 64
    edited 2012-06-11 13:32
    Here you go. The first file gives me a compile error on leds pointing to the second (lower) definition of leds. Changing leds to leds2 on the lower code compiles and works fine.

    compile error
    VAR
      long X
      long Y
      long stack1
      long stack2
      
    Pub Start
          cognew(@start1, @stack1)
          cognew(@start2, @stack2)
          repeat
            x:=Y
          
    DAT
            org 0
    start1
            or  dira, leds
            mov outa, leds
            jmp   start1
    leds    long  $0F<<16
            org 0
    start2
            or  dira, leds
            mov outa, leds
            jmp   start2
    leds   long  $f0<<16   
    
    


    working
    VAR
      long X
      long Y
      long stack1
      long stack2
      
    Pub Start
          cognew(@start1, @stack1)
          cognew(@start2, @stack2)
          repeat
            x:=Y
          
    DAT
            org 0
    start1
            or  dira, leds
            mov outa, leds
            jmp   start1
    leds    long  $0F<<16
            org 0
    start2
            or  dira, leds2
            mov outa, leds2
            jmp   start2
    leds2   long  $f0<<16   
    
    
  • Heater.Heater. Posts: 21,230
    edited 2012-06-11 14:10
    And that is what I would expect and is correct.
  • CncjerryCncjerry Posts: 64
    edited 2012-06-11 14:44
    Heater, what are you doing, stalking me? Thanks for the pointer on the other thread. That is probably going to fix a bunch of recent problems.

    I overlooked the colon ":" in Phi's example above and was on the org 0 method.

    What does the colon in a label prefix mean?
  • Heater.Heater. Posts: 21,230
    edited 2012-06-11 14:54
    "stalking"? No, just addicted to reading these threads and happy to help out when I can.
    As for the colon thing, Imagine your PASM was split into two or more funtions that get called from some other main function. All those functions would need some normal label.
    But perhaps they all have some local place to jump to internaly and you might want to give those places a generic label like "loop". Well normally you would have to label them as "loop1", "loop2" etc. But in PASM you can call them all ":loop" and there will be no conflict. They are local to the function.
  • Phil Pilgrim (PhiPi)Phil Pilgrim (PhiPi) Posts: 23,514
    edited 2012-06-11 15:04
    Jerry,

    You can think of regular, non-colon labels as "fences" that enclose areas of colon labels.The colon labels are known within their own fenced-in area, but not outside of it. Therefore you can reuse them with impunity between multiple fenced-in areas.

    BTW, this statement in my example above may actually produce an error:
    cog2     mov     :ptr,par
    

    Being "on the fence", it can "see" both the :ptr label above it and the one below it. You may need to add an extra fence above the entry point or add a statement before the mov.

    -Phil
  • kuronekokuroneko Posts: 3,623
    edited 2012-06-11 16:56
    cog2     mov     :ptr,par
    

    Being "on the fence", it can "see" both the :ptr label above it and the one below it.
    That's not the case. Only the label below is seen. IOW there is no "on the fence".
  • Phil Pilgrim (PhiPi)Phil Pilgrim (PhiPi) Posts: 23,514
    edited 2012-06-11 17:18
    Ah so, I was right the first time. Thanks.

    -Phil
  • CncjerryCncjerry Posts: 64
    edited 2012-06-11 22:12
    so are you guys saying the :loop is reusable or the (no colon)loop? So if I get it right, if as above I have a start1 function (preceeded by org 0) then every name preceeded by a colon would be local to the (non colon)label function. This makes a little sense.

    Also, per my other thread and helped by Heater, it looks like one of my main problem with consistency was brought about by not prefacing my jumps with the # for literals. Surprised it worked at all. Really strange errors.

    One other question on that note. In my start1 and start2 functions above, is there a way to "phase" the second function so that they become more clock synchronized? So let's say I have a start1 cog that sets a bit, and a start2 cog that clears a bit and I wanted them to be phased so that the start1 sets the bit on one clock cycle and the start2 clears it on the next. Not sure where I would use this, more of an academic issue but curious.

    Thanks for all the help. This is a great forum.


    Jerry
  • Phil Pilgrim (PhiPi)Phil Pilgrim (PhiPi) Posts: 23,514
    edited 2012-06-11 22:28
    :loop and all other colon-preceded labels are reusable in other blocks that are separated by non-colon labels.

    -Phil
Sign In or Register to comment.