Skip to content

Commit 5582d39

Browse files
committed
add UVM intro
1 parent e9d8868 commit 5582d39

File tree

9 files changed

+195
-0
lines changed

9 files changed

+195
-0
lines changed

UVMTutorial.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,15 @@
33

44
# UVM TestBench 和 SystemVerilog TestBench 对比
55
![UVM](./code/uvm-tb.gif)
6+
* [DUT](./code/uvm-tb/dut_wrapper.sv)
7+
- 被测试电路,接口由interface定义
8+
* [Interface](./code/uvm_tb/dut_if.sv)
9+
- 接口定义,所有组件和DUT交流,都通过此interface
10+
* [Environment](./code/uvm_tb/my_pkg.sv)
11+
- 测试的环境组件,会在其中创建所有相关组件,包括:Driver, Monitor, Scoreboard等
12+
* [Test](./code/uvm_tb/test_pkg.sv)
13+
- 配置测试,此组件会实现各种测试的不同配置
14+
* [Top](./code/uvm_tb/tb_top.sv)
15+
- top level module, 启动测试
616

17+
![TB](./code/testbenchComponent.png)

code/uvm-tb/Makefile

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
TARGET=tb_top.sv
2+
SCRIPT=compile.do
3+
4+
all : $(TARGET) $(SCRIPT)
5+
vsim -do $(SCRIPT)
6+
7+
.PHONY: clean
8+
9+
clean:
10+
rm -f transcript
11+
rm -rf work

code/uvm-tb/compile.do

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
set UVM_DPI_HOME C:/modeltech64_10.1c/uvm-1.1/win64
2+
vlib work
3+
vlog -L mtiAvm -L mtiOvm -L mtiUvm -L mtiUPF timescale.v dut_if.sv dut_wrapper.sv my_pkg.sv test_pkg.sv tb_top.sv
4+
vsim -c -novopt -sv_lib $UVM_DPI_HOME/uvm_dpi work.top
5+
run

code/uvm-tb/dut_if.sv

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
`include "timescale.v"
2+
3+
interface dut_if (input clk);
4+
logic rstn;
5+
logic [7:0] wdata;
6+
logic [7:0] rdata;
7+
logic [7:0] addr;
8+
logic wr;
9+
logic en;
10+
endinterface

code/uvm-tb/dut_wrapper.sv

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
`include "timescale.v"
2+
3+
module dut (
4+
input clk, // Clock at some freq
5+
input rstn, // Active Low Sync Reset
6+
input wr, // Active High Write
7+
input en, // Module Enable
8+
input wdata, // Write Data
9+
input addr, // Address
10+
11+
output rdata // Read Data
12+
);
13+
// Empty module
14+
endmodule
15+
16+
module dut_wrapper (dut_if _if);
17+
// Instantiate the design module and connect interface signals to DUT
18+
dut dsn0 ( .clk (_if.clk),
19+
.rstn (_if.rstn),
20+
.wr (_if.wr),
21+
.en (_if.en),
22+
.wdata (_if.wdata),
23+
.addr (_if.addr),
24+
.rdata (_if.rdata));
25+
endmodule

code/uvm-tb/my_pkg.sv

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
`include "uvm_macros.svh"
2+
3+
package my_pkg;
4+
// If you don't use this, it'll complain that it doesn't recognize uvm components
5+
import uvm_pkg::*;
6+
7+
//---------------------------------------------------------------------------------------------------------------------
8+
// my_env
9+
//---------------------------------------------------------------------------------------------------------------------
10+
class my_env extends uvm_env ;
11+
`uvm_component_utils (my_env)
12+
13+
function new (string name, uvm_component parent);
14+
super.new (name, parent);
15+
endfunction : new
16+
17+
function void build_phase (uvm_phase phase);
18+
super.build_phase (phase);
19+
endfunction : build_phase
20+
21+
task run_phase (uvm_phase phase);
22+
set_report_verbosity_level (UVM_MEDIUM);
23+
uvm_report_info (get_name(), $sformatf ("Hello UVM ! Simulation has started."), UVM_MEDIUM, `__FILE__, `__LINE__);
24+
`uvm_info (get_name(), $sformatf("Finishing up with run_phase ... "), UVM_LOW)
25+
endtask : run_phase
26+
endclass : my_env
27+
endpackage

code/uvm-tb/tb_top.sv

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
`include "timescale.v"
2+
3+
module top;
4+
import uvm_pkg::*;
5+
import test_pkg::*;
6+
7+
//-----------------------------------------------------------------------------
8+
// Clock Generation Module; Flips every 10ns => Freq = 50 MHz
9+
//-----------------------------------------------------------------------------
10+
bit clk;
11+
always #10 clk <= ~clk;
12+
13+
//-----------------------------------------------------------------------------
14+
// Instantiate the Interface and pass it to Design Wrapper
15+
//-----------------------------------------------------------------------------
16+
dut_if dut_if1 (clk);
17+
dut_wrapper dut_wr0 (._if (dut_if1));
18+
19+
//-----------------------------------------------------------------------------
20+
// At start of simulation, set the interface handle as a config object in UVM
21+
// database. This IF handle can be retrieved in the test using the get() method
22+
// run_test () accepts the test name as argument. In this case, base_test will
23+
// be run for simulation
24+
//-----------------------------------------------------------------------------
25+
initial begin
26+
uvm_config_db #(virtual dut_if)::set (null, "uvm_test_top", "dut_if", dut_if1);
27+
run_test ("base_test");
28+
end
29+
endmodule

code/uvm-tb/test_pkg.sv

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
`include "uvm_macros.svh"
2+
3+
package test_pkg;
4+
import uvm_pkg::*;
5+
import my_pkg::*;
6+
7+
//---------------------------------------------------------------------------------------------------------------------
8+
// base_test {{{1
9+
//---------------------------------------------------------------------------------------------------------------------
10+
11+
class base_test extends uvm_test;
12+
13+
`uvm_component_utils (base_test)
14+
15+
my_env m_top_env;
16+
virtual dut_if dut_vi;
17+
18+
function new (string name, uvm_component parent = null);
19+
super.new (name, parent);
20+
endfunction : new
21+
22+
virtual function void build_phase (uvm_phase phase);
23+
super.build_phase (phase);
24+
25+
// Instead of using new to creat my_env, type_id::create is the preferred way because it uses factory methods
26+
m_top_env = my_env::type_id::create ("m_top_env", this);
27+
28+
// setting and retrieving variable values in UVM
29+
// get the dut_if object form the database
30+
if (! uvm_config_db #(virtual dut_if) :: get (this, "", "dut_if", dut_vi)) begin
31+
`uvm_error (get_type_name (), "DUT Interface not found !")
32+
end
33+
else
34+
`uvm_info (get_type_name (), "DUT Interface is found !", UVM_LOW)
35+
endfunction : build_phase
36+
37+
virtual function void end_of_elaboration_phase (uvm_phase phase);
38+
`uvm_info (get_type_name (), "end_of_elaboration_phase", UVM_LOW)
39+
uvm_top.print_topology ();
40+
endfunction
41+
42+
function void start_of_simulation_phase (uvm_phase phase);
43+
`uvm_info (get_type_name (), "start_of_simulation_phase", UVM_LOW)
44+
super.start_of_simulation_phase (phase);
45+
endfunction
46+
47+
endclass
48+
49+
//---------------------------------------------------------------------------------------------------------------------
50+
// reg_test {{{1
51+
//---------------------------------------------------------------------------------------------------------------------
52+
53+
class reg_test extends base_test;
54+
`uvm_component_utils (reg_test)
55+
56+
function new (string name, uvm_component parent = null);
57+
super.new (name, parent);
58+
endfunction
59+
60+
// Enter your test for register access here
61+
endclass
62+
63+
//---------------------------------------------------------------------------------------------------------------------
64+
// feature_test {{{1
65+
//---------------------------------------------------------------------------------------------------------------------
66+
67+
class feature_test extends base_test;
68+
`uvm_component_utils (feature_test)
69+
70+
function new (string name, uvm_component parent = null);
71+
super.new (name, parent);
72+
endfunction
73+
// Enter test code for feature here
74+
endclass
75+
76+
endpackage : test_pkg

code/uvm-tb/timescale.v

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
`timescale 1ns/1ps

0 commit comments

Comments
 (0)