Maybe it’s a tool thing, but in Edit code - EDA Playground
I get an error with Incisive and CVS.
In this forum we want to avoid talking about tools.
My first solution avoids this const’ thing,
I agree, the const’ thng is “elegant”, but use what makes it work.
Ben Cohen http://www.systemverilog.us/ben@systemverilog.us
For training, consulting, services: contact Home - My cvcblr
Your code looks perfect but here, user needs to take care toggling of “t1.w” each time after calling randomization. So I teaked your code and modified as below. Please let me know if below code has any side effect,
program blk;
class test;
rand bit [2:0] x;
// odd_even bit to indicate whether siganl should be odd or even after randomization
// odd_even == 1 then even data generated
// odd_even == 0 then odd data generated
static bit odd_even;
//pre_randomize function will be called every time before randomize function
function pre_randomize();
$display("Inside pre_randomize");
odd_even = ~odd_even;
endfunction
constraint cons{
(odd_even == 1) -> x%2 == 0;
(odd_even == 0) -> x%2 != 0;
}
endclass : test
initial begin
test t1;
repeat(10) begin
t1 = new();
void'(t1.randomize());
$display ("t1.x = %0d t1.odd_even = %0d", t1.x, t1.odd_even);
end
end
endprogram
@Omran,
Above code will randomize signal in class itself. Please let me know if it works for you.
In reply to Agrawal Priyank:
This works for me, BUT with some minor mods.
I like the use of the pre_randomize
Tool recommended use of void in the function
Use module instead of program. There are many many discussions in this forum on why you should NOT use the program. SEE Verification Horizons - Siemens Software
It is not a good methodology to use
void'(t1.randomize());
//Instead, use something like
if (! t1.randomize()) `uvm_error("MYERR", "This is a randomize error")
// Modify your uvm_erro tag and message as you see fit
Simulated model available at
import uvm_pkg::*;
`include "uvm_macros.svh"
module blk;
class test;
rand bit [3:0] x;
// odd_even bit to indicate whether siganl should be odd or even after randomization
// odd_even == 1 then even data generated
// odd_even == 0 then odd data generated
static bit odd_even;
//pre_randomize function will be called every time before randomize function
function void pre_randomize();
$display("Inside pre_randomize");
odd_even = ~odd_even;
endfunction
constraint cons{
(odd_even == 1) -> x%2 == 0;
(odd_even == 0) -> x%2 != 0;
}
endclass : test
initial begin :i
test t1;
repeat(10) begin : r
t1 = new();
// void'(t1.randomize()); // NOT A GOOD STYLE
if (! t1.randomize()) `uvm_error("MYERR", "This is a randomize error")
$display ("t1.x = %0d t1.odd_even = %0d", t1.x, t1.odd_even);
end : r
end : i
endmodule