Add a single-precision FPU (the F extension)

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>
This commit is contained in:
brad ArantandClaude Opus 5.5 committed 2026-11-30 20:29:08 -08:00
1 parent 19278d1d74
commit 71fd403294
31 files changed
+9143 -7638

No files matched your search

+111 -3
View File
@@ -11,7 +11,12 @@
* Instructions: RV32I and M, CSRRW..CSRRCI, ECALL EBREAK MRET MNRET WFI FENCE
* FENCE.I, and the usual short forms: NOP LI LA MV NOT NEG SEQZ SNEZ SLTZ SGTZ
* J JR JAL target RET CALL BEQZ BNEZ BLEZ BGEZ BLTZ BGTZ BGT BLE BGTU BLEU CSRR
* CSRW CSRS CSRC CSRWI CSRSI CSRCI. LI is one instruction for a constant of
* CSRW CSRS CSRC CSRWI CSRSI CSRCI.
* F: FLW FSW FMADD.S FMSUB.S FNMSUB.S FNMADD.S FADD.S FSUB.S FMUL.S FDIV.S
* FSQRT.S FSGNJ.S FSGNJN.S FSGNJX.S FMIN.S FMAX.S FEQ.S FLT.S FLE.S FCVT.W.S
* FCVT.WU.S FCVT.S.W FCVT.S.WU FMV.X.W FMV.W.X FCLASS.S and FMV.S FNEG.S
* FABS.S; float registers F0-F31 or FT0-FT11 FS0-FS11 FA0-FA7; a rounding
* mode (RNE RTZ RDN RUP RMM DYN) may follow the operands, DYN if not. LI is one instruction for a constant of
* -2048..2047 known by then, else LUI and ADDI; LA is AUIPC and ADDI.
* Directives: ORG, EQU (NAME EQU value), DB/DEFB/DEFM (numbers and "strings"),
* DH (16 bits), DW/DEFW (32 bits), DS/DEFS n, ALIGN n, END. Instructions must
@@ -174,7 +179,7 @@ static u32 a_expr(void)
}
/* ------------------------------------------------------------- operands */
#define MAXOP 3
#define MAXOP 5
static char a_ops[MAXOP][40];
static u8 a_nops;
@@ -314,6 +319,96 @@ static u32 enc_u(u32 base, u8 rd, u32 imm)
static u32 hi20(u32 v) { return (v + 0x800) >> 12 & 0xFFFFF; }
static i32 lo12(u32 v) { return sx(v, 12); }
/* ----------------------------------------------------------------- F */
static void a_need(u8 n);
/* a float register name, the whole of s; 0xFF if it isn't one */
static u8 a_fregname(const char *s)
{
u8 k = 0, base, lim;
const char *d;
if (s[0] != 'F') return 0xFF;
if (s[1] >= '0' && s[1] <= '9') { d = s + 1; base = 0; lim = 32; }
else if (s[1] == 'T') { d = s + 2; base = 100; lim = 12; } /* FT0-7: 0-7, FT8-11: 28-31 */
else if (s[1] == 'S') { d = s + 2; base = 200; lim = 12; } /* FS0-1: 8-9, FS2-11: 18-27 */
else if (s[1] == 'A') { d = s + 2; base = 10; lim = 8; } /* FA0-7: 10-17 */
else return 0xFF;
if (*d < '0' || *d > '9') return 0xFF;
k = *d++ - '0';
if (*d >= '0' && *d <= '9') k = k * 10 + (*d++ - '0');
if (*d || k >= lim) return 0xFF;
if (base == 100) return k < 8 ? k : k + 20;
if (base == 200) return k < 2 ? k + 8 : k + 16;
return base + k;
}
static u8 a_freg(u8 i)
{
u8 r = i < a_nops ? a_fregname(a_ops[i]) : 0xFF;
if (r == 0xFF) a_fail("BAD FLOAT REGISTER");
return r & 31;
}
/* the rounding mode in operand i, DYN (7) if there is none */
static u32 a_rm(u8 i)
{
static const char *const m[8] = { "RNE", "RTZ", "RDN", "RUP", "RMM", "", "", "DYN" };
u8 k;
if (i >= a_nops) return 7;
for (k = 0; k < 8; k++) if (m[k][0] && !strcmp(a_ops[i], m[k])) return k;
a_fail("BAD ROUNDING MODE");
return 7;
}
enum { G_LD, G_ST, G_R4, G_R3RM, G_R2RM, G_R3, G_XFF, G_XFRM, G_FXRM, G_XF, G_FX, G_P2 };
struct fins { char name[10]; u8 f; u32 base; };
static const struct fins ftab[] = {
{ "FLW", G_LD, 0x00002007 }, { "FSW", G_ST, 0x00002027 },
{ "FMADD.S", G_R4, 0x43 }, { "FMSUB.S", G_R4, 0x47 }, { "FNMSUB.S", G_R4, 0x4B },
{ "FNMADD.S", G_R4, 0x4F },
{ "FADD.S", G_R3RM, 0x00000053 }, { "FSUB.S", G_R3RM, 0x08000053 },
{ "FMUL.S", G_R3RM, 0x10000053 }, { "FDIV.S", G_R3RM, 0x18000053 },
{ "FSQRT.S", G_R2RM, 0x58000053 },
{ "FSGNJ.S", G_R3, 0x20000053 }, { "FSGNJN.S", G_R3, 0x20001053 },
{ "FSGNJX.S", G_R3, 0x20002053 }, { "FMIN.S", G_R3, 0x28000053 }, { "FMAX.S", G_R3, 0x28001053 },
{ "FEQ.S", G_XFF, 0xA0002053 }, { "FLT.S", G_XFF, 0xA0001053 }, { "FLE.S", G_XFF, 0xA0000053 },
{ "FCVT.W.S", G_XFRM, 0xC0000053 }, { "FCVT.WU.S", G_XFRM, 0xC0100053 },
{ "FCVT.S.W", G_FXRM, 0xD0000053 }, { "FCVT.S.WU", G_FXRM, 0xD0100053 },
{ "FMV.X.W", G_XF, 0xE0000053 }, { "FMV.X.S", G_XF, 0xE0000053 },
{ "FCLASS.S", G_XF, 0xE0001053 },
{ "FMV.W.X", G_FX, 0xF0000053 }, { "FMV.S.X", G_FX, 0xF0000053 },
{ "FMV.S", G_P2, 0x20000053 }, { "FNEG.S", G_P2, 0x20001053 }, { "FABS.S", G_P2, 0x20002053 },
};
#define NFINS (sizeof ftab / sizeof ftab[0])
/* the operand count of a form with an optional rounding mode */
static void a_need2(u8 n) { if (a_nops != n && a_nops != n + 1) a_need(n); }
static void a_finstr(const struct fins *t)
{
u32 b = t->base;
i32 off;
u8 r, s;
if (a_pc & 3) { a_fail("NOT ON A WORD: USE ALIGN 4"); return; }
switch (t->f) {
case G_LD: a_need(2); r = a_freg(0); s = a_mem(1, &off); a_emit32(enc_i(b, r, s, off)); return;
case G_ST: a_need(2); r = a_freg(0); s = a_mem(1, &off); a_emit32(enc_s(b, s, r, off)); return;
case G_R4:
a_need2(4);
a_emit32(b | a_freg(0) << 7 | a_freg(1) << 15 | a_freg(2) << 20 | (u32)a_freg(3) << 27 |
a_rm(4) << 12);
return;
case G_R3RM:
a_need2(3);
a_emit32(b | a_freg(0) << 7 | a_freg(1) << 15 | a_freg(2) << 20 | a_rm(3) << 12);
return;
case G_R2RM: a_need2(2); a_emit32(b | a_freg(0) << 7 | a_freg(1) << 15 | a_rm(2) << 12); return;
case G_R3: a_need(3); a_emit32(b | a_freg(0) << 7 | a_freg(1) << 15 | a_freg(2) << 20); return;
case G_XFF: a_need(3); a_emit32(b | a_reg(0) << 7 | a_freg(1) << 15 | a_freg(2) << 20); return;
case G_XFRM: a_need2(2); a_emit32(b | a_reg(0) << 7 | a_freg(1) << 15 | a_rm(2) << 12); return;
case G_FXRM: a_need2(2); a_emit32(b | a_freg(0) << 7 | a_reg(1) << 15 | a_rm(2) << 12); return;
case G_XF: a_need(2); a_emit32(b | a_reg(0) << 7 | a_freg(1) << 15); return;
case G_FX: a_need(2); a_emit32(b | a_freg(0) << 7 | a_reg(1) << 15); return;
case G_P2: a_need(2); r = a_freg(1); a_emit32(b | a_freg(0) << 7 | r << 15 | r << 20); return;
}
}
/* --------------------------------------------------------- instructions */
enum {
F_R, F_I, F_SH, F_LD, F_ST, F_B, F_U, F_JAL, F_JALR, F_CSR, F_CSRI, F_FIX,
@@ -464,7 +559,8 @@ static u8 is_w(const char *w, const char *s) { return !strncmp(w, s, 8); }
/* Assemble one source line (uppercase except in quotes). */
static void a_line(char *s)
{
char w[8], lab[8];
char w[8], lab[8], fm[12];
const char *mn;
u8 has_lab = 0, q = 0;
u16 m;
char *c;
@@ -485,6 +581,7 @@ static void a_line(char *s)
a_sp();
if (!*ap) return;
if (!a_alpha(*ap)) { a_fail("SYNTAX ERROR"); return; }
mn = ap;
a_name(w);
a_sp();
if (*ap == ':') { /* LABEL: */
@@ -493,6 +590,7 @@ static void a_line(char *s)
a_sp();
if (!*ap) { sym_def(lab, a_pc); return; }
if (!a_alpha(*ap)) { a_fail("SYNTAX ERROR"); return; }
mn = ap;
a_name(w);
a_sp();
} else if (!strncmp(ap, "EQU", 3) && !a_alnum(ap[3])) {
@@ -520,6 +618,16 @@ static void a_line(char *s)
else while (a_pc & (n - 1)) a_emit(0);
return;
}
/* F instructions, whose names can be longer than 8 */
for (m = 0; m < 11 && a_alnum(mn[m]); m++) fm[m] = mn[m];
fm[m] = 0;
if (!a_alnum(mn[m]))
for (m = 0; m < NFINS; m++)
if (!strcmp(fm, ftab[m].name)) {
a_split();
if (!a_err) a_finstr(&ftab[m]);
return;
}
for (m = 0; m < NINS; m++)
if (!strncmp(w, itab[m].name, 8)) break;
if (m == NINS) { a_fail("UNKNOWN INSTRUCTION"); return; }
+2 -2
View File
@@ -5,12 +5,12 @@ set -e
cd "$(dirname "$0")"
source ../scripts/rv_env.sh
mkdir -p out
CFLAGS="$RV_ARCH -Os -mno-relax -ffunction-sections -fdata-sections -fno-common -Wall -I../include"
CFLAGS="$RV_ARCH -Os -ffunction-sections -fdata-sections -fno-common -Wall -I../include"
${RV_PREFIX}gcc $CFLAGS -c -o out/crt0.o crt0.S
${RV_PREFIX}gcc $CFLAGS -c -o out/monitor.o monitor.c
${RV_PREFIX}gcc $CFLAGS -c -o out/string.o ../lib/string.c
${RV_PREFIX}gcc $CFLAGS -c -o out/setjmp.o ../lib/setjmp.S
${RV_PREFIX}gcc $RV_ARCH -nostdlib -nostartfiles -T monitor.ld -Wl,--no-relax -Wl,--gc-sections \
${RV_PREFIX}gcc $RV_ARCH -nostdlib -nostartfiles -T monitor.ld -Wl,--gc-sections \
-Wl,--build-id=none -Wl,-Map,out/monitor.map -o out/monitor.elf \
out/crt0.o out/monitor.o out/string.o out/setjmp.o -lgcc
${RV_PREFIX}objcopy -O binary -j .text -j .data out/monitor.elf monitor.bin
+35 -2
View File
@@ -4,13 +4,20 @@
# instruction, a misaligned access, ebreak, ecall, an unhandled interrupt).
# The program's registers go to u (struct regs in monitor.c), the monitor runs,
# and mnret takes the program back to u.pc with its registers as u has them.
.equ U, 0x29F00 # struct regs, below RAM's last 256 bytes
# The float registers and fcsr are saved too, when mstatus.FS is on (Off: the
# F instructions would trap), and FS is put back as it was.
.option arch, +f
.option norelax
.equ U, 0x29E80 # struct regs, below RAM's last 384 bytes
.equ INITED, 0x29FFC # C data is set up once, then kept
.equ U_PC, 128
.equ U_HOW, 132 # 0 NMI, 1 trap
.equ U_CAUSE, 136
.equ U_TVAL, 140
.equ U_STATUS, 144
.equ U_F, 148 # f0-f31
.equ U_FCSR, 276
.equ U_FS, 280 # mstatus.FS on entry; 0: f not saved
.section .text.start, "ax"
.globl _start, u
@@ -56,6 +63,21 @@ trap_entry:
common:
li sp, U # the monitor's stack, below u
.option push
.option norelax
la gp, __global_pointer$ # the monitor's (the program's is in u)
.option pop
csrr t0, mstatus
srli t0, t0, 13
andi t0, t0, 3
sw t0, U_FS(sp)
beqz t0, 5f
.irp n, 0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31
fsw f\n, U_F+4*\n(sp)
.endr
csrr t0, fcsr
sw t0, U_FCSR(sp)
5:
li t0, INITED
lw t1, 0(t0)
li t2, 0xA5A5
@@ -79,7 +101,18 @@ common:
3: call mon_main
li sp, U
lw t0, U_PC(sp)
lw t0, U_FS(sp)
beqz t0, 6f
.irp n, 0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31
flw f\n, U_F+4*\n(sp)
.endr
lw t1, U_FCSR(sp)
csrw fcsr, t1
li t1, 0x6000 # FS as it was (loading f made it Dirty)
csrc mstatus, t1
slli t0, t0, 13
csrs mstatus, t0
6: lw t0, U_PC(sp)
csrw 0x741, t0 # mnepc
lw t0, U_HOW(sp)
beqz t0, 4f
Binary file not shown.
+132 -5
View File
@@ -27,12 +27,15 @@ typedef int32_t i32;
#define fl_ctl IO8(0x40)
#define fl_dat IO8(0x41)
/* saved by crt0.S at 29F00h */
/* 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;
@@ -101,6 +104,7 @@ static const struct csrn csrs[] = {
{ 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 }
};
@@ -123,6 +127,73 @@ static const char *const opn[8] = { "ADD", "SLL", "SLT", "SLTU", "XOR", "SRL", "
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)
{
@@ -199,6 +270,18 @@ static u8 disasm(u32 a)
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; }
@@ -340,6 +423,7 @@ 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 */
@@ -398,10 +482,33 @@ static void draw_stack(void)
}
}
/* 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(" ");
@@ -456,10 +563,9 @@ static void draw_all(void)
at(DIS_TOP - 1, 0); sgr("36"); puts_(" ADDR WORD INSTRUCTION"); sgr("0");
draw_dis();
draw_stack();
at(MEM_TOP - 1, 0); sgr("36"); puts_(" MEMORY"); sgr("0");
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 ? help");
puts_(" ENTER step O over G go X exit B bp D/M addr E edit R reg F fp ? help");
sgr("0");
}
@@ -927,6 +1033,21 @@ 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();
@@ -1052,6 +1173,7 @@ static u8 command(void)
return 0;
case 'M':
if (get_hex(&v)) mem_a = v;
show_f = 0;
draw_mem();
return 0;
case 'E': /* E addr bytes... */
@@ -1060,9 +1182,14 @@ static u8 command(void)
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... = VALUE"; msg_err = 1; return 0; }
draw_regs(); draw_dis(); draw_stack();
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':
+4 -1
View File
@@ -1,5 +1,5 @@
/* The monitor: ROM 20000-27FFF (writes ignored), RAM 28000-29FFF (data only)
with the saved registers at 29F00h (crt0.S) and the stack below them. */
with the saved registers at 29E80h (crt0.S) and the stack below them. */
OUTPUT_ARCH("riscv")
ENTRY(_start)
MEMORY
@@ -22,6 +22,9 @@ SECTIONS
__data_end = .;
} > RAM AT > ROM
__data_load = LOADADDR(.data);
/* the monitor's gp (set in crt0.S): its variables in one instruction */
/* (0xC00, not 0x800: before relaxing, the ROM overflows into 28000h) */
__global_pointer$ = ORIGIN(RAM) + 0xC00;
.bss (NOLOAD) : ALIGN(4) {
__bss_start = .;
*(.sbss .sbss.* .bss .bss.* COMMON)
+3413 -3413
View File
File diff suppressed because it is too large. Load diff
+28 -1
View File
@@ -68,6 +68,33 @@ DONE: ADDI A2,A2,-1
FENCE
FENCE.I
NOP
FLW FA0,8(SP)
FSW F31,-12(A0)
FADD.S FA0,FA1,FA2
FSUB.S F1,F2,F3,RTZ
FMUL.S FT0,FT11,FS0
FDIV.S FS11,FS1,FS2,RMM
FSQRT.S F4,F5,DYN
FMADD.S F1,F2,F3,F4
FNMSUB.S F1,F2,F3,F4,RUP
FSGNJX.S F6,F7,F8
FMV.S F9,F10
FNEG.S F9,F10
FABS.S F9,F10
FMIN.S F11,F12,F13
FMAX.S F11,F12,F13
FEQ.S A0,F1,F2
FLT.S A1,F1,F2
FLE.S A2,F1,F2
FCVT.W.S A3,F4,RTZ
FCVT.WU.S A4,F4
FCVT.S.W F5,A5
FCVT.S.WU F6,A6,RDN
FMV.X.W T0,F7
FMV.W.X F8,T1
FCLASS.S T2,F9
CSRR A0,FCSR
CSRW FRM,A1
RET
MSG: DB "HI",13,10,0
ALIGN 4
@@ -114,7 +141,7 @@ def main():
start, ours = ours.split()
tmp = tempfile.mkdtemp()
open(os.path.join(tmp, "t.s"), "w").write(to_gnu(SRC))
subprocess.run([pre + "as", "-march=rv32im_zicsr_zifencei", "-mno-relax", "-o", os.path.join(tmp, "t.o"),
subprocess.run([pre + "as", "-march=rv32imf_zicsr_zifencei", "-mno-relax", "-o", os.path.join(tmp, "t.o"),
os.path.join(tmp, "t.s")], check=True)
subprocess.run([pre + "ld", "-m", "elf32lriscv", "--no-relax", "-e", "0x12000", "-Ttext=0x12000", "-o", os.path.join(tmp, "t.elf"),
os.path.join(tmp, "t.o")], check=True)
+35 -1
View File
@@ -66,6 +66,27 @@ def words():
ws.append(0x13 | r() << 7 | 0 << 15 | imm << 20) # li
ws.append(0x13 | r() << 7 | r() << 15) # mv
ws.append(0x00008067) # ret
# F
rms = [0, 1, 2, 3, 4, 7]
for _ in range(N):
imm = random.randrange(4096)
ws.append(0x07 | 2 << 12 | r() << 7 | r() << 15 | imm << 20) # flw
ws.append(0x27 | 2 << 12 | (imm & 31) << 7 | r() << 15 | r() << 20 | (imm >> 5) << 25)
ws.append(random.choice([0x43, 0x47, 0x4B, 0x4F]) | random.choice(rms) << 12 | r() << 7 |
r() << 15 | r() << 20 | r() << 27)
f5, f3 = random.choice([(0, None), (1, None), (2, None), (3, None), (4, 0), (4, 1), (4, 2),
(5, 0), (5, 1), (0x14, 0), (0x14, 1), (0x14, 2)])
ws.append(0x53 | f5 << 27 | (random.choice(rms) if f3 is None else f3) << 12 | r() << 7 |
r() << 15 | r() << 20)
rr = r()
ws.append(0x53 | 4 << 27 | random.randrange(3) << 12 | r() << 7 | rr << 15 | rr << 20) # fmv.s ...
ws.append(0x53 | 0x0B << 27 | random.choice(rms) << 12 | r() << 7 | r() << 15) # fsqrt
ws.append(0x53 | random.choice([0x18, 0x1A]) << 27 | random.randrange(2) << 20 |
random.choice(rms) << 12 | r() << 7 | r() << 15) # fcvt
ws.append(0x53 | 0x1C << 27 | random.randrange(2) << 12 | r() << 7 | r() << 15) # fmv.x.w fclass
ws.append(0x53 | 0x1E << 27 | r() << 7 | r() << 15) # fmv.w.x
ws.append(0x73 | random.choice([1, 2, 3, 5, 6, 7]) << 12 | r() << 7 | r() << 15 |
random.choice([1, 2, 3]) << 20) # fcsr
ws += [0x13, 0x73, 0x00100073, 0x30200073, 0x70200073, 0x10500073, 0x0FF0000F, 0x100F]
return ws
@@ -95,7 +116,12 @@ CSRN = {"MSTATUS": 0x300, "MISA": 0x301, "MIE": 0x304, "MTVEC": 0x305, "MSCRATCH
"MNEPC": 0x741, "MNCAUSE": 0x742, "MNSTATUS": 0x744, "TSELECT": 0x7A0, "TDATA1": 0x7A1,
"TDATA2": 0x7A2, "TCONTROL": 0x7A5, "MCYCLE": 0xB00, "MINSTRET": 0xB02, "MCYCLEH": 0xB80,
"MINSTRETH": 0xB82, "CYCLE": 0xC00, "TIME": 0xC01, "INSTRET": 0xC02, "CYCLEH": 0xC80,
"TIMEH": 0xC81, "INSTRETH": 0xC82, "MHARTID": 0xF14}
"TIMEH": 0xC81, "INSTRETH": 0xC82, "MHARTID": 0xF14,
"FFLAGS": 1, "FRM": 2, "FCSR": 3}
# objdump's short forms for the float CSRs: read, write, write immediate
FCSR_ALIAS = {"FRCSR": ("FCSR", "R"), "FRRM": ("FRM", "R"), "FRFLAGS": ("FFLAGS", "R"),
"FSCSR": ("FCSR", ""), "FSRM": ("FRM", ""), "FSFLAGS": ("FFLAGS", ""),
"FSRMI": ("FRM", "I"), "FSFLAGSI": ("FFLAGS", "I")}
def same(gnu_mn, gnu_ops, our):
@@ -109,6 +135,14 @@ def same(gnu_mn, gnu_ops, our):
gnu_ops = ["RA"] + gnu_ops
elif gnu_mn.startswith("RD") and gnu_mn[2:] in CSRN:
gnu_mn, gnu_ops = "CSRR", gnu_ops + [gnu_mn[2:]]
elif gnu_mn in FCSR_ALIAS:
csr, kind = FCSR_ALIAS[gnu_mn]
if kind == "R":
gnu_mn, gnu_ops = "CSRR", gnu_ops + [csr]
elif len(gnu_ops) == 2:
gnu_mn, gnu_ops = "CSRRW" + kind, [gnu_ops[0], csr, gnu_ops[1]]
else:
gnu_mn, gnu_ops = "CSRW" + kind, [csr] + gnu_ops
gnu_ops = [str(CSRN[o]) if o in CSRN else o for o in gnu_ops]
ops = [str(CSRN[o]) if o in CSRN else o for o in ops]
alias = {"FENCE.I": "FENCE.I", "UNIMP": "DW"}