SlideShare a Scribd company logo
ECE 4514
Digital Design II
Spring 2008
Lecture 6:
A Random Number Generator
Patrick Schaumont
Spring 2008
ECE 4514 Digital Design II
Lecture 6: A Random Number Generator in Verilog
Lecture 6:
A Random Number Generator
in Verilog
A Design Lecture
Patrick Schaumont
What is a random number generator?
Random
Number
Generator
11, 86, 82, 52, 60, 46, 64, 10, 98, 2, ...
Patrick Schaumont
Spring 2008
ECE 4514 Digital Design II
Lecture 6: A Random Number Generator in Verilog
Generator
What do I do with randomness?
Play games!
Have the monsters appear in different rooms every time
Do statistical simulations
Simulate customers in a shopping center (find the best spot
for a new Chuck E Cheese)
Run security protocols
Patrick Schaumont
Spring 2008
ECE 4514 Digital Design II
Lecture 6: A Random Number Generator in Verilog
Make protocol resistent against replay
Encrypt documents
Use random numbers as key stream
Encrypt Documents
Random
Number
Generator
XOR
stream of bytes
plaintext
encrypted stream of bytes
cryptext
Patrick Schaumont
Spring 2008
ECE 4514 Digital Design II
Lecture 6: A Random Number Generator in Verilog
Generator
XOR
cryptext
decrypted stream of bytes
plaintext
'one-time pad'
Random numbers by physical methods
Use dice, coin flips, roulette
Use thermal noise (diodes and resistors)
Use clock jitter (use ring oscillators)
Use radioactive decay
Use Lava Lamps
Patrick Schaumont
Spring 2008
ECE 4514 Digital Design II
Lecture 6: A Random Number Generator in Verilog
Use Lava Lamps
Patented!
Random numbers by computational methods
Not truly random, but pseudo random
meaning, after some time the same sequence returns
Linear Congruential Generator
x(n+1) = [ a.x(b) + b ] mod m
Eg. a = 15, b = 5, m = 7
a, b, m must be chosen carefully!
Patrick Schaumont
Spring 2008
ECE 4514 Digital Design II
Lecture 6: A Random Number Generator in Verilog
Eg. a = 15, b = 5, m = 7
X(0) = 1
X(1) = (15 + 5) mod 7 = 6
x(2) = (15*6 + 5) mod 7 = 4
x(3) = 2
x(4) = 0
x(5) = 5
x(6) = 3
x(7) = 1
x(8) = ...
a, b, m must be chosen carefully!
for a maximum lenth sequence
A quick way to generate random numbers
Verilog has a buildin random number generator
module random(q);
output [0:31] q;
reg [0:31] q;
initial
r_seed = 2;
Patrick Schaumont
Spring 2008
ECE 4514 Digital Design II
Lecture 6: A Random Number Generator in Verilog
r_seed = 2;
always
#10 q = $random(r_seed);
endmodule
Nice, but only for testbenches …
Instead, we want an hardware implementation
Linear Feedback Shift Register
Pseudo Random Numbers in Digital Hardware
shift register
Patrick Schaumont
Spring 2008
ECE 4514 Digital Design II
Lecture 6: A Random Number Generator in Verilog
feedback network
Linear Feedback Shift Register
All zeroes
not very useful ...
0 0 0 0
Patrick Schaumont
Spring 2008
ECE 4514 Digital Design II
Lecture 6: A Random Number Generator in Verilog
0
Linear Feedback Shift Register
Non-zero state is more interesting
0 0 0 1
Patrick Schaumont
Spring 2008
ECE 4514 Digital Design II
Lecture 6: A Random Number Generator in Verilog
1
1
Linear Feedback Shift Register
Non-zero state is more interesting
1 0 0 0
Patrick Schaumont
Spring 2008
ECE 4514 Digital Design II
Lecture 6: A Random Number Generator in Verilog
0
10
Linear Feedback Shift Register
Non-zero state is more interesting
0 1 0 0
Patrick Schaumont
Spring 2008
ECE 4514 Digital Design II
Lecture 6: A Random Number Generator in Verilog
0
100
Linear Feedback Shift Register
Non-zero state is more interesting
0 0 1 0
Patrick Schaumont
Spring 2008
ECE 4514 Digital Design II
Lecture 6: A Random Number Generator in Verilog
1
1001
Linear Feedback Shift Register
Non-zero state is more interesting
1 0 0 1
Patrick Schaumont
Spring 2008
ECE 4514 Digital Design II
Lecture 6: A Random Number Generator in Verilog
1
10011
Linear Feedback Shift Register
Non-zero state is more interesting
0 1 0 0
Patrick Schaumont
Spring 2008
ECE 4514 Digital Design II
Lecture 6: A Random Number Generator in Verilog
0
100110
Linear Feedback Shift Register
Non-zero state is more interesting
0 0 1 0
Patrick Schaumont
Spring 2008
ECE 4514 Digital Design II
Lecture 6: A Random Number Generator in Verilog
1
1001101
etc ...
Linear Feedback Shift Register
This is actually a finite state machine
1 0 0 1
State Encoding
Patrick Schaumont
Spring 2008
ECE 4514 Digital Design II
Lecture 6: A Random Number Generator in Verilog
1
Linear Feedback Shift Register
This is actually a finite state machine
0 0 0 1
Patrick Schaumont
Spring 2008
ECE 4514 Digital Design II
Lecture 6: A Random Number Generator in Verilog
0001 1000 0100 0010 . . .
How many states will you see?
Linear Feedback Shift Register
15 states
0 0 0 1
Patrick Schaumont
Spring 2008
ECE 4514 Digital Design II
Lecture 6: A Random Number Generator in Verilog
0001 1000 0100 0010 1001 1100 0110 1011
0101 1010 1101 1110 1111 0111 0011
Linear Feedback Shift Register
We can specify an LFSR by means of the
characteristic polynomial (also called feedback
polynomial)
X^1 X^2 X^3 X^4
Patrick Schaumont
Spring 2008
ECE 4514 Digital Design II
Lecture 6: A Random Number Generator in Verilog
P(x) = x^4 + x^3 + 1
There exists elaborate finite-field math to analyze the properties of
an LFSR - outside of the scope of this class
Linear Feedback Shift Register
So, knowing the polynomial you can also draw the
LFSR
P(x) = x^8 + x^6 + x^5 + x^4 + 1
How many taps ?
How many 2-input XOR?
Patrick Schaumont
Spring 2008
ECE 4514 Digital Design II
Lecture 6: A Random Number Generator in Verilog
How many 2-input XOR?
Linear Feedback Shift Register
So, knowing the polynomial you can also draw the
LFSR
P(x) = x^8 + x^6 + x^5 + x^4 + 1
How many taps ? 8
How many 2-input XOR?
Patrick Schaumont
Spring 2008
ECE 4514 Digital Design II
Lecture 6: A Random Number Generator in Verilog
How many 2-input XOR?
Linear Feedback Shift Register
So, knowing the polynomial you can also draw the
LFSR
P(x) = x^8 + x^6 + x^5 + x^4 + 1
How many taps ? 8
How many 2-input XOR? 3
Patrick Schaumont
Spring 2008
ECE 4514 Digital Design II
Lecture 6: A Random Number Generator in Verilog
How many 2-input XOR? 3
Linear Feedback Shift Register
Certain polynomials generate very long state
sequences. These are called maximal-length LFSR.
P(X) = x^153 + x^152 + 1
Patrick Schaumont
Spring 2008
ECE 4514 Digital Design II
Lecture 6: A Random Number Generator in Verilog
P(X) = x^153 + x^152 + 1
is a maximum-length feedback polynomial
State machine with 2 ^ 153 -1 states ..
Fibonacci and Galois LFSR
This format is called a Fibonacci LFSR
1 2 3 4
Fibonacci
~1175-1250
Patrick Schaumont
Spring 2008
ECE 4514 Digital Design II
Lecture 6: A Random Number Generator in Verilog
Can be converted to an equivalent Galois LFSR
4 3 2 1
Evariste
Galois
1811-1832
Fibonacci and Galois LFSR
Each Fibonacci LFSR can transform into Galois LFSR:
Reverse numbering of taps
Make XOR inputs XOR outputs and vice versa
Example: starting with this Fibonacci LFSR
Patrick Schaumont
Spring 2008
ECE 4514 Digital Design II
Lecture 6: A Random Number Generator in Verilog
1 2 3 4 5 6 7 8
Fibonacci and Galois LFSR
Disconnect XOR inputs
Reverse tap numbering (not the direction of shifting!)
Patrick Schaumont
Spring 2008
ECE 4514 Digital Design II
Lecture 6: A Random Number Generator in Verilog
8 7 6 5 4 3 2 1
Fibonacci and Galois LFSR
Turn XOR inputs into XOR outputs and vice versa
Patrick Schaumont
Spring 2008
ECE 4514 Digital Design II
Lecture 6: A Random Number Generator in Verilog
8 7 6 5 4 3 2 1
Which one is better for digital hardware?
Fibonacci
1 2 3 4 5 6 7 8
Patrick Schaumont
Spring 2008
ECE 4514 Digital Design II
Lecture 6: A Random Number Generator in Verilog
8 7 6 5 4 3 2 1
Galois
Which one is better for digital hardware?
Fibonacci
1 2 3 4 5 6 7 8
Patrick Schaumont
Spring 2008
ECE 4514 Digital Design II
Lecture 6: A Random Number Generator in Verilog
8 7 6 5 4 3 2 1
Galois computes all taps in parallel
Which one is better for software?
Fibonacci
1 2 3 4 5 6 7 8
Patrick Schaumont
Spring 2008
ECE 4514 Digital Design II
Lecture 6: A Random Number Generator in Verilog
8 7 6 5 4 3 2 1
Galois
Which one is better for software?
Fibonacci
1 2 3 4 5 6 7 8
Patrick Schaumont
Spring 2008
ECE 4514 Digital Design II
Lecture 6: A Random Number Generator in Verilog
8 7 6 5 4 3 2 1
Galois
char_v = (char_v >> 1) ^ (-(signed char) (char_v & 1) & 0xe)
Let's write an LFSR in Verilog
xor(out, in1, in2)
Patrick Schaumont
Spring 2008
ECE 4514 Digital Design II
Lecture 6: A Random Number Generator in Verilog
FF You need to build this one
(structural, behavioral)
A Flip flop
module flipflop(q, clk, rst, d);
input clk;
input rst;
input d;
output q;
reg q;
always @(posedge clk or posedge rst)
Patrick Schaumont
Spring 2008
ECE 4514 Digital Design II
Lecture 6: A Random Number Generator in Verilog
always @(posedge clk or posedge rst)
begin
if (rst)
q = 0;
else
q = d;
end
endmodule
A Flip flop
Setup Time:
Time D has to be stable before a clock edge
Hold Time:
Time D has to be stable after clock edge
Propagation Delay:
Delay from clock edge to Q
Patrick Schaumont
Spring 2008
ECE 4514 Digital Design II
Lecture 6: A Random Number Generator in Verilog
Delay from clock edge to Q
Delay from reset to Q
How to specify propagation delay ?
A Flip flop
module flipflop(q, clk, rst, d);
input clk;
input rst;
input d;
output q;
reg q;
always @(posedge clk or posedge rst)
Patrick Schaumont
Spring 2008
ECE 4514 Digital Design II
Lecture 6: A Random Number Generator in Verilog
always @(posedge clk or posedge rst)
begin
if (rst)
q = 0;
else
q = d;
end
endmodule
How to specify propagation delay ?
A Flip flop
module flipflop(q, clk, rst, d);
input clk;
input rst;
input d;
output q;
reg q;
always @(posedge clk or posedge rst)
begin
if (rst)
Patrick Schaumont
Spring 2008
ECE 4514 Digital Design II
Lecture 6: A Random Number Generator in Verilog
if (rst)
#2 q = 0;
else
q = #3 d;
end
specify
$setup(d, clk, 2);
$hold(clk, d, 0);
endspecify
endmodule
See Chapter 10
Palnitkar
Test setup, hold
Let's turn the LFSR into a module
1 2 3 4
How to program this?
Patrick Schaumont
Spring 2008
ECE 4514 Digital Design II
Lecture 6: A Random Number Generator in Verilog
output
(1 bit)
Let's turn the LFSR into a module
seed (4 bit)
load (1 bit)
1 1 1 1
Patrick Schaumont
Spring 2008
ECE 4514 Digital Design II
Lecture 6: A Random Number Generator in Verilog
1 2 3 4
output
(1 bit)
0
1
0 0 0
Multiplexer symbol
1
a
control
if (control)
out = a;
module mux(q, control, a, b);
output q;
reg q;
input control, a, b;
wire notcontrol;
Patrick Schaumont
Spring 2008
ECE 4514 Digital Design II
Lecture 6: A Random Number Generator in Verilog
0
b
out = a;
else
out = b;
bit multiplexer
wire notcontrol;
always @(control or
notcontrol or
a or b)
q = (control & a) |
(notcontrol & b);
not (notcontrol, control);
endmodule;
LFSR, structural
1 2 3 4
output (1 bit)
seed (4 bit)
load (1 bit)
1
0
1
0
1
0
1
0
Patrick Schaumont
Spring 2008
ECE 4514 Digital Design II
Lecture 6: A Random Number Generator in Verilog
module lfsr(q, clk, rst, seed, load);
...
wire [3:0] state_out;
wire [3:0] state_in;
flipflop F[3:0] (state_out, clk, rst, state_in);
endmodule
LFSR, structural
1 2 3 4
output (1 bit)
seed (4 bit)
load (1 bit)
1
0
1
0
1
0
1
0
module lfsr(q, clk, rst, seed, load);
...
Patrick Schaumont
Spring 2008
ECE 4514 Digital Design II
Lecture 6: A Random Number Generator in Verilog
...
wire [3:0] state_out;
wire [3:0] state_in;
wire nextbit;
xor G1(nextbit, state_out[2], state_out[3]);
assign q = nextbit;
endmodule
LFSR, structural
1 2 3 4
output (1 bit)
seed (4 bit)
load (1 bit)
1
0
1
0
1
0
1
0
module lfsr(q, clk, rst, seed, load);
...
Patrick Schaumont
Spring 2008
ECE 4514 Digital Design II
Lecture 6: A Random Number Generator in Verilog
...
wire [3:0] state_out;
wire [3:0] state_in;
wire nextbit;
mux M1[3:0] (state_in, load, seed, {state_out[2],
state_out[1],
state_out[0],
nextbit});
assign q = nextbit;
endmodule
LFSR module - complete
module lfsr(q, clk, rst, seed, load);
output q;
input [3:0] seed;
input load;
input rst;
wire [3:0] state_out;
wire [3:0] state_in;
Patrick Schaumont
Spring 2008
ECE 4514 Digital Design II
Lecture 6: A Random Number Generator in Verilog
flipflop F[3:0] (state_out, clk, rst, state_in);
mux M1[3:0] (state_in, load, seed, {state_out[2],
state_out[1],
state_out[0],
nextbit});
xor G1(nextbit, state_out[2], state_out[3]);
assign q = nextbit;
endmodule
LFSR testbench
module lfsrtst;
reg clk;
reg rst;
reg [3:0] seed;
reg load;
wire q;
lfsr L(q, clk, rst,
seed, load);
// initialization
// drive clock
always
#50 clk = !clk;
// program lfsr
initial begin
#100 seed = 4'b0001;
load = 1;
#100 load = 0;
end
Patrick Schaumont
Spring 2008
ECE 4514 Digital Design II
Lecture 6: A Random Number Generator in Verilog
// initialization
// apply reset pulse
initial
begin
clk = 0;
load = 0;
seed = 0;
rst = 0;
#10 rst = 1;
#10 rst = 0;
end
end
endmodule
Simulation ..
Patrick Schaumont
Spring 2008
ECE 4514 Digital Design II
Lecture 6: A Random Number Generator in Verilog
Synthesis ..
Patrick Schaumont
Spring 2008
ECE 4514 Digital Design II
Lecture 6: A Random Number Generator in Verilog
Place and Route ..
Patrick Schaumont
Spring 2008
ECE 4514 Digital Design II
Lecture 6: A Random Number Generator in Verilog
Can a random number generator have flaws?
Patrick Schaumont
Spring 2008
ECE 4514 Digital Design II
Lecture 6: A Random Number Generator in Verilog
Can a random number generator have flaws?
Problem #1: it can have bias
Meaning: a certain number occurs more often then others
Expressed in the entropy rate of the generator
Entropy = true information rate (in bit/sec), can be lower then
the actual bitrate of the random number generator
Example: Assume an RNG that produces three events A,B,C
encoded with two bits
Patrick Schaumont
Spring 2008
ECE 4514 Digital Design II
Lecture 6: A Random Number Generator in Verilog
RNG
A
B
C
P(A) = 1/2
P(B) = 1/4
P(C) = 1/4
00
01
10
symbol probability bitpattern
So this bitstream has much more '0' then '1'. It has a bias.
E.g. ABACAABC... is encoded as 0001001000000110...
Can a random number generator have flaws?
Problem #1: it can have bias
Meaning: a certain number occurs more often then others
Expressed in the entropy rate of the generator
Entropy = true information rate (in bit/sec), can be lower then
the actual bitrate of the random number generator
Example: Assume an RNG that produces three events A,B,C
encoded with two bits
better
Patrick Schaumont
Spring 2008
ECE 4514 Digital Design II
Lecture 6: A Random Number Generator in Verilog
RNG
A
B
C
P(A) = 1/2
P(B) = 1/4
P(C) = 1/4
0
10
11
symbol probability
better
bitpattern
In this bitstream, the number of '1' and '0' are balanced.
E.g. ABACAABC... is encoded as 010011001011...
Can a random number generator have flaws?
Problem #1: it can have bias
Do LFSR have a bias ?
Patrick Schaumont
Spring 2008
ECE 4514 Digital Design II
Lecture 6: A Random Number Generator in Verilog
Can a random number generator have flaws?
Problem #1: it can have bias
Do LFSR have a bias ?
Yes, they have a small bias because the all-zero
state never appears.
However, for a very long LFSR, the bias becomes
negligible
Patrick Schaumont
Spring 2008
ECE 4514 Digital Design II
Lecture 6: A Random Number Generator in Verilog
negligible
Can a random number generator have flaws?
Problem #2: it can be predicted
Not when truly random phsysical phenomena
But, if it is a Pseudo RNG (like an LFSR), it is a
deterministic sequence.
Is this really a problem? Yes!
• Don't want to use a predictable RNG for dealing cards, driving a
Patrick Schaumont
Spring 2008
ECE 4514 Digital Design II
Lecture 6: A Random Number Generator in Verilog
• Don't want to use a predictable RNG for dealing cards, driving a
slot machine, ... (at least not if you own the place).
• Don't want to use predicatable RNG in security. Predicability =
weakness
Can a random number generator have flaws?
Problem #2: it can be predicted
The real issue for PRNG is: can the value of bit
N+1 be predicted when someone observes the first
N bits.
closed box
Patrick Schaumont
Spring 2008
ECE 4514 Digital Design II
Lecture 6: A Random Number Generator in Verilog
8 7 6 5 4 3 2 1
closed box
010111011..
The next one
will be .. 1 !
LFSR are very predictable ..
Predicting the next output bit is equivalent to knowing
the feedback pattern of the LFSR and the states of all
LFSR flip-flops.
Mathematicians (Berlekamp-Massey) found that:
Given an N-bit LFSR with unknown feedback pattern, then
only 2N bits are needed to predict bit 2N + 1
Patrick Schaumont
Spring 2008
ECE 4514 Digital Design II
Lecture 6: A Random Number Generator in Verilog
So let's say we have and 8-bit LFSR, then we need
only 16 bits of the RNG stream before it becomes
predicatble
LFSR are unsuited for everything that should be
unpredictable
To be unpredictable, the LFSR should be long
Solution 1
Use a maximal-length P(x) = x^N + ... + 1
with N >>> (E.g. 65,536)
Very expensive to make! 64K flip-flops ..
Solution 2: Non-linear Combination Generator
Patrick Schaumont
Spring 2008
ECE 4514 Digital Design II
Lecture 6: A Random Number Generator in Verilog
Solution 2: Non-linear Combination Generator
short LFSR1
short LFSR2
short LFSR3
non
linear
func
A
B
C
out
Eg.
out = AB ^ BC ^ C
Example design by Tkacik, 2002
Patrick Schaumont
Spring 2008
ECE 4514 Digital Design II
Lecture 6: A Random Number Generator in Verilog
Cellular Automata Shift Register
Example design by Tkacik, 2002
43-bit LFSR defined by
P(X) = X^43 + X^41 + X^20 + X + 1 => 3XOR, 43 taps
Maximal Length: 2^43-1
Bias ~ 2^-43 (because all-zero pattern cannot appear)
37-bit cellular automata shift register
Combines previous and next statereg into current state reg
Patrick Schaumont
Spring 2008
ECE 4514 Digital Design II
Lecture 6: A Random Number Generator in Verilog
Similar to 37 intertwined state machines (automata)
Maximal Length: 2^37-1
Bias ~ 2^-37
1 2 3
from 37 from 1 from 2
to 37
...
...
Sample Implementation
On opencores you can find an implementation of
Tkacik's design - assigned reading of today
(this design has a few minor differences with the spec written
by Tkacik - but OK for our purpose)
Patrick Schaumont
Spring 2008
ECE 4514 Digital Design II
Lecture 6: A Random Number Generator in Verilog
Module interface
module rng(clk,reset,loadseed_i,seed_i,number_o);
input clk;
input reset;
input loadseed_i;
input [31:0] seed_i;
output [31:0] number_o;
reg [31:0] number_o;
reg [42:0] LFSR_reg; // internal state
Patrick Schaumont
Spring 2008
ECE 4514 Digital Design II
Lecture 6: A Random Number Generator in Verilog
reg [42:0] LFSR_reg; // internal state
reg [36:0] CASR_reg; // internal state
always (.. CASR ..)
always (.. LFSR ..)
always (.. combine outputs ..)
endmodule
LFSR Part
reg[42:0] LFSR_varLFSR; // temporary working var
reg outbitLFSR; // temporary working var
always @(posedge clk or negedge reset)
begin
if (!reset )
begin
...
end
else
begin
Patrick Schaumont
Spring 2008
ECE 4514 Digital Design II
Lecture 6: A Random Number Generator in Verilog
begin
if (loadseed_i )
begin
...
end
else
begin
...
end
end
end
LFSR Part
reg[42:0] LFSR_varLFSR; // temporary working var
reg outbitLFSR; // temporary working var
always @(posedge clk or negedge reset)
begin
if (!reset )
begin
LFSR_reg = (1);
end
else
begin
assemble bits
Patrick Schaumont
Spring 2008
ECE 4514 Digital Design II
Lecture 6: A Random Number Generator in Verilog
begin
if (loadseed_i )
begin
LFSR_varLFSR [42:32]=0;
LFSR_varLFSR [31:0]=seed_i ;
LFSR_reg = (LFSR_varLFSR );
end
else
begin
...
end
end
end
LFSR_varLFSR
LFSR_reg
LFSR Part
reg[42:0] LFSR_varLFSR; // temporary working var
reg outbitLFSR; // temporary working var
always @(posedge clk or negedge reset)
begin
if (!reset )
else
begin
if (loadseed_i )
else
begin
LFSR_varLFSR
LFSR_reg
LFSR_varLFSR
Patrick Schaumont
Spring 2008
ECE 4514 Digital Design II
Lecture 6: A Random Number Generator in Verilog
begin
LFSR_varLFSR = LFSR_reg;
LFSR_varLFSR [42] = LFSR_varLFSR [41];
outbitLFSR = LFSR_varLFSR [42];
LFSR_varLFSR [42] = LFSR_varLFSR [41];
LFSR_varLFSR [41] = LFSR_varLFSR [40]^outbitLFSR ;
// some lines skipped ...
LFSR_varLFSR [0] = LFSR_varLFSR [42];
LFSR_reg = LFSR_varLFSR;
end
end
end
LFSR_reg
CASR Part (similar ...)
//CASR:
reg[36:0] CASR_varCASR,CASR_outCASR;
always @(posedge clk or negedge reset)
begin
if (!reset )
begin
...
end
else
begin
Patrick Schaumont
Spring 2008
ECE 4514 Digital Design II
Lecture 6: A Random Number Generator in Verilog
begin
if (loadseed_i )
begin
...
end
else
begin
...
end
end
end
CASR Part (similar ...)
//CASR:
reg[36:0] CASR_varCASR,CASR_outCASR; // temp
always @(posedge clk or negedge reset)
begin
if (!reset )
begin
CASR_reg = 1;
end
else
begin
Patrick Schaumont
Spring 2008
ECE 4514 Digital Design II
Lecture 6: A Random Number Generator in Verilog
begin
if (loadseed_i )
begin
CASR_varCASR [36:32]= 0;
CASR_varCASR [31:0] = seed_i ;
CASR_reg = (CASR_varCASR );
end
else
begin
...
end
end
end
CASR Part (similar ...)
//CASR:
reg[36:0] CASR_varCASR,CASR_outCASR; // temp
always @(posedge clk or negedge reset)
begin
if (!reset )
else
begin
if (loadseed_i )
else
Patrick Schaumont
Spring 2008
ECE 4514 Digital Design II
Lecture 6: A Random Number Generator in Verilog
else
begin
CASR_varCASR = CASR_reg ;
CASR_outCASR [36]= CASR_varCASR [35]^CASR_varCASR [0];
CASR_outCASR [35]= CASR_varCASR [34]^CASR_varCASR [36];
// ... some lines skipped
CASR_reg = CASR_outCASR;
end
end
end
Combine outputs
always @(posedge clk or negedge reset)
begin
if (!reset )
begin
number_o = (0);
end
Patrick Schaumont
Spring 2008
ECE 4514 Digital Design II
Lecture 6: A Random Number Generator in Verilog
else
begin
number_o = (LFSR_reg [31:0]^CASR_reg[31:0]);
end
end
Simulation ..
Patrick Schaumont
Spring 2008
ECE 4514 Digital Design II
Lecture 6: A Random Number Generator in Verilog
Simulation (looking at the state registers)
Patrick Schaumont
Spring 2008
ECE 4514 Digital Design II
Lecture 6: A Random Number Generator in Verilog
Synthesis ..
Patrick Schaumont
Spring 2008
ECE 4514 Digital Design II
Lecture 6: A Random Number Generator in Verilog
Place and Route ..
Patrick Schaumont
Spring 2008
ECE 4514 Digital Design II
Lecture 6: A Random Number Generator in Verilog
Summary
Random number generators
Many useful applications
Linear Feedback Shift Registers: PRNG
Fibonacci and Galois
Maximal-length LFSR
Structural Verilog Model
Patrick Schaumont
Spring 2008
ECE 4514 Digital Design II
Lecture 6: A Random Number Generator in Verilog
Flaws of Random Number Generators
Bias
Predictability
Nonlinear Combination Generator
Design by Tkacik
Behavioral Verilog Model

More Related Content

What's hot

Digital Signal Processing Tutorial:Chapt 3 frequency analysis
Digital Signal Processing Tutorial:Chapt 3 frequency analysisDigital Signal Processing Tutorial:Chapt 3 frequency analysis
Digital Signal Processing Tutorial:Chapt 3 frequency analysis
Chandrashekhar Padole
 
Latch and flip flop
Latch and flip flopLatch and flip flop
Latch and flip flop
GargiKhanna1
 
Digital Electronics - Counters
Digital Electronics - CountersDigital Electronics - Counters
Digital Electronics - Counters
Jayakrishnan J
 
testing
testingtesting
testing
Ram Ji D R
 
4 bit Binary counter
4 bit Binary counter4 bit Binary counter
4 bit Binary counter
Jainee Solanki
 
NYQUIST CRITERION FOR ZERO ISI
NYQUIST CRITERION FOR ZERO ISINYQUIST CRITERION FOR ZERO ISI
NYQUIST CRITERION FOR ZERO ISI
FAIZAN SHAFI
 
LDPC Encoding
LDPC EncodingLDPC Encoding
LDPC Encoding
Bhagwat Singh Rathore
 
DSP_2018_FOEHU - Lec 03 - Discrete-Time Signals and Systems
DSP_2018_FOEHU - Lec 03 - Discrete-Time Signals and SystemsDSP_2018_FOEHU - Lec 03 - Discrete-Time Signals and Systems
DSP_2018_FOEHU - Lec 03 - Discrete-Time Signals and Systems
Amr E. Mohamed
 
Design of Filters PPT
Design of Filters PPTDesign of Filters PPT
Design of Filters PPT
Imtiyaz Rashed
 
Comparator
ComparatorComparator
Comparator
Ramen Dutta
 
Verilog tutorial
Verilog tutorialVerilog tutorial
Verilog tutorial
Maryala Srinivas
 
Sequential circuit design
Sequential circuit designSequential circuit design
Sequential circuit design
Satya P. Joshi
 
Presentation 3 PLL_Analog_digital.pptx
Presentation 3 PLL_Analog_digital.pptxPresentation 3 PLL_Analog_digital.pptx
Presentation 3 PLL_Analog_digital.pptx
Prerna Singh
 
Ripple Carry Adder
Ripple Carry AdderRipple Carry Adder
Ripple Carry Adder
Aravindreddy Mokireddy
 
Metastability,MTBF,synchronizer & synchronizer failure
Metastability,MTBF,synchronizer & synchronizer failureMetastability,MTBF,synchronizer & synchronizer failure
Metastability,MTBF,synchronizer & synchronizer failure
prashant singh
 
Convolution codes - Coding/Decoding Tree codes and Trellis codes for multiple...
Convolution codes - Coding/Decoding Tree codes and Trellis codes for multiple...Convolution codes - Coding/Decoding Tree codes and Trellis codes for multiple...
Convolution codes - Coding/Decoding Tree codes and Trellis codes for multiple...
Madhumita Tamhane
 
IIR filter realization using direct form I & II
IIR filter realization using direct form I & IIIIR filter realization using direct form I & II
IIR filter realization using direct form I & II
Sarang Joshi
 
Switch level modeling
Switch level modelingSwitch level modeling
Switch level modeling
Devi Pradeep Podugu
 
Parity generator & checker
Parity generator & checkerParity generator & checker
Parity generator & checker
DEPARTMENT OF PHYSICS
 
Multirate-signal-processing.pptx
Multirate-signal-processing.pptxMultirate-signal-processing.pptx
Multirate-signal-processing.pptx
Adityare1
 

What's hot (20)

Digital Signal Processing Tutorial:Chapt 3 frequency analysis
Digital Signal Processing Tutorial:Chapt 3 frequency analysisDigital Signal Processing Tutorial:Chapt 3 frequency analysis
Digital Signal Processing Tutorial:Chapt 3 frequency analysis
 
Latch and flip flop
Latch and flip flopLatch and flip flop
Latch and flip flop
 
Digital Electronics - Counters
Digital Electronics - CountersDigital Electronics - Counters
Digital Electronics - Counters
 
testing
testingtesting
testing
 
4 bit Binary counter
4 bit Binary counter4 bit Binary counter
4 bit Binary counter
 
NYQUIST CRITERION FOR ZERO ISI
NYQUIST CRITERION FOR ZERO ISINYQUIST CRITERION FOR ZERO ISI
NYQUIST CRITERION FOR ZERO ISI
 
LDPC Encoding
LDPC EncodingLDPC Encoding
LDPC Encoding
 
DSP_2018_FOEHU - Lec 03 - Discrete-Time Signals and Systems
DSP_2018_FOEHU - Lec 03 - Discrete-Time Signals and SystemsDSP_2018_FOEHU - Lec 03 - Discrete-Time Signals and Systems
DSP_2018_FOEHU - Lec 03 - Discrete-Time Signals and Systems
 
Design of Filters PPT
Design of Filters PPTDesign of Filters PPT
Design of Filters PPT
 
Comparator
ComparatorComparator
Comparator
 
Verilog tutorial
Verilog tutorialVerilog tutorial
Verilog tutorial
 
Sequential circuit design
Sequential circuit designSequential circuit design
Sequential circuit design
 
Presentation 3 PLL_Analog_digital.pptx
Presentation 3 PLL_Analog_digital.pptxPresentation 3 PLL_Analog_digital.pptx
Presentation 3 PLL_Analog_digital.pptx
 
Ripple Carry Adder
Ripple Carry AdderRipple Carry Adder
Ripple Carry Adder
 
Metastability,MTBF,synchronizer & synchronizer failure
Metastability,MTBF,synchronizer & synchronizer failureMetastability,MTBF,synchronizer & synchronizer failure
Metastability,MTBF,synchronizer & synchronizer failure
 
Convolution codes - Coding/Decoding Tree codes and Trellis codes for multiple...
Convolution codes - Coding/Decoding Tree codes and Trellis codes for multiple...Convolution codes - Coding/Decoding Tree codes and Trellis codes for multiple...
Convolution codes - Coding/Decoding Tree codes and Trellis codes for multiple...
 
IIR filter realization using direct form I & II
IIR filter realization using direct form I & IIIIR filter realization using direct form I & II
IIR filter realization using direct form I & II
 
Switch level modeling
Switch level modelingSwitch level modeling
Switch level modeling
 
Parity generator & checker
Parity generator & checkerParity generator & checker
Parity generator & checker
 
Multirate-signal-processing.pptx
Multirate-signal-processing.pptxMultirate-signal-processing.pptx
Multirate-signal-processing.pptx
 

Similar to Lecture6[1]

Digital System Design Lab Report - VHDL ECE
Digital System Design Lab Report - VHDL ECEDigital System Design Lab Report - VHDL ECE
Digital System Design Lab Report - VHDL ECE
Ramesh Naik Bhukya
 
Verilog for synthesis - combinational rev a.pdf
Verilog for synthesis - combinational rev a.pdfVerilog for synthesis - combinational rev a.pdf
Verilog for synthesis - combinational rev a.pdf
AzeemMohammedAbdul
 
Mining
MiningMining
Mining
David Evans
 
VLSI lab manual
VLSI lab manualVLSI lab manual
VLSI lab manual
VaniPrasad11
 
[CCC'21] Evaluation of Work Stealing Algorithms
[CCC'21] Evaluation of Work Stealing Algorithms[CCC'21] Evaluation of Work Stealing Algorithms
[CCC'21] Evaluation of Work Stealing Algorithms
Universidad de los Andes
 
Quantum programming in a nutshell Radu Vunvulea ITCamp 2018
Quantum programming in a nutshell Radu Vunvulea  ITCamp 2018Quantum programming in a nutshell Radu Vunvulea  ITCamp 2018
Quantum programming in a nutshell Radu Vunvulea ITCamp 2018
Radu Vunvulea
 
Exactly-Once Made Easy: Transactional Messaging Improvement for Usability and...
Exactly-Once Made Easy: Transactional Messaging Improvement for Usability and...Exactly-Once Made Easy: Transactional Messaging Improvement for Usability and...
Exactly-Once Made Easy: Transactional Messaging Improvement for Usability and...
Guozhang Wang
 
Bitcoin.pdf
Bitcoin.pdfBitcoin.pdf
Bitcoin.pdf
INSPECTORESWSS
 
Static analysis should be used regularly
Static analysis should be used regularlyStatic analysis should be used regularly
Static analysis should be used regularly
PVS-Studio
 
Exactly-Once Made Easy: Transactional Messaging Improvement for Usability and...
Exactly-Once Made Easy: Transactional Messaging Improvement for Usability and...Exactly-Once Made Easy: Transactional Messaging Improvement for Usability and...
Exactly-Once Made Easy: Transactional Messaging Improvement for Usability and...
HostedbyConfluent
 
HHVM on AArch64 - BUD17-400K1
HHVM on AArch64 - BUD17-400K1HHVM on AArch64 - BUD17-400K1
HHVM on AArch64 - BUD17-400K1
Linaro
 
04 sequentialbasics 1
04 sequentialbasics 104 sequentialbasics 1
04 sequentialbasics 1
Poornima Prasad
 
MQTC_v2014_Tracing_On_Linux.pdf
MQTC_v2014_Tracing_On_Linux.pdfMQTC_v2014_Tracing_On_Linux.pdf
MQTC_v2014_Tracing_On_Linux.pdf
HaziNayabD
 
Bitcoin protocol for developers at techfest
Bitcoin protocol for developers at techfestBitcoin protocol for developers at techfest
Bitcoin protocol for developers at techfest
Alberto Gomez Toribio
 
20201029 モデルベース開発モーター制御編~C言語とSimulinkの文法記述を比較する~
20201029 モデルベース開発モーター制御編~C言語とSimulinkの文法記述を比較する~20201029 モデルベース開発モーター制御編~C言語とSimulinkの文法記述を比較する~
20201029 モデルベース開発モーター制御編~C言語とSimulinkの文法記述を比較する~
耕二 阿部
 
Snake Game on FPGA in Verilog
Snake Game on FPGA in VerilogSnake Game on FPGA in Verilog
Snake Game on FPGA in Verilog
Krishnajith S S
 
Magical float repr
Magical float reprMagical float repr
Magical float repr
dickinsm
 
Kotlin Backend Development 6 Yrs Recap. The Good, the Bad and the Ugly
Kotlin Backend Development 6 Yrs Recap. The Good, the Bad and the UglyKotlin Backend Development 6 Yrs Recap. The Good, the Bad and the Ugly
Kotlin Backend Development 6 Yrs Recap. The Good, the Bad and the Ugly
Haim Yadid
 
Computer archi&mp
Computer archi&mpComputer archi&mp
Computer archi&mp
MSc CST
 
Flink. Pure Streaming
Flink. Pure StreamingFlink. Pure Streaming
Flink. Pure Streaming
Indizen Technologies
 

Similar to Lecture6[1] (20)

Digital System Design Lab Report - VHDL ECE
Digital System Design Lab Report - VHDL ECEDigital System Design Lab Report - VHDL ECE
Digital System Design Lab Report - VHDL ECE
 
Verilog for synthesis - combinational rev a.pdf
Verilog for synthesis - combinational rev a.pdfVerilog for synthesis - combinational rev a.pdf
Verilog for synthesis - combinational rev a.pdf
 
Mining
MiningMining
Mining
 
VLSI lab manual
VLSI lab manualVLSI lab manual
VLSI lab manual
 
[CCC'21] Evaluation of Work Stealing Algorithms
[CCC'21] Evaluation of Work Stealing Algorithms[CCC'21] Evaluation of Work Stealing Algorithms
[CCC'21] Evaluation of Work Stealing Algorithms
 
Quantum programming in a nutshell Radu Vunvulea ITCamp 2018
Quantum programming in a nutshell Radu Vunvulea  ITCamp 2018Quantum programming in a nutshell Radu Vunvulea  ITCamp 2018
Quantum programming in a nutshell Radu Vunvulea ITCamp 2018
 
Exactly-Once Made Easy: Transactional Messaging Improvement for Usability and...
Exactly-Once Made Easy: Transactional Messaging Improvement for Usability and...Exactly-Once Made Easy: Transactional Messaging Improvement for Usability and...
Exactly-Once Made Easy: Transactional Messaging Improvement for Usability and...
 
Bitcoin.pdf
Bitcoin.pdfBitcoin.pdf
Bitcoin.pdf
 
Static analysis should be used regularly
Static analysis should be used regularlyStatic analysis should be used regularly
Static analysis should be used regularly
 
Exactly-Once Made Easy: Transactional Messaging Improvement for Usability and...
Exactly-Once Made Easy: Transactional Messaging Improvement for Usability and...Exactly-Once Made Easy: Transactional Messaging Improvement for Usability and...
Exactly-Once Made Easy: Transactional Messaging Improvement for Usability and...
 
HHVM on AArch64 - BUD17-400K1
HHVM on AArch64 - BUD17-400K1HHVM on AArch64 - BUD17-400K1
HHVM on AArch64 - BUD17-400K1
 
04 sequentialbasics 1
04 sequentialbasics 104 sequentialbasics 1
04 sequentialbasics 1
 
MQTC_v2014_Tracing_On_Linux.pdf
MQTC_v2014_Tracing_On_Linux.pdfMQTC_v2014_Tracing_On_Linux.pdf
MQTC_v2014_Tracing_On_Linux.pdf
 
Bitcoin protocol for developers at techfest
Bitcoin protocol for developers at techfestBitcoin protocol for developers at techfest
Bitcoin protocol for developers at techfest
 
20201029 モデルベース開発モーター制御編~C言語とSimulinkの文法記述を比較する~
20201029 モデルベース開発モーター制御編~C言語とSimulinkの文法記述を比較する~20201029 モデルベース開発モーター制御編~C言語とSimulinkの文法記述を比較する~
20201029 モデルベース開発モーター制御編~C言語とSimulinkの文法記述を比較する~
 
Snake Game on FPGA in Verilog
Snake Game on FPGA in VerilogSnake Game on FPGA in Verilog
Snake Game on FPGA in Verilog
 
Magical float repr
Magical float reprMagical float repr
Magical float repr
 
Kotlin Backend Development 6 Yrs Recap. The Good, the Bad and the Ugly
Kotlin Backend Development 6 Yrs Recap. The Good, the Bad and the UglyKotlin Backend Development 6 Yrs Recap. The Good, the Bad and the Ugly
Kotlin Backend Development 6 Yrs Recap. The Good, the Bad and the Ugly
 
Computer archi&mp
Computer archi&mpComputer archi&mp
Computer archi&mp
 
Flink. Pure Streaming
Flink. Pure StreamingFlink. Pure Streaming
Flink. Pure Streaming
 

Recently uploaded

Beyond Degrees - Empowering the Workforce in the Context of Skills-First.pptx
Beyond Degrees - Empowering the Workforce in the Context of Skills-First.pptxBeyond Degrees - Empowering the Workforce in the Context of Skills-First.pptx
Beyond Degrees - Empowering the Workforce in the Context of Skills-First.pptx
EduSkills OECD
 
How to Make a Field Mandatory in Odoo 17
How to Make a Field Mandatory in Odoo 17How to Make a Field Mandatory in Odoo 17
How to Make a Field Mandatory in Odoo 17
Celine George
 
Wound healing PPT
Wound healing PPTWound healing PPT
Wound healing PPT
Jyoti Chand
 
The basics of sentences session 6pptx.pptx
The basics of sentences session 6pptx.pptxThe basics of sentences session 6pptx.pptx
The basics of sentences session 6pptx.pptx
heathfieldcps1
 
writing about opinions about Australia the movie
writing about opinions about Australia the moviewriting about opinions about Australia the movie
writing about opinions about Australia the movie
Nicholas Montgomery
 
Leveraging Generative AI to Drive Nonprofit Innovation
Leveraging Generative AI to Drive Nonprofit InnovationLeveraging Generative AI to Drive Nonprofit Innovation
Leveraging Generative AI to Drive Nonprofit Innovation
TechSoup
 
The History of Stoke Newington Street Names
The History of Stoke Newington Street NamesThe History of Stoke Newington Street Names
The History of Stoke Newington Street Names
History of Stoke Newington
 
PIMS Job Advertisement 2024.pdf Islamabad
PIMS Job Advertisement 2024.pdf IslamabadPIMS Job Advertisement 2024.pdf Islamabad
PIMS Job Advertisement 2024.pdf Islamabad
AyyanKhan40
 
BÀI TẬP BỔ TRỢ TIẾNG ANH 8 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2023-2024 (CÓ FI...
BÀI TẬP BỔ TRỢ TIẾNG ANH 8 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2023-2024 (CÓ FI...BÀI TẬP BỔ TRỢ TIẾNG ANH 8 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2023-2024 (CÓ FI...
BÀI TẬP BỔ TRỢ TIẾNG ANH 8 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2023-2024 (CÓ FI...
Nguyen Thanh Tu Collection
 
South African Journal of Science: Writing with integrity workshop (2024)
South African Journal of Science: Writing with integrity workshop (2024)South African Journal of Science: Writing with integrity workshop (2024)
South African Journal of Science: Writing with integrity workshop (2024)
Academy of Science of South Africa
 
Reimagining Your Library Space: How to Increase the Vibes in Your Library No ...
Reimagining Your Library Space: How to Increase the Vibes in Your Library No ...Reimagining Your Library Space: How to Increase the Vibes in Your Library No ...
Reimagining Your Library Space: How to Increase the Vibes in Your Library No ...
Diana Rendina
 
BÀI TẬP BỔ TRỢ TIẾNG ANH LỚP 9 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2024-2025 - ...
BÀI TẬP BỔ TRỢ TIẾNG ANH LỚP 9 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2024-2025 - ...BÀI TẬP BỔ TRỢ TIẾNG ANH LỚP 9 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2024-2025 - ...
BÀI TẬP BỔ TRỢ TIẾNG ANH LỚP 9 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2024-2025 - ...
Nguyen Thanh Tu Collection
 
What is Digital Literacy? A guest blog from Andy McLaughlin, University of Ab...
What is Digital Literacy? A guest blog from Andy McLaughlin, University of Ab...What is Digital Literacy? A guest blog from Andy McLaughlin, University of Ab...
What is Digital Literacy? A guest blog from Andy McLaughlin, University of Ab...
GeorgeMilliken2
 
Hindi varnamala | hindi alphabet PPT.pdf
Hindi varnamala | hindi alphabet PPT.pdfHindi varnamala | hindi alphabet PPT.pdf
Hindi varnamala | hindi alphabet PPT.pdf
Dr. Mulla Adam Ali
 
Advanced Java[Extra Concepts, Not Difficult].docx
Advanced Java[Extra Concepts, Not Difficult].docxAdvanced Java[Extra Concepts, Not Difficult].docx
Advanced Java[Extra Concepts, Not Difficult].docx
adhitya5119
 
NEWSPAPERS - QUESTION 1 - REVISION POWERPOINT.pptx
NEWSPAPERS - QUESTION 1 - REVISION POWERPOINT.pptxNEWSPAPERS - QUESTION 1 - REVISION POWERPOINT.pptx
NEWSPAPERS - QUESTION 1 - REVISION POWERPOINT.pptx
iammrhaywood
 
Digital Artefact 1 - Tiny Home Environmental Design
Digital Artefact 1 - Tiny Home Environmental DesignDigital Artefact 1 - Tiny Home Environmental Design
Digital Artefact 1 - Tiny Home Environmental Design
amberjdewit93
 
clinical examination of hip joint (1).pdf
clinical examination of hip joint (1).pdfclinical examination of hip joint (1).pdf
clinical examination of hip joint (1).pdf
Priyankaranawat4
 
Liberal Approach to the Study of Indian Politics.pdf
Liberal Approach to the Study of Indian Politics.pdfLiberal Approach to the Study of Indian Politics.pdf
Liberal Approach to the Study of Indian Politics.pdf
WaniBasim
 
UGC NET Exam Paper 1- Unit 1:Teaching Aptitude
UGC NET Exam Paper 1- Unit 1:Teaching AptitudeUGC NET Exam Paper 1- Unit 1:Teaching Aptitude
UGC NET Exam Paper 1- Unit 1:Teaching Aptitude
S. Raj Kumar
 

Recently uploaded (20)

Beyond Degrees - Empowering the Workforce in the Context of Skills-First.pptx
Beyond Degrees - Empowering the Workforce in the Context of Skills-First.pptxBeyond Degrees - Empowering the Workforce in the Context of Skills-First.pptx
Beyond Degrees - Empowering the Workforce in the Context of Skills-First.pptx
 
How to Make a Field Mandatory in Odoo 17
How to Make a Field Mandatory in Odoo 17How to Make a Field Mandatory in Odoo 17
How to Make a Field Mandatory in Odoo 17
 
Wound healing PPT
Wound healing PPTWound healing PPT
Wound healing PPT
 
The basics of sentences session 6pptx.pptx
The basics of sentences session 6pptx.pptxThe basics of sentences session 6pptx.pptx
The basics of sentences session 6pptx.pptx
 
writing about opinions about Australia the movie
writing about opinions about Australia the moviewriting about opinions about Australia the movie
writing about opinions about Australia the movie
 
Leveraging Generative AI to Drive Nonprofit Innovation
Leveraging Generative AI to Drive Nonprofit InnovationLeveraging Generative AI to Drive Nonprofit Innovation
Leveraging Generative AI to Drive Nonprofit Innovation
 
The History of Stoke Newington Street Names
The History of Stoke Newington Street NamesThe History of Stoke Newington Street Names
The History of Stoke Newington Street Names
 
PIMS Job Advertisement 2024.pdf Islamabad
PIMS Job Advertisement 2024.pdf IslamabadPIMS Job Advertisement 2024.pdf Islamabad
PIMS Job Advertisement 2024.pdf Islamabad
 
BÀI TẬP BỔ TRỢ TIẾNG ANH 8 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2023-2024 (CÓ FI...
BÀI TẬP BỔ TRỢ TIẾNG ANH 8 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2023-2024 (CÓ FI...BÀI TẬP BỔ TRỢ TIẾNG ANH 8 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2023-2024 (CÓ FI...
BÀI TẬP BỔ TRỢ TIẾNG ANH 8 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2023-2024 (CÓ FI...
 
South African Journal of Science: Writing with integrity workshop (2024)
South African Journal of Science: Writing with integrity workshop (2024)South African Journal of Science: Writing with integrity workshop (2024)
South African Journal of Science: Writing with integrity workshop (2024)
 
Reimagining Your Library Space: How to Increase the Vibes in Your Library No ...
Reimagining Your Library Space: How to Increase the Vibes in Your Library No ...Reimagining Your Library Space: How to Increase the Vibes in Your Library No ...
Reimagining Your Library Space: How to Increase the Vibes in Your Library No ...
 
BÀI TẬP BỔ TRỢ TIẾNG ANH LỚP 9 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2024-2025 - ...
BÀI TẬP BỔ TRỢ TIẾNG ANH LỚP 9 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2024-2025 - ...BÀI TẬP BỔ TRỢ TIẾNG ANH LỚP 9 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2024-2025 - ...
BÀI TẬP BỔ TRỢ TIẾNG ANH LỚP 9 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2024-2025 - ...
 
What is Digital Literacy? A guest blog from Andy McLaughlin, University of Ab...
What is Digital Literacy? A guest blog from Andy McLaughlin, University of Ab...What is Digital Literacy? A guest blog from Andy McLaughlin, University of Ab...
What is Digital Literacy? A guest blog from Andy McLaughlin, University of Ab...
 
Hindi varnamala | hindi alphabet PPT.pdf
Hindi varnamala | hindi alphabet PPT.pdfHindi varnamala | hindi alphabet PPT.pdf
Hindi varnamala | hindi alphabet PPT.pdf
 
Advanced Java[Extra Concepts, Not Difficult].docx
Advanced Java[Extra Concepts, Not Difficult].docxAdvanced Java[Extra Concepts, Not Difficult].docx
Advanced Java[Extra Concepts, Not Difficult].docx
 
NEWSPAPERS - QUESTION 1 - REVISION POWERPOINT.pptx
NEWSPAPERS - QUESTION 1 - REVISION POWERPOINT.pptxNEWSPAPERS - QUESTION 1 - REVISION POWERPOINT.pptx
NEWSPAPERS - QUESTION 1 - REVISION POWERPOINT.pptx
 
Digital Artefact 1 - Tiny Home Environmental Design
Digital Artefact 1 - Tiny Home Environmental DesignDigital Artefact 1 - Tiny Home Environmental Design
Digital Artefact 1 - Tiny Home Environmental Design
 
clinical examination of hip joint (1).pdf
clinical examination of hip joint (1).pdfclinical examination of hip joint (1).pdf
clinical examination of hip joint (1).pdf
 
Liberal Approach to the Study of Indian Politics.pdf
Liberal Approach to the Study of Indian Politics.pdfLiberal Approach to the Study of Indian Politics.pdf
Liberal Approach to the Study of Indian Politics.pdf
 
UGC NET Exam Paper 1- Unit 1:Teaching Aptitude
UGC NET Exam Paper 1- Unit 1:Teaching AptitudeUGC NET Exam Paper 1- Unit 1:Teaching Aptitude
UGC NET Exam Paper 1- Unit 1:Teaching Aptitude
 

Lecture6[1]

  • 1. ECE 4514 Digital Design II Spring 2008 Lecture 6: A Random Number Generator Patrick Schaumont Spring 2008 ECE 4514 Digital Design II Lecture 6: A Random Number Generator in Verilog Lecture 6: A Random Number Generator in Verilog A Design Lecture Patrick Schaumont
  • 2. What is a random number generator? Random Number Generator 11, 86, 82, 52, 60, 46, 64, 10, 98, 2, ... Patrick Schaumont Spring 2008 ECE 4514 Digital Design II Lecture 6: A Random Number Generator in Verilog Generator
  • 3. What do I do with randomness? Play games! Have the monsters appear in different rooms every time Do statistical simulations Simulate customers in a shopping center (find the best spot for a new Chuck E Cheese) Run security protocols Patrick Schaumont Spring 2008 ECE 4514 Digital Design II Lecture 6: A Random Number Generator in Verilog Make protocol resistent against replay Encrypt documents Use random numbers as key stream
  • 4. Encrypt Documents Random Number Generator XOR stream of bytes plaintext encrypted stream of bytes cryptext Patrick Schaumont Spring 2008 ECE 4514 Digital Design II Lecture 6: A Random Number Generator in Verilog Generator XOR cryptext decrypted stream of bytes plaintext 'one-time pad'
  • 5. Random numbers by physical methods Use dice, coin flips, roulette Use thermal noise (diodes and resistors) Use clock jitter (use ring oscillators) Use radioactive decay Use Lava Lamps Patrick Schaumont Spring 2008 ECE 4514 Digital Design II Lecture 6: A Random Number Generator in Verilog Use Lava Lamps Patented!
  • 6. Random numbers by computational methods Not truly random, but pseudo random meaning, after some time the same sequence returns Linear Congruential Generator x(n+1) = [ a.x(b) + b ] mod m Eg. a = 15, b = 5, m = 7 a, b, m must be chosen carefully! Patrick Schaumont Spring 2008 ECE 4514 Digital Design II Lecture 6: A Random Number Generator in Verilog Eg. a = 15, b = 5, m = 7 X(0) = 1 X(1) = (15 + 5) mod 7 = 6 x(2) = (15*6 + 5) mod 7 = 4 x(3) = 2 x(4) = 0 x(5) = 5 x(6) = 3 x(7) = 1 x(8) = ... a, b, m must be chosen carefully! for a maximum lenth sequence
  • 7. A quick way to generate random numbers Verilog has a buildin random number generator module random(q); output [0:31] q; reg [0:31] q; initial r_seed = 2; Patrick Schaumont Spring 2008 ECE 4514 Digital Design II Lecture 6: A Random Number Generator in Verilog r_seed = 2; always #10 q = $random(r_seed); endmodule Nice, but only for testbenches … Instead, we want an hardware implementation
  • 8. Linear Feedback Shift Register Pseudo Random Numbers in Digital Hardware shift register Patrick Schaumont Spring 2008 ECE 4514 Digital Design II Lecture 6: A Random Number Generator in Verilog feedback network
  • 9. Linear Feedback Shift Register All zeroes not very useful ... 0 0 0 0 Patrick Schaumont Spring 2008 ECE 4514 Digital Design II Lecture 6: A Random Number Generator in Verilog 0
  • 10. Linear Feedback Shift Register Non-zero state is more interesting 0 0 0 1 Patrick Schaumont Spring 2008 ECE 4514 Digital Design II Lecture 6: A Random Number Generator in Verilog 1 1
  • 11. Linear Feedback Shift Register Non-zero state is more interesting 1 0 0 0 Patrick Schaumont Spring 2008 ECE 4514 Digital Design II Lecture 6: A Random Number Generator in Verilog 0 10
  • 12. Linear Feedback Shift Register Non-zero state is more interesting 0 1 0 0 Patrick Schaumont Spring 2008 ECE 4514 Digital Design II Lecture 6: A Random Number Generator in Verilog 0 100
  • 13. Linear Feedback Shift Register Non-zero state is more interesting 0 0 1 0 Patrick Schaumont Spring 2008 ECE 4514 Digital Design II Lecture 6: A Random Number Generator in Verilog 1 1001
  • 14. Linear Feedback Shift Register Non-zero state is more interesting 1 0 0 1 Patrick Schaumont Spring 2008 ECE 4514 Digital Design II Lecture 6: A Random Number Generator in Verilog 1 10011
  • 15. Linear Feedback Shift Register Non-zero state is more interesting 0 1 0 0 Patrick Schaumont Spring 2008 ECE 4514 Digital Design II Lecture 6: A Random Number Generator in Verilog 0 100110
  • 16. Linear Feedback Shift Register Non-zero state is more interesting 0 0 1 0 Patrick Schaumont Spring 2008 ECE 4514 Digital Design II Lecture 6: A Random Number Generator in Verilog 1 1001101 etc ...
  • 17. Linear Feedback Shift Register This is actually a finite state machine 1 0 0 1 State Encoding Patrick Schaumont Spring 2008 ECE 4514 Digital Design II Lecture 6: A Random Number Generator in Verilog 1
  • 18. Linear Feedback Shift Register This is actually a finite state machine 0 0 0 1 Patrick Schaumont Spring 2008 ECE 4514 Digital Design II Lecture 6: A Random Number Generator in Verilog 0001 1000 0100 0010 . . . How many states will you see?
  • 19. Linear Feedback Shift Register 15 states 0 0 0 1 Patrick Schaumont Spring 2008 ECE 4514 Digital Design II Lecture 6: A Random Number Generator in Verilog 0001 1000 0100 0010 1001 1100 0110 1011 0101 1010 1101 1110 1111 0111 0011
  • 20. Linear Feedback Shift Register We can specify an LFSR by means of the characteristic polynomial (also called feedback polynomial) X^1 X^2 X^3 X^4 Patrick Schaumont Spring 2008 ECE 4514 Digital Design II Lecture 6: A Random Number Generator in Verilog P(x) = x^4 + x^3 + 1 There exists elaborate finite-field math to analyze the properties of an LFSR - outside of the scope of this class
  • 21. Linear Feedback Shift Register So, knowing the polynomial you can also draw the LFSR P(x) = x^8 + x^6 + x^5 + x^4 + 1 How many taps ? How many 2-input XOR? Patrick Schaumont Spring 2008 ECE 4514 Digital Design II Lecture 6: A Random Number Generator in Verilog How many 2-input XOR?
  • 22. Linear Feedback Shift Register So, knowing the polynomial you can also draw the LFSR P(x) = x^8 + x^6 + x^5 + x^4 + 1 How many taps ? 8 How many 2-input XOR? Patrick Schaumont Spring 2008 ECE 4514 Digital Design II Lecture 6: A Random Number Generator in Verilog How many 2-input XOR?
  • 23. Linear Feedback Shift Register So, knowing the polynomial you can also draw the LFSR P(x) = x^8 + x^6 + x^5 + x^4 + 1 How many taps ? 8 How many 2-input XOR? 3 Patrick Schaumont Spring 2008 ECE 4514 Digital Design II Lecture 6: A Random Number Generator in Verilog How many 2-input XOR? 3
  • 24. Linear Feedback Shift Register Certain polynomials generate very long state sequences. These are called maximal-length LFSR. P(X) = x^153 + x^152 + 1 Patrick Schaumont Spring 2008 ECE 4514 Digital Design II Lecture 6: A Random Number Generator in Verilog P(X) = x^153 + x^152 + 1 is a maximum-length feedback polynomial State machine with 2 ^ 153 -1 states ..
  • 25. Fibonacci and Galois LFSR This format is called a Fibonacci LFSR 1 2 3 4 Fibonacci ~1175-1250 Patrick Schaumont Spring 2008 ECE 4514 Digital Design II Lecture 6: A Random Number Generator in Verilog Can be converted to an equivalent Galois LFSR 4 3 2 1 Evariste Galois 1811-1832
  • 26. Fibonacci and Galois LFSR Each Fibonacci LFSR can transform into Galois LFSR: Reverse numbering of taps Make XOR inputs XOR outputs and vice versa Example: starting with this Fibonacci LFSR Patrick Schaumont Spring 2008 ECE 4514 Digital Design II Lecture 6: A Random Number Generator in Verilog 1 2 3 4 5 6 7 8
  • 27. Fibonacci and Galois LFSR Disconnect XOR inputs Reverse tap numbering (not the direction of shifting!) Patrick Schaumont Spring 2008 ECE 4514 Digital Design II Lecture 6: A Random Number Generator in Verilog 8 7 6 5 4 3 2 1
  • 28. Fibonacci and Galois LFSR Turn XOR inputs into XOR outputs and vice versa Patrick Schaumont Spring 2008 ECE 4514 Digital Design II Lecture 6: A Random Number Generator in Verilog 8 7 6 5 4 3 2 1
  • 29. Which one is better for digital hardware? Fibonacci 1 2 3 4 5 6 7 8 Patrick Schaumont Spring 2008 ECE 4514 Digital Design II Lecture 6: A Random Number Generator in Verilog 8 7 6 5 4 3 2 1 Galois
  • 30. Which one is better for digital hardware? Fibonacci 1 2 3 4 5 6 7 8 Patrick Schaumont Spring 2008 ECE 4514 Digital Design II Lecture 6: A Random Number Generator in Verilog 8 7 6 5 4 3 2 1 Galois computes all taps in parallel
  • 31. Which one is better for software? Fibonacci 1 2 3 4 5 6 7 8 Patrick Schaumont Spring 2008 ECE 4514 Digital Design II Lecture 6: A Random Number Generator in Verilog 8 7 6 5 4 3 2 1 Galois
  • 32. Which one is better for software? Fibonacci 1 2 3 4 5 6 7 8 Patrick Schaumont Spring 2008 ECE 4514 Digital Design II Lecture 6: A Random Number Generator in Verilog 8 7 6 5 4 3 2 1 Galois char_v = (char_v >> 1) ^ (-(signed char) (char_v & 1) & 0xe)
  • 33. Let's write an LFSR in Verilog xor(out, in1, in2) Patrick Schaumont Spring 2008 ECE 4514 Digital Design II Lecture 6: A Random Number Generator in Verilog FF You need to build this one (structural, behavioral)
  • 34. A Flip flop module flipflop(q, clk, rst, d); input clk; input rst; input d; output q; reg q; always @(posedge clk or posedge rst) Patrick Schaumont Spring 2008 ECE 4514 Digital Design II Lecture 6: A Random Number Generator in Verilog always @(posedge clk or posedge rst) begin if (rst) q = 0; else q = d; end endmodule
  • 35. A Flip flop Setup Time: Time D has to be stable before a clock edge Hold Time: Time D has to be stable after clock edge Propagation Delay: Delay from clock edge to Q Patrick Schaumont Spring 2008 ECE 4514 Digital Design II Lecture 6: A Random Number Generator in Verilog Delay from clock edge to Q Delay from reset to Q How to specify propagation delay ?
  • 36. A Flip flop module flipflop(q, clk, rst, d); input clk; input rst; input d; output q; reg q; always @(posedge clk or posedge rst) Patrick Schaumont Spring 2008 ECE 4514 Digital Design II Lecture 6: A Random Number Generator in Verilog always @(posedge clk or posedge rst) begin if (rst) q = 0; else q = d; end endmodule How to specify propagation delay ?
  • 37. A Flip flop module flipflop(q, clk, rst, d); input clk; input rst; input d; output q; reg q; always @(posedge clk or posedge rst) begin if (rst) Patrick Schaumont Spring 2008 ECE 4514 Digital Design II Lecture 6: A Random Number Generator in Verilog if (rst) #2 q = 0; else q = #3 d; end specify $setup(d, clk, 2); $hold(clk, d, 0); endspecify endmodule See Chapter 10 Palnitkar Test setup, hold
  • 38. Let's turn the LFSR into a module 1 2 3 4 How to program this? Patrick Schaumont Spring 2008 ECE 4514 Digital Design II Lecture 6: A Random Number Generator in Verilog output (1 bit)
  • 39. Let's turn the LFSR into a module seed (4 bit) load (1 bit) 1 1 1 1 Patrick Schaumont Spring 2008 ECE 4514 Digital Design II Lecture 6: A Random Number Generator in Verilog 1 2 3 4 output (1 bit) 0 1 0 0 0
  • 40. Multiplexer symbol 1 a control if (control) out = a; module mux(q, control, a, b); output q; reg q; input control, a, b; wire notcontrol; Patrick Schaumont Spring 2008 ECE 4514 Digital Design II Lecture 6: A Random Number Generator in Verilog 0 b out = a; else out = b; bit multiplexer wire notcontrol; always @(control or notcontrol or a or b) q = (control & a) | (notcontrol & b); not (notcontrol, control); endmodule;
  • 41. LFSR, structural 1 2 3 4 output (1 bit) seed (4 bit) load (1 bit) 1 0 1 0 1 0 1 0 Patrick Schaumont Spring 2008 ECE 4514 Digital Design II Lecture 6: A Random Number Generator in Verilog module lfsr(q, clk, rst, seed, load); ... wire [3:0] state_out; wire [3:0] state_in; flipflop F[3:0] (state_out, clk, rst, state_in); endmodule
  • 42. LFSR, structural 1 2 3 4 output (1 bit) seed (4 bit) load (1 bit) 1 0 1 0 1 0 1 0 module lfsr(q, clk, rst, seed, load); ... Patrick Schaumont Spring 2008 ECE 4514 Digital Design II Lecture 6: A Random Number Generator in Verilog ... wire [3:0] state_out; wire [3:0] state_in; wire nextbit; xor G1(nextbit, state_out[2], state_out[3]); assign q = nextbit; endmodule
  • 43. LFSR, structural 1 2 3 4 output (1 bit) seed (4 bit) load (1 bit) 1 0 1 0 1 0 1 0 module lfsr(q, clk, rst, seed, load); ... Patrick Schaumont Spring 2008 ECE 4514 Digital Design II Lecture 6: A Random Number Generator in Verilog ... wire [3:0] state_out; wire [3:0] state_in; wire nextbit; mux M1[3:0] (state_in, load, seed, {state_out[2], state_out[1], state_out[0], nextbit}); assign q = nextbit; endmodule
  • 44. LFSR module - complete module lfsr(q, clk, rst, seed, load); output q; input [3:0] seed; input load; input rst; wire [3:0] state_out; wire [3:0] state_in; Patrick Schaumont Spring 2008 ECE 4514 Digital Design II Lecture 6: A Random Number Generator in Verilog flipflop F[3:0] (state_out, clk, rst, state_in); mux M1[3:0] (state_in, load, seed, {state_out[2], state_out[1], state_out[0], nextbit}); xor G1(nextbit, state_out[2], state_out[3]); assign q = nextbit; endmodule
  • 45. LFSR testbench module lfsrtst; reg clk; reg rst; reg [3:0] seed; reg load; wire q; lfsr L(q, clk, rst, seed, load); // initialization // drive clock always #50 clk = !clk; // program lfsr initial begin #100 seed = 4'b0001; load = 1; #100 load = 0; end Patrick Schaumont Spring 2008 ECE 4514 Digital Design II Lecture 6: A Random Number Generator in Verilog // initialization // apply reset pulse initial begin clk = 0; load = 0; seed = 0; rst = 0; #10 rst = 1; #10 rst = 0; end end endmodule
  • 46. Simulation .. Patrick Schaumont Spring 2008 ECE 4514 Digital Design II Lecture 6: A Random Number Generator in Verilog
  • 47. Synthesis .. Patrick Schaumont Spring 2008 ECE 4514 Digital Design II Lecture 6: A Random Number Generator in Verilog
  • 48. Place and Route .. Patrick Schaumont Spring 2008 ECE 4514 Digital Design II Lecture 6: A Random Number Generator in Verilog
  • 49. Can a random number generator have flaws? Patrick Schaumont Spring 2008 ECE 4514 Digital Design II Lecture 6: A Random Number Generator in Verilog
  • 50. Can a random number generator have flaws? Problem #1: it can have bias Meaning: a certain number occurs more often then others Expressed in the entropy rate of the generator Entropy = true information rate (in bit/sec), can be lower then the actual bitrate of the random number generator Example: Assume an RNG that produces three events A,B,C encoded with two bits Patrick Schaumont Spring 2008 ECE 4514 Digital Design II Lecture 6: A Random Number Generator in Verilog RNG A B C P(A) = 1/2 P(B) = 1/4 P(C) = 1/4 00 01 10 symbol probability bitpattern So this bitstream has much more '0' then '1'. It has a bias. E.g. ABACAABC... is encoded as 0001001000000110...
  • 51. Can a random number generator have flaws? Problem #1: it can have bias Meaning: a certain number occurs more often then others Expressed in the entropy rate of the generator Entropy = true information rate (in bit/sec), can be lower then the actual bitrate of the random number generator Example: Assume an RNG that produces three events A,B,C encoded with two bits better Patrick Schaumont Spring 2008 ECE 4514 Digital Design II Lecture 6: A Random Number Generator in Verilog RNG A B C P(A) = 1/2 P(B) = 1/4 P(C) = 1/4 0 10 11 symbol probability better bitpattern In this bitstream, the number of '1' and '0' are balanced. E.g. ABACAABC... is encoded as 010011001011...
  • 52. Can a random number generator have flaws? Problem #1: it can have bias Do LFSR have a bias ? Patrick Schaumont Spring 2008 ECE 4514 Digital Design II Lecture 6: A Random Number Generator in Verilog
  • 53. Can a random number generator have flaws? Problem #1: it can have bias Do LFSR have a bias ? Yes, they have a small bias because the all-zero state never appears. However, for a very long LFSR, the bias becomes negligible Patrick Schaumont Spring 2008 ECE 4514 Digital Design II Lecture 6: A Random Number Generator in Verilog negligible
  • 54. Can a random number generator have flaws? Problem #2: it can be predicted Not when truly random phsysical phenomena But, if it is a Pseudo RNG (like an LFSR), it is a deterministic sequence. Is this really a problem? Yes! • Don't want to use a predictable RNG for dealing cards, driving a Patrick Schaumont Spring 2008 ECE 4514 Digital Design II Lecture 6: A Random Number Generator in Verilog • Don't want to use a predictable RNG for dealing cards, driving a slot machine, ... (at least not if you own the place). • Don't want to use predicatable RNG in security. Predicability = weakness
  • 55. Can a random number generator have flaws? Problem #2: it can be predicted The real issue for PRNG is: can the value of bit N+1 be predicted when someone observes the first N bits. closed box Patrick Schaumont Spring 2008 ECE 4514 Digital Design II Lecture 6: A Random Number Generator in Verilog 8 7 6 5 4 3 2 1 closed box 010111011.. The next one will be .. 1 !
  • 56. LFSR are very predictable .. Predicting the next output bit is equivalent to knowing the feedback pattern of the LFSR and the states of all LFSR flip-flops. Mathematicians (Berlekamp-Massey) found that: Given an N-bit LFSR with unknown feedback pattern, then only 2N bits are needed to predict bit 2N + 1 Patrick Schaumont Spring 2008 ECE 4514 Digital Design II Lecture 6: A Random Number Generator in Verilog So let's say we have and 8-bit LFSR, then we need only 16 bits of the RNG stream before it becomes predicatble LFSR are unsuited for everything that should be unpredictable
  • 57. To be unpredictable, the LFSR should be long Solution 1 Use a maximal-length P(x) = x^N + ... + 1 with N >>> (E.g. 65,536) Very expensive to make! 64K flip-flops .. Solution 2: Non-linear Combination Generator Patrick Schaumont Spring 2008 ECE 4514 Digital Design II Lecture 6: A Random Number Generator in Verilog Solution 2: Non-linear Combination Generator short LFSR1 short LFSR2 short LFSR3 non linear func A B C out Eg. out = AB ^ BC ^ C
  • 58. Example design by Tkacik, 2002 Patrick Schaumont Spring 2008 ECE 4514 Digital Design II Lecture 6: A Random Number Generator in Verilog Cellular Automata Shift Register
  • 59. Example design by Tkacik, 2002 43-bit LFSR defined by P(X) = X^43 + X^41 + X^20 + X + 1 => 3XOR, 43 taps Maximal Length: 2^43-1 Bias ~ 2^-43 (because all-zero pattern cannot appear) 37-bit cellular automata shift register Combines previous and next statereg into current state reg Patrick Schaumont Spring 2008 ECE 4514 Digital Design II Lecture 6: A Random Number Generator in Verilog Similar to 37 intertwined state machines (automata) Maximal Length: 2^37-1 Bias ~ 2^-37 1 2 3 from 37 from 1 from 2 to 37 ... ...
  • 60. Sample Implementation On opencores you can find an implementation of Tkacik's design - assigned reading of today (this design has a few minor differences with the spec written by Tkacik - but OK for our purpose) Patrick Schaumont Spring 2008 ECE 4514 Digital Design II Lecture 6: A Random Number Generator in Verilog
  • 61. Module interface module rng(clk,reset,loadseed_i,seed_i,number_o); input clk; input reset; input loadseed_i; input [31:0] seed_i; output [31:0] number_o; reg [31:0] number_o; reg [42:0] LFSR_reg; // internal state Patrick Schaumont Spring 2008 ECE 4514 Digital Design II Lecture 6: A Random Number Generator in Verilog reg [42:0] LFSR_reg; // internal state reg [36:0] CASR_reg; // internal state always (.. CASR ..) always (.. LFSR ..) always (.. combine outputs ..) endmodule
  • 62. LFSR Part reg[42:0] LFSR_varLFSR; // temporary working var reg outbitLFSR; // temporary working var always @(posedge clk or negedge reset) begin if (!reset ) begin ... end else begin Patrick Schaumont Spring 2008 ECE 4514 Digital Design II Lecture 6: A Random Number Generator in Verilog begin if (loadseed_i ) begin ... end else begin ... end end end
  • 63. LFSR Part reg[42:0] LFSR_varLFSR; // temporary working var reg outbitLFSR; // temporary working var always @(posedge clk or negedge reset) begin if (!reset ) begin LFSR_reg = (1); end else begin assemble bits Patrick Schaumont Spring 2008 ECE 4514 Digital Design II Lecture 6: A Random Number Generator in Verilog begin if (loadseed_i ) begin LFSR_varLFSR [42:32]=0; LFSR_varLFSR [31:0]=seed_i ; LFSR_reg = (LFSR_varLFSR ); end else begin ... end end end LFSR_varLFSR LFSR_reg
  • 64. LFSR Part reg[42:0] LFSR_varLFSR; // temporary working var reg outbitLFSR; // temporary working var always @(posedge clk or negedge reset) begin if (!reset ) else begin if (loadseed_i ) else begin LFSR_varLFSR LFSR_reg LFSR_varLFSR Patrick Schaumont Spring 2008 ECE 4514 Digital Design II Lecture 6: A Random Number Generator in Verilog begin LFSR_varLFSR = LFSR_reg; LFSR_varLFSR [42] = LFSR_varLFSR [41]; outbitLFSR = LFSR_varLFSR [42]; LFSR_varLFSR [42] = LFSR_varLFSR [41]; LFSR_varLFSR [41] = LFSR_varLFSR [40]^outbitLFSR ; // some lines skipped ... LFSR_varLFSR [0] = LFSR_varLFSR [42]; LFSR_reg = LFSR_varLFSR; end end end LFSR_reg
  • 65. CASR Part (similar ...) //CASR: reg[36:0] CASR_varCASR,CASR_outCASR; always @(posedge clk or negedge reset) begin if (!reset ) begin ... end else begin Patrick Schaumont Spring 2008 ECE 4514 Digital Design II Lecture 6: A Random Number Generator in Verilog begin if (loadseed_i ) begin ... end else begin ... end end end
  • 66. CASR Part (similar ...) //CASR: reg[36:0] CASR_varCASR,CASR_outCASR; // temp always @(posedge clk or negedge reset) begin if (!reset ) begin CASR_reg = 1; end else begin Patrick Schaumont Spring 2008 ECE 4514 Digital Design II Lecture 6: A Random Number Generator in Verilog begin if (loadseed_i ) begin CASR_varCASR [36:32]= 0; CASR_varCASR [31:0] = seed_i ; CASR_reg = (CASR_varCASR ); end else begin ... end end end
  • 67. CASR Part (similar ...) //CASR: reg[36:0] CASR_varCASR,CASR_outCASR; // temp always @(posedge clk or negedge reset) begin if (!reset ) else begin if (loadseed_i ) else Patrick Schaumont Spring 2008 ECE 4514 Digital Design II Lecture 6: A Random Number Generator in Verilog else begin CASR_varCASR = CASR_reg ; CASR_outCASR [36]= CASR_varCASR [35]^CASR_varCASR [0]; CASR_outCASR [35]= CASR_varCASR [34]^CASR_varCASR [36]; // ... some lines skipped CASR_reg = CASR_outCASR; end end end
  • 68. Combine outputs always @(posedge clk or negedge reset) begin if (!reset ) begin number_o = (0); end Patrick Schaumont Spring 2008 ECE 4514 Digital Design II Lecture 6: A Random Number Generator in Verilog else begin number_o = (LFSR_reg [31:0]^CASR_reg[31:0]); end end
  • 69. Simulation .. Patrick Schaumont Spring 2008 ECE 4514 Digital Design II Lecture 6: A Random Number Generator in Verilog
  • 70. Simulation (looking at the state registers) Patrick Schaumont Spring 2008 ECE 4514 Digital Design II Lecture 6: A Random Number Generator in Verilog
  • 71. Synthesis .. Patrick Schaumont Spring 2008 ECE 4514 Digital Design II Lecture 6: A Random Number Generator in Verilog
  • 72. Place and Route .. Patrick Schaumont Spring 2008 ECE 4514 Digital Design II Lecture 6: A Random Number Generator in Verilog
  • 73. Summary Random number generators Many useful applications Linear Feedback Shift Registers: PRNG Fibonacci and Galois Maximal-length LFSR Structural Verilog Model Patrick Schaumont Spring 2008 ECE 4514 Digital Design II Lecture 6: A Random Number Generator in Verilog Flaws of Random Number Generators Bias Predictability Nonlinear Combination Generator Design by Tkacik Behavioral Verilog Model