Agent实现

定义Agent三个组件的类
// 子组件 A
class comp_a extends uvm_component;
    `uvm_component_utils(comp_a)
    function new(string name, uvm_component parent);
        super.new(name, parent);
    endfunction
    // ... 其他 phase
endclass

// 子组件 B
class comp_b extends uvm_component;
    `uvm_component_utils(comp_b)
    function new(string name, uvm_component parent);
        super.new(name, parent);
    endfunction
endclass

// 子组件 C
class comp_c extends uvm_component;
    `uvm_component_utils(comp_c)
    function new(string name, uvm_component parent);
        super.new(name, parent);
    endfunction
endclass
三个组件都要进行注册
env中创建三个组件
class my_env extends uvm_env;
    // 1. 声明成员变量(句柄)
    comp_a a;
    comp_b b;
    comp_c c;

    `uvm_component_utils(my_env)

    // 2. 构造函数,传递 parent
    function new(string name, uvm_component parent);
        super.new(name, parent);
    endfunction

    // 3. 在 build_phase 中创建三个子组件
    virtual function void build_phase(uvm_phase phase);
        // 先调用 super,完成父类底层初始化
        super.build_phase(phase);

        // 创建子组件,第二个参数传 this,表示 env 是它们的父节点
        a = comp_a::type_id::create("a", this);
        b = comp_b::type_id::create("b", this);
        c = comp_c::type_id::create("c", this);
    endfunction
endclass
这里的env创建环境本质上是定义三个组建的句柄,并通过phase来创建对应的组件对象
在 testcase 中创建 env
class my_test extends uvm_test;
    // 1. 声明 env 句柄
    my_env env;

    `uvm_component_utils(my_test)

    function new(string name, uvm_component parent);
        super.new(name, parent);
    endfunction

    // 2. 在 build_phase 中创建 env
    virtual function void build_phase(uvm_phase phase);
        super.build_phase(phase);

        // 创建 env,this 是 test,所以 env 是 test 的子节点
        env = my_env::type_id::create("env", this);
    endfunction
endclass
这里也是创建env进行注册,并通过phase来创建env组件
最终结构
uvm_test_top          (my_test 实例)
  └── env             (my_env 实例,在 my_test 中创建)
        ├── a         (comp_a 实例,在 my_env 中创建)
        ├── b         (comp_b 实例,在 my_env 中创建)
        └── c         (comp_c 实例,在 my_env 中创建)

posted on 2026-09-13 20:17  tuck0603  阅读(10)  评论(0)    收藏  举报

导航