0

I met a problem when I trying to modify a queue of class in systemverilog function.

Here are the codes:

module my_module;

class dscr;
   logic mode;

   function void print_dscr;
        $display("mode = %d", this.mode);
   endfunction
endclass

dscr a_dscr_q[$];
dscr b_dscr_q[$];

initial begin 
    descriptor_decode(0, a_dscr_q);
    for (int I=0; I<a_dscr_q.size(); i++)
       a_dscr_q[i].print_dscr();

    descriptor_decode(1, b_dscr_q);
    for (int I=0; I<a_dscr_q.size(); i++)
       a_dscr_q[i].print_dscr();
    for (int I=0; I<b_dscr_q.size(); i++)
       b_dscr_q[i].print_dscr();
end


function void descriptor_decode(logic mode, ref dscr dscr_q[$]);
    dscr dscr_dec = new;
    dscr_dec.mode = mode;

    dscr_q.pushback(dscr_dec);
endfunction

endmodule

I am trying to create different class queue in function "descriptor_decoder", with different value of input mode. In function, I firstly create a new dscr class and then push it to a class queue. However the simulation result are:

mode = 0
mode = 1
mode = 1

The first time I call the function, it did push back the correct class into a_dscr_q. But the second function call, it seems the class is push back into both a_dscr_q and b_dscr_q. I am quite confused, What happened in here?

1 Answer 1

2

Your code was made illegal syntax in the IEEE 1800-2009 LRM because of the very problem you are experiencing. Most tools now report this as an error.

Your descriptor_decode is function with a static lifetime, and the dscr_dec variable declared inside it has a static lifetime as well.

You are not allowed to have an initialization on a variable whose lifetime is implicitly static and has the option to be declared automatic. This is because unlike most programming languages, the default lifetime of variables in a SystemVerilog function is static, and initialization of static variables happens once before time 0, not each occurrence of calling the function. In your example, you are expecting dscr_dec to behave as an automatic. So you need to make one of the following code changes:

  • explicitly declare dscr_dec automatic
  • declare the function automatic, which makes variables declared inside it implicitly automatic
  • declare the module automatic, which makes functions declared inside it implicitly automatic
  • split the declaration and initialization do that the initialization happens when the function gets called.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.