forked from xcore/tool_axe
-
Notifications
You must be signed in to change notification settings - Fork 1
/
InstructionHelpers.cpp
154 lines (139 loc) · 3.63 KB
/
InstructionHelpers.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
// 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 "InstructionHelpers.h"
#include "Thread.h"
#include "Core.h"
#include "Exceptions.h"
#include "Trace.h"
#include "Synchroniser.h"
#include "Chanend.h"
#include "ClockBlock.h"
#include <cstdlib>
using namespace Register;
uint32_t exception(Thread &t, uint32_t pc, int et, uint32_t ed)
{
uint32_t sed = t.regs[ED];
uint32_t spc = t.getParent().targetPc(pc);
uint32_t ssr = t.sr.to_ulong();
if (Tracer::get().getTracingEnabled()) {
Tracer::get().exception(t, et, ed, sed, ssr, spc);
}
t.regs[SSR] = sed;
t.regs[SPC] = spc;
t.regs[SED] = sed;
t.ink() = true;
t.eeble() = false;
t.ieble() = false;
t.regs[ET] = et;
t.regs[ED] = ed;
uint32_t newPc = t.regs[KEP];
if (et == ET_KCALL)
newPc += 64;
if ((newPc & 1) || !t.getParent().isValidAddress(newPc)) {
std::cout << "Error: unable to handle exception (invalid kep)\n";
std::abort();
}
return t.getParent().toPc(newPc);
}
Resource *checkResource(Core &state, ResourceID id)
{
Resource *res = state.getResourceByID(id);
if (!res || !res->isInUse())
return 0;
return res;
}
Synchroniser *checkSync(Core &state, ResourceID id)
{
Resource *res = checkResource(state, id);
if (!res)
return 0;
if (res->getType() != RES_TYPE_SYNC)
return 0;
return static_cast<Synchroniser *>(res);
}
Thread *checkThread(Core &state, ResourceID id)
{
Resource *res = checkResource(state, id);
if (!res)
return 0;
if (res->getType() != RES_TYPE_THREAD)
return 0;
return static_cast<Thread *>(res);
}
Chanend *checkChanend(Core &state, ResourceID id)
{
Resource *res = checkResource(state, id);
if (!res)
return 0;
if (res->getType() != RES_TYPE_CHANEND)
return 0;
return static_cast<Chanend *>(res);
}
Port *checkPort(Core &state, ResourceID id)
{
Resource *res = checkResource(state, id);
if (!res)
return 0;
if (res->getType() != RES_TYPE_PORT)
return 0;
return static_cast<Port *>(res);
}
EventableResource *checkEventableResource(Core &state, ResourceID id)
{
Resource *res = checkResource(state, id);
if (!res)
return 0;
if (!res->isEventable())
return 0;
return static_cast<EventableResource *>(res);
}
const uint32_t CLK_REF = 0x1;
bool setClock(Thread &t, ResourceID resID, uint32_t val, ticks_t time)
{
Core &state = t.getParent();
Resource *res = checkResource(state, resID);
if (!res) {
return false;
}
switch (res->getType()) {
default: return false;
case RES_TYPE_CLKBLK:
{
ClockBlock *c = static_cast<ClockBlock*>(res);
if (val == CLK_REF) {
c->setSourceRefClock(t, time);
return true;
}
Port *p = checkPort(state, val);
if (!p || p->getPortWidth() != 1)
return false;
return c->setSource(t, p, time);
}
case RES_TYPE_PORT:
{
Resource *source = state.getResourceByID(val);
if (!source)
return false;
if (source->getType() != RES_TYPE_CLKBLK)
return false;
static_cast<Port*>(res)->setClk(t, static_cast<ClockBlock*>(source),
time);
return true;
}
break;
}
return true;
}
bool setReadyInstruction(Thread &t, ResourceID resID, uint32_t val,
ticks_t time)
{
Core &state = t.getParent();
Resource *res = checkResource(state, resID);
Port *ready = checkPort(state, val);
if (!res || !ready) {
return false;
}
return res->setReady(t, ready, time);
}