OpenASIP 2.2
Loading...
Searching...
No Matches
CompiledSimulation.icc
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 CompiledSimulation.icc
26 *
27 * Inline definitions of CompiledSimulation class.
28 *
29 * @author Viljami Korhonen 2007 (viljami.korhonen-no.spam-tut.fi)
30 * @author Pekka Jääskeläinen 2009 (pekka.jaaskelainen-no.spam-tut.fi)
31 * @note rating: red
32 */
33
34#include "Application.hh"
35
36/**
37 * Adds a SimValue to the FU's results list.
38 *
39 * Used to simulate the FU latency.
40 *
41 * @param results The FU results list
42 * @param cycleCount # of cycles at the time of the addition
43 * @param value value to be added
44 * @param latency Latency of the FU
45 */
46void inline
47CompiledSimulation::addFUResult(
48 FUResultType& results,
49 ClockCycleCount cycleCount,
50 const SimValue& value,
51 int latency) {
52
53 int oldResultIndex = -1;
54
55 // Find the next empty slot and put the result there
56 for (int i = 0; i < results.size; ++i) {
57 FUResultElementType& result = results.data[i];
58
59 // clear overwritten results to make room for the ringbuffer.
60 if (result.used) {
61 // result ready
62 if (result.cycles <= cycleCount) {
63 // first old result
64 if (oldResultIndex == -1) {
65 oldResultIndex = i;
66 } else {
67 FUResultElementType& oldResult =
68 results.data[oldResultIndex];
69 if (oldResult.cycles < result.cycles) {
70 // use the space of the old result for the new one.
71 oldResult.cycles = cycleCount + latency;
72 oldResult.value = value;
73 oldResult.used = true;
74 return;
75 } else {
76 result.used = false;
77 results.numberOfElements--;
78 }
79 }
80 }
81 }
82 if (!result.used) {
83 result.cycles = cycleCount + latency;
84 result.value = value;
85 result.used = true;
86 results.numberOfElements++;
87 return;
88 }
89 }
90 assert(0 && "Ringbuffer full!");
91}
92
93/**
94 * Adds a UIntWord value to the FU's results.
95 *
96 * Used to simulate the FU latency.
97 *
98 * @param results The FU results list
99 * @param cycleCount # of cycles at the time of the addition
100 * @param value value to be added
101 * @param latency Latency of the FU
102 */
103void inline
104CompiledSimulation::addFUResult(
105 FUResultType& results,
106 ClockCycleCount cycleCount,
107 const UIntWord& value,
108 int latency) {
109
110 int oldResultIndex = -1;
111
112 // Find the next empty slot and put the result there
113 for (int i = 0; i < results.size; ++i) {
114 FUResultElementType& result = results.data[i];
115
116 // clear overwritten results to make room for the ringbuffer.
117 if (result.used) {
118 // result ready
119 if (result.cycles <= cycleCount) {
120 // first old result
121 if (oldResultIndex == -1) {
122 oldResultIndex = i;
123 } else {
124 FUResultElementType& oldResult =
125 results.data[oldResultIndex];
126 if (oldResult.cycles < result.cycles) {
127 // use the space of the old result for the new one.
128 oldResult.cycles = cycleCount + latency;
129 oldResult.value = value;
130 oldResult.used = true;
131 return;
132 } else {
133 result.used = false;
134 results.numberOfElements--;
135 }
136 }
137 }
138 }
139
140 if (!result.used) {
141 result.cycles = cycleCount + latency;
142 result.value = value;
143 result.used = true;
144 results.numberOfElements++;
145 return;
146 }
147 }
148 assert(0 && "Ringbuffer full!");
149}
150
151
152/**
153 * Grabs the current value from the FU results list.
154 *
155 * The current value is computed using the current cycle count,
156 * discarding "too old" (overwritten) results.
157 *
158 * @param target Target of the assignment.
159 * @param results The FU results to be checked for.
160 * @param cycles The current cyclecount.
161 */
162void inline
163CompiledSimulation::FUResult(
164 SimValue& target, FUResultType& results, ClockCycleCount cycles) {
165
166 int maxDiff = -2147483647;
167
168 for (int i = 0; results.numberOfElements != 0 && i < results.size; ++i) {
169 FUResultElementType& result = results.data[i];
170
171 if (!result.used) {
172 continue;
173 }
174
175 int diff = static_cast<int>(result.cycles - cycles);
176 if (diff <= 0) {
177 if (diff >= maxDiff) {
178 maxDiff = diff;
179 target = result.value;
180 }
181 result.used = false;
182 results.numberOfElements--;
183 } else {
184 //i++;
185 }
186 }
187}
188
189void inline
190CompiledSimulation::clearFUResults(FUResultType& results) {
191
192 if (results.numberOfElements == 0) {
193 return;
194 }
195
196 for (int i = 0; i < results.size; ++i) {
197 results.data[i].used = false;
198 }
199 results.numberOfElements = 0;
200}