SystemVerilog - 不更新成员类的值

时间:2016-12-15 06:07:39

标签: arrays pass-by-reference system-verilog

我有以下基类:

class base_transaction extends uvm_sequence_item();

   int rand_int_list [$];   
   bit rand_bit_list [$];


   bit [31:0] data [$];

   //uvm_object_utils\
   `uvm_object_utils(base_transaction)

   //constructor
   function new(string name = "base_transaction");
      super.new(name);
   endfunction: new

   //add to list functions
   function void add_rand_int(ref int  mem);
      $display("array size is %d before push", rand_int_list.size());      
      rand_int_list.push_back(mem);
      $display("array size is %d after push", rand_int_list.size());
      //rand_int_list[rand_int_list.size()] = mem;
   endfunction: add_rand_int

   function void add_rand_bit(ref bit mem);
     // rand_bit_list.
      rand_bit_list[rand_bit_list.size()] = mem;      
   endfunction: add_rand_bit   


   //function my_randomize
   function int my_randomize(int seed);
      int temp, success;
      if (rand_int_list.size > 0)
    begin
       for (int i = 0; i < rand_int_list.size(); i++)
         begin
        // temp = $urandom_range(1,11);
        temp = (($urandom(seed)) + 1);
        rand_int_list[i] = temp - 1;
        success = (temp && success);
         end
       if(success == 0)
         return 0;     
    end
      if (rand_bit_list.size() > 0)
    begin
       for (int i = 0; i < rand_bit_list.size(); i++)
         begin
        // temp = $urandom_range(1,11);
        temp = (($urandom(seed)) + 1);
        rand_bit_list[i] = temp - 1;
        success = (temp && success);
         end
       if(success == 0)
         return 0;
       else
         return 1;
    end
   endfunction: my_randomize

endclass: base_transaction

我有以下扩展基类的类:

class tx_transaction extends base_transaction;
   bit [15:0]  data_xi;
   bit [15:0]  data_xq;
   int mem_int = 2, mem_int_a = 5;  //TODO- delete

   //uvm_object_utils\
   `uvm_object_utils(tx_transaction)

   //constructor
   function new(string name = "tx_transaction");
      super.new(name);
   endfunction: new


   function void add_rand_macro();
      add_rand_int(mem_int);
      add_rand_int(mem_int_a);
   endfunction: add_rand_macro

   //TODO - DELETE
   function void foo();
      $display("rand mem int: %d %d", mem_int, mem_int_a);
   endfunction: foo

endclass: tx_transaction

我从序列中调用以下函数(tx_trx - 是tx_transaction实例,我是迭代器):

     tx_trx.add_rand_macro();
     tx_trx.my_randomize(i+1);
     tx_trx.foo();

由于某种原因,foo函数显示全部为2 5(memt_int,mem_int_a),尽管这些值由my_randomize函数更新(成员类通过ref传递的数组是随机的)

1 个答案:

答案 0 :(得分:0)

这里的问题是你的rand_int_list是一个包含int值的队列,不引用int varibles。因此,虽然您已将mem作为ref传递,但它的当前值仍会存储在队列中,而不是它的引用。

解决此问题的方法是跟踪推回值的位置,然后使用该位置检索随机值。

您的代码可能如下所示。

//add to list functions
function int add_rand_int(ref int  mem);
  $display("array size is %d before push", rand_int_list.size());      
  rand_int_list.push_back(mem);
  $display("array size is %d after push", rand_int_list.size());

  return (rand_int_list.size() - 1);
endfunction: add_rand_int

class tx_transaction extends base_transaction;
   bit [15:0]  data_xi;
   bit [15:0]  data_xq;
   int mem_int = 2, mem_int_a = 5;  //TODO- delete
   int location[string]; // An associative array to hold location of variables in rand_int_list queue

   //uvm_object_utils\
   `uvm_object_utils(tx_transaction)

   //constructor
   function new(string name = "tx_transaction");
      super.new(name);
   endfunction: new


   function void add_rand_macro();
      location["mem_inst"] = add_rand_int(mem_int);
      location["mem_inst_a"] = add_rand_int(mem_int_a);
   endfunction: add_rand_macro

   //TODO - DELETE
   function void foo();
      $display("rand mem int: %d %d", rand_int_list[location["mem_int"]], rand_int_list[location["mem_int_a"]]);
   endfunction: foo

endclass: tx_transaction