2020-04-22 02:45:23 +02:00
|
|
|
// -*- mode: C++; c-file-style: "cc-mode" -*-
|
|
|
|
|
//*************************************************************************
|
|
|
|
|
// DESCRIPTION: Verilator: Emit C++ for tree
|
|
|
|
|
//
|
|
|
|
|
// Code available from: https://verilator.org
|
|
|
|
|
//
|
|
|
|
|
//*************************************************************************
|
|
|
|
|
//
|
2021-01-01 16:29:54 +01:00
|
|
|
// Copyright 2003-2021 by Wilson Snyder. This program is free software; you
|
2020-04-22 02:45:23 +02: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"
|
|
|
|
|
|
|
|
|
|
#include "V3Global.h"
|
|
|
|
|
#include "V3EmitC.h"
|
|
|
|
|
#include "V3EmitCBase.h"
|
|
|
|
|
#include "V3EmitCMain.h"
|
|
|
|
|
|
|
|
|
|
#include <map>
|
|
|
|
|
|
|
|
|
|
//######################################################################
|
|
|
|
|
|
2020-11-19 03:32:16 +01:00
|
|
|
class EmitCMain final : EmitCBaseVisitor {
|
2020-04-22 02:45:23 +02:00
|
|
|
// METHODS
|
|
|
|
|
|
|
|
|
|
// VISITORS
|
2020-05-17 00:02:54 +02:00
|
|
|
// This visitor doesn't really iterate, but exist to appease base class
|
2020-08-15 17:44:10 +02:00
|
|
|
virtual void visit(AstNode* nodep) override { iterateChildren(nodep); } // LCOV_EXCL_LINE
|
2020-04-22 02:45:23 +02:00
|
|
|
|
|
|
|
|
public:
|
|
|
|
|
// CONSTRUCTORS
|
2021-07-12 00:42:01 +02:00
|
|
|
EmitCMain() { emitInt(); }
|
2020-04-22 02:45:23 +02:00
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
// MAIN METHOD
|
|
|
|
|
void emitInt() {
|
2021-06-21 00:32:57 +02:00
|
|
|
const string filename = v3Global.opt.makeDir() + "/" + topClassName() + "__main.cpp";
|
2020-04-22 02:45:23 +02:00
|
|
|
newCFile(filename, false /*slow*/, true /*source*/);
|
2021-07-12 00:42:01 +02:00
|
|
|
V3OutCFile cf{filename};
|
2020-04-22 02:45:23 +02:00
|
|
|
m_ofp = &cf;
|
|
|
|
|
|
2021-03-07 17:01:54 +01:00
|
|
|
// Not defining main_time/vl_time_stamp, so
|
|
|
|
|
v3Global.opt.addCFlags("-DVL_TIME_CONTEXT"); // On MSVC++ anyways
|
|
|
|
|
|
2020-04-22 02:45:23 +02:00
|
|
|
// Heavly commented output, as users are likely to look at or copy this code
|
|
|
|
|
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");
|
2021-03-07 17:01:54 +01:00
|
|
|
puts("// Setup context, defaults, and parse command line\n");
|
2020-04-22 02:45:23 +02:00
|
|
|
puts("Verilated::debug(0);\n");
|
2021-03-07 17:01:54 +01:00
|
|
|
puts("const std::unique_ptr<VerilatedContext> contextp{new VerilatedContext};\n");
|
|
|
|
|
puts("contextp->commandArgs(argc, argv);\n");
|
2021-02-21 17:02:02 +01:00
|
|
|
puts("\n");
|
|
|
|
|
|
2020-04-22 02:45:23 +02:00
|
|
|
puts("// Construct the Verilated model, from Vtop.h generated from Verilating\n");
|
2021-03-07 17:01:54 +01:00
|
|
|
puts("const std::unique_ptr<" + topClassName() + "> topp{new " + topClassName()
|
|
|
|
|
+ "{contextp.get()}};\n");
|
2021-02-21 17:02:02 +01:00
|
|
|
puts("\n");
|
|
|
|
|
|
2020-04-22 02:45:23 +02:00
|
|
|
puts("// Evaluate initials\n");
|
|
|
|
|
puts("topp->eval(); // Evaluate\n");
|
2021-02-21 17:02:02 +01:00
|
|
|
puts("\n");
|
2020-04-22 02:45:23 +02:00
|
|
|
|
|
|
|
|
puts("// Simulate until $finish\n");
|
2021-03-07 17:01:54 +01:00
|
|
|
puts("while (!contextp->gotFinish()) {\n");
|
2020-04-24 03:22:47 +02:00
|
|
|
puts(/**/ "// Evaluate model\n");
|
|
|
|
|
puts(/**/ "topp->eval();\n");
|
|
|
|
|
puts(/**/ "// Advance time\n");
|
2021-03-07 17:01:54 +01:00
|
|
|
puts(/**/ "contextp->timeInc(1);\n");
|
|
|
|
|
|
2020-04-24 03:22:47 +02:00
|
|
|
puts("}\n");
|
|
|
|
|
puts("\n");
|
|
|
|
|
|
2021-03-07 17:01:54 +01:00
|
|
|
puts("if (!contextp->gotFinish()) {\n");
|
2020-04-24 03:22:47 +02:00
|
|
|
puts(/**/ "VL_DEBUG_IF(VL_PRINTF(\"+ Exiting without $finish; no events left\\n\"););\n");
|
2020-04-22 02:45:23 +02:00
|
|
|
puts("}\n");
|
|
|
|
|
puts("\n");
|
|
|
|
|
|
|
|
|
|
puts("// Final model cleanup\n");
|
|
|
|
|
puts("topp->final();\n");
|
2021-02-21 17:02:02 +01:00
|
|
|
puts("return 0;\n");
|
2020-04-22 02:45:23 +02:00
|
|
|
puts("}\n");
|
2021-07-25 19:38:27 +02:00
|
|
|
|
|
|
|
|
m_ofp = nullptr;
|
2020-04-22 02:45:23 +02:00
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
//######################################################################
|
|
|
|
|
// EmitC class functions
|
|
|
|
|
|
|
|
|
|
void V3EmitCMain::emit() {
|
|
|
|
|
UINFO(2, __FUNCTION__ << ": " << endl);
|
2021-07-12 00:42:01 +02:00
|
|
|
{ EmitCMain visitor; }
|
2020-04-22 02:45:23 +02:00
|
|
|
}
|