Repository navigation
Expand file tree
/
Copy pathMemoryMap.cpp
More file actions
2129 lines (1844 loc) · 53.6 KB
/
Copy pathMemoryMap.cpp
File metadata and controls
2129 lines (1844 loc) · 53.6 KB
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
//------------------------------------------------------------------
// Display VCC memory for debugging
//
// This file is part of VCC (Virtual Color Computer).
// Copyright 2015 by Joseph Forgione
//
// VCC (Virtual Color Computer) is free software: you can redistribute
// it and/or modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation, either version 3 of the
// License, or (at your option) any later version.
//
// VCC (Virtual Color Computer) 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 VCC (Virtual Color Computer). If not, see
// <http://www.gnu.org/licenses/>.
//
// Memory Map Display - Part of the Debugger package for VCC
// Authors: Mike Rojas, Chet Simpson
// New Enhanced Version 2026/08/01: Craig Allsop, EJJaquay
//------------------------------------------------------------------
#include "MemoryMap.h"
#include "Debugger.h"
#include "DebuggerUtils.h"
#include "defines.h"
#include "tcc1014mmu.h"
#include "resource.h"
#include "pakinterface.h"
#include "tcc1014graphics.h"
#include "config.h"
#include <vcc/util/logger.h>
#include <vcc/util/DialogOps.h>
#include <fstream>
#include <windowsx.h>
namespace VCC::Debugger::UI {
namespace {
MemoryWindow* gMemoryWindows[2] = { 0, 0 };
// windows will allow the track bar to be positioned
// pixel by pixel, but it will snap to the
// nearest whole value. as the horizontal scrolling
// will use small numbers, this causes a visible jumping
// of the track bar which is distracting, so instead
// allow higher resolution for the track bar and scale
// the result.
constexpr int cHorzScrollFactor = 10;
constexpr int cTopBarStaticWidth = 510; // the tool bar
constexpr int cTopBarHeight = 34;
constexpr int cHeaderHeight = 20; // header with labels
constexpr int cVertScrollBarWidth = 20;
constexpr int cHorzScrollBarHeight = 20; // bottom scroll bar
constexpr int cAddressWidth = 60; // address column width
constexpr int cAsciiCharWidth = 7;
// Help text
const char *cDbgHelp =
"The default memory type displayed is 'CPU'.\n"
"Dropdown will select 'REAL', 'ROM', or 'PAK'.\n\n"
"In addition to the scroll bar the mouse wheel,\n"
"Home, End, PgUp, PgDn, Up, and Down keys\n"
"will scroll the display.\n\n"
"Select hex to be edited by clicking on a\n"
"cell. The cell will turn red and it's address\n"
"will be displayed next to the box. Enter byte\n"
"values in hexadecimal. ESC to end edit.\n\n"
"Select display mode: Ascii, SG4, PMODE, HSCREEN.\n\n"
"Click hex title to collapse/expand hex.\n"
"Click view title to switch between normal/gime width\n"
"and the full width.\n\n"
"Other Keys:\n"
" [ ] Dec/inc data row width by 1.\n"
" { } Dec/inc data row width by 8.\n"
"";
}
const std::string MemoryWindow::cMemoryWindow("MemoryWindow");
const std::string MemoryWindow::cMemoryWindow2("MemoryWindow2");
const std::string MemoryWindow::cWindowSizeX("WindowSizeX");
const std::string MemoryWindow::cWindowSizeY("WindowSizeY");
const std::string MemoryWindow::cWindowPosX("WindowPosX");
const std::string MemoryWindow::cWindowPosY("WindowPosY");
const std::string MemoryWindow::cAddress("Address");
const std::string MemoryWindow::cAddressMode("AddressMode");
const std::string MemoryWindow::cViewMode("ViewMode");
const std::string MemoryWindow::cHex("Hex");
const std::string MemoryWindow::cWide("Wide");
const std::string MemoryWindow::cDataWidth("DataWidth");
const std::string MemoryWindow::cDataPosX("DataPosX");
struct MemoryWindow::Measurements
{
const MemoryWindow* mem;
int showHex;
bool showAscii;
int top;
int bottom;
int left;
int right;
int height;
int width;
int hexDigitsWidth; // width of hex digits
int hexMinColums; // hex shown columns
int hexColumns; // hex columns visible
int hexWidth; // hex window width (pixels)
int viewWidth; // view window width (pixels)
int viewOffset; // view window buffer offset
int viewColumns; // view window columns (bytes)
int viewMaxColumns; // maximum number of view columns (gime)
int viewMaxWidth; // view window maximum width (pixels)
int viewWinWidth; // view window minimum width (pixels)
float viewCellWidth;
int lineHeight;
int currLineTop;
int viewPosX; // view window horizontal scroll
int viewStep; // scrolling increment
const int fontHeight = 12;
const int maxDataWidth[MemoryWindow::VM_MAX] =
{
256,//VM_ASCII,
32,//VM_SG4,
16,//VM_PMODE0_S0,
16,//VM_PMODE0_S1,
32,//VM_PMODE1_S0,
32,//VM_PMODE1_S1,
16,//VM_PMODE2_S0,
16,//VM_PMODE2_S1,
32,//VM_PMODE3_S0,
32,//VM_PMODE3_S1,
32,//VM_PMODE4_RGB_S0,
32,//VM_PMODE4_RGB_S1,
32,//VM_PMODE4_NTSC,
40,//VM_TEXT40,
80,//VM_TEXT80,
80,//VM_HSCREEN1,
160,//VM_HSCREEN2,
80,//VM_HSCREEN3,
80,//VM_HSCREEN4
};
const int pixelViewWidth[MemoryWindow::VM_MAX] =
{
8,//VM_ASCII,
8,//VM_SG4,
16,//VM_PMODE0_S0,
16,//VM_PMODE0_S1,
8,//VM_PMODE1_S0,
8,//VM_PMODE1_S1,
16,//VM_PMODE2_S0,
16,//VM_PMODE2_S1,
8,//VM_PMODE3_S0,
8,//VM_PMODE3_S1,
8,//VM_PMODE4_RGB_S0,
8,//VM_PMODE4_RGB_S1,
8,//VM_PMODE4_NTSC,
4,//VM_TEXT40,
2,//VM_TEXT80,
4,//VM_HSCREEN1,
2,//VM_HSCREEN2,
4,//VM_HSCREEN3,
2,//VM_HSCREEN4
};
//
// construct display measurements from back buffer client rectangle
//
Measurements(const MemoryWindow *mem, LPCRECT clientRect) : mem(mem)
{
showHex = mem->showHex;
showAscii = mem->viewMode == MemoryWindow::VM_ASCII;
// rect
top = clientRect->top;
bottom = clientRect->bottom - cHeaderHeight;
left = clientRect->left;
right = clientRect->right;
height = bottom - top;
width = right - left;
// hex column info
hexDigitsWidth = 18;
hexColumns = mem->dataWidth > 32 ? 32 : mem->dataWidth;
hexMinColums = hexColumns < 16 ? 16 : hexColumns;
hexWidth = HexColumnPos(hexMinColums);
if (!showHex)
{
hexWidth = HexColumnPos(0) + 10;
hexColumns = 0;
}
// include a gutter after width of 16
int gutter = (hexColumns > 16 && hexColumns & 7) ? 10 : 0;
// view column info
viewMaxColumns = (mem->dataWidth > maxDataWidth[mem->viewMode] ? maxDataWidth[mem->viewMode] : mem->dataWidth);
viewColumns = mem->showWideView ? mem->dataWidth : viewMaxColumns;
viewWidth = width - hexWidth - gutter; // view width is remaining width
viewWinWidth = viewColumns * 2 * pixelViewWidth[mem->viewMode];
viewMaxWidth = (viewWidth - 2) & (~0xF); // round view width
viewOffset = 0;
int charWidth = 16;
viewStep = 1;
if (mem->viewMode == MemoryWindow::VM_TEXT40 || mem->viewMode == MemoryWindow::VM_TEXT80)
{
charWidth = 4;
viewStep = 2;
}
if (viewMaxWidth > viewColumns * charWidth * pixelViewWidth[mem->viewMode])
viewMaxWidth = viewColumns * charWidth * pixelViewWidth[mem->viewMode];
// line rows
lineHeight = (height / 32) - 1;
currLineTop = top + cHeaderHeight;
if (mem->viewMode == MemoryWindow::VM_SG4)
{
if (viewMaxWidth > viewColumns * 16)
viewMaxWidth = viewColumns * 16;
}
viewCellWidth = ((float)viewMaxWidth / viewWinWidth) * pixelViewWidth[mem->viewMode] * 2;
if (mem->viewMode == MemoryWindow::VM_ASCII)
{
viewMaxWidth = (viewWidth - 2) & (~0xF);
viewColumns = std::min(viewMaxWidth / cAsciiCharWidth, mem->dataWidth);
viewCellWidth = 7;
}
if (mem->viewMode == MemoryWindow::VM_PMODE4_NTSC)
viewOffset = GetPaletteType() == 2 ? 4 : 0;
// reposition view so hex is always visible
const float rounding = 0.000001f; // adjust for rounding
float pos = mem->dataWidth <= hexColumns ? 0 : (float)mem->dataPosX / (mem->dataWidth - hexColumns);
viewPosX = roundDn(std::max(0, (int)((mem->dataWidth - viewColumns) * (pos + rounding))), viewStep);
}
//
// left edge of hex column
//
int HexLeft() const { return left + cAddressWidth + 5; }
//
// left edge of view
//
int ViewLeft() const { return right - viewWidth; }
//
// left/right pos of hex window in view
//
int ViewHexLeft() const { return ViewLeft() + (int)(viewCellWidth * (mem->dataPosX - viewPosX)); }
int ViewHexRight() const { return ViewHexLeft() + (int)(viewCellWidth * hexColumns); }
//
// return pos (pixel offset) of hex value column n
//
int HexColumnPos(int n) const
{
// hex cell spacing
const int minorSpacing = hexDigitsWidth;
// gutter spacing after each 8 values
const int majorSpacing = 10;
// calculate minor & major column position
int minorColumn = n & 7;
int majorColumn = n / 8;
return HexLeft() + minorColumn * minorSpacing + majorColumn * (minorSpacing * 8 + majorSpacing);
}
//
// reverse of hexColumnPos(x) given relative pixel offset
//
int HexPosColumn(int x) const
{
// move origin
x -= HexLeft();
const int minorSpacing = hexDigitsWidth;
const int majorSpacing = 10;
int minorColumnWidth = minorSpacing;
int majorColumnWidth = minorSpacing * 8 + majorSpacing;
// calculate major column
int majorColumn = x / majorColumnWidth;
// get remainder
x -= majorColumn * majorColumnWidth;
// if inside gutter return invalid
if (x > minorSpacing * 8) return -1;
// calculate minor column
int minorColumn = x / minorColumnWidth;
// return column index
return majorColumn * 8 + minorColumn;
}
//
// left edge of address column
//
int RowAddressPos() const
{
return left + 10;
}
//
// return current row, centered for text
//
int RowTextY() const
{
return currLineTop + (lineHeight - fontHeight) / 2;
}
};
MemoryWindow::MemoryWindow(int index) :
hDlgMem(nullptr),
hScrollBar(nullptr),
hHorzScrollBar(nullptr),
hEditAdrBeg(nullptr),
hEditAdrEnd(nullptr),
hEditVal(nullptr),
hStatic(nullptr),
ramCache(nullptr),
ramPos(-1),
addrMode(NotSet),
viewMode(VM_SG4),
winIndex(index),
memSize(0),
memOffset(0),
selectionRangeBeg(1),
selectionRangeEnd(1),
rom(nullptr),
isEditing(false),
showHex(1),
showWideView(0),
editAddress(0),
dataWidth(16),
dataPosX(0)
{
}
void MemoryWindow::ResetMemoryCache()
{
ramPos = -1;
delete[] ramCache;
ramCache = nullptr;
}
void MemoryBackBufferInfo::Init(HWND hDlgMem)
{
dataWidth = 640;
dataHeight = 32;
dataStride = dataWidth * 4;
data = new char[dataStride * dataHeight];
HDC hdc = GetDC(hDlgMem);
RECT frc;
SetRect(&frc, 0, 0, 7 * 16, 14);
// Set display Font
font = CreateFont(14, 0, 0, 0, FW_BOLD, FALSE, FALSE,
FALSE, DEFAULT_CHARSET, OUT_OUTLINE_PRECIS,
CLIP_DEFAULT_PRECIS, CLEARTYPE_QUALITY, FIXED_PITCH,
TEXT("Consolas"));
// Set display pen color
pen = CreatePen(PS_SOLID, 1, RGB(192, 192, 192));
UINT fmt = DT_LEFT | DT_VCENTER | DT_SINGLELINE;
// cache main hex font (black)
fontDC = CreateCompatibleDC(hdc);
fontBitmap = CreateCompatibleBitmap(hdc, 7 * 16, 14);
SelectObject(fontDC, fontBitmap);
SelectObject(fontDC, font);
DrawText(fontDC, "0123456789ABCDEF", 32, &frc, fmt);
// cache secondary hex font (red)
fontRedDC = CreateCompatibleDC(hdc);
fontRedBitmap = CreateCompatibleBitmap(hdc, 7 * 16, 14);
SelectObject(fontRedDC, fontRedBitmap);
SelectObject(fontRedDC, font);
SetTextColor(fontRedDC, RGB(255, 0, 0)); // Red
DrawText(fontRedDC, "0123456789ABCDEF", 32, &frc, fmt);
// cache ascii font
frc.right = 7 * 96;
fontAsciiDC = CreateCompatibleDC(hdc);
fontAsciiBitmap = CreateCompatibleBitmap(hdc, 7 * 96, 14);
SelectObject(fontAsciiDC, fontAsciiBitmap);
SelectObject(fontAsciiDC, font);
DrawText(fontAsciiDC, "!\"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[.]^_`abcdefghijklmnopqrstuvwxyz{|}~.", 96, &frc, fmt);
ReleaseDC(hDlgMem, hdc);
}
void MemoryBackBufferInfo::CleanupPen()
{
DeleteObject(pen);
pen = nullptr;
}
void MemoryBackBufferInfo::CleanupFont()
{
DeleteObject(font);
font = nullptr;
}
void MemoryBackBufferInfo::CleanupFonts(HWND hWnd)
{
DeleteObject(fontBitmap);
DeleteObject(fontRedBitmap);
DeleteObject(fontAsciiBitmap);
ReleaseDC(hWnd, fontDC);
DeleteDC(fontDC);
fontDC = nullptr;
ReleaseDC(hWnd, fontRedDC);
DeleteDC(fontRedDC);
fontRedDC = nullptr;
ReleaseDC(hWnd, fontAsciiDC);
DeleteDC(fontAsciiDC);
fontAsciiDC = nullptr;
}
void MemoryBackBufferInfo::Cleanup(HWND hWnd)
{
delete[] data;
data = nullptr;
CleanupFonts(hWnd);
CleanupFont();
CleanupPen();
CleanupBitmap();
CleanupDC(hWnd);
}
//
// refresh the whole window
//
void MemoryWindow::RepaintAll()
{
ResetMemoryCache();
InvalidateRect(hDlgMem, &BackBuf.Rect, FALSE);
// clear backbuffer
HBRUSH brush = (HBRUSH)GetStockObject(WHITE_BRUSH);
FillRect(BackBuf.DeviceContext, &BackBuf.Rect, brush);
// redraw headers
DrawForm(BackBuf.DeviceContext, &BackBuf.Rect);
}
//
// adjust vertical scroll bar based on data width
//
void MemoryWindow::UpdateVertScrollBar()
{
SCROLLINFO si = { 0 };
int visible = 32 * dataWidth;
si.cbSize = sizeof(si);
si.fMask = SIF_RANGE | SIF_POS | SIF_PAGE;
si.nMin = 0;
si.nPage = visible;
si.nMax = (memSize > visible) ? memSize - 1 : 0;
si.nPos = memOffset;
SetScrollInfo(hScrollBar, SB_CTL, &si, TRUE);
}
//
// adjust horizontal scroll bar based on data width
//
void MemoryWindow::UpdateHorzScrollBar()
{
Measurements m(this,&BackBuf.Rect);
int visible = (cHorzScrollFactor * (showHex ? m.hexColumns : m.viewColumns)) / m.viewStep;
int total = (cHorzScrollFactor * dataWidth) / m.viewStep;
int pos = (cHorzScrollFactor * dataPosX) / m.viewStep;
SCROLLINFO si = { 0 };
si.cbSize = sizeof(si);
si.fMask = SIF_RANGE | SIF_POS | SIF_PAGE;
si.nMin = 0;
si.nPage = visible;
si.nMax = total > visible ? total - 1 : 0;
si.nPos = pos;
SetScrollInfo(hHorzScrollBar, SB_CTL, &si, TRUE);
}
//
// show current width
//
void MemoryWindow::UpdateWidthDisplay()
{
char info[64];
sprintf(info, "%d", dataWidth);
HWND hCtl = GetDlgItem(hDlgMem, IDC_MEM_WIDTH);
SendMessage(hCtl, WM_SETTEXT, 0, (LPARAM)info);
}
//
// on user entering a new width
//
void MemoryWindow::CommitWidth()
{
char info[64];
HWND hCtl = GetDlgItem(hDlgMem, IDC_MEM_WIDTH);
ComboBox_GetText(hCtl, info, 64);
CommitWidth(info);
}
//
// commit new width value or revert
//
void MemoryWindow::CommitWidth(const char* value)
{
Measurements m(this, &BackBuf.Rect);
SetFocus(hEditAdrBeg);
int width = atoi(value);
if (width < m.viewStep || width > 256)
{
UpdateWidthDisplay();
return;
}
dataWidth = width;
// if goes over end step back one line
if (dataPosX + m.hexColumns > dataWidth)
dataPosX = std::max(0, dataWidth - m.hexColumns);
if (memOffset + 32*dataWidth + dataPosX > memSize)
{
dataPosX = 0;
memOffset = memSize - 32*dataWidth;
}
ResetMemoryCache();
UpdateWidthDisplay();
UpdateHorzScrollBar();
UpdateVertScrollBar();
RepaintAll();
}
//
// on user picking new width from combo box
//
void MemoryWindow::SelectWidth()
{
char info[64];
strcpy(info, "-1");
HWND hCtl = GetDlgItem(hDlgMem, IDC_MEM_WIDTH);
int index = (int)SendMessage(hCtl, CB_GETCURSEL, 0, 0);
if (index != CB_ERR)
SendMessage(hCtl, CB_GETLBTEXT, index, (LPARAM)info);
CommitWidth(info);
}
//
// setup new type of view
//
void MemoryWindow::SetViewType()
{
HWND hCtl = GetDlgItem(hDlgMem, IDC_VIEW_TYPE);
ViewMode mode = (ViewMode)SendMessage(hCtl, CB_GETCURSEL, 0, 0);
if (viewMode == mode) return;
viewMode = mode;
if (viewMode == VM_TEXT40 || viewMode == VM_TEXT80)
{
memOffset = roundDn(memOffset, 2);
dataWidth = roundDn(dataWidth, 2);
}
}
//
// save memory window state
//
void MemoryWindow::SaveSettings()
{
RECT CurWindow;
::GetWindowRect(hDlgMem, &CurWindow);
RECT CurScreen;
::GetClientRect(hDlgMem, &CurScreen);
int clientWidth = (int)CurScreen.right;
int clientHeight = (int)CurScreen.bottom;
//if (!IsMaximized(DFState) && !DFState->FullScreen && !DFState->Exiting)
{
VCC::Rect rect;
// remember positioning:
rect.x = CurWindow.left;
rect.y = CurWindow.top;
// remember size:
rect.w = clientWidth; // Used for saving new window size to the ini file.
rect.h = clientHeight - cHeaderHeight;
auto& s = Setting();
auto& section = winIndex ? cMemoryWindow2 : cMemoryWindow;
auto write = [&](const std::string& key, int value)
{
s.write(section, key, value);
};
write(cWindowSizeX, rect.w);
write(cWindowSizeY, rect.h);
write(cWindowPosX, rect.x);
write(cWindowPosY, rect.y);
// other state
write(cAddress, memOffset);
write(cAddressMode, (int)addrMode);
write(cViewMode, (int)viewMode);
write(cHex, (int)showHex);
write(cWide, (int)showWideView);
write(cDataWidth, dataWidth);
write(cDataPosX, dataPosX);
}
}
//
// load memory window state
//
void MemoryWindow::LoadSettings()
{
auto& s = Setting();
auto& section = winIndex ? cMemoryWindow2 : cMemoryWindow;
auto read = [&](const std::string& key, int& value)
{
value = s.read(section, key, value);
};
// assign default if not read
addrMode = Cpu;
read(cAddress, memOffset);
read(cAddressMode, (int&)addrMode);
read(cViewMode, (int&)viewMode);
read(cHex, (int&)showHex);
read(cWide, (int&)showWideView);
read(cDataWidth, dataWidth);
read(cDataPosX, dataPosX);
if (GetAsyncKeyState(VK_SHIFT))
{
dataPosX = 0;
dataWidth = 16;
showWideView = 0;
showHex = 1;
viewMode = VM_SG4;
addrMode = Cpu;
memOffset = 0;
}
}
//
// setup for new data width
//
void MemoryWindow::SetupDataWidth(int delta)
{
Measurements m(this,&BackBuf.Rect);
if (delta < 0)
{
if (GetAsyncKeyState(VK_SHIFT))
{
dataWidth = (dataWidth & 7) ? dataWidth & (~7) : dataWidth - 8;
if (dataWidth < m.viewStep) dataWidth = m.viewStep;
}
else
dataWidth = dataWidth > m.viewStep ? dataWidth - m.viewStep : dataWidth;
}
else if (delta > 0)
{
if (GetAsyncKeyState(VK_SHIFT))
{
dataWidth = (dataWidth & 7) ? (dataWidth + 8) & (~7) : dataWidth + 8;
if (dataWidth > 256) dataWidth = 256;
}
else
dataWidth = dataWidth < (257 - m.viewStep) ? dataWidth + m.viewStep : dataWidth;
}
if (dataPosX + m.hexColumns > dataWidth)
dataPosX = std::max(0, dataWidth - m.hexColumns);
UpdateWidthDisplay();
ResetMemoryCache();
InvalidateRect(hDlgMem, &BackBuf.Rect, FALSE);
// clear backbuffer
HBRUSH brush = (HBRUSH)GetStockObject(WHITE_BRUSH);
FillRect(BackBuf.DeviceContext, &BackBuf.Rect, brush);
// adjust vertical scrolling size
UpdateVertScrollBar();
UpdateHorzScrollBar();
// redraw headers
DrawForm(BackBuf.DeviceContext, &BackBuf.Rect);
}
//
// shutdown the memory window
//
void MemoryWindow::Shutdown()
{
if (hDlgMem)
{
SaveSettings();
KillTimer(hDlgMem, IDT_MEM_TIMER);
BackBuf.Cleanup(hDlgMem);
//DestroyWindow(hDlgMem);
hDlgMem = nullptr;
ResetMemoryCache();
}
}
void MemoryWindow::OnTimer(HWND hDlg)
{
if (addrMode == AddrMode::Cpu || addrMode == AddrMode::Real)
{
InvalidateRect(hDlg, &BackBuf.Rect, FALSE);
}
}
void MemoryWindow::Paint(HWND hDlg)
{
if (DrawMemory(BackBuf.DeviceContext, &BackBuf.Rect))
{
PAINTSTRUCT ps;
HDC hdc = BeginPaint(hDlg, &ps);
BitBlt(hdc, 0, 0, BackBuf.Width, BackBuf.Height, BackBuf.DeviceContext, 0, 0, SRCCOPY);
EndPaint(hDlg, &ps);
}
}
void MemoryWindow::Help(HWND hDlg)
{
MessageBox(hDlg,cDbgHelp,"Usage",0);
SetFocus(hEditAdrBeg);
}
void MemoryWindow::Export()
{
LocateMemory();
ExportMemory();
SetFocus(hEditAdrBeg);
}
void MemoryWindow::ViewType()
{
SetViewType();
UpdateVertScrollBar();
UpdateHorzScrollBar();
RepaintAll();
UpdateWidthDisplay();
}
void MemoryWindow::MemType(HWND hDlg)
{
SetMemType();
UpdateVertScrollBar();
UpdateHorzScrollBar();
ResetMemoryCache();
InvalidateRect(hDlg, &BackBuf.Rect, FALSE);
}
void MemoryWindow::LeftButton(int x, int y)
{
if (addrMode == AddrMode::PAK)
FlashDialogWindow();
else
SetEditPosition(x,y);
}
//------------------------------------------------------------------
// Display Memory Dialog
//------------------------------------------------------------------
LRESULT CALLBACK MemoryWindowSubclassProc(HWND hDlg, UINT uMsg, WPARAM wParam, LPARAM lParam, UINT_PTR uIdSubclass, DWORD_PTR dwRefData)
{
auto memoryWindow = [dwRefData]() { return (MemoryWindow*)dwRefData; };
switch (uMsg)
{
case WM_ERASEBKGND:
return 1;
case WM_GETMINMAXINFO:
{
MINMAXINFO* mmi = (MINMAXINFO*)lParam;
// Set minimum tracking size (width, height)
mmi->ptMinTrackSize.x = 540; // Minimum width in pixels
mmi->ptMinTrackSize.y = 540; // Minimum height in pixels
return 0;
}
case WM_SIZE:
{
UINT width = LOWORD(lParam);
UINT height = HIWORD(lParam);
memoryWindow()->ResizeWindow(width, height);
return 0;
}
case WM_LBUTTONDOWN:
memoryWindow()->LeftButton(LOWORD(lParam), HIWORD(lParam));
break;
case WM_VSCROLL:
memoryWindow()->DoScroll(wParam);
break;
case WM_HSCROLL:
memoryWindow()->DoHorzScroll(wParam);
break;
case WM_MOUSEWHEEL:
{
auto delta = GET_WHEEL_DELTA_WPARAM(wParam);
auto amount = (delta / 30) != 0 ? (delta / 30) : delta;
auto param = amount > 0 ? (WPARAM)SB_LINEUP : (WPARAM)SB_LINEDOWN;
auto mw = memoryWindow();
for (int i = 0; i < std::abs(amount); ++i)
mw->DoScroll((WPARAM)param);
break;
}
case WM_PAINT:
{
memoryWindow()->Paint(hDlg);
break;
}
case WM_TIMER:
switch (wParam) {
case IDT_MEM_TIMER:
memoryWindow()->OnTimer(hDlg);
}
break;
case WM_CLOSE:
DestroyWindow(hDlg);
break;
case WM_NCDESTROY:
{
auto mw = memoryWindow();
if (mw)
{
mw->Shutdown();
gMemoryWindows[mw->winIndex] = nullptr;
delete mw;
}
RemoveWindowSubclass(hDlg, MemoryWindowSubclassProc, uIdSubclass);
break;
}
case WM_COMMAND:
{
switch (LOWORD(wParam))
{
case IDC_MEM_TYPE:
memoryWindow()->MemType(hDlg);
break;
case IDC_VIEW_TYPE:
memoryWindow()->ViewType();
break;
case IDC_BTN_EXPORT_MEM:
memoryWindow()->Export();
break;
case IDC_BTN_HELP:
memoryWindow()->Help(hDlg);
break;
case IDCLOSE:
break;
}
break;
}
}
return DefSubclassProc(hDlg, uMsg, wParam, lParam);
}
//
// resize the window
//
void MemoryWindow::ResizeWindow(int width, int height)
{
RECT Rect;
GetClientRect(hDlgMem, &Rect);
// recreate the back buffer
ResetMemoryCache();
SetBackBuffer(Rect);
InvalidateRect(hDlgMem, &BackBuf.Rect, FALSE);
DrawForm(BackBuf.DeviceContext, &BackBuf.Rect);
// reposition scroll bar
MoveWindow(hScrollBar, Rect.right - cVertScrollBarWidth, cTopBarHeight, cVertScrollBarWidth, Rect.bottom - cTopBarHeight, TRUE);
MoveWindow(hHorzScrollBar, Rect.left, Rect.bottom - cHorzScrollBarHeight, Rect.right - cVertScrollBarWidth, cHorzScrollBarHeight, TRUE);
MoveWindow(hStatic, Rect.left, 0, Rect.right, cTopBarHeight, TRUE);
}
void MemoryWindow::Escape()
{
SetEditing(false);
UpdateWidthDisplay();
SetFocus(hEditAdrBeg);
ResetMemoryCache();
InvalidateRect(hDlgMem, &BackBuf.Rect, FALSE);
}
//
// Subclassed Edit Value Proc
//
LRESULT CALLBACK EditSubclassProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam, UINT_PTR uIdSubclass, DWORD_PTR dwRefData)
{
auto memoryWindow = [dwRefData]() { return (MemoryWindow*)dwRefData; };
switch (uMsg)
{
case WM_CHAR:
case WM_KEYUP:
{
switch (wParam)
{
case '[':
case ']':
case '{':
case '}':
case VK_OEM_4:
case VK_OEM_6:
case VK_RETURN:
case VK_ESCAPE:
return 0;
}
break;
}
case WM_KEYDOWN:
{
switch (wParam)
{
case VK_RETURN:
memoryWindow()->CommitValue();
return 0;
case VK_TAB:
case VK_ESCAPE:
memoryWindow()->Escape();
return 0;
case VK_UP:
case VK_DOWN:
case VK_PRIOR:
case VK_NEXT:
case VK_HOME:
case VK_END:
memoryWindow()->FlashDialogWindow();
return 0;
case VK_OEM_4:
case VK_OEM_6:
return 0;
}
break;
}
case WM_NCDESTROY:
RemoveWindowSubclass(hWnd, &EditSubclassProc, uIdSubclass);
break;
};
return DefSubclassProc(hWnd, uMsg, wParam, lParam);
}
//
// width combo box edit field handling
//
LRESULT CALLBACK MemWidthEditSubclassProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam, UINT_PTR uIdSubclass, DWORD_PTR dwRefData)
{
auto memoryWindow = [dwRefData]() { return (MemoryWindow*)dwRefData; };
switch (uMsg)
{
case WM_CHAR:
case WM_KEYDOWN:
{
switch (wParam)
{
case VK_RETURN: