Repository navigation
LiteX: interrupts on the VexRiscv CPU, TIMER0 example - #5
Open
enjoy-digital wants to merge 2 commits into
Open
enjoy-digital wants to merge 2 commits into
enjoy-digital wants to merge 2 commits into
Conversation
LiteX builds a SoC (CPU, memories, UART, timer, ...) for many FPGA boards and for a Verilator simulation. The register addresses of a LiteX SoC change with its options, so the CPU include is made by a script from the csr.json of the build: python tools/litex2bas.py build/<board>/csr.json -o examples/include/CPU/LITEX.bas The include declares TARGET (from the CPU), CLOCK, CODE, RAM, one DEVICE for each module of the SoC with all its registers, and the Get handler of the UART (the LiteX UART keeps a received character until the RX event is cleared). It is used like the other CPU files, with lib/uart.bas for the console. The program is loaded into the main RAM by the LiteX BIOS (litex_term), or takes the place of the BIOS in the ROM (--code rom). - tools/litex2bas.py: the script (Python 3, no dependencies) - examples/hello_litex.bas: PRINT, INPUT, LEDs - examples/delay_litex.bas: measures DELAY_MS with the LiteX timer - examples/include/boards/TEL0025.bas: LED names of the Trenz TEL0025 - docs/LiteX.md: how to simulate, build for a board and load a program - tests/check_litex.py: checks the script and compiles the examples The compiler is not changed. Tested: hello_litex in litex_sim (VexRiscv, RV32I+M), from the main RAM and from the ROM. On the Trenz TEL0025 (Certus-NX, VexRiscv at 75 MHz), from the main RAM and from the ROM, with the same program before the UART handler moved into the include. DELAY_MS 100 takes 150 ms there: VexRiscv needs 3 cycles per pass of the delay loop. Written by an AI agent (Claude Code), supervised by Florent Kermarrec. Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
ON IRQ MEXT and the interrupt return without mret work on the VexRiscv of LiteX as they are: the compiler is not changed. What a program needs is in two places: - tools/litex2bas.py writes the interrupt number of each module (CONST IRQ_TIMER0, IRQ_UART0, ...) and the clock (CONST LITEX_CLOCK) into the include. - examples/include/CPU/LITEX_VEXRISCV_IRQ.bas: IrqEnable / IrqDisable for one SoC interrupt (VexRiscv has its own enable bits in CSR 0xBC0), IrqOn / IrqOff (mie.MEIE, mstatus.MIE). All interrupts of a LiteX SoC arrive as the machine external interrupt: one handler, ON IRQ MEXT, that tests the EV_PENDING register of each module. examples/timer_litex.bas: TIMER0 interrupts TICK_HZ times per second, the handler counts, the main program prints the uptime (as timer_sapphire.bas). Tested: timer_litex in litex_sim, 100 interrupts per second, exact. On the Trenz TEL0025 (VexRiscv at 75 MHz) with the same program under its first name: 10 s of uptime in 10.00 s at 100 interrupts per second, and 10000 interrupts per second for 30 s while the main program prints. Not tested: UART interrupts, other LiteX CPUs. Written by an AI agent (Claude Code), supervised by Florent Kermarrec. Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
This was referenced Nov 19, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Builds on #4: this branch contains its commit, so only the last commit ("LiteX: interrupts on the VexRiscv CPU, TIMER0 example") is new here. Please merge #4 first.
What
ON IRQ MEXTand the interrupt return withoutmretwork on the VexRiscv of LiteX as they are: VexRiscv hasmtvec,mepc,mcauseandmstatusas in the RISC-V standard. The compiler is not changed. What a program needs is in two places:tools/litex2bas.pywrites the interrupt number of each module (CONST IRQ_TIMER0,IRQ_UART0, ...) and the clock (CONST LITEX_CLOCK) into the include.examples/include/CPU/LITEX_VEXRISCV_IRQ.bas:IrqEnable/IrqDisablefor one SoC interrupt (VexRiscv has its own enable bits in CSR 0xBC0, set with inlineASM), andIrqOn/IrqOff(mie.MEIE,mstatus.MIE).All interrupts of a LiteX SoC arrive as the machine external interrupt, so a program has one handler that tests the
EV_PENDINGregister of each module:examples/timer_litex.basis the complete program, in the form oftimer_sapphire.bas: the timer interruptsTICK_HZtimes per second, the handler counts, the main program prints the uptime. The Sapphire SoC uses the CLINT timer (ON IRQ MTIMER); the default VexRiscv of LiteX has no CLINT, its timer is a module of the SoC with an external interrupt.docs/LiteX.mdhas a new section "Interrupts (VexRiscv)".Tested
timer_litexinlitex_sim(VexRiscv): 100 interrupts per second, exact. Run again after the rebase on the current main.tests/run_tests.pyandtests/check_litex.py: all tests passed.Not tested: UART interrupts, and the other CPUs of LiteX. They enable their interrupts in another way (PicoRV32 has its own instructions; SERV in LiteX has no interrupts), so the include file is for VexRiscv only.
Written by an AI agent (Claude Code), supervised by Florent Kermarrec.
🤖 Generated with Claude Code