OpenASIP 2.2
Loading...
Searching...
No Matches
ControlUnit.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 ControlUnit.cc
26 *
27 * Implementation of ControlUnit class.
28 *
29 * @author Lasse Laasonen 2004 (lasse.laasonen-no.spam-tut.fi)
30 * @author Pekka J��skel�inen 2006 (pekka.jaaskelainen-no.spam-tut.fi)
31 */
32
33#include "ControlUnit.hh"
35#include "Guard.hh"
36#include "MOMTextGenerator.hh"
37#include "Application.hh"
38#include "Machine.hh"
39#include "ObjectState.hh"
40#include "Conversion.hh"
41
42using std::string;
43
44namespace TTAMachine {
45
46// initialization of static data members
47const string ControlUnit::OSNAME_CONTROL_UNIT = "control_unit";
48const string ControlUnit::OSKEY_DELAY_SLOTS = "d_slots";
49const string ControlUnit::OSKEY_GUARD_LATENCY = "g_latency";
50const string ControlUnit::OSKEY_RA_PORT = "ra_port";
51
52/**
53 * Constructor.
54 *
55 * @param name Name of the control unit.
56 * @param delaySlots Number of delay instruction slots of the transport pipeline.
57 * @param globalGuardLatency The global guard latency.
58 * @exception OutOfRange If some of the given values is out of valid range.
59 * @exception InvalidName If the given name is not a valid component name.
60 */
62 const string& name, int delaySlots, int globalGuardLatency)
63 : FunctionUnit(name),
64 delaySlots_(delaySlots),
65 globalGuardLatency_(0),
66 raPort_(NULL) {
69}
70
71/**
72 * Constructor.
73 *
74 * Loads the state of the control unit from the given ObjectState instance.
75 * Does not load references to other components.
76 *
77 * @param state The ObjectState instance from which the name is taken.
78 * @exception ObjectStateLoadingException If the given ObjectState instance
79 * is invalid.
80 */
82 : FunctionUnit(state),
83 delaySlots_(0),
84 globalGuardLatency_(0),
85 raPort_(NULL) {
87}
88
89/**
90 * Destructor.
91 */
95
96
97/**
98 * Copies the instance.
99 *
100 * Current FunctionUnit state is copied to a new FunctionUnit object.
101 *
102 * @return Copy of the instance.
103 */
106
107 return new ControlUnit(saveState());
108}
109
110
111/**
112 * Attaches the control unit to machine.
113 *
114 * @param mach The machine.
115 * @exception ComponentAlreadyExists If the given machine already has a
116 * global control unit.
117 */
118void
120 // if global guard latency is zero, there cannot be a register guard
121 // that reads a register of local guard latency zero
124 }
125
126 internalSetMachine(mach);
127 mach.setGlobalControl(*this);
128}
129
130/**
131 * Detaches the control unit from machine.
132 */
133void
135 if (!isRegistered()) {
136 return;
137 } else {
138 Machine* mach = machine();
140 mach->unsetGlobalControl();
141 }
142}
143
144void
146 if (delaySlots < 0) {
147 throw OutOfRange(__FILE__, __LINE__, __func__);
148 }
150}
151
152/**
153 * Sets the global guard latency.
154 *
155 * @param latency The new latency.
156 * @exception OutOfRange If the given latency is negative.
157 */
158void
160 if (latency < 0) {
161 throw OutOfRange(__FILE__, __LINE__, __func__);
162 }
163
164 // if latency is zero, there must not be a guard term that reads a
165 // registr of a register file with local guard latency of zero
166 if (latency == 0 && isRegistered()) {
168 MOMTextGenerator textGen;
169 boost::format text = textGen.text(
171 throw OutOfRange(
172 __FILE__, __LINE__, __func__, text.str());
173 }
174 }
175
176 globalGuardLatency_ = latency;
177}
178
179/**
180 * Returns the number of special register ports in the control unit.
181 *
182 * @return The number of special register ports.
183 */
184int
188
189
190/**
191 * Tells whether the control unit has a special register port of the given
192 * name.
193 *
194 * @return True if the control unit has the port, otherwise false.
195 */
196bool
197ControlUnit::hasSpecialRegisterPort(const std::string& name) const {
198 return hasPort(name) && !hasOperationPort(name);
199}
200
201
202/**
203 * Returns a special register port by the given index.
204 *
205 * @param index The index.
206 * @return A special register port.
207 * @exception OutOfRange If the given index is less than 0 or greater or
208 * equal to the number of special register ports.
209 */
212 const string procName = "ControlUnit::specialRegisterPort";
213
214 if (index < 0) {
215 throw OutOfRange(__FILE__, __LINE__, procName);
216 }
217
218 int portCount = this->portCount();
219 int srPortCount(-1);
220
221 for (int i = 0; i < portCount; i++) {
222 BaseFUPort* port = this->port(i);
223 SpecialRegisterPort* srPort =
224 dynamic_cast<SpecialRegisterPort*>(port);
225 if (srPort != NULL) {
226 srPortCount++;
227 if (srPortCount == index) {
228 return srPort;
229 }
230 }
231 }
232
233 throw OutOfRange(__FILE__, __LINE__, procName);
234}
235
236/**
237 * Returns a special register port by the given name.
238 *
239 * @param name Name of the port.
240 * @return A special register port.
241 * @exception InstanceNotFound If a special register port does not exist by
242 * the given name.
243 */
245ControlUnit::specialRegisterPort(const std::string& name) const {
246 const string procName = "ControlUnit::specialRegisterPort";
247
248 if (!hasPort(name)) {
249 throw InstanceNotFound(
250 __FILE__, __LINE__, procName, "Port not found: " + name);
251 }
252
253 Port* port = this->port(name);
254 SpecialRegisterPort* srPort = dynamic_cast<SpecialRegisterPort*>(port);
255 if (srPort == NULL) {
256 throw InstanceNotFound(__FILE__, __LINE__, procName);
257 }
258 return srPort;
259}
260
261/**
262 * Binds the given port to return address port.
263 *
264 * If there is another port bound to return address port already, unbinds it.
265 *
266 * @param port The port to be bound.
267 * @exception IllegalRegistration If the given port is not a port of this
268 * control unit.
269 */
270void
272 if (port.parentUnit() != this) {
273 const string procName = "ControlUnit::setReturnAddressPort";
274 throw IllegalRegistration(__FILE__, __LINE__, procName);
275 }
276
277 raPort_ = const_cast<SpecialRegisterPort*>(&port);
278}
279
280/**
281 * Unbinds the return address port if one exists.
282 */
283void
287
288
289/**
290 * Tells whether the control unit has a return address port.
291 *
292 * @return True if a return address port exists, otherwise false.
293 */
294bool
296 return raPort_ != NULL;
297}
298
299
300/**
301 * Returns the return address port.
302 *
303 * @return The return address port.
304 * @exception InstanceNotFound If there is no return address port.
305 */
308 if (raPort_ != NULL) {
309 return raPort_;
310 } else {
311 const string procName = "ControlUnit::returnAddressPort";
312 throw InstanceNotFound(__FILE__, __LINE__, procName);
313 }
314}
315
316/**
317 * Saves the contents to an ObjectState tree.
318 *
319 * @return The newly created ObjectState tree.
320 */
323
326
327 // set delay slots
329
330 // set global guard latency
332
333 // set return address port
334 if (hasReturnAddressPort()) {
336 }
337
338 return cUnit;
339}
340
341
342/**
343 * Loads its state from the given ObjectState instance.
344 *
345 * @param state The ObjectState instance.
346 * @exception ObjectStateLoadingException If the given ObjectState instance
347 * invalid or if connections to other
348 * machine parts cannot be made.
349 */
350void
355
356/**
357 * Removes the given port from the control unit and unsets the binding of
358 * return address port if necessary.
359 *
360 * @param port The port to be removed.
361 */
362void
369
370
371/**
372 * Loads the state of the control unit without references to other
373 * components.
374 *
375 * @param state The ObjectState instance from which the state is loaded.
376 * @exception ObjectStateLoadingException If an error occurs while loading
377 * the state.
378 */
379void
381 const string procName = "ControlUnit::loadStateWithoutReferences";
382
383 if (state->name() != OSNAME_CONTROL_UNIT) {
384 throw ObjectStateLoadingException(__FILE__, __LINE__, procName);
385 }
386
387 try {
390 if (state->hasAttribute(OSKEY_RA_PORT)) {
391 string portName = state->stringAttribute(OSKEY_RA_PORT);
393 } else {
395 }
396
397 } catch (Exception& e) {
398 throw ObjectStateLoadingException(__FILE__, __LINE__, procName,
399 e.errorMessage());
400 }
401}
402
403/**
404 * Tells whether the given machine has a guard that reads a register with
405 * local guard latency of zero.
406 *
407 * @param machine The machine.
408 * @return True if the machine has the guard, otherwise false.
409 */
410bool
413 for (int i = 0; i < busNav.count(); i++) {
414 Bus* bus = busNav.item(i);
415 for (int i = 0; i < bus->guardCount(); i++) {
416 Guard* guard = bus->guard(i);
417 RegisterGuard* regGuard =
418 dynamic_cast<RegisterGuard*>(guard);
419 if (regGuard != NULL) {
420 const RegisterFile* rf = regGuard->registerFile();
421 if (rf->guardLatency() == 0) {
422 return true;
423 }
424 }
425 }
426 }
427 return false;
428}
429}
#define __func__
TTAMachine::Machine * machine
the architecture definition of the estimated processor
std::string errorMessage() const
Definition Exception.cc:123
bool hasAttribute(const std::string &name) const
void setName(const std::string &name)
void setAttribute(const std::string &name, const std::string &value)
std::string stringAttribute(const std::string &name) const
int intAttribute(const std::string &name) const
std::string name() const
FunctionUnit * parentUnit() const
Definition BaseFUPort.cc:96
Guard * guard(int index) const
Definition Bus.cc:456
int guardCount() const
Definition Bus.cc:441
virtual Machine * machine() const
void internalSetMachine(Machine &machine)
virtual bool isRegistered() const
virtual TCEString name() const
SpecialRegisterPort * returnAddressPort() const
virtual ObjectState * saveState() const
void setGlobalGuardLatency(int latency)
SpecialRegisterPort * specialRegisterPort(int index) const
int globalGuardLatency_
The global guard latency.
static const std::string OSNAME_CONTROL_UNIT
ObjectState name for ControlUnit.
void setDelaySlots(int delaySlots)
virtual void unsetMachine()
bool hasReturnAddressPort() const
int globalGuardLatency() const
static const std::string OSKEY_DELAY_SLOTS
ObjectState attribute key for the number of delay slots.
virtual ControlUnit * copy() const
void loadStateWithoutReferences(const ObjectState *state)
void setReturnAddressPort(const SpecialRegisterPort &port)
int delaySlots_
Number of delay instruction slots on the transport pipeline.
bool hasSpecialRegisterPort(const std::string &name) const
int specialRegisterPortCount() const
static bool hasLocalGuardLatencyOfZero(const Machine &machine)
virtual void setMachine(Machine &mach)
static const std::string OSKEY_GUARD_LATENCY
ObjectState attribute key for the global guard latency.
SpecialRegisterPort * raPort_
The return address port.
ControlUnit(const std::string &name, int delaySlots, int globalGuardLatency)
virtual void loadState(const ObjectState *state)
static const std::string OSKEY_RA_PORT
ObjectState attribute key for the name of the return address port.
virtual void removePort(Port &port)
virtual void loadState(const ObjectState *state)
virtual ObjectState * saveState() const
virtual bool hasOperationPort(const std::string &name) const
virtual int operationPortCount() const
virtual BaseFUPort * port(const std::string &name) const
ComponentType * item(int index) const
virtual BusNavigator busNavigator() const
Definition Machine.cc:356
virtual void unsetGlobalControl()
Definition Machine.cc:565
virtual void setGlobalControl(ControlUnit &unit)
Definition Machine.cc:317
virtual int guardLatency() const
const RegisterFile * registerFile() const
virtual bool hasPort(const std::string &name) const
Definition Unit.cc:96
virtual int portCount() const
Definition Unit.cc:135
virtual void removePort(Port &port)
Definition Unit.cc:235
virtual boost::format text(int textId)