Files
Basys3Terminal/docs/basic-manual.md
T
brad ArantandClaude Opus 5.5 dc7c7c22c5 BASIC graphics: 320x240 16-colour layer, manual and demo
- z80_system: dual-clock graphics RAM behind ports 30h-34h (X/Y, plot
  with auto-increment, read-back, hardware fill); text_display overlays it
  under the text, colour 0 transparent.
- BASIC: CLS COLOR PLOT LINE RECT FILL CIRCLE POINT, LOCATE and INK;
  faster keyword tokenizing.
- docs/basic-manual.md (+ PDF via docs/build-manual.sh).
- basic/examples/graphics-demo.bas and tools/send-bas.py to load programs
  over the serial port.
- Simulation checks drawing and saves a frame; the emulator models the
  graphics and timer ports and can stop when BASIC waits for input.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-27 21:09:43 -08:00

621 lines
18 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
# Basys 3 Z-80 BASIC
*A Dartmouth-style BASIC with colour graphics, for the Z-80 inside the Basys 3
VT220 terminal.*
## 1. Getting started
The Z-80 shares the board with the terminal. Two switches decide who the screen
and keyboard belong to:
| Switch | Down | Up |
|---|---|---|
| SW15 | Terminal for the PC's serial port | Console of the Z-80 |
| SW14 | — | Also pass text from the PC's serial port to the Z-80 (for loading programs) |
Flip **SW15 up**. The first time, BASIC greets you:
```
DARTMOUTH-STYLE BASIC Z-80 ON A BASYS 3
READY
```
The Z-80 keeps running while SW15 is down. It just waits until it can print
again, so you can flip back and forth without losing your program. **BTND**
restarts the Z-80; that wipes the program. **BTNC** restarts the whole board.
### Talking to BASIC
Anything typed without a line number runs at once:
```
PRINT 2+2
4
```
A line that starts with a number is stored as part of the program instead:
```
10 PRINT "HELLO"
20 GOTO 10
RUN
```
- **Ctrl-C** stops a running program.
- **Backspace** (or Delete) rubs out the last character.
- **Ctrl-U** erases the whole line.
- Cursor keys are ignored.
Lines can be up to 250 characters. Lower case is accepted and turned into upper
case, except inside quotes.
### Editing a program
- To replace a line, type it again with the same number.
- To delete a line, type its number alone.
- Lines are kept in number order whatever order you type them in. Number in
tens (10, 20, 30…) to leave room for later additions.
- Changing the program clears all variables.
| Command | Does |
|---|---|
| `RUN` | Clear variables and run from the first line |
| `RUN 100` | Clear variables and run from line 100 |
| `LIST` | List the whole program (Ctrl-C stops a long listing) |
| `LIST 100` | List line 100 |
| `LIST 100-200` | List lines 100 to 200 (`LIST -200` and `LIST 100-` work too) |
| `NEW` or `SCRATCH` | Erase the program and all variables |
There is no `SAVE` or `LOAD`: the board has no storage for programs. Keep them
as text files on the PC and send them in (section 10). `LIST` shows the program
on the screen, so you can copy it from a PC terminal.
## 2. Program lines
A program line is a number from 0 to 65535 followed by one or more statements
separated by colons:
```
10 FOR I=1 TO 3: PRINT I: NEXT I
```
Spaces don't matter outside quotes, as at Dartmouth. `FORI=1TO3` is the same as
`FOR I = 1 TO 3`. `LIST` shows lines as you typed them.
The program runs in line-number order until it reaches `END`, `STOP`, the last
line, or an error.
## 3. Numbers, variables and strings
### Numbers
All numbers are single-precision floating point: about 7 significant digits,
magnitudes from about 1E-38 to 3E+38. Constants may be written `12`, `-3.5`,
`.25`, `6.02E23` or `1E-6`.
`PRINT` shows numbers like this:
- At most 6 significant digits.
- A leading space instead of a plus sign, and a trailing space.
- Whole numbers below a million print without a point.
- Values from .001 to 999999 print as decimals. Anything else uses E
notation, such as `1.23457E+08` or `1E-05`.
### Numeric variables
A numeric variable is a letter, or a letter followed by one digit: `X`, `N`,
`A1`, `Z9`. That gives 286 of them. All start at zero when the program is run.
### Arrays
An array has a one-letter name and one or two subscripts, both counting from 0.
`DIM` declares its size:
```
10 DIM A(100), M(8,8)
```
An array used without `DIM` is given room for subscripts 0 to 10. An array and
a plain variable of the same letter are separate: `A` and `A(3)` don't interfere.
### Strings
String variables are `A$` to `Z$`. Each holds up to 79 characters and starts
empty. String constants are written in double quotes. `+` joins strings:
```
10 A$ = "BASYS"
20 B$ = A$ + " 3"
30 PRINT B$
```
## 4. Expressions
From highest precedence to lowest:
| Operators | Meaning |
|---|---|
| `( )` | Grouping |
| `^` | Power: `2^10` is 1024 |
| `-` `+` (unary) | Negation: `-2^2` is -4 |
| `*` `/` | Multiply, divide |
| `+` `-` | Add, subtract |
| `=` `<>` `<` `>` `<=` `>=` | Comparisons (also work on strings) |
| `NOT` | Bitwise not |
| `AND` | Bitwise and |
| `OR` | Bitwise or |
A comparison gives -1 when true and 0 when false. `AND`, `OR` and `NOT` work
on whole numbers from -32768 to 32767, so they combine comparisons the expected
way:
```
IF X > 0 AND X < 10 THEN PRINT "SINGLE DIGIT"
```
Strings compare in character order: `"APPLE" < "BANANA"` is true.
## 5. Statements
In the descriptions below, square brackets mark something you may leave out.
### LET — assignment
```
LET X = 5
X = 5
A$ = "TEXT"
M(I,J) = 0
```
The word `LET` is optional.
### PRINT — output
```
PRINT "X IS"; X
PRINT A, B, C
PRINT "NO NEW LINE";
PRINT TAB(20); "INDENTED"
PRINT
```
- A **comma** moves to the next 15-column zone.
- A **semicolon** prints the next item straight after.
- A **comma or semicolon at the end** keeps the cursor on the same line.
- **`TAB(n)`** moves to column *n*, counting from 0.
- `PRINT` on its own prints an empty line.
### INPUT — read from the keyboard
```
INPUT X
INPUT A, B, C
INPUT "YOUR NAME"; N$
```
`INPUT` prints `? ` (after the prompt, if there is one) and waits for a line.
Separate several values with commas. If you give too few, it asks for the rest
with `?? `. A string can be typed bare or in quotes; quotes are needed if it
contains a comma. Typing something that isn't a number for a numeric variable
gives `?REDO` and asks again. Ctrl-C stops the program.
### IF … THEN — decisions
```
IF X > 100 THEN 500
IF X > 100 GOTO 500
IF A$ = "YES" THEN PRINT "OK": GOTO 20
```
When the condition is true, `THEN` is followed by a line number to jump to, or
by statements to run. When it is false, the rest of the line is skipped, so
statements after the colon run only when the condition is true. Any non-zero
value counts as true.
### GOTO, GOSUB, RETURN, ON
```
GOTO 100
GOSUB 1000
RETURN
ON N GOTO 100, 200, 300
ON N GOSUB 100, 200, 300
```
- `GOSUB` jumps to a subroutine; `RETURN` comes back to the statement after the
`GOSUB`. Subroutines can nest 32 deep.
- `ON` picks the first line when *N* is 1, the second when it is 2, and so on.
If *N* is out of range, the program carries on with the next statement.
### FOR … NEXT — loops
```
FOR I = 1 TO 10
FOR X = 0 TO 1 STEP .1
FOR C = 10 TO 1 STEP -1
NEXT I
NEXT
NEXT J, I
```
- The loop runs while the variable has not passed the limit. `STEP` defaults
to 1 and may be negative or fractional.
- If the starting value is already past the limit, the loop body is skipped
altogether.
- `NEXT` without a name closes the innermost loop.
- Loops nest 16 deep.
- Jumping out of a loop with `GOTO` is allowed. Starting a new `FOR` with the
same variable discards the old loop.
### DIM — arrays
```
DIM A(50), B(10,20)
```
See section 3. An array can hold at most 6000 elements.
### DEF FN — your own functions
```
10 DEF FNA(X) = X*X + 1
20 DEF FNR(D) = D * 3.14159 / 180
30 PRINT FNA(3), SIN(FNR(30))
```
A function has a one-letter name and one numeric argument. Other variables used
in its formula are the program's ordinary variables.
### READ, DATA, RESTORE
```
10 READ N, A$
20 PRINT A$; N
30 DATA 42, "ANSWER"
40 RESTORE
```
`READ` takes values from `DATA` statements in line order, wherever they are in
the program. A `DATA` statement takes up the rest of its line. `RESTORE` starts
again from the first item.
### END, STOP, REM
- `END` stops the program quietly.
- `STOP` stops it with the message `STOP IN line`.
- `REM` starts a comment that runs to the end of the line.
### RANDOMIZE
`RANDOMIZE` mixes the timer into the random number generator, so each run gives
different numbers. Without it, `RND` produces the same sequence every time the
Z-80 is reset.
### POKE and OUT
```
POKE address, value
OUT port, value
```
See section 9.
### Graphics and text placement
`CLS`, `COLOR`, `PLOT`, `LINE`, `RECT`, `FILL`, `CIRCLE`, `LOCATE` and `INK`
are described in sections 7 and 8.
## 6. Functions
### Arithmetic
| Function | Result |
|---|---|
| `ABS(X)` | Absolute value |
| `SGN(X)` | -1, 0 or 1 |
| `INT(X)` | Largest whole number not above *X*: `INT(-2.5)` is -3 |
| `SQR(X)` | Square root |
| `EXP(X)` | e to the power *X* |
| `LOG(X)` | Natural logarithm |
| `SIN(X)`, `COS(X)`, `TAN(X)` | Trigonometry, in radians |
| `ATN(X)` | Arctangent, in radians; `4*ATN(1)` is pi |
| `RND` or `RND(X)` | Random number from 0 up to (not including) 1; *X* is ignored |
| `FNx(X)` | A function made with `DEF` |
A whole number from 1 to 6: `1 + INT(RND * 6)`.
### Strings
| Function | Result |
|---|---|
| `LEN(A$)` | Number of characters |
| `VAL(A$)` | The number written in the string (0 if none) |
| `ASC(A$)` | Code of the first character |
| `CHR$(N)` | The character with code *N* |
| `STR$(X)` | The number as text, with a leading space if positive |
| `LEFT$(A$, N)` | First *N* characters |
| `RIGHT$(A$, N)` | Last *N* characters |
| `MID$(A$, S [, N])` | *N* characters (or the rest) starting at position *S* (from 1) |
### Machine and screen
| Function | Result |
|---|---|
| `TIM` | Seconds since the Z-80 was reset, to the millisecond |
| `POINT(X, Y)` | Colour of the graphics pixel at *X, Y* (0 off screen) |
| `PEEK(A)` | Byte at memory address *A* |
| `INP(P)` | Byte from I/O port *P* |
`TIM` is handy for delays and for timing:
```
10 T = TIM
20 IF TIM - T < 2 THEN 20
```
## 7. Graphics
The screen has a graphics layer **320 pixels wide and 240 high in 16 colours**.
It is mixed with the 80×30 text:
- **Coordinates:** *X* runs from 0 at the left to 319, and *Y* from 0 at the
top to 239. Each graphics pixel is a 2×2 block on the monitor.
- **Mixing with text:** text characters are drawn on top of the graphics. The
graphics show through wherever the text screen is blank.
- **Colour 0** is "no graphics": it shows the text background, normally black.
Drawing in colour 0 erases.
- **Clipping:** anything that falls off the edge is simply not drawn, so a
circle may hang off the screen.
- **Text cells:** a text character covers 4×8 graphics pixels. The character at
row *r*, column *c* (as used by `LOCATE`) starts at
*X* = 4(*c*−1), *Y* = 8(*r*−1).
### Colours
| Number | Colour | Number | Colour |
|---|---|---|---|
| 0 | None / black | 8 | Dark grey |
| 1 | Red | 9 | Bright red |
| 2 | Green | 10 | Bright green |
| 3 | Brown | 11 | Yellow |
| 4 | Blue | 12 | Bright blue |
| 5 | Magenta | 13 | Bright magenta |
| 6 | Cyan | 14 | Bright cyan |
| 7 | Light grey | 15 | White |
### Graphics statements
In every drawing statement the colour is optional. Without it, the colour set
by `COLOR` is used, initially 15 (white).
| Statement | Draws |
|---|---|
| `CLS` | Clears the text screen and the graphics, cursor to the top left |
| `COLOR C` | Sets the drawing colour for later statements |
| `PLOT X, Y [, C]` | One pixel |
| `LINE X1, Y1, X2, Y2 [, C]` | A straight line between two points |
| `RECT X1, Y1, X2, Y2 [, C]` | The outline of a rectangle with those corners |
| `FILL X1, Y1, X2, Y2 [, C]` | A solid rectangle |
| `CIRCLE X, Y, R [, C]` | A circle centred on *X, Y* with radius *R* |
`POINT(X, Y)` reads a pixel back (section 6).
```
10 CLS
20 FILL 0, 200, 319, 239, 2
30 CIRCLE 260, 40, 25, 11
40 FOR I = 0 TO 6
50 LINE 60 + I*30, 200, 75 + I*30, 140, 3
60 NEXT I
70 RECT 20, 120, 300, 230, 7
```
### Animation
Erase something by drawing it again in colour 0, then draw it in its new
place. Use `TIM` to set the pace:
```
10 CLS: X = 0
20 CIRCLE X, 120, 10, 0
30 X = X + 2: IF X > 319 THEN X = 0
40 CIRCLE X, 120, 10, 14
50 T = TIM
60 IF TIM - T < .02 THEN 60
70 GOTO 20
```
### Speed
`CLS` clears the graphics in under a millisecond, and a line right across the
screen takes about 2 milliseconds. `FILL` is the fastest way to cover a large
area. Most of the time in a graphics program goes on BASIC itself working out
the numbers (see section 9).
## 8. The text screen
The text screen is 80 columns by 30 rows. Two statements place and colour text:
| Statement | Does |
|---|---|
| `LOCATE R, C` | Moves the cursor to row *R* (1–30), column *C* (1–80) |
| `INK F [, B]` | Sets the text colour *F* and, optionally, background *B*, using the colour numbers above |
```
10 CLS
20 INK 14: LOCATE 15, 35: PRINT "CENTRED"
30 INK 0, 11: LOCATE 16, 35: PRINT " BLACK ON YELLOW "
```
The terminal is a full VT220 with ANSI colour, so anything it understands can be
printed with `CHR$(27)`. For example, `PRINT CHR$(27);"[0m"` returns to normal
colours, `PRINT CHR$(27);"[4m"` turns on underlining, and
`PRINT CHR$(27);"[2K"` erases the current line.
When the cursor passes the bottom row the text scrolls. The graphics layer
stays where it is.
## 9. The machine
### Memory
| Addresses | Contents |
|---|---|
| 0000–7FFF | BASIC itself (writes here are ignored) |
| 8000–EBFF | BASIC's variables, your program and arrays |
| EC00–FFFF | The Z-80 stack |
`PEEK` can read anything; `POKE` can change anything from 8000 up, including
BASIC's own data. Poke with care.
### I/O ports
| Port | `INP` | `OUT` |
|---|---|---|
| 0 | bit 0: a key is waiting; bit 1: the screen can take a character | — |
| 1 | the waiting key | a character to the screen |
| 16–19 | the millisecond counter, low byte first (reading 16 freezes the other three) | — |
| 32 | — | right two digits of the 7-segment display (hex) |
| 33 | — | left two digits of the 7-segment display |
| 48, 49, 50 | — | graphics X (low byte, high bit), Y |
| 51 | colour at X, Y | plot a pixel at X, Y and move right |
| 52 | bit 0: screen fill in progress | fill the whole graphics layer with a colour |
```
10 FOR I = 0 TO 255
20 OUT 32, I: OUT 33, 255 - I
30 T = TIM
40 IF TIM - T < .05 THEN 40
50 NEXT I
```
A key check that doesn't wait (the key is used up):
```
100 IF INP(0) AND 1 THEN K = INP(1): PRINT "YOU PRESSED "; CHR$(K)
```
### Speed
The Z-80 runs at 90 MHz and needs about 2.7 clocks per instruction, roughly 50
times the speed of a classic 4 MHz Z-80. BASIC does its arithmetic in software,
so in round numbers:
| Operation | Time |
|---|---|
| One pass of an empty `FOR … NEXT` loop | 30 µs (about 30,000 a second) |
| `X = I * 1.5` | 0.12 ms |
| `Y = SIN(X)` | 0.4 ms |
| `LINE` right across the screen | 2 ms |
Constants in a program are converted from text every time they are used, so a
loop runs a little faster if a constant used inside it is put into a variable
first.
## 10. Loading programs from a PC
Keep programs as plain text files with one numbered line per line. To type one
into the board:
1. Flip **SW15** and **SW14** up.
2. Make sure nothing else is using the serial port. If a login getty is running
on it, stop it:
```
sudo systemctl stop serial-getty@ttyUSB1
```
3. Send the file at the baud rate set by SW1:0 (SW0 up is 9600):
```
tools/send-bas.py basic/examples/graphics-demo.bas --baud 9600 --run
```
This sends `NEW`, then the program, then `RUN`. Leave out `--run` to only
load it, and add `--keep` to merge it into the program already there.
4. Flip SW14 back down when you're done. Restart the getty with
`sudo systemctl start serial-getty@ttyUSB1` if you want the PC login back.
Pasting into a terminal program such as `picocom` works too.
## 11. Error messages
An error stops the program and reports the line, for example
`?DIVISION BY ZERO IN 120`.
| Message | Cause |
|---|---|
| `SYNTAX ERROR` | A statement BASIC can't make sense of |
| `UNDEFINED LINE NUMBER` | `GOTO`, `GOSUB`, `THEN` or `RUN` to a line that doesn't exist |
| `RETURN WITHOUT GOSUB` | `RETURN` with no `GOSUB` to go back to |
| `NEXT WITHOUT FOR` | `NEXT` with no loop open (or the wrong variable) |
| `OUT OF DATA` | `READ` after the last `DATA` item |
| `SUBSCRIPT OUT OF RANGE` | An array subscript below 0 or beyond its `DIM` |
| `DIVISION BY ZERO` | Dividing by 0 |
| `OUT OF MEMORY` | The program and arrays don't fit |
| `TYPE MISMATCH` | A string where a number belongs, or the other way round |
| `STRING TOO LONG` | A string over 79 characters, or a program line too long to store |
| `ILLEGAL FUNCTION CALL` | A bad argument: `SQR(-1)`, `LOG(0)`, a colour over 15, `AND` on a number beyond ±32767 |
| `UNDEFINED FUNCTION` | `FNx` used before its `DEF` |
| `FOR NESTED TOO DEEPLY` | More than 16 loops open |
| `GOSUB NESTED TOO DEEPLY` | More than 32 subroutines in progress |
| `BREAK IN n` | You pressed Ctrl-C |
| `STOP IN n` | The program ran `STOP` |
## 12. Example programs
### Times table
```
10 FOR I = 1 TO 9
20 FOR J = 1 TO 9
30 PRINT TAB(J*5); I*J;
40 NEXT J
50 PRINT
60 NEXT I
```
### Guess the number
```
10 RANDOMIZE
20 N = 1 + INT(RND * 100): G = 0
30 PRINT "I'M THINKING OF A NUMBER FROM 1 TO 100."
40 INPUT "YOUR GUESS"; X
50 G = G + 1
60 IF X < N THEN PRINT "HIGHER": GOTO 40
70 IF X > N THEN PRINT "LOWER": GOTO 40
80 PRINT "RIGHT IN"; G; "GUESSES!"
```
### Spirograph
```
10 CLS: C = 1
20 FOR A = 0 TO 6.283 STEP .0628
30 X = 160 + 100*COS(A) + 15*COS(A*7)
40 Y = 120 + 100*SIN(A) - 15*SIN(A*7)
50 IF A > 0 THEN LINE U, V, X, Y, C
60 U = X: V = Y: C = C + 1: IF C > 15 THEN C = 1
70 NEXT A
```
### The graphics demo
`basic/examples/graphics-demo.bas` shows everything in turn. It draws colour
bars, circles, a burst of lines, sine and cosine curves, random rectangles, a
bouncing ball and a Mandelbrot set, then prints how long it all took.
## 13. Limits at a glance
| Item | Limit |
|---|---|
| Program and arrays together | about 22 KB |
| Line numbers | 0 – 65535 |
| Line length | 250 characters typed |
| Numeric variables | A–Z and A0–Z9 |
| String variables | A$–Z$, 79 characters each |
| Arrays | A–Z, 1 or 2 subscripts, up to 6000 elements each |
| User functions | FNA–FNZ, one argument each |
| FOR nesting / GOSUB nesting | 16 / 32 |
| Graphics | 320 × 240, 16 colours |
| Text | 80 × 30 |