Unit5 dsdv
Unit5 dsdv
UNIT -5
Behavioral Modelling
2
Girija.S Dept. of ECE, Dr.Ambedkar Institute of Technology
Verilog HDL
Behavioral modelling
The behavioral model describes the system by showing how the
output behaves according to the changes in the inputs.
The design is specified at algorithmic level.
Behavioral modeling uses two main blocks:
initial block
always block
Structured Procedures
1. always
2. initial
▪ Basic statements in behavioral modeling
▪ Other statements can appear only inside these
blocks
▪ always and initial represent separate activity
flow
▪ Both blocks begins at 0 simulation time
▪ Initial and always blocks cannot be nested
▪ Each initial and always block must form its own
block
Girija S ECE Dr.AIT
Verilog HDL
initial
► Starts at zero simulation time
► executes only once during a simulation
► All initial blocks executed concurrently at
time 0
► begin-end is used to build initial blocks
(similar to {-} in C)
► Used mainly for initialization, monitoring
of waveforms and other processes that
must be executed only once during the
entire simulation run.
Girija S ECE Dr.AIT
Verilog HDL
initial
The syntax is as follows:
initial [timing_control] procedural_statement
initial
begin
#5 a = 1’b1; // multiple statement need to be grouped
#25 b = 1’b0;
end
initial
begin
#10 x = 1'b0;
#25 y = 1'b1;
end
initial
#50 $finish;
endmodule
initial Results
begin
#10 x = 1'b0; time statement executed
#25 y = 1'b1; 0 m = 1’b0;
end 5 a = 1’b1;
10 x = 1’b0;
initial 30 b =1’b0;
#50 $finish; 35 y = 1’b1;
#50 $finish; 50 $finish;
endmodule
Girija S ECE Dr.AIT
Verilog HDL
always
➢ The always statement starts at simulation
time 0 and repeatedly executes the
statements within it in a loop fashion during
simulation
➢ The syntax is as below:
➢ always[timing_control]procedural_statement
➢ An always block is used to model a block
of activities that are continuously
repeated in a digital circuit
always
reg clock;
initial
#1000 $finish;
endmodule
Procedural Assignment
► Update values of reg, integer, real or time variables
<lvalue> = <expression>
Blocking Assignment
► Executed in the order they are specified in a
sequential block
reg x, y, z;
reg [15:0] reg_a, reg_b;
integer count;
endmodule
Non-blocking Assignment
► Allows scheduling of assignments without
blocking execution of the statements that
follow in a sequential block
► A “ <= “ operator is used to specify non-
blocking assignments
► Executed last in the time step in which they are
scheduled, that is, after all the blocking
assignments in that time step are executed
► Usenon-blocking statements to build
Sequential logic block
module dummy;
reg x, y, z;
reg [15:0] reg_a, reg_b;
integer count;
► At each positive edge of clock following sequence takes place for the
non-blocking assignments.
1. Read operation is performed on each right-hand-side variable (in1, in2,
in3 and reg1), at the positive edge of clock and expressions are
evaluated
2. Write operations to the left-hand-side variables are based on the intra-
assignment delay in each assignment
3. Write operations are executed at the scheduled time steps.
► The order in which the write operations are executed is not known
Girija S ECE Dr.AIT
Verilog HDL
Non-blocking statements to eliminate race conditions
//Write operation
//Assign values of temporary variables to left-hand-side variables
a = temp_b;
b = temp_a;
end
Girija S ECE Dr.AIT
Verilog HDL
Conditional Statements
► if statement
▪ if (<expression>)
true_statement;
▪ if (<expression>)
true_statement;
else
false_statement;
▪ if (<expression>)
true_statement1;
else if (<expression>)
true_statement2;
:
else (<expression>)
default;
//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
$
Case Statements
► case statement
▪ case (<expression>)
alternative1 : statement1; //block or statement
alternative2 : statement2;
:
default : default_statement; //optional
endcase
▪ case statement compares 0, 1, x and z values
in the expression and the alternative bit for bit.
If unequal bit width, they are filled with zero to
match the widest width.
Girija S ECE Dr.AIT
Verilog HDL
endmodule
//Account for unknown signals on select. If any select signal is x then outputs are x. If any select signal is
//z, outputs are z. If one is x and the other is z, x gets higher priority.
module stimulus;
LOOPS
► while(<expression>) //any logical expression
module count_mod;
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
endmodule
LOOPS
► For (initial_condition; condition; change of value)
▪ Example
module counter;
integer count;
initial
for ( count=0; count < 128; count = count + 1)
$display("Count = %d", count);
endmodule
► repeat
▪ Executes the loop a fixed number of times
▪ Must contain a number (constant, variable, signal value)
▪ Variable or signal value evaluated only when the loop starts
▪ Example
module counter;
//Illustration 1 : increment and display count from 0 to 127
integer count;
initial
begin
count = 0;
repeat(128)
begin
$play("Count = %d", count);
count = count + 1;
end
end
endmodule
Girija S ECE Dr.AIT
42
Verilog HDL
► forever
▪ No expression
▪ Executes forever until $finish is encountered
▪ Can also be exited using disable
▪ Without a timing constrain this statement is executed indefinitely
▪ Useful to generate a clock in test bench
▪ Example 7.20
module clock_gen;
//Example 1: Clock generation
//Use forever loop instead of always block
reg clock;
initial
begin
clock = 1'b0;
forever #10 clock = ~clock; //Clock with period of 20 units
initial
#100000 $finish;
endmodule
AND/ OR GATES
Have one scalar output & multiple scalar inputs.
The o/p of a gate is evaluated as soon as one of
the i/p changes
AND/ OR GATES
Wire OUT,IN1,IN2;
and a1(OUT,IN1,IN2);
nand na1(OUT,IN1,IN2);
or or1(OUT,IN1,IN2);
AND/ OR GATES
// instantiation
notif1 n1(out)
notif0 n0(out)
Array Instantiation
For repetitive instances representation, differ from each
other only by index of vector to which they are connected.
wire [7:0] out,in1,in2;
//basic gate instantiations
nand n-gate[7:0] (out,in1,in2);
//equivalent to following
nand n-gate0(out[0], in1[0], in2[0]);
nand n-gate1(out[1], in1[1], in2[1]);
nand n-gate2(out[2], in1[2], in2[2]);
nand n-gate3(out[3], in1[3], in2[3]);
nand n-gate4(out[4], in1[4], in2[4]);
nand n-gate5(out[5], in1[5], in2[5]);
nand n-gate6(out[6], in1[6], in2[6]);
nand n-gate7(out[7], in1[7], in2[7]);
Girija S ECE Dr.AIT
Verilog HDL
0 0 I0
0 1 I1
1 0 I2
1 1 I3
s1=0; s0=0;
#1 $display(“s1=%b, s0=%b, output=%b\n”, s1, s0, output);
//choose in1
s1=0; s0=1;
#1 $display(“s1=%b, s0=%b, output=%b\n”, s1, s0, output);
//choose in2
s1=1; s0=0;
#1 $display(“s1=%b, s0=%b, output=%b\n”, s1, s0, output);
//choose in3
s1=1; s0=1;
#1 $display(“s1=%b, s0=%b, output=%b\n”, s1, s0, output);
end
endmodule
1-bit Adder
module fulladd(sum,cout,a,b,cin);
output sum,cout;
input a,b,cin;
wire s1,c1,c2; //internal nets
//instantiate logic gate primitive
xor (s1,a,b);
and (c1,a,b);
xor (sum,s1,cin);
and (c2,s1,cin);
xor ( cout,c2,c1);
endmodule