2

For the following class:

class CPacket;
  int offset;
  rand bit [3:0] sa, da;
  int lo = 2, hi = 10;

constraint LimitA { sa inside {[lo:hi]}; da inside {[lo:hi]}; }

function void pre_randomize(); if(this.offset) begin lo = lo + offset; hi = hi + offset; end endfunction endclass

If I want to run this:

CPacket pkt = new();
pkt.offset = 100;
pkt.randomize();

Will it fail because that offset value makes the new lo and hi define a range of values that does not match 4-bit vector width?

Will it succeed and take the 4 LSBs of the new lo and hi to define the range of values that sa and da can have?

What happens in this situation?

toolic
  • 8,262
  • 7
  • 24
  • 35
Chengineer
  • 107
  • 6

2 Answers2

3

The expressions used in a constraint behave the same as if they were used outside a constraint. There are only 16 possible values for sa and da and if you tried all of them (4'b0000 through 4'b1111), none of them would makes the inside expression return true for the example you show.

It's the same as if you wrote sa >= lo && sa <= hi. Verilog zero extends the shorter unsigned operand to match the width of the longer operand.

dave_59
  • 8,312
  • 1
  • 15
  • 27
2

randomize will fail because that offset value makes the new lo and hi define a range of values that does not match 4-bit vector width. sa can not be in the range of 102-110 because its valid range is 0-15. You can prove this by checking the return value of randomize: 1=pass, 0=fail.

From IEEE Std 1800-2017, section 18.6.3 Behavior of randomization methods:

If randomize() fails, the constraints are infeasible, and the random variables retain their previous values

You can confirm this by displaying the values before and after you call randomize:

module tb;
    CPacket pkt = new();
    initial begin
        pkt.offset = 100;
        $display("sa=%0d", pkt.sa);
        if (pkt.randomize()) begin
            $display("PASS");
        end else begin
            $display("FAIL");
        end
        $display("sa=%0d", pkt.sa);
    end
endmodule

You will see output similar to this:

sa=0
[Possible simulator-specific warnings about randomize failing]
FAIL
sa=0
toolic
  • 8,262
  • 7
  • 24
  • 35