diff --git a/README.md b/README.md index 8d9797a..b0b3e1c 100644 --- a/README.md +++ b/README.md @@ -3,7 +3,7 @@ A complete home computer on a Digilent Basys 3 FPGA board, built around a RISC-V processor written from scratch in Verilog: -- **RV32IMF processor at 90 MHz**: integer, multiply/divide and IEEE 754 +- **RV32IMF processor at 100 MHz**: integer, multiply/divide and IEEE 754 single-precision floating point in hardware, with traps, interrupts, hardware breakpoints and single step. - **VT220 terminal** with ANSI colour on 1280×1024 VGA: 132 × 64 text, or 80 × 32 @@ -119,8 +119,8 @@ store breakpoints, and a resumable NMI (after the Smrnmi extension: `mnepc`, It is a multi-cycle design with the next instruction fetched while the current one decodes: 2 clocks for most instructions, 3 for taken branches, jumps, loads and stores, 4 for multiplies, 35 for divides; BASIC averages about 2.5. -Floating-point add, subtract, multiply and the fused multiply-adds take 12 -clocks, divide and square root 36. It runs at 90 MHz and takes about 5,300 +Floating-point add, subtract, multiply and the fused multiply-adds take 13 +clocks, divide and square root 37. It runs at 100 MHz and takes about 5,300 LUTs (3,000 of them the FPU), 1,900 flip-flops and 6 DSP slices. ## The machine (`rtl/rv32_system.v`) @@ -163,10 +163,10 @@ All in C (plus a little assembly), compiled with GCC: | BASIC on the board | | |---|---| -| `Y = SIN(X)` | 37 µs | -| `Y = EXP(X)` | 45 µs | -| `X = I * 1.5` | 26 µs | -| An empty `FOR` loop, each time round | 8.3 µs | +| `Y = SIN(X)` | 33 µs | +| `Y = EXP(1)` | 33 µs | +| `X = I * 1.5` | 23 µs | +| An empty `FOR` loop, each time round | 6.4 µs | ## Building diff --git a/docs/basic-manual.md b/docs/basic-manual.md index dc8dd28..2f2f4cf 100644 --- a/docs/basic-manual.md +++ b/docs/basic-manual.md @@ -796,19 +796,20 @@ each voice, so a note or a bang fades away by itself. ### Speed 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 +and divide, and single-precision floating point) running at 100 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, +BASIC, some 40 million instructions a second. Its floating-point unit adds, +subtracts and multiplies in 13 clocks and divides or takes a square root in 37, correctly rounded. In round numbers: | Operation | Time | |---|---| -| 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 | +| One pass of an empty `FOR … NEXT` loop | 6.4 µs (about 150,000 a second) | +| `X = I * 1.5` | 23 µs | +| `Y = SIN(X)` | 33 µs | +| `Y = EXP(1)` | 33 µs | +| `LINE` right across the screen | 0.12 ms | Constants in a program are converted from text every time they are used, so a loop runs a little faster if a constant used inside it is put into a variable diff --git a/docs/basic-manual.pdf b/docs/basic-manual.pdf index bce7353..8b65a8d 100644 Binary files a/docs/basic-manual.pdf and b/docs/basic-manual.pdf differ diff --git a/games/cavern/test-cavern.py b/games/cavern/test-cavern.py index 1be51f8..426b6f6 100644 --- a/games/cavern/test-cavern.py +++ b/games/cavern/test-cavern.py @@ -5,7 +5,7 @@ python3 test-cavern.py [--shots DIR] [--play] It checks the title screen, measures the Z-80 instructions each frame takes -(a 60 Hz frame has about 550,000 at 90 MHz), checks that walls stop the miner, +(a 60 Hz frame has about 610,000 at 100 MHz), checks that walls stop the miner, that crystals score and that the bats leave the roost, and with --play lets an autopilot gather every crystal in the cave by breadth-first search. The autopilot can't be caught (it keeps the miner shielded), so this checks the diff --git a/games/raid/test-raid.py b/games/raid/test-raid.py index b3485bf..5e0ec07 100644 --- a/games/raid/test-raid.py +++ b/games/raid/test-raid.py @@ -5,7 +5,7 @@ and take screenshots. python3 test-raid.py [--shots DIR] [--play] It checks the title screen, measures the Z-80 instructions each frame takes -(a 60 Hz frame has about 550,000 at 90 MHz), flies into the fortress, and with +(a 60 Hz frame has about 610,000 at 100 MHz), flies into the fortress, and with --play lets an autopilot fly the whole level: it steers through every doorway at the right height, climbs over low walls and shoots ahead. The autopilot can't be hit by shells or fighters (it keeps the ship invulnerable), so this @@ -57,7 +57,7 @@ def main(): c0 = b.steps b.run_frames(60) per = (b.steps - c0) / 60 - print("%.0f instructions a frame (%.0f%% of a 60 Hz frame at 90 MHz)" % (per, per / 5500)) + print("%.0f instructions a frame (%.0f%% of a 60 Hz frame at 100 MHz)" % (per, per / 6100)) check("a frame fits in a 60th of a second", per < 500000) if a.shots: b.shot(os.path.join(a.shots, "2-approach.png"), font) alt0 = b.m[sym["_alt"]] diff --git a/games/swarm/test-swarm.py b/games/swarm/test-swarm.py index b7a5d62..98c61b7 100644 --- a/games/swarm/test-swarm.py +++ b/games/swarm/test-swarm.py @@ -5,7 +5,7 @@ and take screenshots. python3 test-swarm.py [--shots DIR] [--play] It checks the title screen, measures the Z-80 instructions each frame takes -(a 60 Hz frame has about 550,000 at 90 MHz), checks that the formation sways +(a 60 Hz frame has about 610,000 at 100 MHz), checks that the formation sways and that aliens dive, shoots one out of the formation, and with --play lets an autopilot clear the whole swarm. The autopilot can't be killed (it keeps the ship shielded), so this checks the rules and the waves, not its skill. diff --git a/games/warden/test-warden.py b/games/warden/test-warden.py index f36d774..27834cf 100644 --- a/games/warden/test-warden.py +++ b/games/warden/test-warden.py @@ -5,7 +5,7 @@ and take screenshots. python3 test-warden.py [--shots DIR] [--play] It checks the title screen, measures the Z-80 instructions each frame takes -(a 60 Hz frame has about 550,000 at 90 MHz), flies along the planet checking +(a 60 Hz frame has about 610,000 at 100 MHz), flies along the planet checking that the picture scrolls and the scanner fills in, and with --play lets an autopilot hunt down every enemy until the wave is cleared. The autopilot can't be killed (it keeps the ship shielded), so this checks the rules and the diff --git a/rtl/basys3_terminal.v b/rtl/basys3_terminal.v index b20357f..eeafe4a 100644 --- a/rtl/basys3_terminal.v +++ b/rtl/basys3_terminal.v @@ -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, diff --git a/rtl/rv32_cpu.v b/rtl/rv32_cpu.v index 61773ed..9f69258 100644 --- a/rtl/rv32_cpu.v +++ b/rtl/rv32_cpu.v @@ -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, diff --git a/rtl/rv32_fpu.v b/rtl/rv32_fpu.v index 0df1355..d580121 100644 --- a/rtl/rv32_fpu.v +++ b/rtl/rv32_fpu.v @@ -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; diff --git a/rtl/rv32_system.v b/rtl/rv32_system.v index a4b311b..fd82476 100644 --- a/rtl/rv32_system.v +++ b/rtl/rv32_system.v @@ -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 diff --git a/rtl/sound.v b/rtl/sound.v index c1f2946..7e25256 100644 --- a/rtl/sound.v +++ b/rtl/sound.v @@ -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, diff --git a/scripts/build.tcl b/scripts/build.tcl index 10739e8..fc1f958 100644 --- a/scripts/build.tcl +++ b/scripts/build.tcl @@ -4,7 +4,7 @@ set root [file normalize [file dirname [info script]]/..] set out $root/build file mkdir $out -set mult [expr {[info exists ::env(CPU_MULT)] ? $::env(CPU_MULT) : 9.0}] +set mult [expr {[info exists ::env(CPU_MULT)] ? $::env(CPU_MULT) : 10.0}] set div [expr {[info exists ::env(CPU_DIV)] ? $::env(CPU_DIV) : 10.0}] set hz [expr {int(round(100e6 * $mult / $div))}] puts "CPU clock: [expr {$hz / 1e6}] MHz" diff --git a/tools/rv32emu.py b/tools/rv32emu.py index df3b50d..720ecd9 100644 --- a/tools/rv32emu.py +++ b/tools/rv32emu.py @@ -559,10 +559,10 @@ class RV32: if op in (0x07, 0x27): return 3 if op in (0x43, 0x47, 0x4B, 0x4F): - return 12 + return 13 if op == 0x53: f5 = ins >> 27 - return {0: 12, 1: 12, 2: 12, 3: 36, 0x0B: 36, 0x18: 5, 0x1A: 8}.get(f5, 4) + return {0: 13, 1: 13, 2: 13, 3: 37, 0x0B: 37, 0x18: 5, 0x1A: 9}.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