From 3e35de2be108b7d8b24808aa55a0f0f9f8570705 Mon Sep 17 00:00:00 2001 From: Miodrag Milanovic Date: Fri, 28 Jan 2022 10:18:02 +0100 Subject: Add more options and time handling --- kernel/fstdata.cc | 1 + kernel/fstdata.h | 2 ++ passes/sat/sim.cc | 105 ++++++++++++++++++++++++++++++++++++++++++++++++++++-- 3 files changed, 106 insertions(+), 2 deletions(-) diff --git a/kernel/fstdata.cc b/kernel/fstdata.cc index e497ff7ad..466b8f19f 100644 --- a/kernel/fstdata.cc +++ b/kernel/fstdata.cc @@ -24,6 +24,7 @@ USING_YOSYS_NAMESPACE FstData::FstData(std::string filename) : ctx(nullptr) { ctx = (fstReaderContext *)fstReaderOpen(filename.c_str()); + timescale = pow(10.0, (int)fstReaderGetTimescale(ctx)); extractVarNames(); } diff --git a/kernel/fstdata.h b/kernel/fstdata.h index e7595cbf6..4c54a3f22 100644 --- a/kernel/fstdata.h +++ b/kernel/fstdata.h @@ -58,6 +58,7 @@ class FstData void recalc_time_offsets(fstHandle signal, std::vector time); fstHandle getHandle(std::string name); + double getTimescale() { return timescale; } private: void extractVarNames(); @@ -72,6 +73,7 @@ private: std::vector sample_times; size_t sample_times_ndx; std::map current; + double timescale; }; YOSYS_NAMESPACE_END diff --git a/passes/sat/sim.cc b/passes/sat/sim.cc index 8cabcfbb4..d6f5f3a81 100644 --- a/passes/sat/sim.cc +++ b/passes/sat/sim.cc @@ -28,6 +28,41 @@ USING_YOSYS_NAMESPACE PRIVATE_NAMESPACE_BEGIN +enum class SimulationMode { + cmp, + gold, + gate, +}; + +static const std::map g_units = +{ + { "", -9 }, // default is ns + { "s", 0 }, + { "ms", -3 }, + { "us", -6 }, + { "ns", -9 }, + { "ps", -12 }, + { "fs", -15 }, + { "as", -18 }, + { "zs", -21 }, +}; + +static double stringToTime(std::string str) +{ + if (str=="END") return -1; + + char *endptr; + long value = strtol(str.c_str(), &endptr, 10); + + if (g_units.find(endptr)==g_units.end()) + log_error("Cannot parse '%s', bad unit '%s'\n", str.c_str(), endptr); + + if (value < 0) + log_error("Time value '%s' must be positive\n", str.c_str()); + + return value * pow(10.0, g_units.at(endptr)); +} + struct SimShared { bool debug = false; @@ -36,6 +71,9 @@ struct SimShared bool zinit = false; int rstlen = 1; FstData *fst = nullptr; + double start_time = 0; + double stop_time = -1; + SimulationMode sim_mode = SimulationMode::cmp; }; void zinit(State &v) @@ -900,10 +938,38 @@ struct SimWorker : SimShared } } + uint64_t startCount = 0; + uint64_t stopCount = 0; + if (start_time==0) { + if (start_time < fst->getStartTime()) + log_warning("Start time is before simulation file start time\n"); + startCount = fst->getStartTime(); + } else if (start_time==-1) + startCount = fst->getEndTime(); + else { + startCount = start_time / fst->getTimescale(); + if (startCount > fst->getEndTime()) { + startCount = fst->getEndTime(); + log_warning("Start time is after simulation file end time\n"); + } + } + if (stop_time==0) { + if (stop_time < fst->getStartTime()) + log_warning("Stop time is before simulation file start time\n"); + stopCount = fst->getStartTime(); + } else if (stop_time==-1) + stopCount = fst->getEndTime(); + else { + stopCount = stop_time / fst->getTimescale(); + if (stopCount > fst->getEndTime()) { + stopCount = fst->getEndTime(); + log_warning("Stop time is after simulation file end time\n"); + } + } fst->reconstruct(fst_clock); auto edges = fst->edges(fst_clock.back(), true, true); fst->reconstructAllAtTimes(edges); - for(auto &time : edges) { +/* for(auto &time : edges) { for(auto &item : inputs) { std::string v = fst->valueAt(item.second, time); top->set_state(item.first, Const::from_string(v)); @@ -914,7 +980,7 @@ struct SimWorker : SimShared Const sim_val = top->get_state(item.first); log("%s %s\n", log_signal(fst_val), log_signal(sim_val)); } - } + }*/ } }; @@ -970,6 +1036,21 @@ struct SimPass : public Pass { log(" -scope\n"); log(" scope of simulation top model\n"); log("\n"); + log(" -start