Behavioural
Behavioural
Samir Palnitkar
Structural Procedures
1. always
2. initial
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;
Dr. Behnam Arad 4
endmodule EEE/CSC-273
Example-7.1 : initial block
module clock_gen;
reg clock;
initial
#1000 $finish;
endmodule
module dummy;
reg x, y, z;
reg [15:0] reg_a, reg_b;
integer count;
initial
$monitor($time, " x = %b, y = %b, z = %b, count = %0d, reg_a = %x, reg_b = %x",
x, y, z, count, reg_a, reg_b);
endmodule
module dummy;
reg x, y, z;
reg [15:0] reg_a, reg_b;
integer count;
initial
$monitor($time, " x = %b, y = %b, z = %b, count = %0d, reg_a = %x, reg_b = %x",
x, y, z, count, reg_a, reg_b);
► 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.
//Write operation
//Assign values of temporary variables to left-hand-side variables
a = temp_b;
b = temp_a;
end
Dr. Behnam Arad 16
EEE/CSC-273
Timing Control for Simulation
► If no timing control statement, then the
simulation time does not advance
module regular_delay;
Regular delay control
► Delay is always relative to
//define parameters
parameter latency = 20; when the statement is
parameter delta = 2; Encountered
//define register variables ► Specified to the left of a
reg x, y, z, p, q;
procedural assignment
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 earlier in gate level modeling chapter.
end
endmodule
initial
begin
x = 0;
y = 0;
end
initial
begin
#0 x = 1; //zero delay control
#0 y = 1;
end
Dr. Behnam Arad 22
EEE/CSC-273
Event based timing control
► What is an event…..?
The change in the value of a register or a net
//This is an example of a data buffer storing data after the last packet of data has arrived.
always
wait (count_enable) #20 count = count + 1;
//monitors continuously
//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
$display (“Invalid ALU control signal”);
Dr. Behnam Arad 29
EEE/CSC-273
► case statement
case (<expression>)
alternative1 : statement1; //block or statement
alternative2 : statement2;
:
default : default_statement; //optional
endcase
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;
Example 7.17
module count_mod;
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
endmodule Dr. Behnam Arad 35
EEE/CSC-273
► For (initial_condition; condition; change of value)
Example 7.18
module counter;
integer count;
initial
for ( count=0; count < 128; count = count + 1)
$display("Count = %d", count);
endmodule
Example 7.19
module counter;
//Illustration 1 : increment and display count from 0 to 127
integer count;
initial
begin
count = 0;
repeat(128)
begin
$display("Count = %d", count);
count = count + 1;
end
end
endmodule
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
end
initial
#100000 $finish;
endmodule
Dr. Behnam Arad 38
EEE/CSC-273
Sequential blocks
► begin........end
Statements are processed in the order they are specified
Delay/event control relative to the simulation time when the
previous statement in the block completed execution
Example 7.21
initial
begin
x = 1’b0;
y = 1’b1;
z = {x, y};
w = {y, x};
end Dr. Behnam Arad 39
EEE/CSC-273
► Example 7.21 : sequential block continue…
module sequential;
//Illustration 2: Sequential blocks with delay.
reg x, y;
reg [1:0] z, w;
initial
$monitor($time, " x = %b, y = %b, z = %b, w = %b\n", x, y, z, w);
initial
begin
x = 1'b0; //completes at simulation time 0
#5 y = 1'b1; //completes at simulation time 5
#10 z = {x, y}; //completes at simulation time 15
#20 w = {y, x}; //completes at simulation time 35
end
endmodule
Dr. Behnam Arad 40
EEE/CSC-273
Parallel blocks
► Example 7.22
module parallel;
//Illustration 1:Parallel Blocks with delay
reg x, y;
reg [1:0] z, w;
initial
$monitor ($time, " x = %b, y = %b, z = %b, w = %b\n", x, y, z, w);
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 time 15
#20 w = {y, x}; //completes at simulation time 35
join
endmodule
//Nested blocks
initial
begin
x = 1'b0;
fork
#5 y = 1'b1;
#10 z = {x, y};
join
#20 w = {y, x};
end
endmodule
Dr. Behnam Arad 42
EEE/CSC-273
Named Block
► Named blocks can be disabled, i.e. their execution can be stopped.
module find_true_bit;
//Illustration: 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
while(i < 16)
begin
if (flag[i])
begin
$display("Encountered a TRUE bit at element number %d", i);
disable block1; //disable block1 because you found true bit.
end
i = i + 1;
end
end
end
endmodule Dr. Behnam Arad 43
EEE/CSC-273
Example (traffic signal controller)
road
Main highway
Country
Specifications:
• The traffic signal for the main highway gets highest priority because cars are continuously
present on the main highway. Thus, the main highway signal remains green by default.
• occasionally, cars from the country road arrive at the traffic signal. The traffic signal for the
country road must turn green only long enough to let the cars on the country road go.
• As soon as there are no cars on the country road, the country road traffic signal turns yellow
and then red and the traffic signal on the main highway turns green again.
• There is a sensor to detect cars waiting on the country road. The sensor sends a signal X as
input to the controller. X=1 if there are cars on the country road. Otherwise X=0.
• there are delays on transitions from S1 to S2, from S2 to S3, and from S4 to S0. The delays
must be controllable. Dr. Behnam Arad 44
EEE/CSC-273
State Machine
X=0
S0 X=1
State Signals
S0 Hwy=G, Cntry=R
S4 S1 S1 Hwy=Y, Cntry=R
S2 Hwy=R, Cntry=R
S3 Hwy=R, Cntry=G
X=0 S4 Hwy=R, Cntry=Y
S3 S2
X=1
//I/O ports
output [1:0] hwy, cntry;
cntry; //2 bit output for 3 states of signal GREEN, YELLOW, RED;
reg [1:0] hwy, cntry;
cntry; //declare output signals are registers
input X; //if TRUE, indicates that there is car on the country road, otherwise
otherwise FALSE
//Status of lights
parameter RED = 2'd0, YELLOW = 2'd1, GREEN = 2'd2;
//Setup monitor
initial
$monitor($time, " Main Sig = %b Country Sig = %b Car_on_cntry = %b",
MAIN_SIG, CNTRY_SIG, CAR_ON_CNTRY_RD);
//setup clock
initial begin
CLOCK = `FALSE;
forever #5 CLOCK = ~CLOCK;
end