Professional Documents
Culture Documents
Module 5 DSDV Notes New
Module 5 DSDV Notes New
Module-5
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. (Section 4.1 to 4.2 of Text
3)
Dept of ECE, 1
Referring to the Verilog code in Listing 3.1, always is the Verilog behavioural statement all
Verilog statements inside always are treated as concurrent, the same as in the data-flow
description . Also, here any signal that is declared as an output or appears at the left-hand side
of a signal-assignment statement should be declared as a register (reg) if it appears inside
always. In Listing 3.1, O1 and O2 are declared outputs, so they should also be declared as reg.
LISTING 3.1 Example of an HDL Behavioral Description
Verilog Description
module half_add (I1, I2, O1, O2);
input I1, I2;
output O1, O2;
reg O1, O2;
/* Since O1 and O2 are outputs and they are written inside “always,” they should be declared
as reg */
always @(I1, I2)
begin
#10 O1 = I1 ^ I2; // statement 1.
#10 O2 = I1 & I2; // statement 2.
/*The above two statements are signal-assignment statements with 10 simulation screen units
delay*/
/*Other behavioral (sequential) statements can be added here*/
end
endmodule
Sequential Statements
There are several statements associated with behavioral descriptions. These statements have to
appear inside always or initial in Verilog. The following sections discuss some of these
statements.
IF Statement
IF is a sequential statement that appears inside always or initial in Verilog. It has several
formats, some of which are as follows:
Verilog IF-Else Formats
if (Boolean Expression)
begin
statement 1; /* if only one statement, begin and end can be omitted */
statement 2;
statement 3;
.......
end
else
begin
statement a; /* if only one statement, begin and end can be omitted */
statement b;
statement c;
.......
end
Dept of ECE, 2
Dept of ECE, 3
TABLE 3.1 Output Signals (temp) for Else-IF Statements in Example 3.4
The Boolean expression may specify other relational operations such as inequality or greater
than or less than. To illustrate the difference between signal- and variable-assignment
statements in VHDL code, the behavioral description of a D-latch is written in Example 3.5. A
process is written based on signal-assignment statements, and another process is written based
on variable-assignment statements. A comparison of the simulation waveforms of the two
processes will highlight the differences between the two assignment statements.
EXAMPLE 3.5 BEHAVIORAL DESCRIPTION OF A LATCH USING VARIABLE
AND SIGNAL ASSIGNMENTS
The functionality of a D-latch can be explained as follows: if the enable (E) is active, the output
of the latch (Q) follows the input (d); otherwise, the outputs remain unchanged. Also, Qb, the
invert output, is always the invert of Q. Figure 3.3a shows the logic symbol of a D-latch. A
flowchart that illustrates this functionality is shown in Figure 3.3b.
Dept of ECE, 4
Dept of ECE, 5
Verilog Description
module mux2x1 (A, B, SEL, Gbar, Y);
input A, B, SEL, Gbar;
output Y;
reg Y;
always @ (SEL, A, B, Gbar)
begin
if (Gbar == 1)
Y = 1’bz;
else
begin
if (SEL)
Y = B;
else
Y = A;
end
end
end module
Dept of ECE, 6
Dept of ECE, 7
Table 3.2 shows the excitation table of the JK flip-flop. It conveys the same information as the
state diagram. The state diagram (Figure 3.8b) shows the possible states (two in this case: q
can take 0 or 1), state 0 and state 1. The transition between these states has to occur only at the
positive edges of the clock. If the current state is 0 (q = 0), then the next state is 0(1) if JK =
0x(1x), where x is “don’t care.” If the current state is 1 (q = 1), then the next state is 1(0) if JK
= x0(x1). Table 3.2 shows the same results as the state diagram. For example, a transition from
0 to 1, according to the excitation table, can occur if JK = 10 or JK = 11, which is JK = 1x.
Listing 3.7 shows the HDL code for a positive edge-triggered JK flipflop using the case
statement. In the Listing, rising_edge (VHDL) and posedge (Verilog) are predefined words
called attributes. They represent the positive edge of the clock (clk). If the positive edge is
present, the attribute yields to true. For VHDL, the clk has to be in std_logic to use this attribute.
Other attributes are covered in Chapters 4, 6, and 7. Any of the four case statements can be
replaced with default(Verilog).
For example:
2’d3 : q =~ q; // Verilog
can be replaced by:
default : q =~ q; // Verilog
The waveform of the flip-flop is shown in Figure 3.9.
Dept of ECE, 8
LISTING 3.7 HDL Code for a Positive Edge-Triggered JK Flip-Flop Using the caseStatement
Verilog Description
module JK_FF (JK, clk, q, qb);
input [1:0] JK;
input clk; output
q, qb;reg q, qb;
always @ (posedge clk)
begin
case (JK) 2’d0 : q
= q;
2’d1 : q = 0;
2’d2 : q = 1;
2’d3 : q =~ q;
endcase
qb =~ q; end
endmodule
Dept of ECE, 9
The most efficient approach to describe the above counter is to use the fact that the next state is
the increment of the present for upward counting. The goal here, however, is to use the case
statement. Table 3.3 is treated as a look-up table. Listing 3.8 shows the HDL code for the counter.
To assign initial values, such as 101, to the count at the start of simulation in Verilog, the
procedural initial is used as follows:
initial
begin
q = 3’b101;
end
The begin and end can be omitted if there is a single initial statement Figure 3.11 shows the
simulation waveform of the counter.
LISTING 3.8 HDL Code for a Three-Bit Binary Counter Using the case Statement
Verilog Description
module CT_CASE (clk, clr, q);
input clk, clr;
output [2:0] q;
reg [2:0] q;
initial
/* The above initial statement is to force the counter to start from initial count q=110 */q =
3’b101;
always @ (posedge clk)
begin
if (clr == 0)
begin
case (q)
3’d0 : q = 3’d1;
3’d1 : q = 3’d2;
3’d2 : q = 3’d3;
3’d3 : q = 3’d4;
3’d4 : q = 3’d5;
3’d5 : q = 3’d6;
3’d6 : q = 3’d7;
3’d7 : q = 3’d0;
endcaseend
else
q = 3’b000;
end endmodule
Dept of ECE, 10
Dept of ECE, 11
LISTING 3.11 Verilog Description for a Four-Bit Priority Encoder Using casex
module Encoder_4 (Int_req, Rout_addrs);
input [3:0] Int_req;
output [3:0] Rout_addrs;
reg [3:0] Rout_addrs;
always @ (Int_req) begin
casex (Int_req)
4’bxxx1 : Rout_addrs=4’d1;
4’bxx10 : Rout_addrs=4’d2;
4’bx100 : Rout_addrs=4’d4;
4’b1000 : Rout_addrs= 4’d8;
default : Rout_addrs=4’d0;
endcase
end endmodule
Dept of ECE, 12
always
begin #20 ;
b = ~ b;end
always
begin #40 ;
c = ~ c; end
endmodule
Dept of ECE, 13
While-Loop
The general format of the While-Loop is:
while (condition)
Statement1;
Statement2;
…………end
As long as the condition is true, all statements written before the end of the loop are executed.
Otherwise, the program exits the loop.
In this example, the Loop statement is used to convert values between binary and integer and use
this conversion to describe a binary up counter. The HDL package is assumed to not contain
predefined functions that will increment a binary input or convert values between binary and
integer. In addition, the current and next state are expressed in binary rather than integer.
Describing a counter using the above binary-to-integer conversion is not the most efficient way;
the main goal here is to demonstrate the implementation of the Loop statement.
Dept of ECE, 14
The next state of a binary counter is generated by incrementing the current state. Because, in this
example, a binary value cannot be incremented directly by the HDL code, it is first converted to an
integer. HDL packages can easily increment integers. We increment the integer and convert it back
to binary. To convert an integer to binary, the predefined operator % in Verilog is used. For
example: (X MOD 2) equals 1 if X is 1 (odd) or equals 0 if X is 0 (even, divisible by 2). By
successively dividing the integer by 2 and recording the remainder from theoutcome of the MOD2,
the integer is converted to binary. To convert a binary to integer, multiply each bit by its weight
and accumulate the products: 10112 = (1 × 1) + (1 × 2) + (0 × 4) + (1 × 8) = 1110. If the bit is
equal to 0, it can be ignored. Listing 3.13 shows the HDL code of the counter. The simulation
waveform is the same as that shown in Figure 3.11, except the count here is from 0 to 15 rather
than from 0 to 7 as in the figure.
LISTING 3.13 HDL Code for a Four-Bit Counter With Synchronous Clear: Verilog
Verilog Description
module CNTR_LOP (clk, clr, q);
input clk, clr;
output [3:0] q;
reg [3:0] q; integer i, j,
result;initial
begin
q = 4’b0000; //initialize the count to 0
end
always @ (posedge clk)
begin
if (clr == 0)
begin
result = 0;
//change binary to integerfor (i
= 0; i < 4; i = i + 1) begin
if (q[i] == 1) result =
result + 2end i;
result = result + 1;
for (j = 0; j < 4; j = j + 1)
begin
if (result %2 == 1)
q[j] = 1;
else
q[j] = 0;
result = result/2;end
end
else q = 4’b0000;end
endmodule
A more efficient approach to describe a binary counter is to directly increment the current state. As
mentioned before, the approach implemented in Listing 3.13 is not the most efficient way to
describe a counter. To write an efficient code for a four-bit counter, direct increment of the current
state is used. The following Verilog code describes a four-bit binary counter using direct
increment of the current state:
Dept of ECE, 15
LISTING 3.14 HDL Code for a Four-Bit Counter with Synchronous Hold:Verilog
Verilog 4-Bit Counter with Synchronous Hold Description
module CT_HOLD (clk, hold, q);
input clk, hold;
output [3:0] q;
reg [3:0] q; integer i,
result;initial
begin
q = 4’b0000; //initialize the count to 0
end
always @ (posedge clk)
begin
result = 0;
Dept of ECE, 16
Dept of ECE, 17
Listing 3.15 shows a HDL code for describing a logical shift, as shown in Figure 3.18, using the
Loop statement. The code shifts register q n bits right or left logically. The number of bits to be
shifted is determined by user-selected parameter N. The code resembles the preserved statement
sll and slr in VHDL and ( << and >>) in Verilog. $display statement in Listing 3.15 is one of
Verilog’s system tasks that displays values of objects on the console of the simulator.The statement
$display (“ i= %d”, i);
will display a printout of the text between the quotation marks ( i = ) excluding the %d, which
determines that the object should be displayed in decimal. The i after the comma is the object to be
displayed. The $display is a tool that can be used to display objects that are not listed as an output.
Several other formats can be selected for display such as:
%b for binary
%o for octal
$d for decimal
%h for hexadecimal
%t for time
%e or %f or %g for real
%c for character
%s for string
%v for binary and strength
LISTING 3.15 HDL Code for Logical Shifting of a Register Using the Loop Statement
Verilog Description
module shft_regVerilog(start,shft, N,q);
input start,shft;
input [7:1] N;
//N is number of requested shifts
output [7:0]q;
reg [7:0]q;
integer i,j;
initial
q = 8’b01100110;
/*initial values for the vector is selected to be 1100110 */
always @ (posedge start)
begin
lop2: for (j= 1; j <= N; j = j +1)
begin
lop1: for (i= 0; i <= 6; i = i +1)
begin
if (shft == 1’b0 )
/*shft = 0 is logical right shift; =1 logical left Shift */
begin
$display (“ shft = %d”, shft);/ This is a system taskto
display The value of shift on the console’s
screen of the simulator /
Dept of ECE, 18
Facts
• Structural description simulates the system by describing its logical components. The
components can be gate level (such as AND gates, OR gates, or NOT gates), or components
can be in a higher logical level, such as register-transfer level (RTL) or processor level.
• It is more convenient to use structural description than behavioural description for systems
that require specific design constraints. Consider, for example, a system performing the
operation A + B = C. In behavioural description, the addition can be written as C = A + B with
no choice in selecting the type of adders used to perform this addition. In structural description,
the type of adder, such as look-ahead adders, can be selected.
• ll statements in structural description are concurrent. At any simulation time, all statements that
have an event are executed concurrently.
• A major difference between VHDL and Verilog structural description is the availability of
components (especially primitive gates) to the user. Verilog recognizes all the primitive gates
such as AND, OR, XOR, NOT, and XNOR gates. Basic VHDL packages do not recognize any
gates unless the package is linked to one or more libraries, packages, or modules that have the
gate description.
•
Organization of Structural Description
Listing 4.1 shows an example of HDL code that describes a half adder under the name of system
using structural description. The module (Verilog) name is system; there are two inputs, a and b,
and two outputs, sum and cout. The module declaration is the same as in other description styles
previously covered (data flow and behavioral).
Verilog has a large number of built-in gates. For example, the statement:xor
X1 (sum, a, b);
Dept of ECE, 19
describes a two-input XOR gate. The inputs are a and b, and the output is sum. X1 is an optional
identifier for the gate; the identifier can be omitted as:
xor (sum, a, b);
Verilog has a complete list of built-in primitive gates. The output of the gate sum has to be listed
before the inputs a and b. Accordingly, the Verilog code in Listing 4.1 is a complete structural
description of a half adder. Figure 4.1 shows a list of gates and their code in Verilog. As in
structural Verilog statements are concurrent; the order of appearance of statements in the module is
irrelevant.
Dept of ECE, 20
The VHDL code looks much longer than the Verilog code. This is due to the assumption that the
basic VHDL packages do not have built-in libraries or packages for logical gates. The binding
method above becomes impractical when the number of gates becomes large. Every time a new
description is written, the entities of all gates used must also be written.
shown. Listing 4.14 shows the structural description of the three-bit ripple-carry adder.
Dept of ECE, 21