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