OpenASIP 2.2
Loading...
Searching...
No Matches
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"
46
47using std::string;
48using std::vector;
49using std::ostringstream;
50using std::ifstream;
51
52#define INDENT " "
53
54const std::string TestbenchGenerator::TB_TEMPLATE_ =
55 "testbench.vhdl.template";
56
58 componentDeclaration_(), componentBinding_(), signalDeclaration_(),
59 componentInstantiation_(), inputArrays_(), opcodeArrays_(),
60 loadSignalArrays_(), outputArrays_(), testbenchCode_() {
61}
62
65
66
67void
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
103void
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
115void
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)
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
141std::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
162void
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
182std::ostringstream&
186
187std::ostringstream&
191
192std::ostringstream&
196
197std::ostringstream&
201
202std::ostringstream&
206
207std::ostringstream&
211
212std::ostringstream&
216
217std::ostringstream&
221
222std::ostringstream&
#define INDENT
static std::string toBinary(unsigned int source, unsigned int stringWidth=0)
static std::vector< std::string > implementationTesterTemplatePaths()
static const std::string DIRECTORY_SEPARATOR
static bool fileExists(const std::string fileName)
std::ostringstream testbenchCode_
std::ostringstream & opcodeArrayStream()
std::ostringstream opcodeArrays_
void loadVhdlTemplate(const std::string &fileName, std::string &vhdlTemplate) const
std::ostringstream & inputArrayStream()
void writeTbConstants(int totalCycles, int outputIgnoreCycles)
std::string findVhdlTemplate() const
std::ostringstream signalDeclaration_
std::ostringstream & tbCodeStream()
std::ostringstream outputArrays_
std::ostringstream loadSignalArrays_
static const std::string TB_TEMPLATE_
std::ostringstream & outputArrayStream()
std::ostringstream & instantiationStream()
std::ostringstream componentInstantiation_
std::ostringstream & loadArrayStream()
std::ostringstream componentBinding_
std::ostringstream inputArrays_
std::ostringstream & bindingStream()
std::ostringstream & declarationStream()
std::ostringstream & signalStream()
std::ostringstream componentDeclaration_
void writeTestbench(std::ofstream &file, HDB::HWBlockImplementation *impl)
virtual void writeStimulusArray(std::ostringstream &stream, std::vector< uint32_t > &dataArray, std::string portName, int portWidth)