Below is an example of constraints, where the first implementation doesn’t work, but the second is fine.
I was under the impression that the first implementation should work as well.
Any hint would be appreciated.
Alex
class my_class;
rand my_req_class my_req;
function new();
$display("NEW!!!");
endtask
constraint my_req_length_cnstr { // doesn't work
my_req.length inside { [1:4] };
};
task my_task();
my_req = new();
my_req.randomize() with {length == 5;}; // works
endtask
endclass
Thank you for providing more context. Your problem is because you put the constraint in single_protocol_sequence_c, but the `uvm_do_with macro (which we recommend against using) hides the fact that you are just randomizing my_ip_req. You need randomize single_protocol_sequence_c.
virtual task body();
req = request_c::type_id::create("req",this);
start_item(req);
if(!randomize()) `uvm_error("body","Randomization 1 failed");
finish_item(req);
`uvm_info("burst_length = %d\n", my_ip_req.burst_length); // prints some number inside 1-4096
req = request_c::type_id::create("req",this);
start_item(req);
if(!randomize() with (req.burst_length == 1)) `uvm_error("body","Randomization 2 failed");
finish_item(req);
`uvm_info("burst_length = %d\n", my_ip_req.burst_length); // prints 1
endtask : body
BTW, there is no need to declare a separate my_ip_req; req is already declared in the uvm_sequence with the parameterized type.
[br][br]