Order of Spin parameters in memory
David Betz
Posts: 14,516
Can I count on the fact that adjacent parameters in a Spin argument list are actually contiguous in memory? I have the following code that I'd like to simplify:
Can I change it to this and have it still work?
I would normally not do something like this in C since it depends on the underlying compiler implementation. Should I stay away from it in Spin as well?
CON INIT_MBOXES = 0 ' base of an array of mailboxes INIT_CONFIG_1 = 1 ' driver specific configuration INIT_CONFIG_2 = 2 INIT_CONFIG_3 = 3 INIT_CONFIG_4 = 4 _INIT_SIZE = 5 PUB start(code, mbox_count, mboxes, config1, config2, config3, config4) | params[_INIT_SIZE] xm_mbox := mboxes ' use the first mailbox longfill(mboxes, 0, _MBOX_SIZE * mbox_count) long[mboxes + _MBOX_SIZE * mbox_count] := $00000008 params[INIT_MBOXES] := mboxes params[INIT_CONFIG_1] := config1 params[INIT_CONFIG_2] := config2 params[INIT_CONFIG_3] := config3 params[INIT_CONFIG_4] := config4 cognew(code, @params)
Can I change it to this and have it still work?
PUB start(code, mbox_count, mboxes, config1, config2, config3, config4) xm_mbox := mboxes ' use the first mailbox longfill(mboxes, 0, _MBOX_SIZE * mbox_count) long[mboxes + _MBOX_SIZE * mbox_count] := $00000008 cognew(code, @mboxes)
I would normally not do something like this in C since it depends on the underlying compiler implementation. Should I stay away from it in Spin as well?
Comments
(spin2cpp has to implement some slightly painful memory shuffling to work around exactly this issue in order to maintain compatibility with Spin)
Eric
The code would be changed to this:
I have used this scheme more than once, assuming that the parameters were safe... I'm thinking now that I need to have the cog flag my Spin code that it has picked up all of the parameters before returning from the Spin method that instantiates the cog.
Thanks for the tip.
When I first started coding the Propeller, I would copy the parameters to a DAT or VAR, and then pass that way. Then I tried to compress parameters (they often are not long values, so I can shift and combine multiple bytes and/or words). Then I tried putting DAT blocks after the cog PASM code so the values at coginit were copied to the cog RAM automatically. I have come to the conclusion that the hub RAM used and/or number of cycles executing the SPIN code to compress the parameters is more than just passing a start address in a mailbox - as you are doing here. It's proven reliable for me.