Solve-Before Constraints

Hello, I am trying to run this code, but it’s not working as I have planned,

Here, I wanted that if load = 1, then we will generate data. But data is generated regardless of load. I have used solve before so that load is generated first, and according to it’s value, data will be generated. I have tested it with various tools in EDAP.

Is there anything wrong with my understanding with this code??


class piso_trans;

  logic resetn = 1;
  rand logic load;
  rand logic [7:0] data;
  rand logic LSB;

  constraint DATA_GEN {
    load dist {0:=1, 1:=1};
    if(load == 1) data inside {[0:8'hff]};
    solve load before data;
  }
endclass

module tb();
  piso_trans item;
  initial begin
    item = new();
    repeat (10) begin
      item.randomize();
      $display ("%p", item);
    end
    $finish;
  end
endmodule

In reply to Husni Mahdi:

A few things to note about your constraints :

  1. Randomization generates 2-state values only . This means although load is 4-state type , it would only have 2-state values 0 / 1 .
load dist {0:=1, 1:=1};

Hence this would be redundant as load would have these 2 values with equal probability even without the constraint .
2.

data inside {[0:8'hff]};

data is 8-bit variable which means the solution space varies from 0 to 8’hFF without the constraint as well .
Hence this constraint is redundant as well .
3. If load != 1 , data is unconstrained . This means data can take any value from it’s solution space.
Hence your observation :

But data is generated regardless of load.

solve load before data;

Even without this constraint the probability remains the same . Hence this would be redundant as well .

I believe your requirement is :


constraint DATA_GEN { if(load == 0 ) data == const'( data ) ; }

In reply to ABD_91:

Thankyou for the response,

But I’m confused as to when solve before is actually makes a difference, and when it does not? My thought process was, as data is generated according to the value of load, so it has to be resolved before data is generated.

In reply to Husni Mahdi:

I think you have a misunderstanding on how the if constraint works. It does not work like a procedural statement; it is a boolean expression:

!(load == 1) || data inside {[0:8'hff]}

data inside {[0:8'hff]} is true for all 256 possible values of data, so the value of load is irrelevant. There are 512 possible solutions of load and data , and load already has a 50/50 probability of being 0 or 1, with or without the **dist** or solve/before constraints in your example.

Now let’s modify your constraint to

if(load == 1) data inside {[0:8'h7f]}; // same as
!(load == 1) || data inside {[0:8'h7f]};

Now there are only 384 solutions because we have eliminated the 128 solutions where load is 1 and data is 'h80-'hFF. But now there are only 128 solutions where load is 1, and 256 where load is 0. Also realize that there will be 128 solutions where data is 0-h7f, and 256 solutions where data is 'h80-'hFF.

The solve before construct can be used to get the distribution of load to 50/50, or the dist constraint can be used to get the distribution to anything you want. Note that if you change the distribution of load, you are indirectly changing the distribution of data (to0 much math for me to go over here).

You might want to check out my DVCon paper here:

https://2020.dvcon-virtual.org/sites/dvcon20/files/2020-05/12_1_P.pdf