-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathmonitor.cpp
More file actions
542 lines (468 loc) · 12.3 KB
/
Copy pathmonitor.cpp
File metadata and controls
542 lines (468 loc) · 12.3 KB
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
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
#include <cstring>
#include <vector>
#include <string>
#include "cpu.h"
#include "monitor.h"
#include "prg.h"
using namespace std;
#define COLS 80
#define ROWS 20
static struct {
char Str[128];
unsigned int Code;
char desc[128];
} monCmds[] = {
{ "<dummy>", 0, "Dummy" },
// { "b [<address>]", MON_CMD_BREAKPOINT, "List all or set breakpoint to <address>.") },
{ "> [<address>] [<arg1>] [<arg2>] .. [<arg#>]", MON_CMD_CHANGEMEM, "Show/change memory from <address>." },
{ "c <address1> <address2> <address3>", MON_CMD_COMPAREMEM, "Compare memory between <address1> and <address2> with <address3>." },
{ "d [<address>]", MON_CMD_DISASS, "Disassemble (from <address>)." },
{ "f <src_from> <src_to> <value>", MON_CMD_FILLMEM, "Fill memory range with <value>." },
{ "g [<address>]", MON_CMD_GO, "Set PC (to [<address>])." },
{ "h <from> <to> <a1> [<a2..an]", MON_CMD_HUNT, "Hunt memory for a1..an." },
{ "l <filename> [<address1>]", MON_CMD_LOADPRG, "Load PRG to memory (from <address1>" },
{ "m [<address>]", MON_CMD_MEM, "Memory dump (from [<address>])." },
{ "s <filename> [<address1>] [<address2>]", MON_CMD_SAVEPRG, "Save memory as PRG (from <address1> to <address2>" },
{ "t <src_from> <src_to> <target>", MON_CMD_TRANSFER, "Memory copy transfer (from start address)." },
{ "w", MON_CMD_SELECTCPU, "Cycle CPU context." },
{ "x", MON_CMD_EXIT, "Exit monitor." },
{ "z", MON_CMD_STEP, "Debug" },
//{ "attach <filename>", MON_CMD_ATTACHIMAGE, "Attach image." },
//{ "detach", MON_CMD_DETACHIMAGE, "Detach current image." },
// { "loadbin <filename>", MON_CMD_LOADBIN, "Load binary file.") },
// { "loadrom [<filename>]", MON_CMD_LOADROM, "Load as ROM or restore default one.") },
// { "loadcharset [<filename>]", MON_CMD_LOADCHARSET, "Load charset or restore default one.") },
{ "reg", MON_CMD_REGS, "Register dump." },
{ "reset", MON_CMD_RESET, "Machine soft reset." },
//{ "restart", MON_CMD_RESTART, "Machine restart." },
//{ "savebin <filename> <from> <to>", MON_CMD_SAVEBIN, "Save binary file from address." },
// { "rmb [<address>]", MON_CMD_REMOVEBREAKPOINT, "Remove breakpoint(s) from <address> or all.") },
{ "sys <command>", MON_CMD_SYS, "Do OS <command>." },
// { "w <address>", MON_CMD_WATCHPOINT, "Watchpoint to <address>.") },
{ "?", MON_CMD_HELP, "Help" }
};
#define NR_OF_MON_CMDS (sizeof(monCmds)/sizeof(monCmds[0]))
static CPU *cpuptr;
static int disAssPc, memDumpPc;
static unsigned int command;
static Debuggable *debugContext = 0;
extern unsigned int g_bActive;
inline static char** new2d(int rows, int cols)
{
char* data = new char [rows * cols];
char** rowpointers = new char *[rows * sizeof(*rowpointers)];
int i;
if (data == NULL || rowpointers == NULL)
return NULL;
rowpointers[0] = data;
for (i = 1; i < rows; ++i) {
rowpointers[i] = rowpointers[i-1] + cols;
}
return rowpointers;
}
inline static void free2d(char** rowpointers)
{
if (rowpointers == NULL)
return;
delete [] rowpointers[0];
delete [] rowpointers;
rowpointers = NULL;
}
char cbmToASCII(unsigned char c)
{
if (c >= 0x00 && c <= 0x1A)
return c ^ 0x40;
if (c >= 0x1b && c < 0x20)
return '_';
if (c >= 0x41 && c <= 0x5A)
return c ^ 0x20;
if ((c >= 0xc1) && (c <= 0xda))
return c ^ 0x80;
if (!isprint(c))
return '.';
return (char) c;
}
int xtoi(const char *str)
{
size_t ln = strlen(str);
unsigned int i = 1;
unsigned int s = 0;
while (ln--) {
char c = tolower(str[ln]);
if (c >= 'a' && c <= 'f') {
s += (c - 'a' + 10) * i;
} else if (c >= '0' && c <= '9') {
s += (c - '0') * i;
}
i *= 16;
}
return s;
}
static void getFirstString(char *from, char *to)
{
char *begin = from;
while (begin && begin[0] == ' ') begin++;
strcpy(to, begin);
char *end = strpbrk(to, " \r\n");
if (end) {
*end = 0;
}
}
static void showHelp()
{
for(unsigned int i=1; i<NR_OF_MON_CMDS; i++) {
printf("%s : %s\n", monCmds[i].Str, monCmds[i].desc);
}
}
static void parseCommand(char *line, unsigned int &cmdCode)
{
unsigned int i;
// Strip spaces
while (*line == ' ')
line++;
char commandString[256];
static char lastCommand[256] = "";
memset(commandString, 0, sizeof(commandString));
getFirstString(line, commandString);
cmdCode = MON_CMD_NONE;
if (0 == strlen(commandString)) {
strcpy(commandString, lastCommand);
} else
strcpy(lastCommand, commandString);
for(i=1; i<NR_OF_MON_CMDS; i++) {
if ( !strncmp(commandString, monCmds[i].Str, strlen(commandString)) ) { // CompareString
cmdCode = monCmds[i].Code;
break;
}
}
}
//static int parseArgs(char *line, char args[4][256])
//{
// char *param;
// int argCount = 0;
//
// // Strip spaces
// while (*line == ' ')
// line++;
// argCount = 0;
// param = strchr(line, ' ');
// while (param && param[1] && argCount<4) {
// line = param + 1;
// sscanf(line, "%s", args[argCount]);
// param = strchr(line, ' ');
// argCount += 1;
// }
// return argCount;
//}
static void memShow(int pc, int dumpwidth, unsigned int rows = ROWS)
{
unsigned short current_pc;
int i;
unsigned int rowcount, cols;
char temp[256];
char line[256];
char **linebuffer;
MemoryHandler &mem = debugContext->getMem();
cols = COLS;
linebuffer = new2d(rows,cols);
if (memDumpPc == 0)
memDumpPc = pc;
current_pc = memDumpPc;
rowcount = 0;
while ( rowcount < rows ) {
sprintf( line, " %04X: ", current_pc );
for ( i = 0; i<dumpwidth/2; i++) {
if ((i&0xF)==8) strcat( line, " ");
sprintf(temp, "%02X ", mem.Read(current_pc+i) & 0xFF);
strcat( line, temp);
}
strcat(line, ":");
for(i = 0; i < dumpwidth/2; i++) {
unsigned char a = mem.Read(current_pc + i);
temp[i] = cbmToASCII(a);
}
temp[i] = 0;
strcat(line, temp);
printf("%s\n", line);
strcpy(line, "\0");
current_pc += dumpwidth/2;
rowcount++;
}
memDumpPc = current_pc;
free2d(linebuffer);
}
static void dumpRegs()
{
char line[256];
debugContext->regDump(line,0);
printf("%s", line);
debugContext->regDump(line,1);
printf("%s", line);
}
static void memDump()
{
int dumpwidth = ((COLS - 16) / 2) & 0xF8;
memShow(memDumpPc,dumpwidth);
}
static void memChange(vector<string> args)
{
unsigned int argCount = (unsigned int) args.size();
if (argCount >= 1) {
memDumpPc = xtoi(args[0].c_str()) & 0xffff;
argCount -= 1;
}
while (argCount) {
unsigned char a = xtoi(args[argCount].c_str()) & 0xff;
argCount--;
debugContext->getMem().Write(memDumpPc + argCount, a);
}
if (!argCount) {
int dumpwidth = ((COLS - 16) / 2) & 0xF8;
memShow(memDumpPc, dumpwidth, 1);
}
}
static void step()
{
char line[256];
unsigned short current_pc = debugContext->getProgramCounter();
disAssPc = debugContext->disassemble(current_pc, line);
printf("%s", line);
debugContext->step();
dumpRegs();
}
static void dumpDisAss(int pc)
{
unsigned short current_pc;
unsigned int rows, cols, rowcount;
char **linebuffer;
//con->getDimensions(cols, rows);
cols = COLS;
rows = ROWS;
if (rows+cols == 0)
return;
linebuffer = new2d(rows,cols);
if (disAssPc == 0)
disAssPc = pc;
current_pc = disAssPc;
rowcount = 2;
while ( rowcount < rows ) {
current_pc = debugContext->disassemble(current_pc, linebuffer[rowcount]);
//con->outDisassString(rowcount,linebuffer[rowcount++]);
printf("%s", linebuffer[rowcount++]);
}
disAssPc = current_pc;
free2d(linebuffer);
}
static void disAss(int dir)
{
if (dir == 0) {
dumpDisAss(debugContext->getProgramCounter());
} else {
dumpDisAss(disAssPc);
}
}
void executeCmd(unsigned int cmd, vector<string> &args, char *wholeLine)
{
vector<unsigned int> argval;
unsigned int argCount = (unsigned int) args.size();
unsigned int r = 0;
while (r < argCount) {
argval.push_back(xtoi(args[r].c_str()));
r++;
}
switch (cmd) {
case 0:
// empty line
break;
case MON_CMD_MEM:
if (argCount >= 1) {
memDumpPc = argval[0];
}
memDump();
break;
case MON_CMD_CHANGEMEM:
memChange(args);
break;
case MON_CMD_FILLMEM:
if (argCount == 3) {
while (argval[0] <= argval[1]) {
debugContext->getMem().Write(argval[0]++, argval[2]);
}
}
break;
case MON_CMD_COMPAREMEM:
if (argCount == 3) {
unsigned int mismatches = 0;
while (argval[0] <= argval[1]) {
unsigned short addr = argval[0]++;
unsigned char readval1 = debugContext->getMem().Read(addr);
unsigned char readval2 = debugContext->getMem().Read(argval[2]++);
if (readval1 != readval2) {
printf("%04X: %02X %02X\n", addr, readval1, readval2);
mismatches++;
}
}
printf("\n%u mismatches found.\n", mismatches);
}
break;
case MON_CMD_TRANSFER:
if (argCount == 3) {
while (argval[0] <= argval[1]) {
int readval = debugContext->getMem().Read(argval[0]++);
debugContext->getMem().Write(argval[2]++, readval);
}
}
break;
case MON_CMD_HUNT:
if (argCount >= 3) {
unsigned int matches = 0;
for (unsigned int i = argval[0]; i < argval[1]; i++) {
unsigned int hargs = argCount - 2;
bool found = true;
for (unsigned int j = 0; j < hargs; j++) {
if (debugContext->getMem().Read(i + j) != argval[j + 2]) {
found = false;
continue;
}
}
if (found) {
matches++;
printf("%04X ", i);
}
}
if (matches)
printf("\n%u matches found.\n", matches);
else
printf("No matches found.\n");
}
break;
case MON_CMD_DISASS:
if (argCount >= 1) {
disAssPc = argval[0];
}
disAss(+1);
break;
case MON_CMD_STEP:
step();
break;
case MON_CMD_REGS:
dumpRegs();
break;
case MON_CMD_GO:
if (argCount >= 1) {
cpuptr->setPC(argval[0]);
}
break;
//case MON_CMD_ATTACHIMAGE:
// if (argCount >= 1) {
// theMachine->attachTape(args[0]);
// }
// break;
//case MON_CMD_DETACHIMAGE:
// theMachine->detachTape();
// break;
case MON_CMD_RESET:
cpuptr->Reset();
printf("Machine soft reset.\n");
break;
//case MON_CMD_RESTART:
// theMachine->coldRestart();
// printf("Machine restart.\n");
// break;
case MON_CMD_LOADPRG:
if (argCount >= 1) {
string prgName = args[0] + ".prg";
unsigned short adr1 = argCount >= 2 ? argval[1] : 0;
if (!mainLoadPrgToMemory(prgName.c_str(), adr1))
fprintf(stderr, "Failed to load %s.\n", prgName.c_str());
}
break;
case MON_CMD_LOADBIN:
if (argCount >= 2) {
//cpuptr->loadBinFromFile(args[0], argval[1]);
}
break;
case MON_CMD_SAVEBIN:
if (argCount >= 3) {
//cpuptr->saveToFile(args[0], argval[1], argval[2]);
}
break;
case MON_CMD_SAVEPRG:
if (argCount >= 1) {
string prgName = args[0] + ".prg";
unsigned short adr1 = argCount >= 2 ? argval[1] : 0;
unsigned short adr2 = argCount >= 3 ? argval[2] : 0;
mainSaveMemoryAsPrg(prgName.c_str(), adr1, adr2);
// fprintf(stderr, "Memory saved to %s from %04X to %04X\n", prgName.c_str(), adr1, adr2);
}
break;
case MON_CMD_SYS:
if (argCount >= 1) {
wholeLine = strchr(wholeLine, ' ');
if (wholeLine) {
wholeLine++;
system(wholeLine);
}
}
break;
case MON_CMD_SELECTCPU:
{
Debuggable *newCx = debugContext->cycleToNext(debugContext);
if (debugContext == newCx) {
printf("No more debug contexts funds other than main CPU.\n");
} else {
printf("Switching from debug context '%s' to '%s'\n", debugContext->getName(), newCx->getName());
debugContext = newCx;
}
}
break;
case MON_CMD_HELP:
showHelp();
break;
case MON_CMD_EXIT:
printf("Exiting the monitor...\n");
break;
default:
printf("Unknown command code: %i.\n", cmd);
break;
}
}
static void parseLine(char *line, unsigned int &cmdCode)
{
vector<string> args;
const char *dl = " ,-\n\r\t";
parseCommand(line, cmdCode);
char lineCopy[512];
strcpy(lineCopy, line);
// first token is command string
char *carg = strtok(lineCopy, dl);
carg = strtok(NULL, dl);
//
while (carg) {
args.push_back(carg);
carg = strtok(NULL, dl);
}
executeCmd(cmdCode, args, line);
}
void monitorEnter(CPU *cpu)
{
char buffer[256];
unsigned int wasActive = g_bActive;
g_bActive = 0;
cpuptr = cpu;
if (!debugContext)
debugContext = debugContext->cycleToNext(0);
disAssPc = memDumpPc = debugContext->getProgramCounter();
printf("Welcome to the monitor!\n");
printf("Type ? for help!\n");
do {
command = MON_CMD_NONE;
strcpy(buffer, "");
if (fgets(buffer, 256, stdin))
parseLine(buffer, command);
} while (command != MON_CMD_EXIT);
g_bActive = wasActive;
}