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 | /*
* implemenatation for:
* - the C interface specified in udb.h
* - the C++ interface specified in udb_cpp.hpp
*/
#include "udb_cpp.hpp"
#include "../ulm2/udb.h"
// implementation for the C++ interface
std::unordered_map<std::size_t, enum UdbMod> udb_reg[2];
std::unordered_map<std::uint64_t, enum UdbMod> udb_mem[2];
std::string udb_out;
std::string udb_in;
bool udb_waitingForInput;
bool udb_eof;
std::unordered_map<std::uint64_t, std::uint64_t> udb_breakpoint;
std::unordered_map<std::unique_ptr<std::function<void()>>, void *> udb_listener;
std::uint64_t numCycles;
std::uint64_t prevIP;
static bool
waitingForInput()
{
if (udb_eof) {
return false;
}
if (udb_in.length() == 0 ||
(udb_in.front() == '\\' && udb_in.length() == 1)) {
ulm_waiting = udb_waitingForInput = true;
return true;
}
ulm_waiting = udb_waitingForInput = false;
return false;
}
std::uint64_t
udb_getNumCycles()
{
return numCycles;
}
std::uint64_t
udb_getIP()
{
return ulm_instrPtr;
}
std::uint64_t
udb_getPrevIP()
{
return prevIP;
}
void
udb_step()
{
if (!ulm_halted) {
prevIP = ulm_instrPtr;
++numCycles;
ulm_step();
}
}
void
udb_run(std::size_t maxCycles)
{
std::size_t cycle = 0;
udb_inStep = false;
while (!ulm_halted && !udb_illegalInstruction && !udb_badAlignment) {
prevIP = ulm_instrPtr;
ulm_step();
++numCycles;
if (maxCycles) {
++cycle;
if (cycle > maxCycles) {
break;
}
}
if (udb_waitingForInput) {
break;
}
if (udb_breakpoint.find(ulm_instrPtr) != udb_breakpoint.end()) {
break;
}
}
udb_inStep = true;
}
// implementation for the C interface
extern "C" void
udb_addRegMsg(std::size_t regId, enum UdbMod udbMod)
{
auto found = udb_reg[udb_activeMsg].find(regId);
if (found != udb_reg[udb_activeMsg].end()) {
found->second = static_cast<enum UdbMod>(found->second | udbMod);
} else {
udb_reg[udb_activeMsg][regId] = udbMod;
}
}
extern "C" void
udb_addMemMsg(std::uint64_t addr, enum UdbMod udbMod)
{
auto found = udb_mem[udb_activeMsg].find(addr);
if (found != udb_mem[udb_activeMsg].end()) {
found->second = static_cast<enum UdbMod>(found->second | udbMod);
} else {
udb_mem[udb_activeMsg][addr] = udbMod;
}
}
extern "C" void
udb_notify()
{
for (auto &m : udb_listener) {
m.first->operator()();
}
}
extern "C" void
udb_clearMsg()
{
udb_reg[udb_lastMsg].clear();
udb_mem[udb_lastMsg].clear();
std::swap(udb_activeMsg, udb_lastMsg);
}
extern "C" void
ulm_halt(uint64_t code)
{
ulm_halted = true;
ulm_exitCode = code;
}
extern "C" {
void
ulm_printChar(int ch)
{
udb_out += ch;
}
int
ulm_readChar()
{
if (udb_eof) {
return -1;
}
if (waitingForInput()) {
return 0;
}
int ch = udb_in.front();
udb_in = udb_in.substr(1);
if (ch == '^') {
int ch_ = udb_in.front();
if (ch_ == 'D') {
udb_in = "";
udb_eof = true;
return -1;
}
} else if (ch == '\\') {
ch = udb_in.front();
udb_in = udb_in.substr(1);
switch (ch) {
case 'n':
ch = '\n';
break;
case 't':
ch = '\t';
break;
default:;
}
}
return ch;
}
} // extern "C"
|