remote.cpp 119 KB
Newer Older
kode54's avatar
kode54 committed
1
#ifndef __LIBRETRO__
2
#include <stdio.h>
3
#include <stdlib.h>
4
5
#include <string.h>

6
7
8
9
#include <iosfwd>
#include <sstream>
#include <vector>

DJRobX's avatar
DJRobX committed
10
#ifndef _WIN32
11
12
13
14
15
16
17
18
19
20
21
22
#include <netdb.h>
#include <sys/socket.h>
#include <unistd.h>
#ifdef HAVE_NETINET_IN_H
#include <netinet/in.h>
#endif // HAVE_NETINET_IN_H
#ifdef HAVE_ARPA_INET_H
#include <arpa/inet.h>
#else // ! HAVE_ARPA_INET_H
#define socklen_t int
#endif // ! HAVE_ARPA_INET_H
#define SOCKET int
DJRobX's avatar
DJRobX committed
23
#else // _WIN32
24
25
26
27
28
29
#include <io.h>
#include <winsock.h>
#define socklen_t int
#define close closesocket
#define read _read
#define write _write
skidau's avatar
skidau committed
30
#define strdup _strdup
DJRobX's avatar
DJRobX committed
31
#endif // _WIN32
32

33
#include "BreakpointStructures.h"
34
#include "GBA.h"
35
#include "elf.h"
36
37
38
#include "remote.h"
#include <iomanip>
#include <iostream>
39
40

extern bool debugger;
41
extern int emulating;
42
43
extern void CPUUpdateCPSR();

44
int remotePort = 0;
45
int remoteSignal = 5;
DJRobX's avatar
DJRobX committed
46
47
SOCKET remoteSocket = -1;
SOCKET remoteListenSocket = -1;
48
49
50
bool remoteConnected = false;
bool remoteResumed = false;

51
52
int (*remoteSendFnc)(char*, int) = NULL;
int (*remoteRecvFnc)(char*, int) = NULL;
53
54
55
56
57
58
bool (*remoteInitFnc)() = NULL;
void (*remoteCleanUpFnc)() = NULL;

#ifndef SDL
void remoteSetSockets(SOCKET l, SOCKET r)
{
59
60
    remoteSocket = r;
    remoteListenSocket = l;
61
62
63
}
#endif

64
#define debuggerReadMemory(addr) \
65
    (*(uint32_t*)&map[(addr) >> 24].address[(addr)&map[(addr) >> 24].mask])
66
67

#define debuggerReadHalfWord(addr) \
68
    (*(uint16_t*)&map[(addr) >> 24].address[(addr)&map[(addr) >> 24].mask])
69
70

#define debuggerReadByte(addr) \
71
    map[(addr) >> 24].address[(addr)&map[(addr) >> 24].mask]
72
73

#define debuggerWriteMemory(addr, value) \
74
    *(uint32_t*)&map[(addr) >> 24].address[(addr)&map[(addr) >> 24].mask] = (value)
75
76

#define debuggerWriteHalfWord(addr, value) \
77
    *(uint16_t*)&map[(addr) >> 24].address[(addr)&map[(addr) >> 24].mask] = (value)
78
79

#define debuggerWriteByte(addr, value) \
80
    map[(addr) >> 24].address[(addr)&map[(addr) >> 24].mask] = (value)
81
82
83
84
85
86

bool dontBreakNow = false;
int debuggerNumOfDontBreak = 0;
int debuggerRadix = 0;

#define NUMBEROFDB 1000
87
uint32_t debuggerNoBreakpointList[NUMBEROFDB];
88

89
const char* cmdAliasTable[] = { "help", "?", "h", "?", "continue", "c", "next", "n",
90
91
92
    "cpyb", "copyb", "cpyh", "copyh", "cpyw", "copyw",
    "exe", "execute", "exec", "execute",
    NULL, NULL };
93
94

struct DebuggerCommand {
95
96
97
98
    const char* name;
    void (*function)(int, char**);
    const char* help;
    const char* syntax;
99
100
101
102
};

char monbuf[1000];
void monprintf(std::string line);
103
104
105
106
std::string StringToHex(std::string& cmd);
std::string HexToString(char* p);
void debuggerUsage(const char* cmd);
void debuggerHelp(int n, char** args);
107
void printFlagHelp();
108
void dbgExecute(std::string& cmd);
109

110
111
112
extern bool debuggerBreakOnWrite(uint32_t, uint32_t, int);
extern bool debuggerBreakOnRegisterCondition(uint8_t, uint32_t, uint32_t, uint8_t);
extern bool debuggerBreakOnExecution(uint32_t, uint8_t);
113
114

regBreak* breakRegList[16];
115
116
117
118
119
uint8_t lowRegBreakCounter[4]; //(r0-r3)
uint8_t medRegBreakCounter[4]; //(r4-r7)
uint8_t highRegBreakCounter[4]; //(r8-r11)
uint8_t statusRegBreakCounter[4]; //(r12-r15)
uint8_t* regBreakCounter[4] = {
120
121
122
123
    &lowRegBreakCounter[0],
    &medRegBreakCounter[0],
    &highRegBreakCounter[0],
    &statusRegBreakCounter[0]
124
};
125
uint32_t lastWasBranch = 0;
126

127
struct regBreak* getFromBreakRegList(uint8_t regnum, int location)
128
129
130
{
    if (location > regBreakCounter[regnum >> 2][regnum & 3])
        return NULL;
131

132
133
134
135
136
    struct regBreak* ans = breakRegList[regnum];
    for (int i = 0; i < location && ans; i++) {
        ans = ans->next;
    }
    return ans;
137
138
139
140
}

bool enableRegBreak = false;
reg_pair oldReg[16];
141
uint32_t regDiff[16];
142

143
144
145
146
void breakReg_check(int i)
{
    struct regBreak* brkR = breakRegList[i];
    bool notFound = true;
147
    uint8_t counter = regBreakCounter[i >> 2][i & 3];
148
149
    for (int bri = 0; (bri < counter) && notFound; bri++) {
        if (!brkR) {
150
            regBreakCounter[i >> 2][i & 3] = (uint8_t)bri;
151
152
153
            break;
        } else {
            if (brkR->flags != 0) {
154
                uint32_t regVal = (i == 15 ? (armState ? reg[15].I - 4 : reg[15].I - 2) : reg[i].I);
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
                if ((brkR->flags & 0x1) && (regVal == brkR->intVal)) {
                    debuggerBreakOnRegisterCondition(i, brkR->intVal, regVal, 1);
                    notFound = false;
                }
                if ((brkR->flags & 0x8)) {
                    if ((brkR->flags & 0x4) && ((int)regVal < (int)brkR->intVal)) {
                        debuggerBreakOnRegisterCondition(i, brkR->intVal, regVal, 4);
                        notFound = false;
                    }
                    if ((brkR->flags & 0x2) && ((int)regVal > (int)brkR->intVal)) {
                        debuggerBreakOnRegisterCondition(i, brkR->intVal, regVal, 5);
                        notFound = false;
                    }
                }
                if ((brkR->flags & 0x4) && (regVal < brkR->intVal)) {
                    debuggerBreakOnRegisterCondition(i, brkR->intVal, regVal, 2);
                    notFound = false;
                }
                if ((brkR->flags & 0x2) && (regVal > brkR->intVal)) {
                    debuggerBreakOnRegisterCondition(i, brkR->intVal, regVal, 3);
                    notFound = false;
                }
            }
            brkR = brkR->next;
        }
    }
    if (!notFound) {
        //CPU_BREAK_LOOP_2;
    }
}

void clearParticularRegListBreaks(int regNum)
{

    while (breakRegList[regNum]) {
        struct regBreak* ans = breakRegList[regNum]->next;
        free(breakRegList[regNum]);
        breakRegList[regNum] = ans;
    }
    regBreakCounter[regNum >> 2][regNum & 3] = 0;
}

void clearBreakRegList()
{
    for (int i = 0; i < 16; i++) {
        clearParticularRegListBreaks(i);
    }
}

204
void deleteFromBreakRegList(uint8_t regNum, int num)
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
{
    int counter = regBreakCounter[regNum >> 2][regNum & 3];
    if (num >= counter) {
        return;
    }
    struct regBreak* ans = breakRegList[regNum];
    struct regBreak* prev = NULL;
    for (int i = 0; i < num; i++) {
        prev = ans;
        ans = ans->next;
    }
    if (prev) {
        prev->next = ans->next;
    } else {
        breakRegList[regNum] = ans->next;
    }
    free(ans);
    regBreakCounter[regNum >> 2][regNum & 3]--;
}

225
void addBreakRegToList(uint8_t regnum, uint8_t flags, uint32_t value)
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
{
    struct regBreak* ans = (struct regBreak*)malloc(sizeof(struct regBreak));
    ans->flags = flags;
    ans->intVal = value;
    ans->next = breakRegList[regnum];
    breakRegList[regnum] = ans;
    regBreakCounter[regnum >> 2][regnum & 3]++;
}

void printBreakRegList(bool verbose)
{
    const char* flagsToOP[] = { "never", "==", ">", ">=", "<", "<=", "!=", "always" };
    bool anyPrint = false;
    for (int i = 0; i < 4; i++) {
        for (int k = 0; k < 4; k++) {
            if (regBreakCounter[i][k]) {
                if (!anyPrint) {
                    {
                        sprintf(monbuf, "Register breakpoint list:\n");
                        monprintf(monbuf);
                    }
                    {
                        sprintf(monbuf, "-------------------------\n");
                        monprintf(monbuf);
                    }
                    anyPrint = true;
                }
                struct regBreak* tmp = breakRegList[i * 4 + k];
                for (int j = 0; j < regBreakCounter[i][k]; j++) {
                    if (tmp->flags & 8) {
                        sprintf(monbuf, "No. %d:\tBreak if (signed)%s %08x\n", j, flagsToOP[tmp->flags & 7], tmp->intVal);
                        monprintf(monbuf);
                    } else {
                        sprintf(monbuf, "No. %d:\tBreak if %s %08x\n", j, flagsToOP[tmp->flags], tmp->intVal);
                        monprintf(monbuf);
                    }
                    tmp = tmp->next;
                }
                {
                    sprintf(monbuf, "-------------------------\n");
                    monprintf(monbuf);
                }
            } else {
                if (verbose) {
                    if (!anyPrint) {
                        {
                            sprintf(monbuf, "Register breakpoint list:\n");
                            monprintf(monbuf);
                        }
                        {
                            sprintf(monbuf, "-------------------------\n");
                            monprintf(monbuf);
                        }
                        anyPrint = true;
                    }
                    {
                        sprintf(monbuf, "No breaks on r%d.\n", i);
                        monprintf(monbuf);
                    }
                    {
                        sprintf(monbuf, "-------------------------\n");
                        monprintf(monbuf);
                    }
                }
            }
        }
    }
    if (!verbose && !anyPrint) {
        {
            sprintf(monbuf, "No Register breaks found.\n");
            monprintf(monbuf);
        }
    }
}

301
void debuggerOutput(const char* s, uint32_t addr)
302
303
304
305
306
307
308
309
310
311
312
313
314
315
{
    if (s)
        printf("%s", s);
    else {
        char c;

        c = debuggerReadByte(addr);
        addr++;
        while (c) {
            putchar(c);
            c = debuggerReadByte(addr);
            addr++;
        }
    }
316
317
}

318
// checks that the given address is in the DB list
319
bool debuggerInDB(uint32_t address)
320
321
{

322
323
324
325
    for (int i = 0; i < debuggerNumOfDontBreak; i++) {
        if (debuggerNoBreakpointList[i] == address)
            return true;
    }
326

327
    return false;
328
329
}

330
void debuggerDontBreak(int n, char** args)
331
{
332
    if (n == 2) {
333
        uint32_t address = 0;
334
335
336
337
338
339
340
341
342
343
344
345
346
347
        sscanf(args[1], "%x", &address);
        int i = debuggerNumOfDontBreak;
        if (i > NUMBEROFDB) {
            monprintf("Can't have this many DB entries");
            return;
        }
        debuggerNoBreakpointList[i] = address;
        debuggerNumOfDontBreak++;
        {
            sprintf(monbuf, "Added Don't Break at %08x\n", address);
            monprintf(monbuf);
        }
    } else
        debuggerUsage("db");
348
349
}

350
void debuggerDontBreakClear(int n, char** args)
351
{
352
353
354
355
356
357
358
359
    if (n == 1) {
        debuggerNumOfDontBreak = 0;
        {
            sprintf(monbuf, "Cleared Don't Break list.\n");
            monprintf(monbuf);
        }
    } else
        debuggerUsage("dbc");
360
361
362
363
}

void debuggerDumpLoad(int n, char** args)
{
364
    uint32_t address;
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
    char* file;
    FILE* f;
    int c;

    if (n == 3) {
        file = args[1];

        if (!dexp_eval(args[2], &address)) {
            {
                sprintf(monbuf, "Invalid expression in address.\n");
                monprintf(monbuf);
            }
            return;
        }

        f = fopen(file, "rb");
        if (f == NULL) {
            {
                sprintf(monbuf, "Error opening file.\n");
                monprintf(monbuf);
            }
            return;
        }

        fseek(f, 0, SEEK_END);
        int size = ftell(f);
        fseek(f, 0, SEEK_SET);
392

393
394
395
396
397
398
399
        for (int i = 0; i < size; i++) {
            c = fgetc(f);
            if (c == -1)
                break;
            debuggerWriteByte(address, c);
            address++;
        }
400

401
402
403
404
        fclose(f);
    } else
        debuggerUsage("dload");
}
405

406
407
void debuggerDumpSave(int n, char** args)
{
408
409
    uint32_t address;
    uint32_t size;
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
    char* file;
    FILE* f;

    if (n == 4) {
        file = args[1];
        if (!dexp_eval(args[2], &address)) {
            {
                sprintf(monbuf, "Invalid expression in address.\n");
                monprintf(monbuf);
            }
            return;
        }
        if (!dexp_eval(args[3], &size)) {
            {
                sprintf(monbuf, "Invalid expression in size");
                monprintf(monbuf);
            }
            return;
        }
429

430
431
432
433
434
435
436
437
        f = fopen(file, "wb");
        if (f == NULL) {
            {
                sprintf(monbuf, "Error opening file.\n");
                monprintf(monbuf);
            }
            return;
        }
438

439
        for (uint32_t i = 0; i < size; i++) {
440
441
442
            fputc(debuggerReadByte(address), f);
            address++;
        }
443

444
445
446
        fclose(f);
    } else
        debuggerUsage("dsave");
447
448
}

449
450
451
void debuggerEditByte(int n, char** args)
{
    if (n >= 3) {
452
453
        uint32_t address;
        uint32_t value;
454
455
456
457
458
459
460
461
462
463
464
465
466
467
        if (!dexp_eval(args[1], &address)) {
            {
                sprintf(monbuf, "Invalid expression in address.\n");
                monprintf(monbuf);
            }
            return;
        }
        for (int i = 2; i < n; i++) {
            if (!dexp_eval(args[i], &value)) {
                {
                    sprintf(monbuf, "Invalid expression in %d value.Ignored.\n", (i - 1));
                    monprintf(monbuf);
                }
            }
468
            debuggerWriteByte(address, (uint16_t)value);
469
470
471
472
473
474
475
476
477
            address++;
        }
    } else
        debuggerUsage("eb");
}

void debuggerEditHalfWord(int n, char** args)
{
    if (n >= 3) {
478
479
        uint32_t address;
        uint32_t value;
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
        if (!dexp_eval(args[1], &address)) {
            {
                sprintf(monbuf, "Invalid expression in address.\n");
                monprintf(monbuf);
            }
            return;
        }
        if (address & 1) {
            {
                sprintf(monbuf, "Error: address must be half-word aligned\n");
                monprintf(monbuf);
            }
            return;
        }
        for (int i = 2; i < n; i++) {
            if (!dexp_eval(args[i], &value)) {
                {
                    sprintf(monbuf, "Invalid expression in %d value.Ignored.\n", (i - 1));
                    monprintf(monbuf);
                }
            }
501
            debuggerWriteHalfWord(address, (uint16_t)value);
502
503
504
505
506
507
508
509
510
            address += 2;
        }
    } else
        debuggerUsage("eh");
}

void debuggerEditWord(int n, char** args)
{
    if (n >= 3) {
511
512
        uint32_t address;
        uint32_t value;
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
        if (!dexp_eval(args[1], &address)) {
            {
                sprintf(monbuf, "Invalid expression in address.\n");
                monprintf(monbuf);
            }
            return;
        }
        if (address & 3) {
            {
                sprintf(monbuf, "Error: address must be word aligned\n");
                monprintf(monbuf);
            }
            return;
        }
        for (int i = 2; i < n; i++) {
            if (!dexp_eval(args[i], &value)) {
                {
                    sprintf(monbuf, "Invalid expression in %d value.Ignored.\n", (i - 1));
                    monprintf(monbuf);
                }
            }
534
            debuggerWriteMemory(address, (uint32_t)value);
535
536
537
538
539
540
            address += 4;
        }
    } else
        debuggerUsage("ew");
}

541
bool debuggerBreakOnRegisterCondition(uint8_t registerName, uint32_t compareVal, uint32_t regVal, uint8_t type)
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
{
    const char* typeName;
    switch (type) {
    case 1:
        typeName = "equal to";
        break;
    case 2:
        typeName = "greater (unsigned) than";
        break;
    case 3:
        typeName = "smaller (unsigned) than";
        break;
    case 4:
        typeName = "greater (signed) than";
        break;
    case 5:
        typeName = "smaller (signed) than";
        break;
    default:
        typeName = "unknown";
    }
    {
        sprintf(monbuf, "Breakpoint on R%02d : %08x is %s register content (%08x)\n", registerName, compareVal, typeName, regVal);
        monprintf(monbuf);
    }
    if (debuggerInDB(armState ? reg[15].I - 4 : reg[15].I - 2)) {
        {
            sprintf(monbuf, "But this address is marked not to break, so skipped\n");
            monprintf(monbuf);
        }
        return false;
    }
    debugger = true;
    return true;
}

void debuggerBreakRegisterList(bool verbose)
{
    printBreakRegList(verbose);
}

int getRegisterNumber(char* regName)
584
{
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
    int r = -1;
    if (toupper(regName[0]) == 'P' && toupper(regName[1]) == 'C') {
        r = 15;
    } else if (toupper(regName[0]) == 'L' && toupper(regName[1]) == 'R') {
        r = 14;
    } else if (toupper(regName[0]) == 'S' && toupper(regName[1]) == 'P') {
        r = 13;
    } else if (toupper(regName[0]) == 'R') {
        sscanf((char*)(regName + 1), "%d", &r);
    } else {
        sscanf(regName, "%d", &r);
    }

    return r;
}

void debuggerEditRegister(int n, char** args)
{
    if (n == 3) {
        int r = getRegisterNumber(args[1]);
605
        uint32_t val;
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
        if (r > 16) {
            {
                sprintf(monbuf, "Error: Register must be valid (0-16)\n");
                monprintf(monbuf);
            }
            return;
        }
        if (!dexp_eval(args[2], &val)) {
            {
                sprintf(monbuf, "Invalid expression in value.\n");
                monprintf(monbuf);
            }
            return;
        }
        reg[r].I = val;
        {
            sprintf(monbuf, "R%02d=%08X\n", r, val);
            monprintf(monbuf);
        }
    } else
        debuggerUsage("er");
}

void debuggerEval(int n, char** args)
{
    if (n == 2) {
632
        uint32_t result = 0;
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
        if (dexp_eval(args[1], &result)) {
            {
                sprintf(monbuf, " =$%08X\n", result);
                monprintf(monbuf);
            }
        } else {
            {
                sprintf(monbuf, "Invalid expression\n");
                monprintf(monbuf);
            }
        }
    } else
        debuggerUsage("eval");
}

void debuggerFillByte(int n, char** args)
{
    if (n == 4) {
651
652
653
        uint32_t address;
        uint32_t value;
        uint32_t reps;
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
        if (!dexp_eval(args[1], &address)) {
            {
                sprintf(monbuf, "Invalid expression in address.\n");
                monprintf(monbuf);
            }
            return;
        }
        if (!dexp_eval(args[2], &value)) {
            {
                sprintf(monbuf, "Invalid expression in value.\n");
                monprintf(monbuf);
            }
        }
        if (!dexp_eval(args[3], &reps)) {
            {
                sprintf(monbuf, "Invalid expression in repetition number.\n");
                monprintf(monbuf);
            }
        }
673
674
        for (uint32_t i = 0; i < reps; i++) {
            debuggerWriteByte(address, (uint8_t)value);
675
676
677
678
679
680
681
682
683
            address++;
        }
    } else
        debuggerUsage("fillb");
}

void debuggerFillHalfWord(int n, char** args)
{
    if (n == 4) {
684
685
686
        uint32_t address;
        uint32_t value;
        uint32_t reps;
687
688
689
690
691
692
693
        if (!dexp_eval(args[1], &address)) {
            {
                sprintf(monbuf, "Invalid expression in address.\n");
                monprintf(monbuf);
            }
            return;
        } /*
694
695
696
697
		 if(address & 1) {
		 { sprintf(monbuf, "Error: address must be halfword aligned\n"); monprintf(monbuf); }
		 return;
		 }*/
698
699
700
701
702
703
704
705
706
707
708
709
        if (!dexp_eval(args[2], &value)) {
            {
                sprintf(monbuf, "Invalid expression in value.\n");
                monprintf(monbuf);
            }
        }
        if (!dexp_eval(args[3], &reps)) {
            {
                sprintf(monbuf, "Invalid expression in repetition number.\n");
                monprintf(monbuf);
            }
        }
710
711
        for (uint32_t i = 0; i < reps; i++) {
            debuggerWriteHalfWord(address, (uint16_t)value);
712
713
714
715
716
717
718
719
720
            address += 2;
        }
    } else
        debuggerUsage("fillh");
}

void debuggerFillWord(int n, char** args)
{
    if (n == 4) {
721
722
723
        uint32_t address;
        uint32_t value;
        uint32_t reps;
724
725
726
727
728
729
730
        if (!dexp_eval(args[1], &address)) {
            {
                sprintf(monbuf, "Invalid expression in address.\n");
                monprintf(monbuf);
            }
            return;
        } /*
731
732
733
734
		 if(address & 3) {
		 { sprintf(monbuf, "Error: address must be word aligned\n"); monprintf(monbuf); }
		 return;
		 }*/
735
736
737
738
739
740
741
742
743
744
745
746
        if (!dexp_eval(args[2], &value)) {
            {
                sprintf(monbuf, "Invalid expression in value.\n");
                monprintf(monbuf);
            }
        }
        if (!dexp_eval(args[3], &reps)) {
            {
                sprintf(monbuf, "Invalid expression in repetition number.\n");
                monprintf(monbuf);
            }
        }
747
748
        for (uint32_t i = 0; i < reps; i++) {
            debuggerWriteMemory(address, (uint32_t)value);
749
750
751
752
            address += 4;
        }
    } else
        debuggerUsage("fillw");
753
754
755
756
}

unsigned int SearchStart = 0xFFFFFFFF;
unsigned int SearchMaxMatches = 5;
757
uint8_t SearchData[64]; // It actually doesn't make much sense to search for more than 64 bytes, does it?
758
759
760
unsigned int SearchLength = 0;
unsigned int SearchResults;

761
unsigned int AddressToGBA(uint8_t* mem)
762
{
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
    if (mem >= &bios[0] && mem <= &bios[0x3fff])
        return 0x00000000 + (mem - &bios[0]);
    else if (mem >= &workRAM[0] && mem <= &workRAM[0x3ffff])
        return 0x02000000 + (mem - &workRAM[0]);
    else if (mem >= &internalRAM[0] && mem <= &internalRAM[0x7fff])
        return 0x03000000 + (mem - &internalRAM[0]);
    else if (mem >= &ioMem[0] && mem <= &ioMem[0x3ff])
        return 0x04000000 + (mem - &ioMem[0]);
    else if (mem >= &paletteRAM[0] && mem <= &paletteRAM[0x3ff])
        return 0x05000000 + (mem - &paletteRAM[0]);
    else if (mem >= &vram[0] && mem <= &vram[0x1ffff])
        return 0x06000000 + (mem - &vram[0]);
    else if (mem >= &oam[0] && mem <= &oam[0x3ff])
        return 0x07000000 + (mem - &oam[0]);
    else if (mem >= &rom[0] && mem <= &rom[0x1ffffff])
        return 0x08000000 + (mem - &rom[0]);
    else
        return 0xFFFFFFFF;
781
782
783
784
};

void debuggerDoSearch()
{
785
    int count = 0;
786

787
788
    while (true) {
        unsigned int final = SearchStart + SearchLength - 1;
789
790
        uint8_t* end;
        uint8_t* start;
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875

        switch (SearchStart >> 24) {
        case 0:
            if (final > 0x00003FFF) {
                SearchStart = 0x02000000;
                continue;
            } else {
                start = bios + (SearchStart & 0x3FFF);
                end = bios + 0x3FFF;
                break;
            };
        case 2:
            if (final > 0x0203FFFF) {
                SearchStart = 0x03000000;
                continue;
            } else {
                start = workRAM + (SearchStart & 0x3FFFF);
                end = workRAM + 0x3FFFF;
                break;
            };
        case 3:
            if (final > 0x03007FFF) {
                SearchStart = 0x04000000;
                continue;
            } else {
                start = internalRAM + (SearchStart & 0x7FFF);
                end = internalRAM + 0x7FFF;
                break;
            };
        case 4:
            if (final > 0x040003FF) {
                SearchStart = 0x05000000;
                continue;
            } else {
                start = ioMem + (SearchStart & 0x3FF);
                end = ioMem + 0x3FF;
                break;
            };
        case 5:
            if (final > 0x050003FF) {
                SearchStart = 0x06000000;
                continue;
            } else {
                start = paletteRAM + (SearchStart & 0x3FF);
                end = paletteRAM + 0x3FF;
                break;
            };
        case 6:
            if (final > 0x0601FFFF) {
                SearchStart = 0x07000000;
                continue;
            } else {
                start = vram + (SearchStart & 0x1FFFF);
                end = vram + 0x1FFFF;
                break;
            };
        case 7:
            if (final > 0x070003FF) {
                SearchStart = 0x08000000;
                continue;
            } else {
                start = oam + (SearchStart & 0x3FF);
                end = oam + 0x3FF;
                break;
            };
        case 8:
        case 9:
        case 10:
        case 11:
        case 12:
        case 13:
            if (final <= 0x09FFFFFF) {
                start = rom + (SearchStart & 0x01FFFFFF);
                end = rom + 0x01FFFFFF;
                break;
            };
        default: {
            sprintf(monbuf, "Search completed.\n");
            monprintf(monbuf);
        }
            SearchLength = 0;
            return;
        };

        end -= SearchLength - 1;
876
        uint8_t firstbyte = SearchData[0];
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
        while (start <= end) {
            while ((start <= end) && (*start != firstbyte))
                start++;

            if (start > end)
                break;

            unsigned int p = 1;
            while ((start[p] == SearchData[p]) && (p < SearchLength))
                p++;

            if (p == SearchLength) {
                {
                    sprintf(monbuf, "Search result (%d): %08x\n", count + SearchResults, AddressToGBA(start));
                    monprintf(monbuf);
                }
                count++;
                if (count == SearchMaxMatches) {
                    SearchStart = AddressToGBA(start + p);
                    SearchResults += count;
                    return;
                };

                start += p; // assume areas don't overlap; alternative: start++;
            } else
                start++;
        };

        SearchStart = AddressToGBA(end + SearchLength - 1) + 1;
    };
907
908
};

909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
void debuggerFindText(int n, char** args)
{
    if ((n == 4) || (n == 3)) {
        SearchResults = 0;
        if (!dexp_eval(args[1], &SearchStart)) {
            {
                sprintf(monbuf, "Invalid expression.\n");
                monprintf(monbuf);
            }
            return;
        }

        if (n == 4) {
            sscanf(args[2], "%u", &SearchMaxMatches);
            strncpy((char*)SearchData, args[3], 64);
            SearchLength = strlen(args[3]);
        } else if (n == 3) {
            strncpy((char*)SearchData, args[2], 64);
            SearchLength = strlen(args[2]);
        };

        if (SearchLength > 64) {
            {
                sprintf(monbuf, "Entered string (length: %d) is longer than 64 bytes and was cut.\n", SearchLength);
                monprintf(monbuf);
            }
            SearchLength = 64;
        };

        debuggerDoSearch();

    } else
        debuggerUsage("ft");
942
943
};

944
void debuggerFindHex(int n, char** args)
945
{
946
947
948
949
950
951
952
953
954
    if ((n == 4) || (n == 3)) {
        SearchResults = 0;
        if (!dexp_eval(args[1], &SearchStart)) {
            {
                sprintf(monbuf, "Invalid expression.\n");
                monprintf(monbuf);
            }
            return;
        }
955

956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
        char SearchHex[128];
        if (n == 4) {
            sscanf(args[2], "%u", &SearchMaxMatches);
            strncpy(SearchHex, args[3], 128);
            SearchLength = strlen(args[3]);
        } else if (n == 3) {
            strncpy(SearchHex, args[2], 128);
            SearchLength = strlen(args[2]);
        };

        if (SearchLength & 1) {
            sprintf(monbuf, "Unaligned bytecount: %d,5. Last digit (%c) cut.\n", SearchLength / 2, SearchHex[SearchLength - 1]);
            monprintf(monbuf);
        }

        SearchLength /= 2;

        if (SearchLength > 64) {
            {
                sprintf(monbuf, "Entered string (length: %d) is longer than 64 bytes and was cut.\n", SearchLength);
                monprintf(monbuf);
            }
            SearchLength = 64;
        };

        for (unsigned int i = 0; i < SearchLength; i++) {
            unsigned int cbuf = 0;
            sscanf(&SearchHex[i << 1], "%02x", &cbuf);
            SearchData[i] = cbuf;
        };
986

987
        debuggerDoSearch();
988

989
990
    } else
        debuggerUsage("fh");
991
992
};

993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
void debuggerFindResume(int n, char** args)
{
    if ((n == 1) || (n == 2)) {
        if (SearchLength == 0) {
            {
                sprintf(monbuf, "Error: No search in progress. Start a search with ft or fh.\n");
                monprintf(monbuf);
            }
            debuggerUsage("fr");
            return;
        };

        if (n == 2)
            sscanf(args[1], "%u", &SearchMaxMatches);

        debuggerDoSearch();

    } else
        debuggerUsage("fr");
};

void debuggerCopyByte(int n, char** args)
{
1016
1017
1018
1019
    uint32_t source;
    uint32_t dest;
    uint32_t number = 1;
    uint32_t reps = 1;
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
    if (n > 5 || n < 3) {
        debuggerUsage("copyb");
    }

    if (n == 5) {
        if (!dexp_eval(args[4], &reps)) {
            {
                sprintf(monbuf, "Invalid expression in repetition number.\n");
                monprintf(monbuf);
            }
        }
    }
    if (n > 3) {
        if (!dexp_eval(args[3], &number)) {
            {
                sprintf(monbuf, "Invalid expression in number of copy units.\n");
                monprintf(monbuf);
            }
        }
    }
    if (!dexp_eval(args[1], &source)) {
        {
            sprintf(monbuf, "Invalid expression in source address.\n");
            monprintf(monbuf);
        }
        return;
    }
    if (!dexp_eval(args[2], &dest)) {
        {
            sprintf(monbuf, "Invalid expression in destination address.\n");
            monprintf(monbuf);
        }
    }

1054
1055
    for (uint32_t j = 0; j < reps; j++) {
        for (uint32_t i = 0; i < number; i++) {
1056
1057
1058
1059
1060
1061
1062
1063
            debuggerWriteByte(dest + i, debuggerReadByte(source + i));
        }
        dest += number;
    }
}

void debuggerCopyHalfWord(int n, char** args)
{
1064
1065
1066
1067
    uint32_t source;
    uint32_t dest;
    uint32_t number = 2;
    uint32_t reps = 1;
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
    if (n > 5 || n < 3) {
        debuggerUsage("copyh");
    }

    if (n == 5) {
        if (!dexp_eval(args[4], &reps)) {
            {
                sprintf(monbuf, "Invalid expression in repetition number.\n");
                monprintf(monbuf);
            }
        }
    }
    if (n > 3) {
        if (!dexp_eval(args[3], &number)) {
            {
                sprintf(monbuf, "Invalid expression in number of copy units.\n");
                monprintf(monbuf);
            }
        }
        number = number << 1;
    }
    if (!dexp_eval(args[1], &source)) {
        {
            sprintf(monbuf, "Invalid expression in source address.\n");
            monprintf(monbuf);
        }
        return;
    }
    if (!dexp_eval(args[2], &dest)) {
        {
            sprintf(monbuf, "Invalid expression in destination address.\n");
            monprintf(monbuf);
        }
    }

1103
1104
    for (uint32_t j = 0; j < reps; j++) {
        for (uint32_t i = 0; i < number; i += 2) {
1105
1106
1107
1108
1109
1110
1111
1112
            debuggerWriteHalfWord(dest + i, debuggerReadHalfWord(source + i));
        }
        dest += number;
    }
}

void debuggerCopyWord(int n, char** args)
{
1113
1114
1115
1116
    uint32_t source;
    uint32_t dest;
    uint32_t number = 4;
    uint32_t reps = 1;
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
    if (n > 5 || n < 3) {
        debuggerUsage("copyw");
    }

    if (n == 5) {
        if (!dexp_eval(args[4], &reps)) {
            {
                sprintf(monbuf, "Invalid expression in repetition number.\n");
                monprintf(monbuf);
            }
        }
    }
    if (n > 3) {
        if (!dexp_eval(args[3], &number)) {
            {
                sprintf(monbuf, "Invalid expression in number of copy units.\n");
                monprintf(monbuf);
            }
        }
        number = number << 2;
    }
    if (!dexp_eval(args[1], &source)) {
        {
            sprintf(monbuf, "Invalid expression in source address.\n");
            monprintf(monbuf);
        }
        return;
    }
    if (!dexp_eval(args[2], &dest)) {
        {
            sprintf(monbuf, "Invalid expression in destination address.\n");
            monprintf(monbuf);
        }
    }

1152
1153
    for (uint32_t j = 0; j < reps; j++) {
        for (uint32_t i = 0; i < number; i += 4) {
1154
1155
1156
1157
            debuggerWriteMemory(dest + i, debuggerReadMemory(source + i));
        }
        dest += number;
    }
1158
1159
1160
1161
}

void debuggerIoVideo()
{
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
    {
        sprintf(monbuf, "DISPCNT  = %04x\n", DISPCNT);
        monprintf(monbuf);
    }
    {
        sprintf(monbuf, "DISPSTAT = %04x\n", DISPSTAT);
        monprintf(monbuf);
    }
    {
        sprintf(monbuf, "VCOUNT   = %04x\n", VCOUNT);
        monprintf(monbuf);
    }
    {
        sprintf(monbuf, "BG0CNT   = %04x\n", BG0CNT);
        monprintf(monbuf);
    }
    {
        sprintf(monbuf, "BG1CNT   = %04x\n", BG1CNT);
        monprintf(monbuf);
    }
    {
        sprintf(monbuf, "BG2CNT   = %04x\n", BG2CNT);
        monprintf(monbuf);
    }
    {
        sprintf(monbuf, "BG3CNT   = %04x\n", BG3CNT);
        monprintf(monbuf);
    }
    {
        sprintf(monbuf, "WIN0H    = %04x\n", WIN0H);
        monprintf(monbuf);
    }
    {
        sprintf(monbuf, "WIN0V    = %04x\n", WIN0V);
        monprintf(monbuf);
    }
    {
        sprintf(monbuf, "WIN1H    = %04x\n", WIN1H);
        monprintf(monbuf);
    }
    {
        sprintf(monbuf, "WIN1V    = %04x\n", WIN1V);
        monprintf(monbuf);
    }
    {
        sprintf(monbuf, "WININ    = %04x\n", WININ);
        monprintf(monbuf);
    }
    {
        sprintf(monbuf, "WINOUT   = %04x\n", WINOUT);
        monprintf(monbuf);
    }
    {
        sprintf(monbuf, "MOSAIC   = %04x\n", MOSAIC);
        monprintf(monbuf);
    }
    {
        sprintf(monbuf, "BLDMOD   = %04x\n", BLDMOD);
        monprintf(monbuf);
    }
    {
        sprintf(monbuf, "COLEV    = %04x\n", COLEV);
        monprintf(monbuf);
    }
    {
        sprintf(monbuf, "COLY     = %04x\n", COLY);
        monprintf(monbuf);
    }
1230
1231
1232
1233
}

void debuggerIoVideo2()
{
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
    {
        sprintf(monbuf, "BG0HOFS  = %04x\n", BG0HOFS);
        monprintf(monbuf);
    }
    {
        sprintf(monbuf, "BG0VOFS  = %04x\n", BG0VOFS);
        monprintf(monbuf);
    }
    {
        sprintf(monbuf, "BG1HOFS  = %04x\n", BG1HOFS);
        monprintf(monbuf);
    }
    {
        sprintf(monbuf, "BG1VOFS  = %04x\n", BG1VOFS);
        monprintf(monbuf);
    }
    {
        sprintf(monbuf, "BG2HOFS  = %04x\n", BG2HOFS);
        monprintf(monbuf);
    }
    {
        sprintf(monbuf, "BG2VOFS  = %04x\n", BG2VOFS);
        monprintf(monbuf);
    }
    {
        sprintf(monbuf, "BG3HOFS  = %04x\n", BG3HOFS);
        monprintf(monbuf);
    }
    {
        sprintf(monbuf, "BG3VOFS  = %04x\n", BG3VOFS);
        monprintf(monbuf);
    }
    {
        sprintf(monbuf, "BG2PA    = %04x\n", BG2PA);
        monprintf(monbuf);
    }
    {
        sprintf(monbuf, "BG2PB    = %04x\n", BG2PB);
        monprintf(monbuf);
    }
    {
        sprintf(monbuf, "BG2PC    = %04x\n", BG2PC);
        monprintf(monbuf);
    }
    {
        sprintf(monbuf, "BG2PD    = %04x\n", BG2PD);
        monprintf(monbuf);
    }
    {
        sprintf(monbuf, "BG2X     = %08x\n", (BG2X_H << 16) | BG2X_L);
        monprintf(monbuf);
    }
    {
        sprintf(monbuf, "BG2Y     = %08x\n", (BG2Y_H << 16) | BG2Y_L);
        monprintf(monbuf);
    }
    {
        sprintf(monbuf, "BG3PA    = %04x\n", BG3PA);
        monprintf(monbuf);
    }
    {
        sprintf(monbuf, "BG3PB    = %04x\n", BG3PB);
        monprintf(monbuf);
    }
    {
        sprintf(monbuf, "BG3PC    = %04x\n", BG3PC);
        monprintf(monbuf);
    }
    {
        sprintf(monbuf, "BG3PD    = %04x\n", BG3PD);
        monprintf(monbuf);
    }
    {
        sprintf(monbuf, "BG3X     = %08x\n", (BG3X_H << 16) | BG3X_L);
        monprintf(monbuf);
    }
    {
        sprintf(monbuf, "BG3Y     = %08x\n", (BG3Y_H << 16) | BG3Y_L);
        monprintf(monbuf);
    }
1314
1315
1316
1317
}

void debuggerIoDMA()
{
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
    {
        sprintf(monbuf, "DM0SAD   = %08x\n", (DM0SAD_H << 16) | DM0SAD_L);
        monprintf(monbuf);
    }
    {
        sprintf(monbuf, "DM0DAD   = %08x\n", (DM0DAD_H << 16) | DM0DAD_L);
        monprintf(monbuf);
    }
    {
        sprintf(monbuf, "DM0CNT   = %08x\n", (DM0CNT_H << 16) | DM0CNT_L);
        monprintf(monbuf);
    }
    {
        sprintf(monbuf, "DM1SAD   = %08x\n", (DM1SAD_H << 16) | DM1SAD_L);
        monprintf(monbuf);
    }
    {
        sprintf(monbuf, "DM1DAD   = %08x\n", (DM1DAD_H << 16) | DM1DAD_L);
        monprintf(monbuf);
    }
    {
        sprintf(monbuf, "DM1CNT   = %08x\n", (DM1CNT_H << 16) | DM1CNT_L);
        monprintf(monbuf);
    }
    {
        sprintf(monbuf, "DM2SAD   = %08x\n", (DM2SAD_H << 16) | DM2SAD_L);
        monprintf(monbuf);
    }
    {
        sprintf(monbuf, "DM2DAD   = %08x\n", (DM2DAD_H << 16) | DM2DAD_L);
        monprintf(monbuf);
    }
    {
        sprintf(monbuf, "DM2CNT   = %08x\n", (DM2CNT_H << 16) | DM2CNT_L);
        monprintf(monbuf);
    }
    {
        sprintf(monbuf, "DM3SAD   = %08x\n", (DM3SAD_H << 16) | DM3SAD_L);
        monprintf(monbuf);
    }
    {
        sprintf(monbuf, "DM3DAD   = %08x\n", (DM3DAD_H << 16) | DM3DAD_L);
        monprintf(monbuf);
    }
    {
        sprintf(monbuf, "DM3CNT   = %08x\n", (DM3CNT_H << 16) | DM3CNT_L);
        monprintf(monbuf);
    }
1366
1367
1368
1369
}

void debuggerIoTimer()
{
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
    {
        sprintf(monbuf, "TM0D     = %04x\n", TM0D);
        monprintf(monbuf);
    }
    {
        sprintf(monbuf, "TM0CNT   = %04x\n", TM0CNT);
        monprintf(monbuf);
    }
    {
        sprintf(monbuf, "TM1D     = %04x\n", TM1D);
        monprintf(monbuf);
    }
    {
        sprintf(monbuf, "TM1CNT   = %04x\n", TM1CNT);
        monprintf(monbuf);
    }
    {
        sprintf(monbuf, "TM2D     = %04x\n", TM2D);
        monprintf(monbuf);
    }
    {
        sprintf(monbuf, "TM2CNT   = %04x\n", TM2CNT);
        monprintf(monbuf);
    }
    {
        sprintf(monbuf, "TM3D     = %04x\n", TM3D);
        monprintf(monbuf);
    }
    {
        sprintf(monbuf, "TM3CNT   = %04x\n", TM3CNT);
        monprintf(monbuf);
    }
1402
1403
1404
1405
}

void debuggerIoMisc()
{
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
    {
        sprintf(monbuf, "P1       = %04x\n", P1);
        monprintf(monbuf);
    }
    {
        sprintf(monbuf, "IE       = %04x\n", IE);
        monprintf(monbuf);
    }
    {
        sprintf(monbuf, "IF       = %04x\n", IF);
        monprintf(monbuf);
    }
    {
        sprintf(monbuf, "IME      = %04x\n", IME);
        monprintf(monbuf);
    }
1422
1423
}

1424
void debuggerIo(int n, char** args)
1425
{
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
    if (n == 1) {
        debuggerIoVideo();
        return;
    }
    if (!strcmp(args[1], "video"))
        debuggerIoVideo();
    else if (!strcmp(args[1], "video2"))
        debuggerIoVideo2();
    else if (!strcmp(args[1], "dma"))
        debuggerIoDMA();
    else if (!strcmp(args[1], "timer"))
        debuggerIoTimer();
    else if (!strcmp(args[1], "misc"))
        debuggerIoMisc();
    else {
        sprintf(monbuf, "Unrecognized option %s\n", args[1]);
        monprintf(monbuf);
    }
1444
1445
}

1446
#define ASCII(c) (c)<32 ? '.' : (c)> 127 ? '.' : (c)
1447
1448
1449
1450
1451
1452
1453
1454

bool canUseTbl = true;
bool useWordSymbol = false;
bool thereIsATable = false;
char** wordSymbol;
bool isTerminator[256];
bool isNewline[256];
bool isTab[256];
1455
uint8_t largestSymbol = 1;
1456

1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
void freeWordSymbolContents()
{
    for (int i = 0; i < 256; i++) {
        if (wordSymbol[i])
            free(wordSymbol[i]);
        wordSymbol[i] = NULL;
        isTerminator[i] = false;
        isNewline[i] = false;
        isTab[i] = false;
    }
1467
1468
}

1469
void freeWordSymbol()
1470
{
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
    useWordSymbol = false;
    thereIsATable = false;
    free(wordSymbol);
    largestSymbol = 1;
}

void debuggerReadCharTable(int n, char** args)
{
    if (n == 2) {
        if (!canUseTbl) {
            {
                sprintf(monbuf, "Cannot operate over character table, as it was disabled.\n");
                monprintf(monbuf);
            }
            return;
        }
        if (strcmp(args[1], "none") == 0) {
            freeWordSymbol();
            {
                sprintf(monbuf, "Cleared table. Reverted to ASCII.\n");
                monprintf(monbuf);
            }
            return;
        }
        FILE* tlb = fopen(args[1], "r");
        if (!tlb) {
            {
                sprintf(monbuf, "Could not open specified file. Abort.\n");
                monprintf(monbuf);
            }
            return;
        }
        char buffer[30];
1504
        uint32_t slot;
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
        char* character = (char*)calloc(10, sizeof(char));
        wordSymbol = (char**)calloc(256, sizeof(char*));
        while (fgets(buffer, 30, tlb)) {

            sscanf(buffer, "%02x=%s", &slot, character);

            if (character[0]) {
                if (strlen(character) == 4) {
                    if ((character[0] == '<') && (character[1] == '\\') && (character[3] == '>')) {
                        if (character[2] == '0') {
                            isTerminator[slot] = true;
                        }
                        if (character[2] == 'n') {
                            isNewline[slot] = true;
                        }
                        if (character[2] == 't') {
                            isTab[slot] = true;
                        }
                        continue;
                    } else
                        wordSymbol[slot] = character;
                } else
                    wordSymbol[slot] = character;
            } else
ZachBacon's avatar
ZachBacon committed
1529
                wordSymbol[slot] = (char*)' ';
1530
1531
1532
1533
1534
1535
1536
1537

            if (largestSymbol < strlen(character))
                largestSymbol = strlen(character);

            character = (char*)malloc(10);
        }
        useWordSymbol = true;
        thereIsATable = true;
1538

1539
1540
1541
    } else {
        debuggerUsage("tbl");
    }
1542
1543
}

1544
void printCharGroup(uint32_t addr, bool useAscii)
1545
{
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
    for (int i = 0; i < 16; i++) {
        if (useWordSymbol && !useAscii) {
            char* c = wordSymbol[debuggerReadByte(addr + i)];
            int j;
            if (c) {
                {
                    sprintf(monbuf, "%s", c);
                    monprintf(monbuf);
                }
                j = strlen(c);
            } else {
                j = 0;
            }
            while (j < largestSymbol) {
                {
                    sprintf(monbuf, " ");
                    monprintf(monbuf);
                }
                j++;
            }
        } else {
            {
                sprintf(monbuf, "%c", ASCII(debuggerReadByte(addr + i)));
                monprintf(monbuf);
            }
        }
    }
}
1574

1575
1576
1577
void debuggerMemoryByte(int n, char** args)
{
    if (n == 2) {
1578
        uint32_t addr = 0;
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611

        if (!dexp_eval(args[1], &addr)) {
            {
                sprintf(monbuf, "Invalid expression\n");
                monprintf(monbuf);
            }
            return;
        }
        for (int loop = 0; loop < 16; loop++) {
            {
                sprintf(monbuf, "%08x ", addr);
                monprintf(monbuf);
            }
            for (int j = 0; j < 16; j++) {
                {
                    sprintf(monbuf, "%02x ", debuggerReadByte(addr + j));
                    monprintf(monbuf);
                }
            }
            printCharGroup(addr, true);
            {
                sprintf(monbuf, "\n");
                monprintf(monbuf);
            }
            addr += 16;
        }
    } else
        debuggerUsage("mb");
}

void debuggerMemoryHalfWord(int n, char** args)
{
    if (n == 2) {
1612
        uint32_t addr = 0;
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648

        if (!dexp_eval(args[1], &addr)) {
            {
                sprintf(monbuf, "Invalid expression\n");
                monprintf(monbuf);
            }
            return;
        }

        addr = addr & 0xfffffffe;

        for (int loop = 0; loop < 16; loop++) {
            {
                sprintf(monbuf, "%08x ", addr);
                monprintf(monbuf);
            }
            for (int j = 0; j < 16; j += 2) {
                {
                    sprintf(monbuf, "%02x%02x ", debuggerReadByte(addr + j + 1), debuggerReadByte(addr + j));
                    monprintf(monbuf);
                }
            }
            printCharGroup(addr, true);
            {
                sprintf(monbuf, "\n");
                monprintf(monbuf);
            }
            addr += 16;
        }
    } else
        debuggerUsage("mh");
}

void debuggerMemoryWord(int n, char** args)
{
    if (n == 2) {
1649
        uint32_t addr = 0;
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
        if (!dexp_eval(args[1], &addr)) {
            {
                sprintf(monbuf, "Invalid expression\n");
                monprintf(monbuf);
            }
            return;
        }
        addr = addr & 0xfffffffc;
        for (int loop = 0; loop < 16; loop++) {
            {
                sprintf(monbuf, "%08x ", addr);
                monprintf(monbuf);
            }
            for (int j = 0; j < 16; j += 4) {
                {
                    sprintf(monbuf, "%02x%02x%02x%02x ", debuggerReadByte(addr + j + 3), debuggerReadByte(addr + j + 2), debuggerReadByte(addr + j + 1), debuggerReadByte(addr + j));
                    monprintf(monbuf);
                }
            }
            printCharGroup(addr, true);
            {
                sprintf(monbuf, "\n");
                monprintf(monbuf);
            }
            addr += 16;
        }
    } else
        debuggerUsage("mw");
}

void debuggerStringRead(int n, char** args)
{
    if (n == 2) {
1683
        uint32_t addr = 0;
1684
1685
1686
1687
1688
1689
1690
1691
1692

        if (!dexp_eval(args[1], &addr)) {
            {
                sprintf(monbuf, "Invalid expression\n");
                monprintf(monbuf);
            }
            return;
        }
        for (int i = 0; i < 512; i++) {
1693
            uint8_t slot = debuggerReadByte(addr + i);
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832

            if (useWordSymbol) {
                if (isTerminator[slot]) {
                    {
                        sprintf(monbuf, "\n");
                        monprintf(monbuf);
                    }
                    return;
                } else if (isNewline[slot]) {
                    {
                        sprintf(monbuf, "\n");
                        monprintf(monbuf);
                    }
                } else if (isTab[slot]) {
                    {
                        sprintf(monbuf, "\t");
                        monprintf(monbuf);
                    }
                } else {
                    if (wordSymbol[slot]) {
                        {
                            sprintf(monbuf, "%s", wordSymbol[slot]);
                            monprintf(monbuf);
                        }
                    }
                }
            } else {
                {
                    sprintf(monbuf, "%c", ASCII(slot));
                    monprintf(monbuf);
                }
            }
        }
    } else
        debuggerUsage("ms");
}

void debuggerRegisters(int, char**)
{
    {
        sprintf(monbuf, "R00=%08x R04=%08x R08=%08x R12=%08x\n", reg[0].I, reg[4].I, reg[8].I, reg[12].I);
        monprintf(monbuf);
    }
    {
        sprintf(monbuf, "R01=%08x R05=%08x R09=%08x R13=%08x\n", reg[1].I, reg[5].I, reg[9].I, reg[13].I);
        monprintf(monbuf);
    }
    {
        sprintf(monbuf, "R02=%08x R06=%08x R10=%08x R14=%08x\n", reg[2].I, reg[6].I, reg[10].I, reg[14].I);
        monprintf(monbuf);
    }
    {
        sprintf(monbuf, "R03=%08x R07=%08x R11=%08x R15=%08x\n", reg[3].I, reg[7].I, reg[11].I, reg[15].I);
        monprintf(monbuf);
    }
    {
        sprintf(monbuf, "CPSR=%08x (%c%c%c%c%c%c%c Mode: %02x)\n",
            reg[16].I,
            (N_FLAG ? 'N' : '.'),
            (Z_FLAG ? 'Z' : '.'),
            (C_FLAG ? 'C' : '.'),
            (V_FLAG ? 'V' : '.'),
            (armIrqEnable ? '.' : 'I'),
            ((!(reg[16].I & 0x40)) ? '.' : 'F'),
            (armState ? '.' : 'T'),
            armMode);
        monprintf(monbuf);
    }
}

void debuggerExecuteCommands(int n