- class edata extends uvm_object;
- int data;
- `uvm_object_utils(edata)
- ...endclass
-
- class ecb extends uvm_event_callback;
- `uvm_object_utils(ecb)
- ...//函数名一定是这两个,参数可以设置data
- function bit pre_trigger(uvm_event e, uvm_object data=null);
- `uvm_info("", $sformatf(), UVM_LOW)
- return 0;//这个函数需要有返回值,如果返回1则表示uvm_event不会被trigger,也不会执行下面的post
- endfunction
-
- function void post_trigger(uvm_event e, uvm_object data=null);
- `uvm_info()
- endfunction
- endclass
-
- class comp1 extends uvm_component;
- uvm_event e1;
- `uvm_component_utils()
- ...
- function void build_phase(...);
- super....
- e1=uvm_event_pool::get_global("e1");//借助全局的event_pool例化event1
- //如果没有呢?会帮你创建一个e1的句柄
- endfunction
-
- task run_phase(...);
- edata d=new();
- ecb cb=new();
- d.data=100;
- #10ns;
- e1.add_callback(cb);//将例化了的cb和e1关联起来,在e1被trigger时也会调用回调函数
- e1.trigger(d);//trigger时带上数据d
- `uvm_info()
- endtask
-
- endclass
-
- class comp2 extends uvm_component;
- uvm_event e1;
- `uvm_component_utils()
- ...
- function void build(...);
- super.b.....
- e1=uvm_event_pool::get_global("e1");//也拿一个e1
- endfunction
-
- task run_phase(...);
- uvm_object tmp;
- edata d;
- `uvm_info()
- e1.wait_trigger_data(tmp);//等待e1被触发,tmp是一个object类型
- //既等到了trigger的event,也等到了event带来的数据
- void'($cast(d, tmp));//父类转子类
- `uvm_info()
- endtask
-
- class env1 extends uvm_env;
- comp1 c1;
- comp2 c2;
- `uvm_component_utils()
- ...endclass
- typedef uvm_object_string_pool #(uvm_barrier) uvm_barrier_pool;
- typedef uvm_object_string_pool #(uvm_event#(uvm_object)) uvm_event_pool;
- class comp1 extends uvm_component;
- uvm_barrier b1;
- `uvm_component_utils(comp1)
- ...
- function void build_phase(uvm_phase phase);
- super....
- b1=uvm_barrier_pool::get_global("b1");
- endfunction
-
- task run_phase(uvm_phase phase);
- #10ns;
- `uvm_info()
- b1.wait_for();//站在起跑线上等待c2
- `uvm_info()
- endtask
- endclass
-
- class comp2 extends uvm_component;
- uvm_barrier b2;
- `uvm_component_utils(comp2)
- ...
- function void build_phase(uvm_phase phase);
- super....
- b1=uvm_barrier_pool::get_global("b1");
- endfunction
-
- task run_phase(uvm_phase phase);
- #20ns;
- `uvm_info()
- b1.wait_for();
- `uvm_info()
- endtask
- endclass
-
- class env1 extends uvm_env;
- comp1 c1;
- comp2 c2;
- uvm_barrier b1;
- `uvm_component_utils(env1)
- ...
- function void build_phase(uvm_phase phase);
- super.build_phase(phase);
- c1=comp1::type_id::create("c1", this);
- c2=comp2::type_id::create("c2", this);
- b1=uvm_barrier_pool::get_global("b1");
- endfunction
-
- task tun_phase(uvm_phase phase);
- b1.set_threshold(3);//要等到3个
- `uvm_info("BSYNC", $sformatf("", b1.get_threshold(), $time), UVM_LOW)
- #50ns;
- b1.set_threshold(2);//满足2个了,枪响,c1和c2继续执行wait_for后面的语句
- `uvm_info()
- endtask
- endclass
- class edata extends uvm_object;
- int data;
- `uvm_object_utils(edata)
- ...endclass
-
- class cb1 extends uvm_callback;
- `uvm_object_utils(cb1)
- ...//回调类中可以定义任何名称的方法
- virtual function void do_trans(edata d);//do_trans不是预定义好的
- d.data=200;
- `uvm_info(executed)
- endfunction
- endclass
-
- class cb2 extends cb1;
- `uvm_object_utils(cb2)
- ...
- function void do_trans(edata d);
- d.data=300;
- `uvm_info()
- endfunction
- endclass
-
- class comp1 extends uvm_component;
- `uvm_component_utils()
- `uvm_register_cb(comp1, cb1)//注册callback,将cb1和comp1关联起来
- ...
- task run_phase(uvm_phase phase);
- edata d=new();
- d.data=100;
- `uvm_info(proceeding)
- `uvm_do_callbacks(comp1, cb1, do_trans(d))//绑定与插入callback,域->类型->方法
-
- class env1 extends uvm_env;
- comp1 c1;
- cb1 m_cb1;
- cb2 m_cb2;
- `uvm_component_utils(env1)
- function new(string name, uvm_component parent);
- super.new(name, parent);
- m_cb1=new("m_cb1");
- m_cb2=new("m_cb2");
- endfunction
-
- function void build_phase(uvm_phase phase);
- super.build_phase(phase);
- c1=comp1::type_id::create("c1", this);
- uvm_callbacks #(comp1)::add(c1, m_cb1);//添加callback进来
- cb2同上
- endfunction
- endclass