Digital Design Using Verilog HDL - U3
Digital Design Using Verilog HDL - U3
asia
UNIT - III
BEHAVIORAL MODELING
Introduction, Operations and Assignments, Functional Bifurcation, Initial Construct, Always Construct,
Examples, Assignments with Delays, Wait construct, Multiple Always Blocks, Blocking and Non
blocking Assignments, The case statement, iƒ and iƒ-else constructs, Assign-de-assign construct, repeat
construct, for loop , The disable construct, while loop, forever loop, Parallel blocks, Force-release,
construct, Event
--------------------------------------------------------------------------------------------------------------------------------
INTRODUCTION
Behavioral level modeling constitutes design description at an abstract level. One can visualize
the circuit in terms of its key modular functions and their behavior; it can be described at a functional
level itself instead of getting bogged down with implementation details.
jntuworldupdates.org Specworld.in
Smartzworld.com Smartworld.asia
form a set of two procedures placed within an always block. Generally they are carried out
sequentially in the order specified
The sequential nature of the assignments requires the operands on the left of the assignment to be of reg
(variable) type.
FUNCTIONAL BIFURCATION
Design description at the behavioral level is done in terms of procedures of two types; one involves
functional description and interlinks of functional units. It is carried out through a series of blocks under
an ―always‖
The second concerns simulation – its starting point, steering the simulation flow, observing the process
variables, and stopping of the simulation process; all these can be carried out under the ―always‖ banner,
an ―initial‖ banner, or their combinations. However, each always and each initial block initiates an
activity flow during simulation
In general the activity with all such blocks starts at the simulation time and flows concurrently during the
whole simulation process
A procedure-block of either type – initial or Always
Structure of a typical procedural block
35 | P a g e
jntuworldupdates.org Specworld.in
Smartzworld.com Smartworld.asia
initial #2 a=0;
If more than one procedural assignment is to be carried out in an initial block. All such assignments are
grouped together between ―begin‖ and ―end‖ declarations.
The following are to be noted here:
Every begin declaration must have its associated end declaration.
begin – end constructs can be nested as many times as desired.
Local Variables
Variables used exclusively within a block can be declared within it. Such a variable need not be
declared outside, in the module encompassing the block. Such local declarations conserve memory and
offer other benefits too. Regs declared and used within a block are static by nature. They retain their
values at the time of leaving the block. The values are modified only at the next entry to the block.
INITIAL CONSTRUCT
A set of procedural assignments within an initial construct are executed only once – and, that too,
at the times specified for the respective assignments The initial process is characterized by the following
In any assignment statement the left-hand side has to be a storage type of element (and not a net).
It can be a reg, integer, or real type of variable. The right-hand side can be a storage type of
variable (reg, integer, or real type of variable) or a net.
All the procedural assignments appear within a begin–end block
All the procedural assignments are executed sequentially – in the same order as they appear in the
design description.
The initial block above does three controlling activities during the simulation run.
Initialize the selected set of reg's at the start.
Change values of reg's at predetermined instances of time. These form the inputs to the module(s)
under test and test it for a desired test sequence.
Stop simulation at the specified time.
36 | P a g e
jntuworldupdates.org Specworld.in
Smartzworld.com Smartworld.asia
ALWAYS CONSTRUCT
The always process signifies activities to be executed on an ―always basis.‖ Its
essential characteristics are:
Any behavioral level design description is done using an always block.
The process has to be flagged off by an event or a change in a net or a reg.
The process can have one assignment statement or multiple assignment statements. In the latter
case all the assignments are grouped together within a ―begin – end‖ construct.
Normally the statements are executed sequentially in the order they appear.
Event Control
The always block is executed repeatedly and endlessly. It is necessary to specify a condition or a set of
conditions, which will steer the system to the execution of the block. Alternately such a flagging-off can
be done by specifying an event preceded by the symbol ―@”.
@(negedge clk) : executes the following block at the negative edge of the reg (variable) clk.
@(posedge clk) : executes the following block at the positive edge of the reg (variable) clk.
@clk : executes the following block at both the edges of clk.
The events can be changes in reg, integer, real or a signal on a net. These should be declared
beforehand.
No algebra or logic operation is permitted as an event. The OR'ing signifies ―execute the block if
any one of the events takes place.‖
The positive transition for a reg type single bit variable is a change from 0 to1.
For a logic variable it is a transition from false to true.
The ―posedge‖ transition for a signal on a net can be of three different types:
0 to1
0 to x or z
x or z to 1
The ―negedge‖ transition for a signal on a net can be of three different types:-
37 | P a g e
jntuworldupdates.org Specworld.in
Smartzworld.com Smartworld.asia
1 to 0
1 to x or z
x or z to 0
If the event specified is in terms of a multibit reg, only its least significant bit is considered for the
transition. Changes in the other bits are ignored. The event-based flagging-off of a block is applicable
only to the always block.
According to the recent version of the LRM, the comma operator (,) plays the same role as the keyword
or. The two can be used interchangeably or in a mixed form. Thus the following are identical: @ (a or b
or c)
@ (a or b, c)
@ (a, b, c)
@ (a, b or c)
EXAMPLES
Versatile Counter module
counterup(a,clk,N);
input clk;
input[3:0]N;
output[3:0]a;
reg[3:0]a;
initial a=4'b0000;
always@(negedge clk) a=(a==N)?4'b0000:a+1'b1;
endmodule
TEST_BENCH
module tst_counterup;
reg clk;
reg[3:0]N;
wire[3:0]a;
counterup c1(a,clk,N);
38 | P a g e
jntuworldupdates.org Specworld.in
Smartzworld.com Smartworld.asia
initial
begin
clk = 0;
N = 4'b1011;
end
always #2 clk=~clk;
initial $monitor($time,"a=%b,clk=%b,N=%b",a,clk,N);
endmodule
module counterdn(a,clk,N);
input clk;
input[3:0]N;
output[3:0]a;
reg[3:0]a;
initial a =4'b0000;
endmodule
module updcounter(a,clk,N,u_d);
input clk,u_d;
input[3:0]N;
output[3:0]a;
reg[3:0]a;
initial a =4'b0000;
39 | P a g e
jntuworldupdates.org Specworld.in
Smartzworld.com Smartworld.asia
module clrupdcou(a,clr,clk,N,u_d);
input clr,clk,u_d;
input[3:0]N;
output[3:0]a;
reg[3:0]a;
initial a =4'b0000;
always@(negedge clk or posedge clr) a = (clr) ? 4'h0 : ( (u_d) ? ( (a==N) ? 4'b0000 : a+1'b1) :(
(a == 4'b0000) ? N : a - 1'b1));
endmodule
module shifrlter(a,clk,r_l);
input clk,r_l;
output [7:0]a;
reg[7:0]a;
initial a= 8'h01;
endmodule
initial
do=1'b0;
40 | P a g e
jntuworldupdates.org Specworld.in
Smartzworld.com Smartworld.asia
endmodule
Example 4 D Latch
module
dffen(do,di,en);
output do; input di,en;
reg do;
initial
do=1'b0;
always@(di or en)
if(en) do=di;
endmodule
Intra-assignment Delays
In contrast, the ―intra-assignment‖ delay carries out the assignment in two parts
41 | P a g e
jntuworldupdates.org Specworld.in
Smartzworld.com Smartworld.asia
A = # dl expression;
Here the expression is scheduled to be evaluated as soon as it is encountered. However, the result of the
evaluation is assigned to the right-hand side quantity a after a delay specified by dl. dl can be an integer or
a constant expression
Zero Delay
A delay of 0 ns does not really cause any delay. However, it ensures that the assignment following is
executed last in the concerned time slot. Often it is used to avoid indecision in the precedence of
execution of assignments
wait CONSTRUCT
The wait construct makes the simulator wait for the specified expression to be true before proceeding
with the following assignment or group of assignments. Its syntax has the form wait (alpha) assignment1;
alpha can be a variable, the value on a net, or an expression involving them. If alpha is an expression, it is
evaluated; if true, assignment1 is carried out. One can also have a group of assignments within a block in
place of assignment1. The activity is level-sensitive in nature, in contrast to the edge-sensitive nature of
event specified through @.
Specifically the procedural assignment
@clk a = b; assigns the value of b to a when clk changes; if the value of b changes when clk is steady, the
value of a remains unaltered.
wait(clk) #2 a = b; the simulator waits for the clock to be high and then assigns b to a with a delay of 2 ns.
The assignment will be refreshed as long as the clk remains high
jntuworldupdates.org Specworld.in
Smartzworld.com Smartworld.asia
endmodule
43 | P a g e
jntuworldupdates.org Specworld.in
Smartzworld.com Smartworld.asia
For any of the matches, one can have a block of statements defined for execution. The block
should appear within the begin-end construct.
There can be only one default statement or default block. It can appearanywhere in the case
statement.
One can have multiple signal combination values specified for the same statement for execution.
Commas separate all of them.
module dec2_4beh(o,i);
output[3:0]o;
input[1:0]i;
reg[3:0]o;
always@(i) begin
case(i)
2'b00:o=4'h0;
2'b01:o=4'h1;
2'b10:o=4'h2;
2'b11:o=4'h4;
default: begin
$display ("error"); o=4'h0;
end
endcase
end endmodule
module dec2_4beh1(o,i);
output[3:0]o; input[1:0]i;
reg[3:0]o; always@(i)
begin case(i)
2'b00:o[0]=1'b1;
2'b01:o[1]=1'b1;
2'b10:o[2]=1'b1;
2'b11:o[3]=1'b1;
2'b0x,2'b1x,2'bx0,2'bx1:o=4'b0000; default:
begin
$display ("error"); o=4'h0;
end
endcase
44 | P a g e
jntuworldupdates.org Specworld.in
Smartzworld.com Smartworld.asia
end endmodule
module alubeh(c,s,a,b,f);
output[3:0]c; output s;
input [3:0]a,b;
input[1:0]f; reg s;
reg[3:0]c; always@(a
or b or f)
begin case(f)
2'b00: c=a+b;
2'b01: c=a-b;
2'b10: c=a&b;
2'b11: c=a|b;
Endcase end
endmodule
module pri_enc(a,b);
output[1:0]a;
input[3:0]b; reg[1:0]a;
always@(b) casez(b)
4'bzzz1:a=2'b00;
4'bzz10:a=2'b01;
4'bz100:a=2'b10;
4'b1000:a=2'b11; endcase
endmodule
SIMULATION FLOW
Verilog has to be an inherently parallel processing language. The fact that all the elements of a digital
circuit (or any electronic circuit for that matter) function and interact continuously conforming to their
45 | P a g e
jntuworldupdates.org Specworld.in
Smartzworld.com Smartworld.asia
interconnections demands parallel processing. In Verilog theparallel processing is structured through the
following [IEEE]:
Simulation time: Simulation is carried out in simulation time.
The simulator functions with simulation time advancing in (equal) discrete steps.
At every simulation step a number of active events are sequentially carriedout.
The simulator maintains an event queue – called the ―Stratified Event Queue‖ with an active
segment at its top. The top most event in the active segment of the queue is taken up for execution
next.
The active event can be of an update type or evaluation type. The evaluation event can be for
evaluation of variables, values on nets, expressions, etc. Refreshing the queue and rearranging it
constitutes the update event.
Any updating can call for a subsequent evaluation and vice versa.
Only after all the active events in a time step are executed, the simulation advances to the next
time step.
Completion of the sequence of operations above at any time step signifies the parallel nature of the HDL.
A number of active events can be present for execution at any simulation time step; all may vie for
―attention.‖ Amongst these, an event specified at #0 time is scheduled for execution at the end
Stratified Event Queue
The events being carried out at any instant give rise to other events – inherent in the execution process.
All such events can be grouped into the following 5 types:
Active events – explained above.
Inactive events – The inactive events are the events lined up for execution immediately after the
execution of the active events. Events specified with zero delay are all inactive events.
Blocking Assignment Events – Operations and processes carried out at previous time steps with
results to be updated at the current time step are of this category.
Monitor Events – The Monitor events at the current time step – $monitor and $strobe – are to be
processed after the processing of the active events, inactive events, and nonblocking assignment
events.
Future events – Events scheduled to occur at some future simulation time are the future events.
The simulation process conforming to the stratified event queue is shown in flowchart form in Figure
46 | P a g e
jntuworldupdates.org Specworld.in
Smartzworld.com Smartworld.asia
47 | P a g e
jntuworldupdates.org Specworld.in
Smartzworld.com Smartworld.asia
48 | P a g e
jntuworldupdates.org Specworld.in
Smartzworld.com Smartworld.asia
assign–deassign CONSTRUCT
The assign – deassign constructs allow continuous assignments within a behavioral block.
always@(posedge clk) a = b;
By way of execution, at the positive edge of clk the value of b is assigned to variable a, and a remains
frozen at that value until the next positive edge of clk. Changes in b in the interval are ignored.
Consider the block always@(posedge clk)
assign c = d;
Here at the positive edge of clk, c is assigned the value of d in a continuous manner; subsequent changes
in d are directly reflected as changes in variable c: The assignment here is akin to a direct (one way )
electrical connection to c from d established at the positive edge of clk.
jntuworldupdates.org Specworld.in
Smartzworld.com Smartworld.asia
In short, assign allows a variable or a net change in the sensitivity list to mandate a subsequent
continuous assignment within. deassign terminates the assignment done through the assign-based
statement.
repeat CONSTRUCT
The repeat construct is used to repeat a specified block a specified number of times. The quantity
a can be a number or an expression evaluated to a number. As soon as the repeat statement is encountered,
a is evaluated. The following block is executed ―a‖ times. If ―a‖ evaluates to 0 or x or z, the block is not
executed.
Structure of a repeat block.
…
repeat (a) begin
assignment1;
assignment2;
50 | P a g e
jntuworldupdates.org Specworld.in
Smartzworld.com Smartworld.asia
…
end
…
A module to illustrate the use of the repeat construct
module trial_8b; reg[7:0] m[15:0]; integer i; reg
clk; always begin repeat(8) begin
@(negedge clk)
m[i]=i*8; i=i+1;
end repeat(8)
begin
@(negedge clk) i=i-1;
$display("t=%0d, i=%0d, m[i]=%0d", $time,i,m[i]); end
end
initial begin clk =
1'b0; i=0;
#70 $stop; end
always #2 clk=~clk;
endmodule
for LOOP
The for loop in Verilog is quite similar to the for loop in C; the format of the for loop is
....
for(assignment1; expression; assignment 2) statement;
...
It has four parts; the sequence of execution is as follows:
1. Execute assignment1.
2. Evaluate expression.
3. If the expression evaluates to the true state (1), carry out statement. Go to step 5.
4. If expression evaluates to the false state (0), exit the loop.
5. Execute assignment2. Go to step 2.
51 | P a g e
jntuworldupdates.org Specworld.in
Smartzworld.com Smartworld.asia
while LOOP
The format for the while loop is shown is while
(expression) statement ;
The Boolean expression is evaluated. If it is true, the statement (or block of statements) is executed and
expression evaluated and checked. If the expression evaluates to false, the loop is terminated and the
52 | P a g e
jntuworldupdates.org Specworld.in
Smartzworld.com Smartworld.asia
following statement is taken for execution. If the expression evaluates to true, execution of statement
(block of statements) is repeated. Thus the loop is terminated and broken only if the expression evaluates
to false.
To generates a pulse of definite width.
module while2(b,n,en,clk); input[7:0]n; input
clk,en; output b; reg[7:0]a; reg b;
always@(posedge en) begin a=n;
while(|a) begin b=1'b1; @(posedge clk)
a=a-1'b1;
end b=1'b0;
end initial
b=1'b0;
endmodule
forever LOOP
Repeated execution of a block in an endless manner is best done with the forever loop (compare with
repeat where the repetition is for a fixed number of times).
module to generate a clock waveform using the forever construct
module clk; reg clk, en; always @(posedge en) forever#2
clk=~clk;
initial begin clk=1'b0; en=1'b0;#1 clk=1'b1; #4 en=1'b1;#30 $stop;
end initial $monitor("clk=%b, t=%0d, en=%b ", clk,$time,en);
endmodule
PARALLEL BLOCKS
All the procedural assignments within a begin–end block are executed sequentially. The fork–
join block is an alternate one where all the assignments are carried out concurrently. One can use a fork-
join block within a begin–end block or vice versa.
53 | P a g e
jntuworldupdates.org Specworld.in
Smartzworld.com Smartworld.asia
Force–release CONSTRUCT
When debugging a design with a number of instantiations, one may be stuck with an unexpected behavior
in a localized area. Tracing the paths of individual signals and debugging the design may prove to be too
tedious or difficult. In such cases suspect blocks may be isolated, tested, and debugged and status quo
ante established. The force–release construct is for such a localized isolation for a limited period.
force a = 1'b0;
forces the variable a to take the value 0.
force b = c&d;
forces the variable b to the value obtained by evaluating the expression c&d.
The force–release construct is similar to the assign–deassign construct. The latter construct is for
conditional assignment in a design description. The force–release construct is for ―short time‖
assignments in a test-bench. Synthesis tools will not support the force–release constructs.
The force–release construct is equally valid for net-type variables and reg-type variables. The
net–type variables revert to their normal values on release. With reg-type variables the value
forced remains until another assignment to the reg.
The variable, on which the values are forced during testing, must be properly dereferenced.
In the illustration above, each variable was forced one at a time. It was done only to simplify the
illustration sequence and focus attention on the possible use of the construct. In practice, different
variables can be forced together before the special debug sequence. Their release too can be
together.
OR gate module and its test bench to illustrate the use of force–release construct
module or_fr_rl(a,b,c); input b,c; output a; wire a,b,c; assign a= b|c; initial begin
#1 $display("display:time=%0d, b=%b, c=%b, a=%b", $time,b,c,a);
#6 force b=1'b1;
#1 $display("display:time=%0d, b=%b, c=%b, a=%b", $time,b,c,a);
#6 release b;
54 | P a g e
jntuworldupdates.org Specworld.in
Smartzworld.com Smartworld.asia
EVENT
The keyword event allows an abstract event to be declared. The event is not a data type with any
specific values; it is not a variable (reg) or a net. It signifies a change that can be used as a trigger to
communicate between modules or to synchronize events in different modules.
...
event change;
...
always
...
. . . change;
...
.always@change
...
In the course of execution of an always block, the event is triggered. The operator ―‖ signifies
the triggering. Subsequently, another activity can be started in the module by the event change. The
always@(change) block activates this. The event change can be used in other modules also by proper
dereferencing; with such usage an activity in a module can be synchronized to an event in another
module.
The event construct is quite useful, especially in the early stages of a design. It can be used to establish
the functionality of a design at the behavioral level; it allows communication amongst different
instantiated modules without associated inputs or outputs.
55 | P a g e
jntuworldupdates.org Specworld.in