initialbegin mailbox mlb#(Transaction); //指定类型 Transaction tr,tmp; repeat(10) begin tr = new(); // 在循环内创建对象 assert(tr.randomize()); mlb.put(tr); //存入mlb end repeat(10) begin mlb.get(tmp); //从mlb取 tmp.show(); end end
class Transaction; randbit[7:0] data; randbit[7:0] addr; endclass class Generator; mailbox mlb; Transaction tr; functionnew(mailbox m); this.mlb= m; endfunction task run(); repeat(5) begin tr=new(); assert(tr.randomize()); mlb.put(tr); $display("@ %0t [gen] put a tr",$time); end endtask endclass
class Driver; mailbox mlb; Transaction tr; functionnew(mailbox m); this.mlb= m; endfunction task run(); foreverbegin #10; //每延迟10ns,driver取一个数据,也可以同步到时钟沿@(posedge clk); mlb.get(tr); $display("@ %0t [drv] get a tr",$time); end endtask endclass
programautomatic tb; Generator gen; Driver drv; mailbox mlb; initialbegin mlb=new(1);//定容信箱,容量1,如果参数为空,则mailbox不限容量 gen=new(mlb); drv=new(mlb); fork// 并发执行 gen.run(); drv.run(); join end endprogram // output @ 0 [gen] put a tr @ 10 [drv] get a tr @ 10 [gen] put a tr @ 20 [drv] get a tr @ 20 [gen] put a tr @ 30 [drv] get a tr @ 30 [gen] put a tr @ 40 [drv] get a tr @ 40 [gen] put a tr @ 50 [drv] get a tr
programautomatic tb; event handshake; // 事件 mailbox mlb; class Generator; ... task run(); repeat(5) begin ... mlb.put(tr); $display("@ %0t [gen] put a tr",$time); @handshake; //生成完一个tr后阻塞。 end endtask endclass
class Driver; ... task run(); foreverbegin #10; mlb.get(tr); $display("@ %0t [drv] get a tr",$time); ->handshake; //获取完一个tr后触发事件 end endtask
endclass
Generator gen; Driver drv; initialbegin mlb=new(); //无限容量 gen=new(mlb); drv=new(mlb); fork ... join_none end endprogram @ 0 [gen] put a tr @ 10 [drv] get a tr @ 10 [gen] put a tr @ 20 [drv] get a tr @ 20 [gen] put a tr @ 30 [drv] get a tr @ 30 [gen] put a tr @ 40 [drv] get a tr @ 40 [gen] put a tr @ 50 [drv] get a tr
programautomatic tb; mailbox#(Transaction) mlb; mailbox#(int) m; // 定义两个信箱 class Generator; Transaction tr; int i; task run(); repeat(5) begin tr=new(); assert(tr.randomize()); mlb.put(tr); $display("@ %0t [gen] put a tr",$time); m.get(i); //m为空,阻塞 end endtask endclass
class Driver; Transaction tr; int i; task run(); foreverbegin #10; mlb.get(tr); $display("@ %0t [drv] get a tr",$time); m.put(i); // m不为空,触发了Generator中的语句。 end endtask endclass
Generator gen; Driver drv; initialbegin mlb=new(); m=new(); //新建信箱 gen=new(); drv=new(); fork gen.run(); drv.run(); join end endprogram @ 0 [gen] put a tr @ 10 [drv] get a tr @ 10 [gen] put a tr @ 20 [drv] get a tr @ 20 [gen] put a tr @ 30 [drv] get a tr @ 30 [gen] put a tr @ 40 [drv] get a tr @ 40 [gen] put a tr @ 50 [drv] get a tr