Professional Documents
Culture Documents
Group Member :
1/ Nguyn B Quc Huy
2/ Nguyn L Khanh
3/ L Quc Huy
4/ Dng Vn oan
5/ Nguyn Tn Ti
6/ Nguyn Trung Nhn
MSSV : B1306150
MSSV : B1306158
MSSV : B1306149
MSSV : B1306135
MSSV : B1306195
MSSV : B1306179
PAGE 1
TABLE OF CONTENT
PAGE 2
Objectives
After completing these labs, you will be able to:
Write RTL descriptions for simple circuits
Create a structural Verilog description for simple circuits
Build hierarchy by using Verilog
Create a Verilog testbench to verify the hierarchical structure created in
the
previous steps
Use the simulation software
Create basic input stimulus
Run a simulation
Labs Outline
Lab 1: Building Hierarchy
Lab 2: Simulation/Verification
Lab 3: Memory
Lab 4: n-bit Binary Counter
Lab 5: Comparator
Lab 6: Arithmetic Logic Unit (ALU)
PAGE 3
This lab comprises three primary steps: You will create a software
project; write RTL descriptions; and check the syntax of your RTL code.
PAGE 4
Verilog Code:
PAGE 5
Synthesis output:
PAGE 6
Lab 2: Simulation/Verification
Request :
In this lab, you will write a Verilog testbench for the AND_OR module
completed in the previous exercise. As part of the testbench, you will
create a simple input stimulus by using both concurrent and sequential
statements.
Examine the circuit below (Figure 2). In this lab, you will write a
complete Verilog testbench description for the module AND_OR.
This lab comprises four primary steps: You will create a new project and
import Verilog source files; create a testbench with the Verilog testbench
wizard in the simulation software; create initial and always input stimulus
statements; and, finally, verify the logic structure and functionality by
running a simulation and examining the resulting waveforms.
PAGE 7
Verilog code:
module AND_OR_TB();
reg [3:0] inp_tb;
wire out_view;
initial
#10
#10
#10
#10
end
begin
inp_tb
inp_tb
inp_tb
inp_tb
=
=
=
=
4'b0000;
4'b1111;
4'b0011;
4'b0000;
always begin
#10 inp_tb = inp_tb + 1;
end
initial begin
$monitor("INP = %b , OUT = %d " ,
inp_tb,out_view);
#300 $stop;
end
AND_OR U_test( inp_tb , out_view);
endmodule
PAGE 8
Simulation Result:
PAGE 9
Waveforms:
PAGE 10
PAGE 11
Verilog Code:
//
This is a ROM module by using a
one-dimensional array
//
and a Verilog reg data type and a
case statement.
//File
: rom_case.v
//Coder
: Nhom 5
module rom_case(
address
,
read_en
,
clk
,
data
);
input [3:0] address;
input read_en;
input clk;
output [7:0] data;
reg [7:0] data;
always @(posedge clk, read_en) begin
if (read_en == 0) data = 0'bzzzzzzzz;
else
case (address)
0: data = 8'b1111_1110;
1: data = 8'b1111_1101;
2: data = 8'b1111_1011;
3: data = 8'b1111_0111;
4: data = 8'b1110_1111;
5: data = 8'b1101_1111;
6: data = 8'b1011_1111;
7: data = 8'b0111_1111;
8: data = 8'b0000_1000;
9: data = 8'b0000_1001;
default: data = 8'bzzzz_zzzz;
endcase
end
endmodule
PAGE 12
Testbench:
module rom_case_tb();
reg [3:0] address_tb;
reg clk, read_en_tb;
wire [7:0] data_tb;
initial begin
$monitor("Address = %b , read_en
= %d , data = %b",
address_tb,
read_en_tb,
data_tb
);
#200 $stop
;
end
initial begin
clk = 0;
read_en_tb = 0;
#20 read_en_tb = 1;
#10 address_tb = 8;
#10 address_tb = 0;
repeat (16) #10 address_tb =
address_tb + 1;
end
always #5 clk = !clk;
rom_case ROM_version1 (
.address (address_tb)
.clk
(clk)
.read_en (read_en_tb)
.data
(data_tb)
);
endmodule
,
,
,
PAGE 13
Simulation result:
PAGE 14
Waveforms
HDL PROJECT REPORT
PAGE 15
Lab 4 Counter
Request :
In this lab, you will write a complete RTL description for the module
CNTR by using parameter statements to specify the bit width. This is an
n-bit binary, up/down, loadable counter, with active-Low asynchronous
reset. You will then build a Verilog HDL testbench to verify the
functionality of the RTL code as well as the hardware it models.
Examine the circuit below (Figure 3). In this exercise, you will create a
fully functional binary counter that can be dynamically scaled to any
length. The use of parameter statements is an important tool for module
reuse and source code readability. The circuit is an n-bit binary, up/down
loadable counter, with active- Low asynchronous reset.
This lab comprises three primary steps: You will create a software project; declare
the parameter statements; and, finally, create a testbench to verify the design.
Create the input stimulus:
1. Set the CLOCK input to toggle at a rate of 100 MHz
2. Assert the RESET input at time 15 ns, hold for 25 ns, then de-assert
3. Set the CE input initially High, de-assert (set Low) at time 300, hold for 100 ns,
reassert
4. Set the LOAD input initially Low, toggle High at time 500 ns, for one full clock
cycle
5. Set UPDN to initially High, then Low at time 750 ns
7. Set the D_IN input value to 8h0F or 8b00001111
HDL PROJECT REPORT
PAGE 16
Verilog Code:
// This is an n-bit binary, up/down, loadable
counter,
// with active-low asynchronous reset
// Coder: Nhom 5
module CNTR(
d_in,
ce,
load,
updn,
clk,
rst,
q_out
);
parameter n = 4;
input
input
input
input
input
input
[n-1:0] d_in;
ce;
load;
updn;
clk;
rst;
PAGE 17
Testbench
// This is an testbench for 8-bit binary, up/down, loadable
counter,
// with active-low asynchronous reset
// Coder: Nhom 5
module CNTR_TB ();
reg [7:0] d_in_tb;
reg clk_tb;
reg ce_tb;
reg load_tb;
reg rst_tb;
reg updn_tb;
wire [7:0] q_out_tb;
always #10 clk_tb = !clk_tb;
initial fork
clk_tb = 0;
begin
time 15 ns
#15 rst_tb = 0;
assert
#25 rst_tb = 1;
end
ce_tb = 1;
begin
#300 ce_tb = 0;
#100 ce_tb = 1;
end
load_tb = 0;
begin
#500 load_tb = 1;
#20 load_tb = 0;
end
updn_tb = 1;
#750 updn_tb = 0;
d_in_tb = 8'h0F;
join
PAGE 18
PAGE 19
PAGE 20
Waveforms:
HDL PROJECT REPORT
PAGE 21
Lab 5 Comparator
Request :
In this lab, you will write description for the module COMP
(Synchronous Comparator) using an if/else statement.
Examine the circuit below (Figure 4):
This lab comprises four primary steps: You will create a software project;
create an RTL version of COMP; and, finally, create a testbench to verify
that the behavioral model functions correctly.
If the expected result and the data are equal, the result is TRUE;
otherwise, the result is FALSE.
Declarations of input and output are shown in the following table:
PAGE 22
Verilog code:
// Module name
// File
// Coder
:
:
:
comp
comp.v
Nhom 5
module comp(
expected,
data,
enable,
clk,
result);
input [3:0] expected;
input [3:0] data;
input clk;
input enable;
output result;
reg result;
initial result = 1'bz;
always @(posedge clk)
if (enable)
if (expected == data)
result = 1'b1;
else
result = 1'b0;
else result = 1'bz;
endmodule
PAGE 23
Testbench:
//Module name :
comp_tb
//
-> comparator testbench
//File name
:
comp_tb.v
//Coder
:
Nhom 5
module comp_tb();
reg [3:0] expected ;
reg clk;
reg enable;
reg [3:0] data;
wire result;
initial begin
clk = 0;
data = 0;
expected =0;
end
initial begin
#10 enable = 0;
#5 enable = 1;
#5 data
= 1;
#10 data
= 1;
expected = 1;
#5 expected = 0;
#10 data
= 5;
expected = 4;
#5 expected = 5;
end
always #5 clk = !clk;
comp compare1(
.expected (expected),
.clk
(clk),
.enable
(enable),
.data
(data),
.result
(result)
);
endmodule
PAGE 24
Waveform
PAGE 25
Use a case statement to describe the functionality for the ALU as shown
in the following table, which shows the SELECTION OPCODE and the
operation/function for each. Do not forget the ENABLE input.
PAGE 26
Verilog Code:
// Module name
// File name
// Coder
: alu
: alu.v
: Nhom 5
module alu(
a_in,
b_in,
opcode,
enable,
clk,
alu_out);
input [3:0] a_in
;
input [3:0] b_in
;
input [3:0] opcode;
input clk;
input enable;
output [3:0] alu_out;
reg [3:0] alu_out;
initial alu_out = 4'bzzzz;
always @(posedge clk)
if(enable == 1)
case(opcode)
4'b0000 : alu_out
4'b0001 : alu_out
4'b0010 : alu_out
4'b0011 : alu_out
4'b0100 : alu_out
4'b0101 : alu_out
4'b0110 : alu_out
4'b0111 : alu_out
4'b1000 : alu_out
4'b1001 : alu_out
4'b1010 : alu_out
4'b1011 : alu_out
default alu_out =
endcase
else alu_out = 4'bzzzz;
endmodule
= a_in;
= a_in +1;
= a_in + b_in;
= a_in + b_in + 1;
= a_in + (~b_in);
= a_in + (~b_in) + 1;
= a_in - 1;
= a_in && b_in;
= a_in || b_in;
= a_in ^ b_in;
= ~a_in;
= 0;
4'bzzzz;
//enable == 0
PAGE 27
Testbench:
module alu_tb();
reg [3:0] a_in;
reg [3:0] b_in;
reg [3:0] opcode;
reg clk;
reg enable;
wire [3:0] alu_out;
always #5 clk = !clk;
initial begin
clk = 0;
a_in = 4'b0011;
#10
enable = 0;
#10
b_in = 4'b1001;
#10
opcode = 4'b0000;
#10
enable = 1;
repeat (16) #10 opcode = opcode + 1;
end
initial begin
$monitor("A = %b , B = %b, OPCODE = %b ,Enable = %b,
ALU_OUT = %b ",
a_in, b_in,opcode,enable,alu_out);
#2000 $stop;
end
alu My_alu(
.a_in (a_in),
.b_in (b_in),
.opcode (opcode),
.clk (clk),
.alu_out (alu_out),
.enable (enable)
);
endmodule
PAGE 28
Simulation Result:
PAGE 29
Waveforms:
Waveform :
PAGE 30