Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Use of Queues in System Verilog

727 views
Skip to first unread message

rajat...@gmail.com

unread,
Mar 5, 2007, 9:25:23 AM3/5/07
to
Hi all,
I am trying to use System verilog queues; snippets from my code
++---------++++---------++++---------++++---------++++---------+++
+---------++

Here is a class -

class read_trans;

//Address Parmaters
bit [9:0] row_addr; //DDR Row Address
bit [9:0] col_addr; //DDR Column Address
bit [2:0] bank_addr; //DDR Bank Address
bit [1:0] chip_sel; //DDR Chip Select

//Transaction ID
bit [31:0] transaction_id;

//Display Function
function display;

$display("%%Transaction ID %d",transaction_id);
$display("%%Row Address %x",row_addr);
$display("%%Column Address %x",col_addr);
$display("%%Bank Address %x",bank_addr);
$display("%%Chip Select %x",chip_sel);

endfunction : display


endclass : read_trans
++---------++++---------++++---------++++---------++++---------+++
+---------++

I want to push and pop for the queue which is a queue to read_trans
objects -

module queue;

read_trans read_queue[$] ={};
read_trans r_op;
int j;


initial begin
r_op = new;

for(j=0; j < 3; j++)begin
r_op.transaction_id = j;
r_op.col_addr = j;
r_op.row_addr = j;
r_op.bank_addr = j;
r_op.chip_sel = j;
r_op.display;
read_queue.push_front(r_op);
end

// foreach(read_queue[j])
// read_queue[j].display;


$display();

//`ifdef DEBUG
for(j=0; j<3; j++)begin
r_op=read_queue.pop_back;
r_op.display;
$display();
end
//`endif


end

endmodule

++---------++++---------++++---------++++---------++++---------+++
+---------++

However when I run this with vcs I get -


%Transaction ID 0
%Row Address 000
%Column Address 000
%Bank Address 0
%Chip Select 0
%Transaction ID 1
%Row Address 001
%Column Address 001
%Bank Address 1
%Chip Select 1
%Transaction ID 2
%Row Address 002
%Column Address 002
%Bank Address 2
%Chip Select 2

%Transaction ID 2
%Row Address 002
%Column Address 002
%Bank Address 2
%Chip Select 2

%Transaction ID 2
%Row Address 002
%Column Address 002
%Bank Address 2
%Chip Select 2

%Transaction ID 2
%Row Address 002
%Column Address 002
%Bank Address 2
%Chip Select 2


When I pop off the queue, I get only the last object I pushed.
Is there something I am doing wrong ????

Thansk for your help,

Rajat Mitra

Jonathan Bromley

unread,
Mar 5, 2007, 10:29:25 AM3/5/07
to
On 5 Mar 2007 06:25:23 -0800,
rajat...@gmail.com wrote:

[...]


>When I pop off the queue, I get only the last object I pushed.
>Is there something I am doing wrong ????

Yes!

The guts of your "push four transactions" loop is...

initial begin
r_op = new;

for(j=0; j < 3; j++)begin
r_op.//modify data members of r_op
read_queue.push_front(r_op);
end

NOTE CAREFULLY that you invoke new() only once, BEFORE
the loop. Consequently, you have only one transaction object,
but the queue contains four separate references to it.

In general, whenever you store an object for future use, it is
a good idea (good==paranoid, since this is engineering!)
to store a *completely new copy* of the object. Give your
transaction object a copy() method that makes a clone
of itself, and then

read_queue.push_front(r_op.copy());

And now, whatever you do to r_op in future, the copy
that you saved on the queue is untouched.
--
Jonathan Bromley, Consultant

DOULOS - Developing Design Know-how
VHDL * Verilog * SystemC * e * Perl * Tcl/Tk * Project Services

Doulos Ltd., 22 Market Place, Ringwood, BH24 1AW, UK
jonathan...@MYCOMPANY.com
http://www.MYCOMPANY.com

The contents of this message may contain personal views which
are not the views of Doulos Ltd., unless specifically stated.

Ajeetha (www.noveldv.com)

unread,
Mar 5, 2007, 12:35:28 PM3/5/07
to
Hi,

> In general, whenever you store an object for future use, it is
> a good idea (good==paranoid, since this is engineering!)
> to store a *completely new copy* of the object. Give your
> transaction object a copy() method that makes a clone
> of itself, and then
>
> read_queue.push_front(r_op.copy());
>


Such a common problem I see with many SV newbies here. I wish I have
a TB linter to warn/catch this. Atrenta? Synopsys? Cadence? (Does
Mentor have a Linter?)

Ajeetha, CVC
www.noveldv.com

Jonathan Bromley

unread,
Mar 5, 2007, 12:55:42 PM3/5/07
to
On 5 Mar 2007 09:35:28 -0800, "Ajeetha (www.noveldv.com)"
<aje...@gmail.com> wrote:

Ajeetha,

Does the same thing happen to neophyte Java programmers?
I know it's also a standard hazard in 'e'.

I agree it's a very common pitfall. But I'm not sure you could
ever solve it with a linter. There are times when you really
*want* multiple references to an object; the code patterns for
that look very similar to the standard "newbie error" case.

In a similar way, the desired behaviour for an object's "clone-me"
method is not necessarily a full deep copy. Some object references
might be to a parent object, or an originating data stream, or
some testbench-wide service such as messaging. Such references
need to be shallow-copied. Getting the copy() method exactly right
takes careful thought when your data structures are complex.

It's hard - very hard indeed - until you have a clear picture in
your head. That's what we spend our time trying to help
people achieve; I suspect you do something rather similar :-)

0 new messages