Shop OBEX P1 Docs P2 Docs Learn Events
Dat vs Var and address — Parallax Forums

Dat vs Var and address

bdickensbdickens Posts: 110
edited 2008-10-31 14:12 in Propeller 1
This seems like such a basic question and yet I can't seem to find a clear answer.

I have a variable array that needs to be visible to a wide range of routines (ServoStage). Currently, I pass the address and the number of elements and then play decoding games with BYTE. From what I'm reading, the DAT block is "global" among cogs, so there is only one instance. So my scheme seems to work, but it looks clumsy.

I can define an array in VAR (IncomingSpeed) but that is local to this object and if I happen to have two of them, I get two copies (not good). But it does look cleaner from a coding standpoint.

Is there a more elegant way to pass an array ?
If not, is it worth copying the array to a local variable, and then putting it back. ? Something like ByteMove[noparse][[/noparse]ServoState,IncomingSpeed, 6] ? What do I lose in speed ?
Can I move bytes between objects defined "differently" ?

I'm presuming their is no way to "map" a data structure on top of an address. (but hey; correct me if I missed that)


DAT
ServoState Byte 1, 1, 1, 1, 1, 1 'Values to be sent
Var
Byte IncomingSpeed[noparse][[/noparse]6] ' Values from input

Comments

  • Mike GreenMike Green Posts: 23,101
    edited 2008-10-31 13:51
    1) Variables (whether in a VAR section or a DAT section) are local to the object where they appear. This has absolutely nothing to do with cogs, only with objects. These are completely different concepts.

    2) If you want to access a variable (array, scalar, whatever), in some other object, you pass its address (with "@") to a routine in that other object. That routine can save a local copy of the address or you can pass it whenever a routine is called that requires it. Both schemes have been used. You reference that variable by using "BYTE[noparse][[/noparse] address ]" or "WORD[noparse][[/noparse] address ]" or "LONG[noparse][[/noparse] address ]". If the variable is an array, you can use something like "BYTE[noparse][[/noparse] address ] [noparse][[/noparse] i ]".
  • bdickensbdickens Posts: 110
    edited 2008-10-31 13:57
    Mike Green said...
    If the variable is an array, you can use something like "BYTE[noparse][[/noparse] address ] [noparse][[/noparse] i ]".

    Thats what I have. Just makes the code look ugly. But if it is independent, then the BYTEMOVE[noparse][[/noparse]target, source, number] should work ?

    Thanks for the quick answer.
  • Mike GreenMike Green Posts: 23,101
    edited 2008-10-31 14:05
    BYTEMOVE does work, just remember that the first two parameters are addresses (pointers).

    This notation is bulky and relatively inefficient (takes more memory and time than local accesses to variable names). I've written large programs using it and sometimes converted back to making local copies of the variables when they're just scalars. LONGMOVE is very fast if the data is long-aligned.
  • bdickensbdickens Posts: 110
    edited 2008-10-31 14:12
    Perfect. Thanks for the answer. I'll change the code accordingly.
    Thanks again
Sign In or Register to comment.