forked from xcore/tool_axe
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Resource.h
338 lines (275 loc) · 7.07 KB
/
Resource.h
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
// Copyright (c) 2011, 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/>
#ifndef _Resource_h_
#define _Resource_h_
#include <stdint.h>
#include <cassert>
#include "Runnable.h"
#include "Config.h"
enum ResourceType {
RES_TYPE_PORT = 0,
RES_TYPE_TIMER = 1,
RES_TYPE_CHANEND = 2,
RES_TYPE_SYNC = 3,
RES_TYPE_THREAD = 4,
RES_TYPE_LOCK = 5,
RES_TYPE_CLKBLK = 6,
RES_TYPE_PS = 11,
RES_TYPE_CONFIG = 12
};
enum ResConfigType {
RES_CONFIG_PSCTRL = 0xc2,
RES_CONFIG_SSCTRL = 0xc3,
};
const int LAST_STD_RES_TYPE = RES_TYPE_CLKBLK;
class ResourceID {
private:
static const unsigned RES_TYPE_SHIFT = 0;
static const unsigned RES_TYPE_SIZE = 8;
static const unsigned RES_TYPE_MASK = (1 << RES_TYPE_SIZE) - 1;
static const unsigned RES_NUM_SHIFT = 8;
static const unsigned RES_NUM_SIZE = 8;
static const unsigned RES_NUM_MASK = (1 << RES_NUM_SIZE) - 1;
static const unsigned RES_CHANEND_NODE_SHIFT = 16;
static const unsigned RES_CHANEND_NODE_SIZE = 16;
static const unsigned RES_CHANEND_NODE_MASK = (1 << RES_CHANEND_NODE_SIZE) - 1;
static const unsigned RES_PORT_WIDTH_SHIFT = 16;
static const unsigned RES_PORT_WIDTH_SIZE = 6;
static const unsigned RES_PORT_WIDTH_MASK = (1 << RES_PORT_WIDTH_SHIFT) - 1;
public:
uint32_t id;
ResourceID(uint32_t val) : id(val) {}
ResourceType type() const
{
return (ResourceType)((id >> RES_TYPE_SHIFT) & RES_TYPE_MASK);
}
bool isChanend() const { return type() == RES_TYPE_CHANEND; }
bool isConfig() const { return type() == RES_TYPE_CONFIG; }
bool isChanendOrConfig() const {
return type() == RES_TYPE_CHANEND || type() == RES_TYPE_CONFIG;
}
unsigned num() const
{
return (id >> RES_NUM_SHIFT) & RES_NUM_MASK;
}
void setNum(unsigned value)
{
value &= RES_NUM_MASK;
id &= ~(RES_NUM_MASK << RES_NUM_SHIFT);
id |= value << RES_NUM_SHIFT;
}
void setNode(unsigned value)
{
assert(isChanendOrConfig());
value &= RES_CHANEND_NODE_MASK;
id &= ~(RES_CHANEND_NODE_MASK << RES_CHANEND_NODE_SHIFT);
id |= value << RES_CHANEND_NODE_SHIFT;
}
unsigned node() const
{
assert(isChanendOrConfig());
return (id >> RES_CHANEND_NODE_SHIFT) & RES_CHANEND_NODE_MASK;
}
void setWidth(unsigned value)
{
assert((type() == RES_TYPE_PORT) && "width is only valid for ports");
value &= RES_PORT_WIDTH_MASK;
id &= ~(RES_PORT_WIDTH_MASK << RES_PORT_WIDTH_SHIFT);
id |= value << RES_PORT_WIDTH_SHIFT;
}
unsigned width() const
{
assert((type() == RES_TYPE_PORT) && "width is only valid for ports");
return (id >> RES_PORT_WIDTH_SHIFT) & RES_PORT_WIDTH_MASK;
}
operator uint32_t() const { return id; }
static ResourceID chanendID(unsigned num, uint16_t node)
{
return RES_TYPE_CHANEND | (num << RES_NUM_SHIFT) |
(node << RES_CHANEND_NODE_SHIFT);
}
static ResourceID timerID(unsigned num)
{
return RES_TYPE_TIMER | (num << RES_NUM_SHIFT);
}
static ResourceID syncID(unsigned num)
{
return RES_TYPE_SYNC | (num << RES_NUM_SHIFT);
}
static ResourceID threadID(unsigned num)
{
return RES_TYPE_THREAD | (num << RES_NUM_SHIFT);
}
static ResourceID lockID(unsigned num)
{
return RES_TYPE_LOCK | (num << RES_NUM_SHIFT);
}
static ResourceID clockBlockID(unsigned num)
{
return RES_TYPE_CLKBLK | (num << RES_NUM_SHIFT);
}
};
class Thread;
class Port;
/// Resource base class.
class Resource {
private:
/// Has the resource been allocated / turned on?
bool inUse;
ResourceID ID;
public:
static const char *getResourceName(ResourceType type);
bool isInUse() const
{
return inUse;
}
ResourceID getID() const
{
return ID;
}
ResourceType getType() const
{
return ID.type();
}
unsigned getNum() const
{
return ID.num();
}
void setNum(unsigned value)
{
ID.setNum(value);
}
void setNode(unsigned value)
{
ID.setNode(value);
}
void setWidth(unsigned value)
{
ID.setWidth(value);
}
virtual bool isEventable() const
{
return false;
}
virtual bool setCInUse(Thread &thread, bool val, ticks_t time)
{
return false;
}
enum Condition {
COND_FULL,
COND_AFTER,
COND_EQ,
COND_NEQ
};
virtual bool setCondition(Thread &thread, Condition c, ticks_t time)
{
return false;
}
virtual bool setData(Thread &thread, uint32_t value, ticks_t time)
{
return false;
}
virtual bool setReady(Thread &thread, Port *p, ticks_t time)
{
return false;
}
virtual bool alloc(Thread &master)
{
return false;
}
virtual bool free()
{
return false;
}
virtual void cancel()
{
}
enum ResOpResult {
CONTINUE,
DESCHEDULE,
ILLEGAL
};
virtual ResOpResult in(Thread &thread, ticks_t time, uint32_t &value)
{
return ILLEGAL;
}
virtual ResOpResult out(Thread &thread, uint32_t value, ticks_t time)
{
return ILLEGAL;
}
protected:
Resource(ResourceType t) : inUse(0), ID(t) {}
void setInUse(bool val)
{
inUse = val;
}
};
class EventableResource : public Resource, public Runnable {
private:
/// Event / interrupt vector.
uint32_t vector;
/// Environment vector.
uint32_t EV;
/// Are events enabled on the resource?
bool eventsEnabled;
/// If true the resource will generate interrupts, otherwise it will generate
/// events.
bool interruptMode;
/// The thread which owns this resource. This is the last thread to perform
/// an operation on the resource.
Thread *owner;
uint32_t getTruncatedEV(Thread &s) const;
void clearOwner();
void updateOwnerAux(Thread &t);
protected:
EventableResource(ResourceType Type) :
Resource(Type),
owner(0),
next(0),
prev(0) {}
void updateOwner(Thread &t)
{
if (owner == &t)
return;
updateOwnerAux(t);
}
bool eventsPermitted() const;
/// Generate an event. Sets pending event if the owner is currently
/// executing, otherwise the owner is scheduled.s
void event(ticks_t time);
void eventableSetInUse(Thread &t, bool val);
void eventableSetInUseOn(Thread &t);
void eventableSetInUseOff();
virtual bool seeEventEnable(ticks_t time) = 0;
void scheduleUpdate(ticks_t time);
public:
Thread &getOwner()
{
return *owner;
}
virtual void completeEvent();
void setVector(Thread &thread, uint32_t v);
void setEV(Thread &thread, uint32_t ev);
/// Set the interrupt mode on this resource.
void setInterruptMode(Thread &thread, bool Enable);
void eventDisable(Thread &thread);
/// Enable events on this resource.
void eventEnable(Thread &thread);
/// Checks to see if the resource can event. This should only be called if
/// the resource's owner is the current thread. If the resource can event then
/// a pending event is set.
bool checkEvent();
bool seeOwnerEventEnable(ticks_t &time)
{
return seeEventEnable(time);
}
virtual bool isEventable() const
{
return true;
}
EventableResource *next;
EventableResource *prev;
};
#endif // _Resource_h_