Skip to content

Commit

Permalink
Support rand_mode (verilator#5273)
Browse files Browse the repository at this point in the history
Signed-off-by: Krzysztof Bieganski <[email protected]>
  • Loading branch information
kbieganski authored Jul 31, 2024
1 parent 403a197 commit 2f5c58b
Show file tree
Hide file tree
Showing 21 changed files with 762 additions and 189 deletions.
6 changes: 5 additions & 1 deletion include/verilated_random.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -403,8 +403,12 @@ bool VlRandomizer::parseSolution(std::iostream& f) {

auto it = m_vars.find(name);
if (it == m_vars.end()) continue;
const VlRandomVar& varr = *it->second;
if (m_randmode && !varr.randModeIdxNone()) {
if (!(m_randmode->at(varr.randModeIdx()))) continue;
}

it->second->set(std::move(value));
varr.set(std::move(value));
}

return true;
Expand Down
19 changes: 13 additions & 6 deletions include/verilated_random.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,15 +38,19 @@ class VlRandomVar final : public VlRandomExpr {
const char* const m_name; // Variable name
void* const m_datap; // Reference to variable data
const int m_width; // Variable width in bits
const std::uint32_t m_randModeIdx; // rand_mode index

public:
VlRandomVar(const char* name, int width, void* datap)
VlRandomVar(const char* name, int width, void* datap, std::uint32_t randModeIdx)
: m_name{name}
, m_datap{datap}
, m_width{width} {}
, m_width{width}
, m_randModeIdx{randModeIdx} {}
const char* name() const { return m_name; }
int width() const { return m_width; }
void* datap() const { return m_datap; }
std::uint32_t randModeIdx() const { return m_randModeIdx; }
bool randModeIdxNone() const { return randModeIdx() == std::numeric_limits<unsigned>::max(); }
bool set(std::string&&) const;
void emit(std::ostream& s) const override;
};
Expand Down Expand Up @@ -94,8 +98,9 @@ class VlRandomBinOp final : public VlRandomExpr {
class VlRandomizer final {
// MEMBERS
std::vector<std::string> m_constraints; // Solver-dependent constraints
std::map<std::string, std::shared_ptr<const VlRandomVar>>
m_vars; // Solver-dependent variables
std::map<std::string, std::shared_ptr<const VlRandomVar>> m_vars; // Solver-dependent
// variables
const VlQueue<CData>* m_randmode; // rand_mode state;

// PRIVATE METHODS
std::shared_ptr<const VlRandomExpr> randomConstraint(VlRNG& rngr, int bits);
Expand All @@ -106,13 +111,15 @@ class VlRandomizer final {
// Finds the next solution satisfying the constraints
bool next(VlRNG& rngr);
template <typename T>
void write_var(T& var, int width, const char* name) {
void write_var(T& var, int width, const char* name,
std::uint32_t randmodeIdx = std::numeric_limits<std::uint32_t>::max()) {
auto it = m_vars.find(name);
if (it != m_vars.end()) return;
m_vars[name] = std::make_shared<const VlRandomVar>(name, width, &var);
m_vars[name] = std::make_shared<const VlRandomVar>(name, width, &var, randmodeIdx);
}
void hard(std::string&& constraint);
void clear();
void set_randmode(const VlQueue<CData>& randmode) { m_randmode = &randmode; }
#ifdef VL_DEBUG
void dump() const;
#endif
Expand Down
1 change: 1 addition & 0 deletions src/V3AstNodes.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2834,6 +2834,7 @@ void AstCMethodHard::setPurity() {
{"reverse", false},
{"rsort", false},
{"set", false},
{"set_randmode", false},
{"shuffle", false},
{"size", true},
{"slice", true},
Expand Down
4 changes: 2 additions & 2 deletions src/V3LinkDot.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3322,8 +3322,8 @@ class LinkDotResolveVisitor final : public VNVisitor {
} else if (VN_IS(nodep, New) && m_statep->forPrearray()) {
// Resolved in V3Width
} else if (nodep->name() == "randomize" || nodep->name() == "srandom"
|| nodep->name() == "get_randstate"
|| nodep->name() == "set_randstate") {
|| nodep->name() == "get_randstate" || nodep->name() == "set_randstate"
|| nodep->name() == "rand_mode") {
if (AstClass* const classp = VN_CAST(m_modp, Class)) {
nodep->classOrPackagep(classp);
} else {
Expand Down
Loading

0 comments on commit 2f5c58b

Please sign in to comment.