Need to leave space for stack?
Rayman
Posts: 14,670
In C++ mode is the stack the same as in Spin, starting right after program memory?
Do I need to leave some amount of space for the stack?
Or, is the stack inside the LMM interpreter cog?
Do I need to leave some amount of space for the stack?
Or, is the stack inside the LMM interpreter cog?
Comments
Do you happen to know.... If I run a program through Spin2Cpp and the run with PropGCC, does it use any heap? Or, just stack?
To be sure just search the C/C++ output for any occurance of "malloc" or the C++ "new" operator.
I can't help thinking that there must a symbol existing for the beginning of heap space. Just have a feeling I've seen it but can't remeber what it might be.
That code is rigged to use the upper part of RAM as buffer.
But it now appears that that is where C++ is going to start it's stack...
-Tor
I was thinking...
The initial C/C++ on the Prop will have it's stack pointer at the top of memory and the stack grows down. Thus writing over where you want your buffer.
But any other C/C++ tasks started in other COGs will use a stack space that is defined by your main line and is in low memory, probably an array mixed in with all your other data.
So what about this, write a tiny initial man line that starts up your real code in another COG, your real program is then using a stack defined by you. Then have the initial main line code kill itself (cogstop). BINGO all your code is now using stacks away from the top of memory and you can use that for your buffer.
Heap space starts at ___heap_start (__heap_start from C).
That's a clever solution. A simpler one though is to do something like:
The asm statement will set sp to &stack[STACKSIZE]. Obviously this will trash any local variables and anything stored on the stack, which is why I would suggest putting the "real" main into a separate function realmain(). You could get fancy and save the stack pointer and restore it later if you do want to try to return, but it's probably more trouble than it's worth :-).
My solution is not so clever but it would read like poetry compared to yours, which looks like the baud rate is wrong on my serial port:)
Yours is of course a lot more efficient in saving code space.
Hmm.. actually if you are never planning to return from main(), which is the normal case in an MCU app, and you get no parameters for it there is no need for realmain(); Just put that in line assembler at the beginning of you existing main(). It just shifts the stack pointer and everything can proceed as normal after that.
Good point, Dave -- that's an even easier way to change the stack. There's a symbol __stack_end that's used in the initial Spin boot code to set the PAR register, which in turn ends up determining the stack.
I think defining __stack_end has to be done in assembler or in the linker. You can just add something like: to one of your C files to set the top of stack to 0x3000 (or whatever you want).
This feature hasn't been tested much, though, so it'd be good to get some feedback on how well it works. I'm also not sure if it's supported in the XMM modes.