Source

wxMupen64Plus / debugger / memorypanel.cpp

  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
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
 *   wxMupen64Plus debugger                                                *
 *   Mupen64Plus homepage: http://code.google.com/p/mupen64plus/           *
 *   Copyright (C) 2012 Markus Heikkinen                                   *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program; if not, write to the                         *
 *   Free Software Foundation, Inc.,                                       *
 *   51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.          *
 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */

#include "memorypanel.h"

#include <wx/sizer.h>
#include <wx/dcclient.h>
#include <wx/dcmemory.h>
#include <wx/textctrl.h>
#include <wx/button.h>
#include <wx/menu.h>
#include "debugconfig.h"

#include "debuggerframe.h"
#include "colors.h"
#include "breakpoint.h"
#include "../mupen64plusplus/MupenAPI.h"
#ifdef DrawText
#undef DrawText // windows api ftw
#endif

// These control the drawing of MemoryWindow
#define offset_start_x 5
#define offset_start_y 5
#define data_start_x (offset_start_x + g_bold_number_width * 8 + 10)
#define data_start_y offset_start_y
#define data_inc_x (value_width + value_border_w) // Width reserved for 1 byte
#define data_add_inc_x (g_number_width) // Additional spacing after 4 bytes
#define data_inc_y (g_normal_height - 1) // Height reserved for 1 byte
#define data_clip_cols 4 // Should be multiple of 4
#define value_width (g_number_width * 2) // Actual width of 1 byte (about)
#define value_height (data_inc_y - value_border_h) // Actual height of 1 byte (about)
#define value_border_w 4 // These are total of the borders on both sides
#define value_border_h 2

enum
{
    read_break_1 = 1,
    read_break_2,
    read_break_4,
    write_break_1,
    write_break_2,
    write_break_4,
    remove_break
};

MemoryPanel::MemoryPanel(DebuggerFrame *parent, int id, int type, DebugConfigSection &config) : DebugPanel(parent, id, type)
{
    wxBoxSizer *sizer = new wxBoxSizer(wxVERTICAL), *subsizer = new wxBoxSizer(wxHORIZONTAL);
    data = 0;

    wxPanel *subpanel = new wxPanel(this);
    wxPanel *empty = new wxPanel(subpanel);
    offset_chooser = new wxTextCtrl(subpanel, -1, "0", wxDefaultPosition, wxDefaultSize, wxTE_PROCESS_ENTER);
    offset_chooser->SetMaxLength(10);
    goto_button = new wxButton(subpanel, -1, _("Go"));
    subsizer->Add(empty, 1);
    subsizer->Add(offset_chooser, 0, wxEXPAND | wxALL, 2);
    subsizer->Add(goto_button, 0, wxEXPAND | wxRIGHT | wxTOP | wxBOTTOM, 2);
    subpanel->SetSizer(subsizer);

    memory = new MemoryWindow(this, -1);

    current_position = 0;
    uint32_t addr = strtoul(config.GetValue("Address", "80000000"), 0, 16);
    Goto(addr);
    offset_chooser->SetValue(wxString::Format("%08X", addr));

    sizer->Add(subpanel, 0, wxEXPAND);
    sizer->Add(memory, 1, wxEXPAND);
    SetSizer(sizer);

    current_row = 0;
    current_col = 0;
    data_changed = true;

    goto_button->Bind(wxEVT_COMMAND_BUTTON_CLICKED, &MemoryPanel::Goto, this);
    offset_chooser->Bind(wxEVT_COMMAND_TEXT_ENTER, &MemoryPanel::Goto, this);
}

MemoryPanel::~MemoryPanel()
{
    delete[] data;
}

void MemoryPanel::SaveConfig(DebugConfigOut &config, DebugConfigSection &section)
{
    section.num_values = 1;
    char buf[16];
    sprintf(buf, "%08X", memory->GetAddress());
    section.keys[0] = "Address";
    section.values[0] = buf;
    config.WriteSection(section);
}

void MemoryPanel::Update(bool vi)
{
    data_changed = true;
    memory->Update();
}

void MemoryPanel::SetPosition(uint32_t offset, int size)
{
    if (current_position != offset)
    {
        current_position = offset;
    }
    delete[] data;
    data = new uint8_t[size];
    for (int i = 0; i < size; i++)
    {
        data[i] = MemRead8(current_position + i);
    }
    data_changed = false;
    data_size = size;
}

void MemoryPanel::Goto(wxCommandEvent &evt)
{
    memory->Goto(strtoul(offset_chooser->GetValue(), 0, 16));
}

void MemoryPanel::Goto(uint32_t address)
{
    memory->Goto(address);
    offset_chooser->SetValue(wxString::Format("%08X", address));
}

uint8_t *MemoryPanel::RequestData(int size, int relative_offset)
{
    if (relative_offset != 0 || size > data_size || data_changed)
        SetPosition(current_position + relative_offset, size);
    return data;
}

void MemoryPanel::SetValue(int pos, int value)
{
    data[pos] = value;
    MemWrite8(current_position + pos, value);
}

void MemoryPanel::BreakpointUpdate(Breakpoint *bpt, BreakUpdateCause cause, bool last_update)
{
    if (last_update)
        memory->Draw();
}

/// ------------------------------------------------------------------------------

MemoryWindow::MemoryWindow(MemoryPanel *parent_, int id) : wxWindow(parent_, id, wxDefaultPosition, wxDefaultSize, wxWANTS_CHARS)
{
    data = 0;
    parent = parent_;
    rows = 0;
    cols = 0;
    render_buffer = 0;
    address = 0;
    offsets_changed = true;
    selected = -1;
    Bind(wxEVT_PAINT, &MemoryWindow::PaintEvent, this);
    Bind(wxEVT_SIZE, &MemoryWindow::Resize, this);
    Bind(wxEVT_CHAR, &MemoryWindow::KeyDown, this);
    Bind(wxEVT_LEFT_DOWN, &MemoryWindow::MouseSelect, this);
    Bind(wxEVT_RIGHT_UP, &MemoryWindow::RClickMenu, this);
}

MemoryWindow::~MemoryWindow()
{
    delete render_buffer;
}

void MemoryWindow::Goto(uint32_t pos)
{
    if (address == pos)
        return;
    offsets_changed = true;
    data = parent->RequestData(cols * rows, pos - address);
    address = pos;
    if (!MemIsValid(address + selected))
        Deselect();
    Draw();
}

void MemoryWindow::MouseSelect(wxMouseEvent &evt)
{
    evt.Skip();
    wxPoint pos = evt.GetPosition(), last = GetValuePosition(display_size - 1);
    last.x += value_width + value_border_w / 2;
    last.y += value_height + value_border_h / 2;
    if (pos.x < data_start_x - value_border_w / 2 || pos.x > last.x || pos.y < data_start_y + value_border_h / 2 || pos.y > last.y)
    {
        Deselect();
        return;
    }

    pos.x = pos.x - data_start_x + value_border_w / 2;
    pos.y = pos.y - data_start_y - value_border_h / 2;
    int row = pos.y / data_inc_y;
    int x_incs = pos.x / (data_add_inc_x + data_inc_x * 4);
    int col = x_incs * 4;
    pos.x -= x_incs * (data_add_inc_x + data_inc_x * 4);
    col += pos.x / data_inc_x;
    last = GetValuePosition(row * cols + col);
    wxRect value_rect(last.x - value_border_w, last.y - value_border_h, last.x + value_border_w + value_width, last.y + value_border_h + value_height);
    if (value_rect.Contains(evt.GetPosition()))
       Select(row * cols + col);
    else
        Deselect();
}

void MemoryWindow::RClickMenu(wxMouseEvent &evt)
{
    MouseSelect(evt);
    if (selected != -1)
    {
        wxMenu menu;
        if (!parent->GetParent()->FindBreakpoint(address + selected))
        {
            wxMenuItem *rb1, *rb2, *rb4, *wb1, *wb2, *wb4;
            rb1 = new wxMenuItem(&menu, read_break_1, _("Read breakpoint (1 byte)"));
            menu.Append(rb1);
            if ((address + selected) % 2 == 0)
            {
                rb2 = new wxMenuItem(&menu, read_break_2, _("Read breakpoint (2 bytes)"));
                menu.Append(rb2);
            }
            if ((address + selected) % 4 == 0)
            {
                rb4 = new wxMenuItem(&menu, read_break_4, _("Read breakpoint (4 bytes)"));
                menu.Append(rb4);
            }
            wb1 = new wxMenuItem(&menu, write_break_1, _("Write breakpoint (1 byte)"));
            menu.Append(wb1);
            if ((address + selected) % 2 == 0)
            {
                wb2 = new wxMenuItem(&menu, write_break_2, _("Write breakpoint (2 bytes)"));
                menu.Append(wb2);
            }
            if ((address + selected) % 4 == 0)
            {
                wb4 = new wxMenuItem(&menu, write_break_4, _("Write breakpoint (4 bytes)"));
                menu.Append(wb4);
            }
        }
        else
        {
            wxMenuItem *rem;
            rem = new wxMenuItem(&menu, remove_break, _("Remove Breakpoint"));
            menu.Append(rem);
        }
        menu.Bind(wxEVT_COMMAND_MENU_SELECTED, &MemoryWindow::RClickEvent, this);
        PopupMenu(&menu);
    }
}

void MemoryWindow::RClickEvent(wxCommandEvent &evt)
{
    int id = evt.GetId();
    DebuggerFrame *parent_frame = parent->GetParent();
    if (id == remove_break)
    {
        parent_frame->DeleteBreakpoint(parent_frame->FindBreakpoint(address + selected));
        Draw();
    }
    else
    {
        int selected_address = address + selected, length, type;
        switch (id)
        {
            case read_break_1:
                type = BREAK_TYPE_READ;
                length = 1;
            break;
            case read_break_2:
                type = BREAK_TYPE_READ;
                length = 2;
            break;
            case read_break_4:
                type = BREAK_TYPE_READ;
                length = 4;
            break;
            case write_break_1:
                type = BREAK_TYPE_WRITE;
                length = 1;
            break;
            case write_break_2:
                type = BREAK_TYPE_WRITE;
                length = 2;
            break;
            case write_break_4:
                type = BREAK_TYPE_WRITE;
                length = 4;
            break;
            default:
                type = 0;
                length = 0;
            break;
        }
        wxString name;
        if (type == BREAK_TYPE_READ)
            name.Printf("Read (%X)", length);
        else
            name.Printf("Write (%X)", length);

        Breakpoint *bpt = new Breakpoint(name, selected_address, length, type);
        if(!parent_frame->AddBreakpoint(bpt))
            parent->Print("Unable to add a new breakpoint");
    }
}

void MemoryWindow::Resize(wxSizeEvent &evt)
{
    wxSize size = evt.GetSize();
    int new_cols = (size.GetWidth() - data_start_x + data_add_inc_x) / (data_inc_x * data_clip_cols + data_add_inc_x * data_clip_cols / 4) * data_clip_cols;
    int new_rows = (size.GetHeight() - data_start_y) / data_inc_y;
    if (!new_cols)
        return;
    if (cols != new_cols || rows != new_rows)
    {
        cols = new_cols;
        rows = new_rows;
        display_size = rows * cols;
        data = parent->RequestData(cols * rows);
    }
    delete render_buffer;
    render_buffer = new wxBitmap(size);
    wxMemoryDC dc(*render_buffer);
    dc.SetBackground(*wxWHITE_BRUSH);
    dc.Clear();
    offsets_changed = true;

    Draw();
    return;
}

void MemoryWindow::Deselect()
{
    editing = false;
    if (selected != -1)
    {
        wxMemoryDC dc(*render_buffer);
        dc.SetFont(*g_main_font);
        DrawValue(&dc, selected, wxWHITE_BRUSH);
        wxClientDC clientdc(this);
        DrawValue(&clientdc, selected, wxWHITE_BRUSH);
        selected = -1;
    }
}

void MemoryWindow::Select(int pos)
{
    editing = false;
    if (!MemIsValid(address + pos))
        return;
    if (pos < 0)
    {
        int offset_change = (pos - cols) / cols * cols;
        if(!(pos % cols))
            offset_change += cols;
        if(address + offset_change < 0)
            offset_change = 0 - address;

        address += offset_change;
        data = parent->RequestData(display_size, offset_change);
        pos = (cols + pos % cols) % cols;
    }
    else if (pos >= display_size)
    {
        int offset_change = (pos - display_size + 1) / cols * cols + cols;
        if(!((pos + 1) % cols))
            offset_change -= cols;
        if(address + offset_change + display_size < address) // overflow
            offset_change = 0 - address - display_size;
        address += offset_change;
        data = parent->RequestData(display_size, offset_change);
        pos = pos % cols + cols * (rows - 1);
    }

    selected = pos;
    Draw();
}

void MemoryWindow::Input(int value)
{
    if (selected == -1)
        return;
    if (editing)
    {
        editing = false;
        parent->SetValue(selected, (new_value << 4) | value);
        Select(selected + 1);
    }
    else
    {
        editing = true;
        new_value = value;
        Draw();
    }
}

void MemoryWindow::KeyDown(wxKeyEvent &evt)
{
    if (selected == -1)
        return;

    int code = evt.GetKeyCode();
    if (code >= '0' && code <= '9')
        Input(code - '0');
    else if (code >= 'a' && code <= 'f')
        Input(code - 'a' + 0xa);
    else if (code >= 'A' && code <= 'F')
        Input(code - 'A' + 0xa);
    else
    {
        switch (code)
        {
            case WXK_LEFT:
                Select(selected - 1);
            break;
    //        case WXK_RETURN: // nah
    //        case WXK_NUMPAD_ENTER:
            case WXK_RIGHT:
                Select(selected + 1);
            break;
            case WXK_UP:
                Select(selected - cols);
            break;
            case WXK_DOWN:
                Select(selected + cols);
            break;

            default:
                evt.Skip();
            break;
        }
    }
}

void MemoryWindow::PaintEvent(wxPaintEvent &evt)
{
    wxPaintDC dc(this);
    wxRegion damaged = GetUpdateRegion();
    if (damaged.Contains(0, 0, data_start_x + (cols + 1) * data_inc_x, data_start_y + (rows + 1) * data_inc_y) == wxOutRegion)
    {
        dc.SetBackground(*wxWHITE_BRUSH);
        dc.Clear();
        return;
    }
    dc.DrawBitmap(*render_buffer, 0, 0);
}

wxPoint MemoryWindow::GetValuePosition(int index)
{
    wxPoint ret;
    int col = (index % cols);
    ret.x = data_start_x + data_inc_x * col + col / 4 * data_add_inc_x;
    ret.y = data_start_y + data_inc_y * (index / cols);
    return ret;
}

void MemoryWindow::DrawValue(wxDC *dc, int pos, const wxBrush *bg, const char *value)
{
    wxPoint point = GetValuePosition(pos);
    if (bg)
    {
        dc->SetPen(*wxTRANSPARENT_PEN);
        dc->SetBrush(*bg);
        dc->DrawRectangle(point.x - value_border_w / 2, point.y + value_border_h / 2, value_width + value_border_w, value_height + value_border_h);
    }
    else if (Breakpoint *bpt = parent->GetParent()->FindBreakpoint(address + pos))
    {
        dc->SetPen(*wxTRANSPARENT_PEN);
        int type = bpt->GetType(), count = 0;
        if (!bpt->IsEnabled())
        {
            dc->SetBrush(g_brush_disabled);
            dc->DrawRectangle(point.x - value_border_w / 2, point.y + value_border_h / 2, value_width + value_border_w, value_height + value_border_h);
        }
        else if (type == BREAK_TYPE_ALL) // A special case to keep the color order always as exec-read-write
        {
            dc->SetBrush(g_brush_execute);
            dc->DrawRectangle(point.x - value_border_w / 2, point.y + value_border_h / 2, value_width + value_border_w, value_height + value_border_h);
            dc->SetBrush(g_brush_write);
            dc->DrawRectangle(point.x + value_width / 2, point.y + value_border_h / 2, (value_width + value_border_w) / 2, value_height + value_border_h);
            dc->SetBrush(g_brush_read);
            dc->DrawRectangle(point.x + (value_width + value_border_w) / 4, point.y + value_border_h / 2, // Yeah, / 4. It just works better..
                               (value_width + value_border_w) / 3, value_height + value_border_h);
        }
        else
        {
            if (type & BREAK_TYPE_EXECUTE)
            {
                dc->SetBrush(g_brush_execute);
                dc->DrawRectangle(point.x - value_border_w / 2, point.y + value_border_h / 2, value_width + value_border_w, value_height + value_border_h);
                count++;
            }
            if (type & BREAK_TYPE_READ)
            {
                dc->SetBrush(g_brush_read);
                if (!count)
                    dc->DrawRectangle(point.x - value_border_w / 2, point.y + value_border_h / 2, value_width + value_border_w, value_height + value_border_h);
                else
                    dc->DrawRectangle(point.x + value_width / 2, point.y + value_border_h / 2,
                                      (value_width + value_border_w) / 2, value_height + value_border_h);
                count++;
            }
            if (type & BREAK_TYPE_WRITE)
            {
                dc->SetBrush(g_brush_write);
                if (!count)
                    dc->DrawRectangle(point.x - value_border_w / 2, point.y + value_border_h / 2, value_width + value_border_w, value_height + value_border_h);
                else
                    dc->DrawRectangle(point.x + value_width / 2, point.y + value_border_h / 2,
                                      (value_width + value_border_w) / 2, value_height + value_border_h);
            }
        }
    }
    char buf[4];
    if (!MemIsValid(address + pos))
        value = "--";
    else if (!value)
    {
        sprintf(buf, "%02X", data[pos]);
        value = buf;
    }
    dc->DrawText(value, point);
}

void MemoryWindow::RenderValues(wxDC *dc)
{
    dc->SetPen(*wxTRANSPARENT_PEN);
    dc->SetBrush(*wxWHITE_BRUSH);
    wxPoint last = GetValuePosition(display_size - 1);
    dc->DrawRectangle(data_start_x - value_border_w / 2, data_start_y - value_border_h / 2, last.x + value_border_w * 2 + value_width, last.y + value_border_h + value_height);
    for (int i = 0; i < rows; i++)
    {
        for (int j = 0; j < cols; j++)
        {
            DrawValue(dc, i * cols + j);
        }
    }
}

void MemoryWindow::RenderOffsets(wxDC *dc)
{
    dc->SetPen(*wxTRANSPARENT_PEN);
    dc->SetBrush(*wxWHITE_BRUSH);
    dc->DrawRectangle(offset_start_x, offset_start_y, data_start_x - offset_start_x, data_inc_y * rows);
    for (int i = 0; i < rows; i++)
    {
        char buf[16];
        sprintf(buf, "%08X", address + i * cols);
        dc->DrawText(buf, offset_start_x, offset_start_y + data_inc_y * i);
    }
    offsets_changed = false;
}

void MemoryWindow::Render(wxDC *dc)
{
    dc->SetTextForeground(g_color_text_default);
    if (offsets_changed)
    {
        wxFont orig = dc->GetFont();
        wxFont bold = orig.Bold();
        dc->SetFont(bold);
        RenderOffsets(dc);
        dc->SetFont(orig);
    }
    RenderValues(dc);

    if (selected != -1)
    {
        dc->SetTextForeground(g_color_text_selected);
        if (editing)
        {
            char buf[4];
            sprintf(buf, "%X_", new_value);
            DrawValue(dc, selected, &g_brush_selected, buf);
        }
        else
            DrawValue(dc, selected, &g_brush_selected);
    }
}

void MemoryWindow::Draw()
{
    if (!render_buffer)
        return;

    // Destroy memorydc before using bitmap :p
    {
        wxMemoryDC dc(*render_buffer);
        dc.SetFont(*g_main_font);
        Render(&dc);
    }
    wxClientDC client_dc(this);
    client_dc.DrawBitmap(*render_buffer, 0, 0);
}

void MemoryWindow::Update()
{
    if (!cols)
        return;
    data = parent->RequestData(rows * cols);
    Draw();
}