forked from LIJI32/SameBoy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdebugger.c
2950 lines (2600 loc) · 101 KB
/
debugger.c
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
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "gb.h"
typedef struct {
bool has_bank;
uint16_t bank:9;
uint16_t value;
} value_t;
typedef struct {
enum {
LVALUE_MEMORY,
LVALUE_MEMORY16,
LVALUE_REG16,
LVALUE_REG_H,
LVALUE_REG_L,
} kind;
union {
uint16_t *register_address;
value_t memory_address;
};
} lvalue_t;
#define VALUE_16(x) ((value_t){false, 0, (x)})
struct GB_breakpoint_s {
unsigned id;
union {
struct {
uint16_t addr;
uint16_t bank; /* -1 = any bank*/
};
uint32_t key; /* For sorting and comparing */
};
char *condition;
bool is_jump_to;
uint16_t length;
bool inclusive;
};
#define BP_KEY(x) (((struct GB_breakpoint_s){.addr = ((x).value), .bank = (x).has_bank? (x).bank : -1 }).key)
#define WATCHPOINT_READ (1)
#define WATCHPOINT_WRITE (2)
struct GB_watchpoint_s {
unsigned id;
union {
struct {
uint16_t addr;
uint16_t bank; /* -1 = any bank*/
};
uint32_t key; /* For sorting and comparing */
};
char *condition;
uint8_t flags;
uint16_t length;
bool inclusive;
};
#define WP_KEY(x) (((struct GB_watchpoint_s){.addr = ((x).value), .bank = (x).has_bank? (x).bank : -1 }).key)
static uint16_t bank_for_addr(GB_gameboy_t *gb, uint16_t addr)
{
if (addr < 0x4000) {
return gb->mbc_rom0_bank;
}
if (addr < 0x8000) {
return gb->mbc_rom_bank;
}
if (addr < 0xD000) {
return 0;
}
if (addr < 0xE000) {
return gb->cgb_ram_bank;
}
return 0;
}
typedef struct {
uint16_t rom0_bank;
uint16_t rom_bank;
uint8_t mbc_ram_bank;
bool mbc_ram_enable;
uint8_t ram_bank;
uint8_t vram_bank;
} banking_state_t;
static inline void save_banking_state(GB_gameboy_t *gb, banking_state_t *state)
{
state->rom0_bank = gb->mbc_rom0_bank;
state->rom_bank = gb->mbc_rom_bank;
state->mbc_ram_bank = gb->mbc_ram_bank;
state->mbc_ram_enable = gb->mbc_ram_enable;
state->ram_bank = gb->cgb_ram_bank;
state->vram_bank = gb->cgb_vram_bank;
}
static inline void restore_banking_state(GB_gameboy_t *gb, banking_state_t *state)
{
gb->mbc_rom0_bank = state->rom0_bank;
gb->mbc_rom_bank = state->rom_bank;
gb->mbc_ram_bank = state->mbc_ram_bank;
gb->mbc_ram_enable = state->mbc_ram_enable;
gb->cgb_ram_bank = state->ram_bank;
gb->cgb_vram_bank = state->vram_bank;
}
static inline void switch_banking_state(GB_gameboy_t *gb, uint16_t bank)
{
gb->mbc_rom0_bank = bank;
gb->mbc_rom_bank = bank;
gb->mbc_ram_bank = bank;
gb->mbc_ram_enable = true;
if (GB_is_cgb(gb)) {
gb->cgb_ram_bank = bank & 7;
gb->cgb_vram_bank = bank & 1;
if (gb->cgb_ram_bank == 0) {
gb->cgb_ram_bank = 1;
}
}
}
static const char *value_to_string(GB_gameboy_t *gb, uint16_t value, bool prefer_name, bool prefer_local)
{
static __thread char output[256];
const GB_bank_symbol_t *symbol = GB_debugger_find_symbol(gb, value, prefer_local);
if (symbol && (value - symbol->addr > 0x1000 || symbol->addr == 0) ) {
symbol = NULL;
}
if (!symbol) {
snprintf(output, sizeof(output), "$%04x", value);
}
else if (symbol->addr == value) {
if (prefer_name) {
snprintf(output, sizeof(output), "%s ($%04x)", symbol->name, value);
}
else {
snprintf(output, sizeof(output), "$%04x (%s)", value, symbol->name);
}
}
else {
if (prefer_name) {
snprintf(output, sizeof(output), "%s+$%03x ($%04x)", symbol->name, value - symbol->addr, value);
}
else {
snprintf(output, sizeof(output), "$%04x (%s+$%03x)", value, symbol->name, value - symbol->addr);
}
}
return output;
}
static GB_symbol_map_t *get_symbol_map(GB_gameboy_t *gb, uint16_t bank)
{
if (bank >= gb->n_symbol_maps) {
return NULL;
}
return gb->bank_symbols[bank];
}
static const char *debugger_value_to_string(GB_gameboy_t *gb, value_t value, bool prefer_name, bool prefer_local)
{
if (!value.has_bank) return value_to_string(gb, value.value, prefer_name, prefer_local);
static __thread char output[256];
const GB_bank_symbol_t *symbol = GB_map_find_symbol(get_symbol_map(gb, value.bank), value.value, prefer_local);
if (symbol && (value.value - symbol->addr > 0x1000 || symbol->addr == 0) ) {
symbol = NULL;
}
if (!symbol) {
snprintf(output, sizeof(output), "$%02x:$%04x", value.bank, value.value);
}
else if (symbol->addr == value.value) {
if (prefer_name) {
snprintf(output, sizeof(output), "%s ($%02x:$%04x)", symbol->name, value.bank, value.value);
}
else {
snprintf(output, sizeof(output), "$%02x:$%04x (%s)", value.bank, value.value, symbol->name);
}
}
else {
if (prefer_name) {
snprintf(output, sizeof(output), "%s+$%03x ($%02x:$%04x)", symbol->name, value.value - symbol->addr, value.bank, value.value);
}
else {
snprintf(output, sizeof(output), "$%02x:$%04x (%s+$%03x)", value.bank, value.value, symbol->name, value.value - symbol->addr);
}
}
return output;
}
static value_t read_lvalue(GB_gameboy_t *gb, lvalue_t lvalue)
{
/* Not used until we add support for operators like += */
switch (lvalue.kind) {
case LVALUE_MEMORY:
if (lvalue.memory_address.has_bank) {
banking_state_t state;
save_banking_state(gb, &state);
switch_banking_state(gb, lvalue.memory_address.bank);
value_t r = VALUE_16(GB_read_memory(gb, lvalue.memory_address.value));
restore_banking_state(gb, &state);
return r;
}
return VALUE_16(GB_read_memory(gb, lvalue.memory_address.value));
case LVALUE_MEMORY16:
if (lvalue.memory_address.has_bank) {
banking_state_t state;
save_banking_state(gb, &state);
switch_banking_state(gb, lvalue.memory_address.bank);
value_t r = VALUE_16(GB_read_memory(gb, lvalue.memory_address.value) |
(GB_read_memory(gb, lvalue.memory_address.value + 1) * 0x100));
restore_banking_state(gb, &state);
return r;
}
return VALUE_16(GB_read_memory(gb, lvalue.memory_address.value) |
(GB_read_memory(gb, lvalue.memory_address.value + 1) * 0x100));
case LVALUE_REG16:
return VALUE_16(*lvalue.register_address);
case LVALUE_REG_L:
return VALUE_16(*lvalue.register_address & 0x00FF);
case LVALUE_REG_H:
return VALUE_16(*lvalue.register_address >> 8);
}
return VALUE_16(0);
}
static void write_lvalue(GB_gameboy_t *gb, lvalue_t lvalue, uint16_t value)
{
switch (lvalue.kind) {
case LVALUE_MEMORY:
if (lvalue.memory_address.has_bank) {
banking_state_t state;
save_banking_state(gb, &state);
switch_banking_state(gb, lvalue.memory_address.bank);
GB_write_memory(gb, lvalue.memory_address.value, value);
restore_banking_state(gb, &state);
return;
}
GB_write_memory(gb, lvalue.memory_address.value, value);
return;
case LVALUE_MEMORY16:
if (lvalue.memory_address.has_bank) {
banking_state_t state;
save_banking_state(gb, &state);
switch_banking_state(gb, lvalue.memory_address.bank);
GB_write_memory(gb, lvalue.memory_address.value, value);
GB_write_memory(gb, lvalue.memory_address.value + 1, value >> 8);
restore_banking_state(gb, &state);
return;
}
GB_write_memory(gb, lvalue.memory_address.value, value);
GB_write_memory(gb, lvalue.memory_address.value + 1, value >> 8);
return;
case LVALUE_REG16:
*lvalue.register_address = value;
return;
case LVALUE_REG_L:
*lvalue.register_address &= 0xFF00;
*lvalue.register_address |= value & 0xFF;
return;
case LVALUE_REG_H:
*lvalue.register_address &= 0x00FF;
*lvalue.register_address |= value << 8;
return;
}
}
/* 16 bit value <op> 16 bit value = 16 bit value
25 bit address <op> 16 bit value = 25 bit address
16 bit value <op> 25 bit address = 25 bit address
25 bit address <op> 25 bit address = 16 bit value (since adding pointers, for examples, makes no sense)
Boolean operators always return a 16-bit value
*/
#define FIX_BANK(x) ((value_t){a.has_bank ^ b.has_bank, a.has_bank? a.bank : b.bank, (x)})
static value_t add(value_t a, value_t b) {return FIX_BANK(a.value + b.value);}
static value_t sub(value_t a, value_t b) {return FIX_BANK(a.value - b.value);}
static value_t mul(value_t a, value_t b) {return FIX_BANK(a.value * b.value);}
static value_t _div(value_t a, value_t b)
{
if (b.value == 0) {
return FIX_BANK(0);
}
return FIX_BANK(a.value / b.value);
};
static value_t mod(value_t a, value_t b)
{
if (b.value == 0) {
return FIX_BANK(0);
}
return FIX_BANK(a.value % b.value);
};
static value_t and(value_t a, value_t b) {return FIX_BANK(a.value & b.value);}
static value_t or(value_t a, value_t b) {return FIX_BANK(a.value | b.value);}
static value_t xor(value_t a, value_t b) {return FIX_BANK(a.value ^ b.value);}
static value_t shleft(value_t a, value_t b) {return FIX_BANK(a.value << b.value);}
static value_t shright(value_t a, value_t b) {return FIX_BANK(a.value >> b.value);}
static value_t assign(GB_gameboy_t *gb, lvalue_t a, uint16_t b)
{
write_lvalue(gb, a, b);
return read_lvalue(gb, a);
}
static value_t bool_and(value_t a, value_t b) {return VALUE_16(a.value && b.value);}
static value_t bool_or(value_t a, value_t b) {return VALUE_16(a.value || b.value);}
static value_t equals(value_t a, value_t b) {return VALUE_16(a.value == b.value);}
static value_t different(value_t a, value_t b) {return VALUE_16(a.value != b.value);}
static value_t lower(value_t a, value_t b) {return VALUE_16(a.value < b.value);}
static value_t greater(value_t a, value_t b) {return VALUE_16(a.value > b.value);}
static value_t lower_equals(value_t a, value_t b) {return VALUE_16(a.value <= b.value);}
static value_t greater_equals(value_t a, value_t b) {return VALUE_16(a.value >= b.value);}
static value_t bank(value_t a, value_t b) {return (value_t) {true, a.value, b.value};}
static struct {
const char *string;
int8_t priority;
value_t (*operator)(value_t, value_t);
value_t (*lvalue_operator)(GB_gameboy_t *, lvalue_t, uint16_t);
} operators[] =
{
// Yes. This is not C-like. But it makes much more sense.
// Deal with it.
{"+", 0, add},
{"-", 0, sub},
{"||", 0, bool_or},
{"|", 0, or},
{"*", 1, mul},
{"/", 1, _div},
{"%", 1, mod},
{"&&", 1, bool_and},
{"&", 1, and},
{"^", 1, xor},
{"<<", 2, shleft},
{"<=", 3, lower_equals},
{"<", 3, lower},
{">>", 2, shright},
{">=", 3, greater_equals},
{">", 3, greater},
{"==", 3, equals},
{"=", -1, NULL, assign},
{"!=", 3, different},
{":", 4, bank},
};
value_t debugger_evaluate(GB_gameboy_t *gb, const char *string,
size_t length, bool *error,
uint16_t *watchpoint_address, uint8_t *watchpoint_new_value);
static lvalue_t debugger_evaluate_lvalue(GB_gameboy_t *gb, const char *string,
size_t length, bool *error,
uint16_t *watchpoint_address, uint8_t *watchpoint_new_value)
{
*error = false;
// Strip whitespace
while (length && (string[0] == ' ' || string[0] == '\n' || string[0] == '\r' || string[0] == '\t')) {
string++;
length--;
}
while (length && (string[length-1] == ' ' || string[length-1] == '\n' || string[length-1] == '\r' || string[length-1] == '\t')) {
length--;
}
if (length == 0) {
GB_log(gb, "Expected expression.\n");
*error = true;
return (lvalue_t){0,};
}
if (string[0] == '(' && string[length - 1] == ')') {
// Attempt to strip parentheses
signed depth = 0;
for (unsigned i = 0; i < length; i++) {
if (string[i] == '(') depth++;
if (depth == 0) {
// First and last are not matching
depth = 1;
break;
}
if (string[i] == ')') depth--;
}
if (depth == 0) return debugger_evaluate_lvalue(gb, string + 1, length - 2, error, watchpoint_address, watchpoint_new_value);
}
else if (string[0] == '[' && string[length - 1] == ']') {
// Attempt to strip square parentheses (memory dereference)
signed depth = 0;
for (unsigned i = 0; i < length; i++) {
if (string[i] == '[') depth++;
if (depth == 0) {
// First and last are not matching
depth = 1;
break;
}
if (string[i] == ']') depth--;
}
if (depth == 0) {
return (lvalue_t){LVALUE_MEMORY, .memory_address = debugger_evaluate(gb, string + 1, length - 2, error, watchpoint_address, watchpoint_new_value)};
}
}
else if (string[0] == '{' && string[length - 1] == '}') {
// Attempt to strip curly parentheses (memory dereference)
signed depth = 0;
for (unsigned i = 0; i < length; i++) {
if (string[i] == '{') depth++;
if (depth == 0) {
// First and last are not matching
depth = 1;
break;
}
if (string[i] == '}') depth--;
}
if (depth == 0) {
return (lvalue_t){LVALUE_MEMORY16, .memory_address = debugger_evaluate(gb, string + 1, length - 2, error, watchpoint_address, watchpoint_new_value)};
}
}
// Registers
if (string[0] != '$' && (string[0] < '0' || string[0] > '9')) {
if (length == 1) {
switch (string[0]) {
case 'a': return (lvalue_t){LVALUE_REG_H, .register_address = &gb->af};
case 'f': return (lvalue_t){LVALUE_REG_L, .register_address = &gb->af};
case 'b': return (lvalue_t){LVALUE_REG_H, .register_address = &gb->bc};
case 'c': return (lvalue_t){LVALUE_REG_L, .register_address = &gb->bc};
case 'd': return (lvalue_t){LVALUE_REG_H, .register_address = &gb->de};
case 'e': return (lvalue_t){LVALUE_REG_L, .register_address = &gb->de};
case 'h': return (lvalue_t){LVALUE_REG_H, .register_address = &gb->hl};
case 'l': return (lvalue_t){LVALUE_REG_L, .register_address = &gb->hl};
}
}
else if (length == 2) {
switch (string[0]) {
case 'a': if (string[1] == 'f') return (lvalue_t){LVALUE_REG16, .register_address = &gb->af};
case 'b': if (string[1] == 'c') return (lvalue_t){LVALUE_REG16, .register_address = &gb->bc};
case 'd': if (string[1] == 'e') return (lvalue_t){LVALUE_REG16, .register_address = &gb->de};
case 'h': if (string[1] == 'l') return (lvalue_t){LVALUE_REG16, .register_address = &gb->hl};
case 's': if (string[1] == 'p') return (lvalue_t){LVALUE_REG16, .register_address = &gb->sp};
case 'p': if (string[1] == 'c') return (lvalue_t){LVALUE_REG16, .register_address = &gb->pc};
}
}
GB_log(gb, "Unknown register: %.*s\n", (unsigned) length, string);
*error = true;
return (lvalue_t){0,};
}
GB_log(gb, "Expression is not an lvalue: %.*s\n", (unsigned) length, string);
*error = true;
return (lvalue_t){0,};
}
#define ERROR ((value_t){0,})
value_t debugger_evaluate(GB_gameboy_t *gb, const char *string,
size_t length, bool *error,
uint16_t *watchpoint_address, uint8_t *watchpoint_new_value)
{
/* Disable watchpoints while evaluating expressions */
uint16_t n_watchpoints = gb->n_watchpoints;
gb->n_watchpoints = 0;
value_t ret = ERROR;
*error = false;
// Strip whitespace
while (length && (string[0] == ' ' || string[0] == '\n' || string[0] == '\r' || string[0] == '\t')) {
string++;
length--;
}
while (length && (string[length-1] == ' ' || string[length-1] == '\n' || string[length-1] == '\r' || string[length-1] == '\t')) {
length--;
}
if (length == 0) {
GB_log(gb, "Expected expression.\n");
*error = true;
goto exit;
}
if (string[0] == '(' && string[length - 1] == ')') {
// Attempt to strip parentheses
signed depth = 0;
for (unsigned i = 0; i < length; i++) {
if (string[i] == '(') depth++;
if (depth == 0) {
// First and last are not matching
depth = 1;
break;
}
if (string[i] == ')') depth--;
}
if (depth == 0) {
ret = debugger_evaluate(gb, string + 1, length - 2, error, watchpoint_address, watchpoint_new_value);
goto exit;
}
}
else if (string[0] == '[' && string[length - 1] == ']') {
// Attempt to strip square parentheses (memory dereference)
signed depth = 0;
for (unsigned i = 0; i < length; i++) {
if (string[i] == '[') depth++;
if (depth == 0) {
// First and last are not matching
depth = 1;
break;
}
if (string[i] == ']') depth--;
}
if (depth == 0) {
value_t addr = debugger_evaluate(gb, string + 1, length - 2, error, watchpoint_address, watchpoint_new_value);
banking_state_t state;
if (addr.bank) {
save_banking_state(gb, &state);
switch_banking_state(gb, addr.bank);
}
ret = VALUE_16(GB_read_memory(gb, addr.value));
if (addr.bank) {
restore_banking_state(gb, &state);
}
goto exit;
}
}
else if (string[0] == '{' && string[length - 1] == '}') {
// Attempt to strip curly parentheses (memory dereference)
signed depth = 0;
for (unsigned i = 0; i < length; i++) {
if (string[i] == '{') depth++;
if (depth == 0) {
// First and last are not matching
depth = 1;
break;
}
if (string[i] == '}') depth--;
}
if (depth == 0) {
value_t addr = debugger_evaluate(gb, string + 1, length - 2, error, watchpoint_address, watchpoint_new_value);
banking_state_t state;
if (addr.bank) {
save_banking_state(gb, &state);
switch_banking_state(gb, addr.bank);
}
ret = VALUE_16(GB_read_memory(gb, addr.value) | (GB_read_memory(gb, addr.value + 1) * 0x100));
if (addr.bank) {
restore_banking_state(gb, &state);
}
goto exit;
}
}
// Search for lowest priority operator
signed depth = 0;
unsigned operator_index = -1;
unsigned operator_pos = 0;
for (unsigned i = 0; i < length; i++) {
if (string[i] == '(') depth++;
else if (string[i] == ')') depth--;
else if (string[i] == '[') depth++;
else if (string[i] == ']') depth--;
else if (depth == 0) {
for (unsigned j = 0; j < sizeof(operators) / sizeof(operators[0]); j++) {
unsigned operator_length = strlen(operators[j].string);
if (operator_length > length - i) continue; // Operator too long
if (memcmp(string + i, operators[j].string, operator_length) == 0) {
if (operator_index != -1 && operators[operator_index].priority < operators[j].priority) {
/* for supporting = vs ==, etc*/
i += operator_length - 1;
break;
}
// Found an operator!
operator_pos = i;
operator_index = j;
/* for supporting = vs ==, etc*/
i += operator_length - 1;
break;
}
}
}
}
if (operator_index != -1) {
unsigned right_start = (unsigned)(operator_pos + strlen(operators[operator_index].string));
value_t right = debugger_evaluate(gb, string + right_start, length - right_start, error, watchpoint_address, watchpoint_new_value);
if (*error) goto exit;
if (operators[operator_index].lvalue_operator) {
lvalue_t left = debugger_evaluate_lvalue(gb, string, operator_pos, error, watchpoint_address, watchpoint_new_value);
if (*error) goto exit;
ret = operators[operator_index].lvalue_operator(gb, left, right.value);
goto exit;
}
value_t left = debugger_evaluate(gb, string, operator_pos, error, watchpoint_address, watchpoint_new_value);
if (*error) goto exit;
ret = operators[operator_index].operator(left, right);
goto exit;
}
// Not an expression - must be a register or a literal
// Registers
if (string[0] != '$' && (string[0] < '0' || string[0] > '9')) {
if (length == 1) {
switch (string[0]) {
case 'a': ret = VALUE_16(gb->af >> 8); goto exit;
case 'f': ret = VALUE_16(gb->af & 0xFF); goto exit;
case 'b': ret = VALUE_16(gb->bc >> 8); goto exit;
case 'c': ret = VALUE_16(gb->bc & 0xFF); goto exit;
case 'd': ret = VALUE_16(gb->de >> 8); goto exit;
case 'e': ret = VALUE_16(gb->de & 0xFF); goto exit;
case 'h': ret = VALUE_16(gb->hl >> 8); goto exit;
case 'l': ret = VALUE_16(gb->hl & 0xFF); goto exit;
}
}
else if (length == 2) {
switch (string[0]) {
case 'a': if (string[1] == 'f') {ret = VALUE_16(gb->af); goto exit;}
case 'b': if (string[1] == 'c') {ret = VALUE_16(gb->bc); goto exit;}
case 'd': if (string[1] == 'e') {ret = VALUE_16(gb->de); goto exit;}
case 'h': if (string[1] == 'l') {ret = VALUE_16(gb->hl); goto exit;}
case 's': if (string[1] == 'p') {ret = VALUE_16(gb->sp); goto exit;}
case 'p': if (string[1] == 'c') {ret = (value_t){true, bank_for_addr(gb, gb->pc), gb->pc}; goto exit;}
}
}
else if (length == 3) {
if (watchpoint_address && memcmp(string, "old", 3) == 0) {
ret = VALUE_16(GB_read_memory(gb, *watchpoint_address));
goto exit;
}
if (watchpoint_new_value && memcmp(string, "new", 3) == 0) {
ret = VALUE_16(*watchpoint_new_value);
goto exit;
}
/* $new is identical to $old in read conditions */
if (watchpoint_address && memcmp(string, "new", 3) == 0) {
ret = VALUE_16(GB_read_memory(gb, *watchpoint_address));
goto exit;
}
}
char symbol_name[length + 1];
memcpy(symbol_name, string, length);
symbol_name[length] = 0;
const GB_symbol_t *symbol = GB_reversed_map_find_symbol(&gb->reversed_symbol_map, symbol_name);
if (symbol) {
ret = (value_t){true, symbol->bank, symbol->addr};
goto exit;
}
GB_log(gb, "Unknown register or symbol: %.*s\n", (unsigned) length, string);
*error = true;
goto exit;
}
char *end;
unsigned base = 10;
if (string[0] == '$') {
string++;
base = 16;
length--;
}
uint16_t literal = (uint16_t) (strtol(string, &end, base));
if (end != string + length) {
GB_log(gb, "Failed to parse: %.*s\n", (unsigned) length, string);
*error = true;
goto exit;
}
ret = VALUE_16(literal);
exit:
gb->n_watchpoints = n_watchpoints;
return ret;
}
static void update_debug_active(GB_gameboy_t *gb)
{
gb->debug_active = !gb->debug_disable && (gb->debug_stopped || gb->debug_fin_command || gb->debug_next_command || gb->breakpoints);
}
struct debugger_command_s;
typedef bool debugger_command_imp_t(GB_gameboy_t *gb, char *arguments, char *modifiers, const struct debugger_command_s *command);
typedef char *debugger_completer_imp_t(GB_gameboy_t *gb, const char *string, uintptr_t *context);
typedef struct debugger_command_s {
const char *command;
uint8_t min_length;
debugger_command_imp_t *implementation;
const char *help_string; // Null if should not appear in help
const char *arguments_format; // For usage message
const char *modifiers_format; // For usage message
debugger_completer_imp_t *argument_completer;
debugger_completer_imp_t *modifiers_completer;
} debugger_command_t;
static const char *lstrip(const char *str)
{
while (*str == ' ' || *str == '\t') {
str++;
}
return str;
}
#define STOPPED_ONLY \
if (!gb->debug_stopped) { \
GB_log(gb, "Program is running, use 'interrupt' to stop execution.\n"); \
return false; \
}
#define NO_MODIFIERS \
if (modifiers) { \
print_usage(gb, command); \
return true; \
}
static void print_usage(GB_gameboy_t *gb, const debugger_command_t *command)
{
GB_log(gb, "Usage: %s", command->command);
if (command->modifiers_format) {
GB_log(gb, "[/%s]", command->modifiers_format);
}
if (command->arguments_format) {
GB_log(gb, " %s", command->arguments_format);
}
GB_log(gb, "\n");
}
static bool cont(GB_gameboy_t *gb, char *arguments, char *modifiers, const debugger_command_t *command)
{
NO_MODIFIERS
STOPPED_ONLY
if (strlen(lstrip(arguments))) {
print_usage(gb, command);
return true;
}
gb->debug_stopped = false;
return false;
}
static bool interrupt(GB_gameboy_t *gb, char *arguments, char *modifiers, const debugger_command_t *command)
{
NO_MODIFIERS
if (strlen(lstrip(arguments))) {
print_usage(gb, command);
return true;
}
if (gb->debug_stopped) {
GB_log(gb, "Program already stopped.\n");
return true;
}
GB_debugger_break(gb);
return true;
}
static char *reset_completer(GB_gameboy_t *gb, const char *string, uintptr_t *context)
{
size_t length = strlen(string);
const char *suggestions[] = {"quick", "reload"};
while (*context < sizeof(suggestions) / sizeof(suggestions[0])) {
if (strncmp(string, suggestions[*context], length) == 0) {
return strdup(suggestions[(*context)++] + length);
}
(*context)++;
}
return NULL;
}
static bool reset(GB_gameboy_t *gb, char *arguments, char *modifiers, const debugger_command_t *command)
{
NO_MODIFIERS
const char *stripped_argument = lstrip(arguments);
if (stripped_argument[0] == 0) {
GB_reset(gb);
if (gb->debug_stopped) {
GB_cpu_disassemble(gb, gb->pc, 5);
}
return true;
}
if (strcmp(stripped_argument, "quick") == 0) {
GB_quick_reset(gb);
if (gb->debug_stopped) {
GB_cpu_disassemble(gb, gb->pc, 5);
}
return true;
}
if (strcmp(stripped_argument, "reload") == 0) {
if (gb->debugger_reload_callback) {
gb->debugger_reload_callback(gb);
if (gb->undo_state) {
free(gb->undo_state);
gb->undo_state = NULL;
}
if (gb->debug_stopped) {
GB_cpu_disassemble(gb, gb->pc, 5);
}
return true;
}
GB_log(gb, "ROM reloading via the debugger is not supported in this frontend.\n");
return true;
}
print_usage(gb, command);
return true;
}
static bool next(GB_gameboy_t *gb, char *arguments, char *modifiers, const debugger_command_t *command)
{
NO_MODIFIERS
STOPPED_ONLY
if (strlen(lstrip(arguments))) {
print_usage(gb, command);
return true;
}
gb->debug_stopped = false;
gb->debug_next_command = true;
gb->debug_call_depth = 0;
return false;
}
static bool step(GB_gameboy_t *gb, char *arguments, char *modifiers, const debugger_command_t *command)
{
NO_MODIFIERS
STOPPED_ONLY
if (strlen(lstrip(arguments))) {
print_usage(gb, command);
return true;
}
return false;
}
static bool finish(GB_gameboy_t *gb, char *arguments, char *modifiers, const debugger_command_t *command)
{
NO_MODIFIERS
STOPPED_ONLY
if (strlen(lstrip(arguments))) {
print_usage(gb, command);
return true;
}
gb->debug_stopped = false;
gb->debug_fin_command = true;
gb->debug_call_depth = 0;
return false;
}
static bool registers(GB_gameboy_t *gb, char *arguments, char *modifiers, const debugger_command_t *command)
{
NO_MODIFIERS
if (strlen(lstrip(arguments))) {
print_usage(gb, command);
return true;
}
GB_log(gb, "AF = $%04x (%c%c%c%c)\n", gb->af, /* AF can't really be an address */
(gb->f & GB_CARRY_FLAG)? 'C' : '-',
(gb->f & GB_HALF_CARRY_FLAG)? 'H' : '-',
(gb->f & GB_SUBTRACT_FLAG)? 'N' : '-',
(gb->f & GB_ZERO_FLAG)? 'Z' : '-');
GB_log(gb, "BC = %s\n", value_to_string(gb, gb->bc, false, false));
GB_log(gb, "DE = %s\n", value_to_string(gb, gb->de, false, false));
GB_log(gb, "HL = %s\n", value_to_string(gb, gb->hl, false, false));
GB_log(gb, "SP = %s\n", value_to_string(gb, gb->sp, false, false));
GB_log(gb, "PC = %s\n", value_to_string(gb, gb->pc, false, false));
GB_log(gb, "IME = %s\n", gb->ime? "Enabled" : "Disabled");
return true;
}
static char *on_off_completer(GB_gameboy_t *gb, const char *string, uintptr_t *context)
{
size_t length = strlen(string);
const char *suggestions[] = {"on", "off"};
while (*context < sizeof(suggestions) / sizeof(suggestions[0])) {
if (strncmp(string, suggestions[*context], length) == 0) {
return strdup(suggestions[(*context)++] + length);
}
(*context)++;
}
return NULL;
}
/* Enable or disable software breakpoints */
static bool softbreak(GB_gameboy_t *gb, char *arguments, char *modifiers, const debugger_command_t *command)
{
NO_MODIFIERS
if (strcmp(lstrip(arguments), "on") == 0 || !strlen(lstrip(arguments))) {
gb->has_software_breakpoints = true;
}
else if (strcmp(lstrip(arguments), "off") == 0) {
gb->has_software_breakpoints = false;
}
else {
print_usage(gb, command);
}
return true;
}
static inline bool is_legal_symbol_char(char c)
{
if (c >= '0' && c <= '9') return true;
if (c >= 'A' && c <= 'Z') return true;
if (c >= 'a' && c <= 'z') return true;
if (c == '_') return true;
if (c == '.') return true;
return false;
}
static char *symbol_completer(GB_gameboy_t *gb, const char *string, uintptr_t *_context)
{
const char *symbol_prefix = string;
while (*string) {
if (!is_legal_symbol_char(*string)) {
symbol_prefix = string + 1;
}
string++;
}
if (*symbol_prefix == '$') {
return NULL;
}
struct {
uint16_t bank;
uint32_t symbol;
} *context = (void *)_context;
size_t length = strlen(symbol_prefix);
while (context->bank < 0x200) {
GB_symbol_map_t *map = get_symbol_map(gb, context->bank);
if (map == NULL ||
context->symbol >= map->n_symbols) {
context->bank++;
context->symbol = 0;
continue;
}
const char *candidate = map->symbols[context->symbol++].name;
if (strncmp(symbol_prefix, candidate, length) == 0) {
return strdup(candidate + length);
}
}
return NULL;
}
static char *j_completer(GB_gameboy_t *gb, const char *string, uintptr_t *context)
{
size_t length = strlen(string);
const char *suggestions[] = {"j"};
while (*context < sizeof(suggestions) / sizeof(suggestions[0])) {
if (strncmp(string, suggestions[*context], length) == 0) {
return strdup(suggestions[(*context)++] + length);
}
(*context)++;
}
return NULL;
}
static bool check_inclusive(char *to)
{
size_t length = strlen(to);
while (length > strlen("inclusive")) {
if (to[length - 1] == ' ') {
to[length - 1] = 0;
length--;