Repository navigation
LiteX: Tiny BASIC interpreter on the LiteX console - #6
Open
enjoy-digital wants to merge 4 commits into
Open
enjoy-digital wants to merge 4 commits into
enjoy-digital wants to merge 4 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>
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>
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 #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.basrunslib/tinybasic.bason a LiteX SoC, astinybasic_sapphire.basdoes on the Sapphire SoC: it includes the LiteX CPU file and the console, and definesKeyHitfrom 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.mdhas a new section "Tiny BASIC".It needs #3: the LiteX BIOS leaves values in the registers before it starts the program, and without
pend = 0the first statement gives? Syntax error.Tested
In
litex_sim(VexRiscv,RV32I+M), loaded into the main RAM by the BIOS:tests/run_tests.pyandtests/check_litex.py(which compilestinybasic_litex.bas): all tests passed.Not tested on a board.
DELAYis 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