Name: P. Revathi Department: ECE Designation: Asst. Professor Title: Behavioural Level Modelling
Name: P. Revathi Department: ECE Designation: Asst. Professor Title: Behavioural Level Modelling
Name: P. Revathi
Department: ECE
Designation: Asst. Professor
Title: Behavioural Level Modelling
1
Behavioural Level Modelling
Operations and Assignments, Functional Bifurcation, Procedural constructs: Initial, Always, Assignments with
delays, Wait, Multiple always blocks Designs at Behavioural level, Blocking and Non-blocking assignments, Case
statement, Simulation flow, Conditional statements, and loops- if, if-else, repeat, for, while, forever, parallel
blocks, force-release, Event, System Tasks, and Functions, File based tasks and Functions, Compiler directives,
User-Defined Functions, Tasks and Primitives-Introduction, Function, Tasks, User- Defined Primitives (UDP)
GMR Institute of Technology
Continuous assignment structures, Delays, and Continuous assignments, Assignment to Vectors, Operators,
Practical Components
1. Perform Behavioural model for multiplexer and demultiplexer
2. Perform Behavioural model for 8 to 3 priority encoders
3. Perform Behavioural model for 4-bit counter and shift register
4. Perform the simulation of parity bit generation using functions and tasks
5. Performa two-bit binary addition using functions and tasks
6. Perform the simulation of half adder and display the stimulus, response of system tasks-$monitor,$display,
$monitoron, $monitorof, $stop, $finish
2
BEHAVIORAL MODELING
• Design descriptions at data flow level and gate level are close to the circuit
• An increase in gate count may still be accommodated, if it is due to an increase in vector size one has to work
at a different dimension – the circuit can have a million gates.
• The increase in vector size may still be accommodated at the data flow level (e.g., 32- or 64-bit systems), since
GMR Institute of Technology
3
OPERATIONS AND ASSIGNMENTS
• OPERATIONS AND ASSIGNMENTS:
• The design description at the behavioral level is done through a sequence of assignments.
• These are called ‘procedural assignments’
• The procedure assignment is characterized by the following:
GMR Institute of Technology
1. The assignment is done through the “=” symbol (or the “<=” symbol) as was the case with the continuous
assignment earlier.
2. An operation is carried out and the result assigned through the “=” operator to an operand specified on the left
side of the “=” sign – for example, N = ~N;
• Here the content of reg N is complemented and assigned to the reg N itself.
• The assignment is essentially an updating activity.
3. The operation on the right can involve operands and operators. The operands can be of different types – logical
variables, numbers – real or integer and so on.
4
OPERATIONS AND ASSIGNMENTS
• The operands on the right side can be of the net or variable type. They can be scalars or vectors.
• It is necessary to maintain consistency of the operands in the operation expression – e.g., N = m / l;
• Here m and l have to be same types of quantities – specifically a reg, integer, time, real, real time, or
memory type of data – declared in advance.
GMR Institute of Technology
• The operand to the left of the “=” operator has to be of the variable (e.g., reg) type. It has to be specifically
declared accordingly. It can be a scalar, a vector, a part vector, or a concatenated vector.
• Procedural assignments are very much like sequential statements in C.
• Normally they are carried out one at a time sequentially. As soon as a specified operation on the right is carried
out, the result is assigned to the quantity on the left –
• for example
• N = m + l;
• N1 = N * N;
• The above form a set of two procedures placed within an always block
5
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
• The second concerns simulation – its starting point, steering the simulation flow, observing the 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
6
Structure of a typical procedural block
GMR Institute of Technology
7
begin – end Construct
• If a procedural block has only one assignment to be carried out, it
can be specified as below:
• initial #2 a=0;
• The above statement assigns the value 0 to variable a at the
simulation time of 2 ns.
• It is possibly the simplest initial block. More often more than one
GMR Institute of Technology
8
Name of the Block
• Any block can be assigned a name, but it is not mandatory.
• Only the blocks which are to be identified and referred by the simulator need be named.
• The names assigned to different blocks have to be different.
• Names chosen should conform to the rules for the selection of names to variables
• Assigning names to blocks serves different purposes:
GMR Institute of Technology
• Registers declared within a block are local to it and are not available outside.
• However, during simulation they can be accessed for simulation, etc., by proper dereferencing
• Named blocks can be disabled selectively when 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.
9
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 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
GMR Institute of Technology
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.
• for continuous assignment can be used for procedural assignments as well. The context decides whether the
assignment is of a continuous type or procedural type.
• In the latter case it is present within an always or an initial construct.
• All the procedural assignments appear within a begin–end block explained earlier.
• All the procedural assignments are executed sequentially – in the same order as they appear in the design
description. The waveforms of a and b conforming to the assignments
• Initially (at time t = 0 ns), a and b are set equal to zero.
10
INITIAL CONSTRUCT
module nil;
reg a, b;
initial
begin
a = 1'b0;
b = 1'b0;
GMR Institute of Technology
12
Module with multiple initial blocks
module nil1;
initial
reg a, b;
begin
a = 1'b0;
b = 1'b0; output
GMR Institute of Technology
13
ALWAYS CONSTRUCT
• 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 “@”.
• The event can be a change in the variable specified in either direction or a change in a specified
direction.
For example,
• @(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.
14
Event Control
• @(prt or clr) : the block is executed whenever either of the variables prt or clr undergoes a change.
• @(posedge clk1 or negedge clk2) : With the event, the block is executed in two cases – whenever
the clock clk1 changes from 0 to 1 state or the clock clk2 changes from 1 to 0. One can specify more
elaborate events by OR'ing individual ones.
GMR Institute of Technology
15
Event Control
• The “posedge” transition for a signal on a net can be of three different types:
• 0 to1
• 0 to x or z
GMR Institute of Technology
• x or z to 1
• The “negedge” transition for a signal on a net can be of three different types:-
• 1 to 0
• 1 to x or z
• x or z to 0
16
up counter module
17
BEHAVIORAL MODELING
Clocked Flip-Flop
module tst_dffbeh();//test-bench
module dff(do,di,clk); reg di,clk;
output do; wire do;
input di,clk; dff d1(do,di,clk);
reg do; initial
initial begin
GMR Institute of Technology
do=1'b0; clk=0;
always@(negedge clk) do=di; di=1'b0;
endmodule end
always #3clk=~clk;
always #5 di=~di;
initial
$monitor($time,"clk=%b,di=%b,do=%b",clk,di,do);
initial #35 $stop;
endmodule
18
Output
# 0clk=0,di=0,do=0
# 3clk=1,di=0,do=0
# 5clk=1,di=1,do=0
# 6clk=0,di=1,do=1
# 9clk=1,di=1,do=1
GMR Institute of Technology
# 10clk=1,di=0,do=1
# 12clk=0,di=0,do=0
# 15clk=1,di=1,do=0
# 18clk=0,di=1,do=1
# 20clk=0,di=0,do=1
# 21clk=1,di=0,do=1
# 24clk=0,di=0,do=0
# 25clk=0,di=1,do=0
# 27clk=1,di=1,do=0
# 30clk=0,di=0,do=0
# 33clk=1,di=0,do=0
19
ASSIGNMENTS WITH DELAYS
always #3 b = a;
module del1;
reg a,b;
always #3 b=a;
Initial
begin
GMR Institute of Technology
a = 1’b1;
b = 1’b0;
#1 a = 1’b0;
#3 a = 1’b1;
#1 a = 1’b0;
#2 a = 1’b1;
#3 a = 1’b0;
end
initial $monitor($time, " a = %d, b = %d", a, b);
initial #20 $finish;
endmodule
20
ASSIGNMENTS WITH DELAYS
module del2;
reg a,b;
always @(a) #2 b=a;
Initial
begin
a = 1’b1;
GMR Institute of Technology
b = 1’b0;
#1 a = 1’b0;
#3 a = 1’b1;
#1 a = 1’b0;
#2 a = 1’b1;
#3 a = 1’b0;
end
initial $monitor($time, " a = %d, b = %d", a, b);
initial #20 $finish;
endmodule
21
Intra-assignment Delays
A = # dl expression;
A module to illustrate delayed assignment
module del4;
Integer a, b;
always b = #2 a; Partial output with the simulation of the module
Initial
GMR Institute of Technology
begin
a = 0; b = 0;
#2 a =1;
#2 a =2;
#2 a =3;
#2 a =4;
#2 a = 5;
#2 a =6; #2
a =7;
#2 a =8;
end
initial $monitor($time, " a = %d, b = %d", a, b);
initial #20 $finish;
endmodule
22
ASSIGNMENTS WITH DELAYS
Module del_dem4;
Integer a,b,c,d,n; Output obtained with the simulation of the module
Always
begin
GMR Institute of Technology
#2 b = a;
c = #1 a;
d = a;
end
initial
begin
a = 0; b = 0; c = 0; d = 0;
#1 a = 1; #2 a = 2; #2 a = 3; #2 a = 4;
#2 a = 5; #2 a = 6; #2 a = 7; #2 a = 8; #2 a = 9;
#2 a = 10;
end
initial $monitor ($time, " a = %d, b = %d, c =
%d, d = %d", a, b, c, d);
endmodule
23
Delay Assignments
24
Zero Delay
• wait CONSTRUCT
• The wait construct makes the simulator wait for the specified expression to be true before proceeding with
GMR Institute of Technology
• 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.
25
A counter module to illustrate the use of wait construct.
always initial
begin begin
wait(En) clk=0;N=4'b1111;En=1'b0;#5 En=1'b1;#20 En=1'b0;
@(negedge clk) end
a=(a==N)?4'b0000:a+1'b1; always
end #2 clk=~clk;
endmodule initial #35 $stop;
initial $monitor($time,"clk=%h,En=%b,N=%b,a=
%b",clk,En,N,a,);
endmodule
26
A counter module to illustrate the use of wait construct.
//output
# 0clk=0,En=0,N=1111,a=1111
# 2clk=1,En=0,N=1111,a=1111
# 4clk=0,En=0,N=1111,a=1111
# 5clk=0,En=1,N=1111,a=1111
# 6clk=1,En=1,N=1111,a=1111
GMR Institute of Technology
# 8clk=0,En=1,N=1111,a=0000
# 10clk=1,En=1,N=1111,a=0000
# 12clk=0,En=1,N=1111,a=0001
# 14clk=1,En=1,N=1111,a=0001
# 16clk=0,En=1,N=1111,a=0010
# 18clk=1,En=1,N=1111,a=0010
# 20clk=0,En=1,N=1111,a=0011
# 22clk=1,En=1,N=1111,a=0011
# 24clk=0,En=1,N=1111,a=0100
# 25clk=0,En=0,N=1111,a=0100
# 26clk=1,En=0,N=1111,a=0100
# 28clk=0,En=0,N=1111,a=0101
# 30clk=1,En=0,N=1111,a=0101
# 32clk=0,En=0,N=1111,a=0101
# 34clk=1,En=0,N=1111,a=0101
27
DESIGNS AT BEHAVIORAL LEVEL
A-O-I An A-O-I gate module at the behavioral level and its test bench
module tst_aoibeh;
reg [1:0]a,b; /* specicific values will be assigned to
module aoibeh(o,a,b); a1,a2,b1, and b2 and these connected
to input ports of the gate insatntiations;
output o; hence these variables are declared as reg */
GMR Institute of Technology
input[1:0]a,b; wire o;
reg o,a1,b1,o1; initial
always@(a[1] or a[0]or b[1]or b[0]) begin
a[0]=1'b0;a[1] =1'b0;b[0]=1'b0;b[1] =1'b0;
begin #3 a[0] =1'b1;
a1=&a; #3 a[1] =1'b1;
b1=&b; #3 b[0] =1'b1;
#3 b[1] =1'b0;
o1=a1||b1; #3 a[0] =1'b1;
o=~o1; #3 a[1] =1'b0;
end #3 b[0] =1'b0;
endmodule end
initial #100 $stop;//the simulation ends after running
for 100 tu's.
initial $monitor($time, "o =%b,a[0]=%b,a[1]=%b, b[0] =
%b ,b[1] = %b ",o,a[0],a[1],b[0],b[1]);
aoibeh gg(o,a,b);
endmodule
28
A-O-I
# 0 o = 1,a[0]=0,a[1]=0,b[0]=0,b[1]=0
# 3 o = 1,a[0]=1,a[1]=0,b[0]=0,b[1]=0
# 6 o = 0,a[0]=1,a[1]=1,b[0]=0,b[1]=0
# 9 o = 0,a[0]=1,a[1]=1,b[0]=1,b[1]=0
#18 o = 1,a[0]=1,a[1]=0,b[0]=1,b[1]=0
#21 o = 1,a[0]=1,a[1]=0,b[0]=0,b[1]=0
GMR Institute of Technology
29
BLOCKING AND NONBLOCKING ASSIGNMENTS
• All assignment within an initial or an always block considered so far are done through an equality (“=”)
operator.
• These are executed sequentially – that is, one statement is executed, and only then the following one is
executed.
GMR Institute of Technology
• Such assignments block the execution of the following lot of assignments at any time step.
• Hence they are called “blocking assignments
• where assignments are to be affected concurrently (as with the continuous assignments considered in the
preceding chapter). A facility called the “nonblocking assignment” is available for such situations.
• The symbol “<=” signifies a non-blocking assignment.
• The same symbol signifies the “less than or equal to” operator in the context of an operation
• The context decides the role of the symbol.
• The main characteristic of a nonblocking assignment is that its execution is concurrent with that of the
following assignment or activity
30
BLOCKING AND NONBLOCKING ASSIGNMENTS
assignments. delays
A = 2'b00; A = 2'b00;
B = 2'b01; B = 2'b01;
A <= B; A = B;
B <= A; B = A;
Swapping variable values Another group of blocking
through nonblocking assignments
assignments
31
module involving blocking and nonblocking assignments
initial
begin
A= 1'b0;
B= 1'b1;
C = 1'b0;
end
GMR Institute of Technology
32
A time delay in an evaluation An intra-assignment delay
GMR Institute of Technology
33
THE case STATEMENT
• The case statement is an elegant and simple construct for multiple branching in a module.
• The keywords case, endcase, and default are associated with the case construct
• A statement or a group of statements is executed if and only if there is an exact – bit by bit – match between
the evaluated expression and the specified
ref1, ref2, etc.
• For any of the matches, one can have a block of statements defined for execution. The block should appear
GMR Institute of Technology
Case (expression)
Ref1 : statement1;
Ref2 : statement2;
Ref3 : statement3;
.. .
...
default: statementd;
endcase
Structure of the case statement
34
2-to-4 decoder using the case statement
35
ALU module
case(f) f=2'b00;a=2'b00;b=2'b00;
2'b00: c=a+b; end
2'b01: c=a-b; always
2'b10: c=a&b; begin
2'b11: c=a|b; #2 f=2'b00;a=4'b0011;b=4'b0000;
end case #2 f=2'b01;a=4'b0001;b=4'b0011;
end #2 f=2'b10;a=4'b1100;b=4'b1101;
endmodule #2 f=2'b11;a=4'b1100;b=4'b1101;
end
initial $monitor($time,"f=%b,a=%b,b=%b,c=%b",f,a,b,c);
initial #10 $stop;
endmodule
36
0f=00,a=0000,b=0000,c=0000
#2f=00,a=0011,b=0000,c=0011
#4f=01,a=0001,b=0011,c=1110
#6f=10,a=1100,b=1101,c=1100
#8f=11,a=1100,b=1101,c=1101
#10f=00,a=0011,b=0000,c=0011
GMR Institute of Technology
37
Casex and Casez
• The case statement executes a multiway branching where every bit of the case expression contributes to the
branching decision.
• The statement has two variants
• where some of the bits of the case expression can be selectively treated as don’t cares – that is, ignored.
Casez allows z to be treated as a don’t care. “?” character also can be used in place of z.
GMR Institute of Technology
38
2-bit priority encoder using the casez statement;
39
if AND if-else CONSTRUCTS
• The if construct checks a specific condition and decides execution based on the result. After execution of
assignment1, the condition specified is checked.
• If it is satisfied, assignment2 is executed; if not, it is skipped.
• In either case the execution continues through assignment3, assignment4, etc.
• Execution of assignment2 alone is dependent on the condition number of assignments associated with the
if condition is more than 1, the whole set of them can be grouped within a begin-end block
GMR Institute of Technology
...
assignment1;
if (condition)
assignment2; Flowchart of the if loop
assignment3;
assignment4;
...
Use of if construct
40
if AND if-else CONSTRUCTS
Reg[7:0] a;
Reg c;
always@(posedge clk)
begin
c = a[0];
a = a>>1'b1; // Since the vacated bit of a is filled with a zero, it
GMR Institute of Technology
need be
if( c ) a[7] = c;// set only if a[0] =1
end
41
if AND if-else CONSTRUCTS
..
assignment1;
if(condition)
begin // Alternative 1
assignment2;
assignment3;
GMR Institute of Technology
end
else
begin //alternative 2
assignment4;
assignment5;
end
assignment6;
...
...
Flowchart of execution of the if-else loop
Use of the if–else construct
42
2-to-4 demux module using the if-else-if construct
module demux(a,b,s);
output [3:0]a;
//tst_bench
input b; module tst_demux();
input[1:0]s; reg b;
reg[3:0]a;
always@(b or s)
reg[1:0]s;
begin wire[3:0]a;
if(s==2'b00) demux d1(a,b,s);
begin
initial
GMR Institute of Technology
a[2'b0]=b;
a[3:1]=3'bZZZ; b=1'b0;
end always
else if(s==2'b01)
begin
begin
a[2'd1]=b; #2 s=2'b00;b=1'b1;
{a[3],a[2],a[0]}=3'bZZZ; #2 s=2'b00;b=1'b0;
end
else if(s==2'b10)
#2 s=2'b01;b=1'b0;
begin #2 s=2'b10;b=1'b1;
a[2'd2]=b; #2 s=2'b11;b=1'b0;
{a[3],a[1],a[0]}=3'bZZZ;
end
end
else initial
begin $monitor("t=%0d, s=%b,b=%b,output =%b",
a[2'd3]=b;
a[2:0]=3'bZZZ;
$time,s,b,a);
end initial #30 $stop;
end endmodule
endmodule
43
2-to-4 demux module using the if-else-if construct
//output
# t=0, s=xx,b=0,output a=0zzz
# t=2, s=00,b=1,output a=zzz1
# t=4, s=00,b=0,output a=zzz0
# t=6, s=01,b=0,output a=zz0z
# t=8, s=10,b=1,output a=z1zz
# t=10, s=11,b=0,output a=0zzz
GMR Institute of Technology
44
assign– deassign CONSTRUCT
45
2 to 4 Demux through Procedural Continuous Assignment
46
2 to 4 Demux through Procedural Continuous Assignment
//output
# t=1, s=01, b=0, {a0,a1,a2,a3} =0zzz
# t=2, s=01, b=1, {a0,a1,a2,a3} =z0zz
# t=3, s=10, b=1, {a0,a1,a2,a3} =z1zz
# t=4, s=10, b=0, {a0,a1,a2,a3} =zz1z
# t=5, s=11, b=0, {a0,a1,a2,a3} =zz0z
GMR Institute of Technology
47
D Flip-Flop through assign – deassign Constructs
module dffassign(q,qb,di,clk,clr,pr);
output q,qb;
input di,clk,clr,pr;
reg q;
assign qb=~q;
always@(clr or pr)
GMR Institute of Technology
begin
if(clr)assign q = 1'b0;//asynchronous clear and
if(pr) assign q = 1'b1;// preset of FF overrides
else deassign q;// the synchronous behaviour
end
always@(posedge clk)
q = di;//synchronous (clocked)value assigned to q
endmodule
48
D Flip-Flop through assign – deassign Constructs
//test-bench //output
module dffassign_tst(); # t=0, clk=0, clr=1, pr=0, di=0, q=0
reg di,clk,clr,pr; # t=2, clk=1, clr=0, pr=0, di=0, q=0
wire q,qb; # t=4, clk=0, clr=0, pr=0, di=1, q=0
dffassign dd(q,qb,di,clk,clr,pr); # t=6, clk=1, clr=0, pr=0, di=1, q=1
initial # t=8, clk=0, clr=0, pr=0, di=0, q=1
begin # t=10, clk=1, clr=0, pr=0, di=0, q=0
clr=1'b1;pr=1'b0;clk=1'b0;di=1'b0; # t=12, clk=0, clr=0, pr=0, di=1, q=0
GMR Institute of Technology
49
repeat CONSTRUCT
The repeat construct is used to repeat a specified block a specified number of times.
...
…
always
repeat (a)
begin
begin
repeat(n-1'b1)
assignment1;
GMR Institute of Technology
begin
assignment2;
@(posedge clk)
… begin
end out = m(mar);
… mar = mar + 1'b1;
Structure of a repeat block end
end
end
50
repeat construct
module trial_8b;
reg[7:0] m[15:0];
integer i; # t=32, i=7, m[i]=56
reg clk; # t=36, i=6, m[i]=48
always
begin # t=40, i=5, m[i]=40
repeat(8) # t=44, i=4, m[i]=32
begin
# t=48, i=3, m[i]=24
GMR Institute of Technology
@(negedge clk)
m[i]=i*8; # t=52, i=2, m[i]=16
i=i+1;
End # t=56, i=1, m[i]=8
repeat(8) # t=60, i=0, m[i]=0
begin
@(negedge clk)
i=i-1;
$display("t=%0d, i=%0d, m[i]=%0d", $time,i,m[i]); Results of simulating the test bench
end
end
initial
begin
clk = 1'b0;
i=0;
#70 $stop;
end
always #2 clk=~clk;
endmodule
51
for LOOP
....
for(assignment1; expression; assignment 2)
GMR Institute of Technology
statement;
...
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
52
for construct to load a memory block
module trial_8a;
reg[7:0] m[15:0];
integer i;
reg clk; //Simulation results
always # t=32, i=0, m[i]=0
begin # t=36, i=1, m[i]=8
GMR Institute of Technology
53
An adder module using the for loop
initial
integer i; begin
always@( posedge en ) #0 en=1'b0;
#1 cin=1'b0;a=8'h01;b=8'h00;
begin #2 cin=1'b0;a=8'h01;b=8'h00;
c[0] =cin; #2 cin=1'b0;a=8'h01;b=8'h01;
for(i=0;i<=7;i=i+1) #2 cin=1'b0;a=8'h01;b=8'h01;
begin #2 cin=1'b1;a=8'h01;b=8'h02;
{c[i+1],s[i]}=(a[i]+b[i]+c[i]); #2 en=1'b1;cin=1'b1;a=8'h01;b=8'h03;
end #2 cin=1'b0;a=8'h01;b=8'h09;
#2 cin=1'b1;a=8'h01;b=8'h09;
co=c[8]; #2 cin=1'b0;a=8'hff;b=8'hff;
end #2 cin=1'b1;a=8'hff;b=8'hff;
endmodule #2 cin=1'b1;a=8'hff;b=8'hff;
end
initial $monitor( "t=%0d, en = %b, cin = %b, a = %0h, b
= %0h, s = %0h, co = %b ",$time,en,cin,a,b,s,co);
initial #30 $stop;
endmodule
54
An adder module using the for loop
# t=0, en = 0, cin = x, a = x, b = x, s = x, co = x
# t=1, en = 0, cin = 0, a = 1, b = 0, s = x, co = x
# t=2, en = 1, cin = 0, a = 1, b = 0, s = 1, co = 0
# t=4, en = 0, cin = 0, a = 1, b = 0, s = 1, co = 0
# t=5, en = 0, cin = 0, a = 1, b = 1, s = 1, co = 0
GMR Institute of Technology
# t=6, en = 1, cin = 0, a = 1, b = 1, s = 2, co = 0
# t=8, en = 0, cin = 0, a = 1, b = 1, s = 2, co = 0
# t=9, en = 0, cin = 1, a = 1, b = 2, s = 2, co = 0
# t=10, en = 1, cin = 1, a = 1, b = 2, s = 4, co = 0
# t=11, en = 1, cin = 1, a = 1, b = 3, s = 4, co = 0
# t=12, en = 0, cin = 1, a = 1, b = 3, s = 4, co = 0
# t=13, en = 0, cin = 0, a = 1, b = 9, s = 4, co = 0
# t=14, en = 1, cin = 0, a = 1, b = 9, s = a, co = 0
# t=15, en = 1, cin = 1, a = 1, b = 9, s = a, co = 0
# t=16, en = 0, cin = 1, a = 1, b = 9, s = a, co = 0
# t=17, en = 0, cin = 0, a = ff, b = ff, s = a, co = 0
# t=18, en = 1, cin = 0, a = ff, b = ff, s = fe, co = 1
# t=19, en = 1, cin = 1, a = ff, b = ff, s = fe, co = 1
# t=20, en = 0, cin = 1, a = ff, b = ff, s = fe, co = 1
# t=22, en = 1, cin = 1, a = ff, b = ff, s = ff, co = 1
# t=24, en = 0, cin = 1, a = ff, b = ff, s = ff, co = 1
# t=26, en = 1, cin = 1, a = ff, b = ff, s = ff, co = 1
# t=28, en = 0, cin = 1, a = ff, b = ff, s = ff, co = 1
55
THE disable CONSTRUCT
There can be situations where one has to break out of a block or loop.
The disable statement terminates a named block or task.
Control is transferred to the statement immediately following the block.
Conditional termination of a loop, interrupt servicing, etc., are typical contexts for its use.
GMR Institute of Technology
Often the disabling is carried out from within the block itself.
The disable construct is functionally similar to the break in C
56
An OR gate module to demonstrate the use of the disable construct
begin
always@(posedge en) a = 4'h0;
begin:OR_gate en = 1'b0;
end
b=1'b0; initial begin
for(i=0;i<=3;i=i+1) #2 en=1'b1; #2 a =4'h1; #2 en=1'b0;
if(a[i]==1'b1) #2 en=1'b1; #2 a =4'h2; #2 en=1'b0;
begin #2 en=1'b1; #2 a =4'h0; #2 en=1'b0;
#2 en=1'b1; #2 a =4'h3; #2 en=1'b0;
b=1'b1; #2 en=1'b1; #2 a= 4'h4; #2 en=1'b0;
disable OR_gate; #2 en=1'b1; #2 a=4'hf;
end end
initial $monitor("t=%0d, en = %b, a = %b, b =
end %b",$time,en,a,b);
endmodule initial #60 $stop;
endmodule
57
An OR gate module to demonstrate the use of the disable construct
//OUTPUT
# t=0, en = 0, a = 0000, b = x
# t=2, en = 1, a = 0000, b = 0
# t=4, en = 1, a = 0001, b = 0
# t=6, en = 0, a = 0001, b = 0
# t=8, en = 1, a = 0001, b = 1
GMR Institute of Technology
# t=10, en = 1, a = 0010, b = 1
# t=12, en = 0, a = 0010, b = 1
# t=14, en = 1, a = 0010, b = 1
# t=16, en = 1, a = 0000, b = 1
# t=18, en = 0, a = 0000, b = 1
# t=20, en = 1, a = 0000, b = 0
# t=22, en = 1, a = 0011, b = 0
# t=24, en = 0, a = 0011, b = 0 Synthesized circuit of the OR gate module
# t=26, en = 1, a = 0011, b = 1
# t=28, en = 1, a = 0100, b = 1
# t=30, en = 0, a = 0100, b = 1
# t=32, en = 1, a = 0100, b = 1
# t=34, en = 1, a = 1111, b = 1
58
while LOOP
59
A module to illustrate the use of while construct
60
A module to illustrate the use of while construct
t= 10, output b = 1 ,ww.a = 14 ,en = 1 ,clk = 0 t= 52, output b = 1 ,ww.a = 3 ,en = 1 ,clk = 1
t= 12, output b = 1 ,ww.a = 13 ,en = 1 ,clk = 1 t= 54, output b = 1 ,ww.a = 3 ,en = 1 ,clk = 0
t= 14, output b = 1 ,ww.a = 13 ,en = 1 ,clk = 0 t= 56, output b = 1 ,ww.a = 2 ,en = 1 ,clk = 1
t= 16, output b = 1 ,ww.a = 12 ,en = 1 ,clk = 1 t= 58, output b = 1 ,ww.a = 2 ,en = 1 ,clk = 0
t= 18, output b = 1 ,ww.a = 12 ,en = 1 ,clk = 0 t= 60, output b = 1 ,ww.a = 1 ,en = 1 ,clk = 1
t= 20, output b = 1 ,ww.a = 11 ,en = 1 ,clk = 1 t= 62, output b = 1 ,ww.a = 1 ,en = 1 ,clk = 0
t= 22, output b = 1 ,ww.a = 11 ,en = 1 ,clk = 0 t= 63, output b = 1 ,ww.a = 1 ,en = 0 ,clk = 0
t= 24, output b = 1 ,ww.a = 10 ,en = 1 ,clk = 1 t= 64, output b = 0 ,ww.a = 0 ,en = 0 ,clk = 1
t= 26, output b = 1 ,ww.a = 10 ,en = 1 ,clk = 0 t= 66, output b = 0 ,ww.a = 0 ,en = 0 ,clk = 0
t= 28, output b = 1 ,ww.a = 9 ,en = 1 ,clk = 1 t= 68, output b = 0 ,ww.a = 0 ,en = 0 ,clk = 1
t= 30, output b = 1 ,ww.a = 9 ,en = 1 ,clk = 0 t= 70, output b = 0 ,ww.a = 0 ,en = 0 ,clk = 0
t= 32, output b = 1 ,ww.a = 8 ,en = 1 ,clk = 1 t= 72, output b = 0 ,ww.a = 0 ,en = 0 ,clk = 1
t= 34, output b = 1 ,ww.a = 8 ,en = 1 ,clk = 0 t= 74, output b = 0 ,ww.a = 0 ,en = 0 ,clk = 0
t= 36, output b = 1 ,ww.a = 7 ,en = 1 ,clk = 1 t= 76, output b = 0 ,ww.a = 0 ,en = 0 ,clk = 1
t= 78, output b = 0 ,ww.a = 0 ,en = 0 ,clk = 0
61
forever LOOP
Repeated execution of a block in an endless manner is best done with the forever loop
module clk;
reg clk, en;
always @(posedge en)
forever#2 clk=~clk;
GMR Institute of Technology
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
62
PARALLEL BLOCKS
• The fork–join block is an alternate one where all the assignments are carried out concurrently
GMR Institute of Technology
(a) A module with a begin–end block and the simulation results (b) A module with a fork–join block and the simulation results.
63
An example of a fork–join block within a begin–end block
64
Force–release CONSTRUCT
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.
GMR Institute of Technology
...
force a = 1'b0;
force b = c&d;
assignment1;
assignment2;
...
release a;
release b;
...
65
An OR gate module and its test bench to illustrate the use of force– release construct
module or_fr_rl(a,b,c);
module orfr_tst;
input b,c;
reg b,c;wire a;
output a;
initial
wire a,b,c;
begin b=1'b0;
assign a= b|c;
c=1’b0;
initial begin
GMR Institute of Technology
#20 $stop;
#1 $display("display:time=%0d, b=%b, c=%b, a=%b", $time,b,c,a);
end
#6 force b=1'b1;
always #3 c = ~c;
#1 $display("display:time=%0d, b=%b, c=%b, a=%b", $time,b,c,a);
or_fr_rl dd(a,b,c);
#6 release b;
endmodule
#1 $display("display:time=%0d, b=%b, c=%b, a=%b", $time,b,c,a);
end
endmodule
66
Use of the event construct in a module A module to illustrate the event construct:
.. module rec(a,ddi,clk);
event change; output[8:1]a;
... input ddi,clk;
always reg[8:1] a;
GMR Institute of Technology
67
A module to illustrate the event construct
module rec_tst;
reg clk,di; # t=32, aa=01, a=01
integer n,i; # t=64, aa=72, a=72
reg[8:1] aa; # t=96, aa=e3, a=e3
wire [8:1] a;
always #2 clk = ~clk; # t=128, aa=54, a=54
rec rrcc(a,di,clk); # t=160, aa=c5, a=c5
always @(rrcc.buf_ful) # t=192, aa=36, a=36
GMR Institute of Technology
68
GMR Institute of Technology
Thank You
69