Flash file system for BASIC, with bitstream write protection
BASIC gets SAVE, LOAD, RUN "NAME", DIR and DEL, storing program listings in the configuration flash from 240000h (448 x 4 KB blocks, 64 files, 32 KB each). The directory is kept in two alternating blocks with a sequence number and checksum, new data is written before the directory switches to it, and every page is verified after programming. rtl/flash_spi.v drives the flash through STARTUPE2 on ports 40h/41h. It only passes read, status, WREN/WRDI, RDID, page program and 4 KB erase, and abandons any program or erase below 240000h before the address reaches the chip. Tested on the board: an erase at 0 and a write at 200000h were refused and left the flash unchanged. With SW14 up, the Z-80's output is also copied to the PC's serial port, and tools/get-bas.py uses that to LIST a program into a file or run commands and print the replies. Also fixes port 10h returning the previous millisecond latch (the first read after reset was undefined). Simulation adds a model flash and a save/load/run/protection test. Manual and README updated. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
dc7c7c22c5
commit
29a15bf7c7
15 files changed
+6907
-6056
No files matched your search
Binary file not shown.
+383
-11
@@ -12,6 +12,9 @@
|
||||
* transparent. CLS COLOR PLOT LINE RECT FILL CIRCLE and POINT(x,y); LOCATE and
|
||||
* INK place and colour text.
|
||||
*
|
||||
* Files: SAVE "NAME", LOAD "NAME", RUN "NAME", DIR, DEL "NAME" keep programs
|
||||
* in the spare part of the configuration flash (see the file system below).
|
||||
*
|
||||
* Builds with SDCC for the Z-80 (console on I/O ports 0/1) or with a host C
|
||||
* compiler for testing (stdin/stdout).
|
||||
*/
|
||||
@@ -80,6 +83,44 @@ 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); }
|
||||
|
||||
/* SPI flash: port 40h opens/ends a command (bit 1 reads back "refused"),
|
||||
41h shifts a byte. The hardware refuses to write below the file area. */
|
||||
__sfr __at 0x40 fl_ctl;
|
||||
__sfr __at 0x41 fl_dat;
|
||||
static u8 spi(u8 b) { fl_dat = b; while (fl_ctl & 1); return fl_dat; }
|
||||
static void fl_open(u8 cmd) { fl_ctl = 1; spi(cmd); }
|
||||
static void fl_close(void) { fl_ctl = 0; }
|
||||
static void fl_addr(u32 a) { spi(a >> 16); spi(a >> 8); spi(a); }
|
||||
static u8 fl_busy(void)
|
||||
{
|
||||
u8 st;
|
||||
fl_open(0x05); st = spi(0); fl_close();
|
||||
return st & 1;
|
||||
}
|
||||
static u8 fl_present(void) /* Macronix MX25L3273F, 4 KB sectors */
|
||||
{
|
||||
u8 a, b, c;
|
||||
fl_open(0x9F); a = spi(0); b = spi(0); c = spi(0); fl_close();
|
||||
return a == 0xC2 && b == 0x20 && c == 0x16;
|
||||
}
|
||||
static void fl_read(u32 a, u8 *p, u16 n)
|
||||
{
|
||||
fl_open(0x03); fl_addr(a);
|
||||
while (n--) *p++ = spi(0);
|
||||
fl_close();
|
||||
}
|
||||
/* page program (02) or sector erase (20); returns 0 if the hardware refused */
|
||||
static u8 fl_write_op(u8 cmd, u32 a, const u8 *p, u16 n)
|
||||
{
|
||||
u8 ok;
|
||||
fl_open(0x06); fl_close();
|
||||
fl_open(cmd); fl_addr(a);
|
||||
while (n--) spi(*p++);
|
||||
ok = !(fl_ctl & 2);
|
||||
fl_close();
|
||||
return ok;
|
||||
}
|
||||
#else
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
@@ -108,6 +149,38 @@ 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); }
|
||||
|
||||
/* flash file area kept in basic-flash.img */
|
||||
#define HOST_BASE 0x240000UL
|
||||
static u8 hflash[0x1C0000];
|
||||
static u8 hflash_loaded;
|
||||
static void hflash_load(void)
|
||||
{
|
||||
FILE *f;
|
||||
if (hflash_loaded) return;
|
||||
hflash_loaded = 1;
|
||||
memset(hflash, 0xFF, sizeof hflash);
|
||||
if ((f = fopen("basic-flash.img", "rb"))) { fread(hflash, 1, sizeof hflash, f); fclose(f); }
|
||||
}
|
||||
static void hflash_store(void)
|
||||
{
|
||||
FILE *f = fopen("basic-flash.img", "wb");
|
||||
if (f) { fwrite(hflash, 1, sizeof hflash, f); fclose(f); }
|
||||
}
|
||||
static u8 fl_busy(void) { return 0; }
|
||||
static u8 fl_present(void) { hflash_load(); return 1; }
|
||||
static void fl_read(u32 a, u8 *p, u16 n) { hflash_load(); memcpy(p, hflash + a - HOST_BASE, n); }
|
||||
static u8 fl_write_op(u8 cmd, u32 a, const u8 *p, u16 n)
|
||||
{
|
||||
u8 *d;
|
||||
hflash_load();
|
||||
if (a < HOST_BASE || a >= HOST_BASE + sizeof hflash) return 0;
|
||||
d = hflash + a - HOST_BASE;
|
||||
if (cmd == 0x20) memset(hflash + ((a - HOST_BASE) & ~4095UL), 0xFF, 4096);
|
||||
else while (n--) *d++ &= *p++;
|
||||
hflash_store();
|
||||
return 1;
|
||||
}
|
||||
#endif
|
||||
|
||||
/* ----------------------------------------------------------------- tokens */
|
||||
@@ -116,7 +189,7 @@ enum {
|
||||
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_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_SAVE, T_LOAD, T_DIR, T_DEL, 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_POINT, T_LE, T_GE, T_NE, T_LAST
|
||||
};
|
||||
@@ -126,7 +199,7 @@ static const char *const kw[] = {
|
||||
"TO", "STEP", "NEXT", "DIM", "DEF", "END", "STOP", "REM", "READ", "DATA",
|
||||
"RESTORE", "ON", "RANDOMIZE", "POKE", "OUT", "RUN", "LIST", "NEW", "SCRATCH",
|
||||
"BYE", "CLS", "COLOR", "PLOT", "LINE", "RECT", "FILL", "CIRCLE", "LOCATE", "INK",
|
||||
"AND", "OR", "NOT", "SIN", "COS", "TAN", "ATN", "EXP", "LOG", "ABS",
|
||||
"SAVE", "LOAD", "DIR", "DEL", "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", "POINT", "<=", ">=", "<>"
|
||||
};
|
||||
@@ -193,14 +266,16 @@ static void put_u16(u16 v)
|
||||
/* ----------------------------------------------------------------- errors */
|
||||
enum {
|
||||
E_SYNTAX, E_LINE, E_RETURN, E_NEXT, E_DATA, E_SUBSCRIPT, E_DIV0, E_MEMORY,
|
||||
E_TYPE, E_STRING, E_FUNC, E_UNDEF_FN, E_FOR, E_GOSUB, E_BREAK, E_STOP
|
||||
E_TYPE, E_STRING, E_FUNC, E_UNDEF_FN, E_FOR, E_GOSUB, E_NOFILE, E_DISKFULL,
|
||||
E_DIRFULL, E_NAME, E_FLASH, E_TOOBIG, E_BREAK, E_STOP
|
||||
};
|
||||
static const char *const errmsg[] = {
|
||||
"SYNTAX ERROR", "UNDEFINED LINE NUMBER", "RETURN WITHOUT GOSUB",
|
||||
"NEXT WITHOUT FOR", "OUT OF DATA", "SUBSCRIPT OUT OF RANGE", "DIVISION BY ZERO",
|
||||
"OUT OF MEMORY", "TYPE MISMATCH", "STRING TOO LONG", "ILLEGAL FUNCTION CALL",
|
||||
"UNDEFINED FUNCTION", "FOR NESTED TOO DEEPLY", "GOSUB NESTED TOO DEEPLY",
|
||||
"BREAK", "STOP"
|
||||
"FILE NOT FOUND", "DISK FULL", "TOO MANY FILES", "BAD FILE NAME", "FLASH ERROR",
|
||||
"PROGRAM TOO BIG TO SAVE", "BREAK", "STOP"
|
||||
};
|
||||
|
||||
static u16 line_no(u8 *ln) { return ln[1] | (u16)ln[2] << 8; }
|
||||
@@ -353,15 +428,27 @@ static void tokenize(const char *s, u8 *out)
|
||||
*out = 0;
|
||||
}
|
||||
|
||||
/* Write a program line as text, keywords spelled out, without a line end. */
|
||||
static void list_to(u8 *ln, void (*out)(char))
|
||||
{
|
||||
char b[6], *q = b + 5;
|
||||
const char *k;
|
||||
u8 *p = ln + 3;
|
||||
u16 v = line_no(ln);
|
||||
*q = 0;
|
||||
do { *--q = '0' + v % 10; v /= 10; } while (v);
|
||||
while (*q) out(*q++);
|
||||
out(' ');
|
||||
for (; *p; p++) {
|
||||
if (*p >= T_LET && *p < T_LAST)
|
||||
for (k = kw[*p - T_LET]; *k; k++) out(*k);
|
||||
else out(*p);
|
||||
}
|
||||
}
|
||||
|
||||
static void list_line(u8 *ln)
|
||||
{
|
||||
u8 *p = ln + 3;
|
||||
put_u16(line_no(ln));
|
||||
putch(' ');
|
||||
for (; *p; p++) {
|
||||
if (*p >= T_LET && *p < T_LAST) puts_(kw[*p - T_LET]);
|
||||
else putch(*p);
|
||||
}
|
||||
list_to(ln, putch);
|
||||
newline();
|
||||
}
|
||||
|
||||
@@ -1287,6 +1374,283 @@ static void graphics(u8 t)
|
||||
}
|
||||
}
|
||||
|
||||
/* ---------------------------------------------------------- file system
|
||||
Programs are kept as plain text in the configuration flash from 2.25 MB to
|
||||
the end (448 blocks of 4 KB). Blocks 0 and 1 hold the directory, written
|
||||
alternately with a rising sequence number and a checksum, so a power cut
|
||||
during SAVE or DEL leaves the previous directory intact. A file is saved
|
||||
into free blocks before the directory names it. */
|
||||
#define FS_BASE 0x240000UL
|
||||
#define FS_BLOCKS 448
|
||||
#define MAXFILES 64
|
||||
#define FBLK 8 /* blocks per file: 32 KB of text */
|
||||
|
||||
struct dirent { char name[8]; u16 len; u16 blk[FBLK]; };
|
||||
static struct {
|
||||
char magic[4];
|
||||
u16 seq, next;
|
||||
struct dirent e[MAXFILES];
|
||||
u16 sum;
|
||||
} dir;
|
||||
static u8 dir_cur; /* block holding the current directory */
|
||||
static u8 bmap[FS_BLOCKS / 8]; /* blocks in use */
|
||||
static u8 page[256];
|
||||
static u16 w_pos;
|
||||
static u16 w_blk[FBLK];
|
||||
|
||||
static u32 blk_addr(u16 b) { return FS_BASE + ((u32)b << 12); }
|
||||
static u8 nblk(u16 len) { return (len + 4095) >> 12; }
|
||||
|
||||
static u16 dir_sum(void)
|
||||
{
|
||||
u8 *p = (u8 *)&dir;
|
||||
u16 n = sizeof dir - 2, s = 0x5AA5;
|
||||
while (n--) s = (s << 1 | s >> 15) + *p++;
|
||||
return s;
|
||||
}
|
||||
|
||||
static void fl_wait(void)
|
||||
{
|
||||
i32 t = millis();
|
||||
while (fl_busy())
|
||||
if (millis() - t > 3000) error(E_FLASH);
|
||||
}
|
||||
|
||||
static void fl_erase(u16 b)
|
||||
{
|
||||
if (!fl_write_op(0x20, blk_addr(b), 0, 0)) error(E_FLASH);
|
||||
fl_wait();
|
||||
}
|
||||
|
||||
/* program up to 256 bytes within one flash page, then check them */
|
||||
static void fl_prog(u32 a, const u8 *p, u16 n)
|
||||
{
|
||||
u8 chk[32];
|
||||
u16 i, k;
|
||||
if (!fl_write_op(0x02, a, p, n)) error(E_FLASH);
|
||||
fl_wait();
|
||||
for (i = 0; i < n; i += k) {
|
||||
k = n - i < 32 ? n - i : 32;
|
||||
fl_read(a + i, chk, k);
|
||||
if (memcmp(chk, p + i, k)) error(E_FLASH);
|
||||
}
|
||||
}
|
||||
|
||||
static u8 load_dir(u8 b)
|
||||
{
|
||||
fl_read(blk_addr(b), (u8 *)&dir, sizeof dir);
|
||||
return !memcmp(dir.magic, "BFS1", 4) && dir.sum == dir_sum();
|
||||
}
|
||||
|
||||
/* Read the newest valid directory, or start an empty one. */
|
||||
static void fs_mount(void)
|
||||
{
|
||||
u8 va, vb;
|
||||
u16 sa;
|
||||
if (!fl_present()) error(E_FLASH);
|
||||
va = load_dir(0);
|
||||
sa = dir.seq;
|
||||
vb = load_dir(1);
|
||||
if (va && (!vb || (i16)(sa - dir.seq) > 0)) { load_dir(0); dir_cur = 0; }
|
||||
else if (vb) dir_cur = 1;
|
||||
else {
|
||||
memset(&dir, 0, sizeof dir);
|
||||
memcpy(dir.magic, "BFS1", 4);
|
||||
dir.next = 2;
|
||||
dir_cur = 1; /* the first directory goes in block 0 */
|
||||
}
|
||||
}
|
||||
|
||||
static void fs_commit(void)
|
||||
{
|
||||
u8 b = dir_cur ^ 1, *p = (u8 *)&dir;
|
||||
u16 i, n;
|
||||
dir.seq++;
|
||||
dir.sum = dir_sum();
|
||||
fl_erase(b);
|
||||
for (i = 0; i < sizeof dir; i += 256) {
|
||||
n = sizeof dir - i < 256 ? sizeof dir - i : 256;
|
||||
fl_prog(blk_addr(b) + i, p + i, n);
|
||||
}
|
||||
dir_cur = b;
|
||||
}
|
||||
|
||||
static void build_bmap(void)
|
||||
{
|
||||
u8 i, j, n;
|
||||
memset(bmap, 0, sizeof bmap);
|
||||
bmap[0] = 3; /* the two directory blocks */
|
||||
for (i = 0; i < MAXFILES; i++)
|
||||
if (dir.e[i].name[0])
|
||||
for (j = 0, n = nblk(dir.e[i].len); j < n; j++)
|
||||
bmap[dir.e[i].blk[j] >> 3] |= 1 << (dir.e[i].blk[j] & 7);
|
||||
}
|
||||
|
||||
/* next free block after the last one handed out, spreading the wear */
|
||||
static u16 alloc_blk(void)
|
||||
{
|
||||
u16 b = dir.next, k;
|
||||
for (k = 0; k < FS_BLOCKS; k++, b++) {
|
||||
if (b >= FS_BLOCKS) b = 2;
|
||||
if (!(bmap[b >> 3] & (1 << (b & 7)))) {
|
||||
bmap[b >> 3] |= 1 << (b & 7);
|
||||
dir.next = b + 1;
|
||||
return b;
|
||||
}
|
||||
}
|
||||
error(E_DISKFULL);
|
||||
return 0;
|
||||
}
|
||||
|
||||
static void fs_putc(char c)
|
||||
{
|
||||
if (!(w_pos & 4095)) {
|
||||
u8 k = w_pos >> 12;
|
||||
if (k >= FBLK) error(E_TOOBIG);
|
||||
w_blk[k] = alloc_blk();
|
||||
fl_erase(w_blk[k]);
|
||||
}
|
||||
page[w_pos & 255] = c;
|
||||
w_pos++;
|
||||
if (!(w_pos & 255))
|
||||
fl_prog(blk_addr(w_blk[(w_pos - 1) >> 12]) + ((w_pos - 256) & 4095), page, 256);
|
||||
}
|
||||
|
||||
static void fs_flush(void)
|
||||
{
|
||||
u16 n = w_pos & 255;
|
||||
if (n) fl_prog(blk_addr(w_blk[(w_pos - 1) >> 12]) + ((w_pos - n) & 4095), page, n);
|
||||
}
|
||||
|
||||
/* File name: 1-8 letters and digits, from any string expression. */
|
||||
static void get_name(char *nm)
|
||||
{
|
||||
char s[STRMAX];
|
||||
u8 i, c;
|
||||
sexpr(s);
|
||||
memset(nm, 0, 8);
|
||||
for (i = 0; s[i]; i++) {
|
||||
c = s[i];
|
||||
if (c >= 'a' && c <= 'z') c -= 32;
|
||||
if (i >= 8 || !(is_alpha(c) || is_digit(c))) error(E_NAME);
|
||||
nm[i] = c;
|
||||
}
|
||||
if (!i) error(E_NAME);
|
||||
}
|
||||
|
||||
static i16 find_file(const char *nm)
|
||||
{
|
||||
u8 i;
|
||||
for (i = 0; i < MAXFILES; i++)
|
||||
if (!memcmp(dir.e[i].name, nm, 8)) return i;
|
||||
return -1;
|
||||
}
|
||||
|
||||
static void put_name(const char *nm)
|
||||
{
|
||||
u8 i;
|
||||
for (i = 0; i < 8; i++) putch(nm[i] ? nm[i] : ' ');
|
||||
}
|
||||
|
||||
static void st_save(void)
|
||||
{
|
||||
char nm[8];
|
||||
i16 i;
|
||||
u8 *p;
|
||||
get_name(nm);
|
||||
fs_mount();
|
||||
build_bmap();
|
||||
i = find_file(nm);
|
||||
if (i < 0) {
|
||||
for (i = 0; i < MAXFILES && dir.e[i].name[0]; i++);
|
||||
if (i == MAXFILES) error(E_DIRFULL);
|
||||
}
|
||||
w_pos = 0;
|
||||
for (p = arena; *p; p += *p) {
|
||||
list_to(p, fs_putc);
|
||||
fs_putc('\n');
|
||||
}
|
||||
fs_flush();
|
||||
memcpy(dir.e[i].name, nm, 8);
|
||||
dir.e[i].len = w_pos;
|
||||
memcpy(dir.e[i].blk, w_blk, sizeof w_blk);
|
||||
fs_commit();
|
||||
}
|
||||
|
||||
/* Store a typed or loaded line "<number> <text>"; ignores lines without a number. */
|
||||
static void enter_line(char *s)
|
||||
{
|
||||
u16 n = 0;
|
||||
while (*s == ' ') s++;
|
||||
if (!is_digit(*s)) return;
|
||||
while (is_digit(*s)) n = n * 10 + (*s++ - '0');
|
||||
while (*s == ' ') s++;
|
||||
tokenize(s, tokbuf);
|
||||
store_line(n, tokbuf);
|
||||
}
|
||||
|
||||
static void fs_load(void)
|
||||
{
|
||||
char nm[8];
|
||||
struct dirent e;
|
||||
i16 i;
|
||||
u16 pos;
|
||||
u8 n = 0, c;
|
||||
get_name(nm);
|
||||
fs_mount();
|
||||
i = find_file(nm);
|
||||
if (i < 0) error(E_NOFILE);
|
||||
e = dir.e[i];
|
||||
new_prog();
|
||||
for (pos = 0; pos < e.len; pos++) {
|
||||
if (!(pos & 255))
|
||||
fl_read(blk_addr(e.blk[pos >> 12]) + (pos & 4095), page, 256);
|
||||
c = page[pos & 255];
|
||||
if (c == '\n') {
|
||||
linebuf[n] = 0;
|
||||
enter_line(linebuf);
|
||||
n = 0;
|
||||
} else if (n < 250)
|
||||
linebuf[n++] = c;
|
||||
}
|
||||
linebuf[n] = 0;
|
||||
enter_line(linebuf);
|
||||
}
|
||||
|
||||
static void st_dir(void)
|
||||
{
|
||||
u8 i, n = 0;
|
||||
u16 used = 2;
|
||||
fs_mount();
|
||||
for (i = 0; i < MAXFILES; i++) {
|
||||
if (!dir.e[i].name[0]) continue;
|
||||
put_name(dir.e[i].name);
|
||||
do putch(' '); while (col < 10);
|
||||
put_u16(dir.e[i].len);
|
||||
newline();
|
||||
used += nblk(dir.e[i].len);
|
||||
n++;
|
||||
check_break();
|
||||
}
|
||||
put_u16(n);
|
||||
puts_(n == 1 ? " FILE, " : " FILES, ");
|
||||
put_u16((FS_BLOCKS - used) * 4);
|
||||
puts_("K FREE");
|
||||
newline();
|
||||
}
|
||||
|
||||
static void st_del(void)
|
||||
{
|
||||
char nm[8];
|
||||
i16 i;
|
||||
get_name(nm);
|
||||
fs_mount();
|
||||
i = find_file(nm);
|
||||
if (i < 0) error(E_NOFILE);
|
||||
memset(&dir.e[i], 0, sizeof dir.e[i]);
|
||||
fs_commit();
|
||||
}
|
||||
|
||||
static void statement(void);
|
||||
|
||||
static void st_if(void)
|
||||
@@ -1357,7 +1721,15 @@ static void statement(void)
|
||||
io_out(p, (u8)to_int(expr()));
|
||||
break;
|
||||
}
|
||||
case T_LOAD:
|
||||
fs_load();
|
||||
running = 0; curln = 0; tp = (u8 *)"";
|
||||
break;
|
||||
case T_SAVE: st_save(); break;
|
||||
case T_DIR: st_dir(); break;
|
||||
case T_DEL: st_del(); break;
|
||||
case T_RUN:
|
||||
if (is_str_start()) fs_load();
|
||||
run_prog();
|
||||
running = 1;
|
||||
if (is_digit(peekc())) goto_line(get_lineno());
|
||||
|
||||
+6018
-6018
File diff suppressed because it is too large.
Load diff
Reference in new issue
Block a user