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>
640 lines
28 KiB
Verilog
640 lines
28 KiB
Verilog
`timescale 1ns / 1ps
|
|
|
|
// 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. Floating-point operations run
|
|
// in rv32_fpu.v: 4 clocks for moves, sign injection, compares, min/max and
|
|
// 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),
|
|
// 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.
|
|
//
|
|
// Data port: d_req is high for one cycle with d_addr, d_we, d_wstrb and
|
|
// d_wdata; the bus answers with d_ack (and d_rdata for a read) one or more
|
|
// cycles later. d_addr is word-aligned for words, halfword-aligned for
|
|
// halfwords (misaligned accesses trap); d_rdata is the whole word.
|
|
// d_addr and d_wdata come early in the cycle (an adder from registers), but
|
|
// d_req comes late (after the alignment and breakpoint checks): register it,
|
|
// and the store, before it reaches block RAM write enables. A read with no
|
|
// request is harmless for RAM, so RAM can be read from d_addr every cycle.
|
|
//
|
|
// Exceptions that depend on the branch comparison or the load/store address
|
|
// (a taken branch to a misaligned target, a misaligned load/store, a load/store
|
|
// 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: 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,
|
|
// 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,
|
|
// tdata1, tdata2, tinfo, tcontrol) for execute, load and store address
|
|
// breakpoints. Triggers fire while tcontrol.MTE is set; a trap clears it
|
|
// (saving it in MPTE) and mret restores it.
|
|
//
|
|
// Resumable NMI (after the Smrnmi extension, for a debug monitor): while
|
|
// mnstatus.NMIE is set, `nmi` (checked as each instruction starts; it may come
|
|
// straight from comparators on dbg_pc) or `nmi_step` (after one instruction
|
|
// has completed with NMIE set) saves the pc in mnepc, clears NMIE and jumps to
|
|
// NMI_VEC; mnret goes back and sets NMIE again. nmi_taken pulses as it is
|
|
// taken. Unlike Smrnmi, NMIE resets to 1, software may clear it, and traps
|
|
// still go to mtvec while it is clear. CSRs: mnscratch, mnepc, mncause, mnstatus.
|
|
//
|
|
// Interrupts: mip.MSIP (irq_soft), MTIP (irq_timer), MEIP (irq_ext) and local
|
|
// interrupts 16-31 (irq_local), all level-sensitive. Priority: external,
|
|
// software, timer, then local 16 upward.
|
|
module rv32_cpu #(
|
|
parameter [31:0] RESET_PC = 32'h0000_0000,
|
|
parameter [31:0] NMI_VEC = 32'h0000_0000
|
|
) (
|
|
input clk,
|
|
input rst, // synchronous
|
|
output i_en,
|
|
output [31:0] i_addr,
|
|
input [31:0] i_rdata,
|
|
output d_req,
|
|
output d_we,
|
|
output [3:0] d_wstrb,
|
|
output [31:0] d_addr,
|
|
output [31:0] d_wdata,
|
|
input [31:0] d_rdata,
|
|
input d_ack,
|
|
input irq_soft,
|
|
input irq_timer,
|
|
input irq_ext,
|
|
input [15:0] irq_local,
|
|
input nmi,
|
|
input nmi_step,
|
|
output reg nmi_taken,
|
|
output [31:0] dbg_pc, // address of the instruction being run
|
|
output dbg_decode, // high in an instruction's first cycle
|
|
output reg retire // pulse: an instruction completed
|
|
);
|
|
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;
|
|
assign i_addr = state == S_D ? pc + 32'd4 : pc;
|
|
assign dbg_pc = pc;
|
|
assign dbg_decode = state == S_D;
|
|
|
|
// ------------------------------------------------------------------
|
|
// Registers: distributed RAM, two asynchronous read ports.
|
|
reg [31:0] rf [0:31];
|
|
integer k;
|
|
initial for (k = 0; k < 32; k = k + 1) rf[k] = 32'd0;
|
|
reg rf_we;
|
|
reg [4:0] rf_wa;
|
|
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;
|
|
wire [6:0] opc = inst[6:0];
|
|
wire [2:0] f3 = inst[14:12];
|
|
wire [6:0] f7 = inst[31:25];
|
|
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]};
|
|
wire [31:0] imm_b = {{20{inst[31]}}, inst[7], inst[30:25], inst[11:8], 1'b0};
|
|
wire [31:0] imm_u = {inst[31:12], 12'd0};
|
|
wire [31:0] imm_j = {{12{inst[31]}}, inst[19:12], inst[20], inst[30:21], 1'b0};
|
|
|
|
wire is_lui = opc == 7'b0110111;
|
|
wire is_auipc = opc == 7'b0010111;
|
|
wire is_jal = opc == 7'b1101111;
|
|
wire is_jalr = opc == 7'b1100111 && f3 == 3'd0;
|
|
wire is_br = opc == 7'b1100011 && f3[2:1] != 2'b01;
|
|
wire is_ld = opc == 7'b0000011 && (f3 == 3'd0 || f3 == 3'd1 || f3 == 3'd2 ||
|
|
f3 == 3'd4 || f3 == 3'd5);
|
|
wire is_st = opc == 7'b0100011 && f3[2] == 1'b0 && f3[1:0] != 2'b11;
|
|
wire is_opi = opc == 7'b0010011 &&
|
|
(f3 == 3'd1 ? f7 == 7'd0 :
|
|
f3 == 3'd5 ? (f7 == 7'd0 || f7 == 7'b0100000) : 1'b1);
|
|
wire is_op = opc == 7'b0110011 &&
|
|
(f7 == 7'd0 || (f7 == 7'b0100000 && (f3 == 3'd0 || f3 == 3'd5)));
|
|
wire is_mdv = opc == 7'b0110011 && f7 == 7'b0000001;
|
|
wire is_fence = opc == 7'b0001111 && f3 == 3'd0;
|
|
wire is_fencei= opc == 7'b0001111 && f3 == 3'd1;
|
|
wire is_sys = opc == 7'b1110011;
|
|
wire is_csr = is_sys && f3[1:0] != 2'b00;
|
|
wire is_ecall = inst == 32'h0000_0073;
|
|
wire is_ebrk = inst == 32'h0010_0073;
|
|
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 |
|
|
(fp_on && (is_flw || is_fsw)) | is_fp;
|
|
|
|
// ------------------------------------------------------------------
|
|
// Execute-stage registers, loaded at the end of D.
|
|
reg [31:0] ir; // the instruction
|
|
reg [31:0] a, b; // ALU operands
|
|
reg [31:0] sv; // store data / rs2
|
|
reg [31:0] tgt; // branch or jal target
|
|
reg [31:0] pc4;
|
|
reg e_tmis; // branch / jal target is misaligned
|
|
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_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;
|
|
reg alu_alt; // sra rather than srl
|
|
|
|
// multiplier (DSP), fed straight from decode
|
|
reg signed [32:0] m_a, m_b;
|
|
reg signed [65:0] m_p, m_p2;
|
|
reg m_hi, m_cnt;
|
|
always @(posedge clk) begin
|
|
m_p <= m_a * m_b;
|
|
m_p2 <= m_p;
|
|
end
|
|
|
|
// ------------------------------------------------------------------
|
|
// Execute (state E)
|
|
wire [31:0] sum = a + b;
|
|
wire eq = a == b;
|
|
wire lt = $signed(a) < $signed(b);
|
|
wire ltu = a < b;
|
|
wire [31:0] sra_out = $signed(a) >>> b[4:0];
|
|
reg [31:0] alu_out;
|
|
always @* begin
|
|
case (alu_fn)
|
|
3'd0: alu_out = e_sub ? a - b : sum;
|
|
3'd1: alu_out = a << b[4:0];
|
|
3'd2: alu_out = {31'd0, lt};
|
|
3'd3: alu_out = {31'd0, ltu};
|
|
3'd4: alu_out = a ^ b;
|
|
3'd5: alu_out = alu_alt ? sra_out : a >> b[4:0];
|
|
3'd6: alu_out = a | b;
|
|
default: alu_out = a & b;
|
|
endcase
|
|
end
|
|
reg taken;
|
|
always @* begin
|
|
case (e_f3)
|
|
3'd0: taken = eq;
|
|
3'd1: taken = !eq;
|
|
3'd4: taken = lt;
|
|
3'd5: taken = !lt;
|
|
3'd6: taken = ltu;
|
|
default: taken = !ltu;
|
|
endcase
|
|
end
|
|
wire [31:0] jtarget = e_jalr ? {sum[31:1], 1'b0} : tgt;
|
|
wire jump = e_jal | e_jalr | (e_br & taken);
|
|
|
|
// loads and stores
|
|
wire [1:0] sz = e_f3[1:0];
|
|
wire [1:0] lo = a[1:0] + b[1:0];
|
|
wire mis = (sz == 2'd1 && lo[0]) || (sz == 2'd2 && lo != 2'd0);
|
|
|
|
// ------------------------------------------------------------------
|
|
// CSRs
|
|
reg mie_b, mpie_b; // mstatus
|
|
reg [31:0] mie_r, mtvec, mscratch, mepc, mcause, mtval;
|
|
reg [63:0] mcycle, minstret;
|
|
reg tsel, mte, mpte;
|
|
reg nmie, stepped;
|
|
reg [31:0] mnscratch, mnepc, mncause;
|
|
reg [1:0] t_m, t_x, t_s, t_l;
|
|
reg [31:0] t_a0, t_a1;
|
|
reg [31:0] irq_r;
|
|
localparam [31:0] MIE_MASK = 32'hFFFF_0888;
|
|
|
|
wire [11:0] csr_a = ir[31:20];
|
|
reg [31:0] csr_rd;
|
|
reg csr_ok;
|
|
always @* begin
|
|
csr_ok = 1'b1;
|
|
case (csr_a)
|
|
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
|
|
12'h340: csr_rd = mscratch;
|
|
12'h341: csr_rd = mepc;
|
|
12'h342: csr_rd = mcause;
|
|
12'h343: csr_rd = mtval;
|
|
12'h344: csr_rd = irq_r;
|
|
12'h740: csr_rd = mnscratch;
|
|
12'h741: csr_rd = mnepc;
|
|
12'h742: csr_rd = mncause;
|
|
12'h744: csr_rd = {19'd0, 2'b11, 7'd0, nmie, 3'd0};
|
|
12'h7A0: csr_rd = {31'd0, tsel};
|
|
12'h7A1: csr_rd = {4'd2, 21'd0, t_m[tsel], 3'd0, t_x[tsel], t_s[tsel], t_l[tsel]};
|
|
12'h7A2: csr_rd = tsel ? t_a1 : t_a0;
|
|
12'h7A4: csr_rd = 32'h0000_0004; // tinfo: match triggers
|
|
12'h7A5: csr_rd = {24'd0, mpte, 3'd0, mte, 3'd0};
|
|
12'hB00, 12'hC00, 12'hC01: csr_rd = mcycle[31:0];
|
|
12'hB80, 12'hC80, 12'hC81: csr_rd = mcycle[63:32];
|
|
12'hB02, 12'hC02: csr_rd = minstret[31:0];
|
|
12'hB82, 12'hC82: csr_rd = minstret[63:32];
|
|
12'hF11, 12'hF12, 12'hF13, 12'hF14, 12'hF15: csr_rd = 32'd0;
|
|
default: begin csr_rd = 32'd0; csr_ok = 1'b0; end
|
|
endcase
|
|
end
|
|
wire csr_imm = e_f3[2];
|
|
wire [31:0] csr_src = csr_imm ? {27'd0, ir[19:15]} : a;
|
|
wire csr_wr = e_f3[1:0] == 2'b01 || ir[19:15] != 5'd0;
|
|
wire [31:0] csr_wv = e_f3[1:0] == 2'b01 ? csr_src :
|
|
e_f3[1:0] == 2'b10 ? csr_rd | csr_src : csr_rd & ~csr_src;
|
|
wire csr_bad = !csr_ok || (csr_wr && csr_a[11:10] == 2'b11);
|
|
wire csr_go = state == S_E && e_csr && !csr_bad && csr_wr;
|
|
|
|
// triggers
|
|
wire fire_x = mte && ((t_m[0] && t_x[0] && pc == t_a0) || (t_m[1] && t_x[1] && pc == t_a1));
|
|
// a + b == k without waiting for the carries: bit i of the sum matches k
|
|
// exactly when the carry into it is x[i] = a^b^k, and that carry is the
|
|
// majority of a, b and x one bit down (and 0 into bit 0).
|
|
function addeq(input [31:0] a, input [31:0] b, input [31:0] k);
|
|
reg [31:0] x, c;
|
|
begin
|
|
x = a ^ b ^ k;
|
|
c = (a & b) | (a & x) | (b & x);
|
|
addeq = !x[0] && x[31:1] == c[30:0];
|
|
end
|
|
endfunction
|
|
wire hit0 = addeq(a, b, t_a0), hit1 = addeq(a, b, t_a1);
|
|
wire fire_l = mte && ((t_m[0] && t_l[0] && hit0) || (t_m[1] && t_l[1] && hit1));
|
|
wire fire_s = mte && ((t_m[0] && t_s[0] && hit0) || (t_m[1] && t_s[1] && hit1));
|
|
|
|
// interrupts
|
|
wire [31:0] pend = irq_r & mie_r;
|
|
wire irq_take = mie_b && pend != 32'd0;
|
|
reg [4:0] irq_code;
|
|
integer j;
|
|
always @* begin
|
|
irq_code = 5'd16;
|
|
for (j = 31; j >= 16; j = j - 1) if (pend[j]) irq_code = j[4:0];
|
|
if (pend[7]) irq_code = 5'd7;
|
|
if (pend[3]) irq_code = 5'd3;
|
|
if (pend[11]) irq_code = 5'd11;
|
|
end
|
|
|
|
// ------------------------------------------------------------------
|
|
// Data bus
|
|
wire ls_bad = (e_ld && fire_l) || (e_st && fire_s) || mis;
|
|
wire mem_go = state == S_E && (e_ld || e_st) && !ls_bad;
|
|
assign d_req = mem_go;
|
|
assign d_we = e_st;
|
|
assign d_addr = sum;
|
|
assign d_wstrb = sz == 2'd0 ? 4'b0001 << sum[1:0] :
|
|
sz == 2'd1 ? (sum[1] ? 4'b1100 : 4'b0011) : 4'b1111;
|
|
assign d_wdata = sz == 2'd0 ? {4{sv[7:0]}} : sz == 2'd1 ? {2{sv[15:0]}} : sv;
|
|
reg [1:0] la; // address bits of the load
|
|
wire [31:0] lw_sh = d_rdata >> {la, 3'b000};
|
|
wire [31:0] ld_val = e_f3 == 3'd0 ? {{24{lw_sh[7]}}, lw_sh[7:0]} :
|
|
e_f3 == 3'd1 ? {{16{lw_sh[15]}}, lw_sh[15:0]} :
|
|
e_f3 == 3'd4 ? {24'd0, lw_sh[7:0]} :
|
|
e_f3 == 3'd5 ? {16'd0, lw_sh[15:0]} : d_rdata;
|
|
|
|
// divider
|
|
reg [31:0] dq, dr, dd, da;
|
|
reg [5:0] dcnt;
|
|
reg dneg_q, dneg_r, dzero;
|
|
wire [32:0] dtmp = {dr, dq[31]};
|
|
wire [33:0] dsub = {1'b0, dtmp} - {2'b0, dd};
|
|
|
|
// ------------------------------------------------------------------
|
|
// Traps
|
|
wire nmi_go = state == S_D && nmie && (nmi || (nmi_step && stepped));
|
|
reg trap;
|
|
reg [31:0] t_cause, t_val;
|
|
always @* begin
|
|
trap = 1'b0; t_cause = 32'd0; t_val = 32'd0;
|
|
if (state == S_D) begin
|
|
if (nmi_go) begin
|
|
trap = 1'b1; // to NMI_VEC; see below
|
|
end else if (irq_take) begin
|
|
trap = 1'b1; t_cause = {1'b1, 26'd0, irq_code};
|
|
end else if (fire_x) begin
|
|
trap = 1'b1; t_cause = 32'd3; t_val = pc;
|
|
end
|
|
end else if (state == S_XT) begin
|
|
// Traps that depend on the branch comparison or the load/store
|
|
// address are taken a cycle late, from registers, to keep those
|
|
// long paths out of the trap logic: a taken branch to a misaligned
|
|
// target, a load/store breakpoint, a misaligned load/store.
|
|
if (x_trap) begin
|
|
trap = 1'b1; t_cause = x_cause; t_val = x_val;
|
|
end
|
|
end else if (state == S_E) begin
|
|
if (e_ill || (e_csr && csr_bad)) begin
|
|
trap = 1'b1; t_cause = 32'd2; t_val = ir;
|
|
end else if ((e_jal && e_tmis) || (e_jalr && lo[1])) begin
|
|
trap = 1'b1; t_cause = 32'd0; t_val = jtarget;
|
|
end else if (e_ecall) begin
|
|
trap = 1'b1; t_cause = 32'd11;
|
|
end else if (e_ebrk) begin
|
|
trap = 1'b1; t_cause = 32'd3; t_val = pc;
|
|
end
|
|
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 && !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;
|
|
if (e_f3[1]) // rem, remu
|
|
rf_wd = dzero ? da : dneg_r ? -dr : dr;
|
|
else
|
|
rf_wd = dzero ? 32'hFFFF_FFFF : dneg_q ? -dq : dq;
|
|
end
|
|
default: ;
|
|
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 && !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);
|
|
|
|
// ------------------------------------------------------------------
|
|
always @(posedge clk) begin
|
|
irq_r <= {irq_local, 4'd0, irq_ext, 3'd0, irq_timer, 3'd0, irq_soft, 3'd0};
|
|
retire <= done;
|
|
// a write to either half replaces the count for this cycle
|
|
if (!(csr_go && (csr_a == 12'hB00 || csr_a == 12'hB80))) mcycle <= mcycle + 64'd1;
|
|
// instret counts a cycle late (instructions take at least two, so the
|
|
// next one still reads the right count)
|
|
count_i <= done && !(csr_go && (csr_a == 12'hB02 || csr_a == 12'hB82));
|
|
if (count_i) minstret <= minstret + 64'd1;
|
|
|
|
if (csr_go) begin
|
|
case (csr_a)
|
|
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;
|
|
12'h341: mepc <= {csr_wv[31:2], 2'b00};
|
|
12'h342: mcause <= csr_wv;
|
|
12'h343: mtval <= csr_wv;
|
|
12'h740: mnscratch <= csr_wv;
|
|
12'h741: mnepc <= {csr_wv[31:2], 2'b00};
|
|
12'h742: mncause <= csr_wv;
|
|
12'h744: nmie <= csr_wv[3];
|
|
12'h7A0: if (csr_wv[31:1] == 31'd0) tsel <= csr_wv[0];
|
|
12'h7A1: begin
|
|
t_m[tsel] <= csr_wv[6]; t_x[tsel] <= csr_wv[2];
|
|
t_s[tsel] <= csr_wv[1]; t_l[tsel] <= csr_wv[0];
|
|
end
|
|
12'h7A2: if (tsel) t_a1 <= csr_wv; else t_a0 <= csr_wv;
|
|
12'h7A5: begin mte <= csr_wv[3]; mpte <= csr_wv[7]; end
|
|
12'hB00: mcycle[31:0] <= csr_wv;
|
|
12'hB80: mcycle[63:32] <= csr_wv;
|
|
12'hB02: minstret[31:0] <= csr_wv;
|
|
12'hB82: minstret[63:32] <= csr_wv;
|
|
default: ;
|
|
endcase
|
|
end
|
|
|
|
case (state)
|
|
S_IF: state <= S_D;
|
|
|
|
S_D: begin
|
|
ir <= inst;
|
|
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 || is_fsw) ? imm_s :
|
|
(is_op || is_br || is_mdv) ? rv2 : imm_i;
|
|
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;
|
|
e_sub <= is_op & inst[30];
|
|
e_jal <= is_jal;
|
|
e_jalr <= is_jalr;
|
|
e_br <= is_br;
|
|
e_tmis <= is_jal ? inst[21] : inst[8];
|
|
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;
|
|
e_ecall <= is_ecall;
|
|
e_ebrk <= is_ebrk;
|
|
e_mret <= is_mret;
|
|
e_mnret <= is_mnret;
|
|
e_fencei<= is_fencei;
|
|
e_ill <= !legal;
|
|
m_a <= {f3[1:0] != 2'b11 && rv1[31], rv1}; // mulhu: unsigned
|
|
m_b <= {f3[1:0] == 2'b01 && rv2[31], rv2}; // mulh only: signed
|
|
m_hi <= f3[1:0] != 2'b00;
|
|
if (!trap) state <= S_E;
|
|
end
|
|
|
|
S_E: begin
|
|
// set up the multi-cycle units (unused if this traps)
|
|
la <= lo;
|
|
x_trap <= e_br ? taken : 1'b1;
|
|
x_cause <= e_br ? 32'd0 : (fire_l || fire_s) ? 32'd3 : e_st ? 32'd6 : 32'd4;
|
|
x_val <= e_br ? tgt : sum;
|
|
m_cnt <= 1'b0;
|
|
dneg_q <= !e_f3[0] && (a[31] ^ b[31]) && b != 32'd0;
|
|
dneg_r <= !e_f3[0] && a[31];
|
|
dzero <= b == 32'd0;
|
|
da <= a;
|
|
dq <= (!e_f3[0] && a[31]) ? -a : a;
|
|
dd <= (!e_f3[0] && b[31]) ? -b : b;
|
|
dr <= 32'd0;
|
|
dcnt <= 6'd0;
|
|
if (!trap) begin
|
|
if (e_ld || e_st)
|
|
state <= ls_bad ? S_XT : S_MEM;
|
|
else if (e_mul)
|
|
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
|
|
pc <= mepc; mie_b <= mpie_b; mpie_b <= 1'b1; mte <= mpte;
|
|
state <= S_IF;
|
|
end else if (e_mnret) begin
|
|
pc <= mnepc; nmie <= 1'b1;
|
|
state <= S_IF;
|
|
end else if (jump) begin
|
|
pc <= jtarget;
|
|
state <= S_IF;
|
|
end else if (e_fencei) begin
|
|
pc <= pc4;
|
|
state <= S_IF;
|
|
end else begin
|
|
pc <= pc4;
|
|
state <= S_D;
|
|
end
|
|
end
|
|
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;
|
|
if (m_cnt) begin pc <= pc4; state <= S_D; end
|
|
end
|
|
|
|
S_DIV: begin
|
|
if (!dsub[33]) begin dr <= dsub[31:0]; dq <= {dq[30:0], 1'b1}; end
|
|
else begin dr <= dtmp[31:0]; dq <= {dq[30:0], 1'b0}; end
|
|
dcnt <= dcnt + 6'd1;
|
|
if (dcnt == 6'd31) state <= S_DIVF;
|
|
end
|
|
|
|
S_DIVF: begin pc <= pc4; state <= S_D; end
|
|
|
|
S_XT: begin pc <= pc4; state <= S_D; end // not taken (else trap)
|
|
|
|
endcase
|
|
|
|
nmi_taken <= 1'b0;
|
|
stepped <= nmie && (stepped || done); // only counts outside the monitor
|
|
if (nmi_go) begin
|
|
mnepc <= pc;
|
|
mncause <= 32'h8000_0000;
|
|
nmie <= 1'b0;
|
|
stepped <= 1'b0;
|
|
nmi_taken <= 1'b1;
|
|
pc <= NMI_VEC;
|
|
state <= S_IF;
|
|
end else if (trap) begin
|
|
mepc <= pc;
|
|
mcause <= t_cause;
|
|
mtval <= t_val;
|
|
mpie_b <= mie_b;
|
|
mie_b <= 1'b0;
|
|
mpte <= mte;
|
|
mte <= 1'b0;
|
|
pc <= mtvec;
|
|
state <= S_IF;
|
|
end
|
|
|
|
if (rst) begin
|
|
state <= S_IF;
|
|
pc <= RESET_PC;
|
|
mie_b <= 1'b0; mpie_b <= 1'b0;
|
|
mie_r <= 32'd0;
|
|
mtvec <= RESET_PC;
|
|
mcause <= 32'd0;
|
|
tsel <= 1'b0; mte <= 1'b0; mpte <= 1'b0;
|
|
t_m <= 2'b00; t_x <= 2'b00; t_s <= 2'b00; t_l <= 2'b00;
|
|
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
|