I am trying to write e constraint to generate a memory transaction with
First transaction always write
read addr = write addr
Not sure what’s wrong with the below code.
class mem;
rand int addr;
rand int w_r; //w_r=0 write
int q[$];
function void post_randomize();
int sz;
int idx;
q.push_back(addr);
sz = q.size();
idx = $urandom_range(0, sz-1);
if(sz==1) w_r = 0;
if(w_r==1) addr = q[idx]; //if read use addr from Q
endfunction
endclass
module test;
mem a;
initial begin
a=new;
repeat(20) begin
a.randomize();
$display("sz=%0d addr=%x w_r=%x", a.q.size(), a.addr, a.w_r);
end
end
endmodule
I dont see the exact output by running your code on EDA playground… but i have made some assumptions for your requirements. Below meets ur requirements.
class mem;
rand int addr;
int r_addr;
rand bit w_r;//w_r=0 write
bit temp=1;
int q[$];
constraint ab {temp != w_r;
w_r -> r_addr == addr;
}
function void post_randomize();
temp = w_r;
r_addr = addr;
endfunction
endclass
module test;
mem a;
initial begin
a=new;
repeat(20) begin
a.randomize();
$display("sz=%0d addr=%x w_r=%x", a.q.size(), a.addr, a.w_r);
end
end
endmodule
Thanks @KillSteal
I could see the problem you mentioned with ncsim.
The problem is with “read_addr_cons” not sure what tough.
I did that in post_randomization and it looks good. Not sure if that’s the optimized way.
class mem;
rand int addr;
rand int w_r; //w_r=0 write
int q[$];
constraint w_r_cons {(write_addr_q.size()==0) -> (w_r == 0) ;};
function void post_randomize();
int sz;
int idx;
q.push_back(addr);
sz = q.size();
idx = $urandom_range(0, sz-1);
//if(sz==1) w_r = 0;
if(w_r==1) addr = q[idx]; //if read use addr from Q
endfunction
endclass
module test;
mem a;
initial begin
a=new;
repeat(20) begin
a.randomize();
$display("sz=%0d addr=%x w_r=%x", a.q.size(), a.addr, a.w_r);
end
end
endmodule