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 -18
View File
@@ -1,12 +1,16 @@
`timescale 1ns / 1ps
// RV32IM processor, machine mode only.
// RV32IMF processor, machine mode only.
//
// A multi-cycle core with one instruction in flight. Each instruction is
// decoded in one cycle (D) and executed in the next (E); the decode cycle
// fetches the following word, so straight-line code takes 2 clocks per
// instruction. Taken branches and jumps take 3, loads and stores 3 (more if
// the bus waits), multiplies 4 and divides 35.
// the bus waits), multiplies 4 and divides 35. Floating-point operations run
// in rv32_fpu.v: 4 clocks for moves, sign injection, compares, min/max and
// class, 5 for float -> int, 8 for int -> float, 12 for add, subtract,
// multiply and the multiply-adds (4 if an operand is infinite, NaN or zero),
// 36 for divide and square root; float loads and stores as integer ones.
//
// Instruction port: synchronous read. i_rdata is the word at the i_addr given
// in the last cycle with i_en high, and must hold while i_en is low.
@@ -28,7 +32,9 @@
// Speed: 90 MHz with 1.5 ns to spare on the XC7A35T-1 (bench/), about 2100
// LUTs, 900 flip-flops and 4 DSP slices.
//
// Implemented: RV32I, M, Zicsr, Zicntr (cycle, time = cycle, instret), Zifencei,
// Implemented: RV32I, M, F (rv32_fpu.v; fflags, frm, fcsr; mstatus.FS, which
// resets to Initial: F instructions and CSRs are illegal while it is Off,
// and any F instruction or F CSR write makes it Dirty), Zicsr, Zicntr (cycle, time = cycle, instret), Zifencei,
// mret and wfi (a no-op). Machine-mode CSRs: mstatus (MIE, MPIE; MPP reads 3),
// misa, mie, mip, mtvec (direct mode), mscratch, mepc, mcause, mtval, mcycle,
// minstret, mhartid etc. (read 0), and two Sdtrig match triggers (tselect,
@@ -74,9 +80,9 @@ module rv32_cpu #(
output dbg_decode, // high in an instruction's first cycle
output reg retire // pulse: an instruction completed
);
localparam S_IF = 3'd0, S_D = 3'd1, S_E = 3'd2, S_MEM = 3'd3,
S_MUL = 3'd4, S_DIV = 3'd5, S_DIVF = 3'd6, S_XT = 3'd7;
reg [2:0] state;
localparam S_IF = 4'd0, S_D = 4'd1, S_E = 4'd2, S_MEM = 4'd3,
S_MUL = 4'd4, S_DIV = 4'd5, S_DIVF = 4'd6, S_XT = 4'd7, S_FP = 4'd8;
reg [3:0] state;
reg [31:0] pc;
assign i_en = state == S_IF || state == S_D;
@@ -94,6 +100,15 @@ module rv32_cpu #(
reg [31:0] rf_wd;
always @(posedge clk) if (rf_we && rf_wa != 5'd0) rf[rf_wa] <= rf_wd;
// Floating-point registers: distributed RAM, three read ports.
reg [31:0] frf [0:31];
initial for (k = 0; k < 32; k = k + 1) frf[k] = 32'd0;
reg frf_we;
reg [31:0] frf_wd;
reg [1:0] fs; // mstatus.FS
reg [4:0] fflags;
reg [2:0] frm;
// ------------------------------------------------------------------
// Decode (state D): inst is the fetched word.
wire [31:0] inst = i_rdata;
@@ -103,6 +118,8 @@ module rv32_cpu #(
wire [4:0] rs1 = inst[19:15], rs2 = inst[24:20];
wire [31:0] rv1 = rs1 == 5'd0 ? 32'd0 : rf[rs1];
wire [31:0] rv2 = rs2 == 5'd0 ? 32'd0 : rf[rs2];
wire [4:0] rs3 = inst[31:27];
wire [31:0] fv1 = frf[rs1], fv2 = frf[rs2], fv3 = frf[rs3];
wire [31:0] imm_i = {{20{inst[31]}}, inst[31:20]};
wire [31:0] imm_s = {{20{inst[31]}}, inst[31:25], inst[11:7]};
@@ -133,9 +150,31 @@ module rv32_cpu #(
wire is_mret = inst == 32'h3020_0073;
wire is_wfi = inst == 32'h1050_0073;
wire is_mnret = inst == 32'h7020_0073;
// F: loads, stores, the multiply-adds and OP-FP
wire fp_on = fs != 2'b00;
wire [2:0] rm_d = f3 == 3'd7 ? frm : f3;
wire rm_ok = rm_d <= 3'd4;
wire [4:0] f5 = inst[31:27];
wire is_flw = opc == 7'b0000111 && f3 == 3'd2;
wire is_fsw = opc == 7'b0100111 && f3 == 3'd2;
wire is_fr4 = opc[6:4] == 3'b100 && opc[1:0] == 2'b11 && inst[26:25] == 2'b00 && rm_ok;
wire is_fop = opc == 7'b1010011 && inst[26:25] == 2'b00 &&
((f5 <= 5'h03 && rm_ok) || // add sub mul div
(f5 == 5'h0B && rs2 == 5'd0 && rm_ok) || // sqrt
(f5 == 5'h04 && f3 <= 3'd2) || // sgnj
(f5 == 5'h05 && f3 <= 3'd1) || // min max
(f5 == 5'h14 && f3 <= 3'd2) || // le lt eq
(f5 == 5'h18 && rs2[4:1] == 4'd0 && rm_ok) || // fcvt.w[u].s
(f5 == 5'h1A && rs2[4:1] == 4'd0 && rm_ok) || // fcvt.s.w[u]
(f5 == 5'h1C && rs2 == 5'd0 && f3 <= 3'd1) || // fmv.x.w fclass
(f5 == 5'h1E && rs2 == 5'd0 && f3 == 3'd0)); // fmv.w.x
wire fp_toint = opc == 7'b1010011 && (f5 == 5'h18 || f5 == 5'h1C || f5 == 5'h14);
wire fp_frint = opc == 7'b1010011 && (f5 == 5'h1A || f5 == 5'h1E);
wire is_fp = fp_on && (is_fr4 || is_fop);
wire legal = is_lui | is_auipc | is_jal | is_jalr | is_br | is_ld | is_st |
is_opi | is_op | is_mdv | is_fence | is_fencei | is_csr |
is_ecall | is_ebrk | is_mret | is_wfi | is_mnret;
is_ecall | is_ebrk | is_mret | is_wfi | is_mnret |
(fp_on && (is_flw || is_fsw)) | is_fp;
// ------------------------------------------------------------------
// Execute-stage registers, loaded at the end of D.
@@ -148,7 +187,10 @@ module rv32_cpu #(
reg x_trap; // the deferred trap, below
reg [31:0] x_cause, x_val;
reg e_alu, e_sub, e_jal, e_jalr, e_br, e_ld, e_st, e_mul, e_div,
e_csr, e_ecall, e_ebrk, e_mret, e_mnret, e_fencei, e_ill;
e_csr, e_ecall, e_ebrk, e_mret, e_mnret, e_fencei, e_ill,
e_fp, e_fpint, e_fd; // F operation, its result to x, a load to f
reg [31:0] fa, fb, fc; // F operands
reg [2:0] e_rm;
wire [2:0] e_f3 = ir[14:12];
wire [4:0] e_rd = ir[11:7];
reg [2:0] alu_fn;
@@ -221,8 +263,11 @@ module rv32_cpu #(
always @* begin
csr_ok = 1'b1;
case (csr_a)
12'h300: csr_rd = {19'd0, 2'b11, 3'd0, mpie_b, 3'd0, mie_b, 3'd0};
12'h301: csr_rd = 32'h4000_1100; // RV32 I M
12'h001: begin csr_rd = {27'd0, fflags}; csr_ok = fp_on; end
12'h002: begin csr_rd = {29'd0, frm}; csr_ok = fp_on; end
12'h003: begin csr_rd = {24'd0, frm, fflags}; csr_ok = fp_on; end
12'h300: csr_rd = {fs == 2'b11, 16'd0, fs, 2'b11, 3'd0, mpie_b, 3'd0, mie_b, 3'd0};
12'h301: csr_rd = 32'h4000_1120; // RV32 I M F
12'h304: csr_rd = mie_r;
12'h305: csr_rd = mtvec;
12'h310: csr_rd = 32'd0; // mstatush
@@ -346,16 +391,34 @@ module rv32_cpu #(
end
end
// floating point
wire fpu_done;
wire [31:0] fpu_res;
wire [4:0] fpu_flags;
rv32_fpu fpu (
.clk(clk), .rst(rst), .start(state == S_E && e_fp && !trap),
.ins(ir), .rm(e_rm), .a(fa), .b(fb), .c(fc),
.done(fpu_done), .result(fpu_res), .flags(fpu_flags)
);
// ------------------------------------------------------------------
// Register write-back
always @* begin
rf_we = 1'b0; rf_wa = e_rd; rf_wd = alu_out;
frf_we = 1'b0; frf_wd = fpu_res;
case (state)
S_E: begin
rf_we = !trap && (e_alu || e_jal || e_jalr || e_csr);
rf_wd = e_csr ? csr_rd : (e_jal || e_jalr) ? pc4 : alu_out;
end
S_MEM: begin rf_we = d_ack && e_ld; rf_wd = ld_val; end
S_MEM: begin
rf_we = d_ack && e_ld && !e_fd; rf_wd = ld_val;
frf_we = d_ack && e_fd; frf_wd = d_rdata;
end
S_FP: begin
rf_we = fpu_done && e_fpint; rf_wd = fpu_res;
frf_we = fpu_done && !e_fpint;
end
S_MUL: begin rf_we = m_cnt; rf_wd = m_hi ? m_p2[63:32] : m_p2[31:0]; end
S_DIVF: begin
rf_we = 1'b1;
@@ -368,9 +431,12 @@ module rv32_cpu #(
endcase
end
always @(posedge clk) if (frf_we) frf[e_rd] <= frf_wd;
// an instruction completes (counted by instret unless it writes instret)
reg count_i;
wire done = (state == S_E && !trap && !e_ld && !e_st && !e_mul && !e_div) ||
wire done = (state == S_E && !trap && !e_ld && !e_st && !e_mul && !e_div && !e_fp) ||
(state == S_FP && fpu_done) ||
(state == S_MEM && d_ack) || (state == S_MUL && m_cnt) ||
state == S_DIVF || (state == S_XT && !x_trap);
@@ -387,7 +453,10 @@ module rv32_cpu #(
if (csr_go) begin
case (csr_a)
12'h300: begin mie_b <= csr_wv[3]; mpie_b <= csr_wv[7]; end
12'h001: begin fflags <= csr_wv[4:0]; fs <= 2'b11; end
12'h002: begin frm <= csr_wv[2:0]; fs <= 2'b11; end
12'h003: begin {frm, fflags} <= csr_wv[7:0]; fs <= 2'b11; end
12'h300: begin mie_b <= csr_wv[3]; mpie_b <= csr_wv[7]; fs <= csr_wv[14:13]; end
12'h304: mie_r <= csr_wv & MIE_MASK;
12'h305: mtvec <= {csr_wv[31:2], 2'b00};
12'h340: mscratch <= csr_wv;
@@ -421,9 +490,16 @@ module rv32_cpu #(
pc4 <= pc + 32'd4;
tgt <= pc + (is_jal ? imm_j : imm_b);
a <= is_lui ? 32'd0 : (is_auipc || is_jal) ? pc : rv1;
b <= (is_lui || is_auipc) ? imm_u : is_st ? imm_s :
b <= (is_lui || is_auipc) ? imm_u : (is_st || is_fsw) ? imm_s :
(is_op || is_br || is_mdv) ? rv2 : imm_i;
sv <= rv2;
sv <= is_fsw ? fv2 : rv2;
fa <= fp_frint ? rv1 : fv1;
fb <= fv2;
fc <= fv3;
e_rm <= rm_d;
e_fp <= is_fp;
e_fpint <= fp_toint;
e_fd <= is_flw && fp_on;
alu_fn <= (is_lui || is_auipc) ? 3'd0 : f3;
alu_alt <= inst[30];
e_alu <= is_lui | is_auipc | is_opi | is_op;
@@ -432,8 +508,8 @@ module rv32_cpu #(
e_jalr <= is_jalr;
e_br <= is_br;
e_tmis <= is_jal ? inst[21] : inst[8];
e_ld <= is_ld;
e_st <= is_st;
e_ld <= is_ld || (is_flw && fp_on);
e_st <= is_st || (is_fsw && fp_on);
e_mul <= is_mdv & !f3[2];
e_div <= is_mdv & f3[2];
e_csr <= is_csr;
@@ -471,6 +547,8 @@ module rv32_cpu #(
state <= S_MUL;
else if (e_div)
state <= S_DIV;
else if (e_fp)
state <= S_FP;
else if (e_br && e_tmis)
state <= S_XT;
else if (e_mret) begin
@@ -492,7 +570,16 @@ module rv32_cpu #(
end
end
S_MEM: if (d_ack) begin pc <= pc4; state <= S_D; end
S_MEM: if (d_ack) begin
pc <= pc4; state <= S_D;
if (e_fd) fs <= 2'b11;
end
S_FP: if (fpu_done) begin
pc <= pc4; state <= S_D;
fflags <= fflags | fpu_flags;
fs <= 2'b11;
end
S_MUL: begin
m_cnt <= 1'b1;
@@ -546,6 +633,7 @@ module rv32_cpu #(
mcycle <= 64'd0; minstret <= 64'd0;
retire <= 1'b0; count_i <= 1'b0;
nmie <= 1'b1; stepped <= 1'b0; nmi_taken <= 1'b0;
fs <= 2'b01; fflags <= 5'd0; frm <= 3'd0;
end
end
endmodule