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:
1 parent
59d5e617f2
commit
dc7c7c22c5
14 files changed
+5205
-4141
No files matched your search
+182
-6
@@ -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();
|
||||
|
||||
Reference in new issue
Block a user