how to write a constraint to randomize any 5 bits out of a 32 bit variable???
In reply to surya narayana Gutha:
my solution
// constraint to randomize any 5 bits out of a 32 bit variable?
// select 5 bits to randomize
class C;
bit[31:0] v;
rand bit[4:0] b;
rand bit[31:0] pick5;
constraint ct5 {$countones(pick5)==5;}
function void post_randomize();
automatic int j=0;
for(int i=0; i<=31; i=i+1) begin :for1
if(pick5[i]) begin
v[i]=b[j];
j=j+1'b1;
end
else v[i]=0;
end : for1
endfunction : post_randomize
endclass
//
import uvm_pkg::*; `include "uvm_macros.svh"
module top;
timeunit 1ns; timeprecision 100ps;
C c;
initial begin
c=new();
repeat(10) begin
if (!randomize(c)) `uvm_error("MYERR", "This is a randomize error")
$display("pick5= %b, b=%b, v=%b", c.pick5, c.b, c.v);
end
$stop;
end
endmodule
pick5= 00000110100000010000000000001000, b=00101, v=00000000100000000000000000001000
# pick5= 00000000001000000100100100100000, b=00011, v=00000000000000000000000100100000
# pick5= 00001001100000000000100000000010, b=10110, v=00001000100000000000100000000000
# pick5= 00000000010000010000000110000100, b=00000, v=00000000000000000000000000000000
# pick5= 00000000100000010100011000000000, b=00101, v=00000000000000000100001000000000
# pick5= 00010000000000010000001001000010, b=10000, v=00010000000000000000000000000000
# pick5= 01000000000000100010100000000100, b=00111, v=00000000000000000010100000000100
# pick5= 00110000010000000000101000000000, b=01100, v=00010000010000000000000000000000
# pick5= 10000000000010101000010000000000, b=11111, v=10000000000010101000010000000000
# pick5= 00101000010000100010000000000000, b=00000, v=00000000000000000000000000000000
Ben Cohen
http://www.systemverilog.us/ ben@systemverilog.us
For training, consulting, services: contact Home - My cvcblr
- SVA Handbook 4th Edition, 2016 ISBN 978-1518681448
- A Pragmatic Approach to VMM Adoption 2006 ISBN 0-9705394-9-5
- Using PSL/SUGAR for Formal and Dynamic Verification 2nd Edition, 2004, ISBN 0-9705394-6-0
- Real Chip Design and Verification Using Verilog and VHDL, 2002 isbn 978-1539769712
- Component Design by Example ", 2001 ISBN 0-9705394-0-1
- VHDL Coding Styles and Methodologies, 2nd Edition, 1999 ISBN 0-7923-8474-1
- VHDL Answers to Frequently Asked Questions, 2nd Edition ISBN 0-7923-8115
In reply to surya narayana Gutha:
You need to specify what happens to the bits that are not randomized, do they keep their previous value, or are they 0?
module top;
class A;
rand bit [31:0] value,mask;
constraint c_mask {$countones(mask) == 5; }
constraint c_vari {(~mask & value) == (~mask & const'(value));} // use (~mask & value) == 0) if unrandomized bits are 0
endclass : A
A a_h = new;
initial repeat(10) begin
if (!a_h.randomize()) $error("no solution");
$display("%b\n%b\n\n",a_h.mask,a_h.value);
end
endmodule
I like your solution.
Question:which solution is more compute efficient, the post randomize or the all constraints?
Ben