PRBS Generator Verilog Code and Test Bench
Advertisement
This page provides the Verilog code for a PRBS (Pseudo-Random Binary Sequence) generator and a corresponding test bench script.
PRBS Generator Verilog Code
The following is the Verilog code for a PRBS generator:
module prbs (rand, clk, reset);
input clk, reset;
output rand;
wire rand;
reg [3:0] temp;
always @ (posedge reset) begin
temp <= 4'hf;
end
always @ (posedge clk) begin
if (~reset) begin
temp <= {temp[0]^temp[1],temp[3],temp[2],temp[1]};
end
end
assign rand = temp[0];
endmodule
Explanation:
- The module
prbshas inputsclk(clock) andreset, and an outputrand(random bit). tempis a 4-bit register that holds the state of the PRBS generator.- The
always @ (posedge reset)block initializes thetempregister to4'hf(1111 in binary) when theresetsignal is asserted (positive edge). - The
always @ (posedge clk)block updates thetempregister on each rising edge of the clock. - The core logic of the PRBS generator is within the
if (~reset)statement. It implements a linear feedback shift register (LFSR) with a specific feedback tap. The next state oftempis derived by XORingtemp[0]andtemp[1]and shifting the bits. - The
assign rand = temp[0]statement assigns the value of the least significant bit oftempto the outputrand. This provides the pseudo-random bit.
PRBS Generator Test Script Code
Here’s the test script code for the PRBS generator:
module main;
reg clk, reset;
wire rand;
prbs pr (rand, clk, reset);
initial begin
forever begin
clk <= 0;
#5 clk <= 1;
#5 clk <= 0;
end
end
initial begin
reset = 1;
#12 reset = 0;
#90 reset = 1;
#12 reset = 0;
end
endmodule
Explanation:
- The
mainmodule instantiates theprbsmodule. clkandresetare declared asregbecause their values are driven by the test bench.randis awirebecause it’s an output from theprbsmodule.- The first
initialblock generates a clock signal with a period of 10 time units (5 for the low phase and 5 for the high phase). - The second
initialblock generates a reset signal. It asserts the reset for 12 time units, de-asserts it for 90 time units, asserts it again for 12 time units, and then de-asserts it again. This provides a couple of reset cycles for testing the PRBS generator.
Advertisement
RF