summaryrefslogtreecommitdiff
path: root/Source/Core/DolphinWX/Debugger/WatchView.cpp
blob: be5c5dad64bdce1ebf40da0f04fbd8ae8e86dae1 (plain)
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
// Copyright 2014 Dolphin Emulator Project
// Licensed under GPLv2+
// Refer to the license.txt file included.

#include <wx/colour.h>
#include <wx/grid.h>
#include <wx/menu.h>

#include "Common/GekkoDisassembler.h"
#include "Core/HW/Memmap.h"
#include "Core/PowerPC/PowerPC.h"
#include "DolphinWX/Frame.h"
#include "DolphinWX/WxUtils.h"
#include "DolphinWX/Debugger/BreakpointWindow.h"
#include "DolphinWX/Debugger/CodeWindow.h"
#include "DolphinWX/Debugger/DebuggerUIUtil.h"
#include "DolphinWX/Debugger/MemoryWindow.h"
#include "DolphinWX/Debugger/RegisterView.h"
#include "DolphinWX/Debugger/WatchView.h"
#include "DolphinWX/Debugger/WatchWindow.h"

enum
{
	IDM_DELETEWATCH = 1,
	IDM_ADDMEMCHECK,
	IDM_VIEWMEMORY,
};


static std::string GetWatchName(int count)
{
	return PowerPC::watches.GetWatches().at(count - 1).name;
}

static u32 GetWatchAddr(int count)
{
	return PowerPC::watches.GetWatches().at(count - 1).iAddress;
}

static u32 GetWatchValue(int count)
{
	return PowerPC::HostRead_U32(GetWatchAddr(count));
}

static void AddWatchAddr(int count, u32 value)
{
	PowerPC::watches.Add(value);
}

static void UpdateWatchAddr(int count, u32 value)
{
	PowerPC::watches.Update(count - 1, value);
}

static void SetWatchName(int count, const std::string& value)
{
	if ((count - 1) < (int)PowerPC::watches.GetWatches().size())
	{
		PowerPC::watches.UpdateName(count - 1, value);
	}
	else
	{
		PowerPC::watches.Add(0);
		PowerPC::watches.UpdateName(PowerPC::watches.GetWatches().size() - 1, value);
	}
}

static void SetWatchValue(int count, u32 value)
{
	PowerPC::HostWrite_U32(value, GetWatchAddr(count));
}

static wxString GetValueByRowCol(int row, int col)
{
	if (row == 0)
	{
		// Column Labels
		switch (col)
		{
		case 0: return _("Label");
		case 1: return _("Address");
		case 2: return _("Hexadecimal");
		case 3: return _("Decimal");
		case 4: return _("String");
		default: return wxEmptyString;
		}
	}
	else if (row <= (int)PowerPC::watches.GetWatches().size())
	{
		if (PowerPC::GetState() != PowerPC::CPU_POWERDOWN)
		{
			switch (col)
			{
			case 0: return wxString::Format("%s", GetWatchName(row));
			case 1: return wxString::Format("%08x", GetWatchAddr(row));
			case 2: return wxString::Format("%08x", GetWatchValue(row));
			case 3: return wxString::Format("%u", GetWatchValue(row));
			case 4:
			{
				u32 addr = GetWatchAddr(row);
				if (PowerPC::HostIsRAMAddress(addr))
					return PowerPC::HostGetString(addr, 32).c_str();
				else
					return wxEmptyString;
			}
			default: return wxEmptyString;
			}
		}
	}
	return wxEmptyString;
}

wxString CWatchTable::GetValue(int row, int col)
{
	return GetValueByRowCol(row, col);
}

void CWatchTable::SetValue(int row, int col, const wxString& strNewVal)
{
	u32 newVal = 0;
	if (col == 0 || TryParse("0x" + WxStrToStr(strNewVal), &newVal))
	{
		if (row > 0)
		{
			switch (col)
			{
			case 0:
			{
				SetWatchName(row, std::string(WxStrToStr(strNewVal)));
				break;
			}
			case 1:
			{
				if (row > (int)PowerPC::watches.GetWatches().size())
				{
					AddWatchAddr(row, newVal);
					row = (int)PowerPC::watches.GetWatches().size();
				}
				else
				{
					UpdateWatchAddr(row, newVal);
				}
				break;
			}
			case 2:
			{
				SetWatchValue(row, newVal);
				break;
			}
			default:
				break;
			}
		}
	}
}

void CWatchTable::UpdateWatch()
{
	for (int i = 0; i < (int)PowerPC::watches.GetWatches().size(); ++i)
	{
		m_CachedWatchHasChanged[i] = (m_CachedWatch[i] != GetWatchValue(i + 1));
		m_CachedWatch[i] = GetWatchValue(i + 1);
	}
}

wxGridCellAttr* CWatchTable::GetAttr(int row, int col, wxGridCellAttr::wxAttrKind)
{
	wxGridCellAttr* attr = new wxGridCellAttr();

	attr->SetBackgroundColour(*wxWHITE);
	attr->SetFont(DebuggerFont);

	switch (col)
	{
	case 1:
		attr->SetAlignment(wxALIGN_LEFT, wxALIGN_CENTER);
		break;
	case 3:
	case 4:
		attr->SetAlignment(wxALIGN_LEFT, wxALIGN_CENTER);
		break;
	default:
		attr->SetAlignment(wxALIGN_LEFT, wxALIGN_CENTER);
		break;
	}

	if (row == 0)
	{
		attr->SetReadOnly(true);
		attr->SetBackgroundColour(*wxBLACK);
		attr->SetTextColour(*wxWHITE);
	}
	else
	{
		bool red = false;
		if (col == 1)
			red = m_CachedWatchHasChanged[row];

		attr->SetTextColour(red ? *wxRED : *wxBLACK);

		if (row > (int)(PowerPC::watches.GetWatches().size() + 1) || (PowerPC::GetState() == PowerPC::CPU_POWERDOWN))
		{
			attr->SetReadOnly(true);
			attr->SetBackgroundColour(*wxLIGHT_GREY);
		}
	}

	return attr;
}

CWatchView::CWatchView(wxWindow* parent, wxWindowID id)
	: wxGrid(parent, id)
{
	m_watch_table = new CWatchTable();

	SetTable(m_watch_table, true);
	SetRowLabelSize(0);
	SetColLabelSize(0);
	DisableDragRowSize();

	Bind(wxEVT_GRID_CELL_RIGHT_CLICK, &CWatchView::OnMouseDownR, this);
	Bind(wxEVT_MENU, &CWatchView::OnPopupMenu, this);
}

void CWatchView::Update()
{
	if (PowerPC::GetState() != PowerPC::CPU_POWERDOWN)
	{
		m_watch_table->UpdateWatch();
		ForceRefresh();
	}
}

void CWatchView::OnMouseDownR(wxGridEvent& event)
{
	// popup menu
	int row = event.GetRow();
	int col = event.GetCol();

	m_selectedRow = row;

	if (col == 1 || col == 2)
	{
		wxString strNewVal = GetValueByRowCol(row, col);
		TryParse("0x" + WxStrToStr(strNewVal), &m_selectedAddress);
	}

	wxMenu menu;
	if (row != 0 && row != (int)(PowerPC::watches.GetWatches().size() + 1))
		menu.Append(IDM_DELETEWATCH, _("&Delete watch"));

	if (row != 0 && row != (int)(PowerPC::watches.GetWatches().size() + 1) && (col == 1 || col == 2))
	{
#ifdef ENABLE_MEM_CHECK
		menu.Append(IDM_ADDMEMCHECK, _("Add memory &breakpoint"));
#endif
		menu.Append(IDM_VIEWMEMORY, _("View &memory"));
	}
	PopupMenu(&menu);
}

void CWatchView::OnPopupMenu(wxCommandEvent& event)
{
	CFrame* main_frame = (CFrame*)(GetParent()->GetParent());
	CCodeWindow* code_window = main_frame->g_pCodeWindow;
	CWatchWindow* watch_window = code_window->m_WatchWindow;
	CMemoryWindow* memory_window = code_window->m_MemoryWindow;
	CBreakPointWindow* breakpoint_window = code_window->m_BreakpointWindow;

	wxString strNewVal;
	TMemCheck MemCheck;

	switch (event.GetId())
	{
	case IDM_DELETEWATCH:
		strNewVal = GetValueByRowCol(m_selectedRow, 1);
		if (TryParse("0x" + WxStrToStr(strNewVal), &m_selectedAddress))
		{
			PowerPC::watches.Remove(m_selectedAddress);
			if (watch_window)
				watch_window->NotifyUpdate();
			Refresh();
		}
		break;
	case IDM_ADDMEMCHECK:
		MemCheck.StartAddress = m_selectedAddress;
		MemCheck.EndAddress = m_selectedAddress;
		MemCheck.bRange = false;
		MemCheck.OnRead = true;
		MemCheck.OnWrite = true;
		MemCheck.Log = true;
		MemCheck.Break = true;
		PowerPC::memchecks.Add(MemCheck);

		if (breakpoint_window)
			breakpoint_window->NotifyUpdate();
		Refresh();
		break;
	case IDM_VIEWMEMORY:
		if (memory_window)
			memory_window->JumpToAddress(m_selectedAddress);
		Refresh();
		break;
	}
	event.Skip();
}