write a code to generate an array of size 12 with { 0,0,0,1,1,1,0,0,0,1,1,1 }
In reply to Subbi Reddy:
Your original question didn’t state that requirement. Please ensure that your questions are sufficiently stated so that an answer which meets your requirements can be provided.
class array_class;
rand bit ary[];
function new();
endfunction
endclass
module testbench();
array_class my_array_class;
initial begin
my_array_class = new();
if (!my_array_class.randomize() with {ary.size == 12;
ary[ 0] == 1; ary[ 1] == 1; ary[ 2] == 1;
ary[ 3] == 0; ary[ 4] == 0; ary[ 5] == 0;
ary[ 6] == 1; ary[ 7] == 1; ary[ 8] == 1;
ary[ 9] == 0; ary[10] == 0; ary[11] == 0;
})
$display("Randomization failure");
end
endmodule
please confirm,The below is correct for generic purpose?
`define MAX_ELEMENTS 12
`define COL_ELEMENTS 3
class array_class;
rand bit ary[][];
function new();
endfunction
constraint arr_p { ary.size == (`MAX_ELEMENTS /`COL_ELEMENTS);
foreach(ary[i]) {
ary[i].size ==3;
foreach(ary[i][j]) {
if(i%2==0)
ary[i][j]==0;
else
ary[i][j]==1;
}
}
}
endclass
module testbench();
array_class my_array_class;
initial begin
my_array_class = new();
my_array_class.randomize();
$display("%p",my_array_class.ary);
end
endmodule
In reply to Subbi Reddy:
Please use code tags making your code easier to read. I have added them for you.
We don’t know what your requirements are, so no one can tell you if it is correct or not.
In reply to dave_59:
In reply to Subbi Reddy:
Please use code tags making your code easier to read. I have added them for you.
We don’t know what your requirements are, so no one can tell you if it is correct or not.
I will follow from next time onwards.
Thank you @dave_59
In reply to Subbi Reddy:
I’m I to assume that for each nth row if n is odd, all the elements in that row are 1 and 0 if n is even?
In reply to Subbi Reddy:
Requirement is not very clear. I am assuming that requirement is to randomize 1-D array and repeat 1 and 0 in fixed order.
class arr_val#(MAX_ELEMENT=12, REPEAT_ELEMENT=3);
rand bit arr[MAX_ELEMENT];
constraint val_c {
foreach(arr[i]) { arr[i] == ((i/REPEAT_ELEMENT)%2); }
}
endclass
module top();
arr_val av=new();
initial begin
av.randomize();
$display(av);
end
endmodule
/*
OUTPUT:
'{arr:'{'h0, 'h0, 'h0, 'h1, 'h1, 'h1, 'h0, 'h0, 'h0, 'h1, 'h1, 'h1} }
*/
In reply to Alokpati:
In reply to Subbi Reddy:
Requirement is not very clear. I am assuming that requirement is to randomize 1-D array and repeat 1 and 0 in fixed order.
class arr_val#(MAX_ELEMENT=12, REPEAT_ELEMENT=3);
rand bit arr[MAX_ELEMENT];
constraint val_c {
foreach(arr[i]) { arr[i] == ((i/REPEAT_ELEMENT)%2); }
}
endclass
module top();
arr_val av=new();
initial begin
av.randomize();
$display(av);
end
endmodule
/*
OUTPUT:
'{arr:'{'h0, 'h0, 'h0, 'h1, 'h1, 'h1, 'h0, 'h0, 'h0, 'h1, 'h1, 'h1} }
*/
This Format also correct.