文章目录
概要
UVM验证方法学中,掌握面向对象编程(OOP)的三大核心概念——类(Class)、继承(Inheritance)和多态(Polymorphism)是构建高效验证环境的基础。以下是结合UVM应用场景的详解:
一、类(Class)
1、概念
类是对象的抽象模板,定义属性(数据成员)和方法(函数成员)
对象是类的实例,通过new()构造函数创建
2、UVM 中的类特点
继承自uvm_object或uvm_component
支持工厂模式(Factory)创建对象
内置自动化机制:字段自动化(Field Automation)、事务记录等
3、代码示例
// UVM基类定义
class my_transaction extends uvm_sequence_item;
// 属性
rand bit [31:0] addr;
rand bit [31:0] data;
bit wr_rd; // 1=写, 0=读
// 约束
constraint c_addr_range { addr < 1024; }
// 注册到UVM工厂
`uvm_object_utils_begin(my_transaction)
`uvm_field_int(addr, UVM_ALL_ON)
`uvm_field_int(data, UVM_ALL_ON)
`uvm_field_int(wr_rd, UVM_ALL_ON)
`uvm_object_utils_end
// 构造函数
function new(string name = "my_transaction");
super.new(name);
endfunction
// 自定义方法
virtual function void print_transaction();
`uvm_info("TRANS", $sformatf("Addr: 0x%0h, Data: 0x%0h, Wr/Rd: %b",
addr, data, wr_rd), UVM_MEDIUM)
endfunction
endclass
4、UVM中的应用
组件封装:UVM中uvm_component基类定义了验证平台组件的生命周期(如build_phase),用户通过继承该类实现Driver/Monitor等具体组件 。
事务级建模:测试用例中的事务(Transaction)均通过自定义类封装数据字段和约束条件 。
二、继承(Inheritance)
1、概念
子类(派生类)继承父类(基类)的属性和方法
实现代码复用和层次化组织
通过extends关键字实现
2、UVM 中的继承应用
构建验证组件层次结构(如uvm_driver → my_driver)
扩展基类功能而不修改原有代码
支持方法重写(Override)
3、代码示例
// 继承示例:扩展事务类
class extended_transaction extends my_transaction;
rand bit [7:0] id; // 新增属性
rand bit error; // 新增属性
// 注册到工厂
`uvm_object_utils_begin(extended_transaction)
`uvm_field_int(id, UVM_ALL_ON)
`uvm_field_int(error, UVM_ALL_ON)
`uvm_field_parent(my_transaction)
`uvm_object_utils_end
// 构造函数
function new(string name = "extended_transaction");
super.new(name);
endfunction
// 重写父类方法
virtual function void print_transaction();
super.print_transaction(); // 调用父类方法
`uvm_info("TRANS", $sformatf("ID: 0x%0h, Error: %b", id, error), UVM_MEDIUM)
endfunction
endclass
// 继承示例:创建driver组件
class my_driver extends uvm_driver #(my_transaction);
`uvm_component_utils(my_driver)
function new(string name, uvm_component parent);
super.new(name, parent);
endfunction
// 重写run_phase
virtual task run_phase(uvm_phase phase);
my_transaction tx;
forever begin
seq_item_port.get_next_item(tx);
drive_transaction(tx);
seq_item_port.item_done();
end
endtask
virtual task drive_transaction(my_transaction tx);
// 驱动事务到DUT
`uvm_info("DRIVER", "Driving transaction to DUT", UVM_LOW)
endtask
endclass
4、UVM中的应用
组件层次化:UVM的uvm_test->uvm_env->uvm_agent继承链,形成自顶向下的验证架构 。
工厂机制扩展:通过继承uvm_object_registry并重写create方法,实现动态对象实例化替换 。
三、多态(Polymorphism)
1、概念
父类指针可以指向子类对象
通过虚函数(virtual)实现运行时方法绑定
实现接口统一,行为多样
2、UVM 中的多态应用
在基类中定义虚方法,在子类中重写
通过工厂模式动态创建不同子类对象
实现统一的测试序列接口
3、代码示例
// 多态示例:基类和子类
class shape;
virtual function void draw();
$display("Drawing a generic shape");
endfunction
endclass
class circle extends shape;
virtual function void draw();
$display("Drawing a circle");
endfunction
endclass
class square extends shape;
virtual function void draw();
$display("Drawing a square");
endfunction
endclass
// 多态测试
module test;
shape s;
circle c;
square sq;
initial begin
// 父类指针指向不同子类对象
c = new();
sq = new();
s = c;
s.draw(); // 输出:Drawing a circle
s = sq;
s.draw(); // 输出:Drawing a square
end
endmodule
// UVM中的多态应用:序列基类和子类
class base_sequence extends uvm_sequence #(my_transaction);
`uvm_object_utils(base_sequence)
function new(string name = "base_sequence");
super.new(name);
endfunction
// 虚任务
virtual task body();
my_transaction tx;
`uvm_do(tx)
endtask
endclass
class extended_sequence extends base_sequence;
`uvm_object_utils(extended_sequence)
function new(string name = "extended_sequence");
super.new(name);
endfunction
// 重写body任务
virtual task body();
extended_transaction ext_tx;
// 创建扩展事务
`uvm_do(ext_tx)
// 额外操作
ext_tx.error = 1;
`uvm_do(ext_tx)
endtask
endclass
4、UVM中的应用
-
虚方法重写:在Scoreboard中重写check_phase实现自定义校验逻辑 。
-
TLM接口适配:通过虚接口(Virtual Interface)实现不同协议驱动器的灵活替换 。
-
Sequence动态调度:基类uvm_sequence定义标准接口,子类通过多态实现不同激励生成策略 。
四、三大概念的协同应用
在 UVM 验证环境中,类、继承和多态通常结合使用:
// UVM环境示例
class my_env extends uvm_env;
my_agent agent;
my_scoreboard scoreboard;
my_coverage coverage;
`uvm_component_utils(my_env)
function new(string name, uvm_component parent);
super.new(name, parent);
endfunction
// 构建环境
virtual function void build_phase(uvm_phase phase);
super.build_phase(phase);
// 通过工厂创建组件(支持多态)
agent = my_agent::type_id::create("agent", this);
scoreboard = my_scoreboard::type_id::create("scoreboard", this);
coverage = my_coverage::type_id::create("coverage", this);
endfunction
// 连接组件
virtual function void connect_phase(uvm_phase phase);
agent.monitor.ap.connect(scoreboard.analysis_export);
agent.monitor.ap.connect(coverage.analysis_export);
endfunction
endclass
// 测试基类
class base_test extends uvm_test;
my_env env;
`uvm_component_utils(base_test)
function new(string name, uvm_component parent);
super.new(name, parent);
endfunction
virtual function void build_phase(uvm_phase phase);
super.build_phase(phase);
env = my_env::type_id::create("env", this);
endfunction
virtual task run_phase(uvm_phase phase);
phase.raise_objection(this);
// 启动默认序列(多态)
base_sequence seq;
`uvm_create(seq)
seq.start(env.agent.sequencer);
phase.drop_objection(this);
endtask
endclass
// 扩展测试类
class extended_test extends base_test;
`uvm_component_utils(extended_test)
function new(string name, uvm_component parent);
super.new(name, parent);
endfunction
// 重写run_phase
virtual task run_phase(uvm_phase phase);
phase.raise_objection(this);
// 启动扩展序列(多态)
extended_sequence seq;
`uvm_create(seq)
seq.start(env.agent.sequencer);
phase.drop_objection(this);
endtask
endclass