PDA

View Full Version : A better way to share data between SPIN and ASM



dnaddor
01-09-2009, 04:50 AM
Hi y'all.

I've never liked how I shared data between SPIN and ASM. I often have lots of different sized structures, and it is too easy to make a mistake in the offsets (like forgetting to multiply by 4). I prefer to access all variables by name.

Today, I figured out an easy way to do this. The "trick" is to have a DAT section with this line:

Memory long @Memory

and then start a cog with this:

COGSTART(@CogThread, @Memory - Memory)

Now in ASM, you can access data easily by using PAR and adding the offset to the variable.

I included a simple test program. All it does is print 100+200=300.

mpark
01-09-2009, 07:24 AM
Clever!

Kye
01-09-2009, 07:24 AM
This is much better:

long Something

PUB

SomethingAddress := @Something

cognew(@entry,0)

DAT

entry
....

somethingAddress long 0

----------------------------------------------------------------------------------------------------------

The idea is that you embed the addresses of all the data you want to acess into the image for the cog to run. Once this is done you can simply use the addresses in getting data.

As in:

rdlong buffer, somethingAddress

▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Nyamekye,

Kye
01-09-2009, 07:27 AM
And, how are you even doing that? The compilier usually flags that as an error if you put the "@" symbol in a dat section.

Did they release a new version?

▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Nyamekye,

dnaddor
01-09-2009, 08:57 AM
The reason I don't use:

long Something

PUB

SomethingAddress := @Something

cognew(@entry,0)

DAT

entry
....

somethingAddress long 0

is because it gets tedious when you have lots of variables:

DAT

EntryNumber long 0
HashIndex long 0
ImJustAByte byte 0
BigTable long 0 [16]
:

I would have to create a second variable for the address of each of these. I would have to create an assignment for each of these. With my method, I only have one copy of each variable. I also don't waste any additional RAM.

jazzed
01-09-2009, 09:47 AM
@dnaddor
The example is excellent. Very easy to read. Of course the object will be a singleton, but that is often useful anyway for device drivers.

▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
--Steve

Kye
01-10-2009, 01:41 AM
That is true dnaddor, but notice. You have to first acess all the variables and get them into the driver after its launched.

You are using way more memory from all of that.

Enjoy your new trick. =)

▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Nyamekye,