-
-
Notifications
You must be signed in to change notification settings - Fork 312
/
remote.cpp
4271 lines (3917 loc) · 119 KB
/
remote.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
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
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
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
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
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
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
876
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
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
942
943
944
945
946
947
948
949
950
951
952
953
954
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
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#ifndef __LIBRETRO__
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <iosfwd>
#include <sstream>
#include <vector>
#ifndef _WIN32
#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
#else // _WIN32
#include <io.h>
#include <winsock.h>
#define socklen_t int
#define close closesocket
#define read _read
#define write _write
#define strdup _strdup
#endif // _WIN32
#include "BreakpointStructures.h"
#include "GBA.h"
#include "elf.h"
#include "remote.h"
#include <iomanip>
#include <iostream>
extern bool debugger;
extern int emulating;
extern void CPUUpdateCPSR();
int remotePort = 0;
int remoteSignal = 5;
SOCKET remoteSocket = -1;
SOCKET remoteListenSocket = -1;
bool remoteConnected = false;
bool remoteResumed = false;
int (*remoteSendFnc)(char*, int) = NULL;
int (*remoteRecvFnc)(char*, int) = NULL;
bool (*remoteInitFnc)() = NULL;
void (*remoteCleanUpFnc)() = NULL;
#ifndef SDL
void remoteSetSockets(SOCKET l, SOCKET r)
{
remoteSocket = r;
remoteListenSocket = l;
}
#endif
#define debuggerReadMemory(addr) \
(*(uint32_t*)&map[(addr) >> 24].address[(addr)&map[(addr) >> 24].mask])
#define debuggerReadHalfWord(addr) \
(*(uint16_t*)&map[(addr) >> 24].address[(addr)&map[(addr) >> 24].mask])
#define debuggerReadByte(addr) \
map[(addr) >> 24].address[(addr)&map[(addr) >> 24].mask]
#define debuggerWriteMemory(addr, value) \
*(uint32_t*)&map[(addr) >> 24].address[(addr)&map[(addr) >> 24].mask] = (value)
#define debuggerWriteHalfWord(addr, value) \
*(uint16_t*)&map[(addr) >> 24].address[(addr)&map[(addr) >> 24].mask] = (value)
#define debuggerWriteByte(addr, value) \
map[(addr) >> 24].address[(addr)&map[(addr) >> 24].mask] = (value)
bool dontBreakNow = false;
int debuggerNumOfDontBreak = 0;
int debuggerRadix = 0;
#define NUMBEROFDB 1000
uint32_t debuggerNoBreakpointList[NUMBEROFDB];
const char* cmdAliasTable[] = { "help", "?", "h", "?", "continue", "c", "next", "n",
"cpyb", "copyb", "cpyh", "copyh", "cpyw", "copyw",
"exe", "execute", "exec", "execute",
NULL, NULL };
struct DebuggerCommand {
const char* name;
void (*function)(int, char**);
const char* help;
const char* syntax;
};
char monbuf[1000];
void monprintf(std::string line);
std::string StringToHex(std::string& cmd);
std::string HexToString(char* p);
void debuggerUsage(const char* cmd);
void debuggerHelp(int n, char** args);
void printFlagHelp();
void dbgExecute(std::string& cmd);
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);
regBreak* breakRegList[16];
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] = {
&lowRegBreakCounter[0],
&medRegBreakCounter[0],
&highRegBreakCounter[0],
&statusRegBreakCounter[0]
};
uint32_t lastWasBranch = 0;
struct regBreak* getFromBreakRegList(uint8_t regnum, int location)
{
if (location > regBreakCounter[regnum >> 2][regnum & 3])
return NULL;
struct regBreak* ans = breakRegList[regnum];
for (int i = 0; i < location && ans; i++) {
ans = ans->next;
}
return ans;
}
bool enableRegBreak = false;
reg_pair oldReg[16];
uint32_t regDiff[16];
void breakReg_check(int i)
{
struct regBreak* brkR = breakRegList[i];
bool notFound = true;
uint8_t counter = regBreakCounter[i >> 2][i & 3];
for (int bri = 0; (bri < counter) && notFound; bri++) {
if (!brkR) {
regBreakCounter[i >> 2][i & 3] = (uint8_t)bri;
break;
} else {
if (brkR->flags != 0) {
uint32_t regVal = (i == 15 ? (armState ? reg[15].I - 4 : reg[15].I - 2) : reg[i].I);
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);
}
}
void deleteFromBreakRegList(uint8_t regNum, int num)
{
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]--;
}
void addBreakRegToList(uint8_t regnum, uint8_t flags, uint32_t value)
{
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);
}
}
}
void debuggerOutput(const char* s, uint32_t addr)
{
if (s)
printf("%s", s);
else {
char c;
c = debuggerReadByte(addr);
addr++;
while (c) {
putchar(c);
c = debuggerReadByte(addr);
addr++;
}
}
}
// checks that the given address is in the DB list
bool debuggerInDB(uint32_t address)
{
for (int i = 0; i < debuggerNumOfDontBreak; i++) {
if (debuggerNoBreakpointList[i] == address)
return true;
}
return false;
}
void debuggerDontBreak(int n, char** args)
{
if (n == 2) {
uint32_t address = 0;
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");
}
void debuggerDontBreakClear(int n, char** args)
{
if (n == 1) {
debuggerNumOfDontBreak = 0;
{
sprintf(monbuf, "Cleared Don't Break list.\n");
monprintf(monbuf);
}
} else
debuggerUsage("dbc");
}
void debuggerDumpLoad(int n, char** args)
{
uint32_t address;
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);
for (int i = 0; i < size; i++) {
c = fgetc(f);
if (c == -1)
break;
debuggerWriteByte(address, c);
address++;
}
fclose(f);
} else
debuggerUsage("dload");
}
void debuggerDumpSave(int n, char** args)
{
uint32_t address;
uint32_t size;
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;
}
f = fopen(file, "wb");
if (f == NULL) {
{
sprintf(monbuf, "Error opening file.\n");
monprintf(monbuf);
}
return;
}
for (uint32_t i = 0; i < size; i++) {
fputc(debuggerReadByte(address), f);
address++;
}
fclose(f);
} else
debuggerUsage("dsave");
}
void debuggerEditByte(int n, char** args)
{
if (n >= 3) {
uint32_t address;
uint32_t value;
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);
}
}
debuggerWriteByte(address, (uint16_t)value);
address++;
}
} else
debuggerUsage("eb");
}
void debuggerEditHalfWord(int n, char** args)
{
if (n >= 3) {
uint32_t address;
uint32_t value;
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);
}
}
debuggerWriteHalfWord(address, (uint16_t)value);
address += 2;
}
} else
debuggerUsage("eh");
}
void debuggerEditWord(int n, char** args)
{
if (n >= 3) {
uint32_t address;
uint32_t value;
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);
}
}
debuggerWriteMemory(address, (uint32_t)value);
address += 4;
}
} else
debuggerUsage("ew");
}
bool debuggerBreakOnRegisterCondition(uint8_t registerName, uint32_t compareVal, uint32_t regVal, uint8_t type)
{
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)
{
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]);
uint32_t val;
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) {
uint32_t result = 0;
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) {
uint32_t address;
uint32_t value;
uint32_t reps;
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);
}
}
for (uint32_t i = 0; i < reps; i++) {
debuggerWriteByte(address, (uint8_t)value);
address++;
}
} else
debuggerUsage("fillb");
}
void debuggerFillHalfWord(int n, char** args)
{
if (n == 4) {
uint32_t address;
uint32_t value;
uint32_t reps;
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 halfword aligned\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);
}
}
for (uint32_t i = 0; i < reps; i++) {
debuggerWriteHalfWord(address, (uint16_t)value);
address += 2;
}
} else
debuggerUsage("fillh");
}
void debuggerFillWord(int n, char** args)
{
if (n == 4) {
uint32_t address;
uint32_t value;
uint32_t reps;
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;
}*/
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);
}
}
for (uint32_t i = 0; i < reps; i++) {
debuggerWriteMemory(address, (uint32_t)value);
address += 4;
}
} else
debuggerUsage("fillw");
}
unsigned int SearchStart = 0xFFFFFFFF;
unsigned int SearchMaxMatches = 5;
uint8_t SearchData[64]; // It actually doesn't make much sense to search for more than 64 bytes, does it?
unsigned int SearchLength = 0;
unsigned int SearchResults;
unsigned int AddressToGBA(uint8_t* mem)
{
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;
};
void debuggerDoSearch()
{
int count = 0;
while (true) {
unsigned int final = SearchStart + SearchLength - 1;
uint8_t* end;
uint8_t* start;
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;
uint8_t firstbyte = SearchData[0];
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;
};
};
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");
};
void debuggerFindHex(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;
}
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;
};
debuggerDoSearch();
} else
debuggerUsage("fh");
};
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);
}