uvm_exercise_6(不含DUT的验证环境)

📁 project_root/
├── 📁 env/                       # 验证环境目录 (对应 Makefile 中的 $(LAB_DIR)/env)
│   ├── 📁 apb_uvc/               # 【底层 IP 验证组件】完全独立,可复用于其他项目
│   │   ├── apb_interface.sv
│   │   ├── apb_transaction.sv
│   │   ├── apb_driver.sv
│   │   ├── apb_monitor.sv
│   │   ├── apb_sequencer.sv
│   │   ├── apb_agent.sv
│   │   ├── apb_environment.sv
│   │   ├── apb_sequence.sv       # 提供基础的底层硬件激励
│   │   └── apb_top.sv            # include 了上述所有 apb_uvc 文件
│   │
│   └── 📁 test_top/              # 【顶层控制组件】与具体测试场景绑定
│       ├── virtual_sequencer.sv  # 统筹各个底层 IP 的 sequencer
│       ├── virtual_sequence.sv   # 包含 base_v_sequence 和 vseq_0
│       ├── scoreboard.sv         # 接收底层 monitor 数据并进行比对
│       ├── base_test.sv          # 搭建顶层环境,完成组件连接
│       └── case_0.sv             # 具体的测试用例
│
├── 📁 sim/                       # 仿真脚本目录
│   ├── Makefile                  # 控制 VCS 编译、运行和 Verdi 波形
│   └── flist                     # 源代码文件列表 (或者直接在 Makefile 中指定顶层)
│
└── 📄 top_tb.sv                  # 【顶层硬件模块】生成时钟/复位,例化 interface,启动 UVM

一、apb_uvc文件夹

1.apb_transaction.sv

class apb_trans extends uvm_sequence_item;
    typedef enum{RD,WR} apb_dir_e;
    rand bit [31:0] addr;
    rand bit [31:0] data;
    rand apb_dir_e dir;

    `uvm_object_utils_begin(apb_trans)
        `uvm_field_int(addr, UVM_ALL_ON)
        `uvm_field_int(data, UVM_ALL_ON)
        `uvm_field_enum(apb_dir_e, dir, UVM_ALL_ON)
    `uvm_object_utils_end

    function new(string name = "apb_trans");
        super.new(name);
    endfunction

endclass

2.apb_top.sv
依赖顺序:它严格按照类之间的依赖关系,先 include 底层的数据(Transaction),再 include 组件(Driver/Monitor),最后 include 封装体(Agent/Environment)。如果顺序乱了,编译器会报错(找不到类型)。

高复用性:这个文件代表了整个 APB UVC。如果以后你要把这个 APB 验证环境复用到另一个项目,你只需要把整个 apb_uvc 文件夹拷过去,然后在新的顶层里 include 这个 apb_top.sv 即可。

不含硬件:里面没有任何 module 结构,没有时钟,也没有 DUT。全是纯软件类。

`include "apb_uvc/apb_transaction.sv"
`include "apb_uvc/apb_monitor.sv"
`include "apb_uvc/apb_sequencer.sv"
`include "apb_uvc/apb_driver.sv"
`include "apb_uvc/apb_agent.sv"
`include "apb_uvc/apb_sequence.sv"
`include "apb_uvc/apb_environment.sv"

3.apb_sequence.sv
底层的 apb_sequence.sv中的apb_sequence继承自 uvm_sequence#(apb_trans)。

顶层的virtual_sequence.sv中的 base_v_sequence类继承自 uvm_sequence(无参数)。
它们唯一的联系就是:继承自顶层 sequence 类的子类(vseq_0),在其内部调度了底层 sequence(apb_wr_seq)

virtual class apb_sequence extends uvm_sequence#(apb_trans);

    function new(string name = "apb_sequence");
        super.new(name);
    endfunction

    virtual task pre_body();
        if(starting_phase!=null) starting_phase.raise_objection(this);
        `uvm_info(get_full_name(),$sformatf("%s pre_body",get_full_name()),UVM_INFO);
    endtask

    virtual task post_body();
        if(starting_phase!=null) starting_phase.drop_objection(this);
        `uvm_info(get_full_name(),$sformatf("%s post_body",get_full_name()),UVM_INFO);
    endtask

endclass

class apb_wr_seq extends apb_sequence;

    `uvm_object_utils(apb_wr_seq)
    rand bit[31:0] s_addr;

    function new(string name = "apb_wr_seq");
        super.new(name);
    endfunction
    
    // 注意:图片在此处截断,缺少了 body() 任务和 endclass
    // 你需要在这里补充产生 transaction 的逻辑

4.apb_sequencer.sv
在 UVM 的世界里,继承(Inheritance)大于一切。
当你写下 class apb_sequencer extends uvm_sequencer#(apb_trans); 的那一刻,你的 apb_sequencer 已经免费继承了父类里几万行的核心代码:

TLM 通信机制:自带 seq_item_export 端口,能自动和 Driver 的 seq_item_port 对接。

队列管理:自带一个内部的 FIFO(先进先出队列),能自动接收 Sequence 发来的 req。

握手协议:自带与 Driver 的握手逻辑(get_next_item() 和 item_done() 就是在这套机制下运作的)。

默认仲裁算法:父类默认就是 SEQ_ARB_FIFO(先进先出)。如果同一个 sequencer 上同时跑了两个 sequence,谁先发过来,就先处理谁。

class apb_sequencer extends uvm_sequencer#(apb_trans);

    `uvm_component_utils(apb_sequencer)

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

endclass

5.apb_monitor.sv

class apb_monitor extends uvm_monitor;

    `uvm_component_utils(apb_monitor)
    virtual apb_interface vif;
    
    uvm_analysis_port#(apb_trans) apb_mon_port;

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

    virtual function void build_phase(uvm_phase phase);
        super.build_phase(phase);
        // 原图中 "virtual" 拼写为 "vortual",这里已修正
        if(!uvm_config_db#(virtual apb_interface)::get(this, "", "vif", vif)) begin
            `uvm_fatal("NOVIF/VIP/MON", "No virtual interface for apb_monitor") // 原图 "virtual" 拼写错误同理已修正
        end
        apb_mon_port = new("apb_mon_port", this);
    endfunction

    virtual task run_phase(uvm_phase phase);
        super.run_phase(phase);
        forever begin
            apb_trans tr;
            tr = new();
            @(posedge vif.clk);
            if(vif.psel==1'b1 && vif.penable == 1'b1) begin
                tr.dir = (vif.pwrite) ? apb_trans::WR : apb_trans::RD;
                tr.addr = vif.paddr;
                tr.data = (vif.pwrite) ? vif.pwdata : vif.prdata;
                apb_mon_port.write(tr);
            end
        end
    endtask

endclass

6.apb_interface.sv

interface apb_interface(input clk, input rst_n);
    logic        psel   ;
    logic        penable;
    logic        pwrite ;
    logic [31:0] paddr  ;
    logic [31:0] pwdata ;
    logic [31:0] prdata ;
endinterface

7.apb_env.sv

class apb_environment extends uvm_env;

    `uvm_component_utils(apb_environment)

    apb_agent act_agt;
    apb_agent psv_agt;

    virtual apb_interface vif;

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

    virtual function void build_phase(uvm_phase phase);
        super.build_phase(phase);

        act_agt = apb_agent::type_id::create("act_agt", this);
        act_agt.is_active = UVM_ACTIVE;

        psv_agt = apb_agent::type_id::create("psv_agt", this);
        psv_agt.is_active = UVM_PASSIVE;
    endfunction

    virtual function void connect_phase(uvm_phase phase);
        super.connect_phase(phase);
    endfunction

endclass

8.apb_driver.sv

class apb_driver extends uvm_driver#(apb_trans);

    `uvm_component_utils(apb_driver)
    virtual apb_interface vif;

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

    virtual function void build_phase(uvm_phase phase);
        super.build_phase(phase);

        if (!uvm_config_db#(virtual apb_interface)::get(this, "", "vif", vif)) begin
            `uvm_fatal("VIP/DRV/NOVIF", "No virtual interface specified for this driver instance")
        end
    endfunction

    virtual task reset_phase(uvm_phase phase);
        phase.raise_objection(this);
        @(negedge vif.rst_n);
        vif.psel    <= 0;
        vif.penable <= 0;
        vif.pwrite  <= 0;
        vif.paddr   <= 0;
        vif.pwdata  <= 0;
        phase.drop_objection(this);
    endtask

    virtual protected task run_phase(uvm_phase phase);
        super.run_phase(phase);
        forever begin
            //apb_trans tr;
            seq_item_port.get_next_item(req);
            `uvm_info(get_full_name(), $sformatf("send_req.addr=%x", req.addr), UVM_MEDIUM);
            send(req);
            seq_item_port.item_done();
        end
    endtask

    virtual protected task send(apb_trans tr);
        if(tr.dir == apb_trans::WR) begin
            vif.psel    <= 1;
            vif.pwrite  <= 1;
            vif.paddr   <= tr.addr;
            vif.pwdata  <= tr.data;
            @(posedge vif.clk);
            vif.penable <= 1;
            @(posedge vif.clk);
            vif.psel    <= 0;
            vif.penable <= 0;
        end
        else begin
            vif.psel    <= 1;
            vif.pwrite  <= 0;
            vif.paddr   <= tr.addr;
            @(posedge vif.clk);
            vif.penable <= 1;
            @(posedge vif.clk);
            tr.data     <= vif.prdata;
            vif.psel    <= 0;
            vif.penable <= 0;
        end
    endtask

endclass

9.apb_agent.sv

class apb_agent extends uvm_agent;

    apb_sequencer sqr;
    apb_driver drv;
    apb_monitor mon;

    `uvm_component_utils(apb_agent)

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

    virtual function void build_phase(uvm_phase phase);
        super.build_phase(phase);
        mon = apb_monitor::type_id::create("mon",this);
        if(is_active == UVM_ACTIVE) begin
            drv = apb_driver::type_id::create("drv", this);
            sqr = apb_sequencer::type_id::create("sqr", this);
        end
    endfunction

    virtual function void connect_phase(uvm_phase phase);
        super.connect_phase(phase);
        if(is_active == UVM_ACTIVE) begin
            drv.seq_item_port.connect(sqr.seq_item_export);
        end
    endfunction

endclass

二、test_top文件夹
1.virtual_sequence.sv
很明显这个是用来调度的,假如我写了很多底层的sequence(不同类型的数据包),如果我想要换sequence,我直接在顶层例化这个句柄,传递到需要测量的sequencer句柄上就可以了,也就是说测试用例不断更新,输入目标只有一个,我只需要换测试用例的句柄接在底层sequence句柄上就行。

class base_v_sequence extends uvm_sequence;

    `uvm_object_utils(base_v_sequence)
    `uvm_declare_p_sequencer(virtual_sequencer)

    function new(string name = "base_v_sequence");
        super.new(name);
    endfunction

    virtual task pre_body();
        if(starting_phase!=null) starting_phase.raise_objection(this);
    endtask

    virtual task post_body();
        if(starting_phase!=null) starting_phase.drop_objection(this);
    endtask

endclass

2.virtual_sequencer.sv
这个跟组件的一致。

class virtual_sequencer extends uvm_sequencer;
    apb_sequencer apb_sqr;

    `uvm_component_utils(virtual_sequencer)

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

endclass

3.top_tb.sv
这里能看到我验证的对象是APB总线,为了提高复用性,我们这里直接将APB的apb_top.sv引用进来,如果要验证其他组件,只需要也这么

import uvm_pkg::*;
`include "uvm_macros.svh"

`include "apb_uvc/apb_interface.sv"

module top_tb;

`include "apb_uvc/apb_top.sv"

`include "test_top/scoreboard.sv"
`include "test_top/virtual_sequencer.sv"
`include "test_top/base_test.sv"
`include "test_top/virtual_sequence.sv"
`include "test_top/case_0.sv"

reg clk;
reg rst_n;

apb_interface apb_if(clk, rst_n);

initial begin
    uvm_config_db#(virtual apb_interface)::set(null,"uvm_test_top.apb_env.*","vif",apb_if);
end

initial begin
    clk = 0;
    forever begin
        #10 clk = ~clk;
    end
end

initial begin
    rst_n = 1;
    #1;
    rst_n = 0;
    #100;
    rst_n = 1;
end

initial begin
    run_test();
end

endmodule

4.scoreboard.sv

`uvm_analysis_imp_decl(_act)
`uvm_analysis_imp_decl(_psv)
class scoreboard extends uvm_component;

    `uvm_component_utils(scoreboard)

    apb_trans apb_sb[$];

    uvm_analysis_imp_act #(apb_trans,scoreboard) act_imp;
    uvm_analysis_imp_psv #(apb_trans,scoreboard) psv_imp;

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

    virtual function void build_phase(uvm_phase phase);
        super.build_phase(phase);
        act_imp = new("act_imp", this);
        psv_imp = new("psv_imp", this);
    endfunction

    function void write_act(apb_trans tr);
        `uvm_info("sb", $sformatf("act_tr.addr : %x", tr.addr), UVM_INFO);
    endfunction

    function void write_psv(apb_trans tr);
        `uvm_info("sb", $sformatf("psv_tr.addr : %x", tr.addr), UVM_INFO);
    endfunction

endclass

5.case0.sv

class vseq_0 extends base_v_sequence;

    `uvm_object_utils(vseq_0)
    function new(string name = "vseq_0");
        super.new(name);
    endfunction

    apb_wr_seq              apb_wr;

    virtual task body();
        #200;
        `uvm_do_on_with(apb_wr, p_sequencer.apb_sqr, {
            apb_wr.s_addr == 1;
        });

        #200;
    endtask

endclass

6.base_test.sv

class base_test extends uvm_test;
    `uvm_component_utils(base_test)

    apb_environment  apb_env;
    virtual_sequencer v_sqr;
    scoreboard        scb;

    function new(string name = "base_test",uvm_component parent = null);
        super.new(name,parent);
    endfunction

    virtual function void build_phase(uvm_phase phase);
        super.build_phase(phase);
        apb_env = apb_environment::type_id::create("apb_env",this);
        v_sqr = virtual_sequencer::type_id::create("v_sqr",this);
        scb = scoreboard::type_id::create("scb",this);
    endfunction

    virtual function void connect_phase(uvm_phase phase);
        super.connect_phase(phase);
        v_sqr.apb_sqr = apb_env.act_agt.sqr;
        apb_env.act_agt.mon.apb_mon_port.connect(scb.act_imp);
        apb_env.psv_agt.mon.apb_mon_port.connect(scb.psv_imp);
    endfunction
endclass

三、sim仿真文件
1.Makefile

LAB_DIR = ..

all: elab run

TC = case_0

elab:
	vcs -top top_tb -full64 -sverilog -debug_access+all -timescale=1ns/1ps \
	    -ntb_opts uvm \
	    +incdir+$(LAB_DIR)/env \
	    -f ./flist \
	    -l elab.log -o simv

run:
	./simv +ntb_random_seed +UVM_TESTNAME=$(TC) -l run.log &

verdi:
	verdi -ntb_opts uvm -sverilog -f ./flist -top top_tb&

clean:
	rm -rf csrc elab.log novas* run.log simv simv.daidir ucli.key vc_hdrs.h *.fsdb DVEfiles inter.vpd verdiLog

2.flist

../env/test_top/top_tb.sv

posted on 2026-11-12 21:10  tuck0603  阅读(8)  评论(0)    收藏  举报

导航