Files
verilator/src/V3EmitCMain.cpp
T

121 lines
3.9 KiB
C++
Raw Normal View History

2020-04-21 20:45:23 -04:00
// -*- mode: C++; c-file-style: "cc-mode" -*-
//*************************************************************************
// DESCRIPTION: Verilator: Emit C++ for tree
//
// Code available from: https://verilator.org
//
//*************************************************************************
//
2023-01-01 10:18:39 -05:00
// Copyright 2003-2023 by Wilson Snyder. This program is free software; you
2020-04-21 20:45:23 -04:00
// 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-License-Identifier: LGPL-3.0-only OR Artistic-2.0
//
//*************************************************************************
#include "config_build.h"
#include "verilatedos.h"
2022-08-05 10:56:57 +01:00
#include "V3EmitCMain.h"
2020-04-21 20:45:23 -04:00
#include "V3EmitC.h"
#include "V3EmitCBase.h"
2022-08-05 10:56:57 +01:00
#include "V3Global.h"
2020-04-21 20:45:23 -04:00
#include <map>
2022-09-18 20:53:42 +01:00
VL_DEFINE_DEBUG_FUNCTIONS;
2020-04-21 20:45:23 -04:00
//######################################################################
class EmitCMain final : EmitCBaseVisitorConst {
2020-04-21 20:45:23 -04:00
// METHODS
// VISITORS
2020-05-16 18:02:54 -04:00
// This visitor doesn't really iterate, but exist to appease base class
void visit(AstNode* nodep) override { iterateChildrenConst(nodep); } // LCOV_EXCL_LINE
2020-04-21 20:45:23 -04:00
public:
// CONSTRUCTORS
EmitCMain() { emitInt(); }
2020-04-21 20:45:23 -04:00
private:
// MAIN METHOD
void emitInt() {
2021-06-20 18:32:57 -04:00
const string filename = v3Global.opt.makeDir() + "/" + topClassName() + "__main.cpp";
2020-04-21 20:45:23 -04:00
newCFile(filename, false /*slow*/, true /*source*/);
V3OutCFile cf{filename};
2020-04-21 20:45:23 -04:00
m_ofp = &cf;
// Not defining main_time/vl_time_stamp, so
v3Global.opt.addCFlags("-DVL_TIME_CONTEXT"); // On MSVC++ anyways
// Optional main top name argument, with empty string replacement
string topArg;
string topName = v3Global.opt.mainTopName();
if (!topName.empty()) {
if (topName == "-") topName = "";
topArg = ", \"" + topName + "\"";
}
// Heavily commented output, as users are likely to look at or copy this code
2020-04-21 20:45:23 -04:00
ofp()->putsHeader();
puts("// DESCRIPTION: main() calling loop, created with Verilator --main\n");
puts("\n");
puts("#include \"verilated.h\"\n");
puts("#include \"" + topClassName() + ".h\"\n");
puts("\n//======================\n\n");
puts("int main(int argc, char** argv, char**) {\n");
puts("// Setup context, defaults, and parse command line\n");
2020-04-21 20:45:23 -04:00
puts("Verilated::debug(0);\n");
puts("const std::unique_ptr<VerilatedContext> contextp{new VerilatedContext};\n");
2022-10-09 14:16:44 -04:00
if (v3Global.opt.trace()) puts("contextp->traceEverOn(true);\n");
puts("contextp->commandArgs(argc, argv);\n");
puts("\n");
2020-04-21 20:45:23 -04:00
puts("// Construct the Verilated model, from Vtop.h generated from Verilating\n");
puts("const std::unique_ptr<" + topClassName() + "> topp{new " + topClassName()
+ "{contextp.get()" + topArg + "}};\n");
puts("\n");
2020-04-21 20:45:23 -04:00
puts("// Simulate until $finish\n");
puts("while (!contextp->gotFinish()) {\n");
2020-04-23 21:22:47 -04:00
puts(/**/ "// Evaluate model\n");
puts(/**/ "topp->eval();\n");
puts(/**/ "// Advance time\n");
2022-08-22 14:26:32 +02:00
if (v3Global.rootp()->delaySchedulerp()) {
puts("if (!topp->eventsPending()) break;\n");
puts("contextp->time(topp->nextTimeSlot());\n");
} else {
puts("contextp->timeInc(1);\n");
}
2020-04-23 21:22:47 -04:00
puts("}\n");
puts("\n");
puts("if (!contextp->gotFinish()) {\n");
2020-04-23 21:22:47 -04:00
puts(/**/ "VL_DEBUG_IF(VL_PRINTF(\"+ Exiting without $finish; no events left\\n\"););\n");
2020-04-21 20:45:23 -04:00
puts("}\n");
puts("\n");
puts("// Final model cleanup\n");
puts("topp->final();\n");
puts("return 0;\n");
2020-04-21 20:45:23 -04:00
puts("}\n");
m_ofp = nullptr;
2020-04-21 20:45:23 -04:00
}
};
//######################################################################
// EmitC class functions
void V3EmitCMain::emit() {
UINFO(2, __FUNCTION__ << ": " << endl);
{ EmitCMain visitor; }
2020-04-21 20:45:23 -04:00
}