RISC-V riscv32 embedded - Stack address
The issue occurs on a risc-v 32bit microcontroller, namely the CH32V307 from WCH, that I currently try to add to fpc.
The stack address is defined in the linker script providing a symbol. The linker script is generated in the file compiler/systems/t_embed.pas with the following line:
Add('_stack_top = 0x' + IntToHex(srambase+sramsize-1,4) + ';');
The programm crashes with a hard fault in the moment the stack is used the first time. As far as I understand the microcontroller only allows 32bit alligned memory access. It worked after changing the line to:
Add('_stack_top = 0x' + IntToHex(srambase+sramsize-4,4) + ';');
But I'm not sure what is the best way to fulfill this without breaking existing code. In t_embed.pas it could be distinguished between each controller, but that would perspectively blow up the file. The offset could also be adjusted in the controllers startup code, where the stack pointer is initialized. But then it would differ from the original startup code making future porting of other devices more difficult. Or it could be changed to -4 for all devices, perhaps it doesn't break any compatibility, but just wastes 3 byte for devices that would support byte allignment? Or is the -1 even wrong, as a a 32bit access (riscv32) to (srambase+sramsize-1) would result in an access including the 3 bytes outside of the defined SRAM region?