Mercurial > vba-clojure
diff 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 diff
1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/src/win32/GBMemoryViewerDlg.cpp Sat Mar 03 10:31:27 2012 -0600 1.3 @@ -0,0 +1,448 @@ 1.4 +// GBMemoryViewerDlg.cpp : implementation file 1.5 +// 1.6 + 1.7 +#include "stdafx.h" 1.8 +#include "resource.h" 1.9 +#include "GBMemoryViewerDlg.h" 1.10 +#include "FileDlg.h" 1.11 +#include "MemoryViewerAddressSize.h" 1.12 +#include "Reg.h" 1.13 +#include "WinResUtil.h" 1.14 +#include "VBA.h" 1.15 + 1.16 +//#include "../common/System.h" 1.17 +#include "../gb/gbGlobals.h" 1.18 + 1.19 +GBMemoryViewer::GBMemoryViewer() 1.20 + : MemoryViewer() 1.21 +{ 1.22 + setAddressSize(1); 1.23 +} 1.24 + 1.25 +void GBMemoryViewer::readData(u32 address, int len, u8 *data) 1.26 +{ 1.27 + u16 addr = address & 0xffff; 1.28 + if (emulating && gbRom != NULL) 1.29 + { 1.30 + for (int i = 0; i < len; i++) 1.31 + { 1.32 + *data++ = gbReadMemoryQuick(addr); 1.33 + addr++; 1.34 + } 1.35 + } 1.36 + else 1.37 + { 1.38 + for (int i = 0; i < len; i++) 1.39 + { 1.40 + *data++ = 0; 1.41 + addr++; 1.42 + } 1.43 + } 1.44 +} 1.45 + 1.46 +void GBMemoryViewer::editData(u32 address, int size, int mask, u32 value) 1.47 +{ 1.48 + u32 oldValue; 1.49 + u16 addr = (u16)address & 0xffff; 1.50 + switch (size) 1.51 + { 1.52 + case 8: 1.53 + oldValue = gbReadMemoryQuick(addr); 1.54 + oldValue &= mask; 1.55 + oldValue |= (u8)value; 1.56 + gbWriteMemoryQuick(addr, oldValue); 1.57 + break; 1.58 + case 16: 1.59 + oldValue = gbReadMemoryQuick(addr) | 1.60 + (gbReadMemoryQuick(addr + 1) << 8); 1.61 + oldValue &= mask; 1.62 + oldValue |= (u16)value; 1.63 + gbWriteMemoryQuick(addr, (oldValue & 255)); 1.64 + gbWriteMemoryQuick(addr+1, (oldValue >> 8)); 1.65 + break; 1.66 + case 32: 1.67 + oldValue = gbReadMemoryQuick(addr) | 1.68 + (gbReadMemoryQuick(addr + 1) << 8) | 1.69 + (gbReadMemoryQuick(addr + 2) << 16) | 1.70 + (gbReadMemoryQuick(addr + 3) << 24); 1.71 + oldValue &= mask; 1.72 + oldValue |= (u32)value; 1.73 + gbWriteMemoryQuick(addr, (oldValue & 255)); 1.74 + gbWriteMemoryQuick(addr+1, (oldValue >> 8)); 1.75 + gbWriteMemoryQuick(addr+2, (oldValue >> 16)); 1.76 + gbWriteMemoryQuick(addr+3, (oldValue >> 24)); 1.77 + break; 1.78 + } 1.79 +} 1.80 + 1.81 +///////////////////////////////////////////////////////////////////////////// 1.82 +// GBMemoryViewerDlg dialog 1.83 + 1.84 +GBMemoryViewerDlg::GBMemoryViewerDlg(CWnd*pParent /*=NULL*/) 1.85 + : ResizeDlg(GBMemoryViewerDlg::IDD, pParent) 1.86 +{ 1.87 + //{{AFX_DATA_INIT(GBMemoryViewerDlg) 1.88 + m_size = -1; 1.89 + //}}AFX_DATA_INIT 1.90 + autoUpdate = false; 1.91 +} 1.92 + 1.93 +void GBMemoryViewerDlg::DoDataExchange(CDataExchange*pDX) 1.94 +{ 1.95 + CDialog::DoDataExchange(pDX); 1.96 + //{{AFX_DATA_MAP(GBMemoryViewerDlg) 1.97 + DDX_Control(pDX, IDC_CURRENT_ADDRESS, m_current); 1.98 + DDX_Control(pDX, IDC_ADDRESS, m_address); 1.99 + DDX_Control(pDX, IDC_ADDRESSES, m_addresses); 1.100 + DDX_Radio(pDX, IDC_8_BIT, m_size); 1.101 + //}}AFX_DATA_MAP 1.102 + DDX_Control(pDX, IDC_VIEWER, m_viewer); 1.103 +} 1.104 + 1.105 +BEGIN_MESSAGE_MAP(GBMemoryViewerDlg, CDialog) 1.106 +//{{AFX_MSG_MAP(GBMemoryViewerDlg) 1.107 +ON_BN_CLICKED(IDC_CLOSE, OnClose) 1.108 +ON_BN_CLICKED(IDC_REFRESH, OnRefresh) 1.109 +ON_BN_CLICKED(IDC_8_BIT, On8Bit) 1.110 +ON_BN_CLICKED(IDC_16_BIT, On16Bit) 1.111 +ON_BN_CLICKED(IDC_32_BIT, On32Bit) 1.112 +ON_BN_CLICKED(IDC_AUTO_UPDATE, OnAutoUpdate) 1.113 +ON_BN_CLICKED(IDC_DECIMAL_DISPLAY, OnDecimalDisplay) 1.114 +ON_BN_CLICKED(IDC_ALIGN, OnAlign) 1.115 +ON_BN_CLICKED(IDC_GO, OnGo) 1.116 +ON_CBN_SELCHANGE(IDC_ADDRESSES, OnSelchangeAddresses) 1.117 +ON_BN_CLICKED(IDC_SAVE, OnSave) 1.118 +ON_BN_CLICKED(IDC_LOAD, OnLoad) 1.119 +//}}AFX_MSG_MAP 1.120 +END_MESSAGE_MAP() 1.121 + 1.122 +///////////////////////////////////////////////////////////////////////////// 1.123 +// GBMemoryViewerDlg message handlers 1.124 + 1.125 +BOOL GBMemoryViewerDlg::OnInitDialog() 1.126 +{ 1.127 + CDialog::OnInitDialog(); 1.128 + 1.129 + autoUpdate = !regQueryDwordValue("memViewerAutoUpdate", 1); 1.130 + OnAutoUpdate(); // inverts and update dialog 1.131 + 1.132 + decimalDisplay = !regQueryDwordValue("memViewerDecimalDisplay", 0); 1.133 + OnDecimalDisplay(); 1.134 + 1.135 + align = !regQueryDwordValue("memViewerAlign", 0); 1.136 + OnAlign(); 1.137 + 1.138 + DIALOG_SIZER_START(sz) 1.139 + DIALOG_SIZER_ENTRY(IDC_VIEWER, DS_SizeX | DS_SizeY) 1.140 + DIALOG_SIZER_ENTRY(IDC_REFRESH, DS_MoveY) 1.141 + DIALOG_SIZER_ENTRY(IDC_CLOSE, DS_MoveY) 1.142 + DIALOG_SIZER_ENTRY(IDC_LOAD, DS_MoveY) 1.143 + DIALOG_SIZER_ENTRY(IDC_SAVE, DS_MoveY) 1.144 + DIALOG_SIZER_ENTRY(IDC_AUTO_UPDATE, DS_MoveY) 1.145 + DIALOG_SIZER_ENTRY(IDC_DECIMAL_DISPLAY, DS_MoveY) 1.146 + DIALOG_SIZER_ENTRY(IDC_ALIGN, DS_MoveY) 1.147 + DIALOG_SIZER_ENTRY(IDC_CURRENT_ADDRESS_LABEL, DS_MoveY | DS_MoveX) 1.148 + DIALOG_SIZER_ENTRY(IDC_CURRENT_ADDRESS, DS_MoveY | DS_MoveX) 1.149 + DIALOG_SIZER_END() 1.150 + SetData(sz, 1.151 + TRUE, 1.152 + HKEY_CURRENT_USER, 1.153 + "Software\\Emulators\\VisualBoyAdvance\\Viewer\\GBMemoryView", 1.154 + NULL); 1.155 + 1.156 + m_viewer.setDialog(this); 1.157 + m_viewer.ShowScrollBar(SB_VERT, TRUE); 1.158 + m_viewer.EnableScrollBar(SB_VERT, ESB_ENABLE_BOTH); 1.159 + 1.160 + LPCTSTR s[] = { 1.161 + "0x0000 - ROM", 1.162 + "0x4000 - ROM", 1.163 + "0x8000 - VRAM", 1.164 + "0xA000 - SRAM", 1.165 + "0xC000 - RAM", 1.166 + "0xD000 - WRAM", 1.167 + "0xFF00 - I/O", 1.168 + "0xFF80 - RAM" 1.169 + }; 1.170 + 1.171 + for (int i = 0; i < 8; i++) 1.172 + m_addresses.AddString(s[i]); 1.173 + 1.174 + m_addresses.SetCurSel(0); 1.175 + 1.176 + RECT cbSize; 1.177 + int Height; 1.178 + 1.179 + m_addresses.GetClientRect(&cbSize); 1.180 + Height = m_addresses.GetItemHeight(-1); 1.181 + Height += m_addresses.GetItemHeight(0) * (9); 1.182 + 1.183 + // Note: The use of SM_CYEDGE assumes that we're using Windows '95 1.184 + // Now add on the height of the border of the edit box 1.185 + Height += GetSystemMetrics(SM_CYEDGE) * 2; // top & bottom edges 1.186 + 1.187 + // The height of the border of the drop-down box 1.188 + Height += GetSystemMetrics(SM_CYEDGE) * 2; // top & bottom edges 1.189 + 1.190 + // now set the size of the window 1.191 + m_addresses.SetWindowPos(NULL, 1.192 + 0, 0, 1.193 + cbSize.right, Height, 1.194 + SWP_NOMOVE | SWP_NOZORDER); 1.195 + 1.196 + m_address.LimitText(8); 1.197 + 1.198 + m_size = regQueryDwordValue("memViewerDataSize", 0); 1.199 + if (m_size < 0 || m_size > 2) 1.200 + m_size = 0; 1.201 + m_viewer.setSize(m_size); 1.202 + UpdateData(FALSE); 1.203 + 1.204 + m_current.SetFont(CFont::FromHandle((HFONT)GetStockObject(SYSTEM_FIXED_FONT))); 1.205 + 1.206 + return TRUE; // return TRUE unless you set the focus to a control 1.207 + // EXCEPTION: OCX Property Pages should return FALSE 1.208 +} 1.209 + 1.210 +void GBMemoryViewerDlg::OnClose() 1.211 +{ 1.212 + theApp.winRemoveUpdateListener(this); 1.213 + 1.214 + DestroyWindow(); 1.215 +} 1.216 + 1.217 +void GBMemoryViewerDlg::OnRefresh() 1.218 +{ 1.219 + m_viewer.Invalidate(); 1.220 +} 1.221 + 1.222 +void GBMemoryViewerDlg::update() 1.223 +{ 1.224 + OnRefresh(); 1.225 +} 1.226 + 1.227 +void GBMemoryViewerDlg::On8Bit() 1.228 +{ 1.229 + m_viewer.setSize(0); 1.230 + regSetDwordValue("memViewerDataSize", 0); 1.231 +} 1.232 + 1.233 +void GBMemoryViewerDlg::On16Bit() 1.234 +{ 1.235 + m_viewer.setSize(1); 1.236 + regSetDwordValue("memViewerDataSize", 1); 1.237 +} 1.238 + 1.239 +void GBMemoryViewerDlg::On32Bit() 1.240 +{ 1.241 + m_viewer.setSize(2); 1.242 + regSetDwordValue("memViewerDataSize", 2); 1.243 +} 1.244 + 1.245 +void GBMemoryViewerDlg::OnAutoUpdate() 1.246 +{ 1.247 + autoUpdate = !autoUpdate; 1.248 + if (autoUpdate) 1.249 + { 1.250 + theApp.winAddUpdateListener(this); 1.251 + } 1.252 + else 1.253 + { 1.254 + theApp.winRemoveUpdateListener(this); 1.255 + } 1.256 + if (GetDlgItem(IDC_AUTO_UPDATE)) 1.257 + ((CButton *)GetDlgItem(IDC_AUTO_UPDATE))->SetCheck(autoUpdate ? TRUE : FALSE); 1.258 + regSetDwordValue("memViewerAutoUpdate", autoUpdate); 1.259 +} 1.260 + 1.261 +void GBMemoryViewerDlg::OnDecimalDisplay() 1.262 +{ 1.263 + decimalDisplay = !decimalDisplay; 1.264 + m_viewer.setDecimal(decimalDisplay ? true : false); 1.265 + if (GetDlgItem(IDC_DECIMAL_DISPLAY)) 1.266 + ((CButton *)GetDlgItem(IDC_DECIMAL_DISPLAY))->SetCheck(decimalDisplay ? TRUE : FALSE); 1.267 + 1.268 + regSetDwordValue("memViewerDecimalDisplay", decimalDisplay); 1.269 +} 1.270 + 1.271 +void GBMemoryViewerDlg::OnAlign() 1.272 +{ 1.273 + align = !align; 1.274 + if (GetDlgItem(IDC_ALIGN)) 1.275 + ((CButton *)GetDlgItem(IDC_ALIGN))->SetCheck(align ? TRUE : FALSE); 1.276 + 1.277 + regSetDwordValue("memViewerAlign", align); 1.278 +} 1.279 + 1.280 +void GBMemoryViewerDlg::OnGo() 1.281 +{ 1.282 + CString buffer; 1.283 + 1.284 + m_address.GetWindowText(buffer); 1.285 + 1.286 + u32 address; 1.287 + sscanf(buffer, "%x", &address); 1.288 + if (align) 1.289 + address &= ~0xF; 1.290 + else 1.291 + { 1.292 + if (m_viewer.getSize() == 1) 1.293 + address &= ~1; 1.294 + else if (m_viewer.getSize() == 2) 1.295 + address &= ~3; 1.296 + } 1.297 + m_viewer.setAddress(address); 1.298 +} 1.299 + 1.300 +void GBMemoryViewerDlg::OnSelchangeAddresses() 1.301 +{ 1.302 + int cur = m_addresses.GetCurSel(); 1.303 + 1.304 + switch (cur) 1.305 + { 1.306 + case 0: 1.307 + m_viewer.setAddress(0x0000); 1.308 + break; 1.309 + case 1: 1.310 + m_viewer.setAddress(0x4000); 1.311 + break; 1.312 + case 2: 1.313 + m_viewer.setAddress(0x8000); 1.314 + break; 1.315 + case 3: 1.316 + m_viewer.setAddress(0xa000); 1.317 + break; 1.318 + case 4: 1.319 + m_viewer.setAddress(0xc000); 1.320 + break; 1.321 + case 5: 1.322 + m_viewer.setAddress(0xd000); 1.323 + break; 1.324 + case 6: 1.325 + m_viewer.setAddress(0xff00); 1.326 + break; 1.327 + case 7: 1.328 + m_viewer.setAddress(0xff80); 1.329 + break; 1.330 + } 1.331 +} 1.332 + 1.333 +void GBMemoryViewerDlg::setCurrentAddress(u32 address) 1.334 +{ 1.335 + CString buffer; 1.336 + 1.337 + buffer.Format("0x%08X", address); 1.338 + m_current.SetWindowText(buffer); 1.339 +} 1.340 + 1.341 +void GBMemoryViewerDlg::OnSave() 1.342 +{ 1.343 + MemoryViewerAddressSize dlg; 1.344 + 1.345 + dlg.setAddress(m_viewer.getCurrentAddress()); 1.346 + 1.347 + LPCTSTR exts[] = { ".dmp", NULL }; 1.348 + 1.349 + CString filter = winResLoadFilter(IDS_FILTER_DUMP); 1.350 + CString title = winResLoadString(IDS_SELECT_DUMP_FILE); 1.351 + 1.352 + if (dlg.DoModal() == IDOK) 1.353 + { 1.354 + CString buffer; 1.355 + FileDlg file(this, 1.356 + buffer, 1.357 + filter, 1.358 + 0, 1.359 + "DMP", 1.360 + exts, 1.361 + "", 1.362 + title, 1.363 + true); 1.364 + if (file.DoModal() == IDOK) 1.365 + { 1.366 + buffer = file.GetPathName(); 1.367 + FILE *f = fopen(buffer, "wb"); 1.368 + 1.369 + if (f == NULL) 1.370 + { 1.371 + systemMessage(IDS_ERROR_CREATING_FILE, buffer); 1.372 + return; 1.373 + } 1.374 + 1.375 + int size = dlg.getSize(); 1.376 + u16 addr = dlg.getAddress() & 0xffff; 1.377 + 1.378 + for (int i = 0; i < size; i++) 1.379 + { 1.380 + fputc(gbReadMemoryQuick(addr), f); 1.381 + addr++; 1.382 + } 1.383 + 1.384 + fclose(f); 1.385 + } 1.386 + } 1.387 +} 1.388 + 1.389 +void GBMemoryViewerDlg::OnLoad() 1.390 +{ 1.391 + CString buffer; 1.392 + LPCTSTR exts[] = { ".dmp", NULL }; 1.393 + CString filter = winResLoadFilter(IDS_FILTER_DUMP); 1.394 + CString title = winResLoadString(IDS_SELECT_DUMP_FILE); 1.395 + 1.396 + FileDlg file(this, 1.397 + buffer, 1.398 + filter, 1.399 + 0, 1.400 + "DMP", 1.401 + exts, 1.402 + "", 1.403 + title, 1.404 + false); 1.405 + 1.406 + if (file.DoModal() == IDOK) 1.407 + { 1.408 + buffer = file.GetPathName(); 1.409 + FILE *f = fopen(buffer, "rb"); 1.410 + if (f == NULL) 1.411 + { 1.412 + systemMessage(IDS_CANNOT_OPEN_FILE, 1.413 + "Cannot open file %s", 1.414 + buffer); 1.415 + return; 1.416 + } 1.417 + 1.418 + MemoryViewerAddressSize dlg; 1.419 + 1.420 + fseek(f, 0, SEEK_END); 1.421 + int size = ftell(f); 1.422 + 1.423 + fseek(f, 0, SEEK_SET); 1.424 + 1.425 + dlg.setAddress(m_viewer.getCurrentAddress()); 1.426 + dlg.setSize(size); 1.427 + 1.428 + if (dlg.DoModal() == IDOK) 1.429 + { 1.430 + int size = dlg.getSize(); 1.431 + u16 addr = dlg.getAddress() & 0xffff; 1.432 + 1.433 + for (int i = 0; i < size; i++) 1.434 + { 1.435 + int c = fgetc(f); 1.436 + if (c == -1) 1.437 + break; 1.438 + gbWriteMemoryQuick(addr, c); 1.439 + addr++; 1.440 + } 1.441 + OnRefresh(); 1.442 + } 1.443 + fclose(f); 1.444 + } 1.445 +} 1.446 + 1.447 +void GBMemoryViewerDlg::PostNcDestroy() 1.448 +{ 1.449 + delete this; 1.450 +} 1.451 +