-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDEBUGGER.C
3491 lines (2996 loc) · 93.5 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
/*
* debugger.c
*
* This file is part of Emu42
*
* Copyright (C) 2004 Christoph Gießelink
*
*/
#include "pch.h"
#include "resource.h"
#include "Emu42.h"
#include "io.h"
#include "Opcodes.h"
#include "ops.h"
#include "color.h"
#include "disrpl.h"
#include "debugger.h"
#define MAXCODELINES 15 // number of lines in code window
#define MAXMEMLINES 6 // number of lines in memory window
#define MAXMEMITEMS 16 // number of address items in a memory window line
#define MAXBREAKPOINTS 256 // max. number of breakpoints
#define REG_START IDC_REG_A // first register in register update table
#define REG_STOP IDC_MISC_KEY // last register in register update table
#define REG_SIZE (REG_STOP-REG_START+1) // size of register update table
// assert for register update
#define _ASSERTREG(r) _ASSERT(r >= REG_START && r <= REG_STOP)
#define WM_UPDATE (WM_USER+0x1000) // update debugger dialog box
#define MEMWNDMAX (sizeof(nCol) / sizeof(nCol[0]))
#define RT_TOOLBAR MAKEINTRESOURCE(241) // MFC toolbar resource type
#define CODELABEL 0x80000000 // label in code window
typedef struct CToolBarData
{
WORD wVersion;
WORD wWidth;
WORD wHeight;
WORD wItemCount;
WORD aItems[1];
} CToolBarData;
typedef struct // type of breakpoint table
{
BOOL bEnable; // breakpoint enabled
UINT nType; // breakpoint type
DWORD dwAddr; // breakpoint address
} BP_T;
static CONST int nCol[] =
{
IDC_DEBUG_MEM_COL0, IDC_DEBUG_MEM_COL1, IDC_DEBUG_MEM_COL2, IDC_DEBUG_MEM_COL3,
IDC_DEBUG_MEM_COL4, IDC_DEBUG_MEM_COL5, IDC_DEBUG_MEM_COL6, IDC_DEBUG_MEM_COL7
};
static CONST TCHAR cHex[] = { _T('0'),_T('1'),_T('2'),_T('3'),
_T('4'),_T('5'),_T('6'),_T('7'),
_T('8'),_T('9'),_T('A'),_T('B'),
_T('C'),_T('D'),_T('E'),_T('F') };
static INT nDbgPosX = 0; // position of debugger window
static INT nDbgPosY = 0;
static WORD wBreakpointCount = 0; // number of breakpoints
static BP_T sBreakpoint[MAXBREAKPOINTS]; // breakpoint table
static WORD wBackupBreakpointCount = 0; // number of breakpoints
static BP_T sBackupBreakpoint[MAXBREAKPOINTS]; // breakpoint table
static INT nRplBreak; // RPL breakpoint
static DWORD dwAdrLine[MAXCODELINES]; // addresses of disassember lines in code window
static DWORD dwAdrMem = 0; // start address of memory window
static UINT uIDFol = ID_DEBUG_MEM_FNONE; // follow mode
static DWORD dwAdrMemFol = 0; // follow address memory window
static UINT uIDMap = ID_DEBUG_MEM_MAP; // current memory view mode
static LONG lCharWidth; // width of a character (is a fix font)
static HMENU hMenuCode,hMenuMem,hMenuStack;// handle of context menues
static HWND hWndToolbar; // toolbar handle
static DWORD dwDbgRefCycles; // cpu cycles counter from last opcode
static CHIPSET_M OldChipset; // old chipset content of master (CPU)
static BOOL bRegUpdate[REG_SIZE]; // register update table
static HBITMAP hBmpCheckBox; // checked and unchecked bitmap
static HFONT hFontBold; // bold font for symbol labels in code window
// function prototypes
static BOOL OnMemFind(HWND hDlg);
static BOOL OnProfile(HWND hDlg);
static VOID UpdateRplObjViewWnd(HWND hDlg, DWORD dwAddr);
static BOOL OnRplObjView(HWND hDlg);
static BOOL OnSettings(HWND hDlg);
static INT_PTR OnNewValue(LPTSTR lpszValue);
static VOID OnEnterAddress(HWND hDlg, DWORD *dwValue);
static BOOL OnEditBreakpoint(HWND hDlg);
static BOOL OnInfoIntr(HWND hDlg);
static VOID UpdateProfileWnd(HWND hDlg);
static BOOL OnMemLoadData(HWND hDlg);
static BOOL OnMemSaveData(HWND hDlg);
//################
//#
//# Low level subroutines
//#
//################
//
// load external rpl symbol table
//
static BOOL LoadSymbTable(VOID)
{
TCHAR szSymbFilename[MAX_PATH];
TCHAR szItemname[16];
wsprintf(szItemname,_T("Symb%c"),(TCHAR) cCurrentRomType);
ReadSettingsString(_T("Disassembler"),szItemname,_T(""),szSymbFilename,ARRAYSIZEOF(szSymbFilename));
if (*szSymbFilename == 0) // no reference table defined
return FALSE; // no success
return RplLoadTable(szSymbFilename); // load external reference table
}
//
// disable menu keys
//
static VOID DisableMenuKeys(HWND hDlg)
{
HMENU hMenu = GetMenu(hDlg);
EnableMenuItem(hMenu,ID_DEBUG_RUN,MF_GRAYED);
EnableMenuItem(hMenu,ID_DEBUG_RUNCURSOR,MF_GRAYED);
EnableMenuItem(hMenu,ID_DEBUG_STEP,MF_GRAYED);
EnableMenuItem(hMenu,ID_DEBUG_STEPOVER,MF_GRAYED);
EnableMenuItem(hMenu,ID_DEBUG_STEPOUT,MF_GRAYED);
EnableMenuItem(hMenu,ID_INFO_LASTINSTRUCTIONS,MF_GRAYED);
SendMessage(hWndToolbar,TB_ENABLEBUTTON,ID_DEBUG_RUN,MAKELONG((FALSE),0));
SendMessage(hWndToolbar,TB_ENABLEBUTTON,ID_DEBUG_BREAK,MAKELONG((TRUE),0));
SendMessage(hWndToolbar,TB_ENABLEBUTTON,ID_DEBUG_RUNCURSOR,MAKELONG((FALSE),0));
SendMessage(hWndToolbar,TB_ENABLEBUTTON,ID_DEBUG_STEP,MAKELONG((FALSE),0));
SendMessage(hWndToolbar,TB_ENABLEBUTTON,ID_DEBUG_STEPOVER,MAKELONG((FALSE),0));
SendMessage(hWndToolbar,TB_ENABLEBUTTON,ID_DEBUG_STEPOUT,MAKELONG((FALSE),0));
return;
}
//
// read edit control and decode content as hex number or if enabled as symbol name
//
static BOOL GetAddr(HWND hDlg,INT nID,DWORD *pdwAddr,DWORD dwMaxAddr,BOOL bSymbEnable)
{
TCHAR szBuffer[48];
INT i;
BOOL bSucc = TRUE;
HWND hWnd = GetDlgItem(hDlg,nID);
GetWindowText(hWnd,szBuffer,ARRAYSIZEOF(szBuffer));
if (*szBuffer != 0)
{
// if address is not a symbol name decode number
if ( !bSymbEnable || szBuffer[0] != _T('=')
|| RplGetAddr(&szBuffer[1],pdwAddr))
{
// test if valid hex address
for (i = 0; bSucc && i < (LONG) lstrlen(szBuffer); ++i)
{
bSucc = (_istxdigit(szBuffer[i]) != 0);
}
if (bSucc) // valid characters
{
// convert string to number
*pdwAddr = _tcstoul(szBuffer,NULL,16);
}
}
// inside address range?
bSucc = bSucc && (*pdwAddr <= dwMaxAddr);
if (!bSucc) // invalid address
{
SendMessage(hWnd,EM_SETSEL,0,-1);
SetFocus(hWnd); // focus to edit control
}
}
return bSucc;
}
//
// set mapping menu
//
static VOID SetMappingMenu(HWND hDlg,UINT uID)
{
enum MEM_MAPPING eMode;
LPTSTR szCaption;
UINT uEnable = MF_GRAYED; // disable Memory Data menu items
CheckMenuItem(hMenuMem,uIDMap,MF_UNCHECKED);
switch (uID)
{
case ID_DEBUG_MEM_MAP:
szCaption = _T("Memory");
eMode = MEM_MMU;
uEnable = MF_ENABLED; // enable Memory Data menu items
break;
case ID_DEBUG_MEM_NCE1_M:
szCaption = _T("Memory (NCE1 master)");
eMode = MEM_NCE1_M;
break;
case ID_DEBUG_MEM_NCE2_M:
szCaption = _T("Memory (NCE2 master)");
eMode = MEM_NCE2_M;
break;
case ID_DEBUG_MEM_NCE3_M:
szCaption = _T("Memory (NCE3 master)");
eMode = MEM_NCE3_M;
break;
case ID_DEBUG_MEM_NCE1_S:
szCaption = _T("Memory (NCE1 slave)");
eMode = MEM_NCE1_S;
break;
case ID_DEBUG_MEM_NCE2_S:
szCaption = _T("Memory (NCE2 slave)");
eMode = MEM_NCE2_S;
break;
case ID_DEBUG_MEM_NCE3_S:
szCaption = _T("Memory (NCE3 slave)");
eMode = MEM_NCE3_S;
break;
default: _ASSERT(0);
}
VERIFY(SetMemMapType(eMode));
if (uIDMap != uID) dwAdrMem = 0; // view from address 0
uIDMap = uID;
CheckMenuItem(hMenuMem,uIDMap,MF_CHECKED);
EnableMenuItem(hMenuMem,ID_DEBUG_MEM_LOAD,uEnable);
EnableMenuItem(hMenuMem,ID_DEBUG_MEM_SAVE,uEnable);
SetDlgItemText(hDlg,IDC_STATIC_MEMORY,szCaption);
return;
};
//
// get address of cursor in memory window
//
static DWORD GetMemCurAddr(HWND hDlg)
{
INT i,nPos;
DWORD dwAddr = dwAdrMem;
DWORD dwMapDataMask = GetMemDataMask();
for (i = 0; i < MEMWNDMAX; ++i) // scan all memory cols
{
// column has cursor
if ((nPos = (INT) SendDlgItemMessage(hDlg,nCol[i],LB_GETCURSEL,0,0)) != LB_ERR)
{
dwAddr += (DWORD) (nPos * MEMWNDMAX + i) * 2;
dwAddr &= dwMapDataMask;
break;
}
}
return dwAddr;
}
//
// set/reset breakpoint
//
static __inline VOID ToggleBreakpoint(DWORD dwAddr)
{
INT i;
for (i = 0; i < wBreakpointCount; ++i) // scan all breakpoints
{
// code breakpoint found
if (sBreakpoint[i].nType == BP_EXEC && sBreakpoint[i].dwAddr == dwAddr)
{
if (!sBreakpoint[i].bEnable) // breakpoint disabled
{
sBreakpoint[i].bEnable = TRUE;
return;
}
while (++i < wBreakpointCount) // purge breakpoint
sBreakpoint[i-1] = sBreakpoint[i];
--wBreakpointCount;
return;
}
}
// breakpoint not found
if (wBreakpointCount >= MAXBREAKPOINTS) // breakpoint buffer full
{
AbortMessage(_T("Reached maximum number of breakpoints !"));
return;
}
sBreakpoint[wBreakpointCount].bEnable = TRUE;
sBreakpoint[wBreakpointCount].nType = BP_EXEC;
sBreakpoint[wBreakpointCount].dwAddr = dwAddr;
++wBreakpointCount;
return;
}
//
// init memory mapping table and mapping menu entry
//
static __inline VOID InitMemMap(HWND hDlg)
{
BOOL bActive;
SetMemRomType(cCurrentRomType); // set current model
_ASSERT(hMenuMem);
// enable menu mappings
EnableMenuItem(hMenuMem,ID_DEBUG_MEM_NCE1_M,GetMemAvail(MEM_NCE1_M) ? MF_ENABLED : MF_GRAYED);
EnableMenuItem(hMenuMem,ID_DEBUG_MEM_NCE2_M,GetMemAvail(MEM_NCE2_M) ? MF_ENABLED : MF_GRAYED);
EnableMenuItem(hMenuMem,ID_DEBUG_MEM_NCE3_M,GetMemAvail(MEM_NCE3_M) ? MF_ENABLED : MF_GRAYED);
EnableMenuItem(hMenuMem,ID_DEBUG_MEM_NCE1_S,GetMemAvail(MEM_NCE1_S) ? MF_ENABLED : MF_GRAYED);
EnableMenuItem(hMenuMem,ID_DEBUG_MEM_NCE2_S,GetMemAvail(MEM_NCE2_S) ? MF_ENABLED : MF_GRAYED);
EnableMenuItem(hMenuMem,ID_DEBUG_MEM_NCE3_S,GetMemAvail(MEM_NCE3_S) ? MF_ENABLED : MF_GRAYED);
bActive = (ID_DEBUG_MEM_NCE1_M == uIDMap && GetMemAvail(MEM_NCE1_M))
|| (ID_DEBUG_MEM_NCE2_M == uIDMap && GetMemAvail(MEM_NCE2_M))
|| (ID_DEBUG_MEM_NCE3_M == uIDMap && GetMemAvail(MEM_NCE3_M))
|| (ID_DEBUG_MEM_NCE1_S == uIDMap && GetMemAvail(MEM_NCE1_S))
|| (ID_DEBUG_MEM_NCE2_S == uIDMap && GetMemAvail(MEM_NCE2_S))
|| (ID_DEBUG_MEM_NCE3_S == uIDMap && GetMemAvail(MEM_NCE3_S));
SetMappingMenu(hDlg,bActive ? uIDMap : ID_DEBUG_MEM_MAP);
return;
}
//
// convert nibble register to string
//
static LPTSTR RegToStr(BYTE *pReg, WORD wNib)
{
static TCHAR szBuffer[32];
WORD i;
for (i = 0;i < wNib;++i)
szBuffer[i] = cHex[pReg[wNib-i-1]];
szBuffer[i] = 0;
return szBuffer;
}
//
// convert string to nibble register
//
static VOID StrToReg(BYTE *pReg, WORD wNib, LPTSTR lpszValue)
{
int i,nValuelen;
nValuelen = lstrlen(lpszValue);
for (i = wNib - 1;i >= 0;--i)
{
if (i >= nValuelen) // no character in string
{
pReg[i] = 0; // fill with zero
}
else
{
// convert to number
pReg[i] = _totupper(*lpszValue) - _T('0');
if (pReg[i] > 9) pReg[i] -= _T('A') - _T('9') - 1;
++lpszValue;
}
}
return;
}
//
// write code window
//
static INT ViewCodeWnd(HWND hWnd, DWORD dwAddress)
{
enum MEM_MAPPING eMapMode;
LPCTSTR lpszName;
TCHAR szAddress[64];
DWORD dwNxtAddr;
INT i,j,nLinePC;
nLinePC = -1; // PC not shown (no selection)
eMapMode = GetMemMapType(); // get current map mode
SetMemMapType(MEM_MMU); // disassemble in mapped mode
dwAddress &= 0xFFFFF; // adjust to Saturn address range
SendMessage(hWnd,WM_SETREDRAW,FALSE,0);
SendMessage(hWnd,LB_RESETCONTENT,0,0);
for (i = 0; i < MAXCODELINES; ++i)
{
// entry has a name
if (disassembler_symb && (lpszName = RplGetName(dwAddress)) != NULL)
{
// save address as label
dwAdrLine[i] = dwAddress | CODELABEL;
szAddress[0] = _T('=');
lstrcpyn(&szAddress[1],lpszName,ARRAYSIZEOF(szAddress)-1);
SendMessage(hWnd,LB_ADDSTRING,0,(LPARAM) szAddress);
if (++i == MAXCODELINES) break;
}
// remember line of PC
if (dwAddress == Chipset.pc) nLinePC = i;
dwAdrLine[i] = dwAddress;
j = wsprintf(szAddress,
(dwAddress == Chipset.pc) ? _T("%05lX-%c ") : _T("%05lX "),
dwAddress,nRplBreak ? _T('R') : _T('>'));
dwNxtAddr = (dwAddress + 5) & 0xFFFFF;
// check if address content is a PCO (Primitive Code Object)
// make sure when the PC points to such an address that it's
// interpreted as opcode
if ( (nCurrentHardware == HDW_LEWIS)
&& (dwAddress != Chipset.pc) && (Read5(dwAddress) == dwNxtAddr))
{
if (disassembler_mode == HP_MNEMONICS)
{
_tcscat(&szAddress[j],_T("CON(5) (*)+5"));
}
else
{
wsprintf(&szAddress[j],_T("dcr.5 $%05x"),dwNxtAddr);
}
dwAddress = dwNxtAddr;
}
else
{
dwAddress = disassemble(dwAddress,&szAddress[j]);
}
SendMessage(hWnd,LB_ADDSTRING,0,(LPARAM) szAddress);
}
SendMessage(hWnd,WM_SETREDRAW,TRUE,0);
SetMemMapType(eMapMode); // switch back to old map mode
return nLinePC;
}
//
// write memory window
//
static VOID ViewMemWnd(HWND hDlg, DWORD dwAddress)
{
#define TEXTOFF 32
INT i,j;
TCHAR szBuffer[16],szItem[4];
DWORD dwMapDataMask;
BYTE cChar;
szItem[2] = 0; // end of string
dwAdrMem = dwAddress; // save start address of memory window
dwMapDataMask = GetMemDataMask(); // size mask of data mapping
// purge all list boxes
SendDlgItemMessage(hDlg,IDC_DEBUG_MEM_ADDR,LB_RESETCONTENT,0,0);
SendDlgItemMessage(hDlg,IDC_DEBUG_MEM_TEXT,LB_RESETCONTENT,0,0);
for (j = 0; j < MEMWNDMAX; ++j)
SendDlgItemMessage(hDlg,nCol[j],LB_RESETCONTENT,0,0);
for (i = 0; i < MAXMEMLINES; ++i)
{
BYTE byLineData[MAXMEMITEMS];
wsprintf(szBuffer,_T("%05lX"),dwAddress);
SendDlgItemMessage(hDlg,IDC_DEBUG_MEM_ADDR,LB_ADDSTRING,0,(LPARAM) szBuffer);
// fetch data line
GetMemPeek(byLineData, dwAddress, MAXMEMITEMS);
for (j = 0; j < MAXMEMITEMS; ++j)
{
// read from fetched data line
szItem[j&0x1] = cHex[byLineData[j]];
// characters are saved in LBS, MSB order
cChar = (cChar >> 4) | (byLineData[j] << 4);
if ((j&0x1) != 0)
{
// byte field
_ASSERT(j/2 < MEMWNDMAX);
SendDlgItemMessage(hDlg,nCol[j/2],LB_ADDSTRING,0,(LPARAM) szItem);
// text field
szBuffer[j/2] = (isprint(cChar) != 0) ? cChar : _T('.');
}
}
szBuffer[j/2] = 0; // end of text string
SendDlgItemMessage(hDlg,IDC_DEBUG_MEM_TEXT,LB_ADDSTRING,0,(LPARAM) szBuffer);
dwAddress = (dwAddress + MAXMEMITEMS) & dwMapDataMask;
}
return;
#undef TEXTOFF
}
//################
//#
//# High level Window draw routines
//#
//################
//
// update code window with scrolling
//
static VOID UpdateCodeWnd(HWND hDlg)
{
DWORD dwAddress,dwPriorAddr;
INT i,j;
HWND hWnd = GetDlgItem(hDlg,IDC_DEBUG_CODE);
j = (INT) SendMessage(hWnd,LB_GETCOUNT,0,0); // no. of items in table
// seach for actual address in code area
for (i = 0; i < j; ++i)
{
if (dwAdrLine[i] == Chipset.pc) // found new pc address line
break;
}
// redraw code window
dwAddress = dwAdrLine[0]; // redraw list box with modified pc
if (i == j) // address not found
{
dwAddress = Chipset.pc; // begin with actual pc
if (nCurrentHardware == HDW_LEWIS) // check for PCO on claculators with Lewis hardware
{
// check if current pc is the begin of a PCO, show PCO address
dwPriorAddr = (dwAddress - 5) & 0xFFFFF;
if (Read5(dwPriorAddr) == dwAddress)
dwAddress = dwPriorAddr;
}
}
else
{
if (i > 10) // cursor near bottom line
{
j = 10; // pc to line 11
// new address line is label
if ((dwAdrLine[i-11] & CODELABEL) != 0)
--j; // pc to line 10
dwAddress = dwAdrLine[i-j]; // move that pc is in line 11
}
}
i = ViewCodeWnd(hWnd,dwAddress); // init code area
SendMessage(hWnd,LB_SETCURSEL,i,0); // set cursor on actual pc
return;
}
//
// update register window
//
static VOID UpdateRegisterWnd(HWND hDlg)
{
TCHAR szBuffer[64];
_ASSERTREG(IDC_REG_A);
bRegUpdate[IDC_REG_A-REG_START] = memcmp(Chipset.A, OldChipset.A, sizeof(Chipset.A)) != 0;
wsprintf(szBuffer,_T("A= %s"),RegToStr(Chipset.A,16));
SetDlgItemText(hDlg,IDC_REG_A,szBuffer);
_ASSERTREG(IDC_REG_B);
bRegUpdate[IDC_REG_B-REG_START] = memcmp(Chipset.B, OldChipset.B, sizeof(Chipset.B)) != 0;
wsprintf(szBuffer,_T("B= %s"),RegToStr(Chipset.B,16));
SetDlgItemText(hDlg,IDC_REG_B,szBuffer);
_ASSERTREG(IDC_REG_C);
bRegUpdate[IDC_REG_C-REG_START] = memcmp(Chipset.C, OldChipset.C, sizeof(Chipset.C)) != 0;
wsprintf(szBuffer,_T("C= %s"),RegToStr(Chipset.C,16));
SetDlgItemText(hDlg,IDC_REG_C,szBuffer);
_ASSERTREG(IDC_REG_D);
bRegUpdate[IDC_REG_D-REG_START] = memcmp(Chipset.D, OldChipset.D, sizeof(Chipset.D)) != 0;
wsprintf(szBuffer,_T("D= %s"),RegToStr(Chipset.D,16));
SetDlgItemText(hDlg,IDC_REG_D,szBuffer);
_ASSERTREG(IDC_REG_R0);
bRegUpdate[IDC_REG_R0-REG_START] = memcmp(Chipset.R0, OldChipset.R0, sizeof(Chipset.R0)) != 0;
wsprintf(szBuffer,_T("R0=%s"),RegToStr(Chipset.R0,16));
SetDlgItemText(hDlg,IDC_REG_R0,szBuffer);
_ASSERTREG(IDC_REG_R1);
bRegUpdate[IDC_REG_R1-REG_START] = memcmp(Chipset.R1, OldChipset.R1, sizeof(Chipset.R1)) != 0;
wsprintf(szBuffer,_T("R1=%s"),RegToStr(Chipset.R1,16));
SetDlgItemText(hDlg,IDC_REG_R1,szBuffer);
_ASSERTREG(IDC_REG_R2);
bRegUpdate[IDC_REG_R2-REG_START] = memcmp(Chipset.R2, OldChipset.R2, sizeof(Chipset.R2)) != 0;
wsprintf(szBuffer,_T("R2=%s"),RegToStr(Chipset.R2,16));
SetDlgItemText(hDlg,IDC_REG_R2,szBuffer);
_ASSERTREG(IDC_REG_R3);
bRegUpdate[IDC_REG_R3-REG_START] = memcmp(Chipset.R3, OldChipset.R3, sizeof(Chipset.R3)) != 0;
wsprintf(szBuffer,_T("R3=%s"),RegToStr(Chipset.R3,16));
SetDlgItemText(hDlg,IDC_REG_R3,szBuffer);
_ASSERTREG(IDC_REG_R4);
bRegUpdate[IDC_REG_R4-REG_START] = memcmp(Chipset.R4, OldChipset.R4, sizeof(Chipset.R4)) != 0;
wsprintf(szBuffer,_T("R4=%s"),RegToStr(Chipset.R4,16));
SetDlgItemText(hDlg,IDC_REG_R4,szBuffer);
_ASSERTREG(IDC_REG_D0);
bRegUpdate[IDC_REG_D0-REG_START] = Chipset.d0 != OldChipset.d0;
wsprintf(szBuffer,_T("D0=%05X"),Chipset.d0);
SetDlgItemText(hDlg,IDC_REG_D0,szBuffer);
_ASSERTREG(IDC_REG_D1);
bRegUpdate[IDC_REG_D1-REG_START] = Chipset.d1 != OldChipset.d1;
wsprintf(szBuffer,_T("D1=%05X"),Chipset.d1);
SetDlgItemText(hDlg,IDC_REG_D1,szBuffer);
_ASSERTREG(IDC_REG_P);
bRegUpdate[IDC_REG_P-REG_START] = Chipset.P != OldChipset.P;
wsprintf(szBuffer,_T("P=%X"),Chipset.P);
SetDlgItemText(hDlg,IDC_REG_P,szBuffer);
_ASSERTREG(IDC_REG_PC);
bRegUpdate[IDC_REG_PC-REG_START] = Chipset.pc != OldChipset.pc;
wsprintf(szBuffer,_T("PC=%05X"),Chipset.pc);
SetDlgItemText(hDlg,IDC_REG_PC,szBuffer);
_ASSERTREG(IDC_REG_OUT);
bRegUpdate[IDC_REG_OUT-REG_START] = Chipset.out != OldChipset.out;
wsprintf(szBuffer,_T("OUT=%03X"),Chipset.out);
SetDlgItemText(hDlg,IDC_REG_OUT,szBuffer);
_ASSERTREG(IDC_REG_IN);
bRegUpdate[IDC_REG_IN-REG_START] = Chipset.in != OldChipset.in;
wsprintf(szBuffer,_T("IN=%04X"),Chipset.in);
SetDlgItemText(hDlg,IDC_REG_IN,szBuffer);
_ASSERTREG(IDC_REG_ST);
bRegUpdate[IDC_REG_ST-REG_START] = memcmp(Chipset.ST, OldChipset.ST, sizeof(Chipset.ST)) != 0;
wsprintf(szBuffer,_T("ST=%s"),RegToStr(Chipset.ST,4));
SetDlgItemText(hDlg,IDC_REG_ST,szBuffer);
_ASSERTREG(IDC_REG_CY);
bRegUpdate[IDC_REG_CY-REG_START] = Chipset.carry != OldChipset.carry;
wsprintf(szBuffer,_T("CY=%d"),Chipset.carry);
SetDlgItemText(hDlg,IDC_REG_CY,szBuffer);
_ASSERTREG(IDC_REG_MODE);
bRegUpdate[IDC_REG_MODE-REG_START] = Chipset.mode_dec != OldChipset.mode_dec;
wsprintf(szBuffer,_T("Mode=%c"),Chipset.mode_dec ? _T('D') : _T('H'));
SetDlgItemText(hDlg,IDC_REG_MODE,szBuffer);
_ASSERTREG(IDC_REG_MP);
bRegUpdate[IDC_REG_MP-REG_START] = ((Chipset.HST ^ OldChipset.HST) & MP) != 0;
wsprintf(szBuffer,_T("MP=%d"),(Chipset.HST & MP) != 0);
SetDlgItemText(hDlg,IDC_REG_MP,szBuffer);
_ASSERTREG(IDC_REG_SR);
bRegUpdate[IDC_REG_SR-REG_START] = ((Chipset.HST ^ OldChipset.HST) & SR) != 0;
wsprintf(szBuffer,_T("SR=%d"),(Chipset.HST & SR) != 0);
SetDlgItemText(hDlg,IDC_REG_SR,szBuffer);
_ASSERTREG(IDC_REG_SB);
bRegUpdate[IDC_REG_SB-REG_START] = ((Chipset.HST ^ OldChipset.HST) & SB) != 0;
wsprintf(szBuffer,_T("SB=%d"),(Chipset.HST & SB) != 0);
SetDlgItemText(hDlg,IDC_REG_SB,szBuffer);
_ASSERTREG(IDC_REG_XM);
bRegUpdate[IDC_REG_XM-REG_START] = ((Chipset.HST ^ OldChipset.HST) & XM) != 0;
wsprintf(szBuffer,_T("XM=%d"),(Chipset.HST & XM) != 0);
SetDlgItemText(hDlg,IDC_REG_XM,szBuffer);
return;
}
//
// update memory window
//
static VOID UpdateMemoryWnd(HWND hDlg)
{
// check follow mode setting for memory window
switch(uIDFol)
{
case ID_DEBUG_MEM_FNONE: break;
case ID_DEBUG_MEM_FADDR: dwAdrMem = Read5(dwAdrMemFol); break;
case ID_DEBUG_MEM_FPC: dwAdrMem = Chipset.pc; break;
case ID_DEBUG_MEM_FD0: dwAdrMem = Chipset.d0; break;
case ID_DEBUG_MEM_FD1: dwAdrMem = Chipset.d1; break;
default: _ASSERT(FALSE);
}
ViewMemWnd(hDlg,dwAdrMem);
return;
}
//
// update stack window
//
static VOID UpdateStackWnd(HWND hDlg)
{
INT i;
LONG nPos;
TCHAR szBuffer[64];
HWND hWnd = GetDlgItem(hDlg,IDC_DEBUG_STACK);
SendMessage(hWnd,WM_SETREDRAW,FALSE,0);
nPos = (LONG) SendMessage(hWnd,LB_GETTOPINDEX,0,0);
SendMessage(hWnd,LB_RESETCONTENT,0,0);
for (i = 1; i <= ARRAYSIZEOF(Chipset.rstk); ++i)
{
INT j;
wsprintf(szBuffer,_T("%d: %05X"), i, Chipset.rstk[(Chipset.rstkp-i)&7]);
j = (INT) SendMessage(hWnd,LB_ADDSTRING,0,(LPARAM) szBuffer);
SendMessage(hWnd,LB_SETITEMDATA,j,Chipset.rstk[(Chipset.rstkp-i)&7]);
}
SendMessage(hWnd,LB_SETTOPINDEX,nPos,0);
SendMessage(hWnd,WM_SETREDRAW,TRUE,0);
return;
}
//
// update miscellaneous window
//
static VOID UpdateMiscWnd(HWND hDlg)
{
_ASSERTREG(IDC_MISC_INT);
bRegUpdate[IDC_MISC_INT-REG_START] = Chipset.inte != OldChipset.inte;
SetDlgItemText(hDlg,IDC_MISC_INT,Chipset.inte ? _T("On ") : _T("Off"));
_ASSERTREG(IDC_MISC_KEY);
bRegUpdate[IDC_MISC_KEY-REG_START] = Chipset.intk != OldChipset.intk;
SetDlgItemText(hDlg,IDC_MISC_KEY,Chipset.intk ? _T("On ") : _T("Off"));
return;
}
//
// update complete debugger dialog
//
VOID OnUpdate(HWND hDlg)
{
nDbgState = DBG_STEPINTO; // state "step into"
dwDbgStopPC = -1; // disable "cursor stop address"
// enable debug buttons
EnableMenuItem(GetMenu(hDlg),ID_DEBUG_RUN,MF_ENABLED);
EnableMenuItem(GetMenu(hDlg),ID_DEBUG_RUNCURSOR,MF_ENABLED);
EnableMenuItem(GetMenu(hDlg),ID_DEBUG_STEP,MF_ENABLED);
EnableMenuItem(GetMenu(hDlg),ID_DEBUG_STEPOVER,MF_ENABLED);
EnableMenuItem(GetMenu(hDlg),ID_DEBUG_STEPOUT,MF_ENABLED);
EnableMenuItem(GetMenu(hDlg),ID_INFO_LASTINSTRUCTIONS,MF_ENABLED);
// enable toolbar buttons
SendMessage(hWndToolbar,TB_ENABLEBUTTON,ID_DEBUG_RUN,MAKELONG((TRUE),0));
SendMessage(hWndToolbar,TB_ENABLEBUTTON,ID_DEBUG_BREAK,MAKELONG((FALSE),0));
SendMessage(hWndToolbar,TB_ENABLEBUTTON,ID_DEBUG_RUNCURSOR,MAKELONG((TRUE),0));
SendMessage(hWndToolbar,TB_ENABLEBUTTON,ID_DEBUG_STEP,MAKELONG((TRUE),0));
SendMessage(hWndToolbar,TB_ENABLEBUTTON,ID_DEBUG_STEPOVER,MAKELONG((TRUE),0));
SendMessage(hWndToolbar,TB_ENABLEBUTTON,ID_DEBUG_STEPOUT,MAKELONG((TRUE),0));
// update windows
UpdateCodeWnd(hDlg); // update code window
UpdateRegisterWnd(hDlg); // update registers window
UpdateMemoryWnd(hDlg); // update memory window
UpdateStackWnd(hDlg); // update stack window
UpdateMiscWnd(hDlg); // update bank switcher window
UpdateProfileWnd(hDlgProfile); // update profiler dialog
ShowWindow(hDlg,SW_RESTORE); // pop up if minimized
SetFocus(hDlg); // set focus to debugger
return;
}
//################
//#
//# Virtual key handler
//#
//################
//
// toggle breakpoint key handler (F2)
//
static BOOL OnKeyF2(HWND hDlg)
{
HWND hWnd;
RECT rc;
LONG i;
hWnd = GetDlgItem(hDlg,IDC_DEBUG_CODE);
i = (LONG) SendMessage(hWnd,LB_GETCURSEL,0,0); // get selected item
ToggleBreakpoint(dwAdrLine[i]); // toggle breakpoint at address
// update region of toggled item
SendMessage(hWnd,LB_GETITEMRECT,i,(LPARAM)&rc);
InvalidateRect(hWnd,&rc,TRUE);
return -1; // call windows default handler
}
//
// run key handler (F5)
//
static BOOL OnKeyF5(HWND hDlg)
{
HWND hWnd;
INT i,nPos;
TCHAR szBuf[64];
if (nDbgState != DBG_RUN) // emulation stopped
{
DisableMenuKeys(hDlg); // disable menu keys
hWnd = GetDlgItem(hDlg,IDC_DEBUG_CODE);
nPos = (INT) SendMessage(hWnd,LB_GETCURSEL,0,0);
// clear "->" in code window
for (i = 0; i < MAXCODELINES; ++i)
{
if (dwAdrLine[i] == Chipset.pc) // PC in window
{
SendMessage(hWnd,LB_GETTEXT,i,(LPARAM) szBuf);
szBuf[5] = szBuf[6] = _T(' ');
SendMessage(hWnd,LB_DELETESTRING,i,0);
SendMessage(hWnd,LB_INSERTSTRING,i,(LPARAM) szBuf);
break;
}
}
SendMessage(hWnd,LB_SETCURSEL,nPos,0);
nDbgState = DBG_RUN; // state "run"
OldChipset = Chipset; // save chipset values
SetEvent(hEventDebug); // run emulation
}
return -1; // call windows default handler
UNREFERENCED_PARAMETER(hDlg);
}
//
// step cursor key handler (F6)
//
static BOOL OnKeyF6(HWND hDlg)
{
if (nDbgState != DBG_RUN) // emulation stopped
{
// get address of selected item
INT nPos = (INT) SendDlgItemMessage(hDlg,IDC_DEBUG_CODE,LB_GETCURSEL,0,0);
dwDbgStopPC = dwAdrLine[nPos];
OnKeyF5(hDlg); // run emulation
}
return -1; // call windows default handler
}
//
// step into key handler (F7)
//
static BOOL OnKeyF7(HWND hDlg)
{
if (nDbgState != DBG_RUN) // emulation stopped
{
if (bDbgSkipInt) // skip code in interrupt handler
DisableMenuKeys(hDlg); // disable menu keys
nDbgState = DBG_STEPINTO; // state "step into"
OldChipset = Chipset; // save chipset values
SetEvent(hEventDebug); // run emulation
}
return -1; // call windows default handler
UNREFERENCED_PARAMETER(hDlg);
}
//
// step over key handler (F8)
//
static BOOL OnKeyF8(HWND hDlg)
{
if (nDbgState != DBG_RUN) // emulation stopped
{
LPBYTE I = FASTPTR(Chipset.pc);
if (bDbgSkipInt) // skip code in interrupt handler
DisableMenuKeys(hDlg); // disable menu keys
dwDbgRstkp = Chipset.rstkp; // save stack level
// GOSUB 7aaa, GOSUBL 8Eaaaa, GOSBVL 8Faaaaa
if (I[0] == 0x7 || (I[0] == 0x8 && (I[1] == 0xE || I[1] == 0xF)))
{
nDbgState = DBG_STEPOVER; // state "step over"
}
else
{
nDbgState = DBG_STEPINTO; // state "step into"
}
OldChipset = Chipset; // save chipset values
SetEvent(hEventDebug); // run emulation
}
return -1; // call windows default handler
UNREFERENCED_PARAMETER(hDlg);
}
//
// step out key handler (F9)
//
static BOOL OnKeyF9(HWND hDlg)
{
if (nDbgState != DBG_RUN) // emulation stopped
{
DisableMenuKeys(hDlg); // disable menu keys
dwDbgRstkp = (Chipset.rstkp-1)&7; // save stack data
dwDbgRstk = Chipset.rstk[dwDbgRstkp];
nDbgState = DBG_STEPOUT; // state "step out"
OldChipset = Chipset; // save chipset values
SetEvent(hEventDebug); // run emulation
}
return -1; // call windows default handler
UNREFERENCED_PARAMETER(hDlg);
}
//
// break key handler (F11)
//
static BOOL OnKeyF11(HWND hDlg)
{
nDbgState = DBG_STEPINTO; // state "step into"
if (Chipset.Shutdn) // cpu thread stopped
SetEvent(hEventShutdn); // goto debug session
return -1; // call windows default handler
UNREFERENCED_PARAMETER(hDlg);
}
//
// view of given address in disassembler window
//
static BOOL OnCodeGoAdr(HWND hDlg)
{
DWORD dwAddress = -1; // no address given
OnEnterAddress(hDlg, &dwAddress);
if (dwAddress != -1)
{
HWND hWnd = GetDlgItem(hDlg,IDC_DEBUG_CODE);
ViewCodeWnd(hWnd,dwAddress);
SendMessage(hWnd,LB_SETCURSEL,0,0);
}
return -1; // call windows default handler
}
//
// view pc in disassembler window
//
static BOOL OnCodeGoPC(HWND hDlg)
{
UpdateCodeWnd(hDlg);
return 0;
}
//
// set pc to selction
//
static BOOL OnCodeSetPcToSelection(HWND hDlg)
{
Chipset.pc = dwAdrLine[SendDlgItemMessage(hDlg,IDC_DEBUG_CODE,LB_GETCURSEL,0,0)];
return OnCodeGoPC(hDlg);
}
//
// find PCO object in code window
//
static BOOL OnCodeFindPCO(HWND hDlg,DWORD dwAddr,INT nSearchDir)
{
DWORD dwCnt;
BOOL bMatch;
// searching upwards / downwards
_ASSERT(nSearchDir == 1 || nSearchDir == -1);
dwAddr += nSearchDir; // start address for search
// scan mapped address area until PCO found
for (dwCnt = 0; dwCnt <= 0xFFFFF; ++dwCnt)
{
// is this a PCO?
bMatch = (Read5(dwAddr & 0xFFFFF) == ((dwAddr + 5) & 0xFFFFF));
if (bMatch)
{
// update code window
ViewCodeWnd(GetDlgItem(hDlg,IDC_DEBUG_CODE),dwAddr);
break;
}
dwAddr += nSearchDir;
}
return 0;
}
//