From e3f8bfefdf1f07fb9c94645de0fd03321bc05879 Mon Sep 17 00:00:00 2001 From: Aleksander Kiryk Date: Tue, 31 Oct 2023 17:36:37 +0100 Subject: [PATCH] WIP: Create and call runtime .randomize() on dynamic array randomization --- include/verilated_types.h | 4 ++++ src/V3Randomize.cpp | 12 +++++++++++- 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/include/verilated_types.h b/include/verilated_types.h index be2914d709..e0a3ce2622 100644 --- a/include/verilated_types.h +++ b/include/verilated_types.h @@ -632,6 +632,10 @@ class VlQueue final { } void reverse() { std::reverse(m_deque.begin(), m_deque.end()); } void shuffle() { std::shuffle(m_deque.begin(), m_deque.end(), VlURNG{}); } + void randomize() { + // TODO: Use emitted .randomize()/RAND Funcs + for (const auto& i : m_deque) { m_deque[i] = rand()%10; } + } VlQueue unique() const { VlQueue out; std::set saw; diff --git a/src/V3Randomize.cpp b/src/V3Randomize.cpp index 755e13fb43..cbae4b8b9b 100644 --- a/src/V3Randomize.cpp +++ b/src/V3Randomize.cpp @@ -232,6 +232,12 @@ class RandomizeVisitor final : public VNVisitor { } } return stmtsp; + } else if (const auto* const arrayDtp + = VN_CAST(varrefp->dtypep()->skipRefp(), DynArrayDType)) { + AstNode* argsp = new AstVarRef{fl, varrefp->varp(), VAccess::READWRITE}; + argsp->addNext(new AstText{fl, randcVarp ? ".randomize(__Vm_rng);\n" : ".randomize();\n"}); + AstCStmt* stmtp = new AstCStmt{fl, argsp}; + return stmtp; } else { AstNodeExpr* valp; if (AstEnumDType* const enumDtp = VN_CAST(memberp ? memberp->subDTypep()->subDTypep() @@ -327,7 +333,11 @@ class RandomizeVisitor final : public VNVisitor { if (VN_IS(dtypep, BasicDType) || VN_IS(dtypep, StructDType)) { AstVar* const randcVarp = newRandcVarsp(memberVarp); AstVarRef* const refp = new AstVarRef{fl, memberVarp, VAccess::WRITE}; - AstNodeStmt* const stmtp = newRandStmtsp(fl, refp, randcVarp); + AstNodeStmt* const stmtp = newRandStmtsp(fl, refp, nullptr); + funcp->addStmtsp(stmtp); + } else if (const auto* const arrayp = VN_CAST(dtypep, DynArrayDType)) { + AstVarRef* const refp = new AstVarRef{fl, memberVarp, VAccess::WRITE}; + AstNodeStmt* const stmtp = newRandStmtsp(fl, refp, nullptr); funcp->addStmtsp(stmtp); } else if (const auto* const classRefp = VN_CAST(dtypep, ClassRefDType)) { if (classRefp->classp() == nodep) {