BASIC compiler (tools/basc.py) and a runtime jump table in BASIC's ROM
tools/basc.py compiles a BASIC program to C and through GCC to an Intel HEX file loaded at A200h like the games, with a two-line BLOAD/CALL loader. Arithmetic, variables, arrays, all control flow and graphics arguments are compiled; PRINT, INPUT, strings, files, sound, DATA and the remaining functions go through BASIC's ROM via a fixed jump table at 9F00h (RT_ABI 1), so they behave identically. basic/test-compile.py runs 31 programs both ways and checks identical screens and graphics; typical loops run 7-26x faster. New error PROGRAM NEEDS ANOTHER BASIC. Fixes a bug present since the first version: variable numbers past 255 wrapped, so X1-Z9 shared storage with A-B9 (A7 and Y were the same variable). test-blocks.py checks it. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
fa4752fd7a
commit
2e668c708f
11 files changed
+5703
-3671
No files matched your search
@@ -1392,6 +1392,77 @@ file `PROG`, and goes back to BASIC. `BLOAD "PROG"` then loads it where it was
|
||||
compiled for. It must be at 0A000h or above. That's how The Iron Keep
|
||||
(section 14) gets onto the board.
|
||||
|
||||
### Compiling a program
|
||||
|
||||
`tools/basc.py` compiles a BASIC program on the PC into machine code. The
|
||||
compiled program does exactly what the program does when BASIC runs it, the
|
||||
same results to the last digit and the same error messages with the same line
|
||||
numbers, only faster:
|
||||
|
||||
| Each time round a loop | Interpreted | Compiled | |
|
||||
|---|---|---|---|
|
||||
| `FOR I = 1 TO N: NEXT I` | 6.4 µs | 0.98 µs | 7 times |
|
||||
| `X = I * 1.5` | 31 µs | 1.2 µs | 26 times |
|
||||
| `Y = SIN(I)` | 41 µs | 5.8 µs | 7 times |
|
||||
| `J = I AND 7: A(J) = A(J) + I` | 91 µs | 6.1 µs | 15 times |
|
||||
| `IF I > 0 THEN GOSUB 30` … `RETURN` | 39 µs | 2.1 µs | 18 times |
|
||||
| `PLOT I / 5, 100, 7` | 57 µs | 5.8 µs | 10 times |
|
||||
| `PRINT I;` | 36 µs | 29 µs | about the same |
|
||||
|
||||
Arithmetic, numeric variables and arrays, and every control statement (`GOTO`,
|
||||
`GOSUB`, `ON`, `FOR`, `IF`, `DO`, `SELECT CASE`, `EXIT`, `END`) become machine
|
||||
code, using the FPU directly, and the graphics statements work out their
|
||||
numbers there too. Everything else (`PRINT`, `INPUT`, strings, files, sound,
|
||||
`DATA` and `READ`, `DEF FN`, and functions such as `LEN` and `MOD`) is handed
|
||||
to BASIC in the ROM as it stands, so it behaves exactly as it does
|
||||
interpreted, at about the same speed. A program that spends its time
|
||||
calculating, looping and drawing gains most; one that mostly prints gains
|
||||
little. The graphics demo (`basic/examples/graphics-demo.bas`) runs 4.7 times
|
||||
as fast.
|
||||
|
||||
```
|
||||
tools/basc.py spiral.bas
|
||||
```
|
||||
|
||||
writes `SPIRAL.ihx`, the program, and `SPIRAL.bas`, a two-line loader, next
|
||||
to `spiral.bas` (`-o` puts them elsewhere, `--name` names them). It tells you
|
||||
how much room the program takes. Then send both, and save the loader:
|
||||
|
||||
```
|
||||
tools/send-hex.py SPIRAL.ihx --save SPIRALC --baud 3000000
|
||||
```
|
||||
|
||||
```
|
||||
tools/send-bas.py SPIRAL.bas --baud 3000000
|
||||
```
|
||||
|
||||
and `SAVE "SPIRAL"`. From then on `RUN "SPIRAL"` loads the machine code
|
||||
(the memory file `SPIRALC`) and runs it. It works like the games: it loads at
|
||||
0xA200, in the program area, after the loader.
|
||||
|
||||
- **Variables** are BASIC's own: the program starts with them cleared, as
|
||||
`RUN` does, and they are still there when it ends, for `PRINT`.
|
||||
- **Errors** stop the program with BASIC's message and line number, as they
|
||||
would interpreted. **Ctrl-C** is looked for at every 64th jump (`NEXT`,
|
||||
`LOOP`, `GOTO`, `GOSUB`, `RETURN` or `ON`), so it stops any loop.
|
||||
- **Room:** the program and its variables must fit between 0xA200 and
|
||||
0xF0B7, about 20 KB, less what its arrays need. The graphics demo
|
||||
compiles to 12 KB.
|
||||
- **Not allowed:** `RUN`, `LIST`, `NEW`, `LOAD`, `SAVE` and `RENUM` make no
|
||||
sense inside a compiled program, and `basc` says so.
|
||||
- **Loops:** each `LOOP` goes back to the `DO` before it that it belongs to,
|
||||
by position, where the interpreter keeps a list of `DO`s it has run; in a
|
||||
program that jumps out of a `DO` loop with `GOTO` and back into another the
|
||||
two can differ.
|
||||
- **Which BASIC:** a compiled program calls BASIC through a table at 0x9F00
|
||||
that later versions keep. If BASIC is too old or too new for it, the
|
||||
program stops with `PROGRAM NEEDS ANOTHER BASIC`; compile it again.
|
||||
|
||||
`basc` needs Python 3 and a RISC-V GCC (the one that comes with Vivado will
|
||||
do; the README says where).
|
||||
`--fast` compiles for speed rather than size, though it makes little
|
||||
difference. `--keep-c` keeps the C it makes on the way, for the curious.
|
||||
|
||||
When you're done, flip SW14 back down, and bring the getty back with
|
||||
`sudo systemctl unmask --runtime serial-getty@ttyUSB1` (a reboot does it too).
|
||||
Don't do that with SW14 up.
|
||||
@@ -1422,6 +1493,7 @@ An error stops the program and reports the line, for example
|
||||
| `LOOP WITHOUT DO` | `LOOP` or `EXIT DO` with no `DO` loop open |
|
||||
| `IF WITHOUT END IF` | A block `IF` (or its `ELSE`) with no `END IF` after it |
|
||||
| `SELECT WITHOUT END SELECT` | A `SELECT CASE` (or its `CASE`) with no `END SELECT` after it |
|
||||
| `PROGRAM NEEDS ANOTHER BASIC` | A compiled program (section 12) made for a different version of BASIC |
|
||||
| `GOSUB NESTED TOO DEEPLY` | More than 32 subroutines in progress |
|
||||
| `FILE NOT FOUND` | `LOAD`, `RUN` or `DEL` of a name not in `DIR` |
|
||||
| `BAD FILE NAME` | A name that is empty, longer than 8 or not just letters and digits |
|
||||
|
||||
Binary file not shown.
Reference in new issue
Block a user