Run the CPU at 100 MHz

The FPU picks the FADD/FSUB operands and works out exponents and leading
zeros as it takes them, and finds the back end's leading one a clock
before using it; those were the paths that held the design to 91 MHz.
Add, multiply and the multiply-adds now take 13 clocks, divide and
square root 37, int -> float 9. Setup slack at 100 MHz is +0.286 ns.
On the board: an empty FOR loop 6.4 us (was 8.3), SIN 33 us (was 37).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
brad ArantandClaude Opus 5.5 committed 2026-12-01 07:26:59 -08:00
1 parent bda9bb67b6
commit 2dafda5fe5
14 files changed
+78 -51

No files matched your search

+2 -2
View File
@@ -32,9 +32,9 @@ module basys3_terminal #(
parameter MON_FILE = "monitor.mem",
parameter WIDE_BOOT = 1, // the screen starts at 132x64 (0: 80x32)
parameter CLK_HZ = 100_000_000,
parameter real CPU_MULT = 9.0, // CPU clock = 100 MHz * CPU_MULT / CPU_DIV
parameter real CPU_MULT = 10.0, // CPU clock = 100 MHz * CPU_MULT / CPU_DIV
parameter real CPU_DIV = 10.0,
parameter CPU_HZ = 90_000_000,
parameter CPU_HZ = 100_000_000,
parameter ROM_TOP = 32'h0000_A000 // BASIC's code is below; writes there are ignored
) (
input clk,
+4 -4
View File
@@ -8,9 +8,9 @@
// instruction. Taken branches and jumps take 3, loads and stores 3 (more if
// 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,
// class, 5 for float -> int, 9 for int -> float, 13 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.
// 37 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.
@@ -29,8 +29,8 @@
// breakpoint) are raised one cycle late, from registers, to keep the long
// paths out of the trap logic; the instruction has no effect either way.
//
// Speed: 90 MHz with 1.5 ns to spare on the XC7A35T-1 (bench/), about 2100
// LUTs, 900 flip-flops and 4 DSP slices.
// Speed: 100 MHz in the full design on the XC7A35T-1. The integer core
// alone (bench/) is about 2100 LUTs, 900 flip-flops and 4 DSP slices.
//
// 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,
+47 -21
View File
@@ -20,9 +20,11 @@
// The rest (sign injection, min/max, compares, class, moves, float -> int)
// finish without it.
//
// Clocks from start to done: multiply-add family 10 (2 when an operand is
// infinite, NaN or zero), divide and square root 34, int -> float 6,
// float -> int 3, the rest 2.
// Clocks from start to done: multiply-add family 11 (2 when an operand is
// infinite, NaN or zero), divide and square root 35, int -> float 7,
// float -> int 3, the rest 2. (Exponents, leading zeros and the FADD/FSUB
// operand swap are worked out as the operands are taken, and the back end
// finds the leading one a clock before using it, for 100 MHz.)
module rv32_fpu (
input clk,
input rst,
@@ -40,7 +42,7 @@ module rv32_fpu (
localparam S_IDLE = 4'd0, S_U = 4'd1, S_M1 = 4'd2, S_M2 = 4'd3, S_ADD = 4'd4,
S_ABS = 4'd5, S_N1 = 4'd6, S_N2 = 4'd7, S_R1 = 4'd8, S_R2 = 4'd9,
S_DIV = 4'd10, S_SQRT = 4'd11, S_CW = 4'd12, S_DIVF = 4'd13,
S_SQRTF = 4'd14;
S_SQRTF = 4'd14, S_NZ = 4'd15;
reg [3:0] st;
// ------------------------------------------------------------ operands
@@ -59,10 +61,12 @@ module rv32_fpu (
wire op_fma = r4 || op_add || op_mul;
wire uns = ri[20];
// a (for FADD/FSUB the multiplier is 1.0), b, c
// a (for FADD/FSUB the multiplier is 1.0), b, c: picked, with their
// exponents and leading zeros, as the operands arrive (see S_IDLE)
reg [31:0] rxb, rxc;
wire [31:0] xa = ra;
wire [31:0] xb = op_add ? 32'h3F80_0000 : rb;
wire [31:0] xc = op_add ? rb : rc;
wire [31:0] xb = rxb;
wire [31:0] xc = rxc;
wire negp = r4 && opc[3];
wire negc = (r4 && opc[2]) || (op_add && f5[0]);
wire use_c = !op_mul;
@@ -80,9 +84,8 @@ module rv32_fpu (
wire [23:0] ma = {ex8(xa) != 8'd0, xa[22:0]};
wire [23:0] mb = {ex8(xb) != 8'd0, xb[22:0]};
wire [23:0] mc = {ex8(xc) != 8'd0, xc[22:0]};
wire signed [11:0] ea = ex8(xa) == 8'd0 ? 12'sd1 : {4'd0, ex8(xa)};
wire signed [11:0] eb = ex8(xb) == 8'd0 ? 12'sd1 : {4'd0, ex8(xb)};
wire signed [11:0] ec = ex8(xc) == 8'd0 ? 12'sd1 : {4'd0, ex8(xc)};
reg signed [11:0] ea, eb, ec; // exponents (1 for a subnormal)
reg signed [11:0] eab; // ea + eb - 127, the product's
wire sp = xa[31] ^ xb[31] ^ negp;
wire sc = xc[31] ^ negc;
@@ -158,7 +161,7 @@ module rv32_fpu (
// sits at the top and the exponent comes from c (the product only adds
// stickiness, which rounds the same at any distance that far down).
// (worked out from the operands in S_U, used in S_M1)
wire signed [12:0] s_u = {ec[11], ec} - ({ea[11], ea} + {eb[11], eb} - 13'sd127) + 13'sd23;
wire signed [12:0] s_u = {ec[11], ec} - {eab[11], eab} + 13'sd23;
reg signed [12:0] s_amt;
reg s_big;
reg [76:0] c_sh;
@@ -191,7 +194,8 @@ module rv32_fpu (
reg n_zero;
reg [76:0] y;
reg y_st; // sticky from a right shift
wire signed [11:0] e_norm = n_ex - {5'd0, lz};
reg [6:0] n_lz; // lz, registered in S_NZ
reg signed [11:0] e_norm; // n_ex - lz
reg signed [11:0] r_e; // the result's exponent (1 if subnormal)
// rounding
wire [23:0] y_m = y[76:53];
@@ -229,9 +233,19 @@ module rv32_fpu (
for (i = 0; i < 24; i = i + 1) if (m[i]) lz24 = 5'd23 - i[4:0];
end
endfunction
wire [4:0] lza = lz24(ma), lzb = lz24(mb);
reg [4:0] lza, lzb;
wire [23:0] na = ma << lza, nb = mb << lzb;
wire signed [11:0] nea = ea - {7'd0, lza}, neb = eb - {7'd0, lzb};
reg signed [11:0] nea, neb; // exponents of na, nb
// the same from the operands as they arrive
function signed [11:0] exb(input [31:0] x);
exb = x[30:23] == 8'd0 ? 12'sd1 : {4'd0, x[30:23]};
endfunction
function [23:0] mant(input [31:0] x); mant = {x[30:23] != 8'd0, x[22:0]}; endfunction
wire i_add = ins[6:4] != 3'b100 && ins[31:28] == 4'h0; // FADD, FSUB
wire [31:0] i_b = i_add ? 32'h3F80_0000 : b;
wire [31:0] i_c = i_add ? b : c;
wire [4:0] i_lza = lz24(mant(a)), i_lzb = lz24(mant(i_b));
// ------------------------------------------------------- float -> int
reg [32:0] cw_int;
@@ -246,13 +260,19 @@ module rv32_fpu (
case (st)
S_IDLE: if (start) begin
ra <= a; rb <= b; rc <= c; ri <= ins; rrm <= rm;
rxb <= i_b; rxc <= i_c;
ea <= exb(a); eb <= exb(i_b); ec <= exb(i_c);
eab <= exb(a) + exb(i_b) - 12'sd127;
lza <= i_lza; lzb <= i_lzb;
nea <= exb(a) - {7'd0, i_lza};
neb <= exb(i_b) - {7'd0, i_lzb};
st <= S_U;
end
S_U: begin
// multiply-add specials (only used by the multiply-add family)
pa <= ma; pb <= mb;
ep <= ea + eb - 12'sd127;
ep <= eab;
c_m <= mc; c_e <= ec; c_zero <= cz || !use_c;
s_big <= !(cz || !use_c) && s_u > 13'sd50;
s_amt <= (!(cz || !use_c) && s_u > 13'sd50) ? 13'sd50 : s_u;
@@ -315,7 +335,7 @@ module rv32_fpu (
n_sign <= !uns && ra[31];
n_x <= {(!uns && ra[31] ? -ra : ra), 45'd0};
n_ex <= 12'sd158;
st <= S_N1;
st <= S_NZ;
end else if (op_cvtw) begin
cw_neg <= ra[31];
if (ex8(ra) >= 8'd150) begin
@@ -373,17 +393,23 @@ module rv32_fpu (
n_sign <= noadd ? s_p : s_c;
end
n_ex <= fx;
st <= S_NZ;
end
S_NZ: begin // find the top bit
n_zero <= n_x == 77'd0;
n_lz <= lz;
e_norm <= n_ex - {5'd0, lz};
st <= S_N1;
end
S_N1: begin // where to put the top bit
n_zero <= n_x == 77'd0;
S_N1: begin // where to put it
if (e_norm < 12'sd1) begin
n_sh <= n_ex - 12'sd1; // subnormal: as far as exponent 1
r_e <= 12'sd1;
n_tiny <= 1'b1;
end else begin
n_sh <= {5'd0, lz};
n_sh <= {5'd0, n_lz};
r_e <= e_norm;
n_tiny <= 1'b0;
end
@@ -455,12 +481,12 @@ module rv32_fpu (
S_DIVF: begin
n_x <= {dq, drem != 26'd0, 49'd0};
st <= S_N1;
st <= S_NZ;
end
S_SQRTF: begin
n_x <= {dq, srem != 29'd0, 49'd0};
st <= S_N1;
st <= S_NZ;
end
default: st <= S_IDLE;
+1 -1
View File
@@ -71,7 +71,7 @@ module rv32_system #(
parameter MEM_FILE = "basic.mem", // RAM 0000-FFFF
parameter MEM_HI_FILE = "basic-hi.mem", // RAM 10000-13FFF
parameter MON_FILE = "monitor.mem",
parameter CPU_HZ = 90_000_000,
parameter CPU_HZ = 100_000_000,
parameter ROM_TOP = 32'h0000_A000
) (
input clk, // CPU clock
+1 -1
View File
@@ -25,7 +25,7 @@
// sigma-delta modulator at the full clock rate; the PmodAMP2 filters it and
// drives headphones or a speaker.
module sound #(
parameter CPU_HZ = 90_000_000
parameter CPU_HZ = 100_000_000
) (
input clk,
input rst,