OpenASIP  2.0
GhdlSimulator.cc
Go to the documentation of this file.
1 /*
2  Copyright (c) 2002-2010 Tampere University.
3 
4  This file is part of TTA-Based Codesign Environment (TCE).
5 
6  Permission is hereby granted, free of charge, to any person obtaining a
7  copy of this software and associated documentation files (the "Software"),
8  to deal in the Software without restriction, including without limitation
9  the rights to use, copy, modify, merge, publish, distribute, sublicense,
10  and/or sell copies of the Software, and to permit persons to whom the
11  Software is furnished to do so, subject to the following conditions:
12 
13  The above copyright notice and this permission notice shall be included in
14  all copies or substantial portions of the Software.
15 
16  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19  THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21  FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22  DEALINGS IN THE SOFTWARE.
23  */
24 /**
25  * @file GhdlSimulator.cc
26  *
27  * Implementation of GhdlSimulator
28  *
29  * @author Otto Esko 2010 (otto.esko-no.spam-tut.fi)
30  * @note rating: red
31  */
32 
33 #include <string>
34 #include <vector>
35 #include <iostream>
36 #include "FileSystem.hh"
38 #include "GhdlSimulator.hh"
39 #include "Application.hh"
40 
41 using std::string;
42 using std::vector;
43 
45  std::string tbFile,
46  std::vector<std::string> hdlFiles,
47  bool verbose,
48  bool leaveDirty):
49  ImplementationSimulator(tbFile, hdlFiles, verbose, leaveDirty) {
50 }
51 
53 }
54 
55 bool GhdlSimulator::compile(std::vector<std::string>& errors) {
56  string workDir = createWorkDir();
57  if (workDir.empty()) {
58  errors.push_back("Couldn't create work dir");
59  return false;
60  }
61  string baseDir = tbDirectory();
62  if (!FileSystem::changeWorkingDir(baseDir)) {
63  errors.push_back("Couldn't change directory to " + baseDir);
64  return false;
65  }
66  for (int i = 0; i < hdlFileCount(); i++) {
67  if (!importFile(file(i), errors)) {
68  return false;
69  } else {
70  errors.clear();
71  }
72  }
73  if (!importFile(tbFile(), errors)) {
74  return false;
75  }
76  errors.clear();
77  return compileDesign(errors);
78 }
79 
80 bool GhdlSimulator::simulate(std::vector<std::string>& errors) {
81  errors.clear();
82  string baseDir = tbDirectory();
83  if (!FileSystem::changeWorkingDir(baseDir)) {
84  errors.push_back("Couldn't change directory to " + baseDir);
85  return false;
86  }
87 
88  string command;
89  if (FileSystem::fileExists("testbench")) {
90  command = "./testbench 2>&1";
91  } else {
92  // In the latest GHDL no executable is produces, thus the simulation
93  // command is different.
94  command = string("ghdl -r --ieee=synopsys --workdir=") + workDir()
95  + " testbench 2>&1";
96  }
97  if (verbose()) {
98  std::cout << command << std::endl;
99  }
100  vector<string> messages;
101  int rv = Application::runShellCommandAndGetOutput(command, messages);
102  // call parse
103  parseErrorMessages(messages, errors);
104  return rv == 0 && errors.size() == 0;
105 }
106 
107 bool
109  std::string file, std::vector<std::string>& errors) {
110  string command = "ghdl -i --ieee=synopsys --workdir="
111  + workDir() + " " + file + " 2>&1";
112  if (verbose()) {
113  // TODO: get output stream
114  std::cout << command << std::endl;
115  }
116  int rv = Application::runShellCommandAndGetOutput(command, errors);
117  return rv == 0;
118 }
119 
120 bool GhdlSimulator::compileDesign(std::vector<std::string>& errors) {
121  string command = "ghdl -m --ieee=synopsys --workdir="
122  + workDir() + " testbench 2>&1";
123  if (verbose()) {
124  // TODO: get output stream
125  std::cout << command << std::endl;
126  }
127  int rv = Application::runShellCommandAndGetOutput(command, errors);
128  return rv == 0;
129 }
GhdlSimulator.hh
FileSystem.hh
ImplementationSimulator::verbose
bool verbose()
Definition: ImplementationSimulator.cc:99
ImplementationSimulator::tbDirectory
std::string tbDirectory() const
Definition: ImplementationSimulator.cc:83
ImplementationSimulator::parseErrorMessages
void parseErrorMessages(std::vector< std::string > &inputMsg, std::vector< std::string > &errors)
Definition: ImplementationSimulator.cc:104
ImplementationSimulator::file
std::string file(int index) const
Definition: ImplementationSimulator.cc:95
ImplementationSimulator::createWorkDir
virtual std::string createWorkDir()
Definition: ImplementationSimulator.cc:66
Application::runShellCommandAndGetOutput
static int runShellCommandAndGetOutput(const std::string &command, std::vector< std::string > &outputLines, std::size_t maxOutputLines=DEFAULT_MAX_OUTPUT_LINES, bool includeStdErr=false)
Definition: Application.cc:338
GhdlSimulator::compile
virtual bool compile(std::vector< std::string > &errors)
Definition: GhdlSimulator.cc:55
ImplementationSimulator::tbFile
std::string tbFile() const
Definition: ImplementationSimulator.cc:87
GhdlSimulator::GhdlSimulator
GhdlSimulator(std::string tbFile, std::vector< std::string > hdlFiles, bool verbose, bool leaveDirty)
Definition: GhdlSimulator.cc:44
ImplementationSimulator::hdlFileCount
int hdlFileCount() const
Definition: ImplementationSimulator.cc:91
ImplementationSimulator.hh
GhdlSimulator::importFile
bool importFile(std::string file, std::vector< std::string > &errors)
Definition: GhdlSimulator.cc:108
Application.hh
ImplementationSimulator::workDir
std::string workDir() const
Definition: ImplementationSimulator.cc:79
GhdlSimulator::compileDesign
bool compileDesign(std::vector< std::string > &errors)
Definition: GhdlSimulator.cc:120
GhdlSimulator::simulate
virtual bool simulate(std::vector< std::string > &errors)
Definition: GhdlSimulator.cc:80
FileSystem::changeWorkingDir
static bool changeWorkingDir(const std::string &path)
Definition: FileSystem.cc:185
FileSystem::fileExists
static bool fileExists(const std::string fileName)
GhdlSimulator::~GhdlSimulator
virtual ~GhdlSimulator()
Definition: GhdlSimulator.cc:52
ImplementationSimulator
Definition: ImplementationSimulator.hh:39