Behavioral Modeling
Behavioral Modeling
Structured Procedures
• There are two structured procedure statements in Verilog: always and initial.
• Each always and initial statement represents a separate activity flow in Verilog. Each activity
flow starts at simulation time 0.
Initial Statement
All statements inside an initial statement constitute an initial block.
An initial block starts at time 0, executes exactly once during a simulation, and then does not
execute again
module stimulus;
reg x,y, a,b, m;
initial
m = 1'b0; //single statement; does not need to be grouped
initial
begin
#5 a = 1'b1; //multiple statements; need to be grouped
#25 b = 1'b0;
end
initial
begin
#10 x = 1'b0;
#25 y = 1'b1;
end
initial
#50 $finish;
Endmodule
• The execution sequence of the statements inside the initial blocks will be as follows
endmodule
Procedural Assignments
• Procedural assignments update values of reg, integer, real, or time variables.
• The value placed on a variable will remain unchanged until another procedural
assignment updates the variable with a different value
• Blocking assignment statements are executed in the order they are specified in a
sequential block
• The = operator is used to specify blocking assignments.
• Example -Blocking Statements
reg x, y, z;
reg [15:0] reg_a, reg_b;
integer count;
//All behavioral statements must be inside an initial or always block
initial
begin
x = 0; y = 1; z = 1; //Scalar assignments
count = 0; //Assignment to integer variables
reg_a = 16'b0; reg_b = reg_a; //initialize vectors
#15 reg_a[2] = 1'b1; //Bit select assignment with delay
#10 reg_b[15:13] = {x, y, z} //Assign result of
concatenation to part select of a vecto
count = count + 1; //Assignment to an integer
(increment)
end
Nonblocking Assignments
• Nonblocking assignments allow scheduling of assignments without blocking execution of the statements
that follow in a sequential block.
• A <= operator is used to specify nonblocking assignments
• Example - Nonblocking Assignments
reg x, y, z;
reg [15:0] reg_a, reg_b; integer count;
//All behavioral statements must be inside an initial or always block
initial
begin
x = 0; y = 1; z = 1; //Scalar assignments
count = 0; //Assignment to integer variables
reg_a = 16'b0; reg_b = reg_a; //Initialize vectors
reg_a[2] <= #15 1'b1; //Bit select assignment with delay
reg_b[15:13] <= #10 {x, y, z}; //Assign result of concatenation
//to part select of a vector
a = temp_b;
b = temp_a;
end
Timing Controls
• Timing controls provide a way to specify the simulation time at which procedural
statements will execute.
• There are three methods of timing control:
1.delay-based timing control,
2.event-based timing control,
3.Level-sensitive timing control.
1.Delay-Based Timing Control
• Delay-based timing control in an expression specifies the time duration between
when the statement is encountered and when it is executed
• Delay-based timing control can be specified by a number, identifier, or a
mintypmax_expression.
• There are three types of delay control for procedural assignments:
i)regular delay control,
ii)intra-assignment delay control, and
iii)zero delay control.
Regular delay control
• Regular delay control is used when a non-zero delay is specified to the left of a
procedural assignment
Example:
//define parameters
parameter latency = 20;
parameter delta = 2;
reg x, y, z, p, q;
initial
Begin
x = 0; // no delay control
#10 y = 1; // delay control with a number. Delay execution of
// y = 1 by 10 units
#latency z = 0; // Delay control with identifier. Delay of 20
Units
#(latency + delta) p = 1; // Delay control with expression
#y x = x + 1; // Delay control with identifier. Take value of
y.
#(4:5:6) q = 0; // Minimum, typical and maximum delay values.
//Discussed in gate-level modeling chapter.
end
Intra-assignment delay control
initial
begin
#0 x = 1; //zero delay control
#0 y = 1;
end
2.Event-Based Timing Control
always @(received_data)
data_buf = {data_pkt[0], data_pkt[1], data_pkt[2],data_pkt[3]};
Event OR Control
• Sometimes a transition on any one of multiple signals or events can trigger the
execution of a statement or a block of statements. This is expressed as an OR of
events or signals
Example 7-15 Event OR Control (Sensitivity List)
if (<expression>) true_statement ;
if (<expression>) true_statement ;
else false_statement ;
if (<expression1>) true_statement1 ;
else if (<expression2>) true_statement2 ;
else if (<expression>) true_statement 3;
else default_statement ;
Conditional Statement Examples
//Type 1 statements
if(!lock) buffer = data;
if(enable) out = in;
//Type 2 statements
if (number_queued < MAX_Q_DEPTH)
begin
data_queue = data;
number_queued = number_queued + 1;
end
else
$display("Queue Full. Try again");
//Type 3 statements
//Execute statements based on ALU control
signal.
if (alu_control == 0) y = x + z
else if(alu_control == 1) y = x - z;
else if(alu_control == 2) y = x * z;
else
$display("Invalid ALU control signal");
Multiway Branching
1 case Statement
case (expression)
alternative1: statement1;
alternative2: statement2;
alternative3: statement3;
...
...
default: default_statement;
endcase
endmodule
2.casex, casez Keywords
endcase
Loops
• There are four types of looping statements in Verilog: while, for, repeat, and forever.
• All looping statements can appear only inside an initial or always block.
1.While Loop
Example 1:
integer count;
initial
begin
count = 0;
while (count < 128) //Execute loop till count is 127.
//exit at count 128
begin
$display("Count = %d", count);
count = count + 1;
end
end
2. For Loop
• The keyword for is used to specify this loop. The for loop contains three parts:
• An initial condition
• A check to see if the terminating condition is true
• A procedural assignment to change value of the control variable
Example
integer count;
initial
for ( count=0; count < 128; count = count + 1)
$display("Count = %d", count);
Example:
initial
begin
for(i = 0; i < 32; i = i + 2) //initialize all even
locations with 0
state[i] = 0;
for(i = 1; i < 32; i = i + 2) //initialize all odd
locations with 1
state[i] = 1;
end
3.Repeat Loop
Example :
reg x, y;
reg [1:0] z, w;
initial
begin
x = 1'b0;
y = 1'b1;
z = {x, y};
w = {y, x};
end
2.Parallel blocks
initial
fork
x = 1'b0; //completes at simulation time 0
#5 y = 1'b1; //completes at simulation time 5
#10 z = {x, y}; //completes at simulation time10
#20 w = {y, x}; //completes at simulation time
20
join
Special Features of Blocks
• three special features available with block statements: nested blocks, named blocks, and
disabling of named blocks.
i)Nested blocks
• Blocks can be nested. Sequential and parallel blocks can be mixed
Example :
//Nested blocks
initial
begin
x = 1'b0;
fork
#5 y = 1'b1;
#10 z = {x, y};
join
#20 w = {y, x};
end
ii)Named blocks
• Blocks can be given names.
Example :
//Named blocks
module top;
initial
begin: block1 //sequential block named block1
integer i; //integer i is static and local to block1
// can be accessed by hierarchical name, top.block1.i
...
...
end
initial
fork: block2 //parallel block named block2
reg i; // register i is static and local to block2
// can be accessed by hierarchical name, top.block2.i
...
...
join
iii)Disabling named blocks
The keyword disable provides a way to terminate the execution of a named block
Example :
// Find the first bit with a value 1 in flag (vector variable)
reg [15:0] flag;
integer i; //integer to keep count
initial
begin
flag = 16'b 0010_0000_0000_0000;
i = 0;
begin: block1 //The main block inside while is named block1
end
endmodule
Example: 4 bit magnitude Comparator
module magnitudeComparator(AEQB, AGTB, ALTB, A, B);
output reg AEQB, AGTB, ALTB;
input [3:0] A, B;
always @(A,B)
begin
if( A === B )
begin
AEQB = 1;
AGTB = 0;
ALTB = 0;
end
else if ( A > B )
begin
AEQB = 0;
AGTB = 1;
ALTB = 0;
end
else
begin
AEQB = 0;
AGTB = 0;
ALTB = 1;
end
end
endmodule
Example: Universal Shift Register
module universalShiftRegister(DATAOUT, clock, reset, MODE, DATAIN);
output reg [3:0] DATAOUT;
input clock, reset;
input [1:0] MODE;
input [3:0] DATAIN;
endmodule
Example:up_down_counter
module up_down_counter ( out , up_down , clk , data , reset);
output [7:0] out;
input [7:0] data;
input up_down, clk, reset;
reg [7:0] out;