Mailbox

Hello!
I’m trying to put and get the data into a mailbox, pretty basic stuff that I’m struggling with. So, here’s the code:

module mailbox_eg;
	int i;
	mailbox mbx;
		initial begin
			mbx = new(); //declaration and istantiation of mailbox mbx 	
				fork	
					begin
						$display("\n--------------Sending data----------------");
						#1; //to start receiving the data from 1 ns
						for (i=1;i<=5;i++)
							begin: P1
								mbx.put(i);
								$display("\ndata sent = %0d at time = %0dns",mbx,$time);
								#1;
							end: P1
						$display("\n-----------------Receiving data----------------");
							begin: P2
								for(i=1;i<=5;i++)
									begin

										mbx.get(i);
$display("\ndata received = %0d at time = %0dns",mbx,$time);
										#1;
									end
							end: P2
					end
				join
		end
endmodule

The O/P I was expecting was:

--------------Sending data----------------
# 
# data sent = 1 at time = 1ns
# 
# data sent = 2 at time = 2ns
# 
# data sent = 3 at time = 3ns
# 
# data sent = 4 at time = 4ns
# 
# data sent = 5 at time = 5ns
# 
# -----------------Receiving data----------------
# 
# data received = 1 at time = 6ns
# 
# data received = 2 at time = 7ns
# 
# data received = 3 at time = 8ns
# 
# data received = 4 at time = 9ns
# 
# data received = 5 at time = 10ns

But the O/P I received is:

--------------Sending data----------------
# 
# data sent = 65538 at time = 1ns
# 
# data sent = 65538 at time = 2ns
# 
# data sent = 65538 at time = 3ns
# 
# data sent = 65538 at time = 4ns
# 
# data sent = 65538 at time = 5ns
# 
# -----------------Receiving data----------------
# 
# data received = 65538 at time = 6ns
# 
# data received = 65538 at time = 7ns
# 
# data received = 65538 at time = 8ns
# 
# data received = 65538 at time = 9ns
# 
# data received = 65538 at time = 10ns

What do I do?
Thanks in advance!

In reply to Shashank Gurijala:

in $display statement you are using “max”, you should use “I” in put section.
$display(“\ndata sent = %0d at time = %0dns”,mbx,$time);

In get section, you should use a different variable other than the loop iterator (“i”) to save the value from mailbox, lets say “j”.

mbx.get(j);
$display(“\ndata received = %0d at time = %0dns”,j,$time);

In reply to Shashank Gurijala:

A couple of other comments about your example code

  1. Technically, using class handle (mbx) where numeric expression is expected in $display should have been a run time error.
  2. You should always parameterize your mailbox with the type you expect to put into it.
mailbox #(int) mbx;
  1. You have a fork/join block with one begin/end statement. That behaves the same has having a begin/end block with no fork/join.
  2. Declare your for loop iterator variables as part of the loop statement. That helps it local and avoid potential collisions between different loops executing concurrently.
for (int i=1;i<=5;i++)

  1. Please use code tags making your code easier to read. I have added them for you.

In reply to yourcheers:

Thanks yourcheers!! I replaced the mbx with i and j variables and removed the P2 “for loop” and it’s working.