Show HN: AVR-VM – VM with JIT-compiler for ATMega32 written in Rust
github.com
github.com
This is awesome!
It is a difficult problem to get cycle correct interrupts, no doubt.
In Ruby you can redefine methods at any point, but in my implementation I only check if they have been redefined once per loop in the program. If another thread wants to redefine a method, it just waits a little while until all the other threads are ready.
This means we can basically ignore the problem of method redefinition - as we only allow it when we are ready and then we can handle it slow-time.
Making an example that shows the difference in servicing after every cycle vs. servicing interrupts only after a basic block is trivial. Configure a timer to interrupt in 10 cycles (or X) and then have a basic block writing 1, 2, 3, .. whatever to SRAM. Read the value in the interrupt routine.
I imagine this is no big deal in x86 or similar where nobody wants or can achieve cycle-perfect interrupts anyway, but it is a common design goal for microcontrollers which have perfectly predictable timing and a lot less MHz. One cycle for an 8 MHz AVR takes 125 ns so you can imagine how servicing interrupts after basic blocks of varying length can introduce jitter into your microcontroller generated signals.
But making the timing perfect was not a priority, since the programs, with which the VM was tested, also work well, when the timer interrupt only fires once in a while.