using a local variable in cognew fails
David B
Posts: 592
Is it a known mistake to use a local variable when starting an asm cog?
I ran into a bug over the weekend that I traced to just that, as in this pseudocode.
This asm cog code worked perfectly until the main started setting another completely unrelated variable. When "do_something_else(parameter)" was called, suddenly the emitted frequency was way off.
In the actual code, the "start_nco" and the asm_sine code are in a separate object file from main, and the "do_something_else(parameter)" was also in a separate object file, in case that makes a difference.
I guess that the "start_nco" function starts the cog, then exits, releasing its ownership of the "counts" variable before the cog has initialized far enough along to read it. When the cog finally gets around to reading from that address, the value had been overwritten.
I ran into a bug over the weekend that I traced to just that, as in this pseudocode.
This asm cog code worked perfectly until the main started setting another completely unrelated variable. When "do_something_else(parameter)" was called, suddenly the emitted frequency was way off.
In the actual code, the "start_nco" and the asm_sine code are in a separate object file from main, and the "do_something_else(parameter)" was also in a separate object file, in case that makes a difference.
I guess that the "start_nco" function starts the cog, then exits, releasing its ownership of the "counts" variable before the cog has initialized far enough along to read it. When the cog finally gets around to reading from that address, the value had been overwritten.
VAR long localvar long varcounts ' <-- the bug was fixed by moving "counts" up to the VAR section PUB main start_nco(400) do_something_else(2) repeat PUB start_nco(freq) | counts counts := ((53687 * freq)/1000) cognew(@asm_sine, @counts) PUB do_something_else(parameter) ' return ' asm cog frequency is correct if this return is executed localvar := parameter ' asm cog frequency is wrong if this line executes return DAT org 0 asm_sine rdlong frqa, par configure counter to emit a frequency based on counts
Comments
So yes, you are asking for trouble.