Monitor: wide 132x64 screen showing everything at once

On the 132x64 screen the monitor shows PC and all integer registers, all
float registers (hex and decimal) with FCSR decoded, mstatus/mepc/mcause/
mtval/cycle/instret and the breakpoint, 24 rows of disassembly beside the
stack, and 14 rows of memory, with changed registers highlighted; the 80x32
layout is unchanged. R takes float register names. The CPU's trap CSRs
start at 0, so reading them before the first trap no longer hangs the
simulated bus. send-hex accepts the prompt on any row.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
brad ArantandClaude Opus 5.5 committed 2026-12-02 00:00:43 -08:00
1 parent 1e44ce4383
commit addf0157fc
10 files changed
+3644 -3373

No files matched your search

+197 -49
View File
@@ -411,26 +411,41 @@ int main(int argc, char **argv)
/* ------------------------------------------------------------- screen */
static void puts_(const char *s) { while (*s) putc_(*s++); }
static void esc_n(u8 n) /* 1 to 3 digits */
{
if (n >= 100) putc_('0' + n / 100);
if (n >= 10) putc_('0' + n / 10 % 10);
putc_('0' + n % 10);
}
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);
putc_(27); putc_('[');
esc_n(r + 1); putc_(';'); esc_n(c + 1); putc_('H');
}
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
/* The screen's layout: 80x32 (narrow) or 132x64 (wide, everything at once),
chosen from port 0 bit 2 when the monitor is entered. */
#define REG_TOP 2 /* narrow: six rows of registers */
static u8 wide;
static u8 DIS_ROWS, MEM_ROWS, DIS_TOP, MEM_TOP, STK_COL, HELP_ROW, CMD_ROW, MSG_COL;
static void layout(void)
{
wide = con_stat & 4;
DIS_ROWS = wide ? 24 : 12;
MEM_ROWS = wide ? 14 : 6;
DIS_TOP = wide ? 22 : 9;
MEM_TOP = wide ? 48 : 22;
STK_COL = wide ? 66 : 54;
HELP_ROW = wide ? 62 : 28;
CMD_ROW = wide ? 63 : 29;
MSG_COL = wide ? 66 : 40;
}
static u32 px[32], pf[32]; /* the registers when last shown */
static u8 pv; /* ... saved */
static u32 dis_a; /* first line of the disassembly window */
static u32 mem_a; /* first line of the memory window */
@@ -454,9 +469,125 @@ static void s_rv(const char *n, u32 v)
s_str(n); s_ch(' '); s_h8(v); s_ch(' ');
}
/* a float's value in decimal, " 1.500000E+00", from its bits, without the
FPU (the monitor leaves it alone): 7 digits, the last may be 1 out */
static void s_fdec(u32 b)
{
u32 m = b & 0x7FFFFF, x;
i32 e = b >> 23 & 255, d = 0;
u8 k;
char g[11];
s_ch(b >> 31 ? '-' : ' ');
if (e == 255) { s_str(m ? "NAN" : "INF"); return; }
if (e) m |= 0x800000; else e = 1;
if (!m) { s_str("0"); return; }
x = m;
for (e -= 150; e > 0; e--) /* x * 2^e * 10^d, x kept large */
if (x < 0x80000000u) x <<= 1; else { x /= 10; d++; e++; }
for (; e < 0; e++)
if (x < 0x19999999u) { x *= 10; d--; e--; } else x >>= 1;
while (x >= 100000000u) { x = (x + (x >= 1000000000u ? 0 : 5)) / 10; d++; } /* 8 digits */
x = (x + 5) / 10; d++; /* 7, rounded */
if (x >= 10000000u) { x /= 10; d++; }
for (k = 0; k < 7; k++) { g[6 - k] = '0' + x % 10; x /= 10; }
d += 6;
s_ch(g[0]); s_ch('.');
for (k = 1; k < 7; k++) s_ch(g[k]);
s_ch('E'); s_ch(d < 0 ? '-' : '+');
if (d < 0) d = -d;
s_ch('0' + d / 10); s_ch('0' + d % 10);
}
static u32 csr_mepc(void) { u32 v; __asm__ volatile("csrr %0, mepc" : "=r"(v)); return v; }
static u32 csr_mstatus(void) { u32 v; __asm__ volatile("csrr %0, mstatus" : "=r"(v)); return v; }
static u32 csr_mcause(void) { u32 v; __asm__ volatile("csrr %0, mcause" : "=r"(v)); return v; }
static u32 csr_mtval(void) { u32 v; __asm__ volatile("csrr %0, mtval" : "=r"(v)); return v; }
static void s_cycle(void)
{
u32 h, l;
__asm__ volatile("csrr %0, cycleh; csrr %1, cycle" : "=r"(h), "=r"(l));
s_h8(h); s_h8(l);
}
static void s_instret(void)
{
u32 h, l;
__asm__ volatile("csrr %0, instreth; csrr %1, instret" : "=r"(h), "=r"(l));
s_h8(h); s_h8(l);
}
static const char *cause_text(u32 c);
/* the line built so far at r, c: highlighted when it changed */
static void w_put(u8 r, u8 c, u8 changed)
{
at(r, c);
if (changed) sgr("1;33");
flush_line();
if (changed) sgr("0");
}
static void w_head(u8 r, u8 c, const char *t) { at(r, c); sgr("36"); puts_(t); sgr("0"); }
/* 132x64: the integer registers, the float registers, the CSRs */
static void draw_wregs(void)
{
u8 k;
u32 v;
static const char fl[] = "NVDZOFUFNX";
w_head(2, 1, "REGISTERS");
for (k = 0; k < 33; k++) {
lp = line;
s_ch(' '); /* names of up to 4 letters (ZERO) */
s_rv(k ? regn[k - 1] : "PC", k ? u.x[k - 1] : u.pc);
w_put(3 + k / 4, 1 + (k & 3) * 15, k > 1 && pv && px[k - 1] != u.x[k - 1]);
}
w_head(2, 66, "FLOAT REGISTERS");
for (k = 0; k < 32; k++) {
char n[6];
lp = n; s_freg(k); *lp = 0;
if (!u.fs) { if (!k) { at(3, 66); puts_("THE FPU IS OFF (MSTATUS.FS = 0)"); } continue; }
lp = line;
if (n[3] == 0) s_ch(' '); /* FT0 and FT10 in line */
s_rv(n, u.f[k]);
s_fdec(u.f[k]);
w_put(3 + (k & 15), 66 + (k >> 4) * 33, pv && pf[k] != u.f[k]);
}
if (u.fs) {
lp = line;
s_str("FCSR "); s_h8(u.fcsr); s_str(" ROUNDING "); s_str(rmn[u.fcsr >> 5 & 7] ? rmn[u.fcsr >> 5 & 7] : "?");
s_str(" FLAGS");
for (k = 0; k < 5; k++) {
u8 on = u.fcsr >> (4 - k) & 1;
s_ch(' '); s_ch(on ? fl[2 * k] : '-'); s_ch(on ? fl[2 * k + 1] : '-');
}
at(19, 66); flush_line();
}
w_head(13, 1, "CONTROL AND STATUS");
lp = line;
/* u.status is saved only for a trap; an NMI leaves mstatus as it was */
s_str("MSTATUS "); s_h8(u.how ? u.status : csr_mstatus()); s_str(" MEPC "); s_h8(csr_mepc());
s_str(" MTVAL "); s_h8(csr_mtval());
at(14, 1); flush_line();
v = csr_mcause();
{
const char *ct = cause_text(v);
lp = line;
s_str("MCAUSE "); s_h8(v); s_ch(' '); s_str(ct);
}
while (lp < line + 58) s_ch(' ');
at(15, 1); flush_line();
lp = line;
s_str("CYCLE "); s_cycle(); s_str(" INSTRET "); s_instret();
at(16, 1); flush_line();
lp = line;
s_str("BREAKPOINT ");
if (bp_set) s_hex(bp, 5); else s_str("NONE ");
at(17, 1); flush_line();
}
static void draw_regs(void)
{
u8 r, i;
if (wide) { draw_wregs(); return; }
for (r = 0; r < 6; r++) {
lp = line;
for (i = 0; i < 6; i++) {
@@ -479,7 +610,7 @@ static void draw_dis(void)
s_hex(a, 5); s_str(" ");
s_h8(rd32(a)); s_str(" ");
disasm(a);
while (lp < line + 50) s_ch(' ');
while (lp < line + STK_COL - 4) s_ch(' ');
at(DIS_TOP + r, 0);
if (a == pc) sgr("1;33;44");
flush_line();
@@ -492,11 +623,11 @@ static void draw_stack(void)
{
u32 s = u.x[2];
u8 r;
at(DIS_TOP - 1, 54); sgr("36"); puts_("STACK"); sgr("0");
at(DIS_TOP - 1, STK_COL); 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();
at(DIS_TOP + r, STK_COL); flush_line(); clr_eol();
s += 4;
}
}
@@ -526,7 +657,7 @@ static void draw_mem(void)
{
u32 a = mem_a;
u8 r, j, c;
if (show_f) { draw_fregs(); return; }
if (show_f && !wide) { 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;
@@ -539,22 +670,27 @@ static void draw_mem(void)
}
}
/* an mcause value in words (in t, so not while line is being built) */
static const char *cause_text(u32 c) /* uses lp: call before building a line */
{
static char t[16];
const char *r = t;
if (c >> 31) { lp = t; s_str("INTERRUPT "); s_udec(c & 31); *lp = 0; }
else switch (c) {
case 0: r = "MISALIGNED JUMP"; break;
case 2: r = "ILLEGAL INSTRUCTION"; break;
case 3: r = "EBREAK"; break;
case 4: r = "MISALIGNED LOAD"; break;
case 6: r = "MISALIGNED STORE"; break;
case 11: r = "ECALL"; break;
default: lp = t; s_str("TRAP "); s_udec(c); *lp = 0;
}
return r;
}
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;
}
}
if (u.how) return cause_text(u.cause);
switch (why_in) {
case 1: return "PAUSE KEY";
case 2: return "MONITOR COMMAND";
@@ -567,23 +703,28 @@ static const char *why_text(void)
static void draw_all(void)
{
u8 w = wide ? 132 : 80, n;
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(' ');
{
const char *wt = why_text();
lp = line;
s_str(" RISC-V MONITOR");
while (lp < line + (wide ? 66 : 50)) s_ch(' ');
s_str("STOPPED BY ");
s_str(wt);
}
*lp = 0;
line[80] = 0;
puts_(line); sgr("0");
n = lp - line;
puts_(line);
while (n++ < w) putc_(' ');
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");
at(HELP_ROW, 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");
}
@@ -609,7 +750,7 @@ static u8 get_hex(u32 *v)
static void show_msg(void)
{
at(29, 40); clr_eol();
at(CMD_ROW, MSG_COL); clr_eol();
if (msg) { sgr(msg_err ? "1;31" : "32"); puts_(msg); sgr("0"); }
}
@@ -619,9 +760,9 @@ static const char *prompt = "> ";
static u8 read_cmd(void)
{
u8 n = 0, c;
at(29, 0); puts_(prompt); clr_eol();
at(CMD_ROW, 0); puts_(prompt); clr_eol();
show_msg();
at(29, strlen(prompt));
at(CMD_ROW, strlen(prompt));
puts_("\033[?25h");
for (;;) {
c = getk();
@@ -1053,14 +1194,16 @@ 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))) {
{ /* F0-F31, FT0, FA0 ..., FCSR */
char n[6];
u8 k = 0;
while (k < 5 && cp[k] && cp[k] != '=' && cp[k] != ' ') { n[k] = cp[k]; k++; }
n[k] = 0;
r = !strcmp(n, "FCSR") ? 32 : a_fregname(n);
}
if (r != 0xFF) {
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;
}
while (*cp && *cp != '=' && *cp != ' ') cp++;
skip_sp();
if (*cp == '=') cp++;
if (!get_hex(&v)) return 0;
@@ -1203,6 +1346,7 @@ static u8 command(void)
draw_all();
return 0;
case 'F':
if (wide) { msg = "THE FLOAT REGISTERS ARE ALWAYS SHOWN"; return 0; }
show_f = 1;
draw_mem();
msg = "M TO SHOW MEMORY AGAIN";
@@ -1243,6 +1387,7 @@ void mon_main(void)
}
steps = 0;
go_after = 0;
layout();
if (!dis_shows(u.pc)) dis_a = u.pc;
puts_("\0337\033[?47h\033[?25l"); /* save the cursor, alternate screen */
@@ -1260,6 +1405,9 @@ void mon_main(void)
else if (k == 'Q') mem_a += 16 * MEM_ROWS;
if (k == 'U' || k == 'N') draw_dis(); else draw_mem();
}
memcpy(px, u.x, sizeof px); /* to show what the program changes */
memcpy(pf, u.f, sizeof pf);
pv = 1;
puts_("\033[?25h\033[?47l\0338");
}
#endif