OpenASIP 2.2
Loading...
Searching...
No Matches
FUImplementation.cc
Go to the documentation of this file.
1/*
2 Copyright (c) 2002-2009 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 FUImplementation.cc
26 *
27 * Implementation of FUImplementation class.
28 *
29 * @author Lasse Laasonen 2005 (lasse.laasonen-no.spam-tut.fi)
30 * @note rating: red
31 */
32
33#include <string>
34#include <algorithm>
35
36#include "FUImplementation.hh"
37#include "FUEntry.hh"
38#include "FUExternalPort.hh"
41
42#include "SequenceTools.hh"
43#include "MapTools.hh"
44#include "ContainerTools.hh"
45#include "StringTools.hh"
46#include "MathTools.hh"
47
48using std::string;
49
50namespace HDB {
51
52/**
53 * The constructor.
54 *
55 * @param name Name of the module.
56 * @param opcodePort Name of the opcode port.
57 * @param clkPort Name of the clock port.
58 * @param rstPort Name of the reset port.
59 * @param glockPort Name of the global lock port.
60 * @param glockReqPort Name of the global lock request port.
61 */
63 const std::string& name,
64 const std::string& opcodePort,
65 const std::string& clkPort,
66 const std::string& rstPort,
67 const std::string& glockPort,
68 const std::string& glockReqPort) :
69 HWBlockImplementation(name, clkPort, rstPort, glockPort),
70 opcodePort_(opcodePort), glockReqPort_(glockReqPort) {
71}
72
73
74/**
75 * Copy constructor.
76 *
77 * @param original FUImplementation to copy.
78 */
80 HWBlockImplementation(original) {
81
82 opcodePort_ = original.opcodePort_;
84 opcodes_ = original.opcodes_;
85 parameters_ = original.parameters_;
86
87 // Deep copy architecture ports.
88 for (int i = 0; i < original.architecturePortCount(); i++) {
89
90 FUPortImplementation* portCopy =
92
93 addArchitecturePort(portCopy);
94 }
95
96 // Deep copy external ports.
97 for (int i = 0; i < original.externalPortCount(); i++) {
98
99 FUExternalPort* portCopy =
100 new FUExternalPort(original.externalPort(i));
101
102 addExternalPort(portCopy);
103 }
104}
105
106
107
108/**
109 * The destructor.
110 *
111 * Deletes all the ports.
112 */
117
118/**
119 * Sets the name of the opcode port.
120 *
121 * @param name Name of the port.
122 */
123void
124FUImplementation::setOpcodePort(const std::string& name) {
125 opcodePort_ = name;
126}
127
128
129/**
130 * Returns the name of the opcode port.
131 *
132 * @return The name of the opcode port.
133 */
134std::string
136 return opcodePort_;
137}
138
139
140/**
141 * Sets the name of the global lock request port.
142 *
143 * @param name Name of the port.
144 */
145void
146FUImplementation::setGlockReqPort(const std::string& name) {
147 glockReqPort_ = name;
148}
149
150
151/**
152 * Returns the name of the global lock request port.
153 *
154 * @return The name of the global lock request port.
155 */
156std::string
160
161
162/**
163 * Sets operation code for the given operation.
164 *
165 * @param operation Name of the operation.
166 * @param opcode The operation code.
167 */
168void
169FUImplementation::setOpcode(const std::string& operation, int opcode) {
170 unsetOpcode(operation);
171 string opName = StringTools::stringToUpper(operation);
172 opcodes_.insert(std::pair<std::string, int>(opName, opcode));
173}
174
175
176/**
177 * Unsets the opcode of the given operation.
178 *
179 * @param operation Name of the operation.
180 */
181void
182FUImplementation::unsetOpcode(const std::string& operation) {
183 string opName = StringTools::stringToUpper(operation);
184 opcodes_.erase(opName);
185}
186
187
188/**
189 * Returns the number of opcodes in the FU.
190 *
191 * @return The number of opcodes.
192 */
193int
195 return opcodes_.size();
196}
197
198
199/**
200 * By the given index, returns an operation that has an opcode.
201 *
202 * @param index The index.
203 * @exception OutOfRange If the given index is negative or not smaller than
204 * the number of opcodes.
205 */
206std::string
208 if (index < 0 || index >= opcodeCount()) {
209 throw OutOfRange(__FILE__, __LINE__, __func__);
210 }
211
212 OpcodeMap::const_iterator iter = opcodes_.begin();
213 for (int i = 0; i < index; i++) {
214 iter++;
215 }
216
217 return iter->first;
218}
219
220/**
221 * Tells whether there is an opcode defined for the given operation.
222 *
223 * @param operation Name of the operation.
224 * @return True if there is an opcode, otherwise false.
225 */
226bool
227FUImplementation::hasOpcode(const std::string& operation) const {
228 string opName = StringTools::stringToUpper(operation);
229 return MapTools::containsKey(opcodes_, opName);
230}
231
232
233/**
234 * Returns the opcode of the given operation.
235 *
236 * @param operation The operation.
237 * @return The operation code.
238 * @exception KeyNotFound If there is no opcode defined for the given
239 * operation.
240 */
241int
242FUImplementation::opcode(const std::string& operation) const {
243 string opName = StringTools::stringToUpper(operation);
244 return MapTools::valueForKey<int>(opcodes_, opName);
245}
246
247/**
248 * Returns the maximum bitwidth of an opcode in this FU implementation.
249 *
250 * The bitwidth is not stored explicitly in HDB, this method is provided
251 * only for convenience.
252 *
253 * @return The maximum bitwidth needed by an opcode in this FU.
254 *
255 */
256int
258 int maxFound = 0;
259 for (OpcodeMap::const_iterator i = opcodes_.begin(); i != opcodes_.end();
260 ++i) {
261 maxFound = std::max(MathTools::requiredBits((*i).second), maxFound);
262 }
263 return maxFound;
264}
265
266
267
268/**
269 * Adds the given architectural port.
270 *
271 * @param port The port to be added.
272 */
273void
277
278
279/**
280 * Deletes the given architectural port.
281 *
282 * @param port The port to delete.
283 * @exception InstanceNotFound If the given port is not in this FU
284 * implementation.
285 */
286void
288 bool removed = ContainerTools::deleteValueIfExists(ports_, port);
289 if (!removed) {
290 throw InstanceNotFound(__FILE__, __LINE__, __func__);
291 }
292}
293
294/**
295 * Adds the given external port.
296 *
297 * @param port The port to be added.
298 */
299void
303
304
305/**
306 * Deletes the given external port.
307 *
308 * @param port The port to delete.
309 * @exception InstanceNotFound If the given port is not in this FU
310 * implementation.
311 */
312void
315 if (!removed) {
316 throw InstanceNotFound(__FILE__, __LINE__, __func__);
317 }
318}
319
320/**
321 * Returns the number of architectural ports.
322 *
323 * @return The number of ports.
324 */
325int
327 return ports_.size();
328}
329
330
331/**
332 * Returns the number of external ports.
333 *
334 * @return The number of external ports.
335 */
336int
338 return externalPorts_.size();
339}
340
341
342/**
343 * Returns the architectural port implementation at the given position.
344 *
345 * @param index The position index.
346 * @return The port implementation.
347 * @exception OutOfRange If the given index is negative or not smaller the
348 * number of architectural ports.
349 */
352 if (index < 0 || index >= architecturePortCount()) {
353 const string procName = "FUImplementation::architecturePort";
354 throw OutOfRange(__FILE__, __LINE__, procName);
355 }
356
357 return *ports_[index];
358}
359
360/**
361 * Returns the implementation of a port for the given port architecture
362 * name.
363 *
364 * @param architectureName Name of the port in architecture table.
365 * @return The port implementation.
366 * @exception InstanceNotFound If no port with the given architecture name
367 * is found.
368 */
371 const std::string& architectureName) const {
372 for (int i = 0; i < architecturePortCount(); ++i) {
373 if (ports_[i]->architecturePort() == architectureName)
374 return *ports_[i];
375 }
376 throw InstanceNotFound(
377 __FILE__, __LINE__, __func__,
378 "No port implementation with the given architecture name found.");
379}
380
381/**
382 * Returns the external port at the given position.
383 *
384 * @param index The position index.
385 * @return The port.
386 * @exception OutOfRange If the given index is negative or not smaller the
387 * number of external ports.
388 */
391 if (index < 0 || index >= externalPortCount()) {
392 const string procName = "FUImplementation::externalPort";
393 throw OutOfRange(__FILE__, __LINE__, procName);
394 }
395
396 return *externalPorts_[index];
397}
398
399/**
400 * Adds the given parameter for the implementation.
401 *
402 * @param name Name of the parameter.
403 * @param type Type of the parameter.
404 * @param value Value of the parameter.
405 * @exception IllegalParameters If the FU implementation contains a
406 * parameter with the same name already.
407 */
408void
410 const std::string& name, const std::string& type,
411 const std::string& value) {
412 if (hasParameter(name)) {
413 throw IllegalParameters(__FILE__, __LINE__, __func__);
414 } else {
415 Parameter param = {name, type, value};
416 parameters_.push_back(param);
417 }
418}
419
420/**
421 * Removes the parameter of the given name.
422 *
423 * @param name Name of the parameter.
424 */
425void
426FUImplementation::removeParameter(const std::string& name) {
427 for (ParameterTable::iterator iter = parameters_.begin();
428 iter != parameters_.end(); iter++) {
429 if (iter->name == name) {
430 parameters_.erase(iter);
431 return;
432 }
433 }
434}
435
436
437/**
438 * Returns the number of parameters.
439 *
440 * @return The number of parameters.
441 */
442int
444 return parameters_.size();
445}
446
447
448/**
449 * Returns a parameter by the given index.
450 *
451 * @param index The index.
452 * @return The parameter.
453 * @exception OutOfRange If the index is negative or not smaller than the
454 * number of parameters.
455 */
458 if (index < 0 || index >= parameterCount()) {
459 throw OutOfRange(__FILE__, __LINE__, __func__);
460 }
461
462 return parameters_[index];
463}
464
465/**
466 * Tells whether the implementation has the given parameter.
467 *
468 * @param name Name of the parameter.
469 * @return True if the implementation has the parameter, otherwise false.
470 */
471bool
472FUImplementation::hasParameter(const std::string& name) const {
473 for (ParameterTable::const_iterator iter = parameters_.begin();
474 iter != parameters_.end(); iter++) {
475 if (iter->name == name) {
476 return true;
477 }
478 }
479 return false;
480}
481}
#define __func__
static bool deleteValueIfExists(ContainerType &aContainer, const ElementType &aKey)
void removeParameter(const std::string &name)
FUPortImplementation & architecturePort(int index) const
ParameterTable parameters_
Contains the parameters.
void setGlockReqPort(const std::string &name)
void addArchitecturePort(FUPortImplementation *port)
Parameter parameter(int index) const
void unsetOpcode(const std::string &operation)
void setOpcodePort(const std::string &name)
bool hasOpcode(const std::string &operation) const
std::string glockReqPort_
Name of the global lock request port.
std::string glockReqPort() const
bool hasParameter(const std::string &name) const
std::string opcodeOperation(int index) const
void deleteExternalPort(FUExternalPort *port)
std::string opcodePort_
Name of the opcode port.
OpcodeMap opcodes_
Contains the operation codes.
void addExternalPort(FUExternalPort *port)
void addParameter(const std::string &name, const std::string &type, const std::string &value)
FUImplementation(const std::string &name, const std::string &opcodePort, const std::string &clkPort, const std::string &rstPort, const std::string &glockPort, const std::string &glockReqPort)
void deleteArchitecturePort(FUPortImplementation *port)
FUPortImplementation & portImplementationByArchitectureName(const std::string &architectureName) const
PortTable ports_
Contains the architectural ports.
FUExternalPort & externalPort(int index) const
std::string opcodePort() const
int opcode(const std::string &operation) const
ExternalPortTable externalPorts_
Contains the external ports.
void setOpcode(const std::string &operation, int opcode)
static KeyType keyForValue(const MapType &aMap, const ValueType &aValue)
static bool containsKey(const MapType &aMap, const KeyType &aKey)
static int requiredBits(unsigned long int number)
static void deleteAllItems(SequenceType &aSequence)
static std::string stringToUpper(const std::string &source)