Skip to content

LiteX: interrupts on the VexRiscv CPU, TIMER0 example - #5

Open
enjoy-digital wants to merge 2 commits into
micro-FPGA:mainfrom
enjoy-digital:litex-irq
Open

enjoy-digital wants to merge 2 commits into
micro-FPGA:mainfrom
enjoy-digital:litex-irq

Conversation

@enjoy-digital

Copy link
Copy Markdown
Contributor

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 MEXT and the interrupt return without mret work on the VexRiscv of LiteX as they are: VexRiscv has mtvec, mepc, mcause and mstatus as in the RISC-V standard. 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, set with inline ASM), and IrqOn / 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_PENDING register of each module:

.include "include/CPU/LITEX.bas"
.include "include/CPU/LITEX_VEXRISCV_IRQ.bas"

ON IRQ MEXT
  IF TIMER0.EV_PENDING.0 THEN
    TIMER0.EV_PENDING = 1        ' clear the event first
    ticks = ticks + 1
  END IF
END IRQ

TIMER0.EV_ENABLE = 1             ' the module raises its interrupt
IrqEnable IRQ_TIMER0             ' VexRiscv lets it through
IrqOn                            ' mie.MEIE and mstatus.MIE

examples/timer_litex.bas is the complete program, in the form of timer_sapphire.bas: the timer interrupts TICK_HZ times 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.md has a new section "Interrupts (VexRiscv)".

Tested

  • timer_litex in litex_sim (VexRiscv): 100 interrupts per second, exact. Run again after the rebase on the current main.
  • On the Trenz TEL0025 (VexRiscv at 75 MHz), on 16 November, 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 (300000 interrupts) while the main program prints. Not run again on the board since.
  • tests/run_tests.py and tests/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

enjoy-digital and others added 2 commits November 19, 2026 14:19
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>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant