Skip to content

LiteX: Tiny BASIC interpreter on the LiteX console - #6

Open
enjoy-digital wants to merge 4 commits into
micro-FPGA:mainfrom
enjoy-digital:litex-tinybasic
Open

enjoy-digital wants to merge 4 commits into
micro-FPGA:mainfrom
enjoy-digital:litex-tinybasic

Conversation

@enjoy-digital

Copy link
Copy Markdown
Contributor

Builds on #3, #4 and #5: this branch contains their commits, so only the last commit ("LiteX: Tiny BASIC interpreter on the LiteX console") is new here. Please merge them first.

What

examples/tinybasic_litex.bas runs lib/tinybasic.bas on a LiteX SoC, as tinybasic_sapphire.bas does on the Sapphire SoC: it includes the LiteX CPU file and the console, and defines KeyHit from the RX status of the LiteX UART.

About 11 KB of code and 5 KB of RAM (4 KB of it for the BASIC program), so the default 8 KB SRAM of a LiteX SoC is enough. docs/LiteX.md has a new section "Tiny BASIC".

It needs #3: the LiteX BIOS leaves values in the registers before it starts the program, and without pend = 0 the first statement gives ? Syntax error.

Tested

In litex_sim (VexRiscv, RV32I+M), loaded into the main RAM by the BIOS:

RiscBasic Tiny BASIC, 4020 bytes free
> PRINT 2+3*4
14
OK
> 10 FOR I=1 TO 5
> 20 PRINT I;" squared is ";I*I
> 30 NEXT I
> LIST
10 FOR I=1 TO 5
20 PRINT I;" squared is ";I*I
30 NEXT I
OK
> RUN
1 squared is 1
2 squared is 4
3 squared is 9
4 squared is 16
5 squared is 25
OK
> PRINT FREE
3964
OK

tests/run_tests.py and tests/check_litex.py (which compiles tinybasic_litex.bas): all tests passed.

Not tested on a board. DELAY is 1.5 times too long on VexRiscv, as noted in #4.

Written by an AI agent (Claude Code), supervised by Florent Kermarrec.

🤖 Generated with Claude Code

enjoy-digital and others added 4 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>
pend (the end of the program text) is a register variable in S4. It was set only by
NEW, so the interpreter worked only when the CPU starts with S4 = 0. After a boot
loader that leaves other values in the registers (found with the LiteX BIOS on
VexRiscv) the first statement gives "Syntax error" or writes outside the program
memory. The RAM variables are cleared by the startup code, the registers are not.

Tested in rbsim with all registers set to 0xDEADBEEF before the start: without the
change the first PRINT fails, with it PRINT, a stored program and LIST work.

Written by an AI agent (Claude Code), supervised by Florent Kermarrec.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
examples/tinybasic_litex.bas runs lib/tinybasic.bas on a LiteX SoC, as
tinybasic_sapphire.bas does on the Sapphire SoC: it includes the LiteX CPU file and
the console, and defines KeyHit from the RX status of the LiteX UART. About 11 KB of
code and 5 KB of RAM, so the default 8 KB SRAM of a LiteX SoC is enough.

Needs "tinybasic: clear pend at start": the LiteX BIOS leaves values in the
registers before it starts the program.

Tested in litex_sim (VexRiscv, RV32I+M): immediate PRINT, a stored FOR / NEXT
program, LIST, RUN, FREE. Not tested on a board.

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