// tb_lfsr.sv // Self-checking testbench: for every width from 3 to 16, a Galois LFSR with // the XAPP052 taps must return to its seed after exactly 2**W - 1 clocks and // never reach the all-zero state. `timescale 1ns/1ps module tb_lfsr; logic clk = 1'b0; logic rst = 1'b1; always #5 clk = ~clk; // Masks, bit (t-1) set for each tap t of XAPP052 Table 3. function automatic logic [15:0] mask_for(input int w); case (w) 3: return 16'h0006; // 3,2 4: return 16'h000C; // 4,3 5: return 16'h0014; // 5,3 6: return 16'h0030; // 6,5 7: return 16'h0060; // 7,6 8: return 16'h00B8; // 8,6,5,4 9: return 16'h0110; // 9,5 10: return 16'h0240; // 10,7 11: return 16'h0500; // 11,9 12: return 16'h0829; // 12,6,4,1 13: return 16'h100D; // 13,4,3,1 14: return 16'h2015; // 14,5,3,1 15: return 16'h6000; // 15,14 16: return 16'hD008; // 16,15,13,4 default: return 16'h0000; endcase endfunction int errors = 0; logic [16:3] done; for (genvar w = 3; w <= 16; w++) begin : g_w localparam logic [w-1:0] M = mask_for(w); logic [w-1:0] st; logic b; int count = 0; logic seen_zero = 1'b0; lfsr #(.W(w), .MASK(M), .SEED(1)) dut ( .clk(clk), .rst(rst), .en(1'b1), .state(st), .bit_out(b) ); initial done[w] = 1'b0; always @(posedge clk) begin if (!rst && !done[w]) begin count <= count + 1; if (st == '0) seen_zero <= 1'b1; if (count > 0 && st == 1) begin done[w] <= 1'b1; if (count != (1 << w) - 1 || seen_zero) begin $display("ERROR: W=%0d period %0d, expected %0d", w, count, (1 << w) - 1); errors++; end else begin $display("W=%0d period %0d", w, count); end end end end end initial begin repeat (2) @(posedge clk); rst <= 1'b0; wait (&done); repeat (2) @(posedge clk); if (errors == 0) $display("PASS: 14 widths, maximal length"); else $display("FAIL: %0d errors", errors); $finish; end initial begin #2_000_000; $display("FAIL: timeout"); $finish; end endmodule