# Pseudo Random binary sequence (PRBS) and their Verilog implementation using a linear feedback shift register (LFSR) ultimately to connect to an SFI5 interface.

**A PRBS sequence often come in handy in FPGA work. They're almost perfectly balanced in other words the duty cycle or one to zero ratio is very close to even. Although the pattern is known and predictable, it makes a good substitute for a true random number. In this discussion we will be talking about 'maximal LFSR' which produce a maximum length sequence. A PRBS is sometimes also referred to as a Pseudo random number sequence or PN. Usually the sequence is referred to by it's length such as a PN7 has a pattern length of 2 ^{7}-1**

So lets start with an example for a PN7. To generate an LFSR we need a shift register and one or a few xor gates.

You can check out the Xilinx App Note for a similar approach using their SRLs. But we have more, so keep reading.module prbs_generate ( // Outputs prbs, // Inputs clk, reset ); output prbs; input clk, reset; parameter PN = 7, TAP1 = 6, TAP2 = 5; reg [PN-1:0] prbs_state; wire prbs; integer d; assign prbs = prbs_state[PN-1]; always @ (posedge clk) if (reset) prbs_state <= 1; //anything but the all 0s case is fine. else prbs_state <= {prbs_state,prbs_state[TAP1]^prbs_state[TAP2]}; endmodule // prbs_generate

This code generates a PN7 by taking two 'taps' as they're called from the shift register, exclusive oring them together and feeding them back into the shift register. Left to run on it's own, it will generate the a 127 bit long pattern, and then repeat. In that pattern we have 64 1s and 63 0s. It is always one zero that is missing from the perfect balance. The pattern also contains all of the 7 bit combinations, except the all 0s case. This gives us a maximum run length of 1s as 7 bits, and the maximum run length of 0s as 6 bits. This is useful to know if you are working on a link that has a run length limit due to AC coupling for example. The all 0s case is a special case that should be avoided as if you start there you will never leave.