rtl/rv32_fpu.v: IEEE 754 single precision with all five rounding modes, subnormals, the exception flags and the canonical NaN. Multiply-add (DSPs, exact product, 77-bit sum) with add, subtract and multiply as its special cases; divide and square root a bit a clock; one normalise-and- round back end. 12 clocks for add/mul/fma, 36 for div/sqrt. About 3000 LUTs and 2 DSPs; the system still meets 90 MHz. rv32_cpu.v: float registers, FLW/FSW, OP-FP and the R4 instructions, fflags/frm/fcsr, mstatus.FS (Initial at reset), misa F. tools/f32.py is an exact model (checked against x86 hardware) used by scripts/fputest.sh (random vectors, every instruction and rounding mode) and by tools/rv32emu.py, which now runs RV32IMF. cputest.sh adds rv32uf. BASIC and the games are built for rv32imf/ilp32f (BASIC 36.4 -> 28.7 KB, SIN about twice as fast); setjmp saves fs0-fs11; sqrtf is FSQRT.S. The monitor saves and restores f0-f31 and fcsr, shows them (F), sets them (R F1=...), and its disassembler and assembler know every F instruction (checked against objdump and GNU as). Docs updated. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
1246 lines
41 KiB
C
1246 lines
41 KiB
C
/* Machine-code monitor for the Basys 3 RISC-V computer.
|
|
*
|
|
* Entered by the NMI (the Pause key, BASIC's MONITOR, a breakpoint or a
|
|
* single step) or by a trap (an illegal instruction, a misaligned access,
|
|
* ebreak, ecall, an interrupt with no handler): crt0.S saves the program's
|
|
* registers in u. It shows the registers, a disassembly from the PC, the stack
|
|
* and a memory dump on the terminal's alternate screen, and takes commands on
|
|
* the bottom line. The program and the monitor share the address space, so
|
|
* memory is read directly; writes go only to RAM above BASIC's code.
|
|
*/
|
|
#include <stdint.h>
|
|
#include <string.h>
|
|
#include <setjmp.h>
|
|
|
|
typedef uint8_t u8;
|
|
typedef uint16_t u16;
|
|
typedef int16_t i16;
|
|
typedef uint32_t u32;
|
|
typedef int32_t i32;
|
|
|
|
#ifdef __riscv
|
|
#include "basys3.h"
|
|
#define con_stat IO8(0x00)
|
|
#define con_data IO8(0x01)
|
|
#define mon_ctl IO8(0x60)
|
|
#define bp_ctl IO8(0x67)
|
|
#define fl_ctl IO8(0x40)
|
|
#define fl_dat IO8(0x41)
|
|
|
|
/* saved by crt0.S at 29E80h */
|
|
struct regs {
|
|
u32 x[32]; /* x[0] is 0 */
|
|
u32 pc;
|
|
u32 how; /* 0 NMI, 1 trap */
|
|
u32 cause, tval, status; /* for a trap */
|
|
u32 f[32]; /* float registers (bit patterns) */
|
|
u32 fcsr;
|
|
u32 fs; /* mstatus.FS on entry; 0: f and fcsr not saved */
|
|
};
|
|
extern struct regs u;
|
|
|
|
static void putc_(char c) { while (!(con_stat & 2)); con_data = c; }
|
|
static u8 getk(void) { while (!(con_stat & 1)); return con_data; }
|
|
static u8 rd(u32 a) { return *(volatile u8 *)a; }
|
|
static u8 wr(u32 a, u8 v)
|
|
{
|
|
if (a < ROM_TOP || a >= 0x14000) return 0;
|
|
*(volatile u8 *)a = v;
|
|
return 1;
|
|
}
|
|
#else
|
|
/* host build: only for testing the disassembler and assembler (see main) */
|
|
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#define ROM_TOP 0
|
|
#define MC_FREE 0x12000u
|
|
static u8 hostmem[0x2A000];
|
|
static void putc_(char c) { putchar(c); }
|
|
static u8 rd(u32 a) { return a < sizeof hostmem ? hostmem[a] : 0; }
|
|
static u8 wr(u32 a, u8 v) { if (a >= sizeof hostmem) return 0; hostmem[a] = v; return 1; }
|
|
#endif
|
|
|
|
static u32 rd32(u32 a) { return rd(a) | rd(a + 1) << 8 | rd(a + 2) << 16 | (u32)rd(a + 3) << 24; }
|
|
|
|
/* ------------------------------------------------------------ text out */
|
|
static char line[100];
|
|
static char *lp;
|
|
|
|
static void s_ch(char c) { *lp++ = c; }
|
|
static void s_str(const char *s) { while (*s) *lp++ = *s++; }
|
|
static const char hexd[] = "0123456789ABCDEF";
|
|
static void s_hex(u32 v, u8 n) { while (n--) s_ch(hexd[v >> (4 * n) & 15]); }
|
|
static void s_h2(u8 v) { s_hex(v, 2); }
|
|
static void s_h8(u32 v) { s_hex(v, 8); }
|
|
static void s_udec(u32 v)
|
|
{
|
|
char b[11], *q = b + 10;
|
|
*q = 0;
|
|
do *--q = '0' + v % 10; while (v /= 10);
|
|
s_str(q);
|
|
}
|
|
static void s_dec(i32 v) { if (v < 0) { s_ch('-'); v = -v; } s_udec((u32)v); }
|
|
/* assembler style: 1F0H, 0A200H (a leading 0 when it starts with a letter) */
|
|
static void s_lit(u32 v)
|
|
{
|
|
u8 n = 1;
|
|
while (n < 8 && v >> (4 * n)) n++;
|
|
if ((v >> (4 * (n - 1)) & 15) >= 10) s_ch('0');
|
|
s_hex(v, n);
|
|
s_ch('H');
|
|
}
|
|
|
|
/* ---------------------------------------------------------- disassembler */
|
|
static const char *const regn[32] = {
|
|
"ZERO", "RA", "SP", "GP", "TP", "T0", "T1", "T2", "S0", "S1", "A0", "A1", "A2", "A3", "A4", "A5",
|
|
"A6", "A7", "S2", "S3", "S4", "S5", "S6", "S7", "S8", "S9", "S10", "S11", "T3", "T4", "T5", "T6"
|
|
};
|
|
struct csrn { u16 n; const char *s; };
|
|
static const struct csrn csrs[] = {
|
|
{ 0x300, "MSTATUS" }, { 0x301, "MISA" }, { 0x304, "MIE" }, { 0x305, "MTVEC" },
|
|
{ 0x340, "MSCRATCH" }, { 0x341, "MEPC" }, { 0x342, "MCAUSE" }, { 0x343, "MTVAL" },
|
|
{ 0x344, "MIP" }, { 0x740, "MNSCRATCH" }, { 0x741, "MNEPC" }, { 0x742, "MNCAUSE" },
|
|
{ 0x744, "MNSTATUS" }, { 0x7A0, "TSELECT" }, { 0x7A1, "TDATA1" }, { 0x7A2, "TDATA2" },
|
|
{ 0x7A5, "TCONTROL" }, { 0xB00, "MCYCLE" }, { 0xB02, "MINSTRET" }, { 0xB80, "MCYCLEH" },
|
|
{ 0xB82, "MINSTRETH" }, { 0xC00, "CYCLE" }, { 0xC01, "TIME" }, { 0xC02, "INSTRET" },
|
|
{ 0xC80, "CYCLEH" }, { 0xC81, "TIMEH" }, { 0xC82, "INSTRETH" }, { 0xF14, "MHARTID" },
|
|
{ 0x001, "FFLAGS" }, { 0x002, "FRM" }, { 0x003, "FCSR" },
|
|
{ 0, 0 }
|
|
};
|
|
|
|
static i32 sx(u32 v, u8 bits) { return (i32)(v << (32 - bits)) >> (32 - bits); }
|
|
static void s_reg(u8 r) { s_str(regn[r]); }
|
|
static void s_c(void) { s_ch(','); }
|
|
static void s_csr(u16 n)
|
|
{
|
|
const struct csrn *c;
|
|
for (c = csrs; c->s; c++) if (c->n == n) { s_str(c->s); return; }
|
|
s_lit(n);
|
|
}
|
|
static void s_mn(const char *m) { s_str(m); s_ch(' '); }
|
|
|
|
static const char *const brn[8] = { "BEQ", "BNE", 0, 0, "BLT", "BGE", "BLTU", "BGEU" };
|
|
static const char *const ldn[8] = { "LB", "LH", "LW", 0, "LBU", "LHU", 0, 0 };
|
|
static const char *const stn[4] = { "SB", "SH", "SW", 0 };
|
|
static const char *const opin[8] = { "ADDI", "SLLI", "SLTI", "SLTIU", "XORI", 0, "ORI", "ANDI" };
|
|
static const char *const opn[8] = { "ADD", "SLL", "SLT", "SLTU", "XOR", "SRL", "OR", "AND" };
|
|
static const char *const muln[8] = { "MUL", "MULH", "MULHSU", "MULHU", "DIV", "DIVU", "REM", "REMU" };
|
|
static const char *const csrin[8] = { 0, "CSRRW", "CSRRS", "CSRRC", 0, "CSRRWI", "CSRRSI", "CSRRCI" };
|
|
|
|
/* F: registers F0-F31, the rounding mode after the operands unless DYN */
|
|
static const char *const rmn[8] = { "RNE", "RTZ", "RDN", "RUP", "RMM", 0, 0, "DYN" };
|
|
static void s_freg(u8 r) { s_ch('F'); s_udec(r); }
|
|
static void s_rm(u8 m) { if (m != 7) { s_c(); s_str(rmn[m]); } }
|
|
|
|
/* An F instruction (not FLW/FSW); 0 if it isn't one */
|
|
static u8 dis_f(u32 w)
|
|
{
|
|
u8 op = w & 0x7F, rd = w >> 7 & 31, f3 = w >> 12 & 7, rs1 = w >> 15 & 31, rs2 = w >> 20 & 31;
|
|
u8 f5 = w >> 27;
|
|
if (w >> 25 & 3) return 0;
|
|
if (op != 0x53) { /* FMADD.S FMSUB.S FNMSUB.S FNMADD.S */
|
|
static const char *const r4[4] = { "FMADD.S", "FMSUB.S", "FNMSUB.S", "FNMADD.S" };
|
|
if (!rmn[f3]) return 0;
|
|
s_mn(r4[op >> 2 & 3]); s_freg(rd); s_c(); s_freg(rs1); s_c(); s_freg(rs2); s_c();
|
|
s_freg(w >> 27); s_rm(f3);
|
|
return 1;
|
|
}
|
|
switch (f5) {
|
|
case 0x00: case 0x01: case 0x02: case 0x03: {
|
|
static const char *const ar[4] = { "FADD.S", "FSUB.S", "FMUL.S", "FDIV.S" };
|
|
if (!rmn[f3]) return 0;
|
|
s_mn(ar[f5]); s_freg(rd); s_c(); s_freg(rs1); s_c(); s_freg(rs2); s_rm(f3);
|
|
return 1;
|
|
}
|
|
case 0x0B:
|
|
if (rs2 || !rmn[f3]) return 0;
|
|
s_mn("FSQRT.S"); s_freg(rd); s_c(); s_freg(rs1); s_rm(f3);
|
|
return 1;
|
|
case 0x04: {
|
|
static const char *const sj[3] = { "FSGNJ.S", "FSGNJN.S", "FSGNJX.S" };
|
|
static const char *const sp[3] = { "FMV.S", "FNEG.S", "FABS.S" };
|
|
if (f3 > 2) return 0;
|
|
if (rs1 == rs2) { s_mn(sp[f3]); s_freg(rd); s_c(); s_freg(rs1); return 1; }
|
|
s_mn(sj[f3]); s_freg(rd); s_c(); s_freg(rs1); s_c(); s_freg(rs2);
|
|
return 1;
|
|
}
|
|
case 0x05:
|
|
if (f3 > 1) return 0;
|
|
s_mn(f3 ? "FMAX.S" : "FMIN.S"); s_freg(rd); s_c(); s_freg(rs1); s_c(); s_freg(rs2);
|
|
return 1;
|
|
case 0x14: {
|
|
static const char *const cm[3] = { "FLE.S", "FLT.S", "FEQ.S" };
|
|
if (f3 > 2) return 0;
|
|
s_mn(cm[f3]); s_reg(rd); s_c(); s_freg(rs1); s_c(); s_freg(rs2);
|
|
return 1;
|
|
}
|
|
case 0x18:
|
|
if (rs2 > 1 || !rmn[f3]) return 0;
|
|
s_mn(rs2 ? "FCVT.WU.S" : "FCVT.W.S"); s_reg(rd); s_c(); s_freg(rs1); s_rm(f3);
|
|
return 1;
|
|
case 0x1A:
|
|
if (rs2 > 1 || !rmn[f3]) return 0;
|
|
s_mn(rs2 ? "FCVT.S.WU" : "FCVT.S.W"); s_freg(rd); s_c(); s_reg(rs1); s_rm(f3);
|
|
return 1;
|
|
case 0x1C:
|
|
if (rs2 || f3 > 1) return 0;
|
|
s_mn(f3 ? "FCLASS.S" : "FMV.X.W"); s_reg(rd); s_c(); s_freg(rs1);
|
|
return 1;
|
|
case 0x1E:
|
|
if (rs2 || f3) return 0;
|
|
s_mn("FMV.W.X"); s_freg(rd); s_c(); s_reg(rs1);
|
|
return 1;
|
|
}
|
|
return 0;
|
|
}
|
|
|
|
/* One instruction at a into line (at lp); returns its length, 4. */
|
|
static u8 disasm(u32 a)
|
|
{
|
|
u32 w = rd32(a);
|
|
u8 op = w & 0x7F, rd = w >> 7 & 31, f3 = w >> 12 & 7, rs1 = w >> 15 & 31, rs2 = w >> 20 & 31;
|
|
u8 f7 = w >> 25;
|
|
i32 ii = sx(w >> 20, 12);
|
|
switch (op) {
|
|
case 0x37: s_mn("LUI"); s_reg(rd); s_c(); s_lit(w >> 12); return 4;
|
|
case 0x17: s_mn("AUIPC"); s_reg(rd); s_c(); s_lit(w >> 12); return 4;
|
|
case 0x6F: {
|
|
u32 t = a + sx((w >> 31) << 20 | (w >> 12 & 0xFF) << 12 | (w >> 20 & 1) << 11 |
|
|
(w >> 21 & 0x3FF) << 1, 21);
|
|
if (rd == 0) s_mn("J");
|
|
else if (rd == 1) s_mn("JAL");
|
|
else { s_mn("JAL"); s_reg(rd); s_c(); }
|
|
s_lit(t);
|
|
return 4;
|
|
}
|
|
case 0x67:
|
|
if (f3) break;
|
|
if (rd == 0 && rs1 == 1 && !ii) { s_str("RET"); return 4; }
|
|
if (!ii && (rd == 0 || rd == 1)) { s_mn(rd ? "JALR" : "JR"); s_reg(rs1); return 4; }
|
|
s_mn("JALR"); s_reg(rd); s_c(); s_dec(ii); s_ch('('); s_reg(rs1); s_ch(')');
|
|
return 4;
|
|
case 0x63: {
|
|
u32 t = a + sx((w >> 31) << 12 | (w >> 7 & 1) << 11 | (w >> 25 & 0x3F) << 5 | (w >> 8 & 15) << 1, 13);
|
|
if (!brn[f3]) break;
|
|
if (rs2 == 0 && f3 <= 1) { s_mn(f3 ? "BNEZ" : "BEQZ"); s_reg(rs1); }
|
|
else if (rs2 == 0 && f3 == 4) { s_mn("BLTZ"); s_reg(rs1); }
|
|
else if (rs2 == 0 && f3 == 5) { s_mn("BGEZ"); s_reg(rs1); }
|
|
else if (rs1 == 0 && f3 == 4) { s_mn("BGTZ"); s_reg(rs2); }
|
|
else if (rs1 == 0 && f3 == 5) { s_mn("BLEZ"); s_reg(rs2); }
|
|
else { s_mn(brn[f3]); s_reg(rs1); s_c(); s_reg(rs2); }
|
|
s_c(); s_lit(t);
|
|
return 4;
|
|
}
|
|
case 0x03:
|
|
if (!ldn[f3]) break;
|
|
s_mn(ldn[f3]); s_reg(rd); s_c(); s_dec(ii); s_ch('('); s_reg(rs1); s_ch(')');
|
|
return 4;
|
|
case 0x23:
|
|
if (!stn[f3 & 3] || f3 > 2) break;
|
|
s_mn(stn[f3]); s_reg(rs2); s_c(); s_dec(sx((w >> 25) << 5 | rd, 12));
|
|
s_ch('('); s_reg(rs1); s_ch(')');
|
|
return 4;
|
|
case 0x13:
|
|
if (f3 == 1 || f3 == 5) {
|
|
if (f7 && !(f3 == 5 && f7 == 0x20)) break;
|
|
s_mn(f3 == 1 ? "SLLI" : f7 ? "SRAI" : "SRLI");
|
|
s_reg(rd); s_c(); s_reg(rs1); s_c(); s_udec(rs2);
|
|
return 4;
|
|
}
|
|
if (w == 0x00000013) { s_str("NOP"); return 4; }
|
|
if (f3 == 0 && rs1 == 0) { s_mn("LI"); s_reg(rd); s_c(); s_dec(ii); return 4; }
|
|
if (f3 == 0 && ii == 0) { s_mn("MV"); s_reg(rd); s_c(); s_reg(rs1); return 4; }
|
|
if (f3 == 4 && ii == -1) { s_mn("NOT"); s_reg(rd); s_c(); s_reg(rs1); return 4; }
|
|
if (f3 == 3 && ii == 1) { s_mn("SEQZ"); s_reg(rd); s_c(); s_reg(rs1); return 4; }
|
|
s_mn(opin[f3]); s_reg(rd); s_c(); s_reg(rs1); s_c(); s_dec(ii);
|
|
return 4;
|
|
case 0x33:
|
|
if (f7 == 1) { s_mn(muln[f3]); }
|
|
else if (f7 == 0x20 && f3 == 0) {
|
|
if (rs1 == 0) { s_mn("NEG"); s_reg(rd); s_c(); s_reg(rs2); return 4; }
|
|
s_mn("SUB");
|
|
}
|
|
else if (f7 == 0x20 && f3 == 5) s_mn("SRA");
|
|
else if (f7 == 0) {
|
|
if (f3 == 3 && rs1 == 0) { s_mn("SNEZ"); s_reg(rd); s_c(); s_reg(rs2); return 4; }
|
|
if (f3 == 2 && rs2 == 0) { s_mn("SLTZ"); s_reg(rd); s_c(); s_reg(rs1); return 4; }
|
|
if (f3 == 2 && rs1 == 0) { s_mn("SGTZ"); s_reg(rd); s_c(); s_reg(rs2); return 4; }
|
|
s_mn(opn[f3]);
|
|
}
|
|
else break;
|
|
s_reg(rd); s_c(); s_reg(rs1); s_c(); s_reg(rs2);
|
|
return 4;
|
|
case 0x07:
|
|
if (f3 != 2) break;
|
|
s_mn("FLW"); s_freg(rd); s_c(); s_dec(ii); s_ch('('); s_reg(rs1); s_ch(')');
|
|
return 4;
|
|
case 0x27:
|
|
if (f3 != 2) break;
|
|
s_mn("FSW"); s_freg(rs2); s_c(); s_dec(sx((w >> 25) << 5 | rd, 12));
|
|
s_ch('('); s_reg(rs1); s_ch(')');
|
|
return 4;
|
|
case 0x43: case 0x47: case 0x4B: case 0x4F: case 0x53:
|
|
if (dis_f(w)) return 4;
|
|
break;
|
|
case 0x0F:
|
|
if (f3 == 0) { s_str("FENCE"); return 4; }
|
|
if (f3 == 1) { s_str("FENCE.I"); return 4; }
|
|
break;
|
|
case 0x73: {
|
|
u16 c = w >> 20;
|
|
if (w == 0x00000073) { s_str("ECALL"); return 4; }
|
|
if (w == 0x00100073) { s_str("EBREAK"); return 4; }
|
|
if (w == 0x30200073) { s_str("MRET"); return 4; }
|
|
if (w == 0x70200073) { s_str("MNRET"); return 4; }
|
|
if (w == 0x10500073) { s_str("WFI"); return 4; }
|
|
if (!csrin[f3]) break;
|
|
if (f3 == 2 && rs1 == 0) { s_mn("CSRR"); s_reg(rd); s_c(); s_csr(c); return 4; }
|
|
if (rd == 0) {
|
|
static const char *const sh[8] = { 0, "CSRW", "CSRS", "CSRC", 0, "CSRWI", "CSRSI", "CSRCI" };
|
|
s_mn(sh[f3]); s_csr(c); s_c();
|
|
if (f3 & 4) s_udec(rs1); else s_reg(rs1);
|
|
return 4;
|
|
}
|
|
s_mn(csrin[f3]); s_reg(rd); s_c(); s_csr(c); s_c();
|
|
if (f3 & 4) s_udec(rs1); else s_reg(rs1);
|
|
return 4;
|
|
}
|
|
}
|
|
s_mn("DW"); s_lit(w);
|
|
return 4;
|
|
}
|
|
|
|
#ifndef __riscv
|
|
#include "asm.c"
|
|
|
|
/* Host test: "dis" reads hex words, one a line, and disassembles each as if
|
|
at address 1000h; "asm" assembles lines (at 1000h each, after a first pass
|
|
over all of them for labels) and prints the words. */
|
|
int main(int argc, char **argv)
|
|
{
|
|
char buf[200];
|
|
if (argc > 1 && !strcmp(argv[1], "dis")) {
|
|
while (fgets(buf, sizeof buf, stdin)) {
|
|
u32 w = (u32)strtoul(buf, 0, 16);
|
|
hostmem[0x1000] = w; hostmem[0x1001] = w >> 8; hostmem[0x1002] = w >> 16; hostmem[0x1003] = w >> 24;
|
|
lp = line;
|
|
disasm(0x1000);
|
|
*lp = 0;
|
|
puts(line);
|
|
}
|
|
return 0;
|
|
}
|
|
if (argc > 1 && !strcmp(argv[1], "asm")) {
|
|
static char src[4000][100];
|
|
int n = 0, i, p;
|
|
while (n < 4000 && fgets(src[n], sizeof src[n], stdin)) {
|
|
src[n][strcspn(src[n], "\n")] = 0;
|
|
n++;
|
|
}
|
|
nsym = 0;
|
|
for (p = 1; p <= 2; p++) {
|
|
a_pass = p; a_pc = 0x1000; a_end = 0; a_lo = 0xFFFFFFFF; a_hi = 0;
|
|
for (i = 0; i < n && !a_end; i++) {
|
|
u32 at = a_pc, k;
|
|
strcpy(buf, src[i]);
|
|
a_upper(buf);
|
|
a_undef = 0;
|
|
a_lnum = i + 1;
|
|
a_line(buf);
|
|
if (p == 2) {
|
|
if (a_err) printf("ERROR %s: %s\n", a_err, src[i]);
|
|
else {
|
|
for (k = at; k + 3 < a_pc; k += 4)
|
|
printf("%08X\n", rd(k) | rd(k + 1) << 8 | rd(k + 2) << 16 | (u32)rd(k + 3) << 24);
|
|
if ((a_pc - at) & 3) {
|
|
for (; k < a_pc; k++) printf("%02X", rd(k));
|
|
printf("\n");
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
return 0;
|
|
}
|
|
if (argc > 1 && !strcmp(argv[1], "asmbin")) { /* a whole program: the bytes it fills */
|
|
static char src[4000][100];
|
|
int n = 0, i, p, errs = 0;
|
|
u32 k;
|
|
while (n < 4000 && fgets(src[n], sizeof src[n], stdin)) { src[n][strcspn(src[n], "\n")] = 0; n++; }
|
|
nsym = 0;
|
|
for (p = 1; p <= 2; p++) {
|
|
a_pass = p; a_pc = 0x12000; a_end = 0; a_lo = 0xFFFFFFFF; a_hi = 0;
|
|
for (i = 0; i < n && !a_end; i++) {
|
|
strcpy(buf, src[i]);
|
|
a_upper(buf);
|
|
a_undef = 0;
|
|
a_lnum = i + 1;
|
|
a_line(buf);
|
|
if (a_err && (p == 2 || a_err[0] != 'U')) { printf("ERROR line %d: %s\n", i + 1, a_err); errs++; }
|
|
}
|
|
if (errs) return 1;
|
|
}
|
|
printf("%05X ", a_lo);
|
|
for (k = a_lo; k <= a_hi; k++) printf("%02X", rd(k));
|
|
printf("\n");
|
|
return 0;
|
|
}
|
|
fprintf(stderr, "monitor-host dis|asm|asmbin < input\n");
|
|
return 1;
|
|
}
|
|
#else
|
|
|
|
/* ------------------------------------------------------------- screen */
|
|
static void puts_(const char *s) { while (*s) putc_(*s++); }
|
|
static void at(u8 r, u8 c) /* r, c from 0 */
|
|
{
|
|
char b[12], *p = b;
|
|
*p++ = 27; *p++ = '[';
|
|
if (r >= 9) *p++ = '0' + (r + 1) / 10;
|
|
*p++ = '0' + (r + 1) % 10; *p++ = ';';
|
|
if (c >= 9) *p++ = '0' + (c + 1) / 10;
|
|
*p++ = '0' + (c + 1) % 10; *p++ = 'H'; *p = 0;
|
|
puts_(b);
|
|
}
|
|
static void sgr(const char *s) { putc_(27); putc_('['); puts_(s); putc_('m'); }
|
|
static void flush_line(void) { *lp = 0; puts_(line); }
|
|
static void clr_eol(void) { puts_("\033[K"); }
|
|
|
|
/* --------------------------------------------------------------- state */
|
|
#define REG_TOP 2 /* six rows of registers */
|
|
#define DIS_ROWS 12
|
|
#define MEM_ROWS 6
|
|
#define DIS_TOP 9
|
|
#define MEM_TOP 22
|
|
|
|
static u32 dis_a; /* first line of the disassembly window */
|
|
static u32 mem_a; /* first line of the memory window */
|
|
static u32 bp; /* the user's breakpoint */
|
|
static u8 bp_set;
|
|
static u8 go_after; /* stepping off a breakpoint, then G */
|
|
static u16 steps; /* more single steps to do */
|
|
static char cmd[80];
|
|
static u8 msg_err;
|
|
static const char *msg;
|
|
static u8 why_in; /* why the monitor was entered */
|
|
static u8 show_f; /* the float registers in the memory window */
|
|
static char load_msg[32];
|
|
|
|
/* ------------------------------------------------------------- drawing */
|
|
/* " PC 00012000 ": 13 columns, six to a line */
|
|
static void s_rv(const char *n, u32 v)
|
|
{
|
|
u8 k = strlen(n);
|
|
while (k++ < 3) s_ch(' ');
|
|
s_str(n); s_ch(' '); s_h8(v); s_ch(' ');
|
|
}
|
|
|
|
static void draw_regs(void)
|
|
{
|
|
u8 r, i;
|
|
for (r = 0; r < 6; r++) {
|
|
lp = line;
|
|
for (i = 0; i < 6; i++) {
|
|
u8 k = r * 6 + i;
|
|
if (k == 0) s_rv("PC", u.pc);
|
|
else if (k < 32) s_rv(regn[k], u.x[k]);
|
|
}
|
|
at(REG_TOP + r, 0); flush_line(); clr_eol();
|
|
}
|
|
}
|
|
|
|
static void draw_dis(void)
|
|
{
|
|
u32 a = dis_a, pc = u.pc;
|
|
u8 r;
|
|
for (r = 0; r < DIS_ROWS; r++) {
|
|
lp = line;
|
|
s_ch(a == pc ? '>' : ' ');
|
|
s_ch(bp_set && a == bp ? '*' : ' ');
|
|
s_hex(a, 5); s_str(" ");
|
|
s_h8(rd32(a)); s_str(" ");
|
|
disasm(a);
|
|
while (lp < line + 50) s_ch(' ');
|
|
at(DIS_TOP + r, 0);
|
|
if (a == pc) sgr("1;33;44");
|
|
flush_line();
|
|
if (a == pc) sgr("0");
|
|
a += 4;
|
|
}
|
|
}
|
|
|
|
static void draw_stack(void)
|
|
{
|
|
u32 s = u.x[2];
|
|
u8 r;
|
|
at(DIS_TOP - 1, 54); sgr("36"); puts_("STACK"); sgr("0");
|
|
for (r = 0; r < DIS_ROWS; r++) {
|
|
lp = line;
|
|
s_hex(s, 5); s_str(" "); s_h8(rd32(s));
|
|
at(DIS_TOP + r, 54); flush_line(); clr_eol();
|
|
s += 4;
|
|
}
|
|
}
|
|
|
|
/* the float registers and fcsr, where the memory window goes */
|
|
static void draw_fregs(void)
|
|
{
|
|
u8 r, i;
|
|
at(MEM_TOP - 1, 0); sgr("36"); puts_(" FLOAT REGISTERS"); sgr("0"); clr_eol();
|
|
for (r = 0; r < MEM_ROWS; r++) {
|
|
lp = line;
|
|
if (!u.fs) { if (r == 0) s_str(" THE FPU IS OFF (MSTATUS.FS = 0)"); }
|
|
else for (i = 0; i < 6; i++) {
|
|
u8 k = r * 6 + i;
|
|
char n[4];
|
|
if (k < 32) {
|
|
n[0] = 'F'; n[1] = k < 10 ? '0' + k : '0' + k / 10;
|
|
n[2] = k < 10 ? 0 : '0' + k % 10; n[3] = 0;
|
|
s_rv(n, u.f[k]);
|
|
} else if (k == 32) s_rv("FCSR", u.fcsr);
|
|
}
|
|
at(MEM_TOP + r, 0); flush_line(); clr_eol();
|
|
}
|
|
}
|
|
|
|
static void draw_mem(void)
|
|
{
|
|
u32 a = mem_a;
|
|
u8 r, j, c;
|
|
if (show_f) { draw_fregs(); return; }
|
|
at(MEM_TOP - 1, 0); sgr("36"); puts_(" MEMORY"); sgr("0"); clr_eol();
|
|
for (r = 0; r < MEM_ROWS; r++) {
|
|
lp = line;
|
|
s_ch(' '); s_hex(a, 5); s_str(" ");
|
|
for (j = 0; j < 16; j++) { s_h2(rd(a + j)); s_ch(j == 7 ? '-' : ' '); }
|
|
s_ch(' ');
|
|
for (j = 0; j < 16; j++) { c = rd(a + j); s_ch(c >= 32 && c < 127 ? c : '.'); }
|
|
at(MEM_TOP + r, 0); flush_line(); clr_eol();
|
|
a += 16;
|
|
}
|
|
}
|
|
|
|
static const char *why_text(void)
|
|
{
|
|
static char t[32];
|
|
if (u.how) {
|
|
u32 c = u.cause;
|
|
if (c >> 31) { lp = t; s_str("INTERRUPT "); s_udec(c & 31); *lp = 0; return t; }
|
|
switch (c) {
|
|
case 0: return "MISALIGNED JUMP";
|
|
case 2: return "ILLEGAL INSTRUCTION";
|
|
case 3: return "EBREAK";
|
|
case 4: return "MISALIGNED LOAD";
|
|
case 6: return "MISALIGNED STORE";
|
|
case 11: return "ECALL";
|
|
default: lp = t; s_str("TRAP "); s_udec(c); *lp = 0; return t;
|
|
}
|
|
}
|
|
switch (why_in) {
|
|
case 1: return "PAUSE KEY";
|
|
case 2: return "MONITOR COMMAND";
|
|
case 3: return "STEP";
|
|
case 4: return "BREAKPOINT";
|
|
case 5: return "STEP OVER";
|
|
default: return "";
|
|
}
|
|
}
|
|
|
|
static void draw_all(void)
|
|
{
|
|
puts_("\033[2J");
|
|
at(0, 0); sgr("1;37;44");
|
|
lp = line;
|
|
s_str(" RISC-V MONITOR");
|
|
while (lp < line + 50) s_ch(' ');
|
|
s_str("STOPPED BY ");
|
|
s_str(why_text());
|
|
while (lp < line + 80) s_ch(' ');
|
|
*lp = 0;
|
|
line[80] = 0;
|
|
puts_(line); sgr("0");
|
|
draw_regs();
|
|
at(DIS_TOP - 1, 0); sgr("36"); puts_(" ADDR WORD INSTRUCTION"); sgr("0");
|
|
draw_dis();
|
|
draw_stack();
|
|
draw_mem();
|
|
at(28, 0); sgr("36");
|
|
puts_(" ENTER step O over G go X exit B bp D/M addr E edit R reg F fp ? help");
|
|
sgr("0");
|
|
}
|
|
|
|
/* --------------------------------------------------------------- input */
|
|
static u8 is_hex(char c) { return (c >= '0' && c <= '9') || (c >= 'A' && c <= 'F'); }
|
|
static char *cp;
|
|
static void skip_sp(void) { while (*cp == ' ' || *cp == ',') cp++; }
|
|
|
|
/* hex number at cp; 0 if none */
|
|
static u8 get_hex(u32 *v)
|
|
{
|
|
u8 n = 0;
|
|
skip_sp();
|
|
*v = 0;
|
|
while (is_hex(*cp)) {
|
|
*v = *v << 4 | (*cp <= '9' ? *cp - '0' : *cp - 'A' + 10);
|
|
cp++; n++;
|
|
}
|
|
return n;
|
|
}
|
|
|
|
static void show_msg(void)
|
|
{
|
|
at(29, 40); clr_eol();
|
|
if (msg) { sgr(msg_err ? "1;31" : "32"); puts_(msg); sgr("0"); }
|
|
}
|
|
|
|
/* 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_(prompt); clr_eol();
|
|
show_msg();
|
|
at(29, strlen(prompt));
|
|
puts_("\033[?25h");
|
|
for (;;) {
|
|
c = getk();
|
|
if (c == 27) {
|
|
c = getk();
|
|
if (c == '[' || c == 'O') {
|
|
u8 k = getk(), t = k;
|
|
while (t >= '0' && t <= '9') t = getk(); /* ESC [ 5 ~ */
|
|
puts_("\033[?25l");
|
|
if (k == 'A') return 'U';
|
|
if (k == 'B') return 'N';
|
|
if (k == '5') return 'P';
|
|
if (k == '6') return 'Q';
|
|
puts_("\033[?25h");
|
|
}
|
|
continue;
|
|
}
|
|
if (c == '\r' || c == '\n') { cmd[n] = 0; puts_("\033[?25l"); return 1; }
|
|
if (c == 8 || c == 127) { if (n) { n--; puts_("\b \b"); } continue; }
|
|
if (c == 21) { while (n) { n--; puts_("\b \b"); } continue; }
|
|
if (c >= 'a' && c <= 'z') c -= 32;
|
|
if (c >= ' ' && c < 127 && n < sizeof cmd - 1) { cmd[n++] = c; putc_(c); }
|
|
}
|
|
}
|
|
|
|
/* ------------------------------------------------------- flash files */
|
|
/* The same file system as BASIC's (basic.c): 448 blocks of 4 KB from
|
|
240000h, a directory in block 0 or 1 (the newer valid one). A memory file
|
|
holds its load address (4 bytes, low first) and then the bytes. */
|
|
#define FS_BASE 0x240000UL
|
|
#define FS_BLOCKS 448
|
|
#define MAXFILES 64
|
|
#define FBLK 8
|
|
|
|
struct dirent { char name[8]; u16 len; u16 blk[FBLK]; u16 date[2]; };
|
|
#define OLD_DIRENT 26
|
|
static struct {
|
|
char magic[4];
|
|
u16 seq, next;
|
|
struct dirent e[MAXFILES];
|
|
u16 sum;
|
|
} dir;
|
|
static u8 dir_cur;
|
|
static u8 bmap[FS_BLOCKS / 8];
|
|
static u8 page[256];
|
|
static u16 w_pos;
|
|
static u16 w_blk[FBLK];
|
|
static jmp_buf fs_jmp;
|
|
static const char *fs_msg;
|
|
|
|
static void fs_fail(const char *m) { fs_msg = m; longjmp(fs_jmp, 1); }
|
|
|
|
static i32 millis(void) { return (i32)IO_MS; }
|
|
static u32 now(void)
|
|
{
|
|
u32 s = CLK_SEC;
|
|
return s ? s + (u32)(millis() - CLK_MS) / 1000 : 0;
|
|
}
|
|
|
|
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 void fl_read(u32 a, u8 *p, u16 n)
|
|
{
|
|
fl_open(0x03); fl_addr(a);
|
|
while (n--) *p++ = spi(0);
|
|
fl_close();
|
|
}
|
|
static void fl_wait(void)
|
|
{
|
|
i32 t = millis();
|
|
u8 st;
|
|
do {
|
|
fl_open(0x05); st = spi(0); fl_close();
|
|
if (millis() - t > 3000) fs_fail("FLASH ERROR");
|
|
} while (st & 1);
|
|
}
|
|
static void 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();
|
|
if (!ok) fs_fail("FLASH ERROR");
|
|
fl_wait();
|
|
}
|
|
static u32 blk_addr(u16 b) { return FS_BASE + ((u32)b << 12); }
|
|
static u8 nblk(u16 len) { return (len + 4095) >> 12; }
|
|
static void fl_prog(u32 a, const u8 *p, u16 n)
|
|
{
|
|
u8 chk[32];
|
|
u16 i, k;
|
|
fl_write_op(0x02, a, p, n);
|
|
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)) fs_fail("FLASH ERROR");
|
|
}
|
|
}
|
|
|
|
static u16 sum_of(u16 n)
|
|
{
|
|
u8 *p = (u8 *)&dir;
|
|
u16 s = 0x5AA5;
|
|
while (n--) s = (s << 1 | s >> 15) + *p++;
|
|
return s;
|
|
}
|
|
static u8 load_dir(u8 b)
|
|
{
|
|
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 == sum_of(sizeof dir - 2);
|
|
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;
|
|
}
|
|
static void fs_mount(void)
|
|
{
|
|
u8 va, vb;
|
|
u16 sa;
|
|
fl_open(0x9F); va = spi(0); vb = spi(0); sa = spi(0); fl_close();
|
|
if (va != 0xC2 || vb != 0x20 || sa != 0x16) fs_fail("NO 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, "BFS2", 4);
|
|
dir.next = 2;
|
|
dir_cur = 1;
|
|
}
|
|
}
|
|
static void fs_commit(void)
|
|
{
|
|
u8 b = dir_cur ^ 1, *p = (u8 *)&dir;
|
|
u16 i, n;
|
|
dir.seq++;
|
|
dir.sum = sum_of(sizeof dir - 2);
|
|
fl_write_op(0x20, blk_addr(b), 0, 0);
|
|
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 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 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;
|
|
}
|
|
}
|
|
fs_fail("DISK FULL");
|
|
return 0;
|
|
}
|
|
static void build_bmap(void)
|
|
{
|
|
u8 i, j, n;
|
|
memset(bmap, 0, sizeof bmap);
|
|
bmap[0] = 3;
|
|
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);
|
|
}
|
|
static void put_b(u8 c)
|
|
{
|
|
if (!(w_pos & 4095)) {
|
|
w_blk[w_pos >> 12] = alloc_blk();
|
|
fl_write_op(0x20, blk_addr(w_blk[w_pos >> 12]), 0, 0);
|
|
}
|
|
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);
|
|
}
|
|
|
|
/* "NAME" at cp: 1-8 letters and digits */
|
|
static void get_fname(char *nm)
|
|
{
|
|
u8 n = 0;
|
|
skip_sp();
|
|
if (*cp++ != '"') fs_fail("NAME IN QUOTES");
|
|
memset(nm, 0, 8);
|
|
while (*cp && *cp != '"') {
|
|
if (n == 8 || !((*cp >= 'A' && *cp <= 'Z') || (*cp >= '0' && *cp <= '9'))) fs_fail("BAD FILE NAME");
|
|
nm[n++] = *cp++;
|
|
}
|
|
if (*cp) cp++;
|
|
if (!n) fs_fail("BAD FILE NAME");
|
|
}
|
|
|
|
/* SAVE "NAME" start end: memory start..end (inclusive) to a file */
|
|
static void mem_save(void)
|
|
{
|
|
char nm[8];
|
|
u32 a, e, n;
|
|
i16 i;
|
|
get_fname(nm);
|
|
if (!get_hex(&a) || !get_hex(&e) || e < a) fs_fail("SAVE \"NAME\" START END");
|
|
n = e - a + 1;
|
|
if (n > 32768U - 4) fs_fail("FILE TOO BIG");
|
|
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) fs_fail("TOO MANY FILES");
|
|
}
|
|
w_pos = 0;
|
|
put_b(a); put_b(a >> 8); put_b(a >> 16); put_b(a >> 24);
|
|
while (n--) put_b(rd(a++));
|
|
if (w_pos & 255)
|
|
fl_prog(blk_addr(w_blk[(w_pos - 1) >> 12]) + ((w_pos - (w_pos & 255)) & 4095), page, w_pos & 255);
|
|
if (!dir.e[i].name[0] || !(dir.e[i].date[0] | dir.e[i].date[1])) {
|
|
u32 t = now();
|
|
dir.e[i].date[0] = t; dir.e[i].date[1] = t >> 16;
|
|
}
|
|
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();
|
|
}
|
|
|
|
/* LOAD "NAME" [addr]: a memory file back, to its own address or addr */
|
|
static u32 ld_first, ld_last;
|
|
static void mem_load(void)
|
|
{
|
|
char nm[8];
|
|
u32 a = 0, la = 0;
|
|
u16 pos, len;
|
|
i16 i;
|
|
u8 has;
|
|
get_fname(nm);
|
|
has = get_hex(&a);
|
|
fs_mount();
|
|
i = find_file(nm);
|
|
if (i < 0) fs_fail("FILE NOT FOUND");
|
|
len = dir.e[i].len;
|
|
if (len < 4) fs_fail("NOT A MEMORY FILE");
|
|
for (pos = 0; pos < len; pos++) {
|
|
u8 c;
|
|
if (!(pos & 255))
|
|
fl_read(blk_addr(dir.e[i].blk[pos >> 12]) + (pos & 4095), page, 256);
|
|
c = page[pos & 255];
|
|
if (pos < 4) {
|
|
la |= (u32)c << (8 * pos);
|
|
if (pos == 3) { if (!has) a = la; ld_first = a; }
|
|
} else {
|
|
if (!wr(a, c)) fs_fail("CAN'T LOAD THERE");
|
|
ld_last = a++;
|
|
}
|
|
}
|
|
}
|
|
|
|
/* DIR: the files, four to a line, in the memory window */
|
|
static void mem_dir(void)
|
|
{
|
|
u8 i, n = 0, r;
|
|
fs_mount();
|
|
for (r = 0; r < MEM_ROWS; r++) { at(MEM_TOP + r, 0); clr_eol(); }
|
|
for (i = 0; i < MAXFILES && n < MEM_ROWS * 4; i++) {
|
|
u8 j;
|
|
if (!dir.e[i].name[0]) continue;
|
|
lp = line;
|
|
for (j = 0; j < 8; j++) s_ch(dir.e[i].name[j] ? dir.e[i].name[j] : ' ');
|
|
s_ch(' '); s_hex(dir.e[i].len, 4); s_ch('H');
|
|
at(MEM_TOP + n / 4, 1 + (n & 3) * 20); flush_line();
|
|
n++;
|
|
}
|
|
if (!n) { at(MEM_TOP, 1); puts_("NO FILES"); }
|
|
}
|
|
|
|
/* ------------------------------------------------------------ assembler */
|
|
#include "asm.c"
|
|
|
|
static char a_src[128];
|
|
static u8 a_nerr;
|
|
|
|
/* 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_udec(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 = MC_FREE; a_end = 0; a_lo = 0xFFFFFFFF; 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_lnum = ln;
|
|
a_line(a_src);
|
|
if (a_err) a_report(num ? num : ln);
|
|
}
|
|
}
|
|
}
|
|
if (a_nerr) {
|
|
lp = line; s_udec(a_nerr); s_str(a_nerr == 1 ? " ERROR" : " ERRORS"); *lp = 0;
|
|
strcpy(load_msg, line);
|
|
fs_fail(load_msg);
|
|
}
|
|
if (a_lo > a_hi) fs_fail("NOTHING ASSEMBLED");
|
|
lp = line; s_str("ASSEMBLED "); s_hex(a_lo, 5); s_ch('-'); s_hex(a_hi, 5); *lp = 0;
|
|
strcpy(load_msg, line);
|
|
dis_a = a_lo & ~3u;
|
|
mem_a = a_lo & ~15u;
|
|
draw_mem(); draw_dis();
|
|
}
|
|
|
|
static u8 dis_shows(u32 pc) { return pc >= dis_a && pc < dis_a + 4 * (DIS_ROWS - 2); }
|
|
|
|
/* A addr: assemble lines typed at the bottom, one after another */
|
|
static void line_asm(u32 a)
|
|
{
|
|
static char pr[12];
|
|
for (;;) {
|
|
lp = pr; s_ch('A'); s_ch(' '); s_hex(a, 5); s_str(": "); *lp = 0;
|
|
prompt = pr;
|
|
if (read_cmd() != 1) continue;
|
|
prompt = "> ";
|
|
if (!cmd[0]) { msg = 0; return; }
|
|
strcpy(a_src, cmd);
|
|
a_pass = 2; a_pc = a; a_undef = 0; a_lo = 0xFFFFFFFF; a_hi = 0; a_lnum = 0;
|
|
a_line(a_src);
|
|
if (a_err) { msg = a_err; msg_err = 1; continue; }
|
|
msg = 0;
|
|
if (!dis_shows(a)) dis_a = a & ~3u;
|
|
a = a_pc;
|
|
draw_dis(); draw_mem();
|
|
}
|
|
}
|
|
|
|
/* ---------------------------------------------------------- commands */
|
|
/* JAL or JALR that saves the return address in RA: a call */
|
|
static u8 op_is_call(u32 a)
|
|
{
|
|
u32 w = rd32(a);
|
|
return (w >> 7 & 31) == 1 && ((w & 0x7F) == 0x6F || ((w & 0x7F) == 0x67 && !(w >> 12 & 7)));
|
|
}
|
|
|
|
/* Leave the monitor. mode: 0 run, 2 one instruction */
|
|
static void leave(u8 mode)
|
|
{
|
|
IO_BP0 = bp;
|
|
mon_ctl = mode;
|
|
}
|
|
|
|
static u8 reg_num(void)
|
|
{
|
|
u8 i, k;
|
|
if (cp[0] == 'X' && cp[1] >= '0' && cp[1] <= '9') {
|
|
k = cp[1] - '0';
|
|
cp += 2;
|
|
if (*cp >= '0' && *cp <= '9') k = k * 10 + (*cp++ - '0');
|
|
return k < 32 ? k : 0xFF;
|
|
}
|
|
if (!memcmp(cp, "FP", 2)) { cp += 2; return 8; }
|
|
for (i = 31; i; i--) { /* longest names first: S10 before S1 */
|
|
k = strlen(regn[i]);
|
|
if (!memcmp(cp, regn[i], k) && !(cp[k] >= 'A' && cp[k] <= 'Z') && !(cp[k] >= '0' && cp[k] <= '9')) {
|
|
cp += k;
|
|
return i;
|
|
}
|
|
}
|
|
return 0xFF;
|
|
}
|
|
|
|
static u8 set_reg(void)
|
|
{
|
|
u8 r;
|
|
u32 v;
|
|
skip_sp();
|
|
if (cp[0] == 'F' && ((cp[1] >= '0' && cp[1] <= '9') || !memcmp(cp, "FCSR", 4))) {
|
|
if (!u.fs) return 0; /* not saved: the FPU is off */
|
|
if (cp[1] == 'C') { cp += 4; r = 32; }
|
|
else {
|
|
r = *++cp - '0';
|
|
if (*++cp >= '0' && *cp <= '9') r = r * 10 + (*cp++ - '0');
|
|
if (r > 31) return 0;
|
|
}
|
|
skip_sp();
|
|
if (*cp == '=') cp++;
|
|
if (!get_hex(&v)) return 0;
|
|
if (r == 32) u.fcsr = v & 0xFF; else u.f[r] = v;
|
|
show_f = 1;
|
|
return 1;
|
|
}
|
|
if (cp[0] == 'P' && cp[1] == 'C') { cp += 2; r = 32; }
|
|
else if ((r = reg_num()) == 0xFF || r == 0) return 0;
|
|
skip_sp();
|
|
if (*cp == '=') cp++;
|
|
if (!get_hex(&v)) return 0;
|
|
if (r == 32) {
|
|
if (v & 3) return 0;
|
|
u.pc = v;
|
|
} else u.x[r] = v;
|
|
return 1;
|
|
}
|
|
|
|
/* :LLAAAATT data CC - an Intel HEX record, as tools/send-hex.py sends them.
|
|
Data records are written to memory; the end record reports the range. */
|
|
static u32 hx_lo = 0xFFFFFFFF, hx_hi, hx_base;
|
|
static char hx_msg[32];
|
|
static u8 hexv(char c) { return c <= '9' ? c - '0' : c - 'A' + 10; }
|
|
static u8 hex2(const char *s) { return (hexv(s[0]) << 4) | hexv(s[1]); }
|
|
static void hex_record(void)
|
|
{
|
|
const char *p = cp + 1;
|
|
u8 n, t, sum, i, len = 0;
|
|
u32 a;
|
|
while (is_hex(p[len])) len++;
|
|
if (p[len] || len < 10 || (len & 1) || len != 10 + 2 * hex2(p)) {
|
|
msg = "BAD HEX RECORD"; msg_err = 1; return;
|
|
}
|
|
n = hex2(p);
|
|
for (sum = 0, i = 0; i < n + 5; i++) sum += hex2(p + 2 * i);
|
|
if (sum) { msg = "HEX CHECKSUM ERROR"; msg_err = 1; return; }
|
|
a = (u32)hex2(p + 2) << 8 | hex2(p + 4);
|
|
t = hex2(p + 6);
|
|
if (t == 1) {
|
|
if (hx_lo > hx_hi) { msg = "NO HEX DATA"; msg_err = 1; return; }
|
|
lp = line; s_str("HEX LOADED "); s_hex(hx_lo, 5); s_ch('-'); s_hex(hx_hi, 5); *lp = 0;
|
|
strcpy(hx_msg, line);
|
|
msg = hx_msg;
|
|
mem_a = hx_lo & ~15u;
|
|
hx_lo = 0xFFFFFFFF; hx_hi = 0; hx_base = 0;
|
|
draw_mem();
|
|
return;
|
|
}
|
|
if (t == 4 && n == 2) { hx_base = (u32)hex2(p + 8) << 24 | (u32)hex2(p + 10) << 16; return; }
|
|
if (t == 2 && n == 2) { hx_base = ((u32)hex2(p + 8) << 8 | hex2(p + 10)) << 4; return; }
|
|
if (t != 0 || !n) return;
|
|
a += hx_base;
|
|
for (i = 0; i < n; i++)
|
|
if (!wr(a + i, hex2(p + 8 + 2 * i))) { msg = "ROM: NOT CHANGED"; msg_err = 1; return; }
|
|
if (a < hx_lo) hx_lo = a;
|
|
if (a + n - 1 > hx_hi) hx_hi = a + n - 1;
|
|
}
|
|
|
|
/* Returns 1 when the monitor should be left. */
|
|
static u8 command(void)
|
|
{
|
|
u32 v, a;
|
|
u8 c;
|
|
cp = cmd;
|
|
skip_sp();
|
|
msg = 0; msg_err = 0;
|
|
if (*cp == ':') { hex_record(); return 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])) {
|
|
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;
|
|
if (setjmp(fs_jmp)) { msg = fs_msg; msg_err = 1; return 0; }
|
|
if (c == 'S') { mem_save(); msg = "SAVED"; }
|
|
else if (c == 'L') {
|
|
mem_load();
|
|
lp = line; s_str("LOADED "); s_hex(ld_first, 5); s_ch('-'); s_hex(ld_last, 5); *lp = 0;
|
|
strcpy(load_msg, line);
|
|
msg = load_msg;
|
|
mem_a = ld_first & ~15u;
|
|
draw_mem(); draw_dis();
|
|
} else { mem_dir(); msg = "M TO SHOW MEMORY AGAIN"; }
|
|
return 0;
|
|
}
|
|
c = *cp;
|
|
if (c) cp++;
|
|
switch (c) {
|
|
case 0: /* Enter: one step */
|
|
case 'S':
|
|
steps = get_hex(&v) && v ? v - 1 : 0;
|
|
leave(2);
|
|
return 1;
|
|
case 'O': /* step over a call */
|
|
a = u.pc;
|
|
if (!op_is_call(a)) { leave(2); return 1; }
|
|
IO_BP1 = a + 4;
|
|
bp_ctl = (bp_set && bp != a ? 1 : 0) | 2;
|
|
leave(0);
|
|
return 1;
|
|
case 'G':
|
|
case 'X':
|
|
if (get_hex(&v)) {
|
|
if (v & 3) { msg = "ADDRESS NOT ON A WORD"; msg_err = 1; return 0; }
|
|
u.pc = v;
|
|
}
|
|
if (bp_set && u.pc == bp) { go_after = 1; leave(2); return 1; }
|
|
bp_ctl = bp_set;
|
|
leave(0);
|
|
return 1;
|
|
case 'B':
|
|
if (get_hex(&v)) { bp = v; bp_set = 1; msg = "BREAKPOINT SET"; }
|
|
else { bp_set = 0; msg = "BREAKPOINT CLEARED"; }
|
|
draw_dis();
|
|
return 0;
|
|
case 'D':
|
|
if (get_hex(&v)) dis_a = v & ~3u; else dis_a = u.pc;
|
|
draw_dis();
|
|
return 0;
|
|
case 'M':
|
|
if (get_hex(&v)) mem_a = v;
|
|
show_f = 0;
|
|
draw_mem();
|
|
return 0;
|
|
case 'E': /* E addr bytes... */
|
|
if (!get_hex(&a)) { msg = "E ADDR BYTE BYTE ..."; msg_err = 1; return 0; }
|
|
while (get_hex(&v))
|
|
if (!wr(a++, v)) { msg = "ROM: NOT CHANGED"; msg_err = 1; break; }
|
|
draw_all();
|
|
return 0;
|
|
case 'F':
|
|
show_f = 1;
|
|
draw_mem();
|
|
msg = "M TO SHOW MEMORY AGAIN";
|
|
return 0;
|
|
case 'R':
|
|
if (!set_reg()) { msg = "R PC|X1-X31|RA|SP|A0...|F0-F31|FCSR = VALUE"; msg_err = 1; return 0; }
|
|
draw_regs(); draw_dis(); draw_stack(); draw_mem();
|
|
return 0;
|
|
case '?':
|
|
case 'H':
|
|
msg = "ASM \"N\" A ADDR SAVE \"N\" A B LOAD \"N\" [A] DIR";
|
|
return 0;
|
|
default:
|
|
msg = "? TYPE ? FOR HELP"; msg_err = 1;
|
|
return 0;
|
|
}
|
|
}
|
|
|
|
void mon_main(void)
|
|
{
|
|
u8 why = mon_ctl, k;
|
|
|
|
bp_ctl = 0; /* no breakpoints inside the monitor */
|
|
if (u.how) { /* a trap: ebreak and ecall go on after themselves */
|
|
u32 w = rd32(u.pc);
|
|
if ((u.cause == 3 && w == 0x00100073) || (u.cause == 11 && w == 0x00000073)) u.pc += 4;
|
|
steps = 0;
|
|
go_after = 0;
|
|
} else if (go_after && why == 3) { /* stepped off the breakpoint: now go */
|
|
go_after = 0;
|
|
bp_ctl = bp_set;
|
|
leave(0);
|
|
return;
|
|
} else if (steps && why == 3) {
|
|
steps--;
|
|
leave(2);
|
|
return;
|
|
}
|
|
steps = 0;
|
|
go_after = 0;
|
|
if (!dis_shows(u.pc)) dis_a = u.pc;
|
|
|
|
puts_("\0337\033[?47h\033[?25l"); /* save the cursor, alternate screen */
|
|
why_in = why;
|
|
draw_all();
|
|
for (;;) {
|
|
k = read_cmd();
|
|
if (k == 1) {
|
|
if (command()) break;
|
|
continue;
|
|
}
|
|
if (k == 'U') dis_a -= 4;
|
|
else if (k == 'N') dis_a += 4;
|
|
else if (k == 'P') mem_a -= 16 * MEM_ROWS;
|
|
else if (k == 'Q') mem_a += 16 * MEM_ROWS;
|
|
if (k == 'U' || k == 'N') draw_dis(); else draw_mem();
|
|
}
|
|
puts_("\033[?25h\033[?47l\0338");
|
|
}
|
|
#endif
|