【system verilog】Mailboxes

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

在这里插入图片描述

特此致谢,如有侵权,请联系我删除!

  • 0
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值