-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathconfigurable_prng.v
More file actions
executable file
·59 lines (50 loc) · 2.14 KB
/
Copy pathconfigurable_prng.v
File metadata and controls
executable file
·59 lines (50 loc) · 2.14 KB
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
55
56
57
58
59
module configurable_prng #(
parameter WIDTH = 16, // Width of the PRNG output
parameter [WIDTH-1:0] SEED = 16'hACE1, // Default seed value (non-zero)
parameter [WIDTH-1:0] TAPS = 16'hB400, // Configurable taps for maximum period LFSR
parameter MODE = 0 // 0: Fibonacci (external XOR), 1: Galois (internal XOR)
)(
input wire clk, // Clock signal
input wire rst_n, // Active-low reset
input wire enable, // Enable signal for PRNG operation
input wire reseed, // Trigger to load seed value
input wire [WIDTH-1:0] seed_in, // External seed input
output wire [WIDTH-1:0] random, // Random output
output wire valid // Valid output flag
);
// Internal state register
reg [WIDTH-1:0] lfsr_reg;
reg valid_reg;
// Output assignment
assign random = lfsr_reg;
assign valid = valid_reg;
// Seed cannot be zero for an LFSR
wire [WIDTH-1:0] actual_seed = (seed_in == {WIDTH{1'b0}}) ? SEED : seed_in;
// Determine feedback bit based on current state and taps
// For Fibonacci LFSR
wire feedback_fibonacci = ^(lfsr_reg & TAPS);
// Next state calculation
wire [WIDTH-1:0] next_state_fibonacci = {lfsr_reg[WIDTH-2:0], feedback_fibonacci};
// For Galois LFSR
wire [WIDTH-1:0] next_state_galois = (lfsr_reg[0]) ?
((lfsr_reg >> 1) ^ (TAPS)) :
(lfsr_reg >> 1);
// Select next state based on mode
wire [WIDTH-1:0] next_state = (MODE == 0) ? next_state_fibonacci : next_state_galois;
// State update logic
always @(posedge clk or negedge rst_n) begin
if (!rst_n) begin
// Reset to seed value
lfsr_reg <= SEED;
valid_reg <= 1'b0;
end else if (reseed) begin
// Reseed with input value
lfsr_reg <= actual_seed;
valid_reg <= 1'b0;
end else if (enable) begin
// Update state
lfsr_reg <= next_state;
valid_reg <= 1'b1;
end
end
endmodule