1
// -*- mode: C++; c-file-style: "cc-mode" -*-
2
//*************************************************************************
4
// Code available from: https://verilator.org
6
// Copyright 2024 by Wilson Snyder. This program is free software; you can
7
// redistribute it and/or modify it under the terms of either the GNU Lesser
8
// General Public License Version 3 or the Perl Artistic License Version 2.0.
9
// SPDX-License-Identifier: LGPL-3.0-only OR Artistic-2.0
11
//=========================================================================
14
/// \brief Verilated randomization implementation code
16
/// This file must be compiled and linked against all Verilated objects
17
/// that use randomization features.
19
/// See the internals documentation docs/internals.rst for details.
21
//=========================================================================
23
#include "verilated_random.h"
29
#define _VL_SOLVER_HASH_LEN 1
30
#define _VL_SOLVER_HASH_LEN_TOTAL 4
33
#if defined(__unix__) || defined(__unix) || (defined(__APPLE__) && defined(__MACH__))
34
# define _VL_SOLVER_PIPE // Allow pipe SMT solving. Needs fork()
42
#if defined(_WIN32) || defined(__MINGW32__)
43
# include <io.h> // open, read, write, close
47
class Process final : private std::streambuf, public std::iostream {
48
static constexpr int BUFFER_SIZE = 4096;
49
const char* const* m_cmd = nullptr; // fork() process argv
51
pid_t m_pid = 0; // fork() process id
53
int m_pid = 0; // fork() process id - always zero as disabled
55
bool m_pidExited = true; // If subprocess has exited and can be opened
56
int m_pidStatus = 0; // fork() process exit status, valid if m_pidExited
57
int m_writeFd = -1; // File descriptor TO subprocess
58
int m_readFd = -1; // File descriptor FROM subprocess
59
char m_readBuf[BUFFER_SIZE];
60
char m_writeBuf[BUFFER_SIZE];
63
typedef std::streambuf::traits_type traits_type;
66
int overflow(int c = traits_type::eof()) override {
67
char c2 = static_cast<char>(c);
68
if (pbase() == pptr()) return 0;
69
size_t size = pptr() - pbase();
70
ssize_t n = ::write(m_writeFd, pbase(), size);
71
if (n == -1) perror("write");
74
return traits_type::eof();
77
setp(m_writeBuf, m_writeBuf + sizeof(m_writeBuf));
79
setp(m_writeBuf + n, m_writeBuf + sizeof(m_writeBuf));
80
if (c != traits_type::eof()) sputc(c2);
83
int underflow() override {
85
ssize_t n = ::read(m_readFd, m_readBuf, sizeof(m_readBuf));
86
if (n == -1) perror("read");
89
return traits_type::eof();
91
setg(m_readBuf, m_readBuf, m_readBuf + n);
92
return traits_type::to_int_type(m_readBuf[0]);
100
explicit Process(const char* const* const cmd = nullptr)
102
, std::iostream{this}
108
if (m_pidExited) return;
109
#ifdef _VL_SOLVER_PIPE
110
if (waitpid(m_pid, &m_pidStatus, 0) != m_pid) return;
112
std::stringstream msg;
113
msg << "Subprocess command `" << m_cmd[0];
114
for (const char* const* arg = m_cmd + 1; *arg; arg++) msg << ' ' << *arg;
116
if (WIFSIGNALED(m_pidStatus))
117
msg << strsignal(WTERMSIG(m_pidStatus))
118
<< (WCOREDUMP(m_pidStatus) ? " (core dumped)" : "");
119
else if (WIFEXITED(m_pidStatus))
120
msg << "exit status " << WEXITSTATUS(m_pidStatus);
121
const std::string str = msg.str();
122
VL_WARN_MT("", 0, "Process", str.c_str());
131
if (m_writeFd != -1) {
135
if (m_readFd != -1) {
141
bool open(const char* const* const cmd) {
142
setp(std::begin(m_writeBuf), std::end(m_writeBuf));
143
setg(m_readBuf, m_readBuf, m_readBuf);
144
#ifdef _VL_SOLVER_PIPE
145
if (!cmd || !cmd[0]) return false;
147
int fd_stdin[2]; // Can't use std::array
148
int fd_stdout[2]; // Can't use std::array
149
constexpr int P_RD = 0;
150
constexpr int P_WR = 1;
152
if (pipe(fd_stdin) != 0) {
153
perror("Process::open: pipe");
156
if (pipe(fd_stdout) != 0) {
157
perror("Process::open: pipe");
158
close(fd_stdin[P_RD]);
159
close(fd_stdin[P_WR]);
163
if (fd_stdin[P_RD] <= 2 || fd_stdin[P_WR] <= 2 || fd_stdout[P_RD] <= 2
164
|| fd_stdout[P_WR] <= 2) {
165
// We'd have to rearrange all of the FD usages in this case.
166
// Too unlikely; verilator isn't a daemon.
167
fprintf(stderr, "stdin/stdout closed before pipe opened\n");
168
close(fd_stdin[P_RD]);
169
close(fd_stdin[P_WR]);
170
close(fd_stdout[P_RD]);
171
close(fd_stdout[P_WR]);
175
const pid_t pid = fork();
177
perror("Process::open: fork");
178
close(fd_stdin[P_RD]);
179
close(fd_stdin[P_WR]);
180
close(fd_stdout[P_RD]);
181
close(fd_stdout[P_WR]);
186
close(fd_stdin[P_WR]);
187
dup2(fd_stdin[P_RD], STDIN_FILENO);
188
close(fd_stdout[P_RD]);
189
dup2(fd_stdout[P_WR], STDOUT_FILENO);
190
execvp(cmd[0], const_cast<char* const*>(cmd));
191
std::stringstream msg;
192
msg << "Process::open: execvp(" << cmd[0] << ")";
193
const std::string str = msg.str();
201
m_readFd = fd_stdout[P_RD];
202
m_writeFd = fd_stdin[P_WR];
204
close(fd_stdin[P_RD]);
205
close(fd_stdout[P_WR]);
214
static Process& getSolver() {
215
static Process s_solver;
216
static bool s_done = false;
217
if (s_done) return s_solver;
220
static std::vector<const char*> s_argv;
221
static std::string s_program = Verilated::threadContextp()->solverProgram();
222
s_argv.emplace_back(&s_program[0]);
223
for (char* arg = &s_program[0]; *arg; arg++) {
226
s_argv.emplace_back(arg + 1);
229
s_argv.emplace_back(nullptr);
231
const char* const* const cmd = &s_argv[0];
233
s_solver << "(set-logic QF_BV)\n";
234
s_solver << "(check-sat)\n";
235
s_solver << "(reset)\n";
237
getline(s_solver, s);
238
if (s == "sat") return s_solver;
240
std::stringstream msg;
241
msg << "Unable to communicate with SAT solver, please check its installation or specify a "
242
"different one in VERILATOR_SOLVER environment variable.\n";
243
msg << " ... Tried: $";
244
for (const char* const* arg = cmd; *arg; arg++) msg << ' ' << *arg;
246
const std::string str = msg.str();
247
VL_WARN_MT("", 0, "randomize", str.c_str());
249
while (getline(s_solver, s)) {}
253
//======================================================================
254
// VlRandomizer:: Methods
256
void VlRandomVar::emit(std::ostream& s) const { s << m_name; }
257
void VlRandomConst::emit(std::ostream& s) const {
259
for (int i = 0; i < m_width; i++) s << (VL_BITISSET_Q(m_val, m_width - i - 1) ? '1' : '0');
261
void VlRandomBinOp::emit(std::ostream& s) const {
262
s << '(' << m_op << ' ';
268
void VlRandomExtract::emit(std::ostream& s) const {
269
s << "((_ extract " << m_idx << ' ' << m_idx << ") ";
273
bool VlRandomVar::set(std::string&& val) const {
274
VlWide<VL_WQ_WORDS_E> qowp;
275
VL_SET_WQ(qowp, 0ULL);
276
WDataOutP owp = qowp;
278
if (obits > VL_QUADSIZE) owp = reinterpret_cast<WDataOutP>(datap());
280
for (i = 0; val[i] && val[i] != '#'; i++) {}
281
if (val[i++] != '#') return false;
283
case 'b': _vl_vsss_based(owp, obits, 1, &val[i], 0, val.size() - i); break;
284
case 'o': _vl_vsss_based(owp, obits, 3, &val[i], 0, val.size() - i); break;
285
case 'h': // FALLTHRU
286
case 'x': _vl_vsss_based(owp, obits, 4, &val[i], 0, val.size() - i); break;
288
VL_WARN_MT(__FILE__, __LINE__, "randomize",
289
"Internal: Unable to parse solver's randomized number");
292
if (obits <= VL_BYTESIZE) {
293
CData* const p = static_cast<CData*>(datap());
294
*p = VL_CLEAN_II(obits, obits, owp[0]);
295
} else if (obits <= VL_SHORTSIZE) {
296
SData* const p = static_cast<SData*>(datap());
297
*p = VL_CLEAN_II(obits, obits, owp[0]);
298
} else if (obits <= VL_IDATASIZE) {
299
IData* const p = static_cast<IData*>(datap());
300
*p = VL_CLEAN_II(obits, obits, owp[0]);
301
} else if (obits <= VL_QUADSIZE) {
302
QData* const p = static_cast<QData*>(datap());
303
*p = VL_CLEAN_QQ(obits, obits, VL_SET_QW(owp));
305
_vl_clean_inplace_w(obits, owp);
310
std::shared_ptr<const VlRandomExpr> VlRandomizer::randomConstraint(VlRNG& rngr, int bits) {
311
unsigned long long hash = VL_RANDOM_RNG_I(rngr) & ((1 << bits) - 1);
312
std::shared_ptr<const VlRandomExpr> concat = nullptr;
313
std::vector<std::shared_ptr<const VlRandomExpr>> varbits;
314
for (const auto& var : m_vars) {
315
for (int i = 0; i < var.second->width(); i++)
316
varbits.emplace_back(std::make_shared<const VlRandomExtract>(var.second, i));
318
for (int i = 0; i < bits; i++) {
319
std::shared_ptr<const VlRandomExpr> bit = nullptr;
320
for (unsigned j = 0; j * 2 < varbits.size(); j++) {
321
unsigned idx = j + VL_RANDOM_RNG_I(rngr) % (varbits.size() - j);
322
auto sel = varbits[idx];
323
std::swap(varbits[idx], varbits[j]);
324
bit = bit == nullptr ? sel : std::make_shared<const VlRandomBinOp>("bvxor", bit, sel);
326
concat = concat == nullptr ? bit
327
: std::make_shared<const VlRandomBinOp>("concat", concat, bit);
329
return std::make_shared<const VlRandomBinOp>(
330
"=", concat, std::make_shared<const VlRandomConst>(hash, bits));
333
bool VlRandomizer::next(VlRNG& rngr) {
334
if (m_vars.empty()) return true;
335
std::iostream& f = getSolver();
336
if (!f) return false;
338
f << "(set-option :produce-models true)\n";
339
f << "(set-logic QF_BV)\n";
340
for (const auto& var : m_vars) {
341
f << "(declare-fun " << var.second->name() << " () (_ BitVec " << var.second->width()
344
for (const std::string& constraint : m_constraints) { f << "(assert " << constraint << ")\n"; }
345
f << "(check-sat)\n";
347
bool sat = parseSolution(f);
353
for (int i = 0; i < _VL_SOLVER_HASH_LEN_TOTAL && sat; i++) {
355
randomConstraint(rngr, _VL_SOLVER_HASH_LEN)->emit(f);
357
f << "\n(check-sat)\n";
358
sat = parseSolution(f);
365
bool VlRandomizer::parseSolution(std::iostream& f) {
367
do { std::getline(f, sat); } while (sat == "");
369
if (sat == "unsat") return false;
371
std::stringstream msg;
372
msg << "Internal: Solver error: " << sat;
373
const std::string str = msg.str();
374
VL_WARN_MT(__FILE__, __LINE__, "randomize", str.c_str());
379
for (const auto& var : m_vars) f << var.second->name() << ' ';
382
// Quasi-parse S-expression of the form ((x #xVALUE) (y #bVALUE) (z #xVALUE))
386
VL_WARN_MT(__FILE__, __LINE__, "randomize",
387
"Internal: Unable to parse solver's response: invalid S-expression");
395
VL_WARN_MT(__FILE__, __LINE__, "randomize",
396
"Internal: Unable to parse solver's response: invalid S-expression");
400
std::string name, value;
402
std::getline(f, value, ')');
404
auto it = m_vars.find(name);
405
if (it == m_vars.end()) continue;
407
it->second->set(std::move(value));
413
void VlRandomizer::hard(std::string&& constraint) {
414
m_constraints.emplace_back(std::move(constraint));
418
void VlRandomizer::dump() const {
419
for (const auto& var : m_vars) {
420
VL_PRINTF("Variable (%d): %s\n", var.second->width(), var.second->name());
422
for (const std::string& c : m_constraints) VL_PRINTF("Constraint: %s\n", c.c_str());