I want to implement mailbox peek method’s behavior using queues how to do that ?
In reply to Hardik Trivedi:
int que[$]; //queue declaration
...
function int peek();
while (que.size > 0) begin
peek = que[0];
end
endtask
In reply to rohitk:
That doesn’t work. You will get an infinite loop when (que.size > 0) is true. You need a task with a wait statement
int que[$]; //queue declaration
...
task peek(output int message);
wait (que.size > 0)
message = que[0]; // this assumes you do que.push_back(message)
endtask
In reply to dave_59:
thanks for correcting, so final code will look like following:
int que[$]; //queue declaration
...
task peek(output int message);
wait (que.size > 0
message = que.pop_front(); // Pop queue content
que.push_front(message); // Push message back in front
endtask
In reply to rohitk:
That’s not correct either. You don’t want to be disturbing the queue as other processes may be accessing it as well. My comment about push_back was if you are also implementing a put() task.
In reply to dave_59:
Agreed, thanks for correcting again. your original solution works fine as it is.
In reply to dave_59:
Hi Dave, If I am not wrong the basic difference between peek and get is that peek method doesn’t delete the element from the mailbox. SO you have used this,
wait (que.size > 0
message = que[0]
But this will always give the zeroth location data right?
Correct me If I am wrong.
Thanks,
Sitesh
It will, and that is what peek does.
In reply to ocmob:
Yeah but how we can access all the element from the queue.It will always loop the zero’th element I guess.
Here I am attaching my code please have a look.
module test;
int result ,result_1;
class mailbox #(type T = bit);
T que [$];
task put(input T item);
que.push_back(item);
endtask
task get(output T item);
wait(que.size > 0);
item = que.pop_front();
endtask
task peek(output T item);
wait(que.size > 0);
for(int i; i < que.size -1 ;i ++)
item = que[i];
$display("item =%0d",item);
endtask
endclass
class A;
mailbox #(int) mbx;
rand bit[3:0] data [];
constraint abc {data.size == 10;}
function new();
mbx = new;
endfunction
endclass
A a1,a2;
initial begin
a1 = new();
a1.randomize ();
foreach(a1.data[i])
a1.mbx.put(a1.data[i]);
$display(a1.mbx);
fork
//forever begin
//a1.mbx.get(result);
//$display($time,"Result = %0d",result);
//$display(a1.mbx);
//#10;
//end
forever begin
a1.mbx.peek(result_1);
$display($time,"Result_1 = %0d",result_1);
$display(a1.mbx);
end
join
end
endmodule
In reply to Sitesh11:
A mailbox has FIFO (First-in First-out) semantics. Since you are using push_back() to put() things on the queue, element 0 of a queue is the first-in element, the oldest. This is the element that both get() and and peek() return.
Your peek() routine returns the newest element.