mailbox的功能:
Mailboxes是一种在进程之间交换消息的机制。数据可以通过一个进程发送到Mailboxes,然后由另一个进程获取。
mailbox 中可以放的数据:
数据可以是任何有效的systemVerilog数据类型,包括类class数据类型。
非参数化Mailboxes是无类型的,即单个Mailboxes可以发送和接收不同类型的数据。
SystemVerilog提供以下使用邮箱的方法:
- Mailboxes赋值:new()
- 放数据:put()
- 非阻塞放数据:try_put()
- 获取数据:get()或peek()
- 非阻塞获取数据:try_get()或try_peek()
- 获取Mailboxes中的数据数:num()
new()
使用new()方法创建邮箱。
function new(int bound = 0);
new()函数返回Mailboxes句柄,如果无法创建Mailboxes,则返回null。
如果bound参数为0,则Mailboxes是无界的(默认值),put()操作永远不会阻塞。
num()
可以通过num()方法获取Mailboxes中的数据数。
put()
put()方法在Mailboxes中放置一条数据。数据可以是任何单个表达式,包括对象句柄。
put()方法以严格的FIFO规则在Mailboxes中存储数据。
try_put()
try_put()方法尝试将数据放入Mailboxes中。如果Mailboxes已满,则该方法返回0。
get()
get()方法从Mailboxes中获取数据。如果Mailboxes为空,则当前进程将阻塞,直到Mailboxes中放入数据。
try_get()
try_get()方法尝试从Mailboxes中获取数据而不会阻塞。如果Mailboxes为空,则该方法返回0。
peek()
peek()方法从Mailboxes复制数据而不从队列中删除数据。如果Mailboxes为空,则当前进程将阻塞,直到Mailboxes中放入数据。
try_peek()
try_peek()方法尝试从Mailboxes中复制数据而不会被阻塞。如果Mailboxes为空,则该方法返回0。
代码示例:
program mailbox_ex;
mailbox checker_data = new();
initial begin
fork
input_monitor();
checker();
join_any
#1000;
end
task input_monitor();
begin
integer i = 0;
// This can be any valid data type
bit [7:0] data = 0;
for(i = 0; i < 4; i ++) begin
#(3);
data = $random();
$display("[%0d] Putting data : %x into mailbox", $time,data);
checker_data.put(data);
end
end
endtask
task checker();
begin
integer i = 0;
// This can be any valid data type
bit [7:0] data = 0;
while (1) begin
#(1);
if (checker_data.num() > 0) begin
checker_data.get(data);
$display("[%0d] Got data : %x from mailbox", $time,data);
end else begin
#(7);
end
end
end
endtask
endprogram
输出
[3] Putting data : 24 into mailbox
[6] Putting data : 81 into mailbox
[9] Putting data : 09 into mailbox
[9] Got data : 24 from mailbox
[10] Got data : 81 from mailbox
[11] Got data : 09 from mailbox
[12] Putting data : 63 into mailbox
[12] Got data : 63 from mailbox
SystemVerilog系列所有文章都可以在下面这个网站上在线仿真哦。
致谢:
本文转载自
微信公众号“芯片验证工程师”
原文链接:SystemVerilog教程之Mailboxes
特此致谢,如有侵权,请联系我删除!