-
Notifications
You must be signed in to change notification settings - Fork 0
/
debug.c
executable file
·1711 lines (1585 loc) · 39.2 KB
/
debug.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
/*
* UAE - The Un*x Amiga Emulator
*
* Debugger
*
* (c) 1995 Bernd Schmidt
*
*/
#include "sysconfig.h"
#include "sysdeps.h"
#include <ctype.h>
#include <signal.h>
#include "options.h"
#include "uae.h"
#include "memory.h"
#include "custom.h"
#include "newcpu.h"
#include "cpu_prefetch.h"
#include "debug.h"
#include "cia.h"
#include "xwin.h"
#include "identify.h"
#include "gensound.h"
#include "disk.h"
#include "savestate.h"
#include "autoconf.h"
//#include "akiko.h"
#include "inputdevice.h"
#ifdef DEBUGGER
static int debugger_active;
static uaecptr skipaddr_start, skipaddr_end;
static int skipaddr_doskip;
static uae_u32 skipins;
static int do_skip;
#ifdef SAVESTATE
static int debug_rewind;
#endif
static int memwatch_enabled, memwatch_triggered;
int debugging;
int exception_debugging;
int debug_copper;
int debug_sprite_mask = 0xff;
static uaecptr debug_copper_pc;
extern int audio_channel_mask;
static FILE *logfile;
#ifndef _WIN32
#define console_out printf
#define console_flush() fflush( stdout )
#define console_get( input, len ) fgets( input, len, stdin )
#endif
#define MAX_HIST 100
unsigned int firsthist = 0;
unsigned int lasthist = 0;
static struct regstruct history[MAX_HIST];
void activate_debugger (void)
{
if (debuggable ()) {
if (is_fullscreen ()) {
toggle_fullscreen ();
if (is_fullscreen ()) {
write_log ("Cannot activate debugger in full-screen mode\n");
return;
}
}
if (logfile)
fclose (logfile);
logfile = 0;
do_skip = 0;
if (debugger_active)
return;
debugger_active = 1;
set_special (®s, SPCFLAG_BRK);
debugging = 1;
}
}
static const char help[] = {
" HELP for UAE Debugger\n"
" -----------------------\n\n"
" g [<address>] Start execution at the current address or <address>\n"
" c Dump state of the CIA, disk drives and custom registers\n"
" r Dump state of the CPU\n"
" m <address> [<lines>] Memory dump starting at <address>\n"
" d <address> [<lines>] Disassembly starting at <address>\n"
" t [instructions] Step one or more instructions\n"
" z Step through one instruction - useful for JSR, DBRA etc\n"
" f Step forward until PC in RAM (\"boot block finder\")\n"
" f <address> Add/remove breakpoint\n"
" fi Step forward until PC points to RTS/RTD or RTE\n"
" fi <opcode> Step forward until PC points to <opcode>\n"
" fl List breakpoints\n"
" fd Remove all breakpoints\n"
" f <addr1> <addr2> Step forward until <addr1> <= PC <= <addr2>\n"
" e [<addr>] Dump contents of all custom registers\n"
" i [<addr>] Dump contents of interrupt and trap vectors\n"
" o <0-2|addr> [<lines>]View memory as Copper instructions\n"
" od Enable/disable Copper vpos/hpos tracing\n"
" ot Copper single step trace\n"
" ob <addr> Copper breakpoint\n"
" O Display bitplane offsets\n"
" O <plane> <offset> Offset a bitplane\n"
" H[H] <cnt> Show PC history (HH=full CPU info) <cnt> instructions\n"
" C <value> Search for values like energy or lifes in games\n"
" W <address> <value> Write into Amiga memory\n"
" w <num> <address> <length> <R/W/RW> [<value>]\n"
" Add/remove memory watchpoints\n"
" wd Enable illegal access logger\n"
" S <file> <addr> <n> Save a block of Amiga memory\n"
" s <string>/<values> [<addr>] [<length>]\n"
" Search for string/bytes\n"
" T Show exec tasks and their PCs\n"
" h,? Show this help page\n"
#ifdef SAVESTATE
" b Step to previous state capture position\n"
#endif
" am <channel mask> Enable or disable audio channels\n"
" sm <sprite mask> Enable or disable sprites\n"
" di <mode> [<track>] Break on disk access. R=DMA read,W=write,RW=both,P=PIO\n"
" Also enables extended disk logging\n"
" q Quit the emulator. You don't want to use this command.\n\n"
};
static void debug_help (void)
{
console_out (help);
}
static void ignore_ws (const char **c)
{
while (**c && isspace (**c))
(*c)++;
}
static uae_u32 readint (const char **c);
static uae_u32 readhex (const char **c)
{
uae_u32 val = 0;
char nc;
ignore_ws (c);
if (**c == '!' || **c == '_') {
(*c)++;
return readint (c);
}
while (isxdigit (nc = **c)) {
(*c)++;
val *= 16;
nc = toupper (nc);
if (isdigit (nc)) {
val += nc - '0';
} else {
val += nc - 'A' + 10;
}
}
return val;
}
static uae_u32 readint (const char **c)
{
uae_u32 val = 0;
char nc;
int sign = 1;
ignore_ws (c);
if (**c == '$') {
(*c)++;
return readhex (c);
}
if (**c == '0' && toupper ((*c)[1]) == 'X') {
(*c) += 2;
return readhex (c);
}
if (**c == '-') {
sign = 1;
(*c)++;
}
while (isdigit (nc = **c)) {
(*c)++;
val *= 10;
val += nc - '0';
}
return val * sign;
}
static char next_char (const char **c)
{
ignore_ws (c);
return *(*c)++;
}
static char peek_next_char (const char **c)
{
const char *pc = *c;
return pc[1];
}
static int more_params (const char **c)
{
ignore_ws (c);
return (**c) != 0;
}
static int next_string (const char **c, char *out, unsigned int max, int forceupper)
{
char *p = out;
*p = 0;
while (**c != 0) {
if (**c == 32) {
ignore_ws (c);
return strlen (out);
}
*p = next_char (c);
if (forceupper)
*p = toupper (*p);
*++p = 0;
max--;
if (max <= 1)
break;
}
return strlen (out);
}
static uae_u32 nextaddr (uae_u32 addr)
{
if (addr == 0xffffffff) {
if (currprefs.bogomem_size)
return 0xc00000 + currprefs.bogomem_size;
if (currprefs.fastmem_size)
return 0x200000 + currprefs.fastmem_size;
return currprefs.chipmem_size;
}
if (addr == currprefs.chipmem_size) {
if (currprefs.fastmem_size)
return 0x200000;
else if (currprefs.bogomem_size)
return 0xc00000;
return 0xffffffff;
}
if (addr == 0x200000 + currprefs.fastmem_size) {
if (currprefs.bogomem_size)
return 0xc00000;
return 0xffffffff;
}
if (addr == 0xc00000 + currprefs.bogomem_size)
return 0xffffffff;
return addr + 1;
}
static void dumpmem (uaecptr addr, uaecptr *nxmem, unsigned int lines)
{
char line[80];
int cols = 8;
for (;lines--;) {
int i;
sprintf (line, "%08x ", addr);
for (i = 0; i < cols; i++) {
uae_u8 b1 = get_byte (addr + 0);
uae_u8 b2 = get_byte (addr + 1);
addr += 2;
sprintf (line + 9 + i * 5, "%02x%02x ", b1, b2);
line[9 + cols * 5 + 1 + i * 2 + 0] = b1 >= 32 && b1 < 127 ? b1 : '.';
line[9 + cols * 5 + 1 + i * 2 + 1] = b2 >= 32 && b2 < 127 ? b2 : '.';
}
line[9 + cols * 5] = ' ';
line[9 + cols * 5 + 1 + 2 * cols] = 0;
console_out ("%s", line);
console_out ("\n");
}
*nxmem = addr;
}
static void dump_custom_regs (void)
{
unsigned int len;
unsigned int i, j, end;
uae_u8 *p1, *p2, *p3, *p4;
p1 = p2 = save_custom (&len, 0, 1);
p1 += 4; // skip chipset type
for (i = 0; i < 4; i++) {
p4 = p1 + 0xa0 + i * 16;
p3 = save_audio (i, &len, 0);
p4[0] = p3[12];
p4[1] = p3[13];
p4[2] = p3[14];
p4[3] = p3[15];
p4[4] = p3[4];
p4[5] = p3[5];
p4[6] = p3[8];
p4[7] = p3[9];
p4[8] = 0;
p4[9] = p3[1];
p4[10] = p3[10];
p4[11] = p3[11];
free (p3);
}
end = 0;
while (custd[end].name)
end++;
end++;
end /= 2;
for (i = 0; i < end; i++) {
uae_u16 v1, v2;
int addr1, addr2;
j = end + i;
addr1 = custd[i].adr & 0x1ff;
addr2 = custd[j].adr & 0x1ff;
v1 = (p1[addr1 + 0] << 8) | p1[addr1 + 1];
v2 = (p1[addr2 + 0] << 8) | p1[addr2 + 1];
console_out ("%3.3X %s\t%4.4X\t%3.3X %s\t%4.4X\n",
addr1, custd[i].name, v1,
addr2, custd[j].name, v2);
}
free (p2);
}
static void dump_vectors (uaecptr addr)
{
unsigned int i = 0, j = 0;
if (addr == 0xffffffff)
addr = regs.vbr;
while (int_labels[i].name || trap_labels[j].name) {
if (int_labels[i].name) {
console_out ("$%08X: %s \t $%08X\t", int_labels[i].adr + addr,
int_labels[i].name, get_long (int_labels[i].adr + (int_labels[i].adr == 4 ? 0 : addr)));
i++;
} else {
console_out ("\t\t\t\t");
}
if (trap_labels[j].name) {
console_out ("$%08X: %s \t $%08X", trap_labels[j].adr + addr,
trap_labels[j].name, get_long (trap_labels[j].adr + addr));
j++;
}
console_out ("\n");
}
}
static void disassemble_wait (uae_u32 insn)
{
unsigned int vp, hp, ve, he, bfd, v_mask, h_mask;
vp = (insn & 0xff000000) >> 24;
hp = (insn & 0x00fe0000) >> 16;
ve = (insn & 0x00007f00) >> 8;
he = (insn & 0x000000fe);
bfd = (insn & 0x00008000) >> 15;
/* bit15 can never be masked out*/
v_mask = vp & (ve | 0x80);
h_mask = hp & he;
if (v_mask > 0) {
console_out ("vpos ");
if (ve != 0x7f) {
console_out ("& 0x%02x ", ve);
}
console_out (">= 0x%02x", v_mask);
}
if (he > 0) {
if (v_mask > 0) {
console_out (" and");
}
console_out (" hpos ");
if (he != 0xfe) {
console_out ("& 0x%02x ", he);
}
console_out (">= 0x%02x", h_mask);
} else {
console_out (", ignore horizontal");
}
console_out (".\n \t; VP %02x, VE %02x; HP %02x, HE %02x; BFD %d\n",
vp, ve, hp, he, bfd);
}
#define NR_COPPER_RECORDS 40000
/* Record copper activity for the debugger. */
struct cop_record
{
unsigned int hpos, vpos;
uaecptr addr;
};
static struct cop_record *cop_record[2];
static unsigned int nr_cop_records[2], curr_cop_set;
void record_copper_reset (void)
{
/* Start a new set of copper records. */
curr_cop_set ^= 1;
nr_cop_records[curr_cop_set] = 0;
}
void record_copper (uaecptr addr, unsigned int hpos, unsigned int vpos)
{
unsigned int t = nr_cop_records[curr_cop_set];
if (!cop_record[0]) {
cop_record[0] = malloc (NR_COPPER_RECORDS * sizeof (struct cop_record));
cop_record[1] = malloc (NR_COPPER_RECORDS * sizeof (struct cop_record));
}
if (t < NR_COPPER_RECORDS) {
cop_record[curr_cop_set][t].addr = addr;
cop_record[curr_cop_set][t].hpos = hpos;
cop_record[curr_cop_set][t].vpos = vpos;
nr_cop_records[curr_cop_set] = t + 1;
}
if (debug_copper & 2) { /* trace */
debug_copper &= ~2;
activate_debugger();
}
if ((debug_copper & 4) && addr >= debug_copper_pc && addr <= debug_copper_pc + 3) {
debug_copper &= ~4;
activate_debugger();
}
}
static int find_copper_record (uaecptr addr, unsigned int *phpos, unsigned int *pvpos)
{
unsigned int s = curr_cop_set ^ 1;
unsigned int t = nr_cop_records[s];
unsigned int i;
for (i = 0; i < t; i++) {
if (cop_record[s][i].addr == addr) {
*phpos = cop_record[s][i].hpos;
*pvpos = cop_record[s][i].vpos;
return 1;
}
}
return 0;
}
/* simple decode copper by Mark Cox */
static void decode_copper_insn (uae_u32 insn, uaecptr addr)
{
uae_u32 insn_type = insn & 0x00010001;
unsigned int hpos, vpos;
char here = ' ';
char record[] = " ";
if (find_copper_record (addr, &hpos, &vpos)) {
sprintf (record, " [%03x %03x]", vpos, hpos);
}
if (get_copper_address(-1) >= addr && get_copper_address(-1) <= addr + 3)
here = '*';
console_out ("%c%08x: %04x %04x%s\t; ", here, addr, insn >> 16, insn & 0xFFFF, record);
switch (insn_type) {
case 0x00010000: /* WAIT insn */
console_out ("Wait for ");
disassemble_wait (insn);
if (insn == 0xfffffffe)
console_out (" \t; End of Copperlist\n");
break;
case 0x00010001: /* SKIP insn */
console_out ("Skip if ");
disassemble_wait (insn);
break;
case 0x00000000:
case 0x00000001: /* MOVE insn */
{
uae_u32 addr = (insn >> 16) & 0x1fe;
int i = 0;
while (custd[i].name) {
if (custd[i].adr == addr + 0xdff000)
break;
i++;
}
if (custd[i].name)
console_out ("%s := 0x%04x\n", custd[i].name, insn & 0xffff);
else
console_out ("%04x := 0x%04x\n", addr, insn & 0xffff);
}
break;
default:
abort ();
}
}
static uaecptr decode_copperlist (uaecptr address, unsigned int nolines)
{
uae_u32 insn;
while (nolines-- > 0) {
insn = get_long (address);
decode_copper_insn (insn, address);
address += 4;
}
return address;
/* You may wonder why I don't stop this at the end of the copperlist?
* Well, often nice things are hidden at the end and it is debatable the actual
* values that mean the end of the copperlist */
}
static int copper_debugger (const char **c)
{
static uaecptr nxcopper;
uae_u32 maddr;
unsigned int lines;
if (**c == 'd') {
next_char (c);
if (debug_copper)
debug_copper = 0;
else
debug_copper = 1;
console_out ("Copper debugger %s.\n", debug_copper ? "enabled" : "disabled");
} else if (**c == 't') {
debug_copper = 1|2;
return 1;
} else if (**c == 'b') {
(*c)++;
debug_copper = 1|4;
if (more_params (c)) {
debug_copper_pc = readhex (c);
console_out ("Copper breakpoint @0x%8.8x\n", debug_copper_pc);
} else {
debug_copper &= ~4;
}
} else {
if (more_params (c)) {
maddr = readhex (c);
if (maddr == 1 || maddr == 2)
maddr = get_copper_address (maddr);
else if (maddr == 0)
maddr = get_copper_address (-1);
} else
maddr = nxcopper;
if (more_params (c))
lines = readhex (c);
else
lines = 20;
nxcopper = decode_copperlist (maddr, lines);
}
return 0;
}
/* cheat-search by Holger Jakob */
static void cheatsearch (const char **c)
{
uae_u8 *p = get_real_address (0);
static uae_u32 *vlist = NULL;
uae_u32 ptr;
uae_u32 val = 0;
uae_u32 type = 0; /* not yet */
uae_u32 count = 0;
uae_u32 fcount = 0;
uae_u32 full = 0;
ignore_ws (c);
val = readhex (c);
if (vlist == NULL) {
vlist = malloc (256 * 4);
if (vlist != 0) {
for (count = 0; count < 255; count++)
vlist[count] = 0;
count = 0;
for (ptr = 0; ptr < allocated_chipmem - 40; ptr += 2, p += 2) {
if (ptr >= 0x438 && p[3] == (val & 0xff)
&& p[2] == (val >> 8 & 0xff)
&& p[1] == (val >> 16 & 0xff)
&& p[0] == (val >> 24 & 0xff))
{
if (count < 255) {
vlist[count++]=ptr;
console_out ("%08x: %x%x%x%x\n",ptr,p[0],p[1],p[2],p[3]);
} else
full = 1;
}
}
console_out ("Found %d possible addresses with %d\n",count,val);
console_out ("Now continue with 'g' and use 'C' with a different value\n");
}
} else {
for (count = 0; count<255; count++) {
if (p[vlist[count]+3] == (val & 0xff)
&& p[vlist[count]+2] == (val>>8 & 0xff)
&& p[vlist[count]+1] == (val>>16 & 0xff)
&& p[vlist[count]] == (val>>24 & 0xff))
{
fcount++;
console_out ("%08x: %x%x%x%x\n", vlist[count], p[vlist[count]],
p[vlist[count]+1], p[vlist[count]+2], p[vlist[count]+3]);
}
}
console_out ("%d hits of %d found\n",fcount,val);
free (vlist);
vlist = NULL;
}
}
#define BREAKPOINT_TOTAL 8
struct breakpoint_node {
uaecptr addr;
int enabled;
};
static struct breakpoint_node bpnodes[BREAKPOINT_TOTAL];
static addrbank **debug_mem_banks;
#define MEMWATCH_TOTAL 4
struct memwatch_node {
uaecptr addr;
unsigned int size;
int rw;
uae_u32 val;
int val_enabled;
uae_u32 modval;
int modval_written;
};
static struct memwatch_node mwnodes[MEMWATCH_TOTAL];
static struct memwatch_node mwhit;
static uae_u8 *illgdebug;
static int illgdebug_break;
extern int cdtv_enabled, cd32_enabled;
static void illg_init (void)
{
unsigned int i;
free (illgdebug);
illgdebug = xmalloc (0x1000000);
if (!illgdebug)
return;
memset (illgdebug, 3, 0x1000000);
memset (illgdebug, 0, currprefs.chipmem_size);
memset (illgdebug + 0xc00000, 0, currprefs.bogomem_size);
memset (illgdebug + 0x200000, 0, currprefs.fastmem_size);
i = 0;
while (custd[i].name) {
int rw = custd[i].rw;
illgdebug[custd[i].adr] = rw;
illgdebug[custd[i].adr + 1] = rw;
i++;
}
for (i = 0; i < 16; i++) { /* CIAs */
if (i == 11)
continue;
illgdebug[0xbfe001 + i * 0x100] = 0;
illgdebug[0xbfd000 + i * 0x100] = 0;
}
memset (illgdebug + 0xf80000, 1, 512 * 1024); /* KS ROM */
memset (illgdebug + 0xdc0000, 0, 0x3f); /* clock */
#ifdef CDTV
if (cdtv_enabled) {
memset (illgdebug + 0xf00000, 1, 256 * 1024); /* CDTV ext ROM */
memset (illgdebug + 0xdc8000, 0, 4096); /* CDTV batt RAM */
}
#endif
/*
#ifdef CD32
if (cd32_enabled) {
memset (illgdebug + AKIKO_BASE, 0, AKIKO_BASE_END - AKIKO_BASE);
memset (illgdebug + 0xe00000, 1, 512 * 1024); // CD32 ext ROM
}
#endif
*/
if (cloanto_rom)
memset (illgdebug + 0xe00000, 1, 512 * 1024);
#ifdef FILESYS
if (nr_units (currprefs.mountinfo) > 0) /* filesys "rom" */
memset (illgdebug + RTAREA_BASE, 1, 0x10000);
#endif
}
/* add special custom register check here */
static void illg_debug_check (uaecptr addr, int rw, int size, uae_u32 val)
{
return;
}
static void illg_debug_do (uaecptr addr, int rw, int size, uae_u32 val)
{
uae_u8 mask;
uae_u32 pc = m68k_getpc (®s);
char rws = rw ? 'W' : 'R';
int i;
for (i = size - 1; i >= 0; i--) {
uae_u8 v = val >> (i * 8);
uae_u32 ad = addr + i;
if (ad >= 0x1000000)
mask = 3;
else
mask = illgdebug[ad];
if (!mask)
continue;
if (mask & 0x80) {
illg_debug_check (ad, rw, size, val);
} else if ((mask & 3) == 3) {
if (rw)
write_log ("RW: %8.8X=%2.2X %c PC=%8.8X\n", ad, v, rws, pc);
else
write_log ("RW: %8.8X %c PC=%8.8X\n", ad, rws, pc);
if (illgdebug_break)
activate_debugger ();
} else if ((mask & 1) && rw) {
write_log ("RO: %8.8X=%2.2X %c PC=%8.8X\n", ad, v, rws, pc);
if (illgdebug_break)
activate_debugger ();
} else if ((mask & 2) && !rw) {
write_log ("WO: %8.8X %c PC=%8.8X\n", ad, rws, pc);
if (illgdebug_break)
activate_debugger ();
}
}
}
STATIC_INLINE uae_u8 debug_mem_off (uaecptr addr)
{
return (munge24 (addr) >> 16) & 0xff;
}
static void memwatch_func (uaecptr addr, int rw, int size, uae_u32 val)
{
int i, brk;
if (illgdebug)
illg_debug_do (addr, rw, size, val);
addr = munge24 (addr);
for (i = 0; i < MEMWATCH_TOTAL; i++) {
uaecptr addr2 = mwnodes[i].addr;
uaecptr addr3 = addr2 + mwnodes[i].size;
int rw2 = mwnodes[i].rw;
brk = 0;
if (mwnodes[i].size == 0)
continue;
if (mwnodes[i].val_enabled && mwnodes[i].val != val)
continue;
if (rw != rw2 && rw2 < 2)
continue;
if (addr >= addr2 && addr < addr3)
brk = 1;
if (!brk && size == 2 && (addr + 1 >= addr2 && addr + 1 < addr3))
brk = 1;
if (!brk && size == 4 && ((addr + 2 >= addr2 && addr + 2 < addr3) || (addr + 3 >= addr2 && addr + 3 < addr3)))
brk = 1;
if (brk && mwnodes[i].modval_written) {
if (!rw) {
brk = 0;
} else if (mwnodes[i].modval_written == 1) {
mwnodes[i].modval_written = 2;
mwnodes[i].modval = val;
brk = 0;
} else if (mwnodes[i].modval == val) {
brk = 0;
}
}
if (brk) {
mwhit.addr = addr;
mwhit.rw = rw;
mwhit.size = size;
mwhit.val = 0;
if (mwhit.rw)
mwhit.val = val;
memwatch_triggered = i + 1;
debugging = 1;
set_special (®s, SPCFLAG_BRK);
break;
}
}
}
static REGPARAM2 uae_u32 debug_lget (uaecptr addr)
{
uae_u8 off = debug_mem_off (addr);
uae_u32 v;
v = debug_mem_banks[off]->lget(addr);
memwatch_func (addr, 0, 4, v);
return v;
}
static REGPARAM2 uae_u32 debug_wget (uaecptr addr)
{
uae_u8 off = debug_mem_off (addr);
uae_u32 v;
v = debug_mem_banks[off]->wget(addr);
memwatch_func (addr, 0, 2, v);
return v;
}
static REGPARAM2 uae_u32 debug_bget (uaecptr addr)
{
uae_u8 off = debug_mem_off (addr);
uae_u32 v;
v = debug_mem_banks[off]->bget(addr);
memwatch_func (addr, 0, 1, v);
return v;
}
static REGPARAM2 void debug_lput (uaecptr addr, uae_u32 v)
{
uae_u8 off = debug_mem_off (addr);
memwatch_func (addr, 1, 4, v);
debug_mem_banks[off]->lput(addr, v);
}
static REGPARAM2 void debug_wput (uaecptr addr, uae_u32 v)
{
uae_u8 off = debug_mem_off (addr);
memwatch_func (addr, 1, 2, v);
debug_mem_banks[off]->wput(addr, v);
}
static REGPARAM2 void debug_bput (uaecptr addr, uae_u32 v)
{
uae_u8 off = debug_mem_off (addr);
memwatch_func (addr, 1, 1, v);
debug_mem_banks[off]->bput(addr, v);
}
static REGPARAM2 int debug_check (uaecptr addr, uae_u32 size)
{
return debug_mem_banks[munge24 (addr) >> 16]->check (addr, size);
}
static REGPARAM2 uae_u8 *debug_xlate (uaecptr addr)
{
return debug_mem_banks[munge24 (addr) >> 16]->xlateaddr (addr);
}
static void deinitialize_memwatch (void)
{
unsigned int i;
if (!memwatch_enabled)
return;
for (i = 0; i < 256; i++) {
addrbank *a1 = debug_mem_banks[i];
addrbank *a2 = mem_banks[i];
memcpy (a2, a1, sizeof (addrbank));
free (a1);
}
free (debug_mem_banks);
debug_mem_banks = 0;
memwatch_enabled = 0;
free (illgdebug);
illgdebug = 0;
}
static int initialize_memwatch (void)
{
unsigned int i;
addrbank *a1, *a2;
if (!currprefs.address_space_24)
return 0;
debug_mem_banks = xmalloc (sizeof (addrbank*) * 256);
for (i = 0; i < 256; i++) {
a1 = debug_mem_banks[i] = xmalloc (sizeof (addrbank));
a2 = mem_banks[i];
memcpy (a1, a2, sizeof (addrbank));
}
for (i = 0; i < 256; i++) {
a2 = mem_banks[i];
a2->bget = debug_bget;
a2->wget = debug_wget;
a2->lget = debug_lget;
a2->bput = debug_bput;
a2->wput = debug_wput;
a2->lput = debug_lput;
a2->check = debug_check;
a2->xlateaddr = debug_xlate;
}
memwatch_enabled = 1;
return 1;
}
static void memwatch_dump (int num)
{
unsigned int i;
const struct memwatch_node *mwn;
for (i = 0; i < MEMWATCH_TOTAL; i++) {
if ((num >= 0 && num == (int)i) || (num < 0)) {
mwn = &mwnodes[i];
if (mwn->size == 0)
continue;
console_out ("%d: %8.8X - %8.8X (%d) %s",
i, mwn->addr, mwn->addr + (mwn->size - 1), mwn->size,
mwn->rw == 0 ? "R" : (mwn->rw == 1 ? "W" : "RW"));
if (mwn->val_enabled)
console_out (" =%X", mwn->val);
if (mwn->modval_written)
console_out (" =M");
console_out ("\n");
}
}
}
static void memwatch (const char **c)
{
int num;
struct memwatch_node *mwn;
char nc;
if (!memwatch_enabled) {
if (!initialize_memwatch ()) {
console_out ("Memwatch breakpoints require 24-bit address space\n");
return;
}
console_out ("Memwatch breakpoints enabled\n");
}
ignore_ws (c);
if (!more_params (c)) {
memwatch_dump (-1);
return;
}
nc = next_char (c);
if (nc == '-') {
deinitialize_memwatch ();
console_out ("Memwatch breakpoints disabled\n");
return;
}
if (nc == 'd') {
if (illgdebug) {
ignore_ws (c);
if (more_params (c)) {
uae_u32 addr = readhex (c);
uae_u32 len = 1;
if (more_params (c))
len = readhex (c);
write_log ("cleared logging addresses %8.8X - %8.8X\n", addr, addr + len);
while (len > 0) {
addr &= 0xffffff;
illgdebug[addr] = 0;
addr++;
len--;
}
}
} else {
illg_init ();
console_out ("Illegal memory access logging enabled\n");
ignore_ws (c);
illgdebug_break = 0;
if (more_params (c))
illgdebug_break = 1;
}
return;
}
num = nc - '0';
if (num < 0 || num >= MEMWATCH_TOTAL)
return;
mwn = &mwnodes[num];
mwn->size = 0;
ignore_ws (c);
if (!more_params (c)) {
console_out ("Memwatch %d removed\n", num);
return;
}
mwn->addr = readhex (c);
mwn->size = 1;
mwn->rw = 2;
mwn->val_enabled = 0;
mwn->modval_written = 0;
ignore_ws (c);
if (more_params (c)) {
mwn->size = readhex (c);
ignore_ws (c);
if (more_params (c)) {
char nc = toupper (next_char (c));
if (nc == 'W')
mwn->rw = 1;
else if (nc == 'R' && toupper (**c) != 'W')
mwn->rw = 0;
else if (nc == 'R' && toupper (**c) == 'W')
next_char (c);
ignore_ws (c);
if (more_params (c)) {
if (toupper (**c) == 'M') {
mwn->modval_written = 1;
} else {
mwn->val = readhex (c);
mwn->val_enabled = 1;
}
}
}
}