Continuous Assignment of Class Property

50 Views Asked by At

Title says it all. I'm wondering if there is a way to continuously assign a class property in SystemVerilog.

Something along the lines of:

class test;
    logic test_var [2];
    logic foo; 
 
    function new(); 
        this.foo         = '0; 
        this.test_var[0] = '0;
        assign this.test_var[1] = foo; // continous assignment, however that is possible 
    endfunction

    task updateFoo(input logic temp_foo);
        this.foo = temp_foo; 
    endtask 

    task readTestVar(); 
        $display("%b and %b", this.test_var[0], this.test_var[1]); 
    endtask

endclass

initial begin
    test ex = new();  // Foo and test_var initialize to zero. 
    ex.readTestVar(); // Prints "0 and 0" 
    ex.updateFoo(1);  // Foo becomes 1, so does test_var[1]
    ex.readTestVar();  // Prints "0 and 1"
end 

I've searched some testbooks and the internet, but haven't found anything useful. I suppose I know how to solve this the long way around, but wondering if there is a short way.

2

There are 2 best solutions below

0
dave_59 On BEST ANSWER

When using classes, you have to fork your own process. You cannot use always or continuous assignments.

module top;
  class test;
    logic test_var [2];
    logic foo; 
 
    function new(); 
      this.foo         = '0;
      this.test_var[1] = '0;
      fork
        forever @foo this.test_var[1] = foo; // continous assignment
      join_none
    endfunction

    function void updateFoo(input logic temp_foo); // Dont use tasks unless they need to consume time
        this.foo = temp_foo; 
    endfunction

    function void readTestVar(); 
        $display("%b and %b", this.test_var[0], this.test_var[1]); 
    endfunction

endclass

test ex; // static variable declaration moved outside of procedural block
initial begin // delays added to prevent race conditions. 
    ex = new();  // Foo and test_var initialize to zero.
    #1 ex.readTestVar(); // Prints "0 and 0" 
    #1 ex.updateFoo(1);  // Foo becomes 1, so does test_var[1]
    #1 ex.readTestVar();  // Prints "0 and 1"
end 
endmodule
0
toolic On

No.

That code generates a syntax error on every simulator I try it on, and the error messages are pretty clear.

Error-[DTINPCNS] Dynamic type in non-procedural context
"assign this.test_var[1] = this.foo;"
  Argument: this.test_var
  Class data is not allowed in non-procedural context.

Omitting the assign keyword is probably what you are looking for:

    this.test_var[1] = foo; // continous assignment, however that is possible