forked from xcore/tool_axe
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Core.cpp
388 lines (352 loc) · 9.91 KB
/
Core.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
// Copyright (c) 2011-12, Richard Osborne, All rights reserved
// This software is freely distributable under a derivative of the
// University of Illinois/NCSA Open Source License posted in
// LICENSE.txt and at <http://github.xcore.com/>
#include "Core.h"
#include "SystemState.h"
#include "Node.h"
#include "JIT.h"
#include "Timer.h"
#include "Synchroniser.h"
#include "Lock.h"
#include "Chanend.h"
#include "ClockBlock.h"
#include <iostream>
#include <iomanip>
#include <sstream>
Core::Core(uint32_t RamSize, uint32_t RamBase) :
executionFrequency(new executionFrequency_t[RamSize >> 1]),
opcode(new OPCODE_TYPE[(RamSize >> 1) + ILLEGAL_PC_THREAD_ADDR_OFFSET]),
operands(new Operands[RamSize >> 1]),
ramSizeLog2(31 - countLeadingZeros(RamSize)),
ram_base(RamBase),
ramBaseMultiple(RamBase / RamSize),
thread(new Thread[NUM_THREADS]),
sync(new Synchroniser[NUM_SYNCS]),
lock(new Lock[NUM_LOCKS]),
chanend(new Chanend[NUM_CHANENDS]),
timer(new Timer[NUM_TIMERS]),
clkBlk(new ClockBlock[NUM_CLKBLKS]),
port(new Port*[33]),
portNum(new unsigned[33]),
resource(new Resource**[LAST_STD_RES_TYPE + 1]),
resourceNum(new unsigned[LAST_STD_RES_TYPE + 1]),
memory(new uint32_t[RamSize >> 2]),
coreNumber(0),
parent(0),
invalidationInfo(new unsigned char[RamSize >> 1]),
syscallAddress(~0),
exceptionAddress(~0)
{
memoryOffset = memory - (RamBase / 4);
invalidationInfoOffset = invalidationInfo - (RamBase / 2);
resource[RES_TYPE_PORT] = 0;
resourceNum[RES_TYPE_PORT] = 0;
resource[RES_TYPE_TIMER] = new Resource*[NUM_TIMERS];
for (unsigned i = 0; i < NUM_TIMERS; i++) {
resource[RES_TYPE_TIMER][i] = &timer[i];
}
resourceNum[RES_TYPE_TIMER] = NUM_TIMERS;
resource[RES_TYPE_CHANEND] = new Resource*[NUM_CHANENDS];
for (unsigned i = 0; i < NUM_CHANENDS; i++) {
resource[RES_TYPE_CHANEND][i] = &chanend[i];
}
resourceNum[RES_TYPE_CHANEND] = NUM_CHANENDS;
resource[RES_TYPE_SYNC] = new Resource*[NUM_SYNCS];
for (unsigned i = 0; i < NUM_SYNCS; i++) {
resource[RES_TYPE_SYNC][i] = &sync[i];
}
resourceNum[RES_TYPE_SYNC] = NUM_SYNCS;
resource[RES_TYPE_THREAD] = new Resource*[NUM_THREADS];
for (unsigned i = 0; i < NUM_THREADS; i++) {
thread[i].setParent(*this);
resource[RES_TYPE_THREAD][i] = &thread[i];
}
resourceNum[RES_TYPE_THREAD] = NUM_THREADS;
resource[RES_TYPE_LOCK] = new Resource*[NUM_LOCKS];
for (unsigned i = 0; i < NUM_LOCKS; i++) {
resource[RES_TYPE_LOCK][i] = &lock[i];
}
resourceNum[RES_TYPE_LOCK] = NUM_LOCKS;
resource[RES_TYPE_CLKBLK] = new Resource*[NUM_CLKBLKS];
for (unsigned i = 0; i < RES_TYPE_CLKBLK; i++) {
resource[RES_TYPE_CLKBLK][i] = &clkBlk[i];
}
resourceNum[RES_TYPE_CLKBLK] = NUM_CLKBLKS;
for (int i = RES_TYPE_TIMER; i <= LAST_STD_RES_TYPE; i++) {
for (unsigned j = 0; j < resourceNum[i]; j++) {
resource[i][j]->setNum(j);
}
}
std::memset(port, 0, sizeof(port[0]) * 33);
std::memset(portNum, 0, sizeof(portNum[0]) * 33);
const unsigned portSpec[][2] = {
{1, NUM_1BIT_PORTS},
{4, NUM_4BIT_PORTS},
{8, NUM_8BIT_PORTS},
{16, NUM_16BIT_PORTS},
{32, NUM_32BIT_PORTS},
};
for (unsigned i = 0; i < ARRAY_SIZE(portSpec); i++) {
unsigned width = portSpec[i][0];
unsigned num = portSpec[i][1];
port[width] = new Port[num];
for (unsigned j = 0; j < num; j++) {
port[width][j].setNum(j);
port[width][j].setWidth(width);
port[width][j].setClkInitial(&clkBlk[0]);
}
portNum[width] = num;
}
thread[0].alloc(0);
initInstructionCache(*this);
for (unsigned i = 0; i != (RamSize >> 1); ++i) {
invalidationInfo[i] = INVALIDATE_NONE;
}
std::memset(executionFrequency, 0,
sizeof(executionFrequency[0]) * (RamSize >> 1));
}
Core::~Core() {
delete[] opcode;
delete[] operands;
delete[] invalidationInfo;
delete[] thread;
delete[] sync;
delete[] lock;
delete[] chanend;
delete[] timer;
delete[] resource;
delete[] resourceNum;
delete[] memory;
}
bool Core::allocatable[LAST_STD_RES_TYPE + 1] = {
false, // RES_TYPE_PORT
true, // RES_TYPE_TIMER
true, // RES_TYPE_CHANEND
true, // RES_TYPE_SYNC
true, // RES_TYPE_THREAD
true, // RES_TYPE_LOCK
false, // RES_TYPE_CLKBLK
};
void Core::dumpPaused() const
{
for (unsigned i = 0; i < NUM_THREADS; i++) {
Thread *t = static_cast<Thread*>(resource[RES_TYPE_THREAD][i]);
if (!t->isInUse())
continue;
std::cout << "Thread " << std::dec << i;
Thread &ts = *t;
if (Resource *res = ts.pausedOn) {
std::cout << " paused on ";
std::cout << Resource::getResourceName(res->getType());
std::cout << " 0x" << std::hex << res->getID();
} else if (ts.eeble()) {
std::cout << " waiting for events";
if (ts.ieble())
std::cout << " or interrupts";
} else if (ts.ieble()) {
std::cout << " waiting for interrupts";
} else {
std::cout << " paused on unknown";
}
std::cout << "\n";
}
}
Resource *Core::allocResource(Thread ¤t, ResourceType type)
{
if (type > LAST_STD_RES_TYPE || !allocatable[type])
return 0;
for (unsigned i = 0; i < resourceNum[type]; i++) {
if (!resource[type][i]->isInUse()) {
bool allocated = resource[type][i]->alloc(current);
assert(allocated);
(void)allocated; // Silence compiler.
return resource[type][i];
}
}
return 0;
}
void Core::writeMemory(uint32_t address, void *src, size_t size)
{
std::memcpy(&memOffset()[address], src, size);
}
const Port *Core::getPortByID(ResourceID ID) const
{
assert(ID.type() == RES_TYPE_PORT);
unsigned width = ID.width();
if (width > 32)
return 0;
unsigned num = ID.num();
if (num >= portNum[width])
return 0;
return &port[width][num];
}
const Resource *Core::getResourceByID(ResourceID ID) const
{
ResourceType type = ID.type();
if (type > LAST_STD_RES_TYPE) {
return 0;
}
if (type == RES_TYPE_PORT) {
return getPortByID(ID);
}
unsigned num = ID.num();
if (num >= resourceNum[type]) {
return 0;
}
return resource[type][num];
}
Resource *Core::getResourceByID(ResourceID ID)
{
return const_cast<Resource *>(
static_cast<const Core *>(this)->getResourceByID(ID)
);
}
bool Core::setSyscallAddress(uint32_t value)
{
uint32_t addr = physicalAddress(value) >> 1;
if (!isValidPc(addr))
return false;
syscallAddress = addr;
return true;
}
bool Core::setExceptionAddress(uint32_t value)
{
uint32_t addr = physicalAddress(value) >> 1;
if (!isValidPc(addr))
return false;
exceptionAddress = addr;
return true;
}
void Core::
initCache(OPCODE_TYPE decode, OPCODE_TYPE illegalPC,
OPCODE_TYPE illegalPCThread, OPCODE_TYPE runJit,
OPCODE_TYPE interpretOne)
{
const uint32_t ramSizeShorts = getRamSizeShorts();
// Initialise instruction cache.
for (unsigned i = 0; i < ramSizeShorts; i++) {
opcode[i] = decode;
}
opcode[ramSizeShorts] = illegalPC;
opcode[getRunJitAddr()] = runJit;
opcode[getInterpretOneAddr()] = interpretOne;
opcode[getIllegalPCThreadAddr()] = illegalPCThread;
decodeOpcode = decode;
}
void Core::resetCaches()
{
uint32_t ramEnd = ram_base + (1 << ramSizeLog2);
for (unsigned address = ram_base; address < ramEnd; address += 4) {
invalidateWord(address);
}
}
bool Core::getLocalChanendDest(ResourceID ID, ChanEndpoint *&result)
{
assert(ID.isChanendOrConfig());
if (ID.isConfig()) {
switch (ID.num()) {
case RES_CONFIG_SSCTRL:
return false;
case RES_CONFIG_PSCTRL:
// TODO.
assert(0);
result = 0;
return true;
}
} else {
if (ID.node() == getCoreID()) {
Resource *res = getResourceByID(ID);
if (res) {
assert(res->getType() == RES_TYPE_CHANEND);
result = static_cast<Chanend*>(res);
} else {
result = 0;
}
return true;
}
}
return false;
}
ChanEndpoint *Core::getChanendDest(ResourceID ID)
{
if (!ID.isChanendOrConfig())
return 0;
ChanEndpoint *result;
// Try to lookup locally first.
if (getLocalChanendDest(ID, result))
return result;
return parent->getChanendDest(ID);
}
void Core::finalize()
{
for (unsigned i = 0; i < NUM_THREADS; i++) {
thread[i].finalize();
}
}
void Core::updateIDs()
{
unsigned coreID = getCoreID();
for (unsigned i = 0; i < NUM_CHANENDS; i++) {
resource[RES_TYPE_CHANEND][i]->setNode(coreID);
}
}
uint32_t Core::getCoreID() const
{
return getParent()->getCoreID(coreNumber);
}
std::string Core::getCoreName() const
{
if (!codeReference.empty())
return codeReference;
std::ostringstream buf;
buf << 'c' << getCoreID();
return buf.str();
}
void Core::invalidateWordSlowPath(uint32_t shiftedAddress)
{
if (invalidationInfoOffset[shiftedAddress + 1] == INVALIDATE_NONE) {
invalidateSlowPath(shiftedAddress);
return;
}
invalidationInfoOffset[shiftedAddress + 1] = INVALIDATE_CURRENT_AND_PREVIOUS;
invalidateSlowPath(shiftedAddress + 1);
}
void Core::invalidateSlowPath(uint32_t shiftedAddress)
{
unsigned char info;
do {
info = invalidationInfoOffset[shiftedAddress];
uint32_t pc = shiftedAddress - (ram_base/2);
if (!JIT::invalidate(*this, pc))
opcode[pc] = decodeOpcode;
executionFrequency[pc] = 0;
invalidationInfoOffset[shiftedAddress--] = INVALIDATE_NONE;
} while (info == INVALIDATE_CURRENT_AND_PREVIOUS);
}
void Core::runJIT(uint32_t jitPc)
{
if (!isValidPc(jitPc))
return;
executionFrequency[jitPc] = MIN_EXECUTION_FREQUENCY;
JIT::compileBlock(*this, jitPc);
}
void Core::clearOpcode(uint32_t pc)
{
opcode[pc] = decodeOpcode;
}
void Core::setOpcode(uint32_t pc, OPCODE_TYPE opc, unsigned size)
{
opcode[pc] = opc;
if (invalidationInfo[pc] == INVALIDATE_NONE)
invalidationInfo[pc] = INVALIDATE_CURRENT;
assert((size % 2) == 0);
for (unsigned i = 1; i < size / 2; i++) {
invalidationInfo[pc + i] = INVALIDATE_CURRENT_AND_PREVIOUS;
}
}
void Core::setOpcode(uint32_t pc, OPCODE_TYPE opc, Operands &ops, unsigned size)
{
setOpcode(pc, opc, size);
operands[pc] = ops;
}