// lfsr.sv // Galois linear feedback shift register, shifting right. // // Each enabled clock: if the bit leaving at state[0] is 1, the shifted state // is XORed with MASK; otherwise it is only shifted. MASK has bit (t-1) set // for every tap t of the feedback polynomial, using the tap numbering of // Xilinx XAPP052 (bits 1..W). Examples of maximal-length masks: // W=8 taps 8,6,5,4 MASK = 8'hB8 // W=16 taps 16,15,13,4 MASK = 16'hD008 // W=32 taps 32,22,2,1 MASK = 32'h80200003 // With a maximal-length mask the state visits all 2**W - 1 non-zero values. // The all-zero state is a lock-up state, so SEED must not be zero. `timescale 1ns/1ps module lfsr #( parameter int W = 16, parameter logic [W-1:0] MASK = 16'hD008, parameter logic [W-1:0] SEED = 1 ) ( input logic clk, input logic rst, // synchronous, active high: load SEED input logic en, output logic [W-1:0] state, output logic bit_out // pseudo-random bit stream (state[0]) ); always_ff @(posedge clk) begin if (rst) begin state <= SEED; end else if (en) begin state <= state[0] ? ((state >> 1) ^ MASK) : (state >> 1); end end assign bit_out = state[0]; endmodule