RISC-V riscv32 embedded - Return command in interrupts
The issue occurs on a risc-v 32bit microcontroller, namely the CH32V307 from WCH, that I currently try to add to fpc.
Functions/procedures are returned with the assembler command "JALR" and fpc also uses this command for interrupts, but there it should be the "MRET" command instead. I'm not sure if this a special case for this microcontroller model. The command is mentioned in the risc-v specification in "Volume II: Privileged Architecture". It seems interrupts automatically change the mode from user to machine mode which has to do with memory/register protection, and MRET switches the mode back while returning. For testing I added the "MRET" in inline assembler at the end of the interrupt handler and it works. But then the stackframe and pushed registers have to be cleaned up manually in inline assembler as well, and that of course is not suitable in 'productive code'. Perhaps it could be added in procedures marked with the "interrupt;" procedure directive.
Forum thread: https://forum.lazarus.freepascal.org/index.php/topic,59466.0.html