diff --git a/src/V3Randomize.cpp b/src/V3Randomize.cpp index 4469d7187..daf457b88 100644 --- a/src/V3Randomize.cpp +++ b/src/V3Randomize.cpp @@ -1404,9 +1404,19 @@ class RandomizeVisitor final : public VNVisitor { AstVarRef* tempRefp = new AstVarRef{fl, newRandLoopIndxp, VAccess::READ}; if (VN_IS(tempDTypep, DynArrayDType)) tempElementp = new AstCMethodHard{fl, tempExprp, "atWrite", tempRefp}; - else if (VN_IS(tempDTypep, UnpackArrayDType)) - tempElementp = new AstArraySel{fl, tempExprp, tempRefp}; - else if (VN_IS(tempDTypep, AssocArrayDType)) + else if (VN_IS(tempDTypep, UnpackArrayDType)) { + AstNodeArrayDType* const aryDTypep = VN_CAST(tempDTypep, NodeArrayDType); + // Adjust the bitp to ensure it covers all possible indices + tempElementp = new AstArraySel{ + fl, tempExprp, + new AstSel{ + fl, + new AstSub{fl, tempRefp, + new AstConst{fl, static_cast(aryDTypep->lo())}}, + new AstConst{fl, 0}, + new AstConst{ + fl, static_cast(V3Number::log2b(aryDTypep->hi()) + 1)}}}; + } else if (VN_IS(tempDTypep, AssocArrayDType)) tempElementp = new AstAssocSel{fl, tempExprp, tempRefp}; else if (VN_IS(tempDTypep, QueueDType)) tempElementp = new AstCMethodHard{fl, tempExprp, "atWriteAppend", tempRefp}; diff --git a/test_regress/t/t_randomize_array.v b/test_regress/t/t_randomize_array.v index 2155ac67a..521b7cea4 100755 --- a/test_regress/t/t_randomize_array.v +++ b/test_regress/t/t_randomize_array.v @@ -34,10 +34,14 @@ 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(); @@ -47,6 +51,12 @@ class unconstrained_unpacked_array_test; `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