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

+106 -5
View File
@@ -1,8 +1,9 @@
#!/usr/bin/env python3
"""RV32IM emulator of the Basys 3 RISC-V computer, for testing programs on the
"""RV32IMF emulator of the Basys 3 RISC-V computer, for testing programs on the
PC and as a reference for rtl/rv32_cpu.v.
The same machine as rtl/rv32_cpu.v and rtl/rv32_system.v: RV32IM, machine
The same machine as rtl/rv32_cpu.v and rtl/rv32_system.v: RV32IMF (floating
point from tools/f32.py, bit for bit as rtl/rv32_fpu.v), machine
mode, the CSRs the core has (no triggers), the resumable NMI, 80 KB of RAM
(writes below rom_top ignored), the monitor's 32 KB ROM at 20000h and 8 KB of
RAM at 28000h, the I/O ports at FFFFFF00h + N (io_in / io_out, to be
@@ -15,7 +16,10 @@ runs a raw image from address 0 with the console on ports 0/1: text from
each instruction run. The emulator also counts the clocks rv32_cpu would take
(cycles; see cost()).
"""
import os
import sys
sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))
import f32 # noqa: E402
M32 = 0xFFFFFFFF
RAM_END = 0x14000
@@ -43,6 +47,10 @@ class RV32:
self.m[:len(mem)] = mem
self.rom_top = rom_top # writes below are ignored
self.x = [0] * 32
self.f = [0] * 32 # float registers (bit patterns)
self.fs = 1 # mstatus.FS: Initial
self.fflags = 0
self.frm = 0
self.pc = 0
self.cache = {}
self.csr = {0x300: 0, 0x304: 0, 0x305: 0, 0x340: 0, 0x341: 0, 0x342: 0, 0x343: 0,
@@ -172,9 +180,13 @@ class RV32:
# ------------------------------------------------------------------ CSRs
def csr_read(self, n):
if n == 0x300:
return self.csr[0x300] | 0x1800
return self.csr[0x300] | 0x1800 | self.fs << 13 | (0x80000000 if self.fs == 3 else 0)
if n == 0x301:
return 0x40001100
return 0x40001120
if n in (1, 2, 3):
if not self.fs:
raise Trap(2)
return [0, self.fflags, self.frm, self.frm << 5 | self.fflags][n]
if n == 0x344:
return 0
if n == 0x744:
@@ -194,6 +206,17 @@ class RV32:
raise Trap(2)
if n == 0x300:
self.csr[0x300] = v & 0x88
self.fs = v >> 13 & 3
elif n in (1, 2, 3):
if not self.fs:
raise Trap(2)
if n == 1:
self.fflags = v & 31
elif n == 2:
self.frm = v & 7
else:
self.fflags, self.frm = v & 31, v >> 5 & 7
self.fs = 3
elif n == 0x304:
self.csr[0x304] = v & 0xFFFF0888
elif n in (0x305, 0x341, 0x741):
@@ -367,6 +390,8 @@ class RV32:
x[rd] = op(x[rs1], x[rs2])
return nxt
return f
if opc in (0x07, 0x27, 0x43, 0x47, 0x4B, 0x4F, 0x53):
return self.decode_f(ins, pc)
if opc == 0x0F and f3 in (0, 1): # fence, fence.i
return lambda: nxt
if opc == 0x73:
@@ -408,6 +433,74 @@ class RV32:
return f
return illegal
def decode_f(self, ins, pc):
"""The F extension: as rv32_cpu decodes it (illegal while mstatus.FS
is Off, or with a reserved rounding mode)."""
opc, rd, f3 = ins & 0x7F, ins >> 7 & 31, ins >> 12 & 7
rs1, rs2, rs3 = ins >> 15 & 31, ins >> 20 & 31, ins >> 27
x, fr = self.x, self.f
nxt = (pc + 4) & M32
ill = Trap(2, ins)
def illegal():
raise ill
if opc == 0x07 and f3 == 2: # flw
immi = sx(ins >> 20, 12)
def f():
if not self.fs:
raise ill
a = (x[rs1] + immi) & M32
if a & 3:
raise Trap(4, a)
fr[rd] = self.rd32(a)
self.fs = 3
return nxt
return f
if opc == 0x27 and f3 == 2: # fsw
imms = sx((ins >> 25) << 5 | (ins >> 7 & 31), 12)
def f():
if not self.fs:
raise ill
a = (x[rs1] + imms) & M32
if a & 3:
raise Trap(6, a)
self.wr32(a, fr[rs2])
return nxt
return f
if opc in (0x07, 0x27) or (ins >> 25) & 3:
return illegal
f5 = ins >> 27
uses_rm = opc != 0x53 or f5 in (0, 1, 2, 3, 0x0B, 0x18, 0x1A)
if opc == 0x53:
ok = (f5 <= 3 or (f5 == 0x0B and rs2 == 0) or (f5 == 4 and f3 <= 2) or
(f5 == 5 and f3 <= 1) or (f5 == 0x14 and f3 <= 2) or
(f5 in (0x18, 0x1A) and rs2 <= 1) or (f5 == 0x1C and rs2 == 0 and f3 <= 1) or
(f5 == 0x1E and rs2 == 0 and f3 == 0))
if not ok:
return illegal
to_int = opc == 0x53 and f5 in (0x18, 0x1C, 0x14)
from_int = opc == 0x53 and f5 in (0x1A, 0x1E)
def f():
if not self.fs:
raise ill
rm = self.frm if f3 == 7 else f3
if uses_rm and rm > 4:
raise ill
a = x[rs1] if from_int else fr[rs1]
r, fl, _ = f32.execute(ins, rm, a, fr[rs2], fr[rs3])
self.fflags |= fl
self.fs = 3
if to_int:
if rd:
x[rd] = r
else:
fr[rd] = r
return nxt
return f
# ------------------------------------------------------------------ run
def trap(self, cause, tval):
self.csr[0x341] = self.pc
@@ -452,9 +545,17 @@ class RV32:
def cost(self, pc):
"""Clocks rv32_cpu takes for the instruction at pc, just run: 2, or 3
for a load, store, jump or taken branch (4 for I/O), 4 for a multiply,
35 for a divide."""
35 for a divide; floating point as listed in rv32_cpu.v (the shortcut
for infinite, NaN and zero operands isn't counted)."""
ins = self.rd32(pc)
op = ins & 0x7F
if op in (0x07, 0x27):
return 3
if op in (0x43, 0x47, 0x4B, 0x4F):
return 12
if op == 0x53:
f5 = ins >> 27
return {0: 12, 1: 12, 2: 12, 3: 36, 0x0B: 36, 0x18: 5, 0x1A: 8}.get(f5, 4)
if op in (0x03, 0x23):
a = (self.x[ins >> 15 & 31] + sx(ins >> 20 if op == 3 else (ins >> 25) << 5 | (ins >> 7 & 31), 12)) & M32
return 4 if a >= 0xFFFFFE00 or MON_RAM <= a < MON_END else 3