1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288
|
// DESCRIPTION: Verilator: Verilog Test module
//
// This file ONLY is placed under the Creative Commons Public Domain, for
// any use, without warranty, 2024 by PlanV GmbH.
// SPDX-License-Identifier: CC0-1.0
`define check_rand(cl, field) \
begin \
longint prev_result; \
int ok = 0; \
void'(cl.randomize()); \
prev_result = longint'(field); \
repeat(9) begin \
longint result; \
void'(cl.randomize()); \
result = longint'(field); \
if (result != prev_result) ok = 1; \
prev_result = result; \
end \
if (ok != 1) $stop; \
end
class unconstrained_packed_array_test;
rand bit [3:0] [2:0] [15:0] packed_array;
function new();
packed_array = '{default: '{default: '{default: 'h0}}};
endfunction
function void check_randomization();
`check_rand(this, this.packed_array)
endfunction
endclass
class unconstrained_unpacked_array_test;
rand bit [2:0] [15:0] unpacked_array [3][5];
rand int unpacked_array1 [9:3][4:8];
rand int unpacked_array2 [3:9][8:4];
function new();
unpacked_array = '{ '{default: '{default: 'h0}},
'{default: '{default: 'h1}},
'{default: '{default: 'h2}}};
unpacked_array1 = '{default: '{default: 0}};
unpacked_array2 = '{default: '{default: 0}};
endfunction
function void check_randomization();
foreach (unpacked_array[i]) begin
foreach (unpacked_array[i][j]) begin
// At the innermost packed level, invoke check_rand
`check_rand(this, this.unpacked_array[i][j])
end
end
foreach (unpacked_array1[i, j]) begin
`check_rand(this, this.unpacked_array1[i][j])
end
foreach (unpacked_array2[i, j]) begin
`check_rand(this, this.unpacked_array2[i][j])
end
endfunction
endclass
class Cls;
rand int x = 1;
endclass
class unconstrained_dynamic_array_test;
rand int dynamic_array_1d[];
rand int dynamic_array_2d[][];
rand Cls class_dynamic_array[];
rand Cls class_dynamic_array_null[];
function new();
// Initialize 1D dynamic array
dynamic_array_1d = new[5];
foreach(dynamic_array_1d[i]) begin
dynamic_array_1d[i] = 'h0 + i;
end
// Initialize 2D dynamic array
dynamic_array_2d = new[3];
foreach(dynamic_array_2d[i]) begin
dynamic_array_2d[i] = new[3];
foreach(dynamic_array_2d[i][j]) begin
dynamic_array_2d[i][j] = 'h0 + i + j;
end
end
class_dynamic_array = new[5];
foreach(class_dynamic_array[i]) begin
class_dynamic_array[i] = new;
end
class_dynamic_array_null = new[2];
endfunction
function void check_randomization();
foreach (dynamic_array_1d[i]) begin
`check_rand(this, dynamic_array_1d[i])
end
foreach (dynamic_array_2d[i]) begin
foreach (dynamic_array_2d[i][j]) begin
`check_rand(this, dynamic_array_2d[i][j])
end
end
foreach (class_dynamic_array[i]) begin
`check_rand(this, class_dynamic_array[i].x)
end
foreach (class_dynamic_array_null[i]) begin
if (class_dynamic_array_null[i] != null) $stop;
end
endfunction
endclass
class unconstrained_struct_with_array_test;
typedef struct {
rand bit [7:0] byte_array[4];
} struct_with_array_t;
rand struct_with_array_t struct_with_array;
function new();
struct_with_array = '{'{default: 'h0}};
endfunction
function void check_randomization();
foreach (struct_with_array.byte_array[i]) begin
`check_rand(this, struct_with_array.byte_array[i])
end
endfunction
endclass
class unconstrained_struct_array_test;
typedef struct {
rand int field_a;
rand int field_b;
} simple_struct_t;
rand simple_struct_t struct_array_1[3]; // Unpacked array
rand simple_struct_t struct_array_2[][]; // Dynamic array
function new();
struct_array_1 = '{'{default: 0}, '{default: 1}, '{default: 2}};
struct_array_2 = new[3];
foreach (struct_array_2[i]) begin
struct_array_2[i] = new[4];
end
endfunction
function void check_randomization();
foreach (struct_array_1[i]) begin
`check_rand(this, struct_array_1[i].field_a)
`check_rand(this, struct_array_1[i].field_b)
end
foreach (struct_array_2[i, j]) begin
`check_rand(this, struct_array_2[i][j].field_a)
`check_rand(this, struct_array_2[i][j].field_b)
end
endfunction
endclass
class unconstrained_associative_array_test;
rand int associative_array_1d[string];
rand int associative_array_3d[string][string][string];
string key1, key2, key3;
function new();
associative_array_1d["key1"] = 1;
associative_array_1d["key2"] = 2;
associative_array_3d["key1"]["subkey1"]["subsubkey1"] = 1;
associative_array_3d["key1"]["subkey1"]["subsubkey2"] = 2;
associative_array_3d["key1"]["subkey2"]["subsubkey1"] = 3;
associative_array_3d["key1"]["subkey3"]["subsubkey1"] = 4;
associative_array_3d["key1"]["subkey3"]["subsubkey2"] = 5;
associative_array_3d["key1"]["subkey3"]["subsubkey3"] = 6;
associative_array_3d["key2"]["subkey1"]["subsubkey1"] = 7;
associative_array_3d["key2"]["subkey1"]["subsubkey2"] = 8;
associative_array_3d["key2"]["subkey2"]["subsubkey1"] = 9;
associative_array_3d["key2"]["subkey3"]["subsubkey1"] = 10;
associative_array_3d["key2"]["subkey3"]["subsubkey2"] = 11;
endfunction
function void check_randomization();
`check_rand(this, associative_array_1d["key1"]);
`check_rand(this, associative_array_1d["key2"]);
foreach(associative_array_3d[key1, key2, key3]) begin
`check_rand(this, associative_array_3d[key1][key2][key3]);
end
endfunction
endclass
class unconstrained_queue_test;
rand int queue_array_1d[$];
rand int queue_array_2d[$][$];
function new();
queue_array_1d = {};
for (int i = 0; i < 8; i++) begin
queue_array_1d.push_back('h0 + i);
end
queue_array_2d = {};
queue_array_2d[0] = '{1, 2, 3};
queue_array_2d[1] = '{4, 5, 6, 0, 10};
queue_array_2d[2] = '{6, 7, 8, 9};
endfunction
function void check_randomization();
foreach (queue_array_1d[i]) begin
`check_rand(this, queue_array_1d[i]);
end
foreach(queue_array_2d[i, j]) begin
`check_rand(this, queue_array_2d[i][j]);
end
endfunction
endclass
module t_randomize_array;
unconstrained_packed_array_test packed_class;
unconstrained_unpacked_array_test unpacked_class;
unconstrained_dynamic_array_test dynamic_class;
unconstrained_struct_with_array_test struct_with_array_class;
unconstrained_struct_array_test struct_array_class;
unconstrained_associative_array_test associative_array_class;
unconstrained_queue_test queue_class;
initial begin
// Test 1: Packed Array Unconstrained Constrained Test
packed_class = new();
repeat(2) begin
packed_class.check_randomization();
end
// Test 2: Unpacked Array Unconstrained Constrained Test
unpacked_class = new();
repeat(2) begin
unpacked_class.check_randomization();
end
// Test 3: Dynamic Array Unconstrained Constrained Test
dynamic_class = new();
repeat(2) begin
dynamic_class.check_randomization();
end
// Test 4: Struct Containing Array Test
struct_with_array_class = new();
repeat(2) begin
struct_with_array_class.check_randomization();
end
struct_array_class = new();
repeat(2) begin
struct_array_class.check_randomization();
end
// Test 5: Associative Array Unconstrained Test
associative_array_class = new();
repeat(2) begin
associative_array_class.check_randomization();
end
// Test 6: Queue Unconstrained Test
queue_class = new();
repeat(2) begin
queue_class.check_randomization();
end
$write("*-* All Finished *-*\n");
$finish;
end
endmodule
|