// tb_axil_regs.sv // Self-checking testbench for axil_regs.sv. A small AXI4-Lite manager is // written as tasks. AW and W are driven by separate threads, so a write can // present them in the same cycle or in either order. BREADY and RREADY can be // held low for a number of cycles to apply back-pressure. // // Timing convention: inputs change on the falling clock edge, and READY/VALID // are checked there too. A channel handshakes at the next rising edge when // both VALID and READY are high at the falling edge before it. // // Run: iverilog -g2012 -o sim tb_axil_regs.sv axil_regs.sv && vvp sim `timescale 1ns/1ps module tb_axil_regs; localparam int ADDR_W = 6; localparam int N_COEF = 8; localparam logic [31:0] ID = 32'h4649_5231; localparam logic [1:0] OKAY = 2'b00, SLVERR = 2'b10; logic aclk = 1'b0; logic aresetn = 1'b0; always #5 aclk = ~aclk; // 100 MHz logic [ADDR_W-1:0] awaddr = '0, araddr = '0; logic [2:0] awprot = '0, arprot = '0; logic awvalid = 1'b0, wvalid = 1'b0, bready = 1'b0; logic arvalid = 1'b0, rready = 1'b0; logic [31:0] wdata = '0; logic [3:0] wstrb = '0; logic awready, wready, bvalid, arready, rvalid; logic [1:0] bresp, rresp; logic [31:0] rdata; logic enable_o, start_o; logic [31:0] status_i = 32'h0; logic [N_COEF*32-1:0] coef_o; axil_regs #(.ADDR_W(ADDR_W), .N_COEF(N_COEF), .ID(ID)) dut ( .aclk, .aresetn, .s_axil_awaddr(awaddr), .s_axil_awprot(awprot), .s_axil_awvalid(awvalid), .s_axil_awready(awready), .s_axil_wdata(wdata), .s_axil_wstrb(wstrb), .s_axil_wvalid(wvalid), .s_axil_wready(wready), .s_axil_bresp(bresp), .s_axil_bvalid(bvalid), .s_axil_bready(bready), .s_axil_araddr(araddr), .s_axil_arprot(arprot), .s_axil_arvalid(arvalid), .s_axil_arready(arready), .s_axil_rdata(rdata), .s_axil_rresp(rresp), .s_axil_rvalid(rvalid), .s_axil_rready(rready), .enable_o, .start_o, .status_i, .coef_o ); int errors = 0; int checks = 0; int start_pulses = 0; always @(posedge aclk) if (start_o) start_pulses++; // Protocol check: once BVALID or RVALID is high, it stays high with the // same payload until the manager accepts it. logic b_wait = 1'b0, r_wait = 1'b0; logic [1:0] b_prev; logic [33:0] r_prev; always @(posedge aclk) begin if (b_wait && !(bvalid && bresp == b_prev)) begin $display("ERROR: BVALID or BRESP changed before the handshake at %0t", $time); errors++; end if (r_wait && !(rvalid && {rresp, rdata} == r_prev)) begin $display("ERROR: RVALID or RDATA changed before the handshake at %0t", $time); errors++; end b_wait <= bvalid && !bready; b_prev <= bresp; r_wait <= rvalid && !rready; r_prev <= {rresp, rdata}; end task automatic check(input string what, input logic [31:0] got, input logic [31:0] exp); checks++; if (got !== exp) begin $display("ERROR: %s: got 0x%08h, expected 0x%08h", what, got, exp); errors++; end endtask // One write. aw_dly / w_dly: falling edges to wait before raising AWVALID / WVALID. // b_dly: falling edges to wait before raising BREADY. task automatic axi_write(input logic [ADDR_W-1:0] addr, input logic [31:0] data, input logic [3:0] strb, input int aw_dly, input int w_dly, input int b_dly, output logic [1:0] resp); fork begin repeat (aw_dly) @(negedge aclk); awaddr = addr; awvalid = 1'b1; while (!awready) @(negedge aclk); @(negedge aclk); awvalid = 1'b0; end begin repeat (w_dly) @(negedge aclk); wdata = data; wstrb = strb; wvalid = 1'b1; while (!wready) @(negedge aclk); @(negedge aclk); wvalid = 1'b0; end begin repeat (b_dly) @(negedge aclk); bready = 1'b1; while (!bvalid) @(negedge aclk); resp = bresp; @(negedge aclk); bready = 1'b0; end join endtask task automatic axi_read(input logic [ADDR_W-1:0] addr, input int r_dly, output logic [31:0] data, output logic [1:0] resp); @(negedge aclk); araddr = addr; arvalid = 1'b1; while (!arready) @(negedge aclk); @(negedge aclk); arvalid = 1'b0; repeat (r_dly) @(negedge aclk); rready = 1'b1; while (!rvalid) @(negedge aclk); data = rdata; resp = rresp; @(negedge aclk); rready = 1'b0; endtask logic [31:0] rd; logic [1:0] rsp; logic [31:0] expect_coef [N_COEF]; initial begin if ($test$plusargs("dump")) begin $dumpfile("tb_axil_regs.vcd"); $dumpvars(0, tb_axil_regs); end repeat (3) @(negedge aclk); aresetn = 1'b1; if (bvalid || rvalid) begin $display("ERROR: BVALID or RVALID high after reset"); errors++; end // 1. Values after reset axi_read(6'h08, 0, rd, rsp); check("ID", rd, ID); check("ID resp", 32'(rsp), 32'(OKAY)); axi_read(6'h00, 0, rd, rsp); check("CTRL after reset", rd, 32'h0); axi_read(6'h10, 0, rd, rsp); check("COEF0 after reset", rd, 32'h0); // 2. Coefficients, with three channel orderings and BREADY back-pressure for (int k = 0; k < N_COEF; k++) begin expect_coef[k] = 32'h1000_0000 * (k + 1) + 32'(k * 17); case (k % 3) 0: axi_write(6'(16 + 4*k), expect_coef[k], 4'hF, 0, 0, k, rsp); // AW and W together 1: axi_write(6'(16 + 4*k), expect_coef[k], 4'hF, 0, 3, k, rsp); // AW first default: axi_write(6'(16 + 4*k), expect_coef[k], 4'hF, 3, 0, k, rsp); // W first endcase check($sformatf("write COEF%0d resp", k), 32'(rsp), 32'(OKAY)); end for (int k = 0; k < N_COEF; k++) begin axi_read(6'(16 + 4*k), k % 4, rd, rsp); // RREADY back-pressure check($sformatf("COEF%0d", k), rd, expect_coef[k]); check($sformatf("coef_o[%0d]", k), coef_o[32*k +: 32], expect_coef[k]); end // 3. Byte strobes: only bytes 0 and 2 change axi_write(6'h10, 32'h1122_3344, 4'hF, 0, 0, 0, rsp); axi_write(6'h10, 32'hAABB_CCDD, 4'b0101, 0, 0, 0, rsp); axi_read(6'h10, 0, rd, rsp); check("COEF0 after WSTRB=0101", rd, 32'h11BB_33DD); // 4. CTRL: ENABLE is a level, START is a single-cycle pulse that reads 0 start_pulses = 0; axi_write(6'h00, 32'h3, 4'hF, 0, 0, 0, rsp); repeat (3) @(negedge aclk); check("START pulses", 32'(start_pulses), 32'd1); check("enable_o", 32'(enable_o), 32'd1); axi_read(6'h00, 0, rd, rsp); check("CTRL readback", rd, 32'h1); axi_write(6'h00, 32'h0, 4'hF, 0, 0, 0, rsp); check("enable_o after clear", 32'(enable_o), 32'd0); check("START pulses after clear", 32'(start_pulses), 32'd1); // 5. STATUS follows status_i; a write to it is ignored with OKAY status_i = 32'hCAFE_0002; axi_read(6'h04, 0, rd, rsp); check("STATUS", rd, 32'hCAFE_0002); axi_write(6'h04, 32'h0, 4'hF, 0, 0, 0, rsp); check("write STATUS resp", 32'(rsp), 32'(OKAY)); axi_read(6'h04, 0, rd, rsp); check("STATUS after write", rd, 32'hCAFE_0002); // 6. Unmapped addresses return SLVERR and do not change anything axi_write(6'h0C, 32'hFFFF_FFFF, 4'hF, 0, 0, 2, rsp); check("write 0x0C resp", 32'(rsp), 32'(SLVERR)); axi_read(6'h0C, 0, rd, rsp); check("read 0x0C resp", 32'(rsp), 32'(SLVERR)); check("read 0x0C data", rd, 32'h0); axi_read(6'h3C, 1, rd, rsp); check("read 0x3C resp", 32'(rsp), 32'(SLVERR)); axi_read(6'h14, 0, rd, rsp); check("COEF1 unchanged", rd, expect_coef[1]); repeat (2) @(negedge aclk); if (errors == 0) $display("PASS: %0d checks", checks); else $display("FAIL: %0d errors in %0d checks", errors, checks); $finish; end initial begin #200000; $display("ERROR: timeout"); $finish; end endmodule