0
votes

I'd like to store a reference to an array/queue inside a class. It's doesn't seem possible to do this, though.

I'd like to do something like this:

class some_class;

  // class member that points to the 'q' supplied as a constructor arg
  ??? q_ref;

  function new(ref int q[$]);
    this.q_ref = q;
  endfunction

endclass

If q_ref is merely defined as int q_ref[$], then the assignment operator will create a copy, which isn't what I want. I'd like changes in 'q' to be visible inside the class.

Is there some hidden section in the LRM that shows how this can be done?

I'm not looking for the obvious "you have to wrap the array/queue in a class answer", but for something that allows me to interact with code that uses native arrays/queues.

2
you can create a class which contains an array and then store reference to this class. - Serge
@Serge Not what I want. I want to have a regular queue variable and to be able to wrap it in another class. The class object should see changes done to the queue. Changes inside the object should also propagate to the queue. - Tudor Timi
do you have an example? - Serge
@Serge I've updated the question. - Tudor Timi
I guess that is not possible to reference the queue itself. - Karan Shah

2 Answers

1
votes

There are only three variable types in SystemVerilog that can store references: class, event, and virtual interfaces variables.

You have to wrap the array/queue as a member in a class object. Then, any method of that class can be used in an event expression. Any change to a member of the class object causes a re-evaluation of that method. See the last paragraph and example in section 9.4.2 Event control of the 1800-2012 LRM.

0
votes

So, the only solution for you would be to wrap the queue in a class. The latter is always assigned by a reference, as in this example:

class QueueRef #(type T = int);
   T queue[$];
   function void push_back(T t);
      queue.push_back(t);
   endfunction // push_back
endclass // Queue

class some_class;
  QueueRef q_ref;
  function new(QueueRef q);
     this.q_ref = q;
  endfunction
endclass

program test;
   QueueRef q = new;
   some_class c = new (q);
   initial begin 
      q.push_back(1);
      q.push_back(2);
      $display(c.q_ref.queue);
   end
endprogram // test