2.uvm_base_AI
UVM 核心知识点详解**1. uvm_component 与 uvm_objectuvm_component 和 uvm_object 是 UVM 验证平台的两大核心基类前者侧重「结构化组件」后者侧重「数据 / 配置载体」二者构成了 UVM 框架的基础。1.2 uvm_object 常用派生类uvm_object 是 UVM 中所有数据类、配置类的基类核心用于数据存储、传递与自动化管理常用派生类如下类名父类核心作用transactionuvm_sequence_item事务类封装 DUT 输入 / 输出数据如地址、数据、控制信号是数据传输的基本单元sequenceuvm_sequence序列类组织管理 sequence_item按场景产生测试激励如单次读写、突发传输configuvm_object配置类集中存储验证平台参数如时钟频率、DUT 模式通过 config_db 传递uvm_reg_itemuvm_sequence_item寄存器事务类封装寄存器读写操作的相关信息地址、数据、操作类型uvm_reg_mapuvm_object寄存器映射类管理寄存器地址映射关系提供寄存器访问接口uvm_memuvm_object存储类模拟 DUT 中的内存模块支持读写操作uvm_reg_fielduvm_object寄存器字段类封装寄存器中的单个字段如控制位、状态位uvm_reguvm_object寄存器类由多个 uvm_reg_field 组成描述单个寄存器的属性与行为uvm_reg_fileuvm_object寄存器文件类分组管理多个寄存器简化寄存器集合的访问uvm_reg_blockuvm_object寄存器块类顶层寄存器容器包含多个 uvm_reg_map 和 uvm_reg_fileuvm_phaseuvm_object阶段控制类定义验证平台的运行阶段如 build_phase、run_phase控制组件行为时序1.3 uvm_component 常用派生类uvm_component 是 UVM 中所有「结构化组件」的基类具有生命周期管理、树形层次结构核心用于实现验证平台的功能模块常用派生类如下1.3.1 uvm_driver 类核心作用向 sequencer 索要 sequence_item事务将激励数据转换为 DUT 可识别的物理信号并发送给 DUT部分场景下接收 DUT 响应。关键成员扩展自 uvm_componentuvm_seq_item_pull_port #(REQ, RSP) seq_item_port; // 向 sequencer 拉取事务的端口 uvm_seq_item_pull_port #(REQ, RSP) seq_item_prod_if; // 生产端接口兼容老版本 uvm_analysis_port #(RSP) rsp_port; // 发送响应的端口 REQ req; // 接收的请求事务模板参数需指定具体 transaction 类型 RSP rsp; // 发送的响应事务模板参数1.3.2 uvm_monitor 类核心作用监听 DUT 的接口信号采集数据并转换为 transaction 级别的 sequence_item发送给 scoreboard、reference_model 等组件用于校验。类定义无额外扩展成员仅继承 uvm_component 基础功能class uvm_monitor extends uvm_component; function new(string name, uvm_component parent); super.new(name, parent); endfunction const static string type_name uvm_monitor; // 修正原笔误uvm_monirot → uvm_monitor virtual function string get_type_name(); return type_name; endfunction endclass1.3.3 uvm_sequencer 类核心作用组织管理 sequence维护 sequence 的优先级与执行顺序当 driver 索要数据时将 sequence 产生的 sequence_item 转发给 driver是 sequence 与 driver 之间的桥梁。扩展说明支持事务仲裁、锁定、中断等高级功能默认提供 uvm_sequencer #(REQ, RSP) 模板类需指定事务类型。1.3.4 uvm_scoreboard 类核心作用接收 monitor 采集的 DUT 实际输出与 reference_model 输出的预期结果进行对比判断测试用例是否通过Pass/Fail。扩展说明无额外扩展成员用户需自定义对比逻辑如数组对比、时序对齐对比。1.3.5 reference_model 类核心作用行为级模型模仿 DUT 的功能逻辑接收 monitor 采集的输入事务计算出预期输出结果并发送给 scoreboard。扩展说明UVM 无专门定义的基类需用户手动继承 uvm_component 实现确保接口与 monitor、scoreboard 兼容。1.3.6 uvm_agent 类核心作用封装 driver、monitor 和 sequencer可选形成独立的接口代理模块提高验证平台的可重用性如 UART agent、SPI agent。关键成员与方法virtual class uvm_agent extends uvm_component; uvm_active_passive_enum is_active UVM_ACTIVE; // 模式ACTIVE含 driversequencer/ PASSIVE仅 monitor function void build_phase(uvm_phase phase); int active; super.build_phase(phase); // 修正原笔误build_active → build_phase if(get_config_int(is_active, active)) // 从 config_db 获取模式配置 is_active uvm_active_passive_enum(active); endfunction endclass1.3.7 uvm_env 类核心作用顶层组件容器封装验证平台中所有固定不变的 uvm_component如 agent、scoreboard、reference_model统一管理组件的实例化与连接简化测试用例的编写。类定义virtual class uvm_env extends uvm_component; function new(string name env, uvm_component parent null); super.new(name, parent); endfunction const static string type_name uvm_env; virtual function string get_type_name(); return type_name; endfunction endclass1.3.8 uvm_test 类核心作用测试用例的顶层入口负责实例化 uvm_env配置验证平台参数通过 config_db启动 sequence控制测试流程如终止条件。类定义class uvm_test extends uvm_component; // 修正原笔误extend → extends function new(string name, uvm_component parent); super.new(name, parent); endfunction const static string type_name uvm_test; virtual function string get_type_name(); return type_name; endfunction endclass1.4 uvm_object 相关注册宏uvm_object 及其派生类需通过以下宏注册到 UVM Factory 机制支持动态创建、类型覆盖等功能宏名适用场景uvm_object_utils(T)非参数化的 uvm_object 派生类注册如普通 config 类、transaction 类uvm_object_param_utils(T)参数化的 uvm_object 派生类注册如 uvm_sequence #(REQ)uvm_object_utils_begin(T)带成员变量自动化管理的非参数化类注册配合 uvm_field_* 宏使用uvm_object_param_utils_begin(T)带成员变量自动化管理的参数化类注册配合 uvm_field_* 宏使用uvm_object_utils_end结束 uvm_object_utils_begin 块的定义必须与 begin 成对使用1.5 uvm_component 相关注册宏uvm_component 及其派生类需通过以下宏注册到 UVM Factory 机制支持组件实例化、配置传递等功能宏名适用场景uvm_component_utils(T)非参数化的 uvm_component 派生类注册如普通 agent、scoreboarduvm_component_param_utils(T)参数化的 uvm_component 派生类注册如 uvm_driver #(REQ, RSP)uvm_component_utils_begin(T)带成员变量自动化管理的非参数化组件注册继承 uvm_object 的 compare/print 功能支持 config_db 配置uvm_component_param_utils_begin(T)带成员变量自动化管理的参数化组件注册uvm_component_utils_end结束 uvm_component_utils_begin 块的定义必须与 begin 成对使用1.6 uvm_component 与 uvm_object 的核心差异补充特性uvm_objectuvm_component父类参数无 parent 参数必须指定 parent 参数构建树形结构核心功能数据存储、传递、自动化copy/compare功能实现、组件管理、生命周期控制关键方法clone()、copy()、compare()build_phase()、run_phase()Factory 注册用 uvm_object_* 宏用 uvm_component_* 宏重要限制uvm_object 支持 clone() 方法自动分配内存并复制对象内容。uvm_component 不支持 clone() 方法因需绑定 parent 构建树形结构但可使用 copy() 方法需提前分配内存。2. UVM 的树形结构UVM 验证平台通过 uvm_component 的 parent 参数构建树形层次结构实现组件的统一管理如配置传递、生命周期控制。2.1 树形结构的核心parent 参数所有 uvm_component 实例化时必须通过构造函数指定 parent 参数类型为 uvm_component用于明确组件在树形结构中的层级关系function new(string name component_name, uvm_component parent); super.new(name, parent); // 调用父类构造函数绑定 parent endfunctionparent 为当前组件的父节点如 agent 的 parent 是 env。组件的完整路径由「父组件名 子组件名」构成如 uvm_test_top.env.uart_agent.driver。2.2 树形结构的根uvm_topUVM 树形结构的根节点是uvm_top其特性如下uvm_top 是 uvm_root 类的唯一实例uvm_root 继承自 uvm_component。uvm_top 的 parent 参数为 null无父节点。若实例化 uvm_component 时将 parent 设为 null系统会自动将其 parent 绑定为 uvm_top。uvm_top 确保整个验证平台只有一棵组件树便于全局管理。2.3 树形结构操作函数UVM 提供以下函数用于遍历和操作组件树函数名功能描述get_parent()获取当前组件的父节点get_child(string name)根据名称获取指定子组件get_children()获取当前组件的所有子组件返回队列get_first_child()获取第一个子组件get_next_child()获取下一个子组件配合 get_first_child() 遍历get_num_children()获取当前组件的子组件数量3. Field Automation字段自动化机制Field Automation 机制是 UVM 提供的便捷功能通过 uvm_field_* 系列宏自动实现 uvm_object 派生类成员变量的 copy、compare、print、pack/unpack 等操作无需手动编写冗余代码。3.1 常用 uvm_field_* 宏3.1.1 基础类型宏用于单个基础类型成员变量的自动化管理宏名适用变量类型说明uvm_field_int(ARG, FLAG)int/bit/logic 等整型自动化管理整型变量uvm_field_real(ARG, FLAG)real 浮点型自动化管理浮点型变量uvm_field_enum(T, ARG, FLAG)枚举类型T 为枚举名自动化管理枚举变量需指定枚举类型 Tuvm_field_object(ARG, FLAG)uvm_object 派生类对象自动化管理对象类型变量需提前实例化uvm_field_event(ARG, FLAG)uvm_event 事件类型自动化管理事件变量uvm_field_string(ARG, FLAG)string 字符串类型自动化管理字符串变量3.1.2 数组 / 队列 / 联合数组宏用于集合类型成员变量的自动化管理宏名适用变量类型说明uvm_field_array_int(ARG, FLAG)int 数组固定长度自动化管理整型数组uvm_field_array_enum(T, ARG, FLAG)枚举数组固定长度自动化管理枚举数组uvm_field_array_object(ARG, FLAG)uvm_object 数组固定长度自动化管理对象数组uvm_field_array_string(ARG, FLAG)string 数组固定长度自动化管理字符串数组uvm_field_sarray_int(ARG, FLAG)动态数组sarray自动化管理动态整型数组uvm_field_queue_int(ARG, FLAG)队列queue自动化管理整型队列uvm_field_queue_enum(T, ARG, FLAG)枚举队列自动化管理枚举队列uvm_field_queue_object(ARG, FLAG)对象队列自动化管理对象队列uvm_field_queue_string(ARG, FLAG)字符串队列自动化管理字符串队列uvm_field_aa_int_string(ARG, FLAG)联合数组int→string自动化管理整型到字符串的联合数组3.2 Field Automation 核心函数通过 uvm_field_* 宏注册后uvm_object 派生类会自动获得以下函数无需手动实现函数名功能描述用法示例copy(uvm_object rhs)将 rhs 对象的成员变量值复制到当前对象obj1.copy(obj2);obj1 是目标对象compare(uvm_object rhs, uvm_comparer comparernull)对比当前对象与 rhs 的成员变量返回是否一致1 一致0 不一致if(obj1.compare(obj2)) $display(“一致”);pack_bytes(ref byte unsigned bytestream[], input uvm_packer packernull)将成员变量打包为字节流byte 数组byte unsigned stream[]; obj.pack_bytes(stream);unpack_bytes(ref byte unsigned bytestream[], input uvm_packer packernull)从字节流中解包赋值给成员变量obj.unpack_bytes(stream);pack(ref bit bitstream[], input uvm_packer packernull)打包为比特流bit 数组bit stream[]; obj.pack(stream);unpack(ref bit bitstream[], input uvm_packer packernull)从比特流解包obj.unpack(stream);pack_ints(ref int unsigned intstream[], input uvm_packer packernull)打包为整型流int 数组int unsigned stream[]; obj.pack_ints(stream);unpack_ints(ref int unsigned intstream[], input uvm_packer packernull)从整型流解包obj.unpack_ints(stream);print()打印所有注册的成员变量值带层级obj.print();clone()分配内存并复制当前对象返回新对象指针uvm_object obj2 obj1.clone();3.3 Field Automation 标志位FLAGuvm_field_* 宏的 FLAG 参数用于控制自动化功能的开启 / 关闭常用标志位如下标志位功能描述UVM_ALL_ON开启所有自动化功能copy/compare/print/packUVM_PRINT仅开启打印功能UVM_COPY仅开启拷贝功能UVM_COMPARE仅开启比较功能UVM_PACK仅开启打包 / 解包功能UVM_NOPRINT关闭打印功能UVM_NOCOPY关闭拷贝功能UVM_NOCOMPARE关闭比较功能UVM_NOPACK关闭打包 / 解包功能标志位使用示例class my_transaction extends uvm_sequence_item; int dmac; // 目的 MAC 地址 int smac; // 源 MAC 地址 int ether_type; // 以太网类型 int pload[]; // 负载数据动态数组 int crc; // CRC 校验值 int crc_err; // CRC 错误标记 // 注册类并指定自动化功能FLAG 组合 uvm_object_utils_begin(my_transaction) uvm_field_int(dmac, UVM_ALL_ON) // 开启所有功能 uvm_field_int(smac, UVM_ALL_ON) // 开启所有功能 uvm_field_int(ether_type, UVM_ALL_ON) // 开启所有功能 uvm_field_array_int(pload, UVM_ALL_ON) // 数组类型开启所有功能 uvm_field_int(crc, UVM_ALL_ON) // 开启所有功能 uvm_field_int(crc_err, UVM_ALL_ON | UVM_NOPACK) // 开启所有功能但关闭打包 uvm_object_utils_end function new(string name my_transaction); super.new(name); endfunction endclass