`timescale 1ns / 1ps // Timing bench for rv32_cpu: the core with 64 KB of block RAM (instruction // fetch on one port, loads and stores on the other) at 100 MHz * MULT / DIV. // The LEDs show a register at 0x10000; switches 3:0 drive the interrupts. module cpu_bench #( parameter real MULT = 9.0, parameter real DIV = 10.0, parameter MEM_FILE = "bench.mem" ) ( input clk, input [15:0] sw, output reg [15:0] led ); wire fb, cclk_m, cclk, locked; MMCME2_BASE #( .CLKIN1_PERIOD(10.0), .DIVCLK_DIVIDE(1), .CLKFBOUT_MULT_F(MULT), .CLKOUT0_DIVIDE_F(DIV) ) mmcm ( .CLKIN1(clk), .CLKFBIN(fb), .CLKFBOUT(fb), .CLKFBOUTB(), .CLKOUT0(cclk_m), .CLKOUT0B(), .CLKOUT1(), .CLKOUT1B(), .CLKOUT2(), .CLKOUT2B(), .CLKOUT3(), .CLKOUT3B(), .CLKOUT4(), .CLKOUT5(), .CLKOUT6(), .LOCKED(locked), .PWRDWN(1'b0), .RST(1'b0) ); BUFG bufg (.I(cclk_m), .O(cclk)); reg [3:0] rst_sr = 4'hF; always @(posedge cclk) rst_sr <= {rst_sr[2:0], !locked}; reg [3:0] sw_r; always @(posedge cclk) sw_r <= sw[3:0]; wire i_en, d_req, d_we; wire [31:0] i_addr, d_addr, d_wdata; wire [3:0] d_wstrb; reg [31:0] i_rdata, ram_q; reg d_ack, io_sel; rv32_cpu cpu ( .clk(cclk), .rst(rst_sr[3]), .i_en(i_en), .i_addr(i_addr), .i_rdata(i_rdata), .d_req(d_req), .d_we(d_we), .d_wstrb(d_wstrb), .d_addr(d_addr), .d_wdata(d_wdata), .d_rdata(io_sel ? {16'd0, led} : ram_q), .d_ack(d_ack), .irq_soft(sw_r[0]), .irq_timer(sw_r[1]), .irq_ext(sw_r[2]), .irq_local({15'd0, sw_r[3]}), .nmi(1'b0), .nmi_step(1'b0), .nmi_taken(), .dbg_pc(), .dbg_decode(), .retire() ); // Reads use the core's address directly (a read with no request is // harmless); stores are registered and written the next cycle, so the // late d_req never reaches the block RAM's write enables. reg [31:0] ram [0:16383]; initial $readmemh(MEM_FILE, ram); wire ram_sel = d_addr[31:16] == 16'd0; reg w_go; reg [13:0] w_a; reg [31:0] w_d; reg [3:0] w_s; integer b; always @(posedge cclk) begin if (i_en) i_rdata <= ram[i_addr[15:2]]; end // one address for the data port: the pending store's, else the core's wire [13:0] b_a = w_go ? w_a : d_addr[15:2]; always @(posedge cclk) begin if (w_go) begin for (b = 0; b < 4; b = b + 1) if (w_s[b]) ram[b_a][8*b +: 8] <= w_d[8*b +: 8]; end ram_q <= ram[b_a]; end always @(posedge cclk) begin w_go <= d_req && d_we && ram_sel; w_a <= d_addr[15:2]; w_d <= d_wdata; w_s <= d_wstrb; d_ack <= d_req; // the store is written as the ack goes back if (d_req) io_sel <= !ram_sel; if (d_req && !ram_sel && d_we) led <= d_wdata[15:0]; end endmodule