BASIC: file dates and data files
DATE "YYYY-MM-DD[ HH:MM[:SS]]" sets a clock kept from the millisecond counter, DATE prints it and DATE$ returns it. Directory entries now record each file's creation date (format BFS2; a BFS1 directory is widened when read), and DIR shows it. send-bas.py and get-bas.py set the date from the PC when they connect (--no-date to skip). Data files: OPEN "NAME" FOR INPUT|OUTPUT|APPEND AS #n (n = 1-3), PRINT #n, INPUT #n, LINE INPUT [#n,], EOF(n), CLOSE [#n]. SAVE and LOAD now use the same channel code. A written file replaces the old one only at CLOSE; APPEND shares the old file's full blocks and copies the last part block. New errors FILE ALREADY OPEN, FILE NOT OPEN, INPUT PAST END; PROGRAM TOO BIG TO SAVE becomes FILE TOO BIG. The program area shrinks from 23000 to 22100 bytes to keep the stack at about 3 KB beside the file buffers. Code now ends at 7EBFh. The simulation sets the date, checks it in DIR and round-trips a data file, waiting for expected rows instead of fixed delays. Tested on the board: dates, write/append/read, EOF, the BFS1 conversion. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
29a15bf7c7
commit
767377f38d
9 files changed
+11631
-11174
No files matched your search
Binary file not shown.
+420
-81
@@ -141,7 +141,8 @@ static u8 peek(u16 a) { return fake_mem[a]; }
|
||||
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 void close_all(void);
|
||||
static void bye(void) { close_all(); exit(0); }
|
||||
static u8 gmem[240][320];
|
||||
static u16 hgx;
|
||||
static u8 hgy;
|
||||
@@ -183,15 +184,30 @@ static u8 fl_write_op(u8 cmd, u32 a, const u8 *p, u16 n)
|
||||
}
|
||||
#endif
|
||||
|
||||
/* ---------------------------------------------------------- file channels */
|
||||
#define FBLK 8 /* blocks per file: 32 KB */
|
||||
#define NCHAN 4 /* 0 is SAVE and LOAD's, 1-3 are for OPEN */
|
||||
struct chan {
|
||||
u8 mode; /* 0 closed, 'I' reading, 'O' writing */
|
||||
char name[8];
|
||||
u16 len, pos, pg; /* reading: length, position, page held in buf */
|
||||
u16 blk[FBLK];
|
||||
u8 col; /* PRINT # column */
|
||||
u8 buf[256];
|
||||
};
|
||||
static struct chan ch[NCHAN];
|
||||
static struct chan *chan_arg(u8 mode);
|
||||
static u8 chan_line(struct chan *c);
|
||||
|
||||
/* ----------------------------------------------------------------- tokens */
|
||||
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_CLS, T_COLOR, T_PLOT, T_LINE, T_RECT, T_FILL, T_CIRCLE, T_LOCATE, T_INK,
|
||||
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
|
||||
T_SAVE, T_LOAD, T_DIR, T_DEL, T_DATE, T_OPEN, T_CLOSE, T_OUTPUT, T_APPEND, 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_EOF,
|
||||
T_CHRS, T_STRS, T_LEFTS, T_RIGHTS, T_MIDS, T_DATES, T_TIM, T_POINT, T_LE, T_GE, T_NE, T_LAST
|
||||
};
|
||||
|
||||
static const char *const kw[] = {
|
||||
@@ -199,13 +215,13 @@ 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",
|
||||
"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", "<=", ">=", "<>"
|
||||
"SAVE", "LOAD", "DIR", "DEL", "DATE", "OPEN", "CLOSE", "OUTPUT", "APPEND", "AND", "OR", "NOT", "SIN", "COS", "TAN", "ATN", "EXP", "LOG", "ABS",
|
||||
"SQR", "INT", "RND", "SGN", "FN", "TAB", "PEEK", "INP", "LEN", "VAL", "ASC", "EOF",
|
||||
"CHR$", "STR$", "LEFT$", "RIGHT$", "MID$", "DATE$", "TIM", "POINT", "<=", ">=", "<>"
|
||||
};
|
||||
|
||||
/* ----------------------------------------------------------------- memory */
|
||||
#define ARENA 23000 /* program text grows up, arrays grow down */
|
||||
#define ARENA 22100 /* program text grows up, arrays grow down */
|
||||
#define STRMAX 80
|
||||
#define NVARS (26 * 11)
|
||||
#define FORMAX 16
|
||||
@@ -244,12 +260,22 @@ static u8 running;
|
||||
static u8 jumped; /* tp was moved to another statement */
|
||||
|
||||
/* ----------------------------------------------------------------- output */
|
||||
static u8 out_ch; /* PRINT # channel, 0 = the screen */
|
||||
static u8 scr_col; /* the screen's column while out_ch is set */
|
||||
static void file_putc(u8 c);
|
||||
|
||||
static void putch(char c)
|
||||
{
|
||||
if (c == '\n') { out_c('\r'); out_c('\n'); col = 0; return; }
|
||||
if (c == '\n') {
|
||||
col = 0;
|
||||
if (out_ch) file_putc('\n');
|
||||
else { out_c('\r'); out_c('\n'); }
|
||||
return;
|
||||
}
|
||||
if (c == '\b') { if (col) col--; }
|
||||
else if ((u8)c >= ' ') col++;
|
||||
out_c(c);
|
||||
if (out_ch) file_putc(c);
|
||||
else out_c(c);
|
||||
}
|
||||
|
||||
static void puts_(const char *s) { while (*s) putch(*s++); }
|
||||
@@ -267,7 +293,7 @@ static void put_u16(u16 v)
|
||||
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_NOFILE, E_DISKFULL,
|
||||
E_DIRFULL, E_NAME, E_FLASH, E_TOOBIG, E_BREAK, E_STOP
|
||||
E_DIRFULL, E_NAME, E_FLASH, E_TOOBIG, E_OPEN, E_NOTOPEN, E_EOF, E_BREAK, E_STOP
|
||||
};
|
||||
static const char *const errmsg[] = {
|
||||
"SYNTAX ERROR", "UNDEFINED LINE NUMBER", "RETURN WITHOUT GOSUB",
|
||||
@@ -275,13 +301,15 @@ static const char *const errmsg[] = {
|
||||
"OUT OF MEMORY", "TYPE MISMATCH", "STRING TOO LONG", "ILLEGAL FUNCTION CALL",
|
||||
"UNDEFINED FUNCTION", "FOR NESTED TOO DEEPLY", "GOSUB NESTED TOO DEEPLY",
|
||||
"FILE NOT FOUND", "DISK FULL", "TOO MANY FILES", "BAD FILE NAME", "FLASH ERROR",
|
||||
"PROGRAM TOO BIG TO SAVE", "BREAK", "STOP"
|
||||
"FILE TOO BIG", "FILE ALREADY OPEN", "FILE NOT OPEN", "INPUT PAST END",
|
||||
"BREAK", "STOP"
|
||||
};
|
||||
|
||||
static u16 line_no(u8 *ln) { return ln[1] | (u16)ln[2] << 8; }
|
||||
|
||||
static void error(u8 e)
|
||||
{
|
||||
if (out_ch) { out_ch = 0; col = scr_col; }
|
||||
if (col) newline();
|
||||
if (e < E_BREAK) putch('?');
|
||||
puts_(errmsg[e]);
|
||||
@@ -290,6 +318,94 @@ static void error(u8 e)
|
||||
longjmp(errjmp, 1);
|
||||
}
|
||||
|
||||
/* ------------------------------------------------------------------ clock */
|
||||
/* The date and time, set by DATE (the PC tools set it when they connect) and
|
||||
kept from the millisecond counter until the power goes off. Held as seconds
|
||||
since 2000-01-01 00:00; 0 means not set. */
|
||||
static u32 clk_sec;
|
||||
static i32 clk_ms; /* millis() at clk_sec */
|
||||
|
||||
static u32 now(void)
|
||||
{
|
||||
i32 m;
|
||||
u32 d;
|
||||
if (!clk_sec) return 0;
|
||||
m = millis();
|
||||
d = (u32)(m - clk_ms) / 1000;
|
||||
clk_sec += d;
|
||||
clk_ms += (i32)(d * 1000);
|
||||
return clk_sec;
|
||||
}
|
||||
|
||||
static const u8 mdays[] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
|
||||
static u8 leap(u16 y) { return !(y & 3) && (y % 100 || !(y % 400)); }
|
||||
static u8 month_len(u16 y, u8 m) { return mdays[m - 1] + (m == 2 && leap(y)); }
|
||||
|
||||
/* "YYYY-MM-DD HH:MM" (or with ":SS" when sec is set) for t; "" when t is 0. */
|
||||
static void fmt_date(u32 t, char *out, u8 sec)
|
||||
{
|
||||
u16 y = 2000, days = t / 86400UL;
|
||||
u8 m = 1, i;
|
||||
u32 r = t % 86400UL;
|
||||
u16 v[6];
|
||||
if (!t) { *out = 0; return; }
|
||||
while (days >= 365 + leap(y)) days -= 365 + leap(y++);
|
||||
while (days >= month_len(y, m)) days -= month_len(y, m++);
|
||||
v[0] = y; v[1] = m; v[2] = days + 1;
|
||||
v[3] = r / 3600; v[4] = r / 60 % 60; v[5] = r % 60;
|
||||
for (i = 0; i < (sec ? 6 : 5); i++) {
|
||||
if (i == 0) {
|
||||
*out++ = '0' + y / 1000; *out++ = '0' + y / 100 % 10;
|
||||
}
|
||||
*out++ = '0' + v[i] / 10 % 10;
|
||||
*out++ = '0' + v[i] % 10;
|
||||
if (i < 2) *out++ = '-';
|
||||
else if (i == 2) *out++ = ' ';
|
||||
else if (i < (sec ? 5 : 4)) *out++ = ':';
|
||||
}
|
||||
*out = 0;
|
||||
}
|
||||
|
||||
static u16 date_field(const char **p, u8 n, u16 lo, u16 hi)
|
||||
{
|
||||
u16 v = 0;
|
||||
while (n--) {
|
||||
if (**p < '0' || **p > '9') error(E_FUNC);
|
||||
v = v * 10 + (*(*p)++ - '0');
|
||||
}
|
||||
if (v < lo || v > hi) error(E_FUNC);
|
||||
return v;
|
||||
}
|
||||
|
||||
/* Set the clock from "YYYY-MM-DD[ HH:MM[:SS]]". */
|
||||
static void set_date(const char *s)
|
||||
{
|
||||
u16 y, m, d, hh = 0, mm = 0, ss = 0, i;
|
||||
u32 days;
|
||||
y = date_field(&s, 4, 2000, 2099);
|
||||
if (*s++ != '-') error(E_FUNC);
|
||||
m = date_field(&s, 2, 1, 12);
|
||||
if (*s++ != '-') error(E_FUNC);
|
||||
d = date_field(&s, 2, 1, month_len(y, m));
|
||||
if (*s) {
|
||||
if (*s++ != ' ') error(E_FUNC);
|
||||
hh = date_field(&s, 2, 0, 23);
|
||||
if (*s++ != ':') error(E_FUNC);
|
||||
mm = date_field(&s, 2, 0, 59);
|
||||
if (*s) {
|
||||
if (*s++ != ':') error(E_FUNC);
|
||||
ss = date_field(&s, 2, 0, 59);
|
||||
}
|
||||
if (*s) error(E_FUNC);
|
||||
}
|
||||
days = d - 1;
|
||||
for (i = 2000; i < y; i++) days += 365 + leap(i);
|
||||
for (i = 1; i < m; i++) days += month_len(y, i);
|
||||
clk_ms = millis();
|
||||
clk_sec = days * 86400UL + hh * 3600UL + mm * 60 + ss;
|
||||
if (!clk_sec) clk_sec = 1;
|
||||
}
|
||||
|
||||
/* ---------------------------------------------------------------- numbers */
|
||||
static const float p10[] = { 1e1f, 1e2f, 1e4f, 1e8f, 1e16f, 1e32f };
|
||||
static const float m10[] = { 1e-1f, 1e-2f, 1e-4f, 1e-8f, 1e-16f, 1e-32f };
|
||||
@@ -555,7 +671,8 @@ static u8 var_name(void)
|
||||
static u8 is_str_start(void)
|
||||
{
|
||||
u8 c = peekc();
|
||||
if (c == '"' || c == T_CHRS || c == T_STRS || c == T_LEFTS || c == T_RIGHTS || c == T_MIDS)
|
||||
if (c == '"' || c == T_CHRS || c == T_STRS || c == T_LEFTS || c == T_RIGHTS || c == T_MIDS ||
|
||||
c == T_DATES)
|
||||
return 1;
|
||||
return is_alpha(c) && tp[1] == '$';
|
||||
}
|
||||
@@ -651,6 +768,11 @@ static float func(u8 t)
|
||||
g_at(x, y);
|
||||
return g_get();
|
||||
}
|
||||
if (t == T_EOF) {
|
||||
struct chan *c = chan_arg('I');
|
||||
expect(')');
|
||||
return c->pos >= c->len ? -1 : 0;
|
||||
}
|
||||
if (t == T_LEN || t == T_VAL || t == T_ASC) {
|
||||
u8 *p = (u8 *)s;
|
||||
sexpr(s);
|
||||
@@ -840,6 +962,7 @@ static void str_prim(char *out)
|
||||
return;
|
||||
}
|
||||
tp++;
|
||||
if (c == T_DATES) { fmt_date(now(), out, 1); return; }
|
||||
expect('(');
|
||||
switch (c) {
|
||||
case T_CHRS:
|
||||
@@ -960,7 +1083,24 @@ static void skip_line(void)
|
||||
while (*tp) tp++;
|
||||
}
|
||||
|
||||
static void print_items(void);
|
||||
|
||||
static void st_print(void)
|
||||
{
|
||||
struct chan *c;
|
||||
if (!accept('#')) { print_items(); return; }
|
||||
c = chan_arg('O');
|
||||
if (!at_end()) expect(',');
|
||||
scr_col = col;
|
||||
col = c->col;
|
||||
out_ch = c - ch;
|
||||
print_items();
|
||||
out_ch = 0;
|
||||
c->col = col;
|
||||
col = scr_col;
|
||||
}
|
||||
|
||||
static void print_items(void)
|
||||
{
|
||||
char buf[STRMAX + 2];
|
||||
u8 sep = 0;
|
||||
@@ -1036,9 +1176,53 @@ static u8 assign_item(u8 **pp)
|
||||
return 1;
|
||||
}
|
||||
|
||||
/* INPUT #n, items: values separated by commas, over as many lines as needed */
|
||||
static void input_file(void)
|
||||
{
|
||||
struct chan *c = chan_arg('I');
|
||||
u8 *p;
|
||||
expect(',');
|
||||
if (!chan_line(c)) error(E_EOF);
|
||||
p = (u8 *)linebuf;
|
||||
for (;;) {
|
||||
if (!assign_item(&p)) error(E_TYPE);
|
||||
if (!accept(',')) break;
|
||||
if (*p == ',') p++;
|
||||
else if (!*p) {
|
||||
if (!chan_line(c)) error(E_EOF);
|
||||
p = (u8 *)linebuf;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/* LINE INPUT [#n,] ["prompt";] A$: a whole line, commas and quotes included */
|
||||
static void st_line_input(void)
|
||||
{
|
||||
u8 v;
|
||||
if (accept('#')) {
|
||||
struct chan *c = chan_arg('I');
|
||||
expect(',');
|
||||
if (!chan_line(c)) error(E_EOF);
|
||||
} else {
|
||||
if (peekc() == '"') {
|
||||
char prompt[STRMAX];
|
||||
str_prim(prompt);
|
||||
puts_(prompt);
|
||||
if (!accept(';')) expect(',');
|
||||
}
|
||||
if (!read_line()) error(E_BREAK);
|
||||
}
|
||||
v = peekc();
|
||||
if (!is_alpha(v) || tp[1] != '$') error(E_TYPE);
|
||||
tp += 2;
|
||||
if (strlen(linebuf) >= STRMAX) linebuf[STRMAX - 1] = 0;
|
||||
strcpy(svars[v - 'A'], linebuf);
|
||||
}
|
||||
|
||||
static void st_input(void)
|
||||
{
|
||||
u8 *p;
|
||||
if (accept('#')) { input_file(); return; }
|
||||
if (peekc() == '"') {
|
||||
char prompt[STRMAX];
|
||||
str_prim(prompt);
|
||||
@@ -1383,9 +1567,9 @@ static void graphics(u8 t)
|
||||
#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]; };
|
||||
struct dirent { char name[8]; u16 len; u16 blk[FBLK]; u16 date[2]; };
|
||||
#define OLD_DIRENT 26 /* BFS1 entries had no date */
|
||||
static struct {
|
||||
char magic[4];
|
||||
u16 seq, next;
|
||||
@@ -1394,20 +1578,19 @@ static struct {
|
||||
} 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)
|
||||
static u16 sum_of(u16 n)
|
||||
{
|
||||
u8 *p = (u8 *)&dir;
|
||||
u16 n = sizeof dir - 2, s = 0x5AA5;
|
||||
u16 s = 0x5AA5;
|
||||
while (n--) s = (s << 1 | s >> 15) + *p++;
|
||||
return s;
|
||||
}
|
||||
static u16 dir_sum(void) { return sum_of(sizeof dir - 2); }
|
||||
|
||||
static u32 e_date(u8 i) { return dir.e[i].date[0] | (u32)dir.e[i].date[1] << 16; }
|
||||
|
||||
static void fl_wait(void)
|
||||
{
|
||||
@@ -1436,10 +1619,20 @@ static void fl_prog(u32 a, const u8 *p, u16 n)
|
||||
}
|
||||
}
|
||||
|
||||
/* Read directory block b; a BFS1 directory is widened in memory, with no dates. */
|
||||
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();
|
||||
u8 *p = (u8 *)&dir, i;
|
||||
u16 old = 8 + MAXFILES * OLD_DIRENT;
|
||||
fl_read(blk_addr(b), p, sizeof dir);
|
||||
if (!memcmp(dir.magic, "BFS2", 4)) return dir.sum == dir_sum();
|
||||
if (memcmp(dir.magic, "BFS1", 4) || (p[old] | p[old + 1] << 8) != sum_of(old)) return 0;
|
||||
for (i = MAXFILES; i--; ) {
|
||||
memmove(&dir.e[i], p + 8 + i * OLD_DIRENT, OLD_DIRENT);
|
||||
dir.e[i].date[0] = dir.e[i].date[1] = 0;
|
||||
}
|
||||
memcpy(dir.magic, "BFS2", 4);
|
||||
return 1;
|
||||
}
|
||||
|
||||
/* Read the newest valid directory, or start an empty one. */
|
||||
@@ -1455,7 +1648,7 @@ static void fs_mount(void)
|
||||
else if (vb) dir_cur = 1;
|
||||
else {
|
||||
memset(&dir, 0, sizeof dir);
|
||||
memcpy(dir.magic, "BFS1", 4);
|
||||
memcpy(dir.magic, "BFS2", 4);
|
||||
dir.next = 2;
|
||||
dir_cur = 1; /* the first directory goes in block 0 */
|
||||
}
|
||||
@@ -1484,6 +1677,10 @@ static void build_bmap(void)
|
||||
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);
|
||||
for (i = 0; i < NCHAN; i++)
|
||||
if (ch[i].mode)
|
||||
for (j = 0, n = nblk(ch[i].mode == 'O' ? ch[i].pos : ch[i].len); j < n; j++)
|
||||
bmap[ch[i].blk[j] >> 3] |= 1 << (ch[i].blk[j] & 7);
|
||||
}
|
||||
|
||||
/* next free block after the last one handed out, spreading the wear */
|
||||
@@ -1502,26 +1699,6 @@ static u16 alloc_blk(void)
|
||||
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)
|
||||
{
|
||||
@@ -1552,31 +1729,187 @@ static void put_name(const char *nm)
|
||||
for (i = 0; i < 8; i++) putch(nm[i] ? nm[i] : ' ');
|
||||
}
|
||||
|
||||
static void st_save(void)
|
||||
/* ------------------------------------------------------------- channels */
|
||||
static void chan_putc(struct chan *c, u8 b)
|
||||
{
|
||||
if (!(c->pos & 4095)) {
|
||||
u8 k = c->pos >> 12;
|
||||
if (k >= FBLK) error(E_TOOBIG);
|
||||
c->blk[k] = alloc_blk();
|
||||
fl_erase(c->blk[k]);
|
||||
}
|
||||
c->buf[c->pos & 255] = b;
|
||||
c->pos++;
|
||||
if (!(c->pos & 255))
|
||||
fl_prog(blk_addr(c->blk[(c->pos - 1) >> 12]) + ((c->pos - 256) & 4095), c->buf, 256);
|
||||
}
|
||||
|
||||
static void file_putc(u8 b) { chan_putc(&ch[out_ch], b); }
|
||||
static void save_putc(char b) { chan_putc(&ch[0], b); }
|
||||
|
||||
/* next byte of an input channel, -1 at the end */
|
||||
static int chan_getc(struct chan *c)
|
||||
{
|
||||
if (c->pos >= c->len) return -1;
|
||||
if ((c->pos >> 8) != c->pg) {
|
||||
c->pg = c->pos >> 8;
|
||||
fl_read(blk_addr(c->blk[c->pos >> 12]) + (c->pos & 0xF00), c->buf, 256);
|
||||
}
|
||||
return c->buf[c->pos++ & 255];
|
||||
}
|
||||
|
||||
/* Read a line into linebuf (up to 250 characters); 0 if already at the end. */
|
||||
static u8 chan_line(struct chan *c)
|
||||
{
|
||||
u8 n = 0;
|
||||
int b = chan_getc(c);
|
||||
if (b < 0) return 0;
|
||||
while (b >= 0 && b != '\n') {
|
||||
if (n < 250) linebuf[n++] = b;
|
||||
b = chan_getc(c);
|
||||
}
|
||||
linebuf[n] = 0;
|
||||
return 1;
|
||||
}
|
||||
|
||||
static void chan_open_in(struct chan *c, const char *nm)
|
||||
{
|
||||
char nm[8];
|
||||
i16 i;
|
||||
u8 *p;
|
||||
get_name(nm);
|
||||
fs_mount();
|
||||
i = find_file(nm);
|
||||
if (i < 0) error(E_NOFILE);
|
||||
memcpy(c->name, nm, 8);
|
||||
c->len = dir.e[i].len;
|
||||
memcpy(c->blk, dir.e[i].blk, sizeof c->blk);
|
||||
c->pos = 0;
|
||||
c->pg = 0xFFFF;
|
||||
c->mode = 'I';
|
||||
}
|
||||
|
||||
/* Start writing a new copy of file nm; with append, it starts as the old one.
|
||||
The directory only changes at chan_close, so until then the old file stays. */
|
||||
static void chan_open_out(struct chan *c, const char *nm, u8 append)
|
||||
{
|
||||
i16 i;
|
||||
u16 j, len;
|
||||
u8 b;
|
||||
fs_mount();
|
||||
build_bmap();
|
||||
i = find_file(nm);
|
||||
if (i < 0) {
|
||||
for (j = 0; j < MAXFILES && dir.e[j].name[0]; j++);
|
||||
if (j == MAXFILES) error(E_DIRFULL);
|
||||
}
|
||||
memcpy(c->name, nm, 8);
|
||||
c->pos = 0;
|
||||
c->col = 0;
|
||||
c->mode = 'O';
|
||||
if (append && i >= 0) {
|
||||
struct dirent e = dir.e[i];
|
||||
len = e.len;
|
||||
b = len >> 12; /* whole blocks are shared as they are */
|
||||
memcpy(c->blk, e.blk, b * 2);
|
||||
c->pos = (u16)b << 12;
|
||||
for (j = c->pos; j < len; j++) { /* the part block is copied */
|
||||
u8 v;
|
||||
fl_read(blk_addr(e.blk[b]) + (j & 4095), &v, 1);
|
||||
chan_putc(c, v);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
static void chan_close(struct chan *c)
|
||||
{
|
||||
u16 n;
|
||||
i16 i;
|
||||
u8 m = c->mode;
|
||||
c->mode = 0;
|
||||
if (m != 'O') return;
|
||||
n = c->pos & 255;
|
||||
if (n) fl_prog(blk_addr(c->blk[(c->pos - 1) >> 12]) + ((c->pos - n) & 4095), c->buf, n);
|
||||
fs_mount();
|
||||
i = find_file(c->name);
|
||||
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');
|
||||
if (!dir.e[i].name[0] || !e_date(i)) { /* keep the creation date */
|
||||
u32 t = now();
|
||||
dir.e[i].date[0] = (u16)t;
|
||||
dir.e[i].date[1] = t >> 16;
|
||||
}
|
||||
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);
|
||||
memcpy(dir.e[i].name, c->name, 8);
|
||||
dir.e[i].len = c->pos;
|
||||
memcpy(dir.e[i].blk, c->blk, sizeof c->blk);
|
||||
fs_commit();
|
||||
}
|
||||
|
||||
static void close_all(void)
|
||||
{
|
||||
u8 i;
|
||||
for (i = 1; i < NCHAN; i++) chan_close(&ch[i]);
|
||||
}
|
||||
|
||||
/* [#]n of an open channel in the given mode */
|
||||
static struct chan *chan_arg(u8 mode)
|
||||
{
|
||||
i16 n;
|
||||
accept('#');
|
||||
n = to_int(expr());
|
||||
if (n < 1 || n >= NCHAN) error(E_FUNC);
|
||||
if (ch[n].mode != mode) error(E_NOTOPEN);
|
||||
return &ch[n];
|
||||
}
|
||||
|
||||
/* OPEN "NAME" FOR INPUT|OUTPUT|APPEND AS #n */
|
||||
static void st_open(void)
|
||||
{
|
||||
char nm[8];
|
||||
u8 m;
|
||||
i16 n;
|
||||
get_name(nm);
|
||||
expect(T_FOR);
|
||||
m = peekc();
|
||||
if (m != T_INPUT && m != T_OUTPUT && m != T_APPEND) error(E_SYNTAX);
|
||||
tp++;
|
||||
if (peekc() != 'A' || tp[1] != 'S') error(E_SYNTAX);
|
||||
tp += 2;
|
||||
accept('#');
|
||||
n = to_int(expr());
|
||||
if (n < 1 || n >= NCHAN) error(E_FUNC);
|
||||
if (ch[n].mode) error(E_OPEN);
|
||||
if (m == T_INPUT) chan_open_in(&ch[n], nm);
|
||||
else chan_open_out(&ch[n], nm, m == T_APPEND);
|
||||
}
|
||||
|
||||
/* CLOSE [#n [, #n ...]], or every file */
|
||||
static void st_close(void)
|
||||
{
|
||||
i16 n;
|
||||
if (at_end()) { close_all(); return; }
|
||||
do {
|
||||
accept('#');
|
||||
n = to_int(expr());
|
||||
if (n < 1 || n >= NCHAN) error(E_FUNC);
|
||||
chan_close(&ch[n]);
|
||||
} while (accept(','));
|
||||
}
|
||||
|
||||
static void st_save(void)
|
||||
{
|
||||
char nm[8];
|
||||
u8 *p;
|
||||
struct chan *c = &ch[0];
|
||||
c->mode = 0;
|
||||
get_name(nm);
|
||||
chan_open_out(c, nm, 0);
|
||||
for (p = arena; *p; p += *p) {
|
||||
list_to(p, save_putc);
|
||||
save_putc('\n');
|
||||
}
|
||||
chan_close(c);
|
||||
}
|
||||
|
||||
/* Store a typed or loaded line "<number> <text>"; ignores lines without a number. */
|
||||
static void enter_line(char *s)
|
||||
{
|
||||
@@ -1592,29 +1925,13 @@ static void enter_line(char *s)
|
||||
static void fs_load(void)
|
||||
{
|
||||
char nm[8];
|
||||
struct dirent e;
|
||||
i16 i;
|
||||
u16 pos;
|
||||
u8 n = 0, c;
|
||||
struct chan *c = &ch[0];
|
||||
c->mode = 0;
|
||||
get_name(nm);
|
||||
fs_mount();
|
||||
i = find_file(nm);
|
||||
if (i < 0) error(E_NOFILE);
|
||||
e = dir.e[i];
|
||||
chan_open_in(c, nm);
|
||||
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);
|
||||
while (chan_line(c)) enter_line(linebuf);
|
||||
c->mode = 0;
|
||||
}
|
||||
|
||||
static void st_dir(void)
|
||||
@@ -1627,6 +1944,12 @@ static void st_dir(void)
|
||||
put_name(dir.e[i].name);
|
||||
do putch(' '); while (col < 10);
|
||||
put_u16(dir.e[i].len);
|
||||
if (e_date(i)) {
|
||||
char d[20];
|
||||
fmt_date(e_date(i), d, 0);
|
||||
do putch(' '); while (col < 17);
|
||||
puts_(d);
|
||||
}
|
||||
newline();
|
||||
used += nblk(dir.e[i].len);
|
||||
n++;
|
||||
@@ -1686,7 +2009,7 @@ static void statement(void)
|
||||
case T_NEXT: st_next(); break;
|
||||
case T_DIM: st_dim(); break;
|
||||
case T_DEF: st_def(); break;
|
||||
case T_END: running = 0; break;
|
||||
case T_END: close_all(); running = 0; break;
|
||||
case T_STOP: error(E_STOP); break;
|
||||
case T_REM: skip_line(); break;
|
||||
case T_DATA: skip_line(); break;
|
||||
@@ -1722,13 +2045,25 @@ static void statement(void)
|
||||
break;
|
||||
}
|
||||
case T_LOAD:
|
||||
close_all();
|
||||
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_OPEN: st_open(); break;
|
||||
case T_CLOSE: st_close(); break;
|
||||
case T_DATE: {
|
||||
char d[STRMAX];
|
||||
if (is_str_start()) { sexpr(d); set_date(d); break; }
|
||||
fmt_date(now(), d, 1);
|
||||
puts_(*d ? d : "DATE NOT SET");
|
||||
newline();
|
||||
break;
|
||||
}
|
||||
case T_RUN:
|
||||
close_all();
|
||||
if (is_str_start()) fs_load();
|
||||
run_prog();
|
||||
running = 1;
|
||||
@@ -1736,9 +2071,13 @@ static void statement(void)
|
||||
break;
|
||||
case T_LIST: st_list(); break;
|
||||
case T_NEW:
|
||||
case T_SCRATCH: new_prog(); running = 0; curln = 0; tp = (u8 *)""; break;
|
||||
case T_SCRATCH: close_all(); 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_LINE:
|
||||
if (accept(T_INPUT)) { st_line_input(); break; }
|
||||
graphics(t);
|
||||
break;
|
||||
case T_CLS: case T_COLOR: case T_PLOT: case T_RECT:
|
||||
case T_FILL: case T_CIRCLE: case T_LOCATE: case T_INK:
|
||||
graphics(t);
|
||||
break;
|
||||
@@ -1762,7 +2101,7 @@ static void execute(void)
|
||||
if (peekc()) error(E_SYNTAX);
|
||||
if (!curln) break; /* end of a direct statement */
|
||||
curln += *curln;
|
||||
if (!*curln) break; /* end of program */
|
||||
if (!*curln) { close_all(); break; } /* end of program */
|
||||
tp = curln + 3;
|
||||
}
|
||||
running = 0;
|
||||
|
||||
+11062
-11062
File diff suppressed because it is too large.
Load diff
Reference in new issue
Block a user