在sv中达成同步的方式有 event, semaphore和mailbox。
而在UVM中event进化成uvm_event,不仅仅拥有达成不同组件进程之间同步的功能,还能像TLM通信一样传递数据,并且作用范围更广(TLM通信只能在uvm_component之间,而uvm_event不限于此)。
首先从常见的uvm_event达成同步的方式讲起:
example code:
module test
import uvm_pkg::*;
initial begin
// get a reference to the global singleton object by
// calling a static method
uvm_event_pool ev_pool = uvm_event_pool::get_global_pool();
// either create a uvm_event or return a reference to it
// (which depends on the order of execution of the two
// initial blocks - the first call creates the event,
// the second and subsequent calls return a reference to
// an existing event.)
uvm_event ev = ev_pool.get("ev");
// wait (an arbitrary) 10 and then trigger the event
#10 ev.trigger();
$display("%t: event ev triggered", $time);
end
initial begin
// get a reference to the global singleton object by
// calling a static method
uvm_event_pool ev_pool = uvm_event_pool::get_global_pool();
// either create a uvm_event or return a reference to it
uvm_event ev = ev_pool.get("ev");
// wait for the trigger
ev.wait_trigger();
$display("%t: event ev trigger received", $time);
end
endmodule
(两个initial 块可以想象成两个不同UVM组件的不同进程)
通过上面的注释已经基本可以理解uvm_event的达成同步的方法,下面看看背后的原理:
// get a reference to the global singleton object by
// calling a static method
uvm_event_pool ev_pool = uvm_event_pool::get_global_pool();
要知道上面的的语句干了什么是就得知道uvm_event_pool的源代码:
https://verificationacademy.com/verification-methodology-reference/uvm/src/base/uvm_pool.svh
class uvm_object_string_pool #(type T=uvm_object) extends uvm_pool #(string,T);
typedef uvm_object_string_pool #(T) this_type;
static protected this_type m_global_pool;
// Function: new
//
// Creates a new pool with the given ~name~.
function new (string name="");
super.new(name);
endfunction
const static string type_name = {"uvm_obj_str_pool"};
// Function: get_type_name
//
// Returns the type name of this object.
virtual function string get_type_name();
return type_name;
endfunction
// Function: get_global_pool
//
// Returns the singleton global pool for the item type, T.
//
// This allows items to be shared amongst components throughout the
// verification environment.
static function this_type get_global_pool ();
if (m_global_pool==null)
m_global_pool = new("global_pool");
return m_global_pool;
endfunction
// Function: get_global
//
// Returns the specified item instance from the global item pool.
static function T get_global (string key);
this_type gpool;
gpool = get_global_pool();
return gpool.get(key);
endfunction
// Function: get
//
// Returns the object item at the given string ~key~.
//
// If no item exists by the given ~key~, a new item is created for that key
// and returned.
virtual function T get (string key);
if (!pool.exists(key))
pool[key] = new (key);
return pool[key];
endfunction
// Function: delete
//
// Removes the item with the given string ~key~ from the pool.
...
// Function- do_print
...
endclass
typedef class uvm_barrier;
typedef class uvm_event;
typedef uvm_object_string_pool #(uvm_barrier) uvm_barrier_pool;
typedef uvm_object_string_pool #(uvm_event) uvm_event_pool;
uvm_event_pool是一个参数为uvm_event类型的uvm_object_string_pool,而uvm_object_string_pool又是继承于uvm_pool。uvm_event_pool和uvm_pool没有太大差别,也不是很复杂,在这里不是重点。
typedef uvm_object_string_pool #(T) this_type;
static protected this_type m_global_pool;
protected T pool[KEY]; // type KEY = string
.........
static function this_type get_global_pool ();
if (m_global_pool==null)
m_global_pool = new("global_pool");
return m_global_pool;
endfunction
get_global_pool()的源代码如上,结合uvm_event_pool的成员pool和m_global_pool——其中pool是从uvm_pool中继承过来的,而KEY和T正是传递的类型参数,string和uvm_event——可以知道, 调用get_global_pool()会返回一个类型为uvm_event_pool的句柄,句柄指向的对象是全局并且单例的。也就是说两个initial块中两次调用get_global_pool都是返回同一个uvm_event_pool类型对象的句柄,只是先调用的那个get_global_pool函数会创建这个对象。
uvm_event ev = ev_pool.get("ev");
get ()函数如下:
virtual function T get (string key);
if (!pool.exists(key))
pool[key] = new (key);
return pool[key];
endfunction
根据之前的源代码知道,在这里pool是一个关联数组,类型为uvm_event,索引类型为string(这也是为什么get()的参数为字符串)。两个initial块两次调用get(),是为了获得同一个pool[string]对象的句柄,先调用的那个get()会创建这个pool[string],而后调用的那个只是获得这个pool[string]对象的句柄。这个过程和systemverilog中event的传递是一样的,是在两个进程中完成同步的基础。
#10 ev.trigger();
ev.wait_trigger();
前两部的目的本质上是为了让两个Initial块中两个uvm_event句柄指向同一个对象,这种作法相对于conifg_db或者层次化引用的方式显然方便得多,并且使用get_global()而非get()会更简单。接下来和systemverlog中event达成同步的操作一样了,在一个进程中触发->,另外一个进程中等待触发wait (event.triggered) or @event。
在uvm_event中,触发的方式是uvm_event.trigger ();而等待出发的方式是wait_ptrigger () (电平触发)or wait_trigger () (上升沿触发) (与event的两种等待方式对应)。
uvm_event的源代码如下,去掉了一些不常用的方法,这里只看关键的方法。
class uvm_event extends uvm_object;
const static string type_name = "uvm_event";
local event m_event;
local int num_waiters;
local bit on;
local time trigger_time=0;
local uvm_object trigger_data;
local uvm_event_callback callbacks[$];
// Function: new
function new (string name="");
super.new(name);
endfunction
// Task: wait_on
virtual task wait_on (bit delta=0);
if (on) begin
if (delta)
#0;
return;
end
num_waiters++;
@on;
endtask
// Task: wait_off
virtual task wait_off (bit delta=0);
if (!on) begin
if (delta)
#0;
return;
end
num_waiters++;
@on;
endtask
// Task: wait_trigger
virtual task wait_trigger ();
num_waiters++;
@m_event;
endtask
// Task: wait_ptrigger
virtual task wait_ptrigger ();
if (m_event.triggered)
return;
num_waiters++;
@m_event;
endtask
// Task: wait_trigger_data
virtual task wait_trigger_data (output uvm_object data);
wait_trigger();
data = get_trigger_data();
endtask
// Task: wait_ptrigger_data
virtual task wait_ptrigger_data (output uvm_object data);
wait_ptrigger();
data = get_trigger_data();
endtask
// Function: trigger
virtual function void trigger (uvm_object data=null);
int skip;
skip=0;
if (callbacks.size()) begin
for (int i=0;i<callbacks.size();i++) begin
uvm_event_callback tmp;
tmp=callbacks[i];
skip = skip + tmp.pre_trigger(this,data);
end
end
if (skip==0) begin
->m_event;
if (callbacks.size()) begin
for (int i=0;i<callbacks.size();i++) begin
uvm_event_callback tmp;
tmp=callbacks[i];
tmp.post_trigger(this,data);
end
end
num_waiters = 0;
on = 1;
trigger_time = $realtime;
trigger_data = data;
end
endfunction
// Function: get_trigger_data
virtual function uvm_object get_trigger_data ();
return trigger_data;
endfunction
// Function: get_trigger_time
virtual function time get_trigger_time ();
return trigger_time;
endfunction
// Function: is_on
// Function: is_off
// Function: reset
virtual function void reset (bit wakeup=0);
event e;
if (wakeup)
->m_event;
m_event = e;
num_waiters = 0;
on = 0;
trigger_time = 0;
trigger_data = null;
endfunction
// Function: add_callback
// Function: delete_callback
// Function: cancel
virtual function void cancel ();
if (num_waiters > 0)
num_waiters--;
endfunction
// Function: get_num_waiters
virtual function int get_num_waiters ();
return num_waiters;
endfunction
virtual function uvm_object create(string name="");
uvm_event v;
v=new(name);
return v;
endfunction
virtual function string get_type_name();
return type_name;
endfunction
virtual function void do_print (uvm_printer printer);
endfunction
virtual function void do_copy (uvm_object rhs);
endfunction
endclass : uvm_event
可以发现uvm_event的基础其实还是event,只不过event的触发和等待两个动作进行了很多扩展, 主要区别如下:
http://www.sohu.com/a/140684109_778637
event被->触发之后,会触发使用@等待该事件的对象;uvm_event通过trigger()来触发,会触发使用wait_trigger()等待的对象。如果要再次等待事件触发,event只需要再次用->来触发,而uvm_event需要先通过reset()方法重置初始状态,再使用trigger()来触发。
event无法携带更多的信息,而uvm_event可以通过trigger(uvm_event data = null)的可选参数,将所要伴随触发的数据信息都写入到该触发事件中,而等待该事件的对象可以通过方法wait_trigger_data(output uvm_object data)来获取事件触发时写入的数据对象。这实际上是一个句柄的传递,句柄的类型是uvm_object,也就是说传递的对象句柄得是uvm_object类型的。那如何传递非uvm_object类型的对象呢,首先这个对象必须是uvm_object的子类或者子类的子类,然后才能将其句柄作为trigger()方法的参数,然后wait_trigger ()的参数必须是uvm_object类型的,可以用一个uvm_object类型的tmp作为参数传递句柄,然后使用$cast赋值给目标类型句柄。可以参考:http://www.sohu.com/a/140684109_778637
event触发时无法直接触发回调函数,而uvm_event可以通过add_callback(uvm_event_callback cb, bit append = 1)函数来添加回调函数。
event无法直接获取等待它的进程数目,而uvm_event不但可以通过get_num_waiters()来获取等待它的进程数目。
uvm_event 能够使用callback机制(这里就不讨论uvm_event_callback了,也不复杂), uvm_event能够获取目前事件的等待获触发状态。两者最大的区别还是uvm_event能够传递数据,而event不行。
由于m_global_pool是一个静态全局的单例,没有必要专门创建一个uvm_event_pool的句柄来指向它;
static function T get_global (string key);
this_type gpool;
gpool = get_global_pool();
return gpool.get(key);
endfunction
get_global (string key)中集合了get_global_pool()和 get()方法,于是下面两部可以合成一句话
uvm_event_pool ev_pool = uvm_event_pool::get_global_pool();
// either create a uvm_event or return a reference to it
uvm_event ev = ev_pool.get("ev");
uvm_event ev = uvm_event_pool::get_global ("ev");
其实这两种做法的结果都是一样的,都是在uvm_event_pool::m_global_pool这个静态全局单例中的创建了一个uvm_event对象,并存放在关联数组uvm_event_pool::m_global_pool.pool[string]中,索引就是get or get_global ()的参数, 如果这个字符串索引已经存在,那么就返回这个pool[string]句柄。
https://www.chipverify.com/uvm/uvm-pool
https://www.edaplayground.com/x/2Jyj
https://www.cnblogs.com/lybinger/p/8057653.html
https://verificationacademy.com/verification-methodology-reference/uvm/docs_1.1c/html/files/base/uvm_event-svh.html#uvm_event.wait_on