Timer & external interrupts
Getting timer and external interrupts fully working, through the CLINT, Sstc and the PLIC.
P2 · SystemVerilog · FPGA
A minimal RV64IMA RISC-V CPU, built from scratch, capable of booting Linux all the way into an interactive BusyBox shell. I built this to understand the full stack from instruction decode up through virtual memory and interrupt handling.
64-bit integer base ISA.
Multiply / divide, every signed/unsigned variant. The kernel uses them all.
Atomics. Single-core, but OpenSBI needs at least LR/SC.
Control and Status Registers.
Instruction-fetch fence, implemented as a NOP.
Supervisor mode, where the Linux kernel runs.
User mode, where userspace runs.
39-bit paged virtual memory.
Supervisor timer interrupts. Simple, and cut timer overhead significantly.
Decoder implemented, but 4-byte instructions on 2-byte boundaries need misaligned access.
Parameterized FP add/sub/mul/div/sqrt modules built, never wired in.
Not truly pipelined: each stage runs one after the other. That keeps complexity low once interrupts, virtual memory and variable-latency instructions enter the picture.
Main memory. The boot image is loaded at 0x1FF000.
Serial console at 115200 baud, interrupt-driven through the PLIC.
Platform-level interrupt controller for external interrupts.
Core-local interruptor: mtime / mtimecmp timer interrupts.
Bootloader + fw_payload.bin are sent over serial and written to RAM at 0x1FF000.
Prints "Hello World!", sets a0 to 0 and a1 to the embedded device tree, then jumps to OpenSBI.
Firmware built in payload mode, with the kernel bundled in.
Custom Buildroot kernel with an embedded initramfs.
A working interactive shell prompt.
Getting timer and external interrupts fully working, through the CLINT, Sstc and the PLIC.
Making sure every signed/unsigned edge case is covered, with tests aimed specifically at these instructions.
Perfect in Verilator, random crashes on the Gowin board. After ~10 days of debugging it turned out to be a known chip issue. Moving to an Alinx 7035b (Xilinx) fixed it.
The C extension and floating-point units were built but left out: misaligned fetch and the sheer number of FP instructions weren't worth it.
The full story (scoping, CSRs, interrupts, the device tree and the FPGA saga) is in the build log.