-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlfsr.sv
54 lines (41 loc) · 1.2 KB
/
lfsr.sv
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
/*
* extended from example on verilog shfit register:
* http://www.referencedesigner.com/tutorials/verilog/verilog_32.php
*
*/
/*
* Generate a 10 bit random number using an LFSR, Using 10 bits makes it more random
* Out put the last 2 bits since we only need 2 bits to represent our 4 different shapes
Will constantly cycle, and will be assigned when creating a new block
*/
module lfsr (clk, reset, out);
input logic clk, reset;
output logic [1:0] out;
logic [9:0] ps, ns;
always_ff @(posedge clk) begin
if (reset)
ps <= 0;
else
ps <= ns;
end
assign ns = {ps[8:0] , ~(ps[9]^ps[6]) };
assign out = ps[1:0];
endmodule
module lfsr_testbench ();
logic clk, reset;
logic [1:0] out;
lfsr dut (.clk, .reset, .out);
// Set up a simulated clock.
parameter CLOCK_PERIOD=100;
initial begin
clk <= 0;
// Forever toggle the clock
forever #(CLOCK_PERIOD/2) clk <= ~clk;
end
initial begin
reset <= 1; @(posedge clk); // Always reset FSMs at start
reset <= 0; @(posedge clk); // count = 0
repeat(15) @(posedge clk);
$stop; // End the simulation.
end
endmodule