Laboratory Exercise No.2
VERILOG HDL MODELING
1. Objective(s):
The activity aims to let the students create a Verilog code indifferent level of abstractions. It also aims to let the students realize combinational circuits in HDL.
2. Intended Learning Outcomes (ILOs):
The students shall be able to:
1. Create a Verilog code using different modeling.
2. Simulate the operation of combinational circuit using Verilog.
3. Create appropriate Test Bench for each Verilog code.
3. Discussion:
Modeling Styles
  • Gate – level modeling – using instantiations of predefined and user – defined primitive gates.
  • Dataflow modeling – using continuous assignment statements with keyword assign.
  • Behavioral modeling – using procedural assignment statements with the keyword always.
Gate – Level Modeling. It is the level of abstraction in Verilog which uses instantiations of predefined and user – defined primitive gates describing the available built-in primitive gates and how these can be used to describe hardware. Verilog provides primitive gates and transistors. Some of the more important Verilog primitives and their logical representations are basic logic gates and, nand, or, nor, xor, xnor. These gates can be used with one output and any number of inputs. The other two structures are not and buf. These gates can be used with one input and any number of outputs.
Another group of primitives are three-state (tri-state is also used to refer to these structures) gates. These primitives are bufif1, notif1, bufif0, and notif0.
Also we have another group which are NMOS, PMOS and CMOS structures. These are switches that are used in switch level description of gates, complex gates, and busses. The nmos (pmos) primitive is a simple switch with an active high (low) control input. The cmos switch is usually used with two complementary control inputs. These switches behave like the three state gates. They are different in their output voltage levels and drive strengths.
Data – Flow Modeling. It is the level of abstraction in Verilog using continuous assignment statements with keyword assign. At a higher level than gates and transistors, a combinational circuit may be in data – flow modeling by describing the Boolean, logical, and arithmetic expressions. For this purpose the Verilog concurrent assign statement is used. Effectively, this assign statement is like driving output with the inputs. The difference is that, the use of an assign statement gives us more flexibility and allows the use of more complex functions than what is available as primitive gates. Instead of being limited to the gates in gate – level modeling.
Behavioral Modeling. It is the level of abstraction in Verilog using always or initial block. It describes the functionality of a system. It is also described by specifying a set of concurrent active procedural block. A module may contain multiple always statements and multiple initial statements. Each statement starts a separate control flow and starts execution at time 0.
An initial statement executes only once and begins its execution at start of simulation which is at time 0.
Syntax :initial
[timing_control] procedural_statement
An always statement executesrepeatedly and also begins its execution at start of simulation which is at time 0.
Syntax :always
[timing_control] procedural_statement
A block statement provides a mechanism to group two or more statements to act syntactically like a single statement. There are two kinds of blocks in Verilog HDL. These are :
◦Sequential block ( begin…end ) : Statements are executed sequentially in the given order.
◦Parallel block ( fork … join ) : Statements in this block execute concurrently.
4. Resources:
Computers with Verilog Simulator
5. Procedure:
4 – 1 Multiplexer (Gate Level)
  1. Using notepad, write the following code for the figure below:

module Mux4_1(x,y,a,b,c,d,m);
input x,y,a,b,c,d;
output m;
not n1(net1,x);
not n2(net2,y);
and a1(net3,net1,net2,d);
and a2(net4,net1,y,c);
and a3(net5,x,net2,b);
and a4(net6,x,y,a);
or o1(m,net3,net4,net5,net6);
endmodule
module TestBench;
reg x,y,a,b,c,d;
wire m;
initial
begin
$display("x y d c b a m");
a = 1'b0; b = 1'b0; c = 1'b0; d = 1'b0; x = 1'b0; y = 1'b0;
#64 $finish;
end
always #32 x = ~x;
always #16 y = ~y;
always #8 a = ~a;
always #4 b = ~b;
always #2 c = ~c;
always #1 d = ~d;
Mux4_1 U1(x,y,a,b,c,d,m);
initial
$monitor("%b %b %b %b %b %b %b",x,y,a,b,c,d,m);
endmodule
  1. Save the code with MUX4_1 as filenamein desktop and convert it to a v filewith MUX4_1.v as file name.
  2. Save a copy of MUX4_1.v file to the bin of iverilog folder in drive C.
  3. Using the CMD compile your code. Make sure to enter in bin iverilog before compiling it. Type cd c:\iverilog\bin and hit enter key.
  4. Type iverilog –o mux4_1 MUX4_1.v If there’s no error message displayed in the screen that means your code is compiled correctly.
  5. Type vvp mux4_1 . What happened? ______
Write the output in data and result.
2 – 4 DECODER (Register Transfer Level)
  1. Using notepad, write the following code for the figure below:

2-4 Decoder
module Dec_2_4(A0,A1,D0,D1,D2,D3);
input A0,A1;
output D0,D1,D2,D3;
assign {D3, D2, D1, D0} =
( {A0, A1} == 2'b00 ) ? 4'b0001 :
( {A0, A1} == 2'b01 ) ? 4'b0010 :
( {A0, A1} == 2'b10 ) ? 4'b0100 :
( {A0, A1} == 2'b11 ) ? 4'b1000 :
4'b0000;
endmodule
module TestBench;
reg A0,A1;
wire D0,D1,D2,D3;
initial
begin
$display("A0 A1 D3 D2 D1 D0");
A0 = 1'b0; A1 = 1'b0;
#4 $finish;
end
always #2 A0 = ~A0;
always #1 A1 = ~A1;
Dec_2_4 U1(A0,A1,D0,D1,D2,D3);
initial
$monitor("%b %b %b %b %b %b",A0,A1,D3,D2,D1,D0);
endmodule
  1. Save the code with DEC2_4 as filenamein desktop and convert it to a v filewith DEC2_4.v as file name.
  2. Save a copy of DEC2_4.v file to the bin of iverilog folder in drive C.
  3. Using the CMD compile your code. Make sure to enter in bin iverilog before compiling it. Type cd c:\iverilog\bin and hit enter key.
  4. Type iverilog –o dec2_4 DEC2_4.v If there’s no error message displayed in the screen that means your code is compiled correctly.
  5. Type vvp dec2_4 . What happened? ______
Write the output in data and result.
FULL ADDER (Behavioral Level)
  1. Using notepad, write the following code for the figure below:

Full Adder
module fulladder(a,b,c, sum, carry);
input a,b,c;
output sum,carry;
reg sum,carry;
always @(a or b or c)
begin
case ({a,b,c})
3'b000:begin
sum = 1'b0;
carry = 1'b0;
end
3'b001:begin
sum = 1'b1;
carry = 1'b0;
end
3'b010:begin
sum = 1'b1;
carry = 1'b0;
end
3'b011:begin
sum = 1'b0;
carry = 1'b1;
end
3'b100:begin
sum = 1'b1;
carry = 1'b0;
end
3'b101:begin
sum = 1'b0;
carry = 1'b1;
end
3'b110:begin
sum = 1'b0;
carry = 1'b1;
end
3'b111:begin
sum = 1'b1;
carry = 1'b1;
end
default: begin
sum = 1'b0;
carry = 1'b0;
end
endcase
end
endmodule
module TestBench;
reg a,b,c;
wire sum,carry;
initial
begin
$display(" a b c sum carry");
a = 1'b0; b = 1'b0; c = 1'b0;
#8 $finish;
end
always #4 a = ~a;
always #2 b = ~b;
always #1 c = ~c;
fulladder U1(a,b,c, sum, carry);
initial
$monitor(" %b %b %b %b %b",a,b,c,sum,carry);
endmodule
  1. Save the code with FULL_ADDER as filename in desktop and convert it to a v filewith FULL_ADDER.v as file name.
  2. Save a copy of FULL_ADDER.v file to the bin of iverilog folder in drive C.
  3. Using the CMD compile your code. Make sure to enter in bin iverilog before compiling it. Type cd c:\iverilog\bin and hit enter key.
  4. Type iverilog –o fulladderFULL_ADDER.v If there’s no error message displayed in the screen that means your code is compiled correctly.
  5. Type vvpfulladder . What happened? ______
Write the output in data and result.
Course: / Experiment No.:
Group No.: / Section:
Group Members: / Date Performed:
Date Submitted:
Instructor:
6. Data and Results:
DATA AND RESULT:
x / y / a / b / c / d / m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
Table 2.1 4-1 Multiplexer
A0 / A1 / D3 / D2 / D1 / D0
Table 2.2 2-4 Decoder
a / b / c / Sum / Carry
Table 2.3 3-Bit Full Adder
Supplemental Activity :
  1. Create a Verilog code with test bench for the following:
a. 8 – 1 Multiplexer – Gate Level
b. 1 – 4 Demultiplexer – Gate Level
c. 8 – 3 Encoder – Register Transfer Level
d. 3 – 8 Decoder – Register Transfer Level
e. Half – Adder –Behavioral Level
f. Full Subtracter – Behavioral Level
g. 3-bit Comparator – (Gate Level, Register Transfer Level and Behavioral Level)
7. Conclusion:
______
8. Assessment (Rubric for Laboratory Performance):
AREA / BEGINNER
1 / AVERAGE
2 / EXPERT
3 / EXEMPLARY
4 / RATING
  1. Manipulative Skills
/ Did not demonstrated needed skills / Demonstrated some of the needed skills / Demonstrated most of the needed skills / Demonstrated all of the needed skills
  1. Presentation
/ No presented data and result / Presented some of the data and result / Presented most of data and result / Presented all of data and result
  1. Documentation
/ No answer to the question / Answered some of the questions / Answered most of the questions / Answered all of the questions
  1. Time Management
/ Wasn’t able to finish any of the procedures / Finish some of the procedures / Finish most of the procedures / Finish all of the procedures
  1. Neatness
/ Laboratory report is not neat / Laboratory report is slightly neat / Laboratory report is neat / Laboratory report is neat and with creativity
  1. Completeness
/ No accomplished Laboratory report / Incomplete and incorrect laboratory report / Incomplete but correct Laboratory report / Complete and correct Laboratory report
TOTAL SCORE

Computation =

Evaluated By:Date: