libriscv icon indicating copy to clipboard operation
libriscv copied to clipboard

Q: Interrupts?

Open ethindp opened this issue 10 months ago • 1 comments

Is it possible to interrupt execution to call another function, and then resume execution of the old function? I.e., in a game loop, I might want to do this. (I haven't read the game engine examples so there could be a different way of doing this.)

ethindp avatar Feb 27 '25 04:02 ethindp

Yes, I use it all the time in my game engine. There's a chapter on this in the docs: https://libriscv.no/docs/emulator/vmcalls#interrupting-a-running-machine

If you don't care much about dealing with the complexity in the example, you can use machine.preempt(...) in place of machine.vmcall(...).

But essentially: VM calls are function calls into the sandboxed program that clobber registers/state, while preempted calls will store and restore the register state.

If you're using the C API, then I don't think there's an exposed API function that helps with that. But you can store registers before and restore after manually doing a vmcall. Also note that you can't interrupt a machine from another thread. Part of the low latency of libriscv is the assumption that everything happens in the same thread. In my game engine I have one VM instance per thread for this reason.

fwsGonzo avatar Feb 27 '25 07:02 fwsGonzo