Monitor: Z-80 assembler (ASM "file", A addr)
Two-pass table-driven assembler in the monitor: assembles BASIC-saved source files from the file system, or one line at a time with A addr. Labels, EQU/ORG/DB/DW/DS/END, expressions. Table generated from the disassembler (tools/gen-asmtab.py); round-trip and sdasz80 tests, plus a Z-80 build run in z80emu. Monitor RAM grows to 6000-7FFF for the symbol table. Works around an SDCC 4.5 inline strcpy bug with s_copy(). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
88b5942da5
commit
fe1daf180d
16 files changed
+14320
-13257
No files matched your search
+204
-9
@@ -71,6 +71,11 @@ static void putc_(char c) { putchar(c); }
|
||||
static u8 rd(u16 a) { return hostmem[a]; }
|
||||
#endif
|
||||
|
||||
/* SDCC 4.5 expands strcpy inline with A as scratch, and can lose a value it
|
||||
had parked in A (seen as a wrong pointer to the next call), so copy with a
|
||||
real function. */
|
||||
static void s_copy(char *d, const char *s) { while ((*d++ = *s++)); }
|
||||
|
||||
/* ------------------------------------------------------------ text out */
|
||||
static char line[96];
|
||||
static char *lp;
|
||||
@@ -81,8 +86,17 @@ static const char hexd[] = "0123456789ABCDEF";
|
||||
static void s_h2(u8 v) { s_ch(hexd[v >> 4]); s_ch(hexd[v & 15]); }
|
||||
static void s_h4(u16 v) { s_h2(v >> 8); s_h2(v); }
|
||||
/* assembler style: 3EH, 0FFH, 1234H, 0C000H */
|
||||
static void s_n8(u8 v) { if (v >= 0xA0) s_ch('0'); s_h2(v); s_ch('H'); }
|
||||
static void s_lit8(u8 v) { if (v >= 0xA0) s_ch('0'); s_h2(v); s_ch('H'); }
|
||||
#ifdef __SDCC
|
||||
#define s_n8 s_lit8
|
||||
static void s_n16(u16 v) { if (v >= 0xA000) s_ch('0'); s_h4(v); s_ch('H'); }
|
||||
#else
|
||||
/* host build: symbolic mode prints operand kinds (n, nn, e, d) instead of
|
||||
numbers, for tools/gen-asmtab.py */
|
||||
static u8 symb;
|
||||
static void s_n8(u8 v) { if (symb) s_ch('n'); else s_lit8(v); }
|
||||
static void s_n16(u16 v) { if (symb) s_str("nn"); else { if (v >= 0xA000) s_ch('0'); s_h4(v); s_ch('H'); } }
|
||||
#endif
|
||||
|
||||
/* ---------------------------------------------------------- disassembler */
|
||||
static u16 da; /* next byte to decode */
|
||||
@@ -103,8 +117,11 @@ static void s_hl(void) { s_str(ixm == 0 ? "HL" : ixm == 1 ? "IX" : "IY"); }
|
||||
|
||||
static void s_disp(u8 d)
|
||||
{
|
||||
#ifndef __SDCC
|
||||
if (symb) { s_str("+d"); return; }
|
||||
#endif
|
||||
if (d & 0x80) { s_ch('-'); d = -d; } else s_ch('+');
|
||||
s_n8(d);
|
||||
s_lit8(d);
|
||||
}
|
||||
|
||||
static void s_ind(void) /* (HL) or (IX+d), reading d */
|
||||
@@ -124,7 +141,14 @@ static void s_r8(u8 r, u8 mem)
|
||||
static void s_rp(u8 p) { if (p == 2) s_hl(); else s_str(p == 0 ? "BC" : p == 1 ? "DE" : "SP"); }
|
||||
static void s_rp2(u8 p) { if (p == 3) s_str("AF"); else s_rp(p); }
|
||||
static void s_nn(void) { u16 v = nb(); v |= (u16)nb() << 8; s_n16(v); }
|
||||
static void s_rel(void) { i16 d = (int8_t)nb(); s_n16(da + d); }
|
||||
static void s_rel(void)
|
||||
{
|
||||
i16 d = (int8_t)nb();
|
||||
#ifndef __SDCC
|
||||
if (symb) { s_ch('e'); return; }
|
||||
#endif
|
||||
s_n16(da + d);
|
||||
}
|
||||
|
||||
static void dis_cb(u8 op)
|
||||
{
|
||||
@@ -230,7 +254,7 @@ static void dis_main(u8 op)
|
||||
else { s_str("CALL "); s_nn(); }
|
||||
return;
|
||||
case 6: s_str(alun[y]); s_n8(nb()); return;
|
||||
default: s_str("RST "); s_n8(y * 8); return;
|
||||
default: s_str("RST "); s_lit8(y * 8); return;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -259,13 +283,67 @@ static u8 disasm(u16 a)
|
||||
return da - a;
|
||||
}
|
||||
|
||||
#include "asm.c"
|
||||
|
||||
#ifndef __SDCC
|
||||
static void a_out(u16 addr, u8 b) { hostmem[addr] = b; }
|
||||
|
||||
/* Host test of the assembler: "line" assembles each stdin line alone at
|
||||
address 0 and prints its bytes; "file" assembles stdin as one program in
|
||||
two passes and prints address and bytes per line, or the error. */
|
||||
static int asm_test(int file)
|
||||
{
|
||||
static char src[2000][128];
|
||||
u16 n = 0, i;
|
||||
u8 p;
|
||||
while (n < 2000 && fgets(src[n], sizeof src[n], stdin)) {
|
||||
src[n][strcspn(src[n], "\r\n")] = 0;
|
||||
a_upper(src[n]);
|
||||
n++;
|
||||
}
|
||||
if (!file) {
|
||||
a_pass = 2;
|
||||
for (i = 0; i < n; i++) {
|
||||
char t[128];
|
||||
u16 j;
|
||||
strcpy(t, src[i]);
|
||||
a_pc = 0; a_lo = 0xFFFF; a_hi = 0; a_undef = 0;
|
||||
a_line(t);
|
||||
if (a_err) printf("ERROR %s\n", a_err);
|
||||
else { for (j = 0; j < a_pc; j++) printf("%02X", hostmem[j]); printf("\n"); }
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
nsym = 0;
|
||||
for (p = 1; p <= 2; p++) {
|
||||
a_pass = p; a_pc = 0xF000; a_end = 0; a_lo = 0xFFFF; a_hi = 0;
|
||||
for (i = 0; i < n && !a_end; i++) {
|
||||
char t[128];
|
||||
u16 at = a_pc, j;
|
||||
strcpy(t, src[i]);
|
||||
a_undef = 0;
|
||||
a_line(t);
|
||||
if (a_err && (p == 2 || !a_undef)) { printf("LINE %u: %s\n", i + 1, a_err); if (p == 1) return 1; }
|
||||
if (p == 2) {
|
||||
printf("%04X ", at);
|
||||
for (j = at; j < a_pc; j++) printf("%02X", hostmem[j]);
|
||||
printf("\t%s\n", src[i]);
|
||||
}
|
||||
}
|
||||
}
|
||||
printf("%u symbols, %04X-%04X\n", nsym, a_lo, a_hi);
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* Host test: disassemble every opcode form, one per line, for the round trip
|
||||
through sdasz80 in monitor/test-disasm.sh. Format: hex bytes TAB text. */
|
||||
int main(void)
|
||||
int main(int argc, char **argv)
|
||||
{
|
||||
static const u8 pre[][2] = { {0, 0}, {0xCB, 0}, {0xED, 0}, {0xDD, 0}, {0xFD, 0}, {0xDD, 0xCB}, {0xFD, 0xCB} };
|
||||
u16 k, i;
|
||||
if (argc > 1 && !strcmp(argv[1], "line")) return asm_test(0);
|
||||
if (argc > 1 && !strcmp(argv[1], "file")) return asm_test(1);
|
||||
symb = argc > 1;
|
||||
for (k = 0; k < 7; k++)
|
||||
for (i = 0; i < 256; i++) {
|
||||
u8 n = 0, len, j;
|
||||
@@ -470,12 +548,13 @@ static void show_msg(void)
|
||||
|
||||
/* Read a command into cmd. Returns 1 for a line, or a key code for a key
|
||||
that acts at once on an empty line: 'U', 'N' (up, down), 'P', 'Q' (page). */
|
||||
static const char *prompt = "> ";
|
||||
static u8 read_cmd(void)
|
||||
{
|
||||
u8 n = 0, c;
|
||||
at(29, 0); puts_("> "); clr_eol();
|
||||
at(29, 0); puts_(prompt); clr_eol();
|
||||
show_msg();
|
||||
at(29, 2);
|
||||
at(29, strlen(prompt));
|
||||
puts_("\033[?25h");
|
||||
for (;;) {
|
||||
c = getk();
|
||||
@@ -772,6 +851,108 @@ static void mem_dir(void)
|
||||
if (!n) { at(MEM_TOP, 1); puts_("NO FILES"); }
|
||||
}
|
||||
|
||||
/* ------------------------------------------------------------ assembler */
|
||||
static void a_out(u16 addr, u8 b) { if (!wr(addr, b)) a_fail("CAN'T WRITE BELOW 8000H"); }
|
||||
|
||||
static u8 dis_shows(u16 pc);
|
||||
static char a_src[128];
|
||||
static u8 a_nerr;
|
||||
|
||||
static void s_dec(u16 v)
|
||||
{
|
||||
char b[6], *q = b + 5;
|
||||
*q = 0;
|
||||
do *--q = '0' + v % 10; while (v /= 10);
|
||||
s_str(q);
|
||||
}
|
||||
|
||||
/* one error, in the memory window (six fit) */
|
||||
static void a_report(u16 ln)
|
||||
{
|
||||
if (a_nerr < MEM_ROWS) {
|
||||
lp = line;
|
||||
s_str(" LINE "); s_dec(ln);
|
||||
s_str(": "); s_str(a_err);
|
||||
*lp = 0;
|
||||
at(MEM_TOP + a_nerr, 0); flush_line(); clr_eol();
|
||||
}
|
||||
a_nerr++;
|
||||
}
|
||||
|
||||
/* ASM "NAME": assemble a source file from flash into memory */
|
||||
static void mem_asm(void)
|
||||
{
|
||||
char nm[8];
|
||||
i16 f;
|
||||
u16 len, pos, ln;
|
||||
u8 n, p, r;
|
||||
get_fname(nm);
|
||||
fs_mount();
|
||||
f = find_file(nm);
|
||||
if (f < 0) fs_fail("FILE NOT FOUND");
|
||||
len = dir.e[f].len;
|
||||
nsym = 0;
|
||||
a_nerr = 0;
|
||||
for (r = 0; r < MEM_ROWS; r++) { at(MEM_TOP + r, 0); clr_eol(); }
|
||||
for (p = 1; p <= 2 && !a_nerr; p++) {
|
||||
a_pass = p; a_pc = 0xF000; a_end = 0; a_lo = 0xFFFF; a_hi = 0;
|
||||
n = 0; ln = 0;
|
||||
for (pos = 0; pos <= len && !a_end; pos++) {
|
||||
u8 c = '\n';
|
||||
if (pos < len) {
|
||||
if (!(pos & 255))
|
||||
fl_read(blk_addr(dir.e[f].blk[pos >> 12]) + (pos & 4095), page, 256);
|
||||
c = page[pos & 255];
|
||||
}
|
||||
if (c != '\n') { if (n < sizeof a_src - 1) a_src[n++] = c; continue; }
|
||||
a_src[n] = 0;
|
||||
n = 0;
|
||||
ln++;
|
||||
{
|
||||
u16 num = 0; /* report BASIC's line number if there is one */
|
||||
char *t = a_src;
|
||||
while (*t == ' ') t++;
|
||||
while (*t >= '0' && *t <= '9') num = num * 10 + (*t++ - '0');
|
||||
a_upper(a_src);
|
||||
a_undef = 0;
|
||||
a_line(a_src);
|
||||
if (a_err) a_report(num ? num : ln);
|
||||
}
|
||||
}
|
||||
}
|
||||
if (a_nerr) {
|
||||
lp = line; s_dec(a_nerr); s_str(a_nerr == 1 ? " ERROR" : " ERRORS"); *lp = 0;
|
||||
s_copy(load_msg, line);
|
||||
fs_fail(load_msg);
|
||||
}
|
||||
if (a_lo > a_hi) fs_fail("NOTHING ASSEMBLED");
|
||||
lp = line; s_str("ASSEMBLED "); s_h4(a_lo); s_ch('-'); s_h4(a_hi); *lp = 0;
|
||||
s_copy(load_msg, line);
|
||||
dis_a = mem_a = a_lo;
|
||||
draw_mem(); draw_dis();
|
||||
}
|
||||
|
||||
/* A addr: assemble lines typed at the bottom, one after another */
|
||||
static void line_asm(u16 a)
|
||||
{
|
||||
static char pr[10];
|
||||
for (;;) {
|
||||
lp = pr; s_ch('A'); s_ch(' '); s_h4(a); s_str(": "); *lp = 0;
|
||||
prompt = pr;
|
||||
if (read_cmd() != 1) continue;
|
||||
prompt = "> ";
|
||||
if (!cmd[0]) { msg = 0; return; }
|
||||
s_copy(a_src, cmd);
|
||||
a_pass = 2; a_pc = a; a_undef = 0; a_lo = 0xFFFF; a_hi = 0;
|
||||
a_line(a_src);
|
||||
if (a_err) { msg = a_err; msg_err = 1; continue; }
|
||||
msg = 0;
|
||||
if (a < dis_a || !dis_shows(a)) dis_a = a;
|
||||
a = a_pc;
|
||||
draw_dis(); draw_mem();
|
||||
}
|
||||
}
|
||||
|
||||
/* ---------------------------------------------------------- commands */
|
||||
static u8 op_is_call(u16 a)
|
||||
{
|
||||
@@ -824,6 +1005,20 @@ static u8 command(void)
|
||||
cp = cmd;
|
||||
skip_sp();
|
||||
msg = 0; msg_err = 0;
|
||||
if (!memcmp(cp, "ASM", 3)) {
|
||||
cp += 3;
|
||||
if (setjmp(fs_jmp)) { msg = fs_msg; msg_err = 1; return 0; }
|
||||
mem_asm();
|
||||
msg = load_msg;
|
||||
return 0;
|
||||
}
|
||||
if (cp[0] == 'A' && (cp[1] == ' ' || !cp[1])) {
|
||||
u16 a;
|
||||
cp++;
|
||||
if (!get_hex(&a)) { msg = "A ADDR, THEN INSTRUCTIONS; EMPTY LINE ENDS"; msg_err = 1; return 0; }
|
||||
line_asm(a);
|
||||
return 0;
|
||||
}
|
||||
if (!memcmp(cp, "SAVE", 4) || !memcmp(cp, "LOAD", 4) || !memcmp(cp, "DIR", 3)) {
|
||||
c = *cp;
|
||||
cp += c == 'D' ? 3 : 4;
|
||||
@@ -832,7 +1027,7 @@ static u8 command(void)
|
||||
else if (c == 'L') {
|
||||
mem_load();
|
||||
lp = line; s_str("LOADED "); s_h4(ld_first); s_ch('-'); s_h4(ld_last); *lp = 0;
|
||||
strcpy(load_msg, line);
|
||||
s_copy(load_msg, line);
|
||||
msg = load_msg;
|
||||
mem_a = ld_first;
|
||||
draw_mem(); draw_dis();
|
||||
@@ -888,7 +1083,7 @@ static u8 command(void)
|
||||
return 0;
|
||||
case '?':
|
||||
case 'H':
|
||||
msg = "SAVE \"N\" A B LOAD \"N\" [A] DIR";
|
||||
msg = "ASM \"N\" A ADDR SAVE \"N\" A B LOAD \"N\" [A] DIR";
|
||||
return 0;
|
||||
default:
|
||||
msg = "? TYPE ? FOR HELP"; msg_err = 1;
|
||||
|
||||
Reference in new issue
Block a user