Ic Overview Session8 Verilog Part5 Parameter and FSM
Ic Overview Session8 Verilog Part5 Parameter and FSM
1
COURSE INTRODUCTION
Khóa Học Thiết Kế Vi Mạch Cơ Bản - Trung Tâm Đào Tạo Thiết Kế Vi Mạch ICTC
2
SYMBOL LEGEND
SUMMARY
HOMEWORK
QUESTION
SELF-LEARNING
3
Session 08: Verilog
Fundamental - Part 5 - 1. Parameter
3. Function
.
4. Generate
5. FSM
4
1. Parameter
3. Function
4. Generate
5
WHY NEED TO USE PARAMETER?
▪ Below is our counter example. What if we want to change to 16-bit?
module counter( module counter(
input wire clk, input wire clk,
input wire rst_n, input wire rst_n,
output wire overflow, output wire overflow,
output reg [7:0] count output reg [15:0] count
) )
endmodule endmodule
6
PARAMETER
endmodule
8
PARAMETER DECLARATION
▪ Parameter can be declared as below
module aaa ( <port_list> ); module aaa #(parameter PAR1 = VAL_PAR1,
//parameter list parameter PAR2 = VAL_PAR2)
parameter PAR1 = VAL_PAR1; (<port_list>);
parameter PAR2 = VAL_PAR2;
… //port & data type declaration
//ports & data type declaration //logic description
//logic description endmodule
endmodule
▪ The second way is useful if the port declaration is inside the port list
module counter module counter #(parameter CNT_W=8)
( input wire clk , ( input wire clk , Compiler can
Compiler can input wire rst_n, input wire rst_n, understand
not understand the CNT_W
the CNT_W output reg [CNT_W-1:0] cnt output reg [CNT_W-1:0] cnt
); );
parameter CNT_W = 8; …
endmodule endmodule
9
PARAMETER OVERRIDE
▪ Parameters can be overridden with new values during module instantiation
module top #(parameter ADDR_W=8, module tb;
parameter DATA_W=16) //module instantiation override: override by position
( top #(16, 32) u_dut ( <port_list>);
input wire [ADDR_W-1:0] addr, endmodule
output wire [DATA_W-1:0] data
module tb;
);
//module instantiation override: override by name
… This way is
top #(.DATA_W(32), .ADDR_W(16)) u_dut ( <port_list>);
recommended
endmodule
endmodule
module tb;
//module instantiation
top u_dut ( <port_list>);
//Override using defparam
defparam dut.ADDR_W = 16;
defparam dut.DATA_W = 32;
endmodule
If parameters are not overriden the during module instantiation, they will keep the
10
default values declared the module.
PARAMETER PASSING
▪ Parameters can be passed from upper hierarchy.
module tb;
module top #(parameter ADDR_W=8,
parameter TB_ADDR_W = 16;
parameter DATA_W=16)
parameter DATA_W = 32;
(
//passing the parameter to module
input wire [ADDR_W-1:0] addr,
top #(.ADDR_W (TB_ADDR_W, .DATA_W(DATA_W)) u_dut ( <port_list>);
output wire [DATA_W-1:0] data
…
);
endmodule
…
endmodule
11
1. Parameter
3. Function
4. Generate
12
DEFINE (SELF-LEARNING)
▪ “define” is a compiler directive used to define macros, which are essentially text substitutions.
▪ “define” can be used similar to parameter, but it is global, means all the module in the file will be
affected by this define
`define ADDR_W 8
`define DATA_W 16
module top (
input wire [`ADDR_W-1:0] addr,
input wire [`DATA_W-1:0] data
);
…
Both top and sub
endmodule
module use same define
module sub (
input wire [`ADDR_W-1:0] sub_addr,
input wire [`DATA_W-1:0] sub_data
);
…
endmodule
13
DEFINE (SELF-LEARNING)
▪ When using “define”, it is recommended to create a file as shown below
def.v
`define ADDR_A 8’h00
`define ADDR_B 8’h04
module top;
`include “def.v”
…
endmodule
module counter(
input wire clk,
always @ (posedge clk or negedge rst_n) begin
input wire rst_n,
if( !rst_n)
output wire overflow,
count <= 8’h00;
output reg [7:0] count
else
)
count <= count + 1’b1;
`ff_nrst
end
if( !rst_n)
count <= 8’h00;
else
count <= count + 1’b1;
end
endmodule
15
CONDITIONAL COMPILATION (SELF-LEARNING)
▪ Compiler directive `ifdef and `endif can be used for conditional compilation
module test (
input wire a ,
input wire b , test
`ifdef EXTD
input wire c, a
z
`endif b
output wire z
c
);
16
1. Parameter
3. Function
4. Generate
17
FUNCTION
▪ Function is a procedural block to create combinational logic.
▪ Function can be used to split the code to smaller parts that can be reused.
Calling a function:
LHS = function_name( argument1, argument2)
Note: this can be a continuous assignment or procedural assignment 18
FUNCTION RULE
19
FUNCTION EXAMPLE
module top (
input wire in1,
input wire in2,
input wire in3,
input wire in4, u_and_gate_00 top
output wire out and_gate
); in1 out12 u_and_gate_02
wire out12; in2 and_gate
wire out34;
assign out12 = and_gate( in1, in2); u_and_gate_01 out
assign out34 = and_gate( in3, in4);
and_gate
assign out = and_gate( out12, out34);
in3
in4 out34
function and_gate;
input a;
input b;
begin
and_gate = a & b;
end
Instead of using module as in previous
endfunction sessions, we can use function (only for
endmodule combinational logic)
20
1. Parameter
3. Function
4. Generate
21
LOOP GENERATE
Loop generate constructs allow for moudules with repetitive structure to be described in more
simple way.
The loop index must be declared by
“genvar”. This index is just for the synthesis
tool to know this is generate loop index. It
should not be used outside the loop
generate
genvar i;
assign c[0] = a[0] & b[0];
assign c[1] = a[1] & b[1]; generate
assign c[2] = a[2] & b[2]; for( i=0; i<5; i=i+1) begin :<name> //(name is optional)
assign c[3] = a[3] & b[3]; assign c[i] = a[i] & b[i];
assign c[4] = a[4] & b[4]; end
These are
equivalent
endgenerate
22
CONDITIONAL GENERATE
▪ Conditional generate construct can be used to select which code is active based on parameter.
▪ The simulation and synthesis tool only process with the codes corresponding to the parameter value.
23
FSM OVERVIEW
❑ FSM stands for Finite-State Machine.
❑ FSM is used to describe:
❖ The behavior of a digital system with a finite number of states.
❖ Transitions between these states.
❖ Actions taken in response to these transitions.
❑ The FSM has advantages:
❖ It makes it easy to control the operating sequence.
❖ It simplifies the debugging process for the design.
FSM is crucial for modeling and controlling sequential logic in digital circuits.
For example
AB: state ▪ State = 00, x = 0 -> next state = 00, y = 0
x: input ▪ State = 00, x = 1 -> next state = 01, y = 0
▪ State = 01, x = 0 -> next state = 00 , y = 0
y: output
….
▪ State = 11, x = 1 -> next state = 00, y = 1
FSM COMPONENTS
Feedback path
General diagram of FF
and combinational logic input Combinaitonal
output
Combinaitonal
with feedback path that logic State logic
rst_n
FSM CLASSIFICATION
Comparison
A game machine has 2 states: locked or unlocked. Normally, it is in the locked state; when a coin is
inserted, it unlocks. When the start button is pressed, it locks again. Design FSM to control this game
machine.
clk
rst_n
fsm_ctrl lock
coin
start
EXAMPLE OF FSM DESIGN
coin=1
coin=0 LOCK UNLOCK start=0
start=1
30
FSM TIMING DIAGRAM
{signal: [
{name: 'clk', wave: 'p..........'},
{name: 'rst_n', wave: '01.........'},
{name: 'coin', wave: '0...1.0....'},
{name: 'start', wave: '0......1.0.'},,
{name: 'state', wave: '=....=..=..',data:["LOCK","UNLOCK","LOCK"]},
{name: 'lock (mealy)', wave: '1...0..1...'},
{name: 'lock (moore)', wave: '1....0..1..'},
]}
31
LOGIC DIAGRAM
0
UNLOCK 1
LOCK
state_pre D ==
coin Q
state LOCK
UNLOCK D F.F lock (moore)
0 CK
LOCK 1 rst_n
==
lock (mealy)
start LOCK
32
FSM PRACTICE
33
FSM CLASSIFICATION
35
RTL DESIGN FLOW RECAP
Let’s review again the RTL Design Flow and see how we can apply into this course
36
DESIGN SPECIFICATION EXAMPLE
37
DESIGN SPECIFICATION EXAMPLE
clk
rst_n overflow
count_en counter
count[7:0]
count_clr
Block diagram
38
DESIGN SPECIFICATION EXAMPLE
39
Input-Output description
DESIGN SPECIFICATION EXAMPLE
40
DESIGN SPECIFICATION EXAMPLE
Part 5: describe logic diagram (can use excel, power point, xcircuit)
0
0 count[7:0]
+1 1
8’h00 1
overflow
clk ==
count_en 8’hff ?
count_clr
rst_n
41
SESSION 8
SUMMARY
SUMMARY:
❑ Parameter can be used to improve the readability and reusability.
❑ “define” can be used as macro or to replace text to simplified common logic.
❑ Function is a procedural block to create combinational logic and can be used to split
the code to smaller parts that can be reused.
❑ There are 2 types of generate: loop generate of conditional generate.
❑ FSM is an application of flip-flop feedback loop. The feedback loop is used to derive
next state.
❑ Need to follow the RTL design flow, do specification making before RTL coding !!!
42
Homework
clk count[CNT_W-1:0]
rst_n counter
overflow
43
TIP
WIDTH ?= 8 //declare a variable. If user don’t type it in the command line, default value is 8
run:
vsim […] -GCNT_W=$(WIDTH) //assign CNT_W parameter = WIDTH variable
Run command:
>make build
>make run WIDTH=16 //run the simulation with CNT_W = 16
or simply type
> make all WIDTH = 16
44
Homework
45
FSM PRACTICE
Homework2(*): Design an FSM to detect a sequence of “1011” in a given bit stream.
Note: the left most bit come first.
▪ “match” flag is asserted only when it detects the correct sequence
▪ Copy /ictc/student_data/share/ico/08_ss8/detect_string to your 08_ss8
▪ Draw state transition diagram and logic diagram (can choose either Moore or Mealy FSM) [4p]
▪ Write code in the detect_string.v [3p]
▪ Write simple testbench to drive the stream to DUT [3p]
clk
rst_n match
detect_string
stream
110011001011001010101101101011111
Bit order match match match