Fail to cast of enum

Hi,

Inside a verification driver I want to randomize the value of the address, opcode and opcode_size when req and grant are low (no valid access)
Here is my code:



typedef enum logic [5:0] {LOAD ='b000001, STORE ='b000010, INVAL ='b111000, PURGE ='b001000, .... } opcode_t;

...
     while ((req == 0) || (grant == 0))
        begin
          @(posedge clock)
            vif.addr        <= $urandom_range(32'hffffffff, 0);
            vif.opcode      <= $urandom_range(16, 0);
            vif.opcode_size <= $urandom_range(16, 0);
        end

However I am struggling to pick a random value for opcode

Here is what I have tried so far:

  1. vif.opcode <= $urandom_range(1,0) ? opcode.last : opcode.first;
    Compile OK … but restricted to two values of opcode

  2. vif.opcode <= opcode[ $urandom_range(opcode.num - 1,0) ];
    An enum variable ‘vif.opcode’ may only be assigned the same enum typed variable or one of its values. Value ‘vif.opcode[$urandom_range(opcode.num()-1,0)]’ requires an explicit cast.

  3. vif.opcode <= opcode[ 1 ];
    An enum variable ‘vif.opcode’ may only be assigned the same enum typed variable or one of its values. Value ‘opcode[1]’ requires an explicit cast.

  4. vif.opcode <= opcode.randomize();
    ** Fatal: (SIGSEGV) Bad handle or reference.

  5. vif.opcode <= $cast( opcode, 1);
    An enum variable ‘vif.opcode’ may only be assigned the same enum typed variable or one of its values. Value ‘$cast(,1)’ requires an explicit cast.

  6. vif.opcode <= vif.opcode’( $urandom_range(opcode.num() - 1, 0) );
    Error: Width cast must be constant.

  • What am I doing wrong ?
  • How can I randomize opcode I am stuck ! Any ideas ?

Thank you

In reply to Ep1c F4iL:

You cannot assign an enum variable with an integer without an explicit cast. $urandom_range returns an integer, and the result of randomize is a true or false bit indicating if the constraints could be met.
You probably want

const opcode_t opcode = LOAD;
vif.opcode = opcode.next($urandom_range(16));

A simpler solution would be

opcode_t opcode;
...
void'(randomize(opcode));
vif.opcode <= opcode;

In reply to dave_59:

Dave,

Both solutions are working … I have opted for the first solution.

Thank you