Module-5 DSD
Module-5 DSD
MODULE-5
INTRODUCTION TO VERILOG
Verilog Behavioral description: Highlights of behavioral description, Structure of behavioral description,
Variable Assignment Statement, Sequential Statements, Loop Statements, Verilog Behavioral Description
of Multiplexers (2:1, 4:1, 8:1).
(Section 3.1 to 3.4 (onlyVerilog) 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)
Dept.of ECE, RIT, Hassan Prepared by: Mr. Ravi L.S Page 1
Module-5 DIGITAL SYSTEM DESIGN USING VERILOG (BEC302)
carry=1,b0;
end
2’b01:begin
sum =1’b1;
carry =1,b0;
end
2’b01:begin
sum =1’b1;
carry =1,b0;
end
default: begin
sum =1’b0;
carry =1,b1;
end
endcase
endmodule
Sequential Statements
There are several statements associated with behavioral descriptions. These statements have
to appear inside inside always or initial in Verilog.
IF Statement
Syntax
if (Boolean Expression)
begin
statement 1;
statement 2;
statement 3;
.......
end
Note: begin and end can be omitted if there is only statement
Example:
if (clk == 1)
temp = s1;
IF-ELSE Statement
Syntax
if (Boolean Expression)
begin statement 1;
statement 2;
statement 3;
.......
end
Dept.of ECE, RIT, Hassan Prepared by: Mr. Ravi L.S Page 2
Module-5 DIGITAL SYSTEM DESIGN USING VERILOG (BEC302)
else
begin
statement a;
statement b;
statement c;
.......
end
Example:
ELSE-IF statement
Syntax
if (Boolean Expression1)
begin
statement1;
statement2;
.....
end
else if (Boolean expression2)
begin
statementi;
statementj;
.....
end
else
begin
statementa;
statementb;
....
end
Example:
if (signal1 == 1’b1)
temp = s1;
else if (signal2 == 1’b1)
temp = s2;
else
temp = s3;
Dept.of ECE, RIT, Hassan Prepared by: Mr. Ravi L.S Page 3
Module-5 DIGITAL SYSTEM DESIGN USING VERILOG (BEC302)
Y = A;
end
end
endmodule
HDL Description of a 2x1 Multiplexer Using Else-IF
module mux2x1 (A, B, SEL, Gbar, Y);
input A, B, SEL, Gbar;
output Y;
reg Y;
always @ (SEL, A, B, Gbar)
begin
if (Gbar == 0 & SEL == 1)
Y = B;
else if (Gbar == 0 & SEL == 0)
Y = A;
else
Y = 1’bz; //Y is assigned to high impedance
end
endmodule
Case Statement
The case statement is a sequential control statement. It has the follow ing format:
syntax
case (control-expression)
test value1 : begin statements1; end
test value2 : begin statements2; end
test value3 : begin statements3; end
default : begin default statements end
endcase
Example:
case (sel)
2’b00 : temp = I1;
2’b01 : temp = I2;
2’b10 : temp = I3;
default : temp = I4;
endcase
Dept.of ECE, RIT, Hassan Prepared by: Mr. Ravi L.S Page 5
Module-5 DIGITAL SYSTEM DESIGN USING VERILOG (BEC302)
2’b01 : Y = I[1];
2’b10 : Y = I[2];
default : Y = I[3];
endcase
end
endmodule
Example: BEHAVIORAL DESCRIPTION OF A 8x1 MULTIPLEXER
module mux_8_1(I,SEL,Y);
input[7:0]I;
input[2:0]SEL;
output Y;
reg Y;
always@(I,SEL)
begin
case (SEL)
3’b000 : Y = I[0];
3’b001: Y = I[1];
3’b010 : Y = I[2];
3’b011 : Y = I[3];
3’b100 : Y = I[4];
3’b101 : Y = I[5];
3’b110 : Y = I[6];
default: Y = I[7];
endcase
end
endmodule
Dept.of ECE, RIT, Hassan Prepared by: Mr. Ravi L.S Page 6
Module-5 DIGITAL SYSTEM DESIGN USING VERILOG (BEC302)
Dept.of ECE, RIT, Hassan Prepared by: Mr. Ravi L.S Page 7
Module-5 DIGITAL SYSTEM DESIGN USING VERILOG (BEC302)
Dept.of ECE, RIT, Hassan Prepared by: Mr. Ravi L.S Page 8
Module-5 DIGITAL SYSTEM DESIGN USING VERILOG (BEC302)
If the value of index is between lower and upper, all statements writ ten inside the body of
the loop are executed. For each cycle, the index is modified at the end loop according to the
step. If the value of index is not between the lower and upper values, the loop is terminated.
Example:
for (i = 0; i <= 2; i = i + 1)
begin
if (temp[i] == 1’b1)
begin
result = result + 2**i ;
end
end
statement1; statement2;
....
While-Loop
The general format of the While-Loop is:
while (condition)
Statement1;
Statement2;
…………
end
Example:
while (i < x)
begin
i = i + 1;
z= i*z ;
end
Verilog repeat
In Verilog, the sequential statement repeat causes the execution of statements between its
begin and end to be repeated a fixed number of times; no condition is allowed in repeat.
Example:
repeat (32)
begin
Dept.of ECE, RIT, Hassan Prepared by: Mr. Ravi L.S Page 9
Module-5 DIGITAL SYSTEM DESIGN USING VERILOG (BEC302)
#100 i = i + 1;
end
Verilog forever
The statement forever in Verilog repeats the loop endlessly. One common use for forever is
to generate clocks in code-oriented test benches.
Example:
initial
begin
Clk = 1’b0;
forever #20 clk = ~clk;
end
Calculating the Factorial Using Behavioral Description with WHILE-LOOP
The factorial of N is (N!) = Nx(N-1)x(N 2)x(N-3)x ….x1.
For example, 4!=4×34×24×1=24.
Verilog Description
module factr (N, z);
input [5:0] N;
output [15:0] z;
reg [15:0] z;
integer i;
always @ (N)
begin z = 16’d1;
i = 0;
while (i < N)
begin
i = i + 1;
z=z*i;
end
end
endmodule
Dept.of ECE, RIT, Hassan Prepared by: Mr. Ravi L.S Page 10
Module-5 DIGITAL SYSTEM DESIGN USING VERILOG (BEC302)
Structural Descriptions
Highlights of Structural Description
Structural description is best implemented when the digital logic of the details of hardware
components of the system are known. An example of such a system is a 2x1 multiplexer. The
components of the system are known: AND, OR, and NOT gates. Structural description can
easily de scribe these components.
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.
All statements in structural description are concurrent. At any simulation time, all
statements that have an event are executed concurrently.
In Verilog structural description by primitive gates such as AND, OR, XOR, NOT,
and XNOR gates.
Dept.of ECE, RIT, Hassan Prepared by: Mr. Ravi L.S Page 11
Module-5 DIGITAL SYSTEM DESIGN USING VERILOG (BEC302)
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. Figure shows a list of gates and their code in Verilog.
Dept.of ECE, RIT, Hassan Prepared by: Mr. Ravi L.S Page 12
Module-5 DIGITAL SYSTEM DESIGN USING VERILOG (BEC302)
Dept.of ECE, RIT, Hassan Prepared by: Mr. Ravi L.S Page 13