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>
This commit is contained in:
brad ArantandClaude Opus 5.5 committed 2026-11-27 21:09:43 -08:00
1 parent 59d5e617f2
commit dc7c7c22c5
14 files changed
+5205 -4141

No files matched your search

BIN
View File
Binary file not shown.
+182 -6
View File
@@ -8,6 +8,10 @@
* MID$, PEEK/POKE, INP/OUT and TIM. Commands: RUN LIST NEW (SCRATCH) and any
* statement typed without a line number runs at once. Ctrl-C stops a program.
*
* Graphics: 320x240 in 16 colours over the text, origin top left, colour 0
* transparent. CLS COLOR PLOT LINE RECT FILL CIRCLE and POINT(x,y); LOCATE and
* INK place and colour text.
*
* Builds with SDCC for the Z-80 (console on I/O ports 0/1) or with a host C
* compiler for testing (stdin/stdout).
*/
@@ -66,6 +70,16 @@ static void io_out(u8 p, u8 v)
__endasm;
}
static void bye(void) { }
__sfr __at 0x30 gx_lo;
__sfr __at 0x31 gx_hi;
__sfr __at 0x32 gy_p;
__sfr __at 0x33 gpix; /* write plots and moves right; read gives the colour */
__sfr __at 0x34 gfill;
static void g_at(u16 x, u8 y) { gx_lo = x; gx_hi = x >> 8; gy_p = y; }
static void g_put(u8 c) { gpix = c; }
static u8 g_get(void) { return gpix; }
static void g_clear(u8 c) { gfill = c; while (gfill & 1); }
#else
#include <stdio.h>
#include <stdlib.h>
@@ -87,6 +101,13 @@ static void poke(u16 a, u8 v) { fake_mem[a] = v; }
static u8 io_in(u8 p) { (void)p; return 0xFF; }
static void io_out(u8 p, u8 v) { (void)p; (void)v; }
static void bye(void) { exit(0); }
static u8 gmem[240][320];
static u16 hgx;
static u8 hgy;
static void g_at(u16 x, u8 y) { hgx = x; hgy = y; }
static void g_put(u8 c) { if (hgx < 320 && hgy < 240) gmem[hgy][hgx] = c; hgx++; }
static u8 g_get(void) { return hgx < 320 && hgy < 240 ? gmem[hgy][hgx] : 0; }
static void g_clear(u8 c) { memset(gmem, c, sizeof gmem); }
#endif
/* ----------------------------------------------------------------- tokens */
@@ -94,18 +115,20 @@ enum {
T_LET = 0x80, T_PRINT, T_INPUT, T_IF, T_THEN, T_GOTO, T_GOSUB, T_RETURN, T_FOR,
T_TO, T_STEP, T_NEXT, T_DIM, T_DEF, T_END, T_STOP, T_REM, T_READ, T_DATA,
T_RESTORE, T_ON, T_RANDOMIZE, T_POKE, T_OUT, T_RUN, T_LIST, T_NEW, T_SCRATCH,
T_BYE, T_AND, T_OR, T_NOT, T_SIN, T_COS, T_TAN, T_ATN, T_EXP, T_LOG, T_ABS,
T_BYE, T_CLS, T_COLOR, T_PLOT, T_LINE, T_RECT, T_FILL, T_CIRCLE, T_LOCATE, T_INK,
T_AND, T_OR, T_NOT, T_SIN, T_COS, T_TAN, T_ATN, T_EXP, T_LOG, T_ABS,
T_SQR, T_INT, T_RND, T_SGN, T_FN, T_TAB, T_PEEK, T_INP, T_LEN, T_VAL, T_ASC,
T_CHRS, T_STRS, T_LEFTS, T_RIGHTS, T_MIDS, T_TIM, T_LE, T_GE, T_NE, T_LAST
T_CHRS, T_STRS, T_LEFTS, T_RIGHTS, T_MIDS, T_TIM, T_POINT, T_LE, T_GE, T_NE, T_LAST
};
static const char *const kw[] = {
"LET", "PRINT", "INPUT", "IF", "THEN", "GOTO", "GOSUB", "RETURN", "FOR",
"TO", "STEP", "NEXT", "DIM", "DEF", "END", "STOP", "REM", "READ", "DATA",
"RESTORE", "ON", "RANDOMIZE", "POKE", "OUT", "RUN", "LIST", "NEW", "SCRATCH",
"BYE", "AND", "OR", "NOT", "SIN", "COS", "TAN", "ATN", "EXP", "LOG", "ABS",
"BYE", "CLS", "COLOR", "PLOT", "LINE", "RECT", "FILL", "CIRCLE", "LOCATE", "INK",
"AND", "OR", "NOT", "SIN", "COS", "TAN", "ATN", "EXP", "LOG", "ABS",
"SQR", "INT", "RND", "SGN", "FN", "TAB", "PEEK", "INP", "LEN", "VAL", "ASC",
"CHR$", "STR$", "LEFT$", "RIGHT$", "MID$", "TIM", "<=", ">=", "<>"
"CHR$", "STR$", "LEFT$", "RIGHT$", "MID$", "TIM", "POINT", "<=", ">=", "<>"
};
/* ----------------------------------------------------------------- memory */
@@ -310,7 +333,8 @@ static void tokenize(const char *s, u8 *out)
best = 0; blen = 0;
for (i = 0; i < T_LAST - T_LET; i++) {
k = kw[i];
for (len = 0; k[len]; len++) {
if ((u8)k[0] != c) continue;
for (len = 1; k[len]; len++) {
u8 t = s[len];
if (t >= 'a' && t <= 'z') t -= 32;
if (t != (u8)k[len]) break;
@@ -531,6 +555,15 @@ static float func(u8 t)
}
if (t == T_TIM) return (float)millis() / 1000.0f;
expect('(');
if (t == T_POINT) {
i16 x = to_int(expr()), y;
expect(',');
y = to_int(expr());
expect(')');
if ((u16)x >= 320 || (u16)y >= 240) return 0;
g_at(x, y);
return g_get();
}
if (t == T_LEN || t == T_VAL || t == T_ASC) {
u8 *p = (u8 *)s;
sexpr(s);
@@ -598,7 +631,7 @@ static float primary(void)
}
if (is_str_start()) error(E_TYPE);
if (c == T_FN) { tp++; return call_fn(); }
if (c >= T_SIN && c <= T_TIM && c != T_FN && c != T_TAB) { tp++; return func(c); }
if (c >= T_SIN && c <= T_POINT && c != T_FN && c != T_TAB) { tp++; return func(c); }
if (is_alpha(c)) return *num_ref();
error(E_SYNTAX);
return 0;
@@ -1116,6 +1149,144 @@ static void run_prog(void)
if (!arena[0]) { curln = 0; tp = (u8 *)""; }
}
/* ------------------------------------------------------------- graphics */
static u8 gcolor = 15;
/* Up to n comma-separated integer arguments; returns how many were given. */
static u8 int_args(i16 *a, u8 min, u8 n)
{
u8 i = 0;
do a[i++] = to_int(expr()); while (i < n && accept(','));
if (i < min) error(E_SYNTAX);
return i;
}
static u8 colour_arg(i16 *a, u8 given, u8 need)
{
if (given > need) {
if ((u16)a[need] > 15) error(E_FUNC);
return a[need];
}
return gcolor;
}
static void hline(i16 x1, i16 x2, i16 y, u8 c)
{
i16 t;
if (x1 > x2) { t = x1; x1 = x2; x2 = t; }
if ((u16)y >= 240 || x2 < 0 || x1 > 319) return;
if (x1 < 0) x1 = 0;
if (x2 > 319) x2 = 319;
g_at(x1, y);
for (; x1 <= x2; x1++) g_put(c);
}
static void pset(i16 x, i16 y, u8 c)
{
if ((u16)x < 320 && (u16)y < 240) { g_at(x, y); g_put(c); }
}
static void line(i16 x1, i16 y1, i16 x2, i16 y2, u8 c)
{
i16 dx = x2 > x1 ? x2 - x1 : x1 - x2, sx = x1 < x2 ? 1 : -1;
i16 dy = y2 > y1 ? y1 - y2 : y2 - y1, sy = y1 < y2 ? 1 : -1;
i16 err = dx + dy, e2;
if (y1 == y2) { hline(x1, x2, y1, c); return; }
for (;;) {
pset(x1, y1, c);
if (x1 == x2 && y1 == y2) return;
e2 = 2 * err;
if (e2 >= dy) { err += dy; x1 += sx; }
if (e2 <= dx) { err += dx; y1 += sy; }
}
}
static void circle(i16 cx, i16 cy, i16 r, u8 c)
{
i16 x = r, y = 0, err = 1 - r;
if (r < 0) error(E_FUNC);
while (x >= y) {
pset(cx + x, cy + y, c); pset(cx - x, cy + y, c);
pset(cx + x, cy - y, c); pset(cx - x, cy - y, c);
pset(cx + y, cy + x, c); pset(cx - y, cy + x, c);
pset(cx + y, cy - x, c); pset(cx - y, cy - x, c);
y++;
if (err < 0) err += 2 * y + 1;
else { x--; err += 2 * (y - x) + 1; }
}
}
static void esc_num(u8 n)
{
if (n >= 100) out_c('0' + n / 100);
if (n >= 10) out_c('0' + n / 10 % 10);
out_c('0' + n % 10);
}
static void graphics(u8 t)
{
i16 a[5];
u8 n, c;
switch (t) {
case T_CLS:
out_c(27); out_c('['); out_c('2'); out_c('J');
out_c(27); out_c('['); out_c('H');
col = 0;
g_clear(0);
break;
case T_COLOR:
int_args(a, 1, 1);
if ((u16)a[0] > 15) error(E_FUNC);
gcolor = a[0];
break;
case T_PLOT:
n = int_args(a, 2, 3);
pset(a[0], a[1], colour_arg(a, n, 2));
break;
case T_LINE:
n = int_args(a, 4, 5);
line(a[0], a[1], a[2], a[3], colour_arg(a, n, 4));
break;
case T_RECT:
n = int_args(a, 4, 5);
c = colour_arg(a, n, 4);
hline(a[0], a[2], a[1], c);
hline(a[0], a[2], a[3], c);
line(a[0], a[1], a[0], a[3], c);
line(a[2], a[1], a[2], a[3], c);
break;
case T_FILL: {
i16 y, y2;
n = int_args(a, 4, 5);
c = colour_arg(a, n, 4);
y = a[1] < a[3] ? a[1] : a[3];
y2 = a[1] < a[3] ? a[3] : a[1];
if (y < 0) y = 0;
if (y2 > 239) y2 = 239;
for (; y <= y2; y++) hline(a[0], a[2], y, c);
break;
}
case T_CIRCLE:
n = int_args(a, 3, 4);
circle(a[0], a[1], a[2], colour_arg(a, n, 3));
break;
case T_LOCATE: /* LOCATE row, column (from 1) */
int_args(a, 2, 2);
if (a[0] < 1 || a[0] > 30 || a[1] < 1 || a[1] > 80) error(E_FUNC);
out_c(27); out_c('['); esc_num(a[0]); out_c(';'); esc_num(a[1]); out_c('H');
col = a[1] - 1;
break;
case T_INK: /* INK foreground [, background] */
n = int_args(a, 1, 2);
if ((u16)a[0] > 15 || (n > 1 && (u16)a[1] > 15)) error(E_FUNC);
out_c(27); out_c('[');
esc_num(a[0] < 8 ? 30 + a[0] : 82 + a[0]);
if (n > 1) { out_c(';'); esc_num(a[1] < 8 ? 40 + a[1] : 92 + a[1]); }
out_c('m');
break;
}
}
static void statement(void);
static void st_if(void)
@@ -1195,6 +1366,10 @@ static void statement(void)
case T_NEW:
case T_SCRATCH: new_prog(); running = 0; curln = 0; tp = (u8 *)""; break;
case T_BYE: bye(); break;
case T_CLS: case T_COLOR: case T_PLOT: case T_LINE: case T_RECT:
case T_FILL: case T_CIRCLE: case T_LOCATE: case T_INK:
graphics(t);
break;
default:
tp--;
st_let();
@@ -1230,6 +1405,7 @@ int main(void)
u16 n;
u8 *p;
new_prog();
g_clear(0);
newline();
puts_("DARTMOUTH-STYLE BASIC Z-80 ON A BASYS 3");
newline();
+4123 -4123
View File
File diff suppressed because it is too large. Load diff
+69
View File
@@ -0,0 +1,69 @@
10 REM GRAPHICS DEMO FOR THE BASYS 3 Z-80
20 REM COLOUR BARS, CIRCLES, LINES, CURVES, SHAPES, ANIMATION, MANDELBROT
30 S=TIM
100 CLS
110 INK 15: LOCATE 3,30: PRINT "BASYS 3 Z-80 GRAPHICS"
120 INK 7: LOCATE 5,26: PRINT "320 X 240 PIXELS IN 16 COLOURS"
130 FOR C=1 TO 15
140 FILL C*20-10,100,C*20+7,180,C
150 INK C: LOCATE 25,C*5-1: PRINT C;
160 NEXT C
170 GOSUB 9000
200 CLS: INK 15: PRINT "CIRCLE"
210 C=0
220 FOR R=3 TO 117 STEP 3
230 GOSUB 8000: CIRCLE 160,120,R,C
240 NEXT R
250 GOSUB 9000
300 CLS: INK 15: PRINT "LINE"
310 C=0
320 FOR X=0 TO 319 STEP 8: GOSUB 8000: LINE 160,120,X,0,C: NEXT X
330 FOR Y=0 TO 239 STEP 8: GOSUB 8000: LINE 160,120,319,Y,C: NEXT Y
340 FOR X=319 TO 0 STEP -8: GOSUB 8000: LINE 160,120,X,239,C: NEXT X
350 FOR Y=239 TO 0 STEP -8: GOSUB 8000: LINE 160,120,0,Y,C: NEXT Y
360 GOSUB 9000
400 CLS: INK 10: PRINT "Y = SIN(X)": INK 12: PRINT "Y = COS(X)"
410 LINE 0,120,319,120,8: LINE 160,0,160,239,8
420 FOR X=0 TO 319
430 PLOT X,120-90*SIN((X-160)/25),10
440 PLOT X,120-90*COS((X-160)/25),12
450 NEXT X
460 GOSUB 9000
500 CLS: INK 15: PRINT "FILL AND RECT": RANDOMIZE
510 FOR I=1 TO 120
520 X=INT(RND*290): Y=16+INT(RND*200): C=1+INT(RND*15)
530 IF RND<.5 THEN FILL X,Y,X+5+INT(RND*30),Y+5+INT(RND*20),C: GOTO 550
540 RECT X,Y,X+5+INT(RND*30),Y+5+INT(RND*20),C
550 NEXT I
560 GOSUB 9000
600 CLS: INK 15: PRINT "ANIMATION"
610 RECT 0,16,319,239,7
620 X=40: Y=60: U=3: V=2
630 FOR I=1 TO 500
640 CIRCLE X,Y,6,0
650 X=X+U: Y=Y+V
660 IF X<8 OR X>311 THEN U=-U
670 IF Y<24 OR Y>231 THEN V=-V
680 CIRCLE X,Y,6,11: PLOT X,Y,14
690 T=TIM
700 IF TIM-T<.01 THEN 700
710 NEXT I
720 GOSUB 9000
800 CLS: INK 15: PRINT "MANDELBROT SET": M=TIM
810 FOR J=0 TO 59: FOR I=0 TO 79
820 A=I*.04-2.2: B=J*.04-1.2: X=0: Y=0
830 FOR K=1 TO 15
840 Z=X*X-Y*Y+A: Y=2*X*Y+B: X=Z
850 IF X*X+Y*Y>4 THEN 870
860 NEXT K
870 IF K>15 THEN K=0
880 FILL I*4,J*4,I*4+3,J*4+3,K
890 NEXT I: NEXT J
900 INK 7: LOCATE 30,1: PRINT "MANDELBROT TOOK";INT(TIM-M);"SECONDS.";
910 PRINT " WHOLE DEMO";INT(TIM-S);"SECONDS."
920 END
8000 C=C+1: IF C>15 THEN C=1
8010 RETURN
9000 T=TIM
9010 IF TIM-T<3 THEN 9010
9020 RETURN