队列提供了许多内置方法。如下表所示:

module dq;
bit[7:0] dq1[$]; // A unbounded queue of unsigned 8-bit
int q3[$:5] = {0,1,2,3,4,5}; //bounded queue
int a;
initial begin
a = dq1.size( ); //empty queue
$display ($stime,,, "empty dq1 size = %0d",a);
dq1[0] = 0; dq1[1] = 1; dq1[2] = 2;
$display ($stime,,, "dq1 SIZE = %0d",dq1.size( ));
$display ($stime,,, "dq1=",dq1);
dq1.insert (3,3); //index, value
$display($stime,,, "After Insert dq1 SIZE = %0d",dq1.size( ));
$display ($stime,,, "dq1=",dq1);
dq1.delete (3); //index
$display ($stime,,, "After delete dq1 SIZE = %0d",dq1.size( ));
$display ($stime,,, "dq1=",dq1);
a = dq1.pop_front( ); //pop frst entry of the queue
$display ($stime,,, "dq1 pop front = %0d ",a);
$display ($stime,,, "dq1=",dq1);
a = dq1.pop_back( ); //pop last entry of the queue
$display ($stime,,, "dq1 pop back = %0d ",a);
$display ($stime,,, "dq1=",dq1);
//push the frst entry of the queue with '4'
dq1.push_front(4);
$display ($stime,,, "push front dq1=",dq1);
//push the last entry of the queue with '5'
dq1.push_back(5);
$display ($stime,,, "push back dq1=",dq1);
q3_size = q3.size + 5; //size > q3 size
//underfow : pop from index 6,7,8,9,10 – run time Warning
for (int i = 0; i < q3_size; i++)
$display($stime,,,"q3[%0d] = %0d", i, q3.pop_front( ) );
end
//Solution for underfow - check for size before pop
while (q3.size( ) > 0)
$display($stime,,,"q3 = %0d", q3.pop_front ( ));
//overfow : push over the bound limit – run time Warning
for (int i = 0; i < q3_size; i++) begin
q3.push_front( i );
$display($stime,,,"q3[%0d] :: q3 = %p", i , q3);
end
endmodule
仿真log:
0 empty dq1 size = 0 //empty queue size
0 dq1 SIZE = 3 //size after providing values to frst three elements
0 dq1='{'h0, 'h1, 'h2} //assigned frst three elements
0 After Insert dq1 SIZE = 4 //Insert value 3 at index 3.
0 dq1='{'h0, 'h1, 'h2, 'h3} //shows inserted value
0 After delete dq1 SIZE = 3 //delete value at index 3
0 dq1='{'h0, 'h1, 'h2} //shows dq1 after deletion
0 dq1 pop front = 0 //pop the front index of the queue
0 dq1='{'h1, 'h2} //dq1 after element at index 0 is gone (popped)
0 dq1 pop back = 2 //pop the back (last) index of the queue
0 dq1='{'h1} //the last index/value is gone
0 push front dq1='{'h4, 'h1} //push at the front of the queue (value 4)
0 push back dq1='{'h4, 'h1, 'h5} //push at the end of the queue (value 5)
上面我们通过队列dq1展示了push和pop的行为。然后我们声明了有界队列q3,最大的index限制是5,所以这个队列最大的size是6.
将队列q3初始化为{0,1,2,3,4,5},然后pop超过6次。
q3_size = q3.size + 5; //size > q3 size //underfow : pop from index 6,7,8,9,10 – run time Warning //Queue 'q3' is empty after index 5. for (int i = 0; i < q3_size; i++) $display($stime,,,"q3[%0d] = %0d", i, q3.pop_front( ) ); end
这个时候仿真器会上报warnnin:
# RUNTIME: Warning: RUNTIME_0219 testbench.sv (54): Cannot pop from an empty queue.
审核编辑:汤梓红
全部0条评论
快来发表一下你的评论吧 !