~registry/dolphin-emu/triforce

« back to all changes in this revision

Viewing changes to Source/Core/DolphinWX/Src/Debugger/MemoryWindow.cpp

  • Committer: Sérgio Benjamim
  • Date: 2015-02-13 05:54:40 UTC
  • Revision ID: sergio_br2@yahoo.com.br-20150213055440-ey2rt3sjpy27km78
Dolphin Triforce branch from code.google, commit b957980 (4.0-315).

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
// Copyright 2013 Dolphin Emulator Project
 
2
// Licensed under GPLv2
 
3
// Refer to the license.txt file included.
 
4
 
 
5
#include <wx/button.h>
 
6
#include <wx/textctrl.h>
 
7
#include <wx/listctrl.h>
 
8
#include <wx/thread.h>
 
9
#include <wx/listctrl.h>
 
10
 
 
11
#include "../WxUtils.h"
 
12
#include "MemoryWindow.h"
 
13
#include "HW/CPU.h"
 
14
#include "PowerPC/PowerPC.h"
 
15
#include "FileUtil.h"
 
16
 
 
17
#include "Debugger/PPCDebugInterface.h"
 
18
#include "PowerPC/PPCSymbolDB.h"
 
19
 
 
20
#include "Core.h"
 
21
#include "ConfigManager.h"
 
22
#include "LogManager.h"
 
23
 
 
24
#include "HW/Memmap.h"
 
25
#include "HW/DSP.h"
 
26
 
 
27
#include "../../DolphinWX/Src/Globals.h"
 
28
 
 
29
enum
 
30
{
 
31
        IDM_MEM_ADDRBOX = 350,
 
32
        IDM_SYMBOLLIST,
 
33
        IDM_SETVALBUTTON,
 
34
        IDM_DUMP_MEMORY,
 
35
        IDM_DUMP_MEM2,
 
36
        IDM_DUMP_FAKEVMEM,
 
37
        IDM_VALBOX,
 
38
        IDM_U8,
 
39
        IDM_U16,
 
40
        IDM_U32,
 
41
        IDM_SEARCH,
 
42
        IDM_ASCII,
 
43
        IDM_HEX
 
44
};
 
45
 
 
46
BEGIN_EVENT_TABLE(CMemoryWindow, wxPanel)
 
47
        EVT_TEXT(IDM_MEM_ADDRBOX,               CMemoryWindow::OnAddrBoxChange)
 
48
        EVT_LISTBOX(IDM_SYMBOLLIST,             CMemoryWindow::OnSymbolListChange)
 
49
        EVT_HOST_COMMAND(wxID_ANY,              CMemoryWindow::OnHostMessage)
 
50
        EVT_BUTTON(IDM_SETVALBUTTON,    CMemoryWindow::SetMemoryValue)
 
51
        EVT_BUTTON(IDM_DUMP_MEMORY,             CMemoryWindow::OnDumpMemory)
 
52
        EVT_BUTTON(IDM_DUMP_MEM2,               CMemoryWindow::OnDumpMem2)
 
53
        EVT_BUTTON(IDM_DUMP_FAKEVMEM,   CMemoryWindow::OnDumpFakeVMEM)
 
54
        EVT_CHECKBOX(IDM_U8,                    CMemoryWindow::U8)
 
55
        EVT_CHECKBOX(IDM_U16,                   CMemoryWindow::U16)
 
56
        EVT_CHECKBOX(IDM_U32,                   CMemoryWindow::U32)
 
57
        EVT_BUTTON(IDM_SEARCH,                  CMemoryWindow::onSearch)
 
58
        EVT_CHECKBOX(IDM_ASCII,                 CMemoryWindow::onAscii)
 
59
        EVT_CHECKBOX(IDM_HEX,                   CMemoryWindow::onHex)
 
60
END_EVENT_TABLE()
 
61
 
 
62
CMemoryWindow::CMemoryWindow(wxWindow* parent, wxWindowID id,
 
63
                const wxPoint& pos, const wxSize& size, long style, const wxString& name)
 
64
        : wxPanel(parent, id, pos, size, style, name)
 
65
{
 
66
        wxBoxSizer* sizerBig   = new wxBoxSizer(wxHORIZONTAL);
 
67
        wxBoxSizer* sizerRight = new wxBoxSizer(wxVERTICAL);
 
68
        // Didn't see anything useful in the left part
 
69
        //wxBoxSizer* sizerLeft  = new wxBoxSizer(wxVERTICAL);
 
70
 
 
71
        DebugInterface* di = &PowerPC::debug_interface;
 
72
 
 
73
        //symbols = new wxListBox(this, IDM_SYMBOLLIST, wxDefaultPosition,
 
74
        //              wxSize(20, 100), 0, NULL, wxLB_SORT);
 
75
        //sizerLeft->Add(symbols, 1, wxEXPAND);
 
76
        memview = new CMemoryView(di, this);
 
77
        memview->dataType = 0;
 
78
        //sizerBig->Add(sizerLeft, 1, wxEXPAND);
 
79
        sizerBig->Add(memview, 20, wxEXPAND);
 
80
        sizerBig->Add(sizerRight, 0, wxEXPAND | wxALL, 3);
 
81
        sizerRight->Add(addrbox = new wxTextCtrl(this, IDM_MEM_ADDRBOX, _T("")));
 
82
        sizerRight->Add(valbox = new wxTextCtrl(this, IDM_VALBOX, _T("")));
 
83
        sizerRight->Add(new wxButton(this, IDM_SETVALBUTTON, _("Set &Value")));
 
84
 
 
85
        sizerRight->AddSpacer(5);
 
86
        sizerRight->Add(new wxButton(this, IDM_DUMP_MEMORY, _("&Dump MRAM")));
 
87
        sizerRight->Add(new wxButton(this, IDM_DUMP_MEM2, _("&Dump EXRAM")));
 
88
 
 
89
        if (SConfig::GetInstance().m_LocalCoreStartupParameter.bTLBHack == true)
 
90
                sizerRight->Add(new wxButton(this, IDM_DUMP_FAKEVMEM, _("&Dump FakeVMEM")));
 
91
 
 
92
        wxStaticBoxSizer* sizerSearchType = new wxStaticBoxSizer(wxVERTICAL, this, _("Search"));
 
93
 
 
94
        sizerSearchType->Add(btnSearch = new wxButton(this, IDM_SEARCH, _("Search")));
 
95
        sizerSearchType->Add(chkAscii = new wxCheckBox(this, IDM_ASCII, _T("&Ascii ")));
 
96
        sizerSearchType->Add(chkHex = new wxCheckBox(this, IDM_HEX, _("&Hex")));
 
97
        sizerRight->Add(sizerSearchType);
 
98
        wxStaticBoxSizer* sizerDataTypes = new wxStaticBoxSizer(wxVERTICAL, this, _("Data Type"));
 
99
 
 
100
        sizerDataTypes->SetMinSize(74, 40);
 
101
        sizerDataTypes->Add(chk8 = new wxCheckBox(this, IDM_U8, _T("&U8")));
 
102
        sizerDataTypes->Add(chk16 = new wxCheckBox(this, IDM_U16, _T("&U16")));
 
103
        sizerDataTypes->Add(chk32 = new wxCheckBox(this, IDM_U32, _T("&U32")));
 
104
        sizerRight->Add(sizerDataTypes);
 
105
        SetSizer(sizerBig);
 
106
        chkHex->SetValue(1); //Set defaults
 
107
        chk8->SetValue(1);
 
108
 
 
109
        //sizerLeft->Fit(this);
 
110
        sizerRight->Fit(this);
 
111
        sizerBig->Fit(this);
 
112
}
 
113
 
 
114
void CMemoryWindow::Save(IniFile& _IniFile) const
 
115
{
 
116
        // Prevent these bad values that can happen after a crash or hanging
 
117
        if(GetPosition().x != -32000 && GetPosition().y != -32000)
 
118
        {
 
119
                _IniFile.Set("MemoryWindow", "x", GetPosition().x);
 
120
                _IniFile.Set("MemoryWindow", "y", GetPosition().y);
 
121
                _IniFile.Set("MemoryWindow", "w", GetSize().GetWidth());
 
122
                _IniFile.Set("MemoryWindow", "h", GetSize().GetHeight());
 
123
        }
 
124
}
 
125
 
 
126
void CMemoryWindow::Load(IniFile& _IniFile)
 
127
{
 
128
        int x, y, w, h;
 
129
        _IniFile.Get("MemoryWindow", "x", &x, GetPosition().x);
 
130
        _IniFile.Get("MemoryWindow", "y", &y, GetPosition().y);
 
131
        _IniFile.Get("MemoryWindow", "w", &w, GetSize().GetWidth());
 
132
        _IniFile.Get("MemoryWindow", "h", &h, GetSize().GetHeight());
 
133
        SetSize(x, y, w, h);
 
134
}
 
135
 
 
136
void CMemoryWindow::JumpToAddress(u32 _Address)
 
137
{
 
138
        memview->Center(_Address);
 
139
}
 
140
 
 
141
void CMemoryWindow::SetMemoryValue(wxCommandEvent& event)
 
142
{
 
143
        std::string str_addr = WxStrToStr(addrbox->GetValue());
 
144
        std::string str_val = WxStrToStr(valbox->GetValue());
 
145
        u32 addr;
 
146
        u32 val;
 
147
 
 
148
        if (!TryParse(std::string("0x") + str_addr, &addr))
 
149
        {
 
150
                PanicAlert("Invalid Address: %s", str_addr.c_str());
 
151
                return;
 
152
        }
 
153
 
 
154
        if (!TryParse(std::string("0x") + str_val, &val))
 
155
        {
 
156
                PanicAlert("Invalid Value: %s", str_val.c_str());
 
157
                return;
 
158
        }
 
159
 
 
160
        Memory::Write_U32(val, addr);
 
161
        memview->Refresh();
 
162
}
 
163
 
 
164
void CMemoryWindow::OnAddrBoxChange(wxCommandEvent& event)
 
165
{
 
166
        wxString txt = addrbox->GetValue();
 
167
        if (txt.size())
 
168
        {
 
169
                u32 addr;
 
170
                sscanf(WxStrToStr(txt).c_str(), "%08x", &addr);
 
171
                memview->Center(addr & ~3);
 
172
        }
 
173
 
 
174
        event.Skip(1);
 
175
}
 
176
 
 
177
void CMemoryWindow::Update()
 
178
{
 
179
        memview->Refresh();
 
180
        memview->Center(PC);
 
181
}
 
182
 
 
183
void CMemoryWindow::NotifyMapLoaded()
 
184
{
 
185
        symbols->Show(false); // hide it for faster filling
 
186
        symbols->Clear();
 
187
#if 0
 
188
        #ifdef _WIN32
 
189
        const FunctionDB::XFuncMap &syms = g_symbolDB.Symbols();
 
190
        for (FuntionDB::XFuncMap::iterator iter = syms.begin(); iter != syms.end(); ++iter)
 
191
        {
 
192
        int idx = symbols->Append(iter->second.name.c_str());
 
193
        symbols->SetClientData(idx, (void*)&iter->second);
 
194
        }
 
195
        #endif
 
196
#endif
 
197
        symbols->Show(true);
 
198
        Update();
 
199
}
 
200
 
 
201
void CMemoryWindow::OnSymbolListChange(wxCommandEvent& event)
 
202
{
 
203
        int index = symbols->GetSelection();
 
204
        if (index >= 0)
 
205
        {
 
206
                Symbol* pSymbol = static_cast<Symbol *>(symbols->GetClientData(index));
 
207
                if (pSymbol != NULL)
 
208
                {
 
209
                        memview->Center(pSymbol->address);
 
210
                }
 
211
        }
 
212
}
 
213
 
 
214
void CMemoryWindow::OnHostMessage(wxCommandEvent& event)
 
215
{
 
216
        switch (event.GetId())
 
217
        {
 
218
                case IDM_NOTIFYMAPLOADED:
 
219
                        NotifyMapLoaded();
 
220
                        break;
 
221
        }
 
222
}
 
223
 
 
224
void DumpArray(const std::string& filename, const u8* data, size_t length)
 
225
{
 
226
        if (data)
 
227
        {
 
228
                File::IOFile f(filename, "wb");
 
229
                f.WriteBytes(data, length);
 
230
        }
 
231
}
 
232
 
 
233
// Write mram to file
 
234
void CMemoryWindow::OnDumpMemory( wxCommandEvent& event )
 
235
{
 
236
        DumpArray(File::GetUserPath(F_RAMDUMP_IDX), Memory::m_pRAM, Memory::REALRAM_SIZE);
 
237
}
 
238
 
 
239
// Write exram (aram or mem2) to file
 
240
void CMemoryWindow::OnDumpMem2( wxCommandEvent& event )
 
241
{       
 
242
        if (SConfig::GetInstance().m_LocalCoreStartupParameter.bWii)
 
243
        {
 
244
                DumpArray(File::GetUserPath(F_ARAMDUMP_IDX), Memory::m_pEXRAM, Memory::EXRAM_SIZE);
 
245
        }
 
246
        else
 
247
        {
 
248
                DumpArray(File::GetUserPath(F_ARAMDUMP_IDX), DSP::GetARAMPtr(), DSP::ARAM_SIZE);
 
249
        }
 
250
}
 
251
 
 
252
// Write fake vmem to file
 
253
void CMemoryWindow::OnDumpFakeVMEM( wxCommandEvent& event )
 
254
{
 
255
        DumpArray(File::GetUserPath(F_FAKEVMEMDUMP_IDX), Memory::m_pVirtualFakeVMEM, Memory::FAKEVMEM_SIZE);
 
256
}
 
257
 
 
258
void CMemoryWindow::U8(wxCommandEvent& event)
 
259
{
 
260
        chk16->SetValue(0);
 
261
        chk32->SetValue(0);
 
262
        memview->dataType = 0;
 
263
        memview->Refresh();
 
264
}
 
265
 
 
266
void CMemoryWindow::U16(wxCommandEvent& event)
 
267
{
 
268
        chk8->SetValue(0);
 
269
        chk32->SetValue(0);
 
270
        memview->dataType = 1;
 
271
        memview->Refresh();
 
272
}
 
273
 
 
274
void CMemoryWindow::U32(wxCommandEvent& event)
 
275
{
 
276
        chk16->SetValue(0);
 
277
        chk8->SetValue(0);
 
278
        memview->dataType = 2;
 
279
        memview->Refresh();
 
280
}
 
281
 
 
282
void CMemoryWindow::onSearch(wxCommandEvent& event)
 
283
{
 
284
        u8* TheRAM = 0;
 
285
        u32 szRAM = 0;
 
286
        switch (memview->GetMemoryType())
 
287
        {
 
288
        case 0:
 
289
        default:
 
290
                if (Memory::m_pRAM)
 
291
                {
 
292
                        TheRAM = Memory::m_pRAM;
 
293
                        szRAM = Memory::REALRAM_SIZE;
 
294
                }
 
295
                break;
 
296
        case 1:
 
297
                {
 
298
                        u8* aram = DSP::GetARAMPtr();
 
299
                        if (aram)
 
300
                        {
 
301
                                TheRAM = aram;
 
302
                                szRAM = DSP::ARAM_SIZE;
 
303
                        }
 
304
                }
 
305
                break;
 
306
        }
 
307
        //Now we have memory to look in
 
308
        //Are we looking for ASCII string, or hex?
 
309
        //memview->cu
 
310
        wxString rawData = valbox->GetValue();
 
311
        std::vector<u8> Dest; //May need a better name
 
312
        u32 size = 0;
 
313
        int pad = rawData.size()%2; //If it's uneven
 
314
        unsigned int i = 0;
 
315
        long count = 0;
 
316
        char copy[3] = {0};
 
317
        long newsize = 0;
 
318
        unsigned char *tmp2 = 0;
 
319
        char* tmpstr = 0;
 
320
 
 
321
        if (chkHex->GetValue())
 
322
        {
 
323
                //We are looking for hex
 
324
                //If it's uneven
 
325
                size = (rawData.size()/2) + pad;
 
326
                Dest.resize(size+32);
 
327
                newsize = rawData.size();
 
328
 
 
329
                if (pad)
 
330
                {
 
331
                        tmpstr = new char[newsize + 2];
 
332
                        memset(tmpstr, 0, newsize + 2);
 
333
                        tmpstr[0] = '0';
 
334
                }
 
335
                else
 
336
                {
 
337
                        tmpstr = new char[newsize + 1];
 
338
                        memset(tmpstr, 0, newsize + 1);
 
339
                }
 
340
                sprintf(tmpstr, "%s%s", tmpstr, WxStrToStr(rawData).c_str());
 
341
                tmp2 = &Dest.front();
 
342
                count = 0;
 
343
                for(i = 0; i < strlen(tmpstr); i++)
 
344
                {
 
345
                        copy[0] = tmpstr[i];
 
346
                        copy[1] = tmpstr[i+1];
 
347
                        copy[2] = 0;
 
348
                        int tmpint;
 
349
                        sscanf(copy, "%02x", &tmpint);
 
350
                        tmp2[count++] = tmpint;
 
351
                        // Dest[count] should now be the hex of what the two chars were!
 
352
                        // Also should add a check to make sure it's A-F only
 
353
                        //sscanf(copy, "%02x", &tmp2[count++]);
 
354
                        i += 1;
 
355
                }
 
356
                delete[] tmpstr;
 
357
        }
 
358
        else
 
359
        {
 
360
                //Looking for an ascii string
 
361
                size = rawData.size();
 
362
                Dest.resize(size+1);
 
363
                tmpstr = new char[size+1];
 
364
 
 
365
                tmp2 = &Dest.front();
 
366
                sprintf(tmpstr, "%s", WxStrToStr(rawData).c_str());
 
367
 
 
368
                for(i = 0; i < size; i++)
 
369
                        tmp2[i] = tmpstr[i];
 
370
 
 
371
                delete[] tmpstr;
 
372
        }
 
373
 
 
374
        if(size)
 
375
        {
 
376
                unsigned char* pnt = &Dest.front();
 
377
                unsigned int k = 0;
 
378
                //grab
 
379
                wxString txt = addrbox->GetValue();
 
380
                u32 addr = 0;
 
381
                if (txt.size())
 
382
                {
 
383
                        sscanf(WxStrToStr(txt).c_str(), "%08x", &addr);
 
384
                }
 
385
                i = addr+4;
 
386
                for( ; i < szRAM; i++)
 
387
                {
 
388
                        for(k = 0; k < size; k++)
 
389
                        {
 
390
                                if(i + k > szRAM) break;
 
391
                                if(k > size) break;
 
392
                                if(pnt[k] != TheRAM[i+k])
 
393
                                {
 
394
                                        k = 0;
 
395
                                        break;
 
396
                                }
 
397
                        }
 
398
                        if(k == size)
 
399
                        {
 
400
                                //Match was found
 
401
                                wxMessageBox(_("A match was found. Placing viewer at the offset."));
 
402
                                wxChar tmpwxstr[128] = {0};
 
403
                                wxSprintf(tmpwxstr, _T("%08x"), i);
 
404
                                wxString tmpwx(tmpwxstr);
 
405
                                addrbox->SetValue(tmpwx);
 
406
                                //memview->curAddress = i;
 
407
                                //memview->Refresh();
 
408
                                OnAddrBoxChange(event);
 
409
                                return;
 
410
                        }
 
411
                }
 
412
                wxMessageBox(_("No match was found."));
 
413
        }
 
414
}
 
415
 
 
416
void CMemoryWindow::onAscii(wxCommandEvent& event)
 
417
{
 
418
        chkHex->SetValue(0);
 
419
}
 
420
 
 
421
void CMemoryWindow::onHex(wxCommandEvent& event)
 
422
{
 
423
        chkAscii->SetValue(0);
 
424
}