wfi
lyszt.net covers wfi and other RISC-V instructions in more detail.
Description
wfi halts the program until an interrupt arrives. The letters stand for wait for interrupt. A program that has nothing to do idles here rather than spinning through instructions in a loop. It takes no operands.
wfi
The processor stops at wfi and starts again when an interrupt is pending and its enable bit in mie is set. In this simulator, that occurs when a button is pressed and the button's interrupt is enabled. The simulator displays the waiting for interrupt state and executes no instructions during that time.
What happens next depends on mstatus. If the global interrupt enable bit is set, the processor takes the interrupt as soon as it wakes. The trap writes the address of the instruction after wfi to mepc, the handler runs, and mret returns to the instruction after wfi. If the global enable bit is clear, the processor wakes and proceeds to the instruction after wfi without running a handler.
A program that idles between button presses configures mtvec, mie and mstatus, then repeats wfi in a loop.
loop: wfi j loop
With no interrupt enabled, wfi waits forever, so enable the interrupt before reaching it. csrs turns the enable bits on, and mret returns from the handler.
Example
csrsi mstatus, 8 # allow interrupts wfi # idles here until the button is pressed
