Verilog Programing - ch3 - Design - Abstractions - Handouts
Verilog Programing - ch3 - Design - Abstractions - Handouts
Module Unit
Topics Ref. Hrs.
No. No.
3 Design abstractions and Modeling Styles 1,2 4
3.1 Design Abstractions, Behavioral, Data flow, Gate level and Switch level modelling
3.2 Procedural Assignment and Continuous Assignment
2
LEVELS OF ABSTRACTION
• Behavioral Level : Used to model behavior of design without concern for the
hardware implementation details. Designing at this level is very similar to C
programming.
• Dataflow Level [ RTL ] : Module is specified by specifying the data flow. The designer
is aware of how the data flows between registers.
• Gate Level : Module is implemented in terms of logic gates & interconnections
between these gates. Design at this level is similar to describing design in terms of
gate level logical diagram.
• Switch Level : Lowest level of abstraction provided by verilog. Module can be
implemented in terms of switches, storage nodes & interconnection between them.
3 Moulding Engineers Who Can Build the Nation
Switch Level
(synthesis only)
Custom ASIC
Gate Level
(synthesis only) Technology
Technology ASIC/PLD Independent
RTL Level
Specific
(synthesis only)
ASIC/PLD
Behavioral Level
ASIC/PLD
6
Dataflow style: Verilog Code
8
Behavioral style: Verilog Code
10
DATA FLOW MODELING
— Continuous Assignment
— Delayed Continuous Assignment
11
CONTINUOUS ASSIGNMENT
— A continuous assignment is the most basic statement in DATA FLOW modeling.
input A,B,C;
output Y.
assign Y = ~ (( A & B ) | C)
12
CONTINUOUS ASSIGNMENT
Continuous assignment characteristics :
— Identifier being assigned must be of type wire.
— Assignments can be scalar or vector.
— Values being Read [ on RHS ] can be registers or nets.
— Continuous assignment can be done,
— Implicitly, i.e value is assigned, while the wire is being declared
— Explicitly, i.e . By having the declaration and assignment statements separate.
13
Assign
• The “assign” technique is called a “continuous assignment.”
• Assign can be used only on wire types
• Continuous assignment
—Left operand continuously gets a new value
• E.g.
—assign c = a & b;
14
Two-input NAND gate based on assign keyword
module example(c, a, b);
input a, b; a
output c; c
b
// Functionality
assign c = ~(a & b);
endmodule
If there are several continuous assignments, the order does not matter, they are
executed concurrently.
Example: assign c=~(a&b);
assign d=a^b;
The code has to be modified accordingly adding the signal “d” as an output.
Signals a, b, c are wires by default, we do not have to declare them in this example.
15
17
18
BEHAVIORAL MODELING
19
BEHAVIORAL MODELING
20
BEHAVIORAL MODELING FEATURES
— Describes the functionality in an algorithmic manner.
— Code independent of vendor technology.
— But difficult to visualize the actual hardware.
— Logic structure implementation left to the synthesis tool.
21
BEHAVIORAL MODELING
Ø In Verilog the behavioral representation is described using two procedural blocks :
Ø initial block : Executes at beginning of simulation (time 0 ) only once & then is
suspended forever.
Ø always block : Starts at time 0. All the statements are executed in an infinite loop as
long as simulation is performed.
Note: Procedural Blocks are the sequential region of a verilog module
reg clock; always @ (....)
initial begin Sensitivity list
clock = 1’b0;
always
#10 clock = ~ clock; end
initial
• A variable should be written only in one always block
#1000 $finish;
• The sensitivity list cannot contain the outputs (left-side
As shown clock initialization is done in variables) of the always block
Separate initial block. $finish is used to • Assign cannot be used within an always block
22 stop simulation at 1000 time units. • Multiple always blocks are executed in parallel
BEHAVIORAL MODELING
— Always blocks are used to model synchronous & combinational Logic.
- Sensitivity list provides event control for always block.
- All signals feeding into [being read] the always block must be listed in the sensitivity List.
input d, clk;
output q;
reg q;
always @ (posedge clk)
begin
q = d;
end
23
25
BEHAVIORAL MODELING
reg x, y; — Statements in procedural blocks
initial can be grouped to execute either
begin sequentially or parallel.
x = 1’b0; // executes at time = 0 1. Sequential block :
y = 1’b1; // executes at time = 0 — Statements are enclosed within
#10 x= 1’b1; // executes at time = 10 the keywords begin & end.
— Statements are processed in
#15 y = 1’b0; // executes at time = 25 the order they are specified.
#10 x = 1’b0; // executes at time = 35 — Delays specified are additive.
end
26
BEHAVIORAL MODELING
reg x,y; 2. Parallel block :
initial — Statements are enclosed within the
fork
keyword fork and join
x= 1’b0;
y= 1’b1; — All the statements are executed
#10 x= 1’b1; //executes at time = 10. concurrently. Order in which
#20 y= 1’b0; //executes at time = 20. statements are written is not
#20 x= 1’b0; //executes at time = 20. important.
#40 y= 1’b1; //executes at time = 40. — If the delay is specified, it is relative
join to the time the block was entered.
fork- join not supported for synthesis !!
27
28
always block in Verilog Always block with a sensitivity list
Clock Generator
29
30
Named block example: Disable named block example:
31
PROCEDURAL ASSIGNMENT
— There are two kinds of procedural assignments
— Blocking assignment
— Specified by the operator =
— All the statements within the sequential block are executed sequentially.
— Blocks the execution of operations after it till it is executed -» sequential
operation (don't use it unless really necessary)
— Non blocking assignment
— Specified by the operator <=
— All the statements within the sequential block are executed concurrently
…hardware-like operation
32
BLOCKING ASSIGNMENT
— Blocks the subsequent assignments in the same always block from
occurring until after the current assignment has completed.
reg x, y, z;
always @ (posedge clk) integer a, b;
begin initial
word [15:8] = word [7:0]; begin
word [7:0] = word[15:8]; x = 0; y = 1; //executed at time = 0
end #15 a = 22; //executed at time = 15
#10 b= 33; //executed at time = 15 +10 = 25
end
Dose this perform
a swap ? Similar to Variables hence update immediately.
33
Blocking Assignments
34
RACE AROUND CONDITION !!
— When blocking assignments in two or more always blocks are scheduled to execute
in the same time step, the order of execution is indeterminate & can cause a race
around condition
always @ (posedge clk)
a = b;
35
36
NON BLOCKING ASSIGNMENT
— All the statements within the sequential block are executed concurrently.
reg x, y, z;
integer a, b; always @ (posedge clk)
begin
initial
word[15:8]<=word[7:0];
begin
word[7:0] <=word[15:8];
x = 0; y = 1; //executed at time = 0
end
a <= #15 22; //executed at time = 15
b <= #10 33; //executed at time = 10; does not wait for above statement
end
37
38
39
• Use “if-else” to connect output “Y” to Here the conditional (ternary) operator is used.
one of the two data inputs based on the Read statement like this: “Is the control signal
value of the data selector “Sel”. ‘Sel’ equal to 1? If yes, Y is assigned the value B
• The expression inside the parentheses else A.”
after “if” must evaluate to either “1”
(true) or “0” (false).
41
• Synchronous reset
always @ (posedge clk)
if (rst)
D[0] Q[0]
c <= 1’b0,
else
c <= a & b,
• Asynchronous reset
always @ (posedge clk, posedge rst)
if (rst)
c <= 1’b0,
else
42 c <= a & b,
Blocking & Non-Blocking – Hardware inferred
43
44
D flip-flop, positive-edge triggered, with Q_bar
•In this example the ‘assign’ statement is used.
module D_FF (D, Clock, Q); •The ‘always’ block implements most of the desired behavior,
input D, Clock;
output Q; namely, a single-bit storage device with appropriate behavior
reg Q;
and clocking.
wire Q_bar;
•The ‘assign’ statement simply copies the Q signal to an
assign Q_bar = ~Q; additional output while performing the necessary inversion
always @ (posedge Clock)
Q <= D; along the way.
endmodule
•A variable on the left side of the equal sign must not be in the
trigger list or the machine may go into an infinite loop.
always@(state or A or B or C….)
begin
A<=B+C; //A will immediately retrigger the always procedure
45
always @ (posedge Clock or negedge _Preset) Avoid the temptation to design arbitrary
if (_Preset == 0) flip-flop behavior, e.g., ability to trigger on
Q <= 1;
else both edges of the same clock, ability to
Q <= D; trigger on multiple clock signals, etc. The
endmodule
hardware synthesis tool does not
“magically” create new hardware from thin
air! You have to write circuit descriptions
that are physically possible, that is, can be
mapped onto existing (known) hardware
47
elements such as standard D flip-flops.
48
Non Blocking Procedural assignments
49
50
TIMING CONTROLS
— Timing controls are associated with procedural assignment.
i.e. They are to be used in Procedural Blocks only.
51
DELAY CONTROL
REGULAR DELAY CONTROL :
— It is of the form
#delay procedural_statement.
— It means “wait for delay” before executing the statement.
52
DELAY CONTROL
— INTRA ASSIGNMENT DELAY CONTROL :
A delay appearing on the R.H.S of an assignment operator.
— Simulates Transport Delay.
initial
begin Takes value of x & y at time 0, evaluates x+y
x = 0, y = 1 & assigns this value after 5 time units.
z = #5 x+y;
end
53
EVENT CONTROL
@<posedge,negedge><signal>
— Symbol @ is used to specify an event control.
posedge 1 negedge — Execution is delayed until the transition occurs
on the signal
0
Ø Level-sensitive control
54
REGULAR EVENT CONTROL
input d,clk; input x,y,z;
output q; output q;
reg q; reg q;
always @ (posedge clk) always @ ( x or y or z )
begin begin
q = d; q = x ^ y ^z;
end end
— All the signals feeding into the always block must be listed in the sensitivity list.
55
EVENT OR CONTROL
— Events can also be Or’ed to indicate “ if any of the events occur” then procedural block
will be executed.
56
NAMED EVENT CONTROL
— Verilog provides capability to declare an event, then trigger & then recognize the
occurrence of that event.
— Named event is declared by keyword event.
— Symbol –> is use to trigger the event
always @ (received-data)
data-buf = { data_pkt[0], data_pkt[1], data_pkt[2]}
//when there is event on received data, packets of received data are stored in data buffer.
57
58
Regular event control
59
Event OR control
60
Named event control
61
62
CONDITIONAL STATEMENTS – if
if (condition 1) always @ (a or b )
procedural-statement 1 begin
if (a==1’b1)
else if (condition 2)
q = r;
procedural-statement 2 else if (b==1’b1)
else q = s;
procedural-statement 3 else
end q = t;
end
63
if statement in Verilog
64
CONDITIONAL STATEMENT - case
— Case statement does an identity comparison (including x & z).
— Expression is compared with each case alternative beginning with first & only
the first match is executed.
— If no match is found default clause is executed.
— When the same input is checked under each conditions case is an simpler
alternative to an if.
— Note: Case statement inVerilog also generates a Prioritised hardware
66
DEFAULT IN A case
— Output must be explicitly or implicitly defined for all possible
values of the case expression to avoid inferring of latches.
— Default output can be described
— Before the case statement with a variable assignment
— In case statement with a default clause.
always@(a or b or d or sel) always@(a or b or d or sel)
begin begin
q=1'b0; case (sel)
case (sel) 2'b00 : q=a;
2'b00 : q=a; 2'b01 : q=b;
2'b01 : q=b; 2'b11 : q=d;
2'b11 : q=d; default : q=1'b0;
endcase endcase
end end
67
69
70
4:1 Mux Verilog implementation
71
72
casex statement example
73
74
For loop
75
77
Syntax: forever
procedural assignment
79
80
Repeat loop
81
82
assign and deassign
always @ ( negedge clk )
begin
ØWhen reset goes high procedural continuous assignment
q = d; overrides the effect of regular procedural assignment.
qb = ~d;
always @ ( reset ) ØThe register variable retains the continuously assigned
if (reset) value after the deassign until they are changed by future
begin
assign q = 1’b0;
procedural assignment.
assign qb = 1’b1;
end Application: Using this a single always can be written to
else initialize a system on RESET, instead of having if rst =1’b1
begin kind of statement in every always block.
deassign q;
deassign qb;
end
83
— Forced value on net overrides any continuous assignments until the net is released.
85
86
STRUCTURAL MODELING
87
88
MODULE INSTANTIATION
— Instantiation is a way to include sub designs in a top level module.
89
MODULE INSTANTIATION
—Named instantiation - Syntax
<module name> <instance name>(.<port1>(signal1),.<port2>(signal2));
eg: and1 u1 ( .a( in1),.b( in2),.y(out1) );
—Positional instantiation - Syntax
<module name> <instance name>(<signal1>,<signal2>);
eg: and1 u1 (in1,in2,out1);
— Unconnected signals are designated by two comma’s with no signal listed.
In both examples a two input AND gate is instantiated with input ports a, b
(connected to in1, in2) & output port y (connected to out1).
90
MODULE INSTANTIATION
— module add4 is a 4 bit full adder designed by instantiating a single bit full adder.
module add4 (result, carry, r1, r2, ci);
output [3:0] result;
output carry;
input [3:0] r1,r2;
input ci;
wire [3:0] r1,r2,result;
wire ci,carry,c1,c2,c3;
addbit u1 (r1[0], r2[0], ci, result[0], c1);
addbit u2 (r1[1], r2[1], c1, result[1], c2);
addbit u3 (r1[2], r2[2], c2, result[2], c3);
addbit u4 (r1[3], r2[3], c3, result[3], carry);
endmodule
91
92
MULTIPLE INPUT GATES
nand U1 ( y, a, b); xor U2 (z,a,b,c,d);
// instantiates two input nand //instantiates 4-input gate
gate with instance name U1 with instance name U2
— Truth tables for these gates define how outputs for the gates are computed from inputs
nand 0 1 x z
0 1 1 1 1
1 1 0 x x
x 1 x x x
z 1 x x x
93
buf 0 1 x z
output 0 1 x x
94
TRISTATE GATES
— tri-state gates are used when multiple drivers drive the signal.
95
PULL GATES
— These model Pull Ups and Pull downs. Hence have only one output
and no input.
— Generally used on unconnected signals and tristate outputs.
Syntax Usage
— pullup p1 (data) // data is connected to ‘ logic 1 ’.
— pulldown p1 (data) // data is connected to ‘ logic 0 ’.
96
GATE DELAYS
— Verilog primitives can be modeled according to the functionality as well as
required timing.
— Three types of delays can be modeled
— Rise delay : Delay associated with gate when output changes to 1.
1
0, x or z
— Fall delay : Delay associated with gate when output changes to 0.
1, x or z
0
— Turn-off delay : Delay associated with gate when output changes from “1, 0, x” to high
impedance value.
97
GATE DELAYS
— Types of delay specification
98
GATE DELAYS
— For each delay it’s minimum, maximum & typical value also can be specified.
Min/typ/max values are used to model devices whose delays vary within
minimum to maximum range due to variation in IC fabrication process
99
100
Gate delays
101
102
SWITCH LEVEL MODELING
103
104
MOS SWITCHES
— Two types of MOS switches: nmos and pmos
— NMOS switch
— PMOS switch
x z x x x x x z x x
z z z z z z z z z z
— Note : NMOS conducts when its control is high while PMOS conducts when its control is low.
— nmos (O,I,G) // instantiates NMOS switch, no instance name
O = output, I = input , G = control
105
107
BIDIRECTIONAL SWITCHES
— MOS & CMOS switches are unidirectional, conduct from drain to source.
— Bidirectional switches conduct in both directions.
— Used to provides isolation between input & output.
108
RESISTIVE SWITCHES
— MOS, CMOS & Bidirectional [tran] switches are modeled with Zero ON_Resistance.
To model them with ON-Resistance primitives rnmos, rpmos, rcmos, rtran, rtranif0,
rtranif1 are provided.
input output
— Features of resistive switches strength strength
— High source to drain impedance. supply pull
Regular switches have a low source to drain impedance. strong pull
pull weak
— Hence when a signal passes through a resistive switch its
strength reduces. weak medium
Regular switches retains the signal strength. large medium
medium small
small small
high high
109
110
Power and ground Bidirectional switches
111
Resistive switches
112
Summary……
113
114
115
116
117
118