Jump to content

Class fields dependency


Recommended Posts

Hi all. I have a question regarding definition of class members:

Is it possible to create a field in a class which existence will depend on the value of other field?

For example, something like:

typedef enum {RED, GREEN, BLUE} color_type;

class my_class;

color_type my_color;

// Only when my_color is RED, I want this class to have another field called my_data

if (my_color == RED) then (logic [7:0] my_data);

...

endclass: my_class

(sorry if any syntax errors)

I know this capability exists in e language (by using "when" construct) but I have not been able to find anything similar for SystemVerilog.

Thanks.

Link to comment
Share on other sites

No You cannot declare variables like this at run time.

NOTE: you cannot use logic to declare class members.

You could use `defines to declare variables at compile time

eg:

`define MY_COLOR_IS_RED 1
class my_class;
     color_type my_color;
     // Only when my_color is RED, I want this class to have another field called my_data
     `ifdef MY_COLOR_IS_RED
     rand bit [7:0] my_data;
     `endif
endclass

Not sure what your intentions are here, but this may be enough for you.

class my_class;
     color_type my_color;
     rand bit [7:0] my_data;
     constarint c_my_data_valid_only_when_color_red {
          ( (my_color != RED) -> (my_data == 0) );
     }
endclass
Edited by krb
make it clearer
Link to comment
Share on other sites

E's when inheritance/AOP with determinant can be achieved using a "flat structure" in SV - i.e. declare all fields and guard them during pack/unpack/display etc.

It is not straight forward and requires some re-thinking especially if you are a die-hard Specman fan.

Regards

Ajeetha, CVC

www.cvcblr.com/blog

Link to comment
Share on other sites

I wasn't mentioning to the use of logic data type, but to logic in declaring class members.

I also changed your "logic [7:0] my_data" to bit type because the rand modifier only works on 2-state data types (bit).

You don't have to use the rand modifier, as I did in the example.

The constraint block will have no meaning without the rand.

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.

Guest
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...