C++ problem (was Question about DAT variables)
David Betz
Posts: 14,530
Should this code work? In particular, is this statement okay for referencing the "mailbox" variable in the DAT section? The value of that variable gets patched to the address of the mailbox before I load this code so it should have a valid address by the time this code runs.
This is the code I'm wondering about. Will it fetch the 32 bits that are pointed to by "mailbox" and assign it to "params"? The reason I'm asking is that I seem to be getting the wrong value in "params".
Here is the full code:
Edit: Added @ in front of the mailbox references. That didn't fix the problem though.
This is the code I'm wondering about. Will it fetch the 32 bits that are pointed to by "mailbox" and assign it to "params"? The reason I'm asking is that I seem to be getting the wrong value in "params".
repeat while (params := long[mailbox]) == 0
Here is the full code:
' This file was generated by spinwrap. Do not edit!
OBJ
x : "test"
PUB dispatch | params, object, index, arg1, arg2, arg3, arg4
repeat
repeat while (params := long[@mailbox]) == 0
longmove(@object, params, 6)
case index
0: result := x.set_pin({pin} arg1)
1: result := x.set_pins({pin1} arg1, {pin2} arg2)
2: result := x.swap_pins
3: result := x.show_pins
4: result := x.blink
other: result := -1
long[params] := result
long[@mailbox] := 0
DAT
mailbox long 0
Edit: Added @ in front of the mailbox references. That didn't fix the problem though.

Comments
I'm working on a sign controller that holds patterns and their addresses in a table; here's a portion:
MLP_11 long %00000000_00011111 long %00011111_00000000 word ML_Pats word @MLP_00, @MLP_01, @MLP_02, @MLP_03, @MLP_04, @MLP_05 word @MLP_06, @MLP_07, @MLP_08, @MLP_09, @MLP_10, @MLP_11The operational code that fetches starting location of a specific pattern (I use p_ to preface pointer variables):
Finally, the bits from a step in the pattern are retrieved with:
The variable p_pats gets incremented (by 4) for every step.
Enjoy!
Mike
VAR long stack[32] PUB null cognew(patch, @stack{0}) dispatch PUB dispatch | params dira[16..23]~~ repeat repeat until ([COLOR="#FF8C00"]params := mailbox[/COLOR]) outa[16..23] := params mailbox := 0 DAT mailbox long 0 PRI patch : n repeat repeat while mailbox [COLOR="#FF8C00"]mailbox := ++n[/COLOR] waitcnt(clkfreq + cnt) DATHow do you patch mailbox and how do you prepare what is stored at that address?test::test() { memset(m_variables, 0, sizeof(m_variables)); if (m_cogid < 0) { Params *params = (Params *)spinBinary; uint16_t *dbase = (uint16_t *)(uint32_t)params->dbase; uint32_t *dat = (uint32_t *)(spinBinary + 0x0018); params->pbase += (uint16_t)(uint32_t)spinBinary - 4; params->vbase = (uint16_t)(uint32_t)m_variables; params->dbase = (uint16_t)(uint32_t)(m_stack + 2); params->pcurr += (uint16_t)(uint32_t)spinBinary - 4; params->dcurr = params->dbase + 32; dbase[-4] = 2; // pbase + abort-trap + return-value dbase[-3] = 0; // vbase (not used) dbase[-2] = 0; // dbase (not used) dbase[-1] = 0xfff9; // return address *(uint32_t *)dbase = 0; // result dat[0] = (uint32_t)&m_mailbox; m_cogid = cognew(SPINVM, spinBinary); } }This code seems to work correctly:
uint32_t test::set_pins(uint32_t pin1, uint32_t pin2) { uint32_t params[4]; params[0] = (uint32_t)m_variables; params[1] = 1; params[2] = pin1; params[3] = pin2; printf("params %08x\n", (uint32_t)params); m_mailbox = params; while (m_mailbox) ; return params[0]; }But this fails by passing a bad value in params[1]:
uint32_t test::set_pins(uint32_t pin1, uint32_t pin2) { uint32_t params[4]; params[0] = (uint32_t)m_variables; params[1] = 1; params[2] = pin1; params[3] = pin2; m_mailbox = params; while (m_mailbox) ; return params[0]; }Adding the printf seems to make the program work. I'm not sure why. Here is the declaration of the class containing this method:class test { public: test(); ~test(); uint32_t set_pin(uint32_t pin); uint32_t set_pins(uint32_t pin1, uint32_t pin2); uint32_t swap_pins(); uint32_t show_pins(); uint32_t blink(); private: uint8_t m_variables[16]; static uint32_t m_stack[64]; static uint32_t *volatile m_mailbox; static int m_cogid; };The code was compiled with the following command line: