您好,欢迎来到外链网!
当前位置:外链网 » 站长资讯 » 专业问答 » 文章详细 订阅RssFeed

SVSystemVerilog Mailbox

来源:互联网 浏览:62次 时间:2023-04-08
SystemVerilog Mailbox

? ? ? ?前言:mailboxe是一种通信机制,允许进程之间交换消息。希望与另一个进程通信的进程将消息发送到mailboxe,mailboxe将消息临时存储在系统定义的内存对象中,以便将消息传递给所需的进程。根据大小,mailboxe被分类为:

bounded mailboxunbounded mailboxA bounded mailbox is with the size defined. mailbox becomes full when on storing a bounded number of messages. A process that attempts to place a message into a full mailbox shall be suspended until enough space becomes available in the mailbox queue.Unbounded mailboxes are with unlimited size.? 一、Mailbox types

? ? There are two types of mailboxes,

Generic MailboxParameterized mailbox? ?1.1 Generic Mailbox (type-less mailbox) ?香港vps The default mailbox is type-less. that is, a single mailbox can send and receive data of any type.mailbox mailbox_name; Parameterized mailbox (mailbox with particular type)Parameterized mailbox is used to transfer a data of particular type.mailbox#(type)?mailbox_name; ?1.2 Mailbox Methods ? SystemVerilog Mailbox is a built-in class that provides the following methods. these are applicable for both Generic and Parameterized mailboxes? ? ? ? No.? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ajdxtd()Creat a mailbox2put()Place a message in a mailbox3try_put()Try to place a message in a mailbox without blocking4get() / peek()Retrieve a message from a mailbox5num()Returns the number of messages in the mailbox6try_get() / try_peek()Try to retrieve a message from a mailbox without blockingnew( );?

? Mailboxes are created with the new() method.

mailbox_name = new(); // Creates unbounded mailbox and returns mailbox handle mailbox_name = new(m_size); //Creates bounded mailbox with size m_size and returns mailbox//handle ,where m_size is integer variable ? ?1.3 SystemVerilog Mailbox example

? ? ? ?In the example below,Mailbox is used for communication between generator and driver.

Process-1(Generator class) will generate (created and randomize) the packet and put into the mailbox mb_boxProcess-2(Driver class) gets the generated packet from the mailbox and display the fields//-------------------------------------------------------------------------// Packet//-------------------------------------------------------------------------class packet; rand bit [7:0] addr; rand bit [7:0] data; //Displaying randomized values function void post_randomize(); $display("Packet::Packet Generated"); $display("Packet::Addr=%0d,Data=%0d",addr,data); endfunctionendclass //-------------------------------------------------------------------------//Generator - Generates the transaction packet and send to driver//-------------------------------------------------------------------------class generator; packet pkt; mailbox m_box; //constructor, getting mailbox handle function new(mailbox m_box); this.m_box = m_box; endfunction task run; repeat(2) begin pkt = new(); pkt.randomize(); //generating packet m_box.put(pkt); //putting packet into mailbox $display("Generator::Packet Put into Mailbox"); #5; end endtaskendclass //-------------------------------------------------------------------------// Driver - Gets the packet from generator and display's the packet items//-------------------------------------------------------------------------class driver; packet pkt; mailbox m_box; //constructor, getting mailbox handle function new(mailbox m_box); this.m_box = m_box; endfunction task run; repeat(2) begin m_box.get(pkt); //getting packet from mailbox $display("Driver::Packet Recived"); $display("Driver::Addr=%0d,Data=%0d\n",pkt.addr,pkt.data); end endtaskendclass //-------------------------------------------------------------------------// tbench_top //-------------------------------------------------------------------------module mailbox_ex; generator gen; driver dri; mailbox m_box; //declaring mailbox m_box initial begin //Creating the mailbox, Passing the same handle to generator and driver, //because same mailbox should be shared in-order to communicate. m_box = new(); //creating mailbox gen = new(m_box); //creating generator and passing mailbox handle dri = new(m_box); //creating driver and passing mailbox handle $display("------------------------------------------"); fork gen.run(); //Process-1 dri.run(); //Process-2 join $display("------------------------------------------"); endendmodule ?Simulator Output?------------------------------------------Packet::Packet GeneratedPacket::Addr=3,Data=38Generator::Packet Put into MailboxDriver::Packet RecivedDriver::Addr=3,Data=38Packet::Packet GeneratedPacket::Addr=118,Data=92Generator::Packet Put into MailboxDriver::Packet RecivedDriver::Addr=118,Data=92------------------------------------------ 57516447