view src/win32/GBMemoryViewerDlg.cpp @ 1:f9f4f1b99eed

importing src directory
author Robert McIntyre <rlm@mit.edu>
date Sat, 03 Mar 2012 10:31:27 -0600
parents
children
line wrap: on
line source
1 // GBMemoryViewerDlg.cpp : implementation file
2 //
4 #include "stdafx.h"
5 #include "resource.h"
6 #include "GBMemoryViewerDlg.h"
7 #include "FileDlg.h"
8 #include "MemoryViewerAddressSize.h"
9 #include "Reg.h"
10 #include "WinResUtil.h"
11 #include "VBA.h"
13 //#include "../common/System.h"
14 #include "../gb/gbGlobals.h"
16 GBMemoryViewer::GBMemoryViewer()
17 : MemoryViewer()
18 {
19 setAddressSize(1);
20 }
22 void GBMemoryViewer::readData(u32 address, int len, u8 *data)
23 {
24 u16 addr = address & 0xffff;
25 if (emulating && gbRom != NULL)
26 {
27 for (int i = 0; i < len; i++)
28 {
29 *data++ = gbReadMemoryQuick(addr);
30 addr++;
31 }
32 }
33 else
34 {
35 for (int i = 0; i < len; i++)
36 {
37 *data++ = 0;
38 addr++;
39 }
40 }
41 }
43 void GBMemoryViewer::editData(u32 address, int size, int mask, u32 value)
44 {
45 u32 oldValue;
46 u16 addr = (u16)address & 0xffff;
47 switch (size)
48 {
49 case 8:
50 oldValue = gbReadMemoryQuick(addr);
51 oldValue &= mask;
52 oldValue |= (u8)value;
53 gbWriteMemoryQuick(addr, oldValue);
54 break;
55 case 16:
56 oldValue = gbReadMemoryQuick(addr) |
57 (gbReadMemoryQuick(addr + 1) << 8);
58 oldValue &= mask;
59 oldValue |= (u16)value;
60 gbWriteMemoryQuick(addr, (oldValue & 255));
61 gbWriteMemoryQuick(addr+1, (oldValue >> 8));
62 break;
63 case 32:
64 oldValue = gbReadMemoryQuick(addr) |
65 (gbReadMemoryQuick(addr + 1) << 8) |
66 (gbReadMemoryQuick(addr + 2) << 16) |
67 (gbReadMemoryQuick(addr + 3) << 24);
68 oldValue &= mask;
69 oldValue |= (u32)value;
70 gbWriteMemoryQuick(addr, (oldValue & 255));
71 gbWriteMemoryQuick(addr+1, (oldValue >> 8));
72 gbWriteMemoryQuick(addr+2, (oldValue >> 16));
73 gbWriteMemoryQuick(addr+3, (oldValue >> 24));
74 break;
75 }
76 }
78 /////////////////////////////////////////////////////////////////////////////
79 // GBMemoryViewerDlg dialog
81 GBMemoryViewerDlg::GBMemoryViewerDlg(CWnd*pParent /*=NULL*/)
82 : ResizeDlg(GBMemoryViewerDlg::IDD, pParent)
83 {
84 //{{AFX_DATA_INIT(GBMemoryViewerDlg)
85 m_size = -1;
86 //}}AFX_DATA_INIT
87 autoUpdate = false;
88 }
90 void GBMemoryViewerDlg::DoDataExchange(CDataExchange*pDX)
91 {
92 CDialog::DoDataExchange(pDX);
93 //{{AFX_DATA_MAP(GBMemoryViewerDlg)
94 DDX_Control(pDX, IDC_CURRENT_ADDRESS, m_current);
95 DDX_Control(pDX, IDC_ADDRESS, m_address);
96 DDX_Control(pDX, IDC_ADDRESSES, m_addresses);
97 DDX_Radio(pDX, IDC_8_BIT, m_size);
98 //}}AFX_DATA_MAP
99 DDX_Control(pDX, IDC_VIEWER, m_viewer);
100 }
102 BEGIN_MESSAGE_MAP(GBMemoryViewerDlg, CDialog)
103 //{{AFX_MSG_MAP(GBMemoryViewerDlg)
104 ON_BN_CLICKED(IDC_CLOSE, OnClose)
105 ON_BN_CLICKED(IDC_REFRESH, OnRefresh)
106 ON_BN_CLICKED(IDC_8_BIT, On8Bit)
107 ON_BN_CLICKED(IDC_16_BIT, On16Bit)
108 ON_BN_CLICKED(IDC_32_BIT, On32Bit)
109 ON_BN_CLICKED(IDC_AUTO_UPDATE, OnAutoUpdate)
110 ON_BN_CLICKED(IDC_DECIMAL_DISPLAY, OnDecimalDisplay)
111 ON_BN_CLICKED(IDC_ALIGN, OnAlign)
112 ON_BN_CLICKED(IDC_GO, OnGo)
113 ON_CBN_SELCHANGE(IDC_ADDRESSES, OnSelchangeAddresses)
114 ON_BN_CLICKED(IDC_SAVE, OnSave)
115 ON_BN_CLICKED(IDC_LOAD, OnLoad)
116 //}}AFX_MSG_MAP
117 END_MESSAGE_MAP()
119 /////////////////////////////////////////////////////////////////////////////
120 // GBMemoryViewerDlg message handlers
122 BOOL GBMemoryViewerDlg::OnInitDialog()
123 {
124 CDialog::OnInitDialog();
126 autoUpdate = !regQueryDwordValue("memViewerAutoUpdate", 1);
127 OnAutoUpdate(); // inverts and update dialog
129 decimalDisplay = !regQueryDwordValue("memViewerDecimalDisplay", 0);
130 OnDecimalDisplay();
132 align = !regQueryDwordValue("memViewerAlign", 0);
133 OnAlign();
135 DIALOG_SIZER_START(sz)
136 DIALOG_SIZER_ENTRY(IDC_VIEWER, DS_SizeX | DS_SizeY)
137 DIALOG_SIZER_ENTRY(IDC_REFRESH, DS_MoveY)
138 DIALOG_SIZER_ENTRY(IDC_CLOSE, DS_MoveY)
139 DIALOG_SIZER_ENTRY(IDC_LOAD, DS_MoveY)
140 DIALOG_SIZER_ENTRY(IDC_SAVE, DS_MoveY)
141 DIALOG_SIZER_ENTRY(IDC_AUTO_UPDATE, DS_MoveY)
142 DIALOG_SIZER_ENTRY(IDC_DECIMAL_DISPLAY, DS_MoveY)
143 DIALOG_SIZER_ENTRY(IDC_ALIGN, DS_MoveY)
144 DIALOG_SIZER_ENTRY(IDC_CURRENT_ADDRESS_LABEL, DS_MoveY | DS_MoveX)
145 DIALOG_SIZER_ENTRY(IDC_CURRENT_ADDRESS, DS_MoveY | DS_MoveX)
146 DIALOG_SIZER_END()
147 SetData(sz,
148 TRUE,
149 HKEY_CURRENT_USER,
150 "Software\\Emulators\\VisualBoyAdvance\\Viewer\\GBMemoryView",
151 NULL);
153 m_viewer.setDialog(this);
154 m_viewer.ShowScrollBar(SB_VERT, TRUE);
155 m_viewer.EnableScrollBar(SB_VERT, ESB_ENABLE_BOTH);
157 LPCTSTR s[] = {
158 "0x0000 - ROM",
159 "0x4000 - ROM",
160 "0x8000 - VRAM",
161 "0xA000 - SRAM",
162 "0xC000 - RAM",
163 "0xD000 - WRAM",
164 "0xFF00 - I/O",
165 "0xFF80 - RAM"
166 };
168 for (int i = 0; i < 8; i++)
169 m_addresses.AddString(s[i]);
171 m_addresses.SetCurSel(0);
173 RECT cbSize;
174 int Height;
176 m_addresses.GetClientRect(&cbSize);
177 Height = m_addresses.GetItemHeight(-1);
178 Height += m_addresses.GetItemHeight(0) * (9);
180 // Note: The use of SM_CYEDGE assumes that we're using Windows '95
181 // Now add on the height of the border of the edit box
182 Height += GetSystemMetrics(SM_CYEDGE) * 2; // top & bottom edges
184 // The height of the border of the drop-down box
185 Height += GetSystemMetrics(SM_CYEDGE) * 2; // top & bottom edges
187 // now set the size of the window
188 m_addresses.SetWindowPos(NULL,
189 0, 0,
190 cbSize.right, Height,
191 SWP_NOMOVE | SWP_NOZORDER);
193 m_address.LimitText(8);
195 m_size = regQueryDwordValue("memViewerDataSize", 0);
196 if (m_size < 0 || m_size > 2)
197 m_size = 0;
198 m_viewer.setSize(m_size);
199 UpdateData(FALSE);
201 m_current.SetFont(CFont::FromHandle((HFONT)GetStockObject(SYSTEM_FIXED_FONT)));
203 return TRUE; // return TRUE unless you set the focus to a control
204 // EXCEPTION: OCX Property Pages should return FALSE
205 }
207 void GBMemoryViewerDlg::OnClose()
208 {
209 theApp.winRemoveUpdateListener(this);
211 DestroyWindow();
212 }
214 void GBMemoryViewerDlg::OnRefresh()
215 {
216 m_viewer.Invalidate();
217 }
219 void GBMemoryViewerDlg::update()
220 {
221 OnRefresh();
222 }
224 void GBMemoryViewerDlg::On8Bit()
225 {
226 m_viewer.setSize(0);
227 regSetDwordValue("memViewerDataSize", 0);
228 }
230 void GBMemoryViewerDlg::On16Bit()
231 {
232 m_viewer.setSize(1);
233 regSetDwordValue("memViewerDataSize", 1);
234 }
236 void GBMemoryViewerDlg::On32Bit()
237 {
238 m_viewer.setSize(2);
239 regSetDwordValue("memViewerDataSize", 2);
240 }
242 void GBMemoryViewerDlg::OnAutoUpdate()
243 {
244 autoUpdate = !autoUpdate;
245 if (autoUpdate)
246 {
247 theApp.winAddUpdateListener(this);
248 }
249 else
250 {
251 theApp.winRemoveUpdateListener(this);
252 }
253 if (GetDlgItem(IDC_AUTO_UPDATE))
254 ((CButton *)GetDlgItem(IDC_AUTO_UPDATE))->SetCheck(autoUpdate ? TRUE : FALSE);
255 regSetDwordValue("memViewerAutoUpdate", autoUpdate);
256 }
258 void GBMemoryViewerDlg::OnDecimalDisplay()
259 {
260 decimalDisplay = !decimalDisplay;
261 m_viewer.setDecimal(decimalDisplay ? true : false);
262 if (GetDlgItem(IDC_DECIMAL_DISPLAY))
263 ((CButton *)GetDlgItem(IDC_DECIMAL_DISPLAY))->SetCheck(decimalDisplay ? TRUE : FALSE);
265 regSetDwordValue("memViewerDecimalDisplay", decimalDisplay);
266 }
268 void GBMemoryViewerDlg::OnAlign()
269 {
270 align = !align;
271 if (GetDlgItem(IDC_ALIGN))
272 ((CButton *)GetDlgItem(IDC_ALIGN))->SetCheck(align ? TRUE : FALSE);
274 regSetDwordValue("memViewerAlign", align);
275 }
277 void GBMemoryViewerDlg::OnGo()
278 {
279 CString buffer;
281 m_address.GetWindowText(buffer);
283 u32 address;
284 sscanf(buffer, "%x", &address);
285 if (align)
286 address &= ~0xF;
287 else
288 {
289 if (m_viewer.getSize() == 1)
290 address &= ~1;
291 else if (m_viewer.getSize() == 2)
292 address &= ~3;
293 }
294 m_viewer.setAddress(address);
295 }
297 void GBMemoryViewerDlg::OnSelchangeAddresses()
298 {
299 int cur = m_addresses.GetCurSel();
301 switch (cur)
302 {
303 case 0:
304 m_viewer.setAddress(0x0000);
305 break;
306 case 1:
307 m_viewer.setAddress(0x4000);
308 break;
309 case 2:
310 m_viewer.setAddress(0x8000);
311 break;
312 case 3:
313 m_viewer.setAddress(0xa000);
314 break;
315 case 4:
316 m_viewer.setAddress(0xc000);
317 break;
318 case 5:
319 m_viewer.setAddress(0xd000);
320 break;
321 case 6:
322 m_viewer.setAddress(0xff00);
323 break;
324 case 7:
325 m_viewer.setAddress(0xff80);
326 break;
327 }
328 }
330 void GBMemoryViewerDlg::setCurrentAddress(u32 address)
331 {
332 CString buffer;
334 buffer.Format("0x%08X", address);
335 m_current.SetWindowText(buffer);
336 }
338 void GBMemoryViewerDlg::OnSave()
339 {
340 MemoryViewerAddressSize dlg;
342 dlg.setAddress(m_viewer.getCurrentAddress());
344 LPCTSTR exts[] = { ".dmp", NULL };
346 CString filter = winResLoadFilter(IDS_FILTER_DUMP);
347 CString title = winResLoadString(IDS_SELECT_DUMP_FILE);
349 if (dlg.DoModal() == IDOK)
350 {
351 CString buffer;
352 FileDlg file(this,
353 buffer,
354 filter,
355 0,
356 "DMP",
357 exts,
358 "",
359 title,
360 true);
361 if (file.DoModal() == IDOK)
362 {
363 buffer = file.GetPathName();
364 FILE *f = fopen(buffer, "wb");
366 if (f == NULL)
367 {
368 systemMessage(IDS_ERROR_CREATING_FILE, buffer);
369 return;
370 }
372 int size = dlg.getSize();
373 u16 addr = dlg.getAddress() & 0xffff;
375 for (int i = 0; i < size; i++)
376 {
377 fputc(gbReadMemoryQuick(addr), f);
378 addr++;
379 }
381 fclose(f);
382 }
383 }
384 }
386 void GBMemoryViewerDlg::OnLoad()
387 {
388 CString buffer;
389 LPCTSTR exts[] = { ".dmp", NULL };
390 CString filter = winResLoadFilter(IDS_FILTER_DUMP);
391 CString title = winResLoadString(IDS_SELECT_DUMP_FILE);
393 FileDlg file(this,
394 buffer,
395 filter,
396 0,
397 "DMP",
398 exts,
399 "",
400 title,
401 false);
403 if (file.DoModal() == IDOK)
404 {
405 buffer = file.GetPathName();
406 FILE *f = fopen(buffer, "rb");
407 if (f == NULL)
408 {
409 systemMessage(IDS_CANNOT_OPEN_FILE,
410 "Cannot open file %s",
411 buffer);
412 return;
413 }
415 MemoryViewerAddressSize dlg;
417 fseek(f, 0, SEEK_END);
418 int size = ftell(f);
420 fseek(f, 0, SEEK_SET);
422 dlg.setAddress(m_viewer.getCurrentAddress());
423 dlg.setSize(size);
425 if (dlg.DoModal() == IDOK)
426 {
427 int size = dlg.getSize();
428 u16 addr = dlg.getAddress() & 0xffff;
430 for (int i = 0; i < size; i++)
431 {
432 int c = fgetc(f);
433 if (c == -1)
434 break;
435 gbWriteMemoryQuick(addr, c);
436 addr++;
437 }
438 OnRefresh();
439 }
440 fclose(f);
441 }
442 }
444 void GBMemoryViewerDlg::PostNcDestroy()
445 {
446 delete this;
447 }