Verilog 4 Beh - Modeling
Verilog 4 Beh - Modeling
Anand S Moghe
* ANAND S MOGHE 1
Behavioral Modeling
• Behavioral modeling enables you to describe a system at a
high level of abstraction. (At this level of abstraction, implementation is
not as important as the high-level description of a functional block or of the
system.)
* ANAND S MOGHE 2
Continuous assignments
• Continuous assignments drive values on to a net (wire).
• Any changes in the RHS of the continuous assignment are evaluated and
the LHS is updated.
* ANAND S MOGHE 3
Conditional operator
• Conditional operator is similar to a case statement (seen later). The value
assigned to the LHS is the one that results true from the expression
evaluation.
in out
enable
* ANAND S MOGHE 4
Conditional operator .. cont
module mux_1 (out, in1, in2, in3, in4, sel);
parameter WIDTH = 8
output [WIDTH-1:0] out;
input [WIDTH-1:0] in1, in2, in3, in4;
input [1:0] sel;
• The default value is specified to ensure proper behavior for any case that
is not encountered. In the above example, it would be when one of the
bits of sel[1:0] is unknown.
* ANAND S MOGHE 5
Concatenation and replication operators
• For swapping the bytes in a word, the following statement can be used:
wire [15:0] word, swapped_word;
swapped_word = {word[7:0], word[15:8]};
• For usage of concatenation operator on the LHS, the following can be an example:
module add_32 (c0, sum, a, b, ci);
output c0;
output [31:0] sum;
input [31:0] a, b;
input ci;
assign #10 {c0, sum} = a + b + ci;
endmodule
• Sign extension:
word = {{8{byte[7]}},byte};
* ANAND S MOGHE 6
Timing control in procedural blocks
• Procedural blocks are the basis for behavioral modeling.
• Procedural blocks are of two types:
– initial procedural block that executes only once
– always procedural blocks that execute in a continuous loop which has timing control
• Procedural blocks have the following components:
– Procedural assignment statements to describe the data flow within the block
– Timing controls to control the triggering of the block and the execution of the
statements in the block.
– High-level programming language constructs that describe the functional operation of
the block.
initial always
c c
c c
c c
c c
c c
c c
c c
c c
* ANAND S MOGHE 7
Timing control in procedural blocks
• Procedural blocks’ timing is specified using 3 types of timing controls:
– Simple delay.
#10 regA = regB;
#(cycle/2) clk = ~clk; // cycle is declared as a parameter
This delays the execution for a specific number of simulation time steps.
* ANAND S MOGHE 8
Timing control in procedural blocks
10 30 50 70 90 110 130 150
clk
set
15 48 71
q
33 43 93 103
always wait(set)
begin
@(posedge clk) always #5
#3 q = 1; clk = ~clk;
#10 q = 0;
wait(!set);
end
• Posedge at 10 is ignored since it is waiting for set = 1; Posedge at 30 is caught and
q = 1 at 33 followed by q=0 at 43.
• Set goes low at 48; caught by wait(!set)
• Now waiting for set = 1 which happens at 71; posedge at 90 is caught. q=1 at 93
*
and q=0 at 103. Loop repeats. ANAND S MOGHE 9
Timing control in procedural blocks
• Assignments made in procedural blocks are known as procedural assignments.
module dff (q, qb, d, clk);
output q, qb;
input d, clk;
reg q, qb;
always @(posedge clk) begin
#5 q <= d;
#1 qb <= ~d;
end
endmodule
* ANAND S MOGHE 10
Block statements
• Block statements are used to group two or more statements together, so they act
as one statement syntactically.
• Sequential block statements are enclosed between the keywords begin and end.
The statements in this block are executed in sequential manner.
• Parallel block statements are grouped together between the keywords fork and
join. The statements in this block are executed concurrently (concurrent with
respect to simulation time).
* ANAND S MOGHE 11
Nonblocking procedural statements
• Non-blocking assignments in Verilog are meant for modeling sequential elements
like flip flops.
module swap_values;
reg a, b, c;
initial begin // executes in simulation time step 0
a = 0;
b = 1;
c = 0;
end
b tn e
always @(posedge c)
begin
b <= a; // read a, write into a_temp=0;
a <= b; // read b, write into b_temp=1; nonblocking assignment swaps the values
of a and b; a=1; b=0
end
endmodule
• A nonblocking statement allows the assignments without blocking the procedural
flow
• The assignment happens in two steps:
– The simulator evaluates all the RHS expressions and schedules the assignments to take
place at the time specified by the timing control.
– At the end of the time step in which the delay has expired, the simulator executes the
assignment by assigning the value to the LHS expression
* ANAND S MOGHE 12
Stratified Event Queue
ANAND S MOGHE
Nonblocking procedural assignments
// non-block1.v
module non_block1;
reg a, b, c, d, e, f;
// blocking assignments
initial begin
The simulator assigns 1 to register a
a = #10 1;
b = #2 0;
at simulation time 10, assigns 0 to register
c = #4 1; b at simultation time 12, and assigns 1
end to register c at simulation time 16
// nonblocking assignments
initial begin
d <= #10 1;
The simulator assigns 1 to register d
e <= #2 0;
at simulation time 10, assigns 0 to register
f <= #4 1;
end e at simulation time 2, and assigns 1
initial begin to register f at simulation time 4
$monitor($time, “a=%3d b=%3d c=%3d
d=%3d e=%3d f=%3d”, a, b, c, d, e,
f);
end
endmodule
* ANAND S MOGHE 14
Conditional statements – if / if-else
• if and if-else statements
• In nested if sequences, the else is associated with the closest previous if.
• To ensure proper readability and proper association, use begin-end block
statements.
* ANAND S MOGHE 15
Conditional statements – if / if-else
if (sel == 0)
out = a;
a
else out
out = b;
b
sel
* ANAND S MOGHE 16
Conditional statements.. cont
• case statement: example case expression
case (opcode)
3’b000: result = regA + regB;
3’b001: result = regA - regB;
3’b010, controlling expression
3’b100: result = ‘bx;
default: begin
result = ‘bx;
$display(“no match”);
end
endcase
• The case statement is a multiway conditional statement that tests whether the
expression matches one of a number of other expressions, and branches
accordingly.
• The case statement does a bit-by-bit comparison for an exact match; the default
statement is optional; It is executed when none of the previous statements match
the case expression.
• Use of multiple default statements is illegal.
• It is always good practice to use the default statement especially to check for x
(unknown) values.
* ANAND S MOGHE 17
Conditional statements.. cont
• case / casex / casez matching
case casex casez
0 = 0 0 = 0 0 = 0
1 = 1 1 = 1 1 = 1
x = x x = 0,1,x,z x = x
Z = z z = 0,1,x,z z = 0,1,x,z
* ANAND S MOGHE 18
Case and if-else statements.. comparison
• Comparison of case and if-else statements
– case is more compact than if-else statement
– Any set of general expression can be used with if-else statement
– With the case statement, the case expression are all evaluated against a common
expression
– case comparison is done using a 4-value logic – 0, 1, x, z
– case expression and controlling expression width should match. In contrast, the if-else
expression involving x or z bits may result in an x or z value which might be interpreted
as FALSE.
casez and casex :
– casez and casex are two variants of the basic case statement
– casez allows for ‘z’ to be treated as don’t-care.
– casex allows for both ‘z’ and ‘x’ to be treated as don’t care
• casez and casex are similar in syntax to case , except that the casez or casex
keyword is substituted for case .
* ANAND S MOGHE 19
Case and if-else statements.. comparison
• Example
module decode
sensitivity list
reg [7:0] r;
reg [7:0] mask;
* ANAND S MOGHE 20
Looping statements..
• Repeat Loop
module multiplier (result, op_a, op_b);
parameter size = 8;
input [size-1:0] op_a, op_b;
output [2*size:0] result;
* ANAND S MOGHE 21
Looping statements..
• while Loop
...
...
reg [7:0] tempreg;
. . .
. . .
while (tempreg) begin
if (tempreg[0]) count = count + 1;
tempreg = tempreg >> 1; // right shift
end
endmodule
• A while loop executes a block of statements as long as its expression is true (or
nonzero)
• If the expression starts out false, the statements are not executed.
* ANAND S MOGHE 22
Looping statements..
• for Loop
...
• The for loop functionality can be implemented with a while loop, but this
requires separate initialization and counter incrementing assignments, that are
now included as a part of the for loop.
* ANAND S MOGHE 23