OpenASIP  2.0
TestbenchGenerator.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 TestbenchGenerator.cc
26  *
27  * Implementation of TestbenchGenerator class
28  *
29  * @author Otto Esko 2010 (otto.esko-no.spam-tut.fi)
30  * @note rating: red
31  */
32 
33 #include <string>
34 #include <sstream>
35 #include <fstream>
36 #include <vector>
37 #include <map>
38 #include <stdint.h>
39 #include <boost/format.hpp>
40 #include "TestbenchGenerator.hh"
41 #include "StringTools.hh"
42 #include "Environment.hh"
43 #include "FileSystem.hh"
44 #include "Conversion.hh"
45 #include "HWBlockImplementation.hh"
46 
47 using std::string;
48 using std::vector;
49 using std::ostringstream;
50 using std::ifstream;
51 
52 #define INDENT " "
53 
54 const std::string TestbenchGenerator::TB_TEMPLATE_ =
55  "testbench.vhdl.template";
56 
58  componentDeclaration_(), componentBinding_(), signalDeclaration_(),
59  componentInstantiation_(), inputArrays_(), opcodeArrays_(),
60  loadSignalArrays_(), outputArrays_(), testbenchCode_() {
61 }
62 
64 }
65 
66 
67 void
69  std::ostringstream& stream,
70  std::vector<uint32_t>& dataArray, std::string portName, int portWidth) {
71 
72  stream
73  << INDENT INDENT << "type " << portName
74  << "_data_array is array (natural range <>) of" << std::endl
76  << "std_logic_vector(" << portWidth - 1 << " downto 0);"
77  << std::endl << std::endl << INDENT INDENT
78  << "constant " << portName << "_data : "
79  << portName << "_data_array :=" << std::endl;
80 
81  for (std::size_t i = 0; i < dataArray.size(); ++i) {
82  uint32_t input = dataArray.at(i);
83  std::string inputAsBinaryLiteral =
84  Conversion::toBinary(input, portWidth);
85  stream << INDENT INDENT;
86 
87  if (i == 0) {
88  stream << "(";
89  } else {
90  stream << " ";
91  }
92  stream << "\"" << inputAsBinaryLiteral << "\"";
93  if (i == dataArray.size() - 1) {
94  stream << ");";
95  } else {
96  stream << ",";
97  }
98  stream << "\t -- @" << i << " = " << input << std::endl;
99  }
100  stream << std::endl;
101 }
102 
103 void
105  int totalCycles, int outputIgnoreCycles) {
106 
108  << INDENT INDENT << "constant IGNORE_OUTPUT_COUNT : integer := "
109  << outputIgnoreCycles << ";" << std::endl;
111  << INDENT INDENT << "constant TOTAL_CYCLE_COUNT : integer := "
112  << totalCycles << ";" << std::endl;
113 }
114 
115 void
117  std::ofstream& file, HDB::HWBlockImplementation* impl) {
118 
119  string templateFile = findVhdlTemplate();
120  string vhdlTemplate = "";
121  loadVhdlTemplate(templateFile, vhdlTemplate);
122 
123  string testBench =
124  (boost::format(vhdlTemplate)
125  % componentDeclaration_.str()
126  % componentBinding_.str()
127  % signalDeclaration_.str()
129  % inputArrays_.str()
130  % opcodeArrays_.str()
131  % loadSignalArrays_.str()
132  % outputArrays_.str()
133  % impl->clkPort()
134  % impl->rstPort()
135  % impl->glockPort()
136  % testbenchCode_.str()).str();
137 
138  file << testBench;
139 }
140 
141 std::string
143 
144  vector<string> paths = Environment::implementationTesterTemplatePaths();
145  for (unsigned int i = 0; i < paths.size(); i++) {
146  string file = paths.at(i) + FileSystem::DIRECTORY_SEPARATOR
147  + TB_TEMPLATE_;
148  if (FileSystem::fileExists(file)) {
149  return file;
150  }
151  }
152 
153  InvalidData exception(__FILE__, __LINE__, "",
154  "The VHDL template file " + TB_TEMPLATE_ +
155  "not found");
156  throw exception;
157 
158  string notFound = "";
159  return notFound;
160 }
161 
162 void
164  const std::string& fileName, std::string& vhdlTemplate) const {
165 
166  ifstream input(fileName.c_str());
167  if (!input.is_open()) {
168  InvalidData exception(__FILE__, __LINE__, "",
169  "The VHDL template file " + fileName +
170  "unreadable.");
171  throw exception;
172  }
173 
174  string line = "";
175  while (getline(input, line)) {
176  vhdlTemplate += line;
177  vhdlTemplate += "\n";
178  }
179  input.close();
180 }
181 
182 std::ostringstream&
184  return componentDeclaration_;
185 }
186 
187 std::ostringstream&
189  return componentBinding_;
190 }
191 
192 std::ostringstream&
194  return signalDeclaration_;
195 }
196 
197 std::ostringstream&
200 }
201 
202 std::ostringstream&
204  return inputArrays_;
205 }
206 
207 std::ostringstream&
209  return opcodeArrays_;
210 }
211 
212 std::ostringstream&
214  return loadSignalArrays_;
215 }
216 
217 std::ostringstream&
219  return outputArrays_;
220 }
221 
222 std::ostringstream&
224  return testbenchCode_;
225 }
TestbenchGenerator::bindingStream
std::ostringstream & bindingStream()
Definition: TestbenchGenerator.cc:188
HDB::HWBlockImplementation::clkPort
std::string clkPort() const
Definition: HWBlockImplementation.cc:175
TestbenchGenerator::tbCodeStream
std::ostringstream & tbCodeStream()
Definition: TestbenchGenerator.cc:223
TestbenchGenerator::writeTestbench
void writeTestbench(std::ofstream &file, HDB::HWBlockImplementation *impl)
Definition: TestbenchGenerator.cc:116
FileSystem.hh
TestbenchGenerator::opcodeArrayStream
std::ostringstream & opcodeArrayStream()
Definition: TestbenchGenerator.cc:208
TestbenchGenerator::instantiationStream
std::ostringstream & instantiationStream()
Definition: TestbenchGenerator.cc:198
TestbenchGenerator::writeTbConstants
void writeTbConstants(int totalCycles, int outputIgnoreCycles)
Definition: TestbenchGenerator.cc:104
TestbenchGenerator::componentDeclaration_
std::ostringstream componentDeclaration_
Definition: TestbenchGenerator.hh:85
TestbenchGenerator::inputArrays_
std::ostringstream inputArrays_
Definition: TestbenchGenerator.hh:89
TestbenchGenerator::inputArrayStream
std::ostringstream & inputArrayStream()
Definition: TestbenchGenerator.cc:203
TestbenchGenerator::writeStimulusArray
virtual void writeStimulusArray(std::ostringstream &stream, std::vector< uint32_t > &dataArray, std::string portName, int portWidth)
Definition: TestbenchGenerator.cc:68
Environment::implementationTesterTemplatePaths
static std::vector< std::string > implementationTesterTemplatePaths()
Definition: Environment.cc:1129
HDB::HWBlockImplementation::rstPort
std::string rstPort() const
Definition: HWBlockImplementation.cc:197
INDENT
#define INDENT
Definition: TestbenchGenerator.cc:52
StringTools.hh
TestbenchGenerator::TB_TEMPLATE_
static const std::string TB_TEMPLATE_
Definition: TestbenchGenerator.hh:95
TestbenchGenerator::componentBinding_
std::ostringstream componentBinding_
Definition: TestbenchGenerator.hh:86
TestbenchGenerator::declarationStream
std::ostringstream & declarationStream()
Definition: TestbenchGenerator.cc:183
Conversion::toBinary
static std::string toBinary(unsigned int source, unsigned int stringWidth=0)
Definition: Conversion.cc:155
TestbenchGenerator::loadSignalArrays_
std::ostringstream loadSignalArrays_
Definition: TestbenchGenerator.hh:91
InvalidData
Definition: Exception.hh:149
Conversion.hh
TestbenchGenerator::TestbenchGenerator
TestbenchGenerator()
Definition: TestbenchGenerator.cc:57
TestbenchGenerator::signalStream
std::ostringstream & signalStream()
Definition: TestbenchGenerator.cc:193
Environment.hh
TestbenchGenerator::componentInstantiation_
std::ostringstream componentInstantiation_
Definition: TestbenchGenerator.hh:88
TestbenchGenerator.hh
HDB::HWBlockImplementation
Definition: HWBlockImplementation.hh:49
TestbenchGenerator::opcodeArrays_
std::ostringstream opcodeArrays_
Definition: TestbenchGenerator.hh:90
TestbenchGenerator::~TestbenchGenerator
virtual ~TestbenchGenerator()
Definition: TestbenchGenerator.cc:63
FileSystem::DIRECTORY_SEPARATOR
static const std::string DIRECTORY_SEPARATOR
Definition: FileSystem.hh:189
TestbenchGenerator::findVhdlTemplate
std::string findVhdlTemplate() const
Definition: TestbenchGenerator.cc:142
FileSystem::fileExists
static bool fileExists(const std::string fileName)
TestbenchGenerator::loadArrayStream
std::ostringstream & loadArrayStream()
Definition: TestbenchGenerator.cc:213
TestbenchGenerator::loadVhdlTemplate
void loadVhdlTemplate(const std::string &fileName, std::string &vhdlTemplate) const
Definition: TestbenchGenerator.cc:163
HDB::HWBlockImplementation::glockPort
std::string glockPort() const
Definition: HWBlockImplementation.cc:219
TestbenchGenerator::signalDeclaration_
std::ostringstream signalDeclaration_
Definition: TestbenchGenerator.hh:87
TestbenchGenerator::outputArrayStream
std::ostringstream & outputArrayStream()
Definition: TestbenchGenerator.cc:218
TestbenchGenerator::outputArrays_
std::ostringstream outputArrays_
Definition: TestbenchGenerator.hh:92
HWBlockImplementation.hh
TestbenchGenerator::testbenchCode_
std::ostringstream testbenchCode_
Definition: TestbenchGenerator.hh:93