-
Notifications
You must be signed in to change notification settings - Fork 90
/
debugmem.cpp
4054 lines (3796 loc) · 98.4 KB
/
debugmem.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
/*
*
* Combined Enforcer, MungWall and SegTracker emulation.
*
*/
#include "sysconfig.h"
#include "sysdeps.h"
#include "options.h"
#include "memory.h"
#include "newcpu.h"
#include "debug.h"
#include "debugmem.h"
#include "filesys.h"
#include "zfile.h"
#include "uae.h"
#include "fsdb.h"
#include "rommgr.h"
#define ELFMODE_NORMAL 0
#define ELFMODE_ROM 1
#define ELFMODE_DEBUGMEM 2
#define ELF_ALIGN_MASK 7
#define N_GSYM 0x20
#define N_FUN 0x24
#define N_STSYM 0x26
#define N_LCSYM 0x28
#define N_MAIN 0x2a
#define N_ROSYM 0x2c
#define N_RSYM 0x40
#define N_SLINE 0x44
#define N_DSLINE 0x46
#define N_SSYM 0x60
#define N_SO 0x64
#define N_LSYM 0x80
#define N_SOL 0x84
#define N_PSYM 0xa0
#define N_EINCL 0xa2
#define N_LBRAC 0xc0
#define N_RBRAC 0xe0
#define MAX_SOURCELINELEN 100
static mem_get_func debugmem_func_lgeti;
static mem_get_func debugmem_func_wgeti;
static mem_get_func debugmem_func_lget;
static mem_get_func debugmem_func_wget;
static mem_get_func debugmem_func_bget;
static mem_put_func debugmem_func_lput;
static mem_put_func debugmem_func_wput;
static mem_put_func debugmem_func_bput;
static xlate_func debugmem_func_xlate;
bool debugmem_initialized;
static bool debug_waiting;
static uaecptr debug_task;
static uae_u8 *exec_thistask;
static int executable_last_segment;
static bool libraries_loaded, fds_loaded;
static bool debugmem_mapped;
static int debugstack_word_state;
static uaecptr debugstack_word_addr;
static uaecptr debugstack_word_val;
static bool break_stack_pop, break_stack_push, break_stack_s;
static bool debugmem_active;
static int stackframemode;
uae_u32 debugmem_chiplimit;
bool debugmem_trace;
#define MAX_DEBUGMEMALLOCS 10000
#define MAX_DEBUGSEGS 1000
#define MAX_DEBUGSYMS 10000
#define MAX_STACKVARS 10000
#define MAX_DEBUGCODEFILES 10000
#define MAX_STACKFRAMES 100
struct debugstackframe
{
uaecptr current_pc;
uaecptr branch_pc;
uaecptr next_pc;
uaecptr stack;
uae_u32 regs[16];
uae_u16 sr;
};
static struct debugstackframe *stackframes, *stackframessuper;
static int stackframecnt, stackframecntsuper;
static uae_u32 debugmemptr;
struct stab
{
TCHAR *string;
uae_u8 type;
uae_u8 other;
uae_u16 desc;
uae_u32 val;
};
static struct stab *stabs;
static int stabscount;
struct stabtype
{
TCHAR *name;
uae_u32 id;
};
struct debugcodefile
{
const TCHAR *name, *path;
int length;
uae_u8 *data;
int lines;
uae_u8 **lineptr;
struct stabtype *stabtypes;
int stabtypecount;
uae_u32 start_pc;
uae_u32 end_pc;
};
static struct debugcodefile **codefiles;
static int codefilecnt;
struct linemapping
{
struct debugcodefile *file;
int line;
};
static struct linemapping *linemap;
static int linemapsize;
#define SYMBOL_LOCAL 0
#define SYMBOL_GLOBAL 1
#define SYMBOLTYPE_FUNC 1
struct debugsymbol
{
TCHAR *name;
uae_u32 segment;
uae_u32 allocid;
uae_u32 value;
uae_u32 flags;
uae_u32 type;
struct debugmemallocs *section;
void *data;
};
static struct debugsymbol **symbols;
static int symbolcnt, symbolindex;
struct libname
{
TCHAR *name;
uae_char *aname;
uae_u32 id;
uae_u32 base;
};
static struct libname *libnames;
static int libnamecnt;
struct libsymbol
{
struct libname *lib;
TCHAR *name;
uae_u32 value;
};
static struct libsymbol *libsymbols;
static int libsymbolcnt;
struct stackvar
{
TCHAR *name;
uae_u32 offset;
struct debugsymbol *func;
};
static struct stackvar **stackvars;
static int stackvarcnt;
#define DEBUGALLOC_HUNK 1
#define DEBUGALLOC_ALLOCMEM 2
#define DEBUGALLOC_ALLOCVEC 3
#define DEBUGALLOC_SEG 4
struct debugmemallocs
{
uae_u16 type;
TCHAR *name;
uae_u32 id;
uae_u16 internalid;
uae_u32 parentid;
uae_u32 idtype;
uaecptr start;
uae_u32 size;
uae_u32 start_page;
uae_u32 pages;
uaecptr pc;
uae_u32 data;
uae_u32 relative_start;
};
static struct debugmemallocs **allocs;
static int alloccnt;
#define HUNK_GAP 32768
#define PAGE_SIZE 256
#define PAGE_SIZE_MASK (PAGE_SIZE - 1)
#define DEBUGMEM_READ 0x01
#define DEBUGMEM_WRITE 0x02
#define DEBUGMEM_FETCH 0x04
#define DEBUGMEM_INITIALIZED 0x08
#define DEBUGMEM_STARTBLOCK 0x10
#define DEBUGMEM_ALLOCATED 0x40
#define DEBUGMEM_INUSE 0x80
#define DEBUGMEM_PARTIAL 0x100
#define DEBUGMEM_NOSTACKCHECK 0x200
#define DEBUGMEM_WRITE_NOCACHEFLUSH 0x400
#define DEBUGMEM_STACK 0x1000
struct debugmemdata
{
uae_u16 id;
uae_u16 flags;
uae_u8 unused_start;
uae_u8 unused_end;
uae_u8 state[PAGE_SIZE];
};
static struct debugmemdata **dmd;
static int totalmemdata;
struct debugsegtracker
{
TCHAR *name;
uae_u32 allocid;
uaecptr resident;
};
static struct debugsegtracker **dsegt;
static int segtrackermax, segtrackerindex;
static uae_u32 inhibit_break, last_break;
static uae_u8 *lebx(uae_u8 *p, uae_u32 *v)
{
uae_u32 val = 0;
for (;;) {
uae_u8 b = *p++;
val |= b & 0x7f;
if (!(b & 0x80))
break;
val <<= 7;
}
*v = val;
return p;
}
bool debugmem_break(int type)
{
if (inhibit_break & (1 << type))
return false;
last_break = type;
activate_debugger_new();
return true;
}
static bool debugmem_break_pc(int type, uaecptr pc, int len)
{
if (inhibit_break & (1 << type))
return false;
last_break = type;
activate_debugger_new_pc(pc, len);
return true;
}
bool debugmem_inhibit_break(int mode)
{
if (mode < 0) {
inhibit_break = 0;
return true;
} else if (mode > 0) {
inhibit_break = 0xffffffff;
return true;
} else {
inhibit_break ^= 1 << last_break;
}
return (inhibit_break & (1 << last_break)) != 0;
}
static void debugreportsegment(struct debugsegtracker *seg, bool verbose)
{
struct debugmemallocs *alloc = allocs[seg->allocid];
int parentid = alloc->id;
for (int i = 0; i < MAX_DEBUGMEMALLOCS; i++) {
struct debugmemallocs *a = allocs[i];
if (a->parentid == parentid) {
if (verbose) {
console_out_f(_T("Segment %d (%s): %08x %08x - %08x (%d)\n"),
a->internalid, seg->name, a->idtype, a->start, a->start + a->size - 1, a->size);
} else {
console_out_f(_T("Segment %d: %08x %08x - %08x (%d)\n"),
a->internalid, a->idtype, a->start, a->start + a->size - 1, a->size);
}
}
}
}
static void debugreportalloc(struct debugmemallocs *a)
{
uae_u32 off = debugmem_bank.start;
if (a->type == DEBUGALLOC_HUNK) {
console_out_f(_T("Segment %d: %08x %08x - %08x (%d)"),
a->id, a->idtype, a->start + off, a->start + off + a->size - 1, a->size);
if (a->name) {
console_out_f(_T(" %s"), a->name);
}
console_out_f(_T("\n"));
} else if (a->type == DEBUGALLOC_ALLOCMEM) {
console_out_f(_T("AllocMem ID=%4d: %08x %08x - %08x (%d) AllocFlags: %08x PC: %08x\n"),
a->id, a->idtype, a->start + off, a->start + off + a->size - 1, a->size, a->data, a->pc);
} else if (a->type == DEBUGALLOC_ALLOCVEC) {
console_out_f(_T("AllocVec ID=%4d: %08x %08x - %08x (%d) AllocFlags: %08x PC: %08x\n"),
a->id, a->idtype, a->start + off, a->start + off + a->size - 1, a->size, a->data, a->pc);
} else if (a->type == DEBUGALLOC_SEG) {
static int lastsegment;
struct debugsegtracker *sg = NULL;
const TCHAR *name = _T("<unknown>");
for (int i = 0; i < MAX_DEBUGSEGS; i++) {
sg = dsegt[(i + lastsegment) % MAX_DEBUGSEGS];
if (sg->allocid == a->parentid) {
name = sg->name;
lastsegment = i;
break;
}
}
console_out_f(_T("Segment %d (%s): %08x %08x - %08x (%d)\n"),
a->internalid, name, a->idtype, a->start, a->start + a->size - 1, a->size);
}
}
static void debugreport(struct debugmemdata *dm, uaecptr addr, int rwi, int size, const TCHAR *msg)
{
int offset = addr & PAGE_SIZE_MASK;
uaecptr addr_start = addr & ~PAGE_SIZE_MASK;
uae_u8 state = dm->state[offset];
console_out_f(_T("Invalid access. Addr=%08x RW=%c Size=%d: %s\n"), addr, (rwi & DEBUGMEM_WRITE) ? 'W' : 'R', size, msg);
console_out_f(_T("Page: %08x - %08x. State=%c Modified=%c, Start=%02X, End=%02X\n"),
addr_start, addr_start + PAGE_SIZE - 1,
!(state & (DEBUGMEM_ALLOCATED | DEBUGMEM_INUSE)) ? 'I' : (state & DEBUGMEM_WRITE) ? 'W' : 'R',
(state & DEBUGMEM_WRITE) ? '*' : (state & DEBUGMEM_INITIALIZED) ? '+' : '-',
dm->unused_start, PAGE_SIZE - dm->unused_end - 1);
if (peekdma_data.mask && (peekdma_data.addr == addr || (size > 2 && peekdma_data.addr + 2 == addr))) {
console_out_f(_T("DMA DAT=%04x PTR=%04x\n"), peekdma_data.reg, peekdma_data.ptrreg);
}
debugmem_break(1);
}
#if 0
static uae_u32 debug_get_disp_ea(uaecptr pc, uae_u32 base)
{
uae_u16 dp = get_word_debug(pc);
pc += 2;
int reg = (dp >> 12) & 15;
uae_s32 regd = regs.regs[reg];
if ((dp & 0x800) == 0)
regd = (uae_s32)(uae_s16)regd;
regd <<= (dp >> 9) & 3;
if (dp & 0x100) {
uae_s32 outer = 0;
if (dp & 0x80) base = 0;
if (dp & 0x40) regd = 0;
if ((dp & 0x30) == 0x20) {
base += (uae_s32)(uae_s16)get_word_debug(pc);
pc += 2;
}
if ((dp & 0x30) == 0x30) {
base += get_long_debug(pc);
pc += 4;
}
if ((dp & 0x3) == 0x2) {
outer = (uae_s32)(uae_s16)get_word_debug(pc);
pc += 2;
}
if ((dp & 0x3) == 0x3) {
outer = get_long_debug(pc);
pc += 4;
}
if ((dp & 0x4) == 0)
base += regd;
if (dp & 0x3)
base = get_long_debug(base);
if (dp & 0x4)
base += regd;
return base + outer;
} else {
return base + (uae_s32)((uae_s8)dp) + regd;
}
}
static uaecptr calc_jsr(void)
{
uae_u16 opcode = regs.opcode;
uaecptr pc = regs.instruction_pc;
if ((opcode & 0xff00) == 0x6100) {
uae_u8 o = opcode & 0xff;
if (o == 0) {
return pc + (uae_s16)get_word_debug(pc + 2) + 2;
} else if (o == 0xff) {
return pc + (uae_s32)get_long_debug(pc + 2) + 2;
} else {
return pc + (uae_s8)o + 2;
}
} else if ((opcode & 0xffc0) == 0x4e80) {
int reg = opcode & 7;
int mode = (opcode >> 3) & 7;
switch (mode)
{
case 2:
return m68k_areg(regs, reg);
case 5:
return m68k_areg(regs, reg) + (uae_s16)get_word_debug(pc + 2);
case 6:
return debug_get_disp_ea(pc + 2, m68k_areg(regs, reg));
case 7:
if (reg == 0)
return (uae_u32)(uae_s16)get_word_debug(pc + 2);
if (reg == 1)
return get_long_debug(pc + 2);
if (reg == 2)
return pc + (uae_s16)get_word_debug(pc + 2) + 2;
if (reg == 3)
return debug_get_disp_ea(pc + 2, pc + 2);
break;
}
}
return 0xffffffff;
}
#endif
void branch_stack_pop_rte(uaecptr oldpc)
{
if (!stackframes)
return;
uaecptr newpc = m68k_getpc();
bool found = false;
for (int i = stackframecntsuper - 1; i >= 0; i--) {
struct debugstackframe *sf = &stackframessuper[i];
if (sf->next_pc == newpc) {
stackframecntsuper = i;
found = true;
break;
}
}
if (found) {
if (break_stack_pop) {
break_stack_pop = false;
debugmem_break(0);
}
} else {
// if no match, it probably was stack switch to other address,
// assume it matched..
if (stackframecntsuper > 0)
stackframecntsuper--;
}
if (found && break_stack_pop && break_stack_s) {
break_stack_push = false;
break_stack_pop = false;
debugmem_break(0);
}
}
void branch_stack_pop_rts(uaecptr oldpc)
{
if (!stackframes)
return;
if (!stackframemode) {
if (debug_waiting || (!regs.s && get_long_host(exec_thistask) != debug_task))
return;
}
int cnt = regs.s ? stackframecntsuper : stackframecnt;
uaecptr newpc = m68k_getpc();
bool found = false;
for (int i = cnt - 1; i >= 0; i--) {
struct debugstackframe *sf = regs.s ? &stackframessuper[i] : &stackframes[i];
if (sf->next_pc == newpc) {
cnt = i;
found = true;
break;
}
}
if (regs.s) {
stackframecntsuper = cnt;
} else {
stackframecnt = cnt;
stackframecntsuper = 0;
}
if (found && break_stack_pop && ((break_stack_s && regs.s) || (!break_stack_s && !regs.s))) {
break_stack_push = false;
break_stack_pop = false;
debugmem_break(0);
}
}
void branch_stack_push(uaecptr oldpc, uaecptr newpc)
{
if (!stackframes) {
return;
}
if (!stackframemode) {
if (debug_waiting || (!regs.s && get_long_host(exec_thistask) != debug_task)) {
return;
}
}
int cnt = regs.s ? stackframecntsuper : stackframecnt;
if (cnt >= MAX_STACKFRAMES) {
write_log(_T("Stack frame %c max limit reached!\n"), regs.s ? 'S' : 'U');
stackframecntsuper = 0;
stackframecnt = 0;
return;
}
struct debugstackframe *sf = regs.s ? &stackframessuper[cnt] : &stackframes[cnt];
sf->current_pc = regs.instruction_pc;
sf->next_pc = newpc;
sf->stack = m68k_areg(regs, 7);
sf->branch_pc = m68k_getpc();
MakeSR();
sf->sr = regs.sr;
memcpy(sf->regs, regs.regs, sizeof(uae_u32) * 16);
if (regs.s) {
stackframecntsuper++;
} else {
stackframecnt++;
stackframecntsuper = 0;
}
if (break_stack_push && ((break_stack_s && regs.s) || (!break_stack_s && !regs.s))) {
break_stack_push = false;
break_stack_pop = false;
debugmem_break(11);
}
}
bool debugmem_break_stack_pop(void)
{
if (!stackframes)
return false;
break_stack_s = regs.s != 0;
break_stack_pop = true;
return true;
}
bool debugmem_break_stack_push(void)
{
if (!stackframes)
return false;
break_stack_s = regs.s != 0;
break_stack_pop = true;
break_stack_push = true;
return true;
}
void debugmem_flushcache(uaecptr addr, int size)
{
if (!debugmem_initialized)
return;
if (size < 0) {
for (int i = 0; i < totalmemdata; i++) {
struct debugmemdata* dm = dmd[i];
if (dm->flags & DEBUGMEM_WRITE_NOCACHEFLUSH) {
for (int j = 0; j < PAGE_SIZE; j++) {
dm->state[j] &= ~DEBUGMEM_WRITE_NOCACHEFLUSH;
}
dm->flags &= ~DEBUGMEM_WRITE_NOCACHEFLUSH;
}
}
return;
}
if (addr + size < debugmem_bank.start || addr >= debugmem_bank.start + debugmem_bank.allocated_size)
return;
for (int i = 0; i < (PAGE_SIZE + size - 1) / PAGE_SIZE; i++) {
uaecptr a = (addr & ~PAGE_SIZE) + i * PAGE_SIZE;
if (a < debugmem_bank.start || a >= debugmem_bank.start + debugmem_bank.allocated_size)
continue;
struct debugmemdata* dm = dmd[(a - debugmem_bank.start) / PAGE_SIZE];
for (int j = 0; j < PAGE_SIZE; j++) {
uaecptr aa = a + j;
if (aa < addr || aa >= addr + size)
continue;
dm->state[j] &= ~DEBUGMEM_WRITE_NOCACHEFLUSH;
}
}
}
static bool debugmem_func(uaecptr addr, int rwi, int size, uae_u32 val)
{
bool ret = true;
uaecptr oaddr = addr;
struct debugmemdata *dmfirst = NULL;
if (debug_waiting && (rwi & DEBUGMEM_FETCH)) {
// first instruction?
if (addr == debugmem_bank.start + HUNK_GAP + 8) {
debugmem_break(2);
debug_waiting = false;
}
}
// in some situations addr may already have start substracted
if (addr >= debugmem_bank.start) {
addr -= debugmem_bank.start;
}
for (int i = 0; i < size; i++) {
int offset = addr & PAGE_SIZE_MASK;
int page = addr / PAGE_SIZE;
struct debugmemdata *dm = dmd[page];
uae_u8 state = dm->state[offset];
if (!i)
dmfirst = dm;
if (!(state & DEBUGMEM_INUSE)) {
debugreport(dm, oaddr, rwi, size, _T("Accessing invalid memory"));
return false;
}
if (!(rwi & DEBUGMEM_NOSTACKCHECK) || ((rwi & DEBUGMEM_NOSTACKCHECK) && !(dm->flags & DEBUGMEM_STACK))) {
if ((rwi & DEBUGMEM_FETCH) && !(state & DEBUGMEM_INITIALIZED) && !(state & DEBUGMEM_WRITE)) {
debugreport(dm, oaddr, rwi, size, _T("Instruction fetch from uninitialized memory"));
return false;
}
if ((rwi & DEBUGMEM_FETCH) && (state & DEBUGMEM_WRITE_NOCACHEFLUSH)) {
debugreport(dm, oaddr, rwi, size, _T("Instruction fetch from memory that was modified without flushing caches"));
return false;
}
if ((rwi & DEBUGMEM_FETCH) && (state & DEBUGMEM_WRITE) && (state & DEBUGMEM_FETCH)) {
debugreport(dm, oaddr, rwi, size, _T("Instruction fetch from memory that was modified after being executed at least once"));
return false;
}
}
if ((rwi & DEBUGMEM_READ) && !(state & DEBUGMEM_INITIALIZED) && !(state & DEBUGMEM_WRITE)) {
debugreport(dm, oaddr, rwi, size, _T("Reading uninitialized memory"));
return false;
}
if (dm->flags & DEBUGMEM_PARTIAL) {
if (offset < dm->unused_start) {
debugreport(dm, oaddr, rwi, size, _T("Reading from partially invalid page (low)"));
return false;
}
if (offset >= PAGE_SIZE - dm->unused_end) {
debugreport(dm, oaddr, rwi, size, _T("Reading from partially invalid page (high)"));
return false;
}
}
if (rwi & DEBUGMEM_WRITE) {
rwi |= DEBUGMEM_WRITE_NOCACHEFLUSH;
dm->flags |= DEBUGMEM_WRITE_NOCACHEFLUSH;
}
if ((rwi & DEBUGMEM_FETCH) && (state & DEBUGMEM_WRITE))
state &= ~(DEBUGMEM_WRITE | DEBUGMEM_WRITE_NOCACHEFLUSH);
if ((state | rwi) != state) {
//console_out_f(_T("addr %08x %d/%d (%02x -> %02x) PC=%08x\n"), addr, i, size, state, rwi, M68K_GETPC);
dm->state[offset] |= rwi;
}
addr++;
}
return ret;
}
static uae_u32 REGPARAM2 debugmem_lget(uaecptr addr)
{
uae_u32 v = 0xdeadf00d;
if (debugmem_func(addr, DEBUGMEM_READ, 4, 0))
v = debugmem_func_lget(addr);
return v;
}
static uae_u32 REGPARAM2 debugmem_wget(uaecptr addr)
{
uae_u32 v = 0xd00d;
if (debugmem_func(addr, DEBUGMEM_READ, 2, 0))
v = debugmem_func_wget(addr);
return v;
}
static uae_u32 REGPARAM2 debugmem_bget(uaecptr addr)
{
uae_u32 v = 0xdd;
if (debugmem_func(addr, DEBUGMEM_READ, 1, 0))
v = debugmem_func_bget(addr);
return v;
}
static uae_u32 REGPARAM2 debugmem_lgeti(uaecptr addr)
{
uae_u32 v = 0x4afc4afc;
if (debugmem_func(addr, DEBUGMEM_READ | DEBUGMEM_FETCH, 4, 0))
v = debugmem_func_lgeti(addr);
return v;
}
static uae_u32 REGPARAM2 debugmem_wgeti(uaecptr addr)
{
uae_u32 v = 0x4afc;
if (debugmem_func(addr, DEBUGMEM_READ | DEBUGMEM_FETCH, 2, 0))
v = debugmem_func_wgeti(addr);
return v;
}
static void REGPARAM2 debugmem_lput(uaecptr addr, uae_u32 v)
{
if (debugmem_func(addr, DEBUGMEM_WRITE, 4, v))
debugmem_func_lput(addr, v);
}
static void REGPARAM2 debugmem_wput(uaecptr addr, uae_u32 v)
{
if (debugmem_func(addr, DEBUGMEM_WRITE, 2, v))
debugmem_func_wput(addr, v);
}
static void REGPARAM2 debugmem_bput(uaecptr addr, uae_u32 v)
{
if (debugmem_func(addr, DEBUGMEM_WRITE, 1, v))
debugmem_func_bput(addr, v);
}
static uae_u8 *REGPARAM2 debugmem_xlate(uaecptr addr)
{
if (debug_waiting)
debugmem_func(addr, DEBUGMEM_FETCH | DEBUGMEM_READ | DEBUGMEM_NOSTACKCHECK, 2, 0);
addr -= debugmem_bank.start & debugmem_bank.mask;
addr &= debugmem_bank.mask;
return debugmem_bank.baseaddr + addr;
}
static int debugmem_free(uaecptr addr, uae_u32 size)
{
uaecptr oaddr = addr;
addr -= debugmem_bank.start;
int page = addr / PAGE_SIZE;
struct debugmemdata *dm = dmd[page];
bool ok = true;
if (!(dm->flags & DEBUGMEM_ALLOCATED)) {
console_out_f(_T("Invalid memory free (%08x %d) Start address points to unallocated memory\n"), oaddr, size);
ok = false;
} else if (!(dm->flags & DEBUGMEM_STARTBLOCK)) {
console_out_f(_T("Invalid memory free (%08x %d) Start address points to allocated memory but not start of allocated memory\n"), oaddr, size);
ok = false;
} else {
struct debugmemallocs *dma = allocs[dm->id];
if (dma->start == addr && dma->size == size) {
// it was valid!
for (int i = 0; i < dma->pages; i++) {
struct debugmemdata *dm2 = dmd[page + i];
memset(dm2, 0, sizeof(struct debugmemdata));
}
memset(debugmem_bank.baseaddr + dma->start_page * PAGE_SIZE, 0x95, dma->pages * PAGE_SIZE);
memset(dma, 0, sizeof(struct debugmemallocs));
return dm->id;
}
console_out_f(_T("Invalid memory free (%08x %d) ID=%d Start address points to start of allocated memory but size does not match allocation size (%d <> %d)\n"),
oaddr, size, dm->id, size, dma->size);
}
// report free memory error
int end = (size + PAGE_SIZE - 1) & ~PAGE_SIZE_MASK;
int allocid = -1;
for (int i = 0; i < end; i++) {
if (page + i >= totalmemdata) {
console_out_f(_T("Free end address is out of range\n"));
ok = false;
break;
}
struct debugmemdata *dm2 = dmd[page + i];
if ((dm2->flags & DEBUGMEM_ALLOCATED) && allocid != page + i) {
struct debugmemallocs *dma = allocs[dm2->id];
console_out_f(_T("Conflicts with existing allocation ID=%d (%08x - %08x %d)\n"),
dm->id, dma->start, dma->start + dma->size - 1, dma->size);
debugreportalloc(dma);
allocid = page + i;
}
}
debugmem_break(3);
return 0;
}
static uae_u32 REGPARAM2 debugmem_chipmem_lget(uaecptr addr)
{
uae_u32 *m;
if (addr < debugmem_chiplimit)
return debugmem_chiphit(addr, 0, -4);
addr &= chipmem_bank.mask;
m = (uae_u32 *)(chipmem_bank.baseaddr + addr);
return do_get_mem_long(m);
}
static uae_u32 REGPARAM2 debugmem_chipmem_wget(uaecptr addr)
{
uae_u16 *m, v;
if (addr < debugmem_chiplimit)
return debugmem_chiphit(addr, 0, -2);
addr &= chipmem_bank.mask;
m = (uae_u16 *)(chipmem_bank.baseaddr + addr);
v = do_get_mem_word(m);
return v;
}
static uae_u32 REGPARAM2 debugmem_chipmem_bget(uaecptr addr)
{
uae_u8 v;
if (addr < debugmem_chiplimit)
return debugmem_chiphit(addr, 0, -1);
addr &= chipmem_bank.mask;
v = chipmem_bank.baseaddr[addr];
return v;
}
void REGPARAM2 debugmem_chipmem_lput(uaecptr addr, uae_u32 l)
{
if (addr < debugmem_chiplimit) {
debugmem_chiphit(addr, l, 4);
} else {
uae_u32 *m;
addr &= chipmem_bank.mask;
m = (uae_u32 *)(chipmem_bank.baseaddr + addr);
do_put_mem_long(m, l);
}
}
void REGPARAM2 debugmem_chipmem_wput(uaecptr addr, uae_u32 w)
{
if (addr < debugmem_chiplimit) {
debugmem_chiphit(addr, w, 2);
} else {
uae_u16 *m;
addr &= chipmem_bank.mask;
m = (uae_u16 *)(chipmem_bank.baseaddr + addr);
do_put_mem_word(m, w);
}
}
void REGPARAM2 debugmem_chipmem_bput(uaecptr addr, uae_u32 b)
{
if (addr < debugmem_chiplimit) {
debugmem_chiphit(addr, b, 1);
} else {
addr &= chipmem_bank.mask;
chipmem_bank.baseaddr[addr] = b;
}
}
static int REGPARAM2 debugmem_chipmem_check(uaecptr addr, uae_u32 size)
{
addr &= chipmem_bank.mask;
return (addr + size) <= chipmem_bank.reserved_size;
}
static uae_u8 *REGPARAM2 debugmem_chipmem_xlate(uaecptr addr)
{
addr &= chipmem_bank.mask;
return chipmem_bank.baseaddr + addr;
}
static addrbank debugmem_chipmem_bank = {
debugmem_chipmem_lget, debugmem_chipmem_wget, debugmem_chipmem_bget,
debugmem_chipmem_lput, debugmem_chipmem_wput, debugmem_chipmem_bput,
debugmem_chipmem_xlate, debugmem_chipmem_check, NULL, _T("chip"), _T("Debug Chip memory"),
debugmem_chipmem_lget, debugmem_chipmem_wget,
ABFLAG_RAM | ABFLAG_THREADSAFE | ABFLAG_CHIPRAM | ABFLAG_CACHE_ENABLE_BOTH, 0, 0
};
static void setchipbank(bool activate)
{
if (activate) {
map_banks(&debugmem_chipmem_bank, 0, 1, 0);
} else {
map_banks(&chipmem_bank, 0, 1, 0);
}
}
static struct debugmemallocs *getallocblock(void)
{
int round = 0;
alloccnt++;
if (alloccnt >= MAX_DEBUGMEMALLOCS) {
alloccnt = 1;
}
while (allocs[alloccnt]->id != 0) {
alloccnt++;
if (alloccnt >= MAX_DEBUGMEMALLOCS) {
alloccnt = 1;
if (round) {
console_out_f(_T("debugmem out of alloc blocks!\n"));
return NULL;
}
round++;
}
}
struct debugmemallocs *dm = allocs[alloccnt];
dm->id = alloccnt;
return dm;
}
static struct debugmemallocs *debugmem_allocate(uae_u32 size, uae_u32 flags, uae_u32 parentid)
{
struct debugmemallocs *dm = getallocblock();
if (!dm)
return NULL;
if (size >= totalmemdata) {
console_out_f(_T("debugmem allocation larger than free space! Alloc size %d (%08x), flags %08x\n"), size, size, flags);
return 0;
}
int offset = debugmemptr / PAGE_SIZE;
bool gotit = true;
int totalsize = 0;
int extrasize = 0;
for (extrasize = HUNK_GAP; extrasize >= PAGE_SIZE; extrasize /= 2) {
totalsize = (extrasize + size + PAGE_SIZE - 1) & ~PAGE_SIZE_MASK;
for (int i = 0; i < totalmemdata; i++) {
struct debugmemdata *dm = dmd[offset];
if (offset + totalsize / PAGE_SIZE >= totalmemdata) {
offset = 0;
continue;
}
gotit = true;
// extra + size continous space available?
if (!(dm->flags & DEBUGMEM_ALLOCATED)) {
for (int j = 0; j < totalsize / PAGE_SIZE; j++) {
struct debugmemdata *dm2 = dmd[offset + j];
if (dm->flags & DEBUGMEM_ALLOCATED) {
gotit = false;
break;
}
}
}
if (gotit)
break;
offset++;
offset = offset % totalmemdata;
}
if (gotit)
break;
}
if (!gotit || !totalsize || !extrasize) {
console_out_f(_T("debugmem out of free space! Alloc size %d (%08x), flags %08x\n"), size, size, flags);
return 0;
}
dm->parentid = parentid;
dm->start_page = offset;
dm->pages = totalsize / PAGE_SIZE;
for (int j = 0; j < dm->pages; j++) {
struct debugmemdata *dm2 = dmd[offset + j];
dm2->flags |= DEBUGMEM_ALLOCATED;
dm2->id = dm->id;
}
memset(debugmem_bank.baseaddr + offset * PAGE_SIZE, 0xa3, totalsize);
int startoffset = extrasize / PAGE_SIZE;
dm->start = (offset + startoffset) * PAGE_SIZE;
dm->size = size;
dm->pc = M68K_GETPC;
for (int j = 0; j < (size + PAGE_SIZE - 1) / PAGE_SIZE; j++) {
struct debugmemdata *dm2 = dmd[offset + startoffset + j];
dm2->flags |= DEBUGMEM_INUSE | flags;
if (j == 0) {
dm2->flags |= DEBUGMEM_STARTBLOCK;
}
memset(dm2->state, ((flags & DEBUGMEM_INITIALIZED) ? DEBUGMEM_INITIALIZED : 0) | DEBUGMEM_INUSE, PAGE_SIZE);
uae_u8 filler = (flags & DEBUGMEM_INITIALIZED) ? 0x00 : 0x99;
memset(debugmem_bank.baseaddr + (offset + startoffset + j) * PAGE_SIZE, filler, PAGE_SIZE);
if (j == (size + PAGE_SIZE - 1) / PAGE_SIZE - 1) {
if (size & PAGE_SIZE_MASK) {
dm2->unused_end = PAGE_SIZE - (size & PAGE_SIZE_MASK);
dm2->flags |= DEBUGMEM_PARTIAL;
memset(dm2->state + (PAGE_SIZE - dm2->unused_end), 0, dm2->unused_end);
memset(debugmem_bank.baseaddr + (offset + startoffset + j) * PAGE_SIZE + (PAGE_SIZE - dm2->unused_end), 0x97, dm2->unused_end);
}
}
}
if (flags & DEBUGMEM_STACK) {
dm->name = my_strdup(_T("STACK"));
}
debugmemptr = offset * PAGE_SIZE + extrasize + ((size + PAGE_SIZE - 1) & ~PAGE_SIZE_MASK);
return dm;
}
static int debugmem_unreserve(uaecptr addr, uae_u32 size, bool noerror)
{
for (int i = 0; i < MAX_DEBUGMEMALLOCS; i++) {
struct debugmemallocs *alloc = allocs[i];
if (alloc->type != DEBUGALLOC_SEG)
continue;