-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathDebugger.cpp
287 lines (271 loc) · 8.13 KB
/
Debugger.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
#include "ComponentRegistry.h"
#include "Processor.h"
#include <iostream>
#include <sstream>
void usage(char** argv)
{
std::cerr << "Usage: " << argv[0] << " <memory file>" << std::endl;
}
int main(int argc,char** argv)
{
if (argc < 2)
{
usage(argv);
return 0;
}
Memory & memory = ComponentRegistry::instance().memory();
memory.initializeFromFile(argv[1], 0xFFFF);
// memory.dump();
if (!memory.good())
{
std::cerr << "Failed to initialised memory. Exiting. " << std::endl;
return 0;
}
//memory.dump();
Processor & p = ComponentRegistry::instance().processor();
std::string c;
unsigned int N = 0;
bool quiet = false;
bool started = false;
int dumpStart = -1;
int dumpEnd = -1;
while (true)
{
if (!quiet) std::cout << ">: ";
std::getline(std::cin, c);
if (c == "start")
{
p.step();
if (!quiet) p.show();
started = true;
}
else if (c == "exit")
{
std::cout << "Exiting" << std::endl;
break;
}
else if (c == "e")
{
if (started)
{
p.execute();
p.step();
if (!quiet) p.show();
N++;
}
else
{
std::cout << "Use \"start\" before \"e\"" << std::endl;
}
}
else if (c == "q")
{
std::cout << "Quiet mode. \"v\" for verbose." << std::endl;
quiet = true;
}
else if (c == "v")
{
std::cout << "Verbose mode. \"q\" for quiet." << std::endl;
quiet = false;
}
else if (c == "r")
{
std::cout << "Registers : " ;
ComponentRegistry::instance().registerBank().dumpRegisters();
}
else if ((c[0] == 'p') && (c.size() > 2))
{
std::istringstream is(c.substr(2));
unsigned short int i;
is >> std::hex >> i;
if (!is.fail())
{
std::cout << "Page Registers : " << std::endl ;
ComponentRegistry::instance().pageRegisters().dump(i);
}
}
else if (c == "s")
{
std::cout << "StatusWord: " ;
ComponentRegistry::instance().statusWord().dump();
}
else if (c == "l")
{
p.show();
}
else if (c == "ic")
{
std::cout << "Inst. Cnt : " << std::hex << std::setfill('0') << std::setw(4) << std::right << ComponentRegistry::instance().instructionCounter().value() << std::endl;
}
else if (c == "f")
{
std::cout << "Faults : " << std::hex << std::setfill('0') << std::setw(4) << ComponentRegistry::instance().instructionCounter().value() << std::endl;
}
else if (c == "I")
{
std::cout << (p.interruptsEnabled() ? "Interrupts Enabled." : "Interrupts Disabled") << std::endl;
std::cout << "Interrupts : " << std::hex << std::setfill('0') << std::right << std::setw(4) << ComponentRegistry::instance().interrupts().value() << std::endl;
std::cout << "Pending : " << std::hex << std::setfill('0') << std::right << std::setw(4) << ComponentRegistry::instance().pendingInterrupts().value() << std::endl;
std::cout << "Mask : " << std::hex << std::setfill('0') << std::right << std::setw(4) << ComponentRegistry::instance().interruptMask().value() << std::endl;
}
else if ((c[0] == 'c') && (c[1] == ' '))
{
std::cout << c.substr(2) << std::endl;
}
else if (c[0] == 'b')
{
std::istringstream is(c.substr(1));
unsigned int address;
is >> std::hex >> address;
while (ComponentRegistry::instance().instructionCounter().value() != address)
{
p.step();
if (ComponentRegistry::instance().instructionCounter().value() != address)
{
p.execute();
N++;
}
}
p.step();
p.show();
}
else if (c[0] == 'n')
{
std::istringstream is(c.substr(1));
unsigned int nextN;
is >> std::dec >> nextN;
while (nextN > 0)
{
p.step();
p.execute();
N++;
nextN--;
}
p.step();
if (!quiet) p.show();
}
else if (c == "N")
{
std::cout << "Executed " << std::dec << N << " instructions." << std::endl;
}
else if (c[0] == 'm')
{
std::istringstream is(c.substr(1));
unsigned int address;
is >> std::hex >> address;
std::cout << std::hex << std::setfill('0') << std::setw(8) << std::right << std::uppercase << address << " : " << std::setw(4) << ComponentRegistry::instance().memory().read(address) << std::endl;
}
else if (c == "x")
{
break;
}
else if ((c.substr(0,4) == "dump") && (c.size() > 5))
{
std::istringstream is(c.substr(5));
std::string s;
is >> s;
if (dumpStart < dumpEnd)
{
memory.dump(s, dumpStart, dumpEnd);
}
else
{
memory.dump(s);
}
}
else if ((c.substr(0,7) == "cfgdump") && (c.size() > 8))
{
std::istringstream is(c.substr(8));
unsigned int a, b;
is >> std::hex >> a >> b;
if (!is.fail())
{
dumpStart = a;
dumpEnd = b;
}
}
else if ((c.substr(0,3) == "set") && (c.size() > 4))
{
std::istringstream is(c.substr(4));
std::string s;
is >> s;
if (s == "pi")
{
unsigned short int i;
is >> std::hex >> i;
if (!is.fail())
{
ComponentRegistry::instance().pendingInterrupts().write(i);
}
}
else if (s == "mk")
{
unsigned short int i;
is >> std::hex >> i;
if (!is.fail())
{
ComponentRegistry::instance().interruptMask().write(i);
}
}
else if (s == "ic")
{
unsigned short int i;
is >> std::hex >> i;
if (!is.fail())
{
ComponentRegistry::instance().instructionCounter().write(i);
p.step();
p.show();
}
}
else if (s == "reg")
{
unsigned short int r;
unsigned short int v;
is >> std::dec >> r >> std::hex >> v;
if (!is.fail())
{
ComponentRegistry::instance().registerBank().start()[r]->write(v);
}
}
}
else if ((c.substr(0,3) == "int") && (c.size() > 4))
{
std::istringstream is(c.substr(4));
unsigned short int i;
is >> i;
if ((i >= 0) && (i <= 15))
{
ComponentRegistry::instance().interruptGenerator().addInterrupt(i);
p.step();
if (!quiet) p.show();
}
}
else if (c == "h")
{
std::cout << "Help : " << std::endl;
std::cout << "start : Start execution." << std::endl;
std::cout << "exit : Exit." << std::endl;
std::cout << "l : Show current instruction line" << std::endl;
std::cout << "e : Execute current instruction" << std::endl;
std::cout << "n [N] : Execute next N instructions" << std::endl;
std::cout << "ic : Show value of instruction counter" << std::endl;
std::cout << "r : Show contents of general registers" << std::endl;
std::cout << "s : Show status words" << std::endl;
std::cout << "p [g] : Show page register group g" << std::endl;
std::cout << "I : Show content of interrupt registers" << std::endl;
std::cout << "m [add] : Show content of memory" << std::endl;
std::cout << "dump \"file\" : Dump memory to file" << std::endl;
std::cout << "cfgdump [start] [end] : Configure addresses for memory dump" << std::endl;
std::cout << "b [add] : Break at address" << std::endl;
std::cout << "N : Display number of executed instructions" << std::endl;
std::cout << "set xx yyy : Set" << std::endl;
std::cout << "int n : Add interrupt n" << std::endl;
std::cout << "q : Quiet mode." << std::endl;
std::cout << "v : Verbose mode." << std::endl;
std::cout << "c [comment] : Display comment." << std::endl;
std::cout << "h : Show this help" << std::endl;
}
}
return 1;
}