Verilog Behavioral Modeling
Verilog Behavioral Modeling
Concepts
Constructs
Structured Procedures
Two basic structured procedure statements
always
initial
All behavioral statements can appear only inside these
blocks
Each always or initial block has a separate activity flow
(concurrency)
Start from simulation time 0
Cannot be nested
Structured Procedures:
initial statement
Starts at time 0
Executes only once during a simulation
Multiple initial blocks, execute in parallel
All start at time 0
Each finishes independently
Syntax:
initial
begin
// behavioral statements
end
Structured Procedures:
initial statement (cont’d)
Example:
module stimulus;
reg x, y, a, b, m; initial
#50 $finish;
initial endmodule
m= 1’b0;
initial
begin
#5 a=1’b1;
#25 b=1’b0;
end
initial
begin
#10 x=1’b0;
#25 y=1’b1;
end
Structured Procedures:
always statement
Start at time 0
Execute the statements in a looping fashion
Example
module clock_gen;
reg clock;
<lvalue> can be
reg, integer, real, time
A bit-select of the above (e.g., addr[0])
A part-select of the above (e.g., addr[31:16])
A concatenation of any of the above
<expression> is the same as introduced in dataflow modeling
What happens if the widths do not match?
LHS wider than RHS => RHS is zero-extended
RHS wider than LHS => RHS is truncated (Least significant part is kept)
Types of Procedural Assignments
Blocking Assignments
module evaluates2(out,a,b,c);
output out;
reg a, b, c;
initial
begin
a = 0; b = 1; c = 0;
end
always c = #5 ~c;
always @(posedge c)
begin
a = b;
b = a;
end
endmodule
A sequence of nonblocking assignments
don’t communicate
a = 1; a <= 1;
b = a; b <= a;
c = b; c <= b;
if (<expression>) true_statement;
else false_statement;
if (<expression>) true_statement1;
else if (<expression>) true_statement2;
else if (<expression>) true_statement3;
else default_statement;
True is 1 or non-zero
False is 0 or ambiguous (x or z)
More than one statement: begin end
Conditional Statements (cont’d)
Examples:
if (!lock) buffer = data;
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.”);
Behavioral Modeling Statements:
Multiway Branching
Similar to switch-case statement in C
Syntax:
case (<expression>)
alternative1: statement1;
alternative2: statement2;
...
default: default_statement; // optional
endcase
Notes:
<expression> is compared to the alternatives in the
order specified.
Default statement is optional
Multiway Branching (cont’d)
Examples:
reg [1:0] alu_control;
...
case (alu_control)
2’d0: y = x + z;
2’d1: y = x – z;
2’d2: y = x * z;
default: $display(“Invalid ALU control signal.”);
Multiway Branching (cont’d)
Example 2:
module mux4_to_1(out, i0, i1, i2, i3, s1, s0);
output out;
input i0, i1, i2, i3, s1, s0;
reg out;
Recommendation
Concurrent data transfers => race condition
Use non-blocking assignments wherever
concurrent data transfers
Example: pipeline modeling
Disadvantage:
Lower simulation performance
Higher memory usage in the simulator
TIMING CONTROLS
3 – TYPES
DELAY BASED TIMING CONTROL
1.REGULAR DELAY
…
parameter latency = 20;
parameter delta = 10;
reg x, y, z, p, q;
initial
begin
x = 0;
#10 y = 1;
#latency z = 0;
#(latency + delta) p = 1;
#y x = x + 1;
end
2. Intra Assignment Delay
OR
initial
begin
x = 0; z = 0;
temp_a = x + z;
#5 y = temp_a;
end
3. Zero Delay
initial
begin
x = 0;
y = 0;
end
Executed last
initial
begin
#0 x = 1;
#0 y = 1;
end
Event Based Control Timings
1. Regular event delay
@ - symbol
Changes when ever clock changes
@ (clock) q = d;
1. event packets;
always @ (posedge)
beign
if (last_data_recevied)
-> packets; // this will trigger the event
2. event count;
always @ (count)
3. Event OR control
always
wait (count_enable)
#20 count = count + 1;
module JK_flip_flop ( j ,k ,clk ,reset ,q ,qb );
output q ; reg q ;
output qb ; reg qb ;
input j ; wire j ;
input k ; wire k ;
input clk ; wire clk ;
input reset ; wire reset ;
always @ (posedge (clk)) begin
if (reset) begin
q <= 0; qb <= 1;
end
else begin
if (j!=k) begin q <= j;
qb <= k; end
else if (j==1 && k==1) begin
q <= ~q; qb <= ~qb;
endend
end
endmodule