Can you add/remove more than 1 item from a queue?
Can you add more than one item to a SystemVerilog queue using the concatenation operator? Yes, you can. Here's some code:
module M;
initial begin
// Here's a queue, initialised with 3 members. (And a maximum
// index of 8 - more on that story later).
int i, numbers[$:8] = {1,2,3};
// Let's add a member to the front and another to the back
// using concatenation.
numbers = {numbers, 4}; $display("numbers= %p", numbers);
numbers = {0, numbers}; $display("numbers= %p", numbers);
// I told you you couldn't do this. I don't know where I got
// that idea from. (I do remember trying and it not working).
// Anyway, it works.
numbers = {numbers, 5, 6}; $display("numbers= %p", numbers);
numbers = {-2, -1, numbers}; $display("numbers= %p", numbers);
// We know we can do this.
numbers = numbers[1:$]; $display("numbers= %p", numbers);
numbers = numbers[0:$-1]; $display("numbers= %p", numbers);
// I was asked whether we could do this. It turns out we can
// and given the above shocking revelation, that's hardly
// surprising.
numbers = numbers[2:$]; $display("numbers= %p", numbers);
numbers = numbers[0:$-2]; $display("numbers= %p", numbers);
// So, what happens when we exceed the index?
numbers = {-2, -1, 0, numbers, 4, 5, 6}; $display("numbers= %p", numbers);
numbers = {numbers, 7}; $display("numbers= %p", numbers);
numbers = {-3, numbers}; $display("numbers= %p", numbers);
numbers.insert(3,0); $display("numbers= %p", numbers);
// It turns out that stuff just falls off the back, but you do get a
// warning in Incisive. Other simulators behave differently.
end
endmodule
https://www.edaplayground.com/x/59cR
A customer asked me about this. I didn't know the answer, so I wrote a few lines of code on EDA Playground. EDA Playground is great for that, because it's always on. You don't have to queue for licences, wait for EDA tools to start, create new files, fire up editors...
yes you can, see the following code
ReplyDeletemodule queue_data();
// Queue is declated with $ in array size
integer queue[$] = { 0, 1, 2, 3, 4 };
integer i;
initial begin
$display ("Initial value of queue");
print_queue;
// Insert new element at begin of queue
queue = {5, queue};
$display ("new element added using concate");
print_queue;
// Insert using method at begining
queue.push_front(6);
$display ("new element added using push_front");
print_queue;
// Insert using method at end
queue.push_back(7);
$display ("new element added using push_back");
print_queue;
// Using insert to insert, here 4 is index
// and 8 is value
queue.insert(4,8);
$display ("new element added using insert(index,value)");
print_queue;
// get first queue element method at begining
i = queue.pop_front();
$display ("element poped using pop_front");
print_queue;
// get last queue element method at end
i = queue.pop_back();
$display ("element poped using pop_end");
print_queue;
// Use delete method to delete element at index 4 in queue
queue.delete(4);
$display ("deleted element at index 4");
print_queue;
#1 $finish;
end
task print_queue;
integer i;
$write("Queue contains ");
for (i = 0; i < queue.size(); i ++) begin
$write (" %g", queue[i]);
end
$write("\n");
endtask
endmodule