Lecture 4: Continuation of Systemverilog
Lecture 4: Continuation of Systemverilog
SystemVerilog
Last Lecture: Divide by 3 FSM
• Output should be “1” every 3 clock cycles
S2
S0
S1
The double circle indicates the reset state
Slide derived from slides by Harris & Harris from their book 2
Finite State Machines (FSMs)
• A simple Moore machine looks like the
following
CLK
M next
next k state k output N
inputs state
state
outputs
logic
logic
Slide derived from slides by Harris & Harris from their book 3
FSM Example in SystemVerilog
module divideby3FSM (input logic clk, reset_n,
output logic q);
// output logic
assign q = (state == S0); output is “1” every clock cycles when we are in state S0
endmodule
4
FSM Example in SystemVerilog
module divideby3FSM (input logic clk, reset_n,
output logic q);
// output logic
assign q = (state == S0);
endmodule
5
What asynchronous reset means
• “Negative-edge asynchronous reset” means
the following:
clk
6
Continuing with the FSM Example
• What if we want to design the “Divide by 3 FSM”
example with just one “always_ff” statement (no
separate “assign” statement)?
10
RTL Example
reset_n clk
module rtl_example
clocked always block c logic always block
a 0 00
+ 01 c t
8 f
10 <<1 +1
11 8 8 8
–
b
8
count
0 00
+1 01
10 count
11 8
myinc
s1 00 state
s2 01
10 state
s0 2
11
11
Fibonacci Calculator
• F(0) = 0, F(1) = 1
• F(n) = F(n – 1) + F(n – 2), when n > 1
• Examples:
12
Fibonacci Calculator
• Design a FSM with the interface below.
• input_s is “n”, and fibo_out is “F(n)”.
• Wait in IDLE state until begin_fibo.
• When testbench sees done==1, it will check if fibo_out==
F(input_s).
module fibonacci_calculator (input logic clk, reset_n,
input logic [4:0] input_s,
input logic begin_fibo,
output logic [15:0] fibo_out,
output logic done);
...
always_ff @(posedge clk, negedge reset_n)
begin
... clk
end fibonacci_ fibo_out
reset_n
endmodule input_s calculator
done
begiin_fibo 13
Fibonacci Calculator
• Basic idea is to introduce 3 registers:
logic [4:0] counter;
logic [15:0] R0, R1;
• Set loop counter to “n”
counter <= input_s;
• Repeat as long as counter is greater than 1 since we
already know what F(0) and F(1) are:
counter <= counter – 1;
R0 <= R0 + R1;
R1 <= R0;
• Finally, set output to “F(n)”
done <= 1;
fibo_out <= R0;
14
Fibonacci Calculator
module fibonacci_calculator (input logic clk, reset_n,
input logic [4:0] input_s,
input logic begin_fibo,
output logic [15:0] fibo_out,
output logic done);
enum logic [1:0] {IDLE=2'b00, COMPUTE=2'b01, DONE=2'b10} state;
15
Fibonacci Calculator
• A three state solution is provided.
17