Jump to content

systemverilog class extension issue

Recommended Posts

Hi All,

I have an example to test my class extensions but something I don't understand clearly..

In below example, I push my extended class instance "TEST_REG_1" into "test_base" class queue. and simulation result shows that Line 33 is error ..

So I think not really extended class instance is put into parent class queue. From the queue, I can still get parent class field "num", I can't access to child class field "test_reg_1".

So who knows how to modify below codes to make it working?




4 class test_base;

5 int num = 1;


7 endclass


9 class test_reg_1 extends test_base;

10 int test_reg_1 = 1;

11 endclass


13 class test_reg_2 extends test_base;

14 int test_reg_2 = 1;

15 endclass


17 class test;

18 test_reg_1 TEST_REG_1;

19 test_reg_2 TEST_REG_2;


21 test_base list[$];


23 function new();

24 TEST_REG_1 = new();

25 list.push_back(TEST_REG_1);


27 test_my_reg();

28 endfunction


30 task test_my_reg();

31 $display("num = %d", list[0].num);


33 $display("test_reg_1 = %d", list[0].test_reg_1);

34 endtask


36 endclass


38 program main;


40 test t ;


42 initial begin

43 t = new();

44 end


46 endprogram

Best wishes,

Link to comment
Share on other sites

I know that if I create a class ins after Line 30.

like : test_reg_1 my_cast_reg;

$cast(my_cast_reg, list[0]);

This will sovle the issue, but I don't want to do this way.. because if I have 1000 sub-class of test_base.. I need to instance 1000 test_reg_xx , and based on maybe name or something else to implement 1000 $cast() ...it doesn't make sense to me.

So how do you deal with these kind of issues?

Link to comment
Share on other sites

Normally what you do is define a virtual method in the base class to access the additional fields in the extended class - you never access class members directly. These are the do_copy, do_compare, and do_print, etc. virtual methods in the UVM.

You better explain what you are eventually trying to do because if every class extension has exactly one extra member, there is really no need to extend the class, just put the test variable in the base class. Or why not use a simple array?

Link to comment
Share on other sites

your list is of elements of test_base. it can hold arbitrary elements being at least a test_base. however you can only access the properties of test_base directly (since this is guaranteed). to access properties of derived members you either have to use a cast or use virtual access functions.


Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • Create New...