-
Notifications
You must be signed in to change notification settings - Fork 16
/
debugger.c
1370 lines (1113 loc) · 34 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
/*
Copyright (C) 1997-2008 ZSNES Team ( zsKnight, _Demo_, pagefault, Nach )
http://www.zsnes.com
http://sourceforge.net/projects/zsnes
https://zsnes.bountysource.com
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
version 2 as published by the Free Software Foundation.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
#include <ctype.h>
#include <string.h>
#ifndef NCURSES
#include <curses.h>
#else
#include <ncurses.h>
#endif
#include "zpath.h"
#include "c_vcache.h"
#include "cpu/c_execute.h"
#include "cpu/memory.h"
#include "cpu/memtable.h"
#include "cpu/regs.h"
#include "cpu/spc700.h"
#include "debugasm.h"
#include "endmem.h"
#include "init.h"
#include "initc.h"
#include "zstate.h"
// All of these should be in headers, people!
extern unsigned char oamram[1024], DSPMem[256];
extern unsigned char CurrentCPU;
extern unsigned char soundon;
extern unsigned int cycpbl;
extern unsigned short xa, xx, xy, xs;
extern unsigned char xdb;
extern unsigned char debuggeron;
// should be in "zstate.h"
void debugloadstate();
char* ocname;
unsigned char addrmode[];
char* spcnametab[];
char* AddressTable[];
unsigned char ArgumentTable[];
unsigned char debugds = 0; // debug disable (bit 0 = 65816, bit 1 = SPC)
unsigned int numinst = 0;
unsigned char wx = 0, wy = 0, wx2 = 0, wy2 = 0;
unsigned char execut = 0;
unsigned char debstop = 0, debstop2 = 0, debstop3 = 0, debstop4 = 0;
char debugsa1 = 0;
char skipdebugsa1 = 1;
#define CP(n) (A_BOLD | COLOR_PAIR(n))
enum color_pair {
cp_white = 1,
cp_magenta,
cp_red,
cp_cyan,
cp_green,
cp_yellow,
cp_white_on_blue,
};
WINDOW* debugwin;
// can't get this to work right...
// #define CHECK (COLOR_PAIR(cp_white) | A_DIM | ACS_CKBOARD)
#define CHECK (CP(cp_white) | ' ')
void debugloop();
void startdisplay();
void nextopcode();
void cleardisplay();
void nextspcopcode();
void SaveOAMRamLog();
void debugdump();
void out65816();
void traceops(unsigned count);
void SPCbreakops(unsigned short addr);
unsigned char* findop();
unsigned char* findoppage();
void startdebugger(void)
{
static int firsttime = 1;
curblank = 0x40;
debuggeron = 1;
if (firsttime) {
initscr();
cbreak();
noecho();
intrflush(stdscr, FALSE);
keypad(stdscr, TRUE);
/* set up colors */
start_color();
init_pair(cp_white, COLOR_WHITE, COLOR_BLACK);
init_pair(cp_magenta, COLOR_MAGENTA, COLOR_BLACK);
init_pair(cp_red, COLOR_RED, COLOR_BLACK);
init_pair(cp_cyan, COLOR_CYAN, COLOR_BLACK);
init_pair(cp_green, COLOR_GREEN, COLOR_BLACK);
init_pair(cp_yellow, COLOR_YELLOW, COLOR_BLACK);
init_pair(cp_white_on_blue, COLOR_WHITE, COLOR_BLUE);
}
execut = 0;
if (firsttime) {
startdisplay();
debugwin = newwin(20, 77, 2, 1);
wbkgd(debugwin, CP(cp_white_on_blue) | ' ');
// wattrset(debugwin, CP(cp_white_on_blue));
scrollok(debugwin, TRUE);
idlok(debugwin, TRUE);
firsttime = 0;
} else {
touchwin(stdscr);
touchwin(debugwin);
refresh();
wrefresh(debugwin);
}
debugloop();
cleardisplay();
// "pushad / call LastLog / ... / popad" elided
SaveOAMRamLog();
if (execut == 1) {
start65816();
return;
}
endprog();
return;
}
WINDOW* openwindow(int nlines, int ncols, int begin_y, int begin_x,
char* message)
{
WINDOW* w = newwin(nlines, ncols, begin_y, begin_x);
wbkgd(w, CP(cp_white_on_blue | ' '));
// wattrset(w, CP(cp_white_on_blue));
mvwprintw(w, 1, 1, "%s", message);
wclrtoeol(w);
box(w, 0, 0);
return w;
}
void closewindow(WINDOW* w)
{
delwin(w);
touchwin(debugwin);
wrefresh(debugwin);
}
//*******************************************************
// Debug Loop
//*******************************************************
unsigned short PrevBreakPt_offset;
unsigned char PrevBreakPt_page;
void debugloop()
{
int key;
a:
if (!(debugds & 2))
nextopcode();
if (!(debugds & 1))
nextspcopcode();
b:
// redrawing the display is always a good idea
refresh();
wrefresh(debugwin);
key = getch();
if (key >= 0 && key < 256)
key = toupper(key);
switch (key) {
case KEY_F(1): // run
execut = 1;
return;
case KEY_F(2): // debugsavestate
statesaver();
goto b;
case KEY_F(4): // debugloadstate
debugloadstate();
goto a;
case 27: // exit
return;
case '\n': // step
goto e;
case 'C': // clear
numinst = 0;
goto a;
case 'M': // modify
{
WINDOW* w;
unsigned addr, value, n;
w = openwindow(7, 33, 11, 24, " Enter Address : ");
mvwaddstr(w, 3, 1, " Previous Value: ");
mvwaddstr(w, 5, 1, " Enter Value : ");
wrefresh(w);
echo();
n = mvwscanw(w, 1, 21, "%x", &addr);
noecho();
if (n == 1) {
mvwprintw(w, 3, 21, "%02x", memr8(addr >> 16, addr));
wrefresh(w);
echo();
n = mvwscanw(w, 5, 21, "%x", &value);
noecho();
if (n == 1) {
memw8(addr >> 16, addr, value);
}
}
closewindow(w);
goto b;
}
case 'B': // breakpoint
{
WINDOW* w = openwindow(3, 33, 11, 24, " Enter Address : ");
unsigned addr, n;
wrefresh(w);
echo();
n = wscanw(w, "%x", &addr);
noecho();
closewindow(w);
if (n == 1) {
w = openwindow(3, 52, 11, 14,
" Locating Breakpoint ... Press ESC to stop. ");
wrefresh(w);
nodelay(stdscr, 1);
PrevBreakPt_page = addr >> 16;
PrevBreakPt_offset = addr;
breakops();
nodelay(stdscr, 0);
closewindow(w);
goto a;
}
goto b;
}
case 'R': // repeat breakpoint
breakops();
goto a;
case 'S': // SPC breakpoint
{
WINDOW* w;
unsigned addr, n;
w = openwindow(3, 33, 11, 24, " Enter Address : ");
wrefresh(w);
echo();
n = mvwscanw(w, 1, 22, "%x", &addr);
noecho();
closewindow(w);
if (n == 1) {
SPCbreakops(addr);
goto a;
}
goto b;
}
case 'A': // SPC modify
{
WINDOW* w;
unsigned addr, value, n;
w = openwindow(7, 33, 11, 24, " Enter Address : ");
mvwaddstr(w, 3, 1, " Previous Value: ");
mvwaddstr(w, 5, 1, " Enter Value : ");
wrefresh(w);
echo();
n = mvwscanw(w, 1, 22, "%x", &addr);
noecho();
addr &= 0xFFFF;
if (n == 1) {
mvwprintw(w, 3, 22, "%02x", SPCRAM[addr]);
wrefresh(w);
echo();
n = mvwscanw(w, 5, 22, "%x", &value);
noecho();
if (n == 1) {
SPCRAM[addr] = value;
}
}
closewindow(w);
goto b;
}
case 'T': // trace
{
WINDOW* w;
unsigned n, instrs;
w = openwindow(3, 52, 11, 14, " Enter # of Instructions to Trace : ");
wrefresh(w);
echo();
n = wscanw(w, "%d", &instrs);
noecho();
closewindow(w);
if (n == 1) {
traceops(instrs);
goto a;
}
goto b;
}
case 'D': // debug dump
debugdump();
goto b;
case 'W': // break at signal (breakatsign)
{
WINDOW* w;
w = openwindow(3, 52, 11, 14,
" Waiting for Signal .... Press ESC to stop.");
wrefresh(w);
debstop3 = 0;
nodelay(w, TRUE);
do {
execnextop();
} while ((!((++numinst % 256) && (wgetch(w) == 27)))
&& (debstop3 != 1));
debstop3 = 0;
nodelay(w, FALSE);
closewindow(w);
goto a;
}
case 'L': // break at signal & log (breakatsignlog)
{
FILE* fp;
WINDOW *w, *real_debugwin;
w = openwindow(3, 52, 11, 14,
" Waiting for Signal .... Press ESC to stop.");
wrefresh(w);
// Open output file
fp = fopen_dir(ZStartPath, "debug.log", "w");
real_debugwin = debugwin;
debugwin = newpad(2, 77);
scrollok(debugwin, TRUE);
debstop3 = 0;
nodelay(w, TRUE);
do {
char buf[78];
// log instruction
move(0, 0);
out65816();
mvwinnstr(debugwin, 0, 0, buf, 77);
buf[77] = 0;
fprintf(fp, "%s\n", buf);
fflush(fp);
execnextop();
} while ((!((++numinst % 256) && (wgetch(w) == 27)))
&& (debstop3 != 1));
debstop3 = 0;
nodelay(w, FALSE);
fclose(fp);
delwin(debugwin);
debugwin = real_debugwin;
closewindow(w);
goto a;
}
case '1': // toggle SPC
debugds ^= 1;
break;
case '2': // toggle 65816
debugds ^= 2;
break;
default:
wprintw(debugwin, "Unknown key code: %d\n", key);
goto b;
}
e:
skipdebugsa1 = 0;
execnextop();
skipdebugsa1 = 1;
if (soundon && (debugds & 2) && (cycpbl >= 55))
goto e;
goto a;
}
//*******************************************************
// BreakatSignC Breaks whenever sndwrit = 1
//*******************************************************
unsigned char sndwrit;
/* void breakatsignc() {} */
void traceops(unsigned count)
{
WINDOW* w;
w = openwindow(3, 52, 11, 14, " Tracing. Press ESC to stop.");
wrefresh(w);
nodelay(w, TRUE);
while (count-- && (wgetch(w) != 27)) {
execnextop();
}
closewindow(w);
}
void SPCbreakops(unsigned short addr)
{
WINDOW* w;
unsigned char* breakarea;
breakarea = SPCRAM + addr;
w = openwindow(3, 52, 11, 14, "Locating Breakpoint ... Press ESC to stop.");
wrefresh(w);
nodelay(w, TRUE);
do {
execnextop();
} while ((!((++numinst % 256)
&& (wgetch(w) == 27)))
&& (spcPCRam != breakarea));
nodelay(w, FALSE);
closewindow(w);
}
void printinfo(char* s)
{
while (s[0]) {
if (s[0] == '@') {
int colors[] = {
0, 0, cp_green, cp_cyan,
cp_red, cp_magenta, cp_yellow, cp_white
};
attrset(COLOR_PAIR(colors[s[1] - '0']));
s += 2;
} else {
addch(s[0]);
s += 1;
}
}
}
//*******************************************************
// Start Display
//*******************************************************
void startdisplay()
{
int i;
// Clear the screen
bkgd(CP(cp_white) | ' ');
clear();
// Draw to screen
// ASM for some reason puts the same thing in the upper left corner again?
move(1, 0);
attrset(CP(cp_white_on_blue));
addch(CurrentCPU + '0');
for (i = 15; i; i--)
addch(ACS_HLINE);
printw(" CC: Y: ");
for (i = 19; i; i--)
addch(ACS_HLINE);
addch(' ');
for (i = 11; i; i--)
addch(' ');
addch(' ');
for (i = 16; i; i--)
addch(ACS_HLINE);
addch(ACS_URCORNER);
for (i = 2; i < 22; i++) {
mvaddch(i, 0, ACS_VLINE);
hline(' ', 77);
mvaddch(i, 78, ACS_VLINE);
mvaddch(i, 79, CHECK);
}
mvaddch(22, 0, ACS_LLCORNER);
for (i = 77; i; i--)
addch(ACS_HLINE);
mvaddch(22, 78, ACS_LRCORNER);
mvaddch(22, 79, CHECK);
move(23, 1);
for (i = 79; i; i--)
addch(CHECK);
// Print debugger information
move(0, 2);
attrset(CP(cp_white));
printinfo("- @5Z@4S@3N@2E@6S@7 debugger -");
move(1, 4);
attrset(CP(cp_white_on_blue));
printinfo(" 65816 ");
// HACK ALERT! this should really be on the bottom line, but
// xterms default to being one line shorter than 80x25, so this
// won't be on the bottom line on DOS!
// Also, we are printing on top of the (currently invisible) drop shadow!"
move(23, 0);
printinfo("@4(@6T@4)@7race for @4(@6B@4)@7reakpoint "
"@4(@6Enter@4)@7 Next "
"@4(@6M@4)@7odify @4(@6F9@4)@7 Signal @4(@6F1@4)@7 Run");
// ...
move(0, 0);
refresh();
}
//*******************************************************
// Next Opcode Writes the next opcode & regs
//*******************************************************
// 008000 STZ $123456,x A:0000 X:0000 Y:0000 S:01FF DB:00 D:0000 P:33 E+
/*
void addtail() {
debugt++;
if (debugt == 100)
debugt = 0;
if (debugt == debugh)
debugh++;
if (debugh == 100)
debugh = 0;
}
*/
// I'm going to have to completely rip out byuu's effective address
// stuff, it is just plain *WRONG*, besides being unsafe...
// For next time, http://www.zophar.net/tech/files/65c816.txt seems
// like a good reference for effective address calculation; also, use
// 24-bit addresses for all calculations (so completely rip out
// memr8, too). Also, preferably read memory in a
// non-destructive way.
// This whole instr[1] thing probably isn't quite right either, but it
// seems unlikely that instructions would be stored discontiguously
// than that data would span 64kb boundaries.
void out65816_addrmode(unsigned char* instr)
{
char* padding = "";
#define GETXB() ((ocname[4 * instr[0]] != 'J') ? xdb : xpb)
#define INDEX_RIGHT(addr, index) \
((xp & 0x10) \
? (((addr) & ~0xff) | (((addr) + (index)) & 0xff)) \
: (((addr) & ~0xffff) | (((addr) + (index)) & 0xffff)))
// each mode must output 19 characters
switch (addrmode[instr[0]]) {
case 0:
case 6:
case 21:
// nothing to show
wprintw(debugwin, "%19s", padding);
break;
case 1: // #$12,#$1234 (M-flag)
wprintw(debugwin, "#$");
if (xp & 0x20) {
wprintw(debugwin, "%02x", instr[1]);
wprintw(debugwin, "%15s", padding);
} else {
wprintw(debugwin, "%04x", *(unsigned short*)(instr + 1));
wprintw(debugwin, "%13s", padding);
}
break;
case 2: // $1234 : db+$1234
wprintw(debugwin, "$%04x", *(unsigned short*)(instr + 1));
wprintw(debugwin, "%5s[%02x%04x] ", padding, GETXB(),
*(unsigned short*)(instr + 1));
break;
case 3: // $123456
wprintw(debugwin, "$%02x%04x", instr[3], *(unsigned short*)(instr + 1));
wprintw(debugwin, "%12s", padding);
break;
case 4: // $12 : $12+d
wprintw(debugwin, "$%02x%7s[%02x%04x] ", instr[1], padding, 0,
instr[1] + xd);
break;
case 5: // A
wprintw(debugwin, "A%18s", padding);
break;
case 7: // ($12),y
{
wprintw(debugwin, "($%02x),Y ", instr[1]);
wprintw(debugwin, "[nnnnnn] ");
break;
}
case 8: // [$12],y
{
unsigned short addr;
unsigned int t;
wprintw(debugwin, "[$%02x],Y ", instr[1]);
addr = instr[1] + xd;
t = memr8(0, addr);
t |= memr8(0, addr + 1) << 8;
t |= memr8(0, addr + 2) << 16;
t = INDEX_RIGHT(t, xy);
wprintw(debugwin, "[%06x] ", t);
break;
}
case 9: // ($12,x)
{
wprintw(debugwin, "($%02x,X) ", instr[1]);
wprintw(debugwin, "[nnnnnn] ");
break;
}
case 10: // $12,x : $12+d+x
{
wprintw(debugwin, "$%02x,X%5s", instr[1], padding);
wprintw(debugwin, "[%06x] ", INDEX_RIGHT(instr[1] + xd, xx));
break;
}
case 11: // $12,y
{
wprintw(debugwin, "$%02x,Y%5s", instr[1], padding);
wprintw(debugwin, "[%06x] ", INDEX_RIGHT(instr[1] + xd, xy));
break;
}
case 12: // $1234,x : dbr+$1234+x
{
unsigned int t = instr[1] | (instr[2] << 8);
wprintw(debugwin, "$%04x,X ", t);
t = INDEX_RIGHT(t, xx);
wprintw(debugwin, "[%02x%04x] ", xdb, t);
break;
}
case 13: // $1234,y : dbr+$1234+y
{
unsigned int t = instr[1] | (instr[2] << 8);
wprintw(debugwin, "$%04x,Y ", t);
t = INDEX_RIGHT(t, xy);
wprintw(debugwin, "[%02x%04x] ", xdb, t);
break;
}
case 14: // $123456,x : $123456+x
{
unsigned int t = instr[1] | (instr[2] << 8) | (instr[3] << 16);
wprintw(debugwin, "$%06x,X ", t);
t = INDEX_RIGHT(t, xx);
wprintw(debugwin, "[%06x] ", t);
break;
}
case 15: // +-$12 / $1234
{
signed char c = instr[1];
unsigned short t = c + xpc + 2;
wprintw(debugwin, "$%04x%4s [%02x%04x] ", t, padding, xpb, t);
break;
}
case 16: // +-$1234 / $1234
{
unsigned short s = instr[1] | (instr[2] << 8);
unsigned short t = s + xpc + 3;
wprintw(debugwin, "$%04x%4s [%02x%04x] ", t, padding, xpb, t);
break;
}
case 17: // ($1234)
{
wprintw(debugwin, "($%04x) ", instr[1]);
wprintw(debugwin, "[nnnnnn] ");
break;
}
case 18: // ($12)
{
unsigned short addr1, addr2;
wprintw(debugwin, "($%02x)%5s", instr[1], padding);
addr1 = instr[1] + xd;
addr2 = memr8(00, addr1);
addr2 |= memr8(00, addr1 + 1) << 8;
wprintw(debugwin, "[%02x%04x] ", xdb, addr2);
break;
}
case 19: // [$12]
{
wprintw(debugwin, "[$%02x]%5s", instr[1], padding);
wprintw(debugwin, "[nnnnnn] ");
break;
}
case 20: // ($1234,x)
{
unsigned short cx = *(unsigned short*)(instr + 1);
unsigned short x;
wprintw(debugwin, "($%04x,X) [%02x", cx, xpb);
if (xp & 0x10)
cx = (cx & 0xFF00) | ((cx + xx) & 0xFF);
else
cx += xx;
// .out20n
x = memr8(xpb, cx);
x += memr8(xpb, cx + 1) << 8;
wprintw(debugwin, "%04x] ", x);
break;
}
case 22: // d,s
wprintw(debugwin, "$%02x,S%5s", instr[1], padding);
wprintw(debugwin, "[nnnnnn] ");
break;
case 23: // (d,s),y
wprintw(debugwin, "($%02x,S),Y ", instr[1]);
wprintw(debugwin, "[nnnnnn] ");
break;
case 24: // xyc - $1234
wprintw(debugwin, "$%02x%02x%14s", instr[2], instr[1], padding);
break;
case 25: // #$12 (Flag Operations)
wprintw(debugwin, "#$%02x%15s", instr[1], padding);
break;
case 26: // #$12,#$1234 (X-flag)
if (xp & 0x10) {
wprintw(debugwin, "#$%02x%15s", instr[1], padding);
} else {
wprintw(debugwin, "#$%04x%13s",
*(unsigned short*)(instr + 1), padding);
}
break;
case 27: // [$1234]
wprintw(debugwin, "[$%02x%02x] ", instr[2], instr[1]);
break;
default:
wprintw(debugwin, "%15s %02d ", "bad addr mode", addrmode[instr[0]]);
}
}
unsigned char* findoppage()
{
if (xpc & 0x8000) {
return snesmmap[xpb];
} else {
// lower address
if ((xpc < 0x4300) || (memtabler8[xpb] != regaccessbankr8)) {
// lower memory
return snesmap2[xpb];
} else {
// dma
return (u1*)dmadata - 0x4300; // XXX ugly cast
}
}
}
unsigned char* findop()
{
unsigned char* address = findoppage() + xpc;
return address;
}
// print out a 65816 instruction
void out65816()
{
unsigned char *address, opcode;
char opname[5] = "FOO ";
wprintw(debugwin, "%02x%04x ", xpb, xpc);
// is this safe?
address = findop();
opcode = *address;
memcpy(opname, &ocname[opcode * 4], 4);
wprintw(debugwin, "%s", opname);
out65816_addrmode(address);
wprintw(debugwin, "A:%04x X:%04x Y:%04x S:%04x DB:%02x D:%04x P:%02x %c",
xa, xx, xy, xs, xdb, xd, xp, (xe == 1) ? 'E' : 'e');
}
void outsa1()
{
// stub!
}
void nextopcode()
{
attrset(CP(cp_white_on_blue));
move(1, 50);
printw("%11d", numinst);
move(1, 20);
printw("%3d", curcyc);
move(1, 26);
printw("%3d", curypos);
// I don't understand the buffering scheme here... I'm just going
// to hope it isn't really all that important.
// if (debugsa1 != 1)
out65816();
// else
// outputbuffersa1();
}
void cleardisplay()
{
move(0, 0); /* clear(); refresh(); */
endwin();
}
//*******************************************************
// Next SPC Opcode Writes the next opcode & regs
//*******************************************************
// 008000 STZ $123456,x A:0000 X:0000 Y:0000 S:01FF DB:00 D:0000 P:33 E+
void outspc_addrmode()
{
unsigned char mode;
char* format;
char buf[16] = " ";
char* p;
#define HEX8(val) \
do { \
p += sprintf(p, "%02x", val); \
*p = ' '; \
} while (0)
#define HEX16(val) \
do { \
p += sprintf(p, "%04x", val); \
*p = ' '; \
} while (0)
mode = ArgumentTable[spcPCRam[0]];
format = AddressTable[mode];
// memset(buf, ' ', 15); buf[15] = 0;
p = buf;
while (*format) {
if (*format != '%') {
*p++ = *format++;
continue;
}
format++;
switch (*format++) {
case '1': // first byte
HEX8(spcPCRam[1]);
break;
case '2': // second byte
HEX8(spcPCRam[2]);
break;
case '3': // hinib
*p++ = (spcPCRam[0] >> 4) + '0';
*p++ = ' ';
break;
case '4': // hinib2
*p++ = (spcPCRam[0] >> 5) + '0';
*p++ = ' ';
break;
case '5': // rela2pc2
{