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

+27 -13
View File
@@ -360,8 +360,9 @@ MONITOR
```
`CALL` runs machine code at *address* as a subroutine, called the standard
RISC-V way: it must end with `RET` and leave `SP`, `GP`, `TP` and `S0`–`S11` as
it found them (`A0`–`A7` and `T0`–`T6` are free to use). `MONITOR` stops BASIC in the
RISC-V way: it must end with `RET` and leave `SP`, `GP`, `TP`, `S0`–`S11` and
the float registers `FS0`–`FS11` as it found them (`A0`–`A7`, `T0`–`T6` and the
other float registers are free to use). `MONITOR` stops BASIC in the
machine-code monitor (section 10). See section 9 for memory and ports.
### Graphics and text placement
@@ -763,18 +764,19 @@ each voice, so a note or a bang fades away by itself.
### Speed
The processor is a 32-bit RISC-V (RV32IM: the base instructions plus
multiply and divide) running at 90 MHz. Most instructions take 2 clocks; loads,
stores, jumps and taken branches 3, a multiply 4 and a divide 35, which comes to
about 2.5 clocks per instruction in BASIC, some 36 million instructions a
second. BASIC does its floating-point arithmetic in software, so in round
numbers:
The processor is a 32-bit RISC-V (RV32IMF: the base instructions, multiply
and divide, and single-precision floating point) running at 90 MHz. Most
instructions take 2 clocks; loads, stores, jumps and taken branches 3, a
multiply 4 and a divide 35, which comes to about 2.5 clocks per instruction in
BASIC, some 36 million instructions a second. Its floating-point unit adds,
subtracts and multiplies in 12 clocks and divides or takes a square root in 36,
correctly rounded. In round numbers:
| Operation | Time |
|---|---|
| One pass of an empty `FOR … NEXT` loop | 9 µs (about 100,000 a second) |
| `X = I * 1.5` | 43 µs |
| `Y = SIN(X)` | 85 µs |
| One pass of an empty `FOR … NEXT` loop | 8 µs (about 120,000 a second) |
| `X = I * 1.5` | 26 µs |
| `Y = SIN(X)` | 37 µs |
| `LINE` right across the screen | 0.2 ms |
Constants in a program are converted from text every time they are used, so a
@@ -837,7 +839,8 @@ Type a command on the bottom line and press Enter. Numbers are in hex.
| `D addr` | Show the disassembly from *addr* (`D` alone: from PC) |
| `M addr` | Show memory from *addr* |
| `E addr bytes…` | Write bytes into memory: `E 12000 13 05 10 04` |
| `R reg=value` | Set PC or a register, by name or number: `R A0=1234`, `R X10=1234` |
| `R reg=value` | Set PC or a register, by name or number: `R A0=1234`, `R X10=1234`; a float register as its bit pattern: `R F1=3FC00000` (1.5), or `R FCSR=0` |
| `F` | Show the float registers F0–F31 and FCSR, as bit patterns, in the memory window (`M` brings the memory back) |
| `SAVE "NAME" start end` | Save memory *start* to *end* (inclusive) to a file in flash |
| `LOAD "NAME"` | Load a memory file back to where it was saved from |
| `LOAD "NAME" addr` | Load it at *addr* instead (0A000h or above) |
@@ -856,7 +859,9 @@ Type a command on the bottom line and press Enter. Numbers are in hex.
itself, the monitor first steps past it.
- **Memory below 0A000h** is BASIC's ROM and can be viewed but not changed. `E`
says so if you try.
- **The disassembler** shows every RV32IM instruction and the CSR and system
- **The float registers** are saved and put back like the others, while the
FPU is on (`mstatus.FS`, which BASIC turns on); `F` says when it is off.
- **The disassembler** shows every RV32IMF instruction and the CSR and system
instructions, using the usual short forms where they apply (`LI`, `MV`, `J`,
`RET`, `BEQZ`, `CSRR` and so on), with hex numbers like `1F0H` and `0A200H`;
anything else is shown as `DW` and its value.
@@ -939,6 +944,15 @@ address is offered. An empty line ends. Labels from the last `ASM` can be used.
*target* (saving RA), `JALR` *register*, `RET`, `CALL`, `BEQZ`, `BNEZ`,
`BLEZ`, `BGEZ`, `BLTZ`, `BGTZ`, `BGT`, `BLE`, `BGTU`, `BLEU`, `CSRR`, `CSRW`,
`CSRS`, `CSRC`, `CSRWI`, `CSRSI`, `CSRCI`.
- **Floating point:** `FLW`, `FSW`, `FADD.S`, `FSUB.S`, `FMUL.S`, `FDIV.S`,
`FSQRT.S`, `FMADD.S`, `FMSUB.S`, `FNMSUB.S`, `FNMADD.S`, `FMIN.S`, `FMAX.S`,
`FSGNJ.S`, `FSGNJN.S`, `FSGNJX.S` (and `FMV.S`, `FNEG.S`, `FABS.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`. Float registers are `F0`–`F31` or `FT0`–`FT11`,
`FS0`–`FS11`, `FA0`–`FA7`; a rounding mode (`RNE`, `RTZ`, `RDN`, `RUP`, `RMM`)
may follow the operands, `FADD.S FA0,FA1,FA2,RTZ`, and without one the
instruction uses FCSR's (round to nearest unless changed). CSRs `FFLAGS`,
`FRM` and `FCSR`.
- **`LI`** is one instruction (`ADDI`) for a value from −2048 to 2047 that is
known by then, otherwise two (`LUI`, `ADDI`); **`LA`** is always two
(`AUIPC`, `ADDI`); **`CALL`** is `JAL RA`, which reaches 1 MB either way.