2011-02-23 08:42:07| 分类: SystemVerilog | 标签: |举报 |字号大中小 订阅
之前介绍了uvm_blocking_put_port/uvm_blocking_put_export, 其对应的uvm_blocking_get_port/uvm_blocking_get_export的简单模型可以照猫画虎的设计,
源代码如下:
1 /**
2 ** tlm_get.sv
3 ** 说明: 通过简单的模型来阐述 UVM/OVM中TLM uvm_get_port
4 ** 和uvm_port_imp直接连接的基本原理。直接编译运行即可。
5 ** 作者:http://electron64.blog.163.com
6 */
7 virtual class port_base#(type T=int) ; // 构造一个抽象类
8 typedef port_base#(T) this_type;
9 protected this_type port_handle; // 申明一个对象句柄(指针)
10
11 pure virtual task get_t(output T trans); // 纯虚函数,get_port和get_imp都必须以它为基类
12
13 function void connect(this_type port); // connect仅仅是实现对象句柄的赋值
14 port_handle = port;
15 endfunction // connect
16
17 endclass // port_base
18
19 class get_port#(type T=int) extends port_base#(T);
20 virtual task get_t(output T trans);
21 port_handle.get_t(trans);
22 endtask // get_t
23 endclass // get_port
24
25 class get_imp#(type T=int, type IMP=int) extends port_base#(T);
26 local IMP m_imp; // imp表示具体实现get_t()函数的对象句柄。
27 function new(IMP imp);
29 endfunction // new
30
31 virtual task get_t(output T trans);
32 trans = $random;
34 endtask // get_t
35
36 endclass // get_imp
37
38 class producer;
39 get_imp#(int, producer) m_get_export;
40 function new();
42 endfunction // new
43
44 task get_t(int trans);
45 $display("[INFO]: Generated the transaction:%d", trans);
46 endtask // get_t
47 endclass // producer
48
49 class consumer;
50 get_port#(int) m_get_port;
51 function new();
53 endfunction // new
54
55 task run(int num=10);
56 int trans;
57 for(int i=0; i<num; i++) begin // 顺序产生10个int类型的transaction.
59 $display("[INFO]: Get the transaction:%d", trans);
60 end
61 endtask // run
62 endclass // consumer
63
64 module top;
65 producer p;
66 consumer c;
67 initial begin
68 p = new();
72 #10 $finish;
73 end
74 endmodule
75
需要说明的是,get需要使用(output T trans)做参数,这样才能将数据通过层层函数传递到消费者。
运行结果如下:
# [INFO]: Generated the transaction: 303379748
# [INFO]: Get the transaction: 303379748
# [INFO]: Generated the transaction:-1064739199
# [INFO]: Get the transaction:-1064739199
# [INFO]: Generated the transaction:-2071669239
# [INFO]: Get the transaction:-2071669239
# [INFO]: Generated the transaction:-1309649309
# [INFO]: Get the transaction:-1309649309
# [INFO]: Generated the transaction: 112818957
# [INFO]: Get the transaction: 112818957
# [INFO]: Generated the transaction: 1189058957
# [INFO]: Get the transaction: 1189058957
# [INFO]: Generated the transaction:-1295874971
# [INFO]: Get the transaction:-1295874971
# [INFO]: Generated the transaction:-1992863214
# [INFO]: Get the transaction:-1992863214
# [INFO]: Generated the transaction: 15983361
# [INFO]: Get the transaction: 15983361
# [INFO]: Generated the transaction: 114806029
# [INFO]: Get the transaction: 114806029
# ** Note: $finish : tlm_get.sv(72)
# Time: 10 ns Iteration: 0 Instance: /top
其对应的UML模型图如下:
评论