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:
1 parent
19278d1d74
commit
71fd403294
31 files changed
+9143
-7638
No files matched your search
+111
-3
@@ -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; }
|
||||
|
||||
Reference in new issue
Block a user