BEC302 MOD-5 NOTES
BEC302 MOD-5 NOTES
MODULE – 5
Syllabus: Verilog Behavioral description: Structure, Variable Assignment Statement, Sequential
Statements, Loop Statements, Verilog Behavioral Description of Multiplexers (2:1, 4:1, 8:1).
(Section 3.1 to 3.4 (only Verilog)of Text 3)
Verilog Structural description: Highlights of Structural description, Organization of structural
description, Structural description of ripple carry adder.(Section4.1 to 4.2 of Text 3)
Text book: 3. HDL Programming VHDL and Verilog by Nazeih M Botros, 2009 reprint, Dream
techpress.
Introduction:
The behavioral description is a powerful tool to describe systems for which digital logic
structures are not known or are hard to generate.
The behavioral description describes the system by showing how outputs behave with the
changes in inputs. In this description, details of the logic diagram of the system are not needed;
what is needed is how the output behaves in response to a change in the input. In Verilog, the
major behavioral description statements are always and initial.
With the increasing complexity of digital design, it has become vitally important to make wise
design decisions early in a project.
Designers need to be able to evaluate the trade-offs of various architectures and algorithms
before they decide on the optimum architecture and algorithm to implement in hardware.
Thus, architectural evaluation takes place at an algorithmic level where the designers do not
necessarily think in terms of logic gates or data flow but in terms of the algorithm they wish to
implement in hardware. They are more concerned about the behavior of the algorithm and its
performance.
Only after the high-level architecture and algorithm are finalized, do designers start focusing
on building the digital circuit to implement the algorithm.
Verilog provides designers the ability to describe design functionality in an algorithmic manner.
In other words, the designer describes the behavior of the circuit. Thus, behavioral modeling
represents the circuit at a very high level of abstraction. Design at this level resembles C
Page 1
Digital System Design using Verilog BEC302
programming more than it resembles digital circuit design. Behavioral Verilog constructs are
similar to C language constructs in many ways.
Verilog is rich in behavioral constructs that provide the designer with a great amount of
flexibility.
Structured Procedures:
These statements are the two most basic statements in behavioral modeling. All other
behavioral statements can appear only inside these structured procedure statements.
initial Statement
o All statements inside an initial statement constitute an initial block.
o An initial block starts at time 0, executes exactly once during a simulation, and then
does not execute again.
o If there are multiple initial blocks, each block starts to execute concurrently at time 0.
o Each block finishes execution independently of other blocks. Multiple behavioral
statements must be grouped, typically using the keywords begin and end.
o If there is only one behavioral statement, grouping is not necessary.
o This is similar to the begin-end blocks in Pascal programming language or the { }
grouping in the C programming language.
Example: illustrates the use of the initial statement.
initial Statement
module stimulus;
reg x,y, a,b, m;
Page 2
Digital System Design using Verilog BEC302
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
In the above example, the three initial statements start to execute in parallel at time 0. If a
delay #<delay> is seen before a statement, the statement is executed <delay> time units after
the current simulation time.
Thus, the execution sequence of the statements inside the initial blocks will be as follows.
0 m = 1'b0;
5 a = 1'b1;
10 x = 1'b0;
30 b = 1'b0;
35 y = 1'b1;
50 $finish;
The initial blocks are typically used for initialization, monitoring, waveforms and other
processes that must be executed only once during the entire simulation run.
Page 3
Digital System Design using Verilog BEC302
always Statement
initial
clock = 1'b0;
always
initial
#1000 $finish;
endmodule
In Example:
The always statement starts at time 0 and executes the statement clock = ~clock every 10
time units. Notice that the initialization of clock has to be done inside a separate initial
statement. If we put the initialization of clock inside the always block, clock will be
initialized every time the always is entered. Also, the simulation must be halted inside an
initial statement. If there is no $ stop or $finish statement to halt the simulation, the clock
generator will run foreverC programmers might draw an analogy between the always block
and an infinite loop. But hardware designers tend to view it as a continuously repeated
Page 4
Digital System Design using Verilog BEC302
activity in a digital circuit starting from power on. The activity is stopped only by power
off ($finish) or by an interrupt ($stop).
2. Procedural Assignments
The left-hand side of a procedural assignment <lvalue> can be one of the following:
Blocking Assignments
o Blocking assignment statements are executed in the order they are specified in a
sequential block.
o A blocking assignment will not block execution of statements that follow in a
parallel block..
o 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
Page 5
Digital System Design using Verilog BEC302
end
Nonblocking Assignments
To illustrate the behavior of nonblocking statements and its difference from blocking
statements, let us consider Example, where we convert some blocking assignments to
nonblocking assignments, and observe the behavior.
Page 6
Digital System Design using Verilog BEC302
reg_b[15:13] <= #10 {x, y, z}; //Assign result of concatenation //to part select
of a vector
end
In this example, the statements x = 0 through reg_b = reg_a are executed sequentially at
time 0. Then the three nonblocking assignments are processed at the same simulation time.
In the example above, we mixed blocking and nonblocking assignments to illustrate their
behavior. However, it is recommended that blocking and nonblocking assignments not be
mixed in the same always block.
Page 7
Digital System Design using Verilog BEC302
Conditional Statements
Conditional statements are used for making decisions based upon certain conditions. These
conditions are used to decide whether or not a statement should be executed. Keywords if
and else are used for conditional statements. There are three types of conditional
statements. Usage of conditional statements is shown below.
if (<expression>) true_statement ;
//Type 2 conditional statement. One else statement //Either true_statement
or false_statement is evaluated
if (<expression1>) true_statement1 ;
else if (<expression2>) true_statement2 ;
else default_statement ;
Example:
Page 8
Digital System Design using Verilog BEC302
y = x - z;
else if(alu_control == 2)
y = x * z;
else
$display("Invalid ALU control signal");
Multiway Branching
1 case Statement
The keywords case, endcase, and default are used in the case statement..
case (expression)
alternative1: statement1;
alternative2: statement2;
alternative3: statement3;
...
...
default: default_statement;
endcase
Each of statement1, statement2 , default_statement can be a single statement or a block of
multiple statements. A block of multiple statements must be grouped by keywords begin and
end.
The expression is compared to the alternatives in the order they are written. For the first
alternative that matches, the corresponding statement or block is executed.
If none of the alternatives matches, the default_statement is executed.
The default_statement is optional.
Placing of multiple default statements in one case statement is not allowed.
The case statements can be nested.
Page 9
Digital System Design using Verilog BEC302
...
...
case (alu_control)
2'd0 : y = x + z;
2'd1 : y = x - z;
2'd2 : y = x * z;
case ({s1, s0}) //Switch based on concatenation of control signals 2'd0 : out = i0;
2'd1 : out = i1;
endcase
endmodule
Page 10
Digital System Design using Verilog BEC302
The case statement compares 0, 1, x, and z values in the expression and the alternative bit
for bit. If the expression and the alternative are of unequal bit width, they are zero filled to
match the bit width of the widest of the expression and the alternative. In Example 4-20 , we
will define a 1-to-4 demultiplexer for which outputs are completely specified, that is,
definitive results are provided even for x and z values on the select signal.
casez treats all z values in the case alternatives or the case expression as don't cares.
All bit positions with z can also represented by ? in that position.
casex treats all x and z values in the case item or the case expression as don't cares.
The use of casex and casez allows comparison of only non-x or -z positions in the case
expression and the case alternatives. Example 4-21 illustrates the decoding of state bits
in a finite state machine using a casex statement.
The use of casez is similar. Only one bit is considered to determine the next state and
the other bits are ignored.
endcase
Page 11
Digital System Design using Verilog BEC302
Loops
There are four types of looping statements in Verilog: while, for, repeat, and forever. The
syntax of these loops is very similar to the syntax of loops in the C programming language.
All looping statements can appear only inside an initial or always block. Loops may contain
delay expressions.
1 While Loop
The keyword while is used to specify this loop. The while loop executes until the while-
expression is not true. If the loop is entered when the while-expression is not true, the loop
is not executed at all. Any logical expression can be specified with these operators. If
multiple statements are to be executed in the loop, they must be grouped typically using
keywords begin and end. Example bwlow illustrates the use of the while loop.
count = 0;
while (count < 128) //Execute loop till count is 127.,exit at count 128
begin
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
Page 12
Digital System Design using Verilog BEC302
The counter described in Example 4-22 can be coded as a for loop (Example 4-23). The
initialization condition and the incrementing procedural assignment are included in the for
loop and do not need to be specified separately. Thus, the for loop provides a more compact
loop structure than the while loop. Note, however, that the while loop is more general-
purpose than the for loop. The for loop cannot be used in place of the while loop in all
situations.
integer count;
initial
for loops are generally used when there is a fixed beginning and end to the loop. If the
loop is simply looping on a certain condition, it is better to use the while loop.
3 Repeat Loop
Page 13
Digital System Design using Verilog BEC302
initial
begin
count = 0;
repeat(128)
begin
count = count + 1;
end
end
4 Forever loop
Example:Forever Loop
: Clock generation
reg clock;
initial
Page 14
Digital System Design using Verilog BEC302
begin
clock = 1'b0;
end
Block statements are used to group multiple statements to act together as one. In previous
examples, we used keywords begin and end to group multiple statements. Thus, we used
sequential blocks where the statements in the block execute one after another. In this section
we discuss the block types: sequential blocks and parallel blocks. We also discuss three special
features of blocks: named blocks, disabling named blocks, and nested blocks.
1 Block Types
There are two types of blocks: sequential blocks and parallel blocks.
Sequential blocks
The keywords begin and end are used to group statements into sequential blocks.
The statements in a sequential block are processed in the order they are
specified. A statement is executed only after its preceding statement completes execution
(except for nonblocking assignments with intra-assignment timing control).
Page 15
Digital System Design using Verilog BEC302
reg x, y;
reg [1:0] z, w;
initial
begin
x = 1'b0;
y = 1'b1;
z = {x, y};
w = {y, x};
end
Parallel blocks
Parallel blocks, specified by keywords fork and join, provide interesting simulation
features. Parallel blocks have the following characteristics:
If delay or event control is specified, it is relative to the time the block was entered.
Notice the fundamental difference between sequential and parallel blocks. All statements
in a parallel block start at the time when the block was entered. Thus, the order in which the
statements are written in the block is not important.\
Example:Parallel Blocks
Page 16
Digital System Design using Verilog BEC302
reg x, y;
reg [1:0] 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 10
#20 w = {y, x}; //completes at simulation time 20
join
Page 17
Digital System Design using Verilog BEC302
Examples
1 4-to-1 Multiplexer
output out;
input i0, i1, i2, i3; input s1,
s0;
reg out;
//recompute the signal out if any input signal changes. //All input signals
that cause a recomputation of out to //occur must go into the always @(...)
sensitivity list.
always @(s1 or s0 or i0 or i1 or i2 or i3)
begin
endcase
end
endmodule
Page 18
Digital System Design using Verilog BEC302
I/O ports
output [3:0] Q;
input clock, clear; //output defined as
register
reg [3:0] Q;
always @( posedge clear or negedge clock)
begin
if (clear)
else
end
endmodule
Page 19