Difference between VAR and DAT
andrewsi
Posts: 59
I'm working on my first project and have encountered some odd behavior where I have a freerunning cog from a secondary object measuring pulse widths, and it's returning the pulsewidths back to the main program object by writing to that long... e.g.:
In the secondary object, in a new cog:
What I seem to be finding is a difference in behavior between VAR and DAT as the main program's "holder" for returning values from the free running cog. I think that if "pulse" is defined as a var, it's never getting written by the measuring cog, but if pulse is defined as a long in a DAT block instead, then it works.
The manual is not super clear on this point, but it looks like a DAT block is just a set of VARs where you get to preinitialize it with data. I think I must be fundamentally misunderstanding something, though, about scope and accessibility of VARs from other objects, even if passed by reference.
If anybody can enlighten me on the subtleties, (or really, on the recommended means of passing data asynchronously from a cog running in the context of one object to a cog running in the context of a different object), that would be greatly appreciated.
Thanks for helping with my newbie question!
Andy
In the secondary object, in a new cog:
PUB func (pulsewidth) 'called as func(@pulse) ... LONG[pulseWidth] := tempcount
What I seem to be finding is a difference in behavior between VAR and DAT as the main program's "holder" for returning values from the free running cog. I think that if "pulse" is defined as a var, it's never getting written by the measuring cog, but if pulse is defined as a long in a DAT block instead, then it works.
The manual is not super clear on this point, but it looks like a DAT block is just a set of VARs where you get to preinitialize it with data. I think I must be fundamentally misunderstanding something, though, about scope and accessibility of VARs from other objects, even if passed by reference.
If anybody can enlighten me on the subtleties, (or really, on the recommended means of passing data asynchronously from a cog running in the context of one object to a cog running in the context of a different object), that would be greatly appreciated.
Thanks for helping with my newbie question!
Andy
Comments
In which case there will be multiple copies of your variables in the VAR section created. One copy per instance of the same object.
BUT there will only be one copy of the DAT variables. That copy is shared among all instances of the object.
Perhaps this detail does not apply to you as you only have on instance of each object.
In that case we probably need to know more about what you are doing, and see the complete code to be able to help.
It may, perhaps, be related somehow to the fact that the call to the "Start" method in the subobject was by reference, but then the cognew operation in that method simply passes the parameter on to the actual method that does the writing back to the original address. I figure since what the parameter contains is the address of the global variable, that passing THAT on by value to the newly started cog should be perfectly fine, since the address to write back to hasn't changed. (And, this is working for a DAT address if not for a VAR one.)
Anyway, thanks for the ideas.
If I undestand correctly what you are saying then I think you are quite right in that assumption.
For sure passing VAR addresses, obtained with @, around should work. In my ZOG emulator, for example, I have:
And then use it to start a COG so:
zog.start then starts some PASM in a COG passing that parameter on via PAR.
From what you've said, VAR should work too. Something is fishy. I think you should post your code, though if it's lengthy, please cut it down as much as possible.