// axil_regs.sv // AXI4-Lite subordinate with a small register file that lets software // control a hardware block (for example the FIR filter from lesson 15). // // Register map (32-bit registers, byte addresses): // 0x00 CTRL RW bit 0 ENABLE (level), bit 1 START (write 1: one-cycle // pulse on start_o; always reads 0). Other bits read 0. // 0x04 STATUS RO value of status_i, sampled when the read is accepted // 0x08 ID RO constant ID parameter, lets software find the block // 0x0C - reserved: reads and writes return SLVERR // 0x10 COEF[0] RW coefficient registers, COEF[k] at 0x10 + 4*k // ... // Any address outside the map returns SLVERR (0b10). Writes to read-only // registers are ignored and return OKAY. WSTRB is honoured byte by byte. // // Handshake (AMBA AXI spec IHI0022, A3.2 and A3.3): // - AW and W are accepted independently, in either order or in the same // cycle. Each is held in a one-entry register until both have arrived. // - The write is performed, and BVALID raised, only after both handshakes, // and only when no earlier response is still waiting for BREADY. // - One read is in flight at a time: ARREADY is low while RVALID is high. // - All READY and VALID outputs come from registers, so there is no // combinational path from an input to an output. // Reset: aresetn, active low, synchronous to aclk. BVALID and RVALID are low // during reset. `timescale 1ns/1ps module axil_regs #( parameter int ADDR_W = 6, // byte address width; 2**(ADDR_W-2) words parameter int N_COEF = 8, // needs 4 + N_COEF <= 2**(ADDR_W-2) parameter logic [31:0] ID = 32'h4649_5231 // "FIR1" in ASCII ) ( input logic aclk, input logic aresetn, // write address channel input logic [ADDR_W-1:0] s_axil_awaddr, input logic [2:0] s_axil_awprot, input logic s_axil_awvalid, output logic s_axil_awready, // write data channel input logic [31:0] s_axil_wdata, input logic [3:0] s_axil_wstrb, input logic s_axil_wvalid, output logic s_axil_wready, // write response channel output logic [1:0] s_axil_bresp, output logic s_axil_bvalid, input logic s_axil_bready, // read address channel input logic [ADDR_W-1:0] s_axil_araddr, input logic [2:0] s_axil_arprot, input logic s_axil_arvalid, output logic s_axil_arready, // read data channel output logic [31:0] s_axil_rdata, output logic [1:0] s_axil_rresp, output logic s_axil_rvalid, input logic s_axil_rready, // hardware side output logic enable_o, output logic start_o, input logic [31:0] status_i, output logic [N_COEF*32-1:0] coef_o ); localparam logic [1:0] OKAY = 2'b00; localparam logic [1:0] SLVERR = 2'b10; localparam int WW = ADDR_W - 2; // word index width localparam int A_CTRL = 0; localparam int A_STAT = 1; localparam int A_ID = 2; localparam int A_COEF0 = 4; // AXI4-Lite has no use for PROT here, and the two low address bits only // select a byte inside a 32-bit register. Verilator ignores names with "unused". logic unused; assign unused = &{1'b0, s_axil_awprot, s_axil_arprot, s_axil_awaddr[1:0], s_axil_araddr[1:0]}; function automatic logic mapped(input logic [WW-1:0] word); int w; w = int'(word); mapped = (w == A_CTRL) || (w == A_STAT) || (w == A_ID) || (w >= A_COEF0 && w < A_COEF0 + N_COEF); endfunction function automatic logic [31:0] apply_strb(input logic [31:0] old_v, input logic [31:0] new_v, input logic [3:0] strb); apply_strb = old_v; for (int b = 0; b < 4; b++) begin if (strb[b]) apply_strb[8*b +: 8] = new_v[8*b +: 8]; end endfunction // ------------------------------------------------------------------ // Register storage // ------------------------------------------------------------------ logic [31:0] coef_q [N_COEF]; logic enable_q; assign enable_o = enable_q; for (genvar k = 0; k < N_COEF; k++) begin : g_coef assign coef_o[32*k +: 32] = coef_q[k]; end // ------------------------------------------------------------------ // Write path: two one-entry holding registers, then one write // ------------------------------------------------------------------ logic aw_full, w_full; logic [WW-1:0] aw_word; logic [31:0] w_data; logic [3:0] w_strb; logic aw_hs, w_hs, do_write; assign s_axil_awready = !aw_full; assign s_axil_wready = !w_full; assign aw_hs = s_axil_awvalid && s_axil_awready; assign w_hs = s_axil_wvalid && s_axil_wready; assign do_write = aw_full && w_full && !s_axil_bvalid; always_ff @(posedge aclk) begin start_o <= 1'b0; // default: no pulse if (!aresetn) begin aw_full <= 1'b0; w_full <= 1'b0; aw_word <= '0; w_data <= '0; w_strb <= '0; s_axil_bvalid <= 1'b0; s_axil_bresp <= OKAY; enable_q <= 1'b0; for (int k = 0; k < N_COEF; k++) coef_q[k] <= '0; end else begin if (aw_hs) begin aw_full <= 1'b1; aw_word <= s_axil_awaddr[ADDR_W-1:2]; end if (w_hs) begin w_full <= 1'b1; w_data <= s_axil_wdata; w_strb <= s_axil_wstrb; end if (do_write) begin aw_full <= 1'b0; w_full <= 1'b0; s_axil_bvalid <= 1'b1; s_axil_bresp <= mapped(aw_word) ? OKAY : SLVERR; if (int'(aw_word) == A_CTRL && w_strb[0]) begin enable_q <= w_data[0]; start_o <= w_data[1]; end for (int k = 0; k < N_COEF; k++) begin if (int'(aw_word) == A_COEF0 + k) coef_q[k] <= apply_strb(coef_q[k], w_data, w_strb); end end else if (s_axil_bvalid && s_axil_bready) begin s_axil_bvalid <= 1'b0; end end end // ------------------------------------------------------------------ // Read path: one read in flight // ------------------------------------------------------------------ logic [WW-1:0] ar_word; logic [31:0] rd_mux; logic ar_hs; assign s_axil_arready = !s_axil_rvalid; assign ar_hs = s_axil_arvalid && s_axil_arready; assign ar_word = s_axil_araddr[ADDR_W-1:2]; always_comb begin rd_mux = '0; case (int'(ar_word)) A_CTRL: rd_mux = {31'd0, enable_q}; A_STAT: rd_mux = status_i; A_ID: rd_mux = ID; default: begin for (int k = 0; k < N_COEF; k++) begin if (int'(ar_word) == A_COEF0 + k) rd_mux = coef_q[k]; end end endcase end always_ff @(posedge aclk) begin if (!aresetn) begin s_axil_rvalid <= 1'b0; s_axil_rdata <= '0; s_axil_rresp <= OKAY; end else if (ar_hs) begin s_axil_rvalid <= 1'b1; s_axil_rdata <= rd_mux; s_axil_rresp <= mapped(ar_word) ? OKAY : SLVERR; end else if (s_axil_rvalid && s_axil_rready) begin s_axil_rvalid <= 1'b0; end end endmodule