PandA-2024.02
VIVADO_xsim_wrapper.cpp
Go to the documentation of this file.
1 /*
2  *
3  * _/_/_/ _/_/ _/ _/ _/_/_/ _/_/
4  * _/ _/ _/ _/ _/_/ _/ _/ _/ _/ _/
5  * _/_/_/ _/_/_/_/ _/ _/_/ _/ _/ _/_/_/_/
6  * _/ _/ _/ _/ _/ _/ _/ _/ _/
7  * _/ _/ _/ _/ _/ _/_/_/ _/ _/
8  *
9  * ***********************************************
10  * PandA Project
11  * URL: http://panda.dei.polimi.it
12  * Politecnico di Milano - DEIB
13  * System Architectures Group
14  * ***********************************************
15  * Copyright (C) 2004-2024 Politecnico di Milano
16  *
17  * This file is part of the PandA framework.
18  *
19  * The PandA framework is free software; you can redistribute it and/or modify
20  * it under the terms of the GNU General Public License as published by
21  * the Free Software Foundation; either version 3 of the License, or
22  * (at your option) any later version.
23  *
24  * This program is distributed in the hope that it will be useful,
25  * but WITHOUT ANY WARRANTY; without even the implied warranty of
26  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
27  * GNU General Public License for more details.
28  *
29  * You should have received a copy of the GNU General Public License
30  * along with this program. If not, see <http://www.gnu.org/licenses/>.
31  *
32  */
44 #include "VIVADO_xsim_wrapper.hpp"
45 
46 #include "Parameter.hpp"
47 #include "ToolManager.hpp"
48 #include "constant_strings.hpp"
49 #include "dbgPrintHelper.hpp"
50 #include "fileIO.hpp"
51 #include "utility.hpp"
52 
53 #include <boost/algorithm/string.hpp>
54 #include <filesystem>
55 #include <fstream>
56 #include <utility>
57 
58 #define XSIM_VLOG "xvlog"
59 #define XSIM_VHDL "xvhdl"
60 #define XSIM_XELAB "xelab"
61 #define XSIM_XSC "xsc"
62 
63 // constructor
64 VIVADO_xsim_wrapper::VIVADO_xsim_wrapper(const ParameterConstRef& _Param, const std::string& _suffix,
65  const std::string& _top_fname, const std::string& _inc_dirs)
66  : SimulationTool(_Param, _top_fname, _inc_dirs), suffix(_suffix)
67 {
68  PRINT_DBG_MEX(DEBUG_LEVEL_PEDANTIC, debug_level, "Creating the XSIM wrapper...");
69  std::filesystem::create_directory(XSIM_SUBDIR + suffix + "/");
70 }
71 
72 // destructor
74 
76 {
77 }
78 
79 static const std::string& create_project_file(const std::string& project_filename,
80  const std::list<std::string>& file_list)
81 {
82  std::ofstream prj_file(project_filename);
83  for(auto const& file : file_list)
84  {
85  if(ends_with(file, "mdpi.c"))
86  {
87  continue;
88  }
89  std::filesystem::path file_path(file);
90  const auto extension = file_path.extension().string();
91  if(extension == ".vhd" || extension == ".vhdl" || extension == ".VHD" || extension == ".VHDL")
92  {
93  prj_file << "VHDL";
94  }
95  else if(extension == ".v" || extension == ".V" || extension == ".sv" || extension == ".SV")
96  {
97  prj_file << "SV";
98  }
99  else
100  {
101  THROW_ERROR("Extension not recognized! " + extension);
102  }
103  prj_file << " work ";
104  const auto filename = file_path.string();
105  if(filename[0] != '/')
106  {
107  prj_file << std::filesystem::path(GetCurrentPath()).string() << "/";
108  }
109  prj_file << filename << std::endl;
110  }
111  prj_file.close();
112  return project_filename;
113 }
114 
115 std::string VIVADO_xsim_wrapper::GenerateScript(std::ostream& script, const std::string& top_filename,
116  const std::list<std::string>& file_list)
117 {
118  script << "#configuration" << std::endl;
119  const auto setupscr =
120  Param->isOption(OPT_xilinx_vivado_settings) ? Param->getOption<std::string>(OPT_xilinx_vivado_settings) : "";
121  if(!setupscr.empty())
122  {
123  script << ". " << setupscr << " >& /dev/null;" << std::endl << std::endl;
124  }
125  script << "BEH_DIR=\"" << XSIM_SUBDIR << suffix << "\"" << std::endl << "BEH_CC=\"${CC}\"" << std::endl << std::endl;
126  log_file = "${BEH_DIR}/" + top_filename + "_xsim.log";
127  const auto xilinx_root = Param->isOption(OPT_xilinx_root) ? Param->getOption<std::string>(OPT_xilinx_root) : "";
128  std::string beh_cflags =
129  "-DXILINX_SIMULATOR " + (xilinx_root.size() ? ("-isystem " + xilinx_root + "/data/xsim/include") : "");
130  const auto cflags = GenerateLibraryBuildScript(script, "${BEH_DIR}", beh_cflags);
131  const auto vflags = [&]() {
132  std::string flags;
133  if(cflags.find("-m32") != std::string::npos)
134  {
135  flags += " -define __M32";
136  }
137  else if(cflags.find("-mx32") != std::string::npos)
138  {
139  flags += " -define __MX32";
140  }
141  else if(cflags.find("-m64") != std::string::npos)
142  {
143  flags += " -define __M64";
144  }
145  if(Param->isOption(OPT_generate_vcd) && Param->getOption<bool>(OPT_generate_vcd))
146  {
147  flags += " -define GENERATE_VCD";
148  }
149  if(Param->isOption(OPT_discrepancy) && Param->getOption<bool>(OPT_discrepancy))
150  {
151  flags += " -define GENERATE_VCD_DISCREPANCY";
152  }
153  const auto inc_dir_list = string_to_container<std::vector<std::string>>(inc_dirs, ",");
154  for(const auto& inc : inc_dir_list)
155  {
156  flags += " -i " + inc;
157  }
158  return flags;
159  }();
160  const auto prj_file = create_project_file(XSIM_SUBDIR + suffix + "/" + top_filename + ".prj", file_list);
161 
162  std::string sim_cmd =
163  "rm -rf xsim.* xelab.*; " XSIM_XELAB " -sv_root ${BEH_DIR} -sv_lib libmdpi " + vflags + " -prj " + prj_file;
164  if(Param->isOption(OPT_assert_debug) && Param->getOption<bool>(OPT_assert_debug))
165  {
166  sim_cmd += " --debug all --rangecheck -O2";
167  }
168  else
169  {
170  sim_cmd += " --debug off -O3";
171  }
172  sim_cmd += " -L work -L unifast_ver -L unisims_ver -L unimacro_ver -L secureip --snapshot " + top_filename +
173  "tb_behav work.clocked_bambu_testbench -nolog -stat -R";
174  sim_cmd += " 2>&1 | tee " + log_file;
175  return sim_cmd;
176 }
177 
179 {
180 }
static const std::string & create_project_file(const std::string &project_filename, const std::list< std::string > &file_list)
File containing functions and utilities to support the printing of debug messagges.
#define PRINT_DBG_MEX(dbgLevel, curDbgLevel, mex)
We are producing a debug version of the program, so the message is printed;.
#define DEBUG_LEVEL_PEDANTIC
very verbose debugging print is performed.
std::string filename
std::string log_file
log file
~VIVADO_xsim_wrapper() override
Destructor.
const ParameterConstRef Param
class containing all the parameters
std::string GenerateScript(std::ostream &script, const std::string &top_filename, const std::list< std::string > &file_list) override
Generates the proper simulation script.
void CheckExecution() override
Checks if the current specification can be executed or not.
VIVADO_xsim_wrapper(const ParameterConstRef &Param, const std::string &suffix, const std::string &top_fname, const std::string &inc_dirs)
Constructor.
#define XSIM_SUBDIR
std::string GenerateLibraryBuildScript(std::ostream &script, const std::string &libtb_filename, std::string &beh_cflags) const
utility function used to read files.
bool ends_with(const std::string &str, const std::string &pattern)
This file collects some utility functions and macros.
#define THROW_ERROR(str_expr)
helper function used to throw an error in a standard way
Definition: exceptions.hpp:263
Class to manage a wrapped tool.
int debug_level
debug level of the class
Template borrowed from the ANTLR library by Terence Parr (http://www.jGuru.com - Software rights: htt...
Definition: refcount.hpp:94
Wrapper to XSIM by XILINX VIVADO.
this class is used to manage the command-line or XML options.
void Clean() const override
Remove files created during simulation.
constant strings
std::string GetCurrentPath()
Definition: fileIO.hpp:123
std::string suffix
suffix added to the XSIM dir
const std::string inc_dirs
comma separated list of include dirs
#define XSIM_XELAB

Generated on Mon Feb 12 2024 13:02:56 for PandA-2024.02 by doxygen 1.8.13