UVM-入门实验1

1.工厂的注册、创建和覆盖机制

UVM验证顶层都必须有import uvm_pkg:: *和`include “uvm_macros.svh” 这两行代码代表着预编译的UVM库。

UVM中只有两种用例注册的宏

`uvm_component_utils(T)
`uvm_object_utils(T)

1.1 object_create类

class object_create extends top;
    trans t1, t2, t3, t4;
    `uvm_component_utils(object_create)
    function new(string name = "object_create", uvm_component parent = null);
      super.new(name, parent);
    endfunction    
    function void build_phase(uvm_phase phase);
      uvm_factory f = uvm_factory::get(); // get singleton factory
      super.build_phase(phase);
      t1 = new("t1"); // direct construction
      t2 = trans::type_id::create("t2", this); // common method
      void'($cast(t3,f.create_object_by_type(trans::get_type(), get_full_name(), "t3"))); // factory method
      void'($cast(t4,create_object("trans", "t4"))); // pre-defined method inside component
    endfunction
  endclass

仿真时执行命令:

vsim -novopt -classdebug +UVM_TESTNAME=object_create work.factory_mechanism

仿真结果:

1.2 component_create类

class component_create extends top;
    unit u1, u2, u3, u4;
    `uvm_component_utils(component_create)
    function new(string name = "component_create", uvm_component parent = null);
      super.new(name, parent);
    endfunction
    function void build_phase(uvm_phase phase);
      uvm_factory f = uvm_factory::get(); // get singleton factory
      super.build_phase(phase);
      u1 = new("u1"); // direct construction
      u2 = unit::type_id::create("u2", this); // common method
      void'($cast(u3,f.create_component_by_type(unit::get_type(), get_full_name(), "u3", this))); // factory method
      void'($cast(u4,create_component("unit", "u4"))); // pre-defined method inside component
    endfunction
  endclass

仿真时执行命令:

vsim -novopt -classdebug +UVM_TESTNAME=component_create work.factory_mechanism

仿真结果:

1.3 object_override类

class object_override extends object_create;
    `uvm_component_utils(object_override)
    function new(string name = "object_override", uvm_component parent = null);
      super.new(name, parent);
    endfunction
    function void build_phase(uvm_phase phase);
      set_type_override_by_type(trans::get_type(), bad_trans::get_type());		//bad_trans替换trans
      super.build_phase(phase);
    endfunction
  endclass

仿真时执行命令:

vsim -novopt -classdebug +UVM_TESTNAME=object_override work.factory_mechanism

仿真结果:

1.4 component_override类

class component_override extends component_create;
    `uvm_component_utils(component_override)
    function new(string name = "component_override", uvm_component parent = null);
      super.new(name, parent);
    endfunction
    function void build_phase(uvm_phase phase);
      set_type_override("unit", "big_unit");
      super.build_phase(phase);
    endfunction
  endclass

仿真时执行命令:

vsim -novopt -classdebug +UVM_TESTNAME=component_override work.factory_mechanism

仿真结果:

2. 域的自动化

typedef enum {WRITE, READ, IDLE} op_t;
 bit[31:0] addr;
 bit[31:0] data;
 op_t op;
 string name;
 `uvm_object_utils_begin(trans)//域的自动化声明
      `uvm_field_int(addr, UVM_ALL_ON)
      `uvm_field_int(data, UVM_ALL_ON)
      `uvm_field_enum(op_t, op, UVM_ALL_ON)
      `uvm_field_string(name, UVM_ALL_ON)
  `uvm_object_utils_end

比较t1和t2是否相等

	  trans t1, t2;
      bit is_equal;
      phase.raise_objection(this);
      t1 = trans::type_id::create("t1");
      t1.data = 'h1FF;
      t1.addr = 'hF100;
      t1.op = WRITE;
      t1.name = "t1";
      t2 = trans::type_id::create("t2");
      t2.data = 'h2FF;
      t2.addr = 'hF200;
      t2.op = WRITE;
      t2.name = "t2";
      is_equal = t1.compare(t2);
      uvm_default_comparer.show_max = 10;//全局控制对象
      is_equal = t1.compare(t2);

全局控制对象
uvm_default_comparer(uvm_comparer类型)是默认的UVM全局比较器,show_max可以设置最大比较结果
回调函数
执行compare()函数会自动调用do_compare()这个回调函数

function bit do_compare(uvm_object rhs, uvm_comparer comparer);
      trans t;
      do_compare = 1;
      void'($cast(t, rhs));
      if(addr != t.addr) begin
        do_compare = 0;
        `uvm_warning("CMPERR", $sformatf("addr %8x != %8x", addr, t.addr))
      end
      if(data != t.data) begin
        do_compare = 0;
        `uvm_warning("CMPERR", $sformatf("data %8x != %8x", data, t.data))
      end
      if(op != t.op) begin
        do_compare = 0;
        `uvm_warning("CMPERR", $sformatf("op %s != %8x", op, t.op))
      end
      if(addr != t.addr) begin
        do_compare = 0;
        `uvm_warning("CMPERR", $sformatf("name %8x != %8x", name, t.name))
      end
    endfunction

uvm_object::print()方法及uvm_object_copy()方法

if(!is_equal)
        `uvm_warning("CMPERR", "t1 is not equal to t2")
      else
        `uvm_info("CMPERR", "t1 is equal to t2", UVM_LOW)
        
      `uvm_info("COPY", "Before uvm_object copy() taken", UVM_LOW)
      t1.print();
      t2.print();
      `uvm_info("COPY", "After uvm_object t2 is copied to t1", UVM_LOW)
      t1.copy(t2);
      t1.print();
      t2.print();
      `uvm_info("CMP", "Compare t1 and t2", UVM_LOW)
      is_equal = t1.compare(t2);
      if(!is_equal)
        `uvm_warning("CMPERR", "t1 is not equal to t2")
      else
        `uvm_info("CMPERR", "t1 is equal to t2", UVM_LOW) 

3.phase机制

3.1 comp2类

class comp2 extends uvm_component;
    `uvm_component_utils(comp2)
    function new(string name = "comp2", uvm_component parent = null);
      super.new(name, parent);
      `uvm_info("CREATE", $sformatf("unit type [%s] created", name), UVM_LOW)
    endfunction
    function void build_phase(uvm_phase phase);
      super.build_phase(phase);
      `uvm_info("BUILD", "comp2 build phase entered", UVM_LOW)
      `uvm_info("BUILD", "comp2 build phase exited", UVM_LOW)
    endfunction
    function void connect_phase(uvm_phase phase);
      super.connect_phase(phase);
      `uvm_info("CONNECT", "comp2 connect phase entered", UVM_LOW)
      `uvm_info("CONNECT", "comp2 connect phase exited", UVM_LOW)
    endfunction
    task run_phase(uvm_phase phase);
      super.run_phase(phase);
      `uvm_info("RUN", "comp2 run phase entered", UVM_LOW)
      `uvm_info("RUN", "comp2 run phase entered", UVM_LOW)
    endtask
    function void report_phase(uvm_phase phase);
      super.report_phase(phase);
      `uvm_info("REPORT", "comp2 report phase entered", UVM_LOW)
      `uvm_info("REPORT", "comp2 report phase exited", UVM_LOW)   
    endfunction
  endclass

3.2 comp3类

class comp3 extends uvm_component;
    `uvm_component_utils(comp3)
    function new(string name = "comp3", uvm_component parent = null);
      super.new(name, parent);
      `uvm_info("CREATE", $sformatf("unit type [%s] created", name), UVM_LOW)
    endfunction
    function void build_phase(uvm_phase phase);
      super.build_phase(phase);
      `uvm_info("BUILD", "comp3 build phase entered", UVM_LOW)
      `uvm_info("BUILD", "comp3 build phase exited", UVM_LOW)
    endfunction
    function void connect_phase(uvm_phase phase);
      super.connect_phase(phase);
      `uvm_info("CONNECT", "comp3 connect phase entered", UVM_LOW)
      `uvm_info("CONNECT", "comp3 connect phase exited", UVM_LOW)
    endfunction
    task run_phase(uvm_phase phase);
      super.run_phase(phase);
      `uvm_info("RUN", "comp3 run phase entered", UVM_LOW)
      `uvm_info("RUN", "comp3 run phase entered", UVM_LOW)
    endtask
    function void report_phase(uvm_phase phase);
      super.report_phase(phase);
      `uvm_info("REPORT", "comp3 report phase entered", UVM_LOW)
      `uvm_info("REPORT", "comp3 report phase exited", UVM_LOW)   
    endfunction
  endclass

3.3 comp1类

在comp1中声明comp2、comp3作为成员变量,并且在build_phase中做例化

class comp1 extends uvm_component;
    comp2 c2;
    comp3 c3;
    `uvm_component_utils(comp1)
    function new(string name = "comp1", uvm_component parent = null);
      super.new(name, parent);
      `uvm_info("CREATE", $sformatf("unit type [%s] created", name), UVM_LOW)
    endfunction
    function void build_phase(uvm_phase phase);
      super.build_phase(phase);
      `uvm_info("BUILD", "comp1 build phase entered", UVM_LOW)
      c2 = comp2::type_id::create("c2", this);
      c3 = comp3::type_id::create("c3", this);
      `uvm_info("BUILD", "comp1 build phase exited", UVM_LOW)
    endfunction
    function void connect_phase(uvm_phase phase);
      super.connect_phase(phase);
      `uvm_info("CONNECT", "comp1 connect phase entered", UVM_LOW)
      `uvm_info("CONNECT", "comp1 connect phase exited", UVM_LOW)
    endfunction
    task run_phase(uvm_phase phase);
      super.run_phase(phase);
      `uvm_info("RUN", "comp1 run phase entered", UVM_LOW)
      `uvm_info("RUN", "comp1 run phase entered", UVM_LOW)
    endtask
    function void report_phase(uvm_phase phase);
      super.report_phase(phase);
      `uvm_info("REPORT", "comp1 report phase entered", UVM_LOW)
      `uvm_info("REPORT", "comp1 report phase exited", UVM_LOW)   
    endfunction
  endclass

3.4 phase_order_ref类

phase_order_test中包含了comp1 c1,并且在build_phase中也创建了c1

class phase_order_test extends uvm_test;
    comp1 c1;
    `uvm_component_utils(phase_order_test)
    function new(string name = "phase_order_test", uvm_component parent = null);
      super.new(name, parent);
    endfunction
    function void build_phase(uvm_phase phase);
      super.build_phase(phase);
      `uvm_info("BUILD", "phase_order_test build phase entered", UVM_LOW)
      c1 = comp1::type_id::create("c1", this);
      `uvm_info("BUILD", "phase_order_test build phase exited", UVM_LOW)
    endfunction
    function void connect_phase(uvm_phase phase);
      super.connect_phase(phase);
      `uvm_info("CONNECT", "phase_order_test connect phase entered", UVM_LOW)
      `uvm_info("CONNECT", "phase_order_test connect phase exited", UVM_LOW)
    endfunction
    task run_phase(uvm_phase phase);
      super.run_phase(phase);
      `uvm_info("RUN", "phase_order_test run phase entered", UVM_LOW)
      phase.raise_objection(this);
      #1us;
      phase.drop_objection(this);
      `uvm_info("RUN", "phase_order_test run phase exited", UVM_LOW)
    endtask
    function void report_phase(uvm_phase phase);
      super.report_phase(phase);
      `uvm_info("REPORT", "phase_order_test report phase entered", UVM_LOW)
      `uvm_info("REPORT", "phase_order_test report phase exited", UVM_LOW)    
    endfunction
    
    task reset_phase(uvm_phase phase);
      `uvm_info("RESET", "phase_order_test reset phase entered", UVM_LOW)
      phase.raise_objection(this);
      #1us;
      phase.drop_objection(this);
      `uvm_info("RESET", "phase_order_test reset phase exited", UVM_LOW)
    endtask
    
    task main_phase(uvm_phase phase);
      `uvm_info("MAIN", "phase_order_test main phase entered", UVM_LOW)
      phase.raise_objection(this);
      #1us;
      phase.drop_objection(this);
      `uvm_info("MAIN", "phase_order_test main phase exited", UVM_LOW)
    endtask 
  endclass

仿真时执行命令:

vsim -novopt -classdebug +UVM_TESTNAME=phase_order_test work.phase_order

可以看出,build_phase、connect_phase、run_phase、report_phase都是按照自顶向下或者自底向上的顺序依次执行的,只有build phase de1执行顺序是自顶向下。run phase和12个分支phase是并行执行的,需要等待run_phase执行完才可以进入下一个阶段。

4.config机制

4.1 接口传递

设置接口

interface uvm_config_if;
  logic [31:0] addr;
  logic [31:0] data;
  logic [ 1:0] op;
endinterface

设置comp2类

if(!uvm_config_db#(virtual uvm_config_if)::get(this, "", "vif", vif))
class comp2 extends uvm_component;
    int var2;
    virtual uvm_config_if vif;  
    config_obj cfg; 
    `uvm_component_utils(comp2)
    function new(string name = "comp2", uvm_component parent = null);
      super.new(name, parent);
      var2 = 200;
      `uvm_info("CREATE", $sformatf("unit type [%s] created", name), UVM_LOW)
    endfunction
    function void build_phase(uvm_phase phase);
      super.build_phase(phase);
      `uvm_info("BUILD", "comp2 build phase entered", UVM_LOW)
      if(!uvm_config_db#(virtual uvm_config_if)::get(this, "", "vif", vif))
        `uvm_error("GETVIF", "no virtual interface is assigned")
        
      `uvm_info("GETINT", $sformatf("before config get, var2 = %0d", var2), UVM_LOW)
      uvm_config_db#(int)::get(this, "", "var2", var2);
      `uvm_info("GETINT", $sformatf("after config get, var2 = %0d", var2), UVM_LOW)
      
      uvm_config_db#(config_obj)::get(this, "", "cfg", cfg);
      `uvm_info("GETOBJ", $sformatf("after config get, cfg.comp2_var = %0d", cfg.comp2_var), UVM_LOW)     
      
      `uvm_info("BUILD", "comp2 build phase exited", UVM_LOW)
    endfunction
  endclass

设置comp1类,在comp1中例化一个comp2对象

if(!uvm_config_db#(virtual uvm_config_if)::get(this, "", "vif", vif))
class comp1 extends uvm_component;
    int var1;
    comp2 c2;
    config_obj cfg; 
    virtual uvm_config_if vif;
    `uvm_component_utils(comp1)
    function new(string name = "comp1", uvm_component parent = null);
      super.new(name, parent);
      var1 = 100;
      `uvm_info("CREATE", $sformatf("unit type [%s] created", name), UVM_LOW)
    endfunction
    function void build_phase(uvm_phase phase);
      super.build_phase(phase);
      `uvm_info("BUILD", "comp1 build phase entered", UVM_LOW)
      if(!uvm_config_db#(virtual uvm_config_if)::get(this, "", "vif", vif))
        `uvm_error("GETVIF", "no virtual interface is assigned")
        
      `uvm_info("GETINT", $sformatf("before config get, var1 = %0d", var1), UVM_LOW)
      uvm_config_db#(int)::get(this, "", "var1", var1);
      `uvm_info("GETINT", $sformatf("after config get, var1 = %0d", var1), UVM_LOW)
      
      uvm_config_db#(config_obj)::get(this, "", "cfg", cfg);
      `uvm_info("GETOBJ", $sformatf("after config get, cfg.comp1_var = %0d", cfg.comp1_var), UVM_LOW)
      
      c2 = comp2::type_id::create("c2", this);
      `uvm_info("BUILD", "comp1 build phase exited", UVM_LOW)
    endfunction
  endclass

设置顶层uvm_config模块

module uvm_config;

  import uvm_pkg::*;
  `include "uvm_macros.svh"
  import uvm_config_pkg::*;
  
  uvm_config_if if0();		//在module模块才可以看得到interface,实现例化
  initial begin
  	//在验证环境中设置interface到c1、c2
    uvm_config_db#(virtual uvm_config_if)::set(uvm_root::get(), "uvm_test_top.*", "vif", if0);
    run_test(""); // empty test name
  end
endmodule

4.2 单一变量传递

实现在顶层uvm_config_test对c1.var1和c2.var2的变量设置
设置uvm_config_test类

class uvm_config_test extends uvm_test;
    comp1 c1;
    config_obj cfg;
    `uvm_component_utils(uvm_config_test)
    function new(string name = "uvm_config_test", uvm_component parent = null);
      super.new(name, parent);
    endfunction
    function void build_phase(uvm_phase phase);
      super.build_phase(phase);
      `uvm_info("BUILD", "uvm_config_test build phase entered", UVM_LOW)
      
      cfg = config_obj::type_id::create("cfg");
      cfg.comp1_var = 100;
      cfg.comp2_var = 200;
      uvm_config_db#(config_obj)::set(this, "*", "cfg", cfg);
      uvm_config_db#(int)::set(this, "c1", "var1", 10);
      uvm_config_db#(int)::set(this, "c1.c2", "var2", 20);
      
      c1 = comp1::type_id::create("c1", this);
      `uvm_info("BUILD", "uvm_config_test build phase exited", UVM_LOW)
    endfunction
    task run_phase(uvm_phase phase);
      super.run_phase(phase);
      `uvm_info("RUN", "uvm_config_test run phase entered", UVM_LOW)
      phase.raise_objection(this);
      #1us;
      phase.drop_objection(this);
      `uvm_info("RUN", "uvm_config_test run phase exited", UVM_LOW)
    endtask
  endclass

在comp1中get:

uvm_config_db#(int)::get(this, "", "var1", var1)

在comp2中get:

uvm_config_db#(int)::get(this, "", "var2", var2);

4.3 对象传递

实现配置对象config_obj从uvm_config_test到c1和c2的传递

class config_obj extends uvm_object;
    int comp1_var;
    int comp2_var;
    `uvm_object_utils(config_obj)
    function new(string name = "config_obj");
      super.new(name);
      `uvm_info("CREATE", $sformatf("config_obj type [%s] created", name), UVM_LOW)
    endfunction
  endclass

在comp2中get:

uvm_config_db#(config_obj)::get(this, "", "cfg", cfg);

在comp1中get:

uvm_config_db#(config_obj)::get(this, "", "cfg", cfg);

在uvm_config_test中set:

uvm_config_db#(config_obj)::set(this, "*", "cfg", cfg);

执行仿真命令:

vsim -novopt -classdebug +UVM_TESTNAME=uvm_config_test work.uvm_config
### 回答1: 《UVM入门进阶实验1文档》是一份用于帮助初学者进一步理解和学习UVM实验指导文档。该文档提供了UVM编程框架的第一个实验,旨在帮助读者熟悉UVM的基本概念和用法。 实验1的目标是创建一个简单的UVM测试环境,并展示如何使用UVM框架进行测试。实验包括以下几个主要步骤: 1. 创建一个UVM测试环境:在这一步骤中,我们需要定义测试环境的结构和组件。包括创建一个顶层测试模块、一个顶层环境模块,以及其他必要的组件,如驱动器、监视器、生成器等。 2. 编写测试用例:在这一步骤中,我们需要编写一个简单的测试用例来验证被测设计的功能。测试用例需要继承自UVM的`uvm_test_case`类,并在`run_phase`中定义测试过程。 3. 编写环境配置:在这一步骤中,我们需要将测试用例和测试环境进行连接,并设置一些必要的运行时参数。通过配置对象的方式,我们可以很方便地配置测试环境中的各个组件。 4. 运行仿真:在这一步骤中,我们需要运行仿真并观察测试结果。通过在测试用例中创建一个sequence对象,我们可以在运行时动态生成测试序列。 《UVM入门进阶实验1文档》详细说明了每一步的具体实现方法,并提供了代码示例和可参考的资源链接。 通过完成实验1,读者可以对UVM的基本概念和使用方法有一个更深入的了解。这将为进一步学习和掌握UVM提供坚实的基础,并为以后的工作打下良好的基础。同时,实验1还可以帮助读者培养UVM编程的思维方式和调试技巧,提高工作效率。 总之,《UVM入门进阶实验1文档》是一份非常有价值的学习资料,通过按照文档的指导完成实验,读者可以在短时间内快速入门并掌握UVM的基本用法。 ### 回答2: 《UVM入门进阶实验1文档》是一本介绍UVM(Universal Verification Methodology)的入门实验指导书。UVM是一种用于验证硬件设计的方法学,它提供了一套面向对象的验证框架,可以用于设计验证的自动化和重用。 本文档首先简要介绍了UVM的概念和特点,然后详细讲解了实验1的内容。实验1主要涉及到UVM中最基础的概念和类的使用方法。首先,介绍了UVM中的基础类,如uvm_component、uvm_object和uvm_sequence等,以及它们的继承关系和功能。然后,介绍了如何创建和管理UVM环境,并讲解了如何使用UVM Testbench中的各种组件来进行设计验证。最后,讲解了一些常用的调试技巧和工具,如波形查看器和消息记录器等。 在实验1中,学员将通过几个简单的示例,来熟悉UVM的基本概念和使用方法。例如,学员将学习如何创建一个简单的UVM Testbench,并使用UVM的配置机制来对其进行配置。此外,学员还将学习如何创建和管理UVM Sequences,并在Testbench中使用它们来生成随机的输入数据。最后,学员将学习如何使用UVM里的Transaction来封装输入输出数据,以及如何使用Scoreboard来进行结果验证。 通过完成实验1,学员将掌握UVM中最基本的概念和使用方法,为后续的进阶实验打下基础。同时,学员将对UVM的工作原理和设计验证的流程有一个清晰的认识,为进一步深入学习和应用UVM提供了基础。 ### 回答3: 《UVM入门进阶实验1文档》是一份详细介绍了如何使用UVM进行验证的教程。UVM是一种用于硬件验证的开放式框架,能够帮助工程师更高效地开发和执行验证环境。 该文档首先简要介绍了UVM的背景和原理,包括UVM Testbench的组成结构和工作流程。然后,文档逐步指导读者完成实验1,并提供了实验所需的样例代码和测试平台。 在实验1中,文档首先指导读者创建一个简单的UVM环境,并介绍了UVM的基本类和功能。然后,通过一个简单的例子演示了如何创建一个UVM测试,包括定义测试类、产生和驱动测试向量、分析和比较结果等。读者可以按照文档提供的步骤和示例代码,逐步完成实验。 在实验进行过程中,文档还不断提供了一些常见问题和解决方法。这些问题和解决方法能够帮助读者更好地理解和应用UVM,解决遇到的困惑和难题。 该文档还包括对实验的详细说明和解析,比如UVM环境的搭建、测试向量的生成和分析等。通过这些详细说明和解析,读者可以更深入地理解UVM的工作原理和实现方式。 总之,《UVM入门进阶实验1文档》是一份非常实用的教程,帮助读者快速入门和进阶使用UVM进行硬件验证。通过该文档的学习和实验,读者能够掌握UVM的基本概念和使用方法,为日后的硬件验证工作打下坚实的基础。
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值