This commit is contained in:
parent
985f45759c
commit
f351882cf0
|
|
@ -4067,13 +4067,10 @@ class RandomizeVisitor final : public VNVisitor {
|
||||||
// Restrict enum variables in solver to valid members only
|
// Restrict enum variables in solver to valid members only
|
||||||
{
|
{
|
||||||
AstNodeModule* const genModp = VN_AS(genp->user2p(), NodeModule);
|
AstNodeModule* const genModp = VN_AS(genp->user2p(), NodeModule);
|
||||||
nodep->foreachMember([&](AstClass*, AstVar* memberVarp) {
|
// Emit enum range hard constraint for a single variable
|
||||||
if (!memberVarp->user3()) return;
|
const auto emitEnumConstraint = [&](const std::string& smtName,
|
||||||
AstEnumDType* const enumDtp
|
AstEnumDType* const enumDtp) {
|
||||||
= VN_CAST(memberVarp->dtypep()->skipRefToEnump(), EnumDType);
|
|
||||||
if (!enumDtp) return;
|
|
||||||
const int width = enumDtp->width();
|
const int width = enumDtp->width();
|
||||||
const std::string smtName = memberVarp->name();
|
|
||||||
std::string constraint = "(__Vbv (or";
|
std::string constraint = "(__Vbv (or";
|
||||||
for (AstEnumItem* itemp = enumDtp->itemsp(); itemp;
|
for (AstEnumItem* itemp = enumDtp->itemsp(); itemp;
|
||||||
itemp = VN_AS(itemp->nextp(), EnumItem)) {
|
itemp = VN_AS(itemp->nextp(), EnumItem)) {
|
||||||
|
|
@ -4088,6 +4085,41 @@ class RandomizeVisitor final : public VNVisitor {
|
||||||
new AstCExpr{fl, AstCExpr::Pure{}, "\"" + constraint + "\""}};
|
new AstCExpr{fl, AstCExpr::Pure{}, "\"" + constraint + "\""}};
|
||||||
callp->dtypeSetVoid();
|
callp->dtypeSetVoid();
|
||||||
randomizep->addStmtsp(callp->makeStmt());
|
randomizep->addStmtsp(callp->makeStmt());
|
||||||
|
};
|
||||||
|
// Recursively emit enum constraints for sub-object members
|
||||||
|
std::function<void(AstClass*, const std::string&)> addSubObjEnumConstraints
|
||||||
|
= [&](AstClass* classp, const std::string& pathPrefix) {
|
||||||
|
classp->foreachMember([&](AstClass*, AstVar* subVarp) {
|
||||||
|
if (!subVarp->rand().isRandomizable()) return;
|
||||||
|
const std::string smtName = pathPrefix + "." + subVarp->name();
|
||||||
|
AstEnumDType* const enumDtp
|
||||||
|
= VN_CAST(subVarp->dtypep()->skipRefToEnump(), EnumDType);
|
||||||
|
if (enumDtp) {
|
||||||
|
emitEnumConstraint(smtName, enumDtp);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if (!subVarp->globalConstrained()) return;
|
||||||
|
const AstNodeDType* const subDtypep = subVarp->dtypep()->skipRefp();
|
||||||
|
const AstClassRefDType* const subClassRefp
|
||||||
|
= VN_CAST(subDtypep, ClassRefDType);
|
||||||
|
if (!subClassRefp) return;
|
||||||
|
addSubObjEnumConstraints(subClassRefp->classp(), smtName);
|
||||||
|
});
|
||||||
|
};
|
||||||
|
nodep->foreachMember([&](AstClass*, AstVar* memberVarp) {
|
||||||
|
// Direct enum members
|
||||||
|
if (memberVarp->user3()) {
|
||||||
|
AstEnumDType* const enumDtp
|
||||||
|
= VN_CAST(memberVarp->dtypep()->skipRefToEnump(), EnumDType);
|
||||||
|
if (enumDtp) emitEnumConstraint(memberVarp->name(), enumDtp);
|
||||||
|
}
|
||||||
|
// Enum members inside globalConstrained sub-objects
|
||||||
|
if (memberVarp->globalConstrained()) {
|
||||||
|
const AstNodeDType* const dtypep = memberVarp->dtypep()->skipRefp();
|
||||||
|
const AstClassRefDType* const classRefp = VN_CAST(dtypep, ClassRefDType);
|
||||||
|
if (!classRefp) return;
|
||||||
|
addSubObjEnumConstraints(classRefp->classp(), memberVarp->name());
|
||||||
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,21 @@
|
||||||
|
#!/usr/bin/env python3
|
||||||
|
# DESCRIPTION: Verilator: Verilog Test driver/expect definition
|
||||||
|
#
|
||||||
|
# This program is free software; you can redistribute it and/or modify it
|
||||||
|
# under the terms of either the GNU Lesser General Public License Version 3
|
||||||
|
# or the Perl Artistic License Version 2.0.
|
||||||
|
# SPDX-FileCopyrightText: 2026 Wilson Snyder
|
||||||
|
# SPDX-License-Identifier: LGPL-3.0-only OR Artistic-2.0
|
||||||
|
|
||||||
|
import vltest_bootstrap
|
||||||
|
|
||||||
|
test.scenarios('simulator')
|
||||||
|
|
||||||
|
if not test.have_solver:
|
||||||
|
test.skip("No constraint solver installed")
|
||||||
|
|
||||||
|
test.compile()
|
||||||
|
|
||||||
|
test.execute()
|
||||||
|
|
||||||
|
test.passes()
|
||||||
|
|
@ -0,0 +1,93 @@
|
||||||
|
// DESCRIPTION: Verilator: Verilog Test module
|
||||||
|
//
|
||||||
|
// This file ONLY is placed under the Creative Commons Public Domain.
|
||||||
|
// SPDX-FileCopyrightText: 2026 PlanV GmbH
|
||||||
|
// SPDX-License-Identifier: CC0-1.0
|
||||||
|
|
||||||
|
// verilog_format: off
|
||||||
|
`define stop $stop
|
||||||
|
`define checkd(gotv,expv) do if ((gotv) !== (expv)) begin $write("%%Error: %s:%0d: got=%0d exp=%0d\n", `__FILE__,`__LINE__, (gotv), (expv)); `stop; end while(0);
|
||||||
|
// verilog_format: on
|
||||||
|
|
||||||
|
// Check that an enum value is one of the valid members
|
||||||
|
`define check_enum_color(gotv) \
|
||||||
|
if ((gotv) !== RED && (gotv) !== GREEN && (gotv) !== BLUE) begin \
|
||||||
|
$write("%%Error: %s:%0d: invalid color=%0d\n", `__FILE__, `__LINE__, (gotv)); \
|
||||||
|
`stop; \
|
||||||
|
end
|
||||||
|
|
||||||
|
`define check_enum_state(gotv) \
|
||||||
|
if ((gotv) !== IDLE && (gotv) !== RUN && (gotv) !== STOP) begin \
|
||||||
|
$write("%%Error: %s:%0d: invalid state=%0d\n", `__FILE__, `__LINE__, (gotv)); \
|
||||||
|
`stop; \
|
||||||
|
end
|
||||||
|
|
||||||
|
module t;
|
||||||
|
|
||||||
|
typedef enum logic [1:0] {
|
||||||
|
RED = 2'd0,
|
||||||
|
GREEN = 2'd1,
|
||||||
|
BLUE = 2'd2
|
||||||
|
} color_e;
|
||||||
|
|
||||||
|
typedef enum logic [2:0] {
|
||||||
|
IDLE = 3'd0,
|
||||||
|
RUN = 3'd3,
|
||||||
|
STOP = 3'd5
|
||||||
|
} state_e;
|
||||||
|
|
||||||
|
class InnerObj;
|
||||||
|
rand color_e color;
|
||||||
|
rand state_e state;
|
||||||
|
endclass
|
||||||
|
|
||||||
|
class OuterObj;
|
||||||
|
rand InnerObj inner;
|
||||||
|
|
||||||
|
function new();
|
||||||
|
inner = new();
|
||||||
|
endfunction
|
||||||
|
endclass
|
||||||
|
|
||||||
|
// Nested: TopObj -> MiddleObj -> InnerObj
|
||||||
|
class MiddleObj;
|
||||||
|
rand InnerObj inner;
|
||||||
|
|
||||||
|
function new();
|
||||||
|
inner = new();
|
||||||
|
endfunction
|
||||||
|
endclass
|
||||||
|
|
||||||
|
class TopObj;
|
||||||
|
rand MiddleObj mid;
|
||||||
|
|
||||||
|
function new();
|
||||||
|
mid = new();
|
||||||
|
endfunction
|
||||||
|
endclass
|
||||||
|
|
||||||
|
initial begin
|
||||||
|
OuterObj obj;
|
||||||
|
TopObj top;
|
||||||
|
obj = new();
|
||||||
|
top = new();
|
||||||
|
|
||||||
|
// Test direct sub-object enum (one level deep)
|
||||||
|
repeat (20) begin
|
||||||
|
`checkd(obj.randomize(), 1)
|
||||||
|
`check_enum_color(obj.inner.color)
|
||||||
|
`check_enum_state(obj.inner.state)
|
||||||
|
end
|
||||||
|
|
||||||
|
// Test nested sub-object enum (two levels deep)
|
||||||
|
repeat (20) begin
|
||||||
|
`checkd(top.randomize(), 1)
|
||||||
|
`check_enum_color(top.mid.inner.color)
|
||||||
|
`check_enum_state(top.mid.inner.state)
|
||||||
|
end
|
||||||
|
|
||||||
|
$write("*-* All Finished *-*\n");
|
||||||
|
$finish;
|
||||||
|
end
|
||||||
|
|
||||||
|
endmodule
|
||||||
Loading…
Reference in New Issue