07-verilog behavioral modeling (2)
TRANSCRIPT
-
8/8/2019 07-Verilog Behavioral Modeling (2)
1/39
Digital System DesignVerilog HDL
Behavioral Modeling (2)
Maziar Goudarzi
-
8/8/2019 07-Verilog Behavioral Modeling (2)
2/39
2005 Verilog HDL 2
Today program
Behavioral Modeling
Timing controls
Other features
-
8/8/2019 07-Verilog Behavioral Modeling (2)
3/39
Timing Controls in
Behavioral Modeling
Behavioral Modeling
-
8/8/2019 07-Verilog Behavioral Modeling (2)
4/39
2005 Verilog HDL 4
Introduction
No timing controls No advance in
simulation time
Three methods of timing controldelay-based
event-based
level-sensitive
-
8/8/2019 07-Verilog Behavioral Modeling (2)
5/39
2005 Verilog HDL 5
Delay-based
Timing ControlsDelay| Duration between encountering
and executing a statement
Delay symbol: #
Delay specification syntax:
::= #
||= #
||= # *)
-
8/8/2019 07-Verilog Behavioral Modeling (2)
6/39
2005 Verilog HDL 6
Delay-based
Timing Controls (contd) Types of delay-based timing controls
1. Regular delay control
2. Intra-assignment delay control
3. Zero-delay control
-
8/8/2019 07-Verilog Behavioral Modeling (2)
7/39
2005 Verilog HDL 7
Delay-based
Timing Controls (contd)
Regular Delay Control
Symbol: non-zero delay before a procedural assignment
Used in most of our previous examples
-
8/8/2019 07-Verilog Behavioral Modeling (2)
8/39
2005 Verilog HDL 8
Delay-based
Timing Controls (contd)
Intra-assignment Delay Control
Symbol: non-zero delay to the right of the
assignment operator
Operation sequence:
1. Compute the right-hand-side expression at the
current time.
2. Defer the assignment of the above computed value
to the LHS by the specified delay.
-
8/8/2019 07-Verilog Behavioral Modeling (2)
9/39
2005 Verilog HDL 9
Delay-based
Timing Controls (contd)
Intra-assignment delay examples
-
8/8/2019 07-Verilog Behavioral Modeling (2)
10/39
2005 Verilog HDL 10
Delay-based
Timing Controls (contd)
Zero-Delay ControlSymbol: #0
Different initial/always blocks in the same
simulation timeExecution order non-deterministic
Zero-delay ensures execution after all otherstatements
Eliminates race conditionsMultiple zero-delay statementsNon-deterministic execution order
-
8/8/2019 07-Verilog Behavioral Modeling (2)
11/39
2005 Verilog HDL 11
Delay-based
Timing Controls (contd)
Zero-delay control examples
-
8/8/2019 07-Verilog Behavioral Modeling (2)
12/39
2005 Verilog HDL 12
Event-based
Timing Control
EventChange in the value of a register or net
Used to trigger execution of a statement or
block (reactive behavior/reactivity)
Types ofEvent-based timing control1. Regular event control
2. Named event control3. Event OR control
4. Level-sensitive timing control (next section)
-
8/8/2019 07-Verilog Behavioral Modeling (2)
13/39
2005 Verilog HDL 13
Event-based
Timing Control (contd)
Regular event control
Symbol: @()
Events to specify:posedgesig Change ofsig from any value to 1
or from 0 to any value
negedgesig
Change ofsig from any value to 0or from 1 to any value
sig
Any chage in sig value
-
8/8/2019 07-Verilog Behavioral Modeling (2)
14/39
2005 Verilog HDL 14
Event-based
Timing Control (contd)
Regular event control examples
-
8/8/2019 07-Verilog Behavioral Modeling (2)
15/39
2005 Verilog HDL 15
Event-based
Timing Control (contd)
Named event control
You can declare (name) an event, and then
triggerand recognize it.
Verilog keyword for declaration: event
event calc_finished;
Verilog symbol for triggering: ->
->calc_fin
ishedVerilog symbol for recognizing: @()
@(calc_finished)
-
8/8/2019 07-Verilog Behavioral Modeling (2)
16/39
2005 Verilog HDL 16
Event-based
Timing Control (contd)
Named event control examples
-
8/8/2019 07-Verilog Behavioral Modeling (2)
17/39
2005 Verilog HDL 17
Event-based
Timing Control (contd)
Event OR control
Used when need to trigger a block upon occurrence of
any of a set of events.
The list of the events: sensitivity listVerilog keyword: or
-
8/8/2019 07-Verilog Behavioral Modeling (2)
18/39
2005 Verilog HDL 18
Level-sensitive
Timing Control
Level-sensitive vs. event-based
event-based: wait for triggering of an event
(change in signal value)
level-sensitive: wait for a certain condition (on
values/levels of signals)
Verilog keyword: wait()
alwayswait(count_enable) #20 count=count+1;
-
8/8/2019 07-Verilog Behavioral Modeling (2)
19/39
Other Features
Behavioral Modeling
-
8/8/2019 07-Verilog Behavioral Modeling (2)
20/39
2005 Verilog HDL 20
Contents
Sequential and Parallel Blocks
Special Features of Blocks
-
8/8/2019 07-Verilog Behavioral Modeling (2)
21/39
2005 Verilog HDL 21
Sequential and Parallel Blocks
Blocks: used to group multiple statements
Sequential blocksKeywords: beginend
Statements are processed in order.
A statement is executed only after its precedingone completes.Exception: non-blocking assignments with intra-
assignment delays
A delay or event is relative to the simulationtime when the previous statement completedexecution
-
8/8/2019 07-Verilog Behavioral Modeling (2)
22/39
2005 Verilog HDL 22
Sequential and Parallel Blocks (contd)
Parallel Blocks
Keywords: fork, join
Statements in the blocks are executed
concurrently
Timing controls specify the order of execution of
the statements
All delays are relative to the time the block wasentered
The written order of statements is not important
-
8/8/2019 07-Verilog Behavioral Modeling (2)
23/39
2005 Verilog HDL 23
Sequential and Parallel Blocks (contd)
initial
begin
x=1b0;
#5 y=1b1;
#10 z={x,y};#20 w={y,x};
end
initial
fork
x=1b0;
#5 y=1b1;
#10 z={x,y};
#20 w={y,x};
join
-
8/8/2019 07-Verilog Behavioral Modeling (2)
24/39
2005 Verilog HDL 24
Sequential and Parallel Blocks (contd)
Parallel execution Race conditions may ariseinitial
begin
x=1b0;
y=1b1;
z={x,y};
w={y,x};
end
z,w
can take either2b01,2b
10or2bxx,2bxx depending on simulator
-
8/8/2019 07-Verilog Behavioral Modeling (2)
25/39
2005 Verilog HDL 25
Special Features of Blocks
Contents
Nested blocks
Named blocks
Disabling named blocks
-
8/8/2019 07-Verilog Behavioral Modeling (2)
26/39
2005 Verilog HDL 26
Special Features of Blocks (contd)
Nested blocks
Sequential and parallel blocks can be mixedinitial
beginx=1b0;
fork
#5 y=1b1;
#10 z={x,y};
join
#20 w={y,x};
end
-
8/8/2019 07-Verilog Behavioral Modeling (2)
27/39
2005 Verilog HDL 27
Special Features of Blocks (contd)
Named blocks
Syntax:
begin: fork:
end join
Advantages:
Can have local variables
Are part of the design hierarchy.Their local variables can be accessed using hierarchical
names
Can be disabled
-
8/8/2019 07-Verilog Behavioral Modeling (2)
28/39
2005 Verilog HDL 28
Special Features of Blocks (contd)
module top;
initialbegin : block1
integer i; //hiera. name: top.block1.i
end
initialfork : block2reg i; //hierarchical name: top.block2.i
joinendmodule
-
8/8/2019 07-Verilog Behavioral Modeling (2)
29/39
2005 Verilog HDL 29
Special Features of Blocks (contd)
Disabling named blocks
Keyword: disable
Action:
Similar to break in C/C++, but can disable any
named block not just the inner-most block.
-
8/8/2019 07-Verilog Behavioral Modeling (2)
30/39
2005 Verilog HDL 30
Special Features of Blocks (contd)
module find_true_bit;
reg [15:0] flag;integer i;
initialbegin
flag = 16'b0010_0000_0000_0000;i = 0;
begin: block1
while(i < 16)begin
if (flag[i])begin
$display("Encountered a
TRUE bit atelement
number %d", i);disable block1;end//ifi = i + 1;
end//w
hileend// block1end//initialendmodule
-
8/8/2019 07-Verilog Behavioral Modeling (2)
31/39
Examples
Behavioral Modeling
-
8/8/2019 07-Verilog Behavioral Modeling (2)
32/39
2005 Verilog HDL 32
4-to-1 Multiplexer
// 4-to-1 multiplexer. Port lististakenexactly from
//the I/Odiagram.
module mux4_to_1(out,i0,i1,i2,i3,s1,s0);
// Portdeclarations from the I/Odiagram
outputout;
inputi0,i1,i2,i3;
inputs1,s0;
regout;//outputdeclared asregister
//recomputethesignal outif any inputsignal changes.
//All inputsignalsthat cause a recomputationof outto
//occur mustgointothe always@(...)
always@(s1ors0ori0ori1ori2ori3)
begin
case({s1,s0})
2'b00:out=i0;
2'b01:out=i1;
2'b10:out=i2;
2'b11:out=i3;
default:out=1'bx;
endcase
end
endmodule
-
8/8/2019 07-Verilog Behavioral Modeling (2)
33/39
2005 Verilog HDL 33
4-bit Counter
//Binary counter
module counter(Q, clock, clear);
// I/Oports
output [3:0]Q;
input clock, clear;
//outputdefined asregister
reg [3:0]Q;
always@(posedge clear ornegedge clock)
begin
if (clear)
Q= 4'd0;
else
//Q=(Q+1) % 16;
Q=(Q+1) ;
end
endmodule
-
8/8/2019 07-Verilog Behavioral Modeling (2)
34/39
2005 Verilog HDL 34
Traffic Signal Controller
-
8/8/2019 07-Verilog Behavioral Modeling (2)
35/39
2005 Verilog HDL 35
`define TRUE 1'b1
`define FALSE 1'b0
`define RED 2'd0
`define YELLOW2'd1
`define GREEN 2'd2
//Statedefinition HWY CNTRY
`defineS0 3'd0 //GREEN RED
`defineS1 3'd1 //YELLOW RED
`defineS2 3'd2 //RED RED
`defineS3 3'd3 //RED GREEN
`defineS4 3'd4 //RED YELLOW
//Delays
`define Y2RDELAY 3 //Yellowtoreddelay
`define R2GDELAY 2 //Redto GreenDelay
modulesig_control (hwy, cntry, X, clock, clear);
//I/Oports
output [1:0]hwy, cntry;//2 bitoutput for3statesof signal GREEN, YELLOW, RED;
reg [1:0]hwy, cntry; //declareoutputsignals areregisters
input X; //if TRUE,indicatesthatthereis caron the country road,otherwise FALSE
input clock, clear;
//Internal statevariables
reg [2:0]state;
reg [2:0]next_state;
-
8/8/2019 07-Verilog Behavioral Modeling (2)
36/39
2005 Verilog HDL 36
//Signal controllerstartsinS0state
initial begin
state=`S0;
next_state=`S0;
hwy =`GREEN;
cntry =`RED;
end
always@(posedge clock) //state changesonly atpositiveedgeof clock
state=next_state;
always@(state) //Computevaluesof mainsignal and country signal
begin
case(state)
`S0: begin
hwy =`GREEN;
cntry =`RED;
end
`S1: begin
hwy =`YELLOW;
cntry =`RED;
end
`S2: begin
hwy =`RED;
cntry =`RED;
end`S3: begin
hwy =`RED;
cntry =`GREEN;
end
`S4: begin
hwy =`RED;
cntry =`YELLOW;
end
endcaseend
-
8/8/2019 07-Verilog Behavioral Modeling (2)
37/39
2005 Verilog HDL 37
//State machineusing casestatements
always@(stateor clearor X)
begin
if(clear)
next_state=`S0;
else
case(state)`S0:if( X)
next_state=`S1;
else
next_state=`S0;
`S1: begin//delay somepositiveedgesof clock
repeat(`Y2RDELAY) @(posedge clock) ;
next_state=`S2;
end
`S2
:begi
n//delay someposi
tiveedgesof clock
repeat(`R2GDELAY) @(posedge clock)
next_state=`S3;
end
`S3:if( X)
next_state=`S3;
else
next_state=`S4;
`S4: begin//delay somepositiveedgesof clock
repeat(`Y2RDELAY) @(posedge clock) ;
next_state=`S0;
end
default:next_state=`S0;
endcase
end
endmodule
-
8/8/2019 07-Verilog Behavioral Modeling (2)
38/39
2005 Verilog HDL 38
Today Summary
Timing control in behavioral statementsRequired to advance simulation time
Different types
Delay-based
Event-based
Level-sensitive
Sequential and Parallel Blocks
Special Features of BlocksNested blocks
Named blocks
Disabling named blocks
-
8/8/2019 07-Verilog Behavioral Modeling (2)
39/39
2005 Verilog HDL 39
Other Notes
Homework 6
Chapter 7, the remaining exercises (no. 5-9, 11,
16-18)
Due date: Next Sunday (Azar 13th)