Lab - 04 - Handout - Behavioral Modeling
Lab - 04 - Handout - Behavioral Modeling
Islamabad
Digital System Design LAB
Name of Student:
Roll No.:
Date of Experiment:
Marks obtained:
Remarks:
Instructor’s Signature:
2. Resources Required
• A Computer
• Xilinx ISE
• ModelSim
3. Introduction
HDL (Hardware Description Language) is any language from a class of computer languages,
specification languages, or modeling languages for formal description and design of electronic
circuits, and most-commonly, digital logic. It can describe the circuit's operation, its design and
organization, and tests to verify its operation by means of simulation. The two most popular HDLs
are Verilog and VHDL. Verilog due to its similarity to C language is easier to understand so has
become most widely used HDL in educational institutions.
Dataflow level
At this level, the module is designed by specifying the data flow. The designer is aware of how
data flows between hardware registers and how the data is processed in the design.
Gate level
The module is implemented in terms of logic gates and interconnections between these gates.
Design at this level is similar to describing a design in terms of a gate-level logic diagram.
Switch level
This is the lowest level of abstraction provided by Verilog. A module can be implemented in terms
of switches, storage nodes, and the interconnections between them. Design at this level requires
knowledge of switch-level implementation details.
Verilog allows the designer to mix and match all four levels of abstractions in a design.
Due to increasing complexity of circuits, Switch Level Modeling is becoming rare so we will
not discuss it in these labs.
a) initial Statement
All statements inside an initial statement constitute an initial block. An initial block starts at time
0, executes exactly once during a simulation, and then does not execute again. If there are multiple
initial blocks, each block starts to execute concurrently at time 0. Each block finishes execution
independently of other blocks. Multiple behavioral statements must be grouped; typically using
the keywords begin and end. If there is only one behavioral statement, grouping is not necessary.
This is similar to the { } grouping in the C programming language.
Example:
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;
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
The initial blocks are typically used for initialization, monitoring, waveforms and other processes
that must be executed only once during the entire simulation run.
b) always Statement
All behavioral statements inside an always statement constitute an always block. The always
statement starts at time 0 and executes the statements in the always block continuously in a looping
fashion. This statement is used to model a block of activity that is repeated continuously in a digital
circuit. An example is a clock generator module that toggles the clock signal every half cycle. In
real circuits, the clock generator is active from time 0 to as long as the circuit is powered on.
Example:
initial
#1000 $finish;
In the 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 forever.
C programmers might draw an analogy between the always block and an infinite loop. But
hardware designers tend to view it as a continuously repeated activity in a digital circuit starting
from power on. The activity is stopped only by power off ($finish) or by an interrupt ($stop).
3.2.2 Procedural Assignment
Procedural assignments update values of reg, integer, real, or time variables. The value placed on
a variable will remain unchanged until another procedural assignment updates the variable with a
different value. These are unlike continuous assignments discussed in Dataflow Modeling, where
one assignment statement can cause the value of the right-hand-side expression to be continuously
The left-hand side of a procedural assignment <lvalue> can be one of the following:
The right-hand side can be any expression that evaluates to a value. In behavioral modeling, all
operators discussed in Dataflow Modeling can be used in behavioral expressions.
Example
Example
Sensitivity lists can also be specified using the "," (comma) operator instead of the or operator.
Comma operators can also be applied to sensitivity lists that have edge-sensitive triggers. The
above example can be rewritten using the comma operator, the only change would be:
When the number of input variables to a combination logic block are very large, sensitivity lists
can become very cumbersome to write. To solve this problem, Verilog HDL contains two special
symbols: @* and @(*). Both symbols exhibit identical behavior. These special symbols are
sensitive to a change on any signal that may be read by the statement group that follows this
symbol.
case (expression)
alternative1: statement1;
alternative2: statement2;
alternative3: statement3;
...
endmodule
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.
4. Verilog Codes (to be utilized in this lab)
output S, C;
reg S, C; //output must be declared as register in behavioral modeling
input A, B;
//always block makes sure that whenever is a change in A or B, we enter this block
always @ (A or B)
begin
endmodule
module Stimulus;
//This time we have instantiated full-adder module as it is the one we are testing
5. Lab Task
Implement an 8X3 (Octal to Binary) encoder in Verilog using Behavioral modeling. Also
simulate your design for verification (Create a proper Stimulus or Test Bench file).
Hint: Use if-else or case statements. Set D with 8 bit size ([7:0]). It is clear from above table that
when D=8’b00000001 then x=0,y=0,z=0; i.e. we can write
6. Home Work
Implement a 4X2 priority encoder using behavioral modeling. Simulate in either ModelSim
or Xilinx ISE. Submit the code and wave files in the next lab.
Note:
a) This assignment must be submitted before the next lab.
b) The assignment submitted must be in proper format as instructed by the teacher to get
maximum marks.
c) Marks will be deducted on late submissions.
d) Cheating or using any unfair means will award ZERO marks.
Q.3 Write the code for implementing 4X1 Mux using behavioral modeling.
________________________________________________________________________
________________________________________________________________________
________________________________________________________________________
________________________________________________________________________
________________________________________________________________________