Module 5 Notes
Module 5 Notes
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,SJBIT 1
Digital System Design using Verilog 21EC32
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
Dept of ECE,SJBIT 2
Digital System Design using Verilog 21EC32
Verilog
if (clk == 1’b1)
// 1’b1 means 1-bit binary number of value 1.
temp = s1;
else
temp = s2;
In Example 3.1, if clk is high (1), the value of s1 is assigned to the variable temp. Otherwise,
s2 is assigned to the variable temp. The else statement can be eliminated, and in this case, the
IF statement simulates a latch, as shown in Example 3.2.
Dept of ECE,SJBIT 3
Digital System Design using Verilog 21EC32
Verilog
if (clk == 1)
begin
temp = s1;
end
If clk is high, the value of s1 is assigned to temp. If clk is not high, temp retains its current
value, thus simulating a latch. Another format for the IF statement is Else-IF.
Verilog
if (Boolean Expression1)
begin
statement1; statement 2;.....
end
else if (Boolean expression2)
begin
statementi; statementii;.....
end
else
begin
statementa; statement b;....
end
Verilog
if (signal1 == 1’b1)
temp = s1;
else if (signal2 == 1’b1)
temp = s2;
else
temp = s3;
After execution of the above IF statement, temp acquires the values shown in Table 3.1.
TABLE 3.1 Output Signals (temp) for Else-IF Statements in Example 3.4
Dept of ECE,SJBIT 4
Digital System Design using Verilog 21EC32
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.
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,SJBIT 5
Digital System Design using Verilog 21EC32
begin
if (E == 1)
begin
Q = d;
Qb = ~ Q;
end
end
endmodule
Dept of ECE,SJBIT 6
Digital System Design using Verilog 21EC32
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
endmodule
Dept of ECE,SJBIT 7
Digital System Design using Verilog 21EC32
Dept of ECE,SJBIT 8
Digital System Design using Verilog 21EC32
If, for example, test value1 is true (i.e., it is equal to the value of the control expression),
statements1 is executed. The case statement must include all possible conditions (values) of
the control-expression. The statement when default (Verilog) can be used to guarantee that all
conditions are covered. The case resembles IF except the correct condition in case is
determined directly, not serially as in IF statements. The begin and end are not needed in
Verilog if only a single statement is specified for a certain test value. The case statement can
be used to describe data listed into tables.
Dept of ECE,SJBIT 9
Digital System Design using Verilog 21EC32
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,SJBIT 10
Digital System Design using Verilog 21EC32
LISTING 3.7 HDL Code for a Positive Edge-Triggered JK Flip-Flop Using the case
Statement
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
Counters are sequential circuits. For count-up counters (or simply up counters), the next state
is the increment of the present state. For example, if the present state is 101, then the next state
is 110. For down-count counters (or simply down counters), the next state is the decrement of
the present state. For example, if the present state is 101, then the next state is 100. A three-bit
binary up counter counts from 0 to 7 (Mod 8). Decade counters count from 0 to 9 (Mod10).
Synchronous clear means that clear resets the counter when the clock is active; in contrast,
asynchronous clear resets the counter instantaneously. The counter can be depicted by a
flowchart showing its function (see Figure 3.10). Although the flowchart here represents a
counter, it could have represented any other system with the same behavior. The excitation
table for the three-bit binary counter is as shown in Table 3.3. The logic symbol is shown in
Figure 3.10a.
Dept of ECE,SJBIT 11
Digital System Design using Verilog 21EC32
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
Dept of ECE,SJBIT 12
Digital System Design using Verilog 21EC32
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;
endcase
end
else
q = 3’b000;
end
endmodule
Dept of ECE,SJBIT 13
Digital System Design using Verilog 21EC32
Dept of ECE,SJBIT 14
Digital System Design using Verilog 21EC32
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,SJBIT 15
Digital System Design using Verilog 21EC32
initial
begin
// Initialize Inputs
a = 0;
b = 0;
c = 0;
end
always
begin
#10 ;
a = ~ a;
end
always
begin
#20 ;
b = ~ b;
end
always
begin
#40 ;
c = ~ c;
end
endmodule
Dept of ECE,SJBIT 16
Digital System Design using Verilog 21EC32
[Link] 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.
Dept of ECE,SJBIT 17
Digital System Design using Verilog 21EC32
Dept of ECE,SJBIT 18
Digital System Design using Verilog 21EC32
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 the
outcome 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 integer
for (i = 0; i < 4; i = i + 1)
begin
if (q[i] == 1)
result = result + 2 i;
end
result = result + 1;
for (j = 0; j < 4; j = j + 1)
begin
if (result %2 == 1)
q[j] = 1;
else
q[j] = 0;
Dept of ECE,SJBIT 19
Digital System Design using Verilog 21EC32
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:
module countr_direct (clk, Z);
input clk;
output [3:0] Z;
reg [3:0] Z;
initial
Z = 4’b0000;
/*This initialization is needed if we want to start counting from 0000 */
always @ (posedge clk)
Z = Z + 1;
endmodule
Dept of ECE,SJBIT 20
Digital System Design using Verilog 21EC32
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;
//change binary to integer
for (i = 0; i <= 3; i = i + 1)
begin
if (q[i] == 1)
result = result + 2 i;
end
result = result + 1;
for (i = 0; i <= 3; i = i + 1)
begin
if (hold == 1)
i = 4; //4 is out of range, exit.
else
begin
Dept of ECE,SJBIT 21
Digital System Design using Verilog 21EC32
if (result %2 == 1)
q[i] = 1;
else
q[i] = 0;
result = result/2;
end
end
end
endmodule
Dept of ECE,SJBIT 22
Digital System Design using Verilog 21EC32
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 */
Dept of ECE,SJBIT 23
Digital System Design using Verilog 21EC32
begin
$display (“ shft = %d”, shft);/ This is a system task
to display The value of shift on the console’s
screen of the simulator /
$display (“ i= %d”, i);
$display (“q[i] = %b”, q[i]);
$display (“q[i+1] = %b”, q[i+1]);
q[i] = q[i+1];
q[7] =1’b0; $display (“ q = %b”, q);end
else
begin q[7-i] = q[6-i];
q[0] = 1’b0; end
$display (“ shft = %d”, shft);
end
end
end
Dept of ECE,SJBIT 24
Digital System Design using Verilog 21EC32
Dept of ECE,SJBIT 25
Digital System Design using Verilog 21EC32
Verilog Description
Module system (a, b, sum, cout);
input a, b;
output sum, cout;
xor X1 (sum, a, b);
/* X1 is an optional identifier; it can be omitted.*/
and a1 (cout, a, b);
/* a1 is optional identifier; it can be omitted.*/
endmodule
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.
Dept of ECE,SJBIT 26
Digital System Design using Verilog 21EC32
Inspection of the code in Listing 4.14 shows that there may be lag time between the steady state
of each of the adders and the carryout (cout). This lag time produces transient states before the
values of the sum and carryout settle. For example, if the inputs to the adder are 101 and 001,
and the previous output of the adder is 1001, some transient states can be 0100 and 1010 before
the output settles at 0110. The appearance of these transient states is called hazards. These
transient states, however, have short duration and may not be noticed.
Dept of ECE,SJBIT 27