aboutsummaryrefslogtreecommitdiff
path: root/src/frontend/qt_sdl/RAMInfoDialog.cpp
blob: 5bff99adf016e835ea38d7148e4c26afb25ada57 (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
/*
    Copyright 2016-2021 Arisotura

    This file is part of melonDS.

    melonDS 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 3 of the License, or (at your option)
    any later version.

    melonDS 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 melonDS. If not, see http://www.gnu.org/licenses/.
*/

#include "RAMInfoDialog.h"
#include "ui_RAMInfoDialog.h"

#include "main.h"

using namespace melonDS;
extern EmuThread* emuThread;

s32 GetMainRAMValue(NDS& nds, const u32& addr, const ramInfo_ByteType& byteType)
{
    switch (byteType)
    {
    case ramInfo_OneByte:
        return *(s8*)(nds.MainRAM + (addr&nds.MainRAMMask));
    case ramInfo_TwoBytes:
        return *(s16*)(nds.MainRAM + (addr&nds.MainRAMMask));
    case ramInfo_FourBytes:
        return *(s32*)(nds.MainRAM + (addr&nds.MainRAMMask));
    default:
        return 0;
    }
}

RAMInfoDialog* RAMInfoDialog::currentDlg = nullptr;

RAMInfoDialog::RAMInfoDialog(QWidget* parent, EmuThread* emuThread) : QDialog(parent), emuThread(emuThread), ui(new Ui::RAMInfoDialog)
{
    ui->setupUi(this);
    setAttribute(Qt::WA_DeleteOnClose);

    qRegisterMetaType<QVector<int>>("QVector<int>");
    qRegisterMetaType<u32>("u32");
    qRegisterMetaType<s32>("s32");
    qRegisterMetaType<s16>("s16");
    qRegisterMetaType<s8>("s8");

    SearchThread = new RAMSearchThread(this);
    connect(SearchThread, &RAMSearchThread::SetProgressbarValue, this, &RAMInfoDialog::SetProgressbarValue);
    connect(SearchThread, &RAMSearchThread::finished, this, &RAMInfoDialog::OnSearchFinished);
    // First search (Show everything in main ram)
    SearchThread->Start(ramInfoSTh_SearchAll);

    TableUpdater = new QTimer(this);
    TableUpdater->setInterval(100);
    connect(TableUpdater, &QTimer::timeout, this, &RAMInfoDialog::ShowRowsInTable);
    TableUpdater->start();
}

RAMInfoDialog::~RAMInfoDialog()
{
    delete SearchThread;
    if (TableUpdater->isActive())
        TableUpdater->stop();
    delete TableUpdater;
    delete ui;
}

void RAMInfoDialog::OnSearchFinished()
{
    SearchThread->wait();
    ui->btnSearch->setEnabled(true);
    ui->ramTable->clearContents();
    ui->ramTable->setRowCount(SearchThread->GetResults()->size());
    ui->ramTable->verticalScrollBar()->setSliderPosition(0);
    ui->txtFound->setText(QString("Found: %1").arg(SearchThread->GetResults()->size()));
}

void RAMInfoDialog::ShowRowsInTable()
{
    const u32& scrollValue = ui->ramTable->verticalScrollBar()->sliderPosition();
    std::vector<ramInfo_RowData>* RowDataVector = SearchThread->GetResults();

    for (u32 row = scrollValue; row < std::min<u32>(scrollValue+25, RowDataVector->size()); row++)
    {
        ramInfo_RowData& rowData = RowDataVector->at(row);
        rowData.Update(*emuThread->NDS, SearchThread->GetSearchByteType());

        if (ui->ramTable->item(row, ramInfo_Address) == nullptr)
        {
            // A new row
            QTableWidgetItem* addressItem = new QTableWidgetItem(QString("%1").arg(rowData.Address, 8, 16));
            QTableWidgetItem* valueItem = new QTableWidgetItem(QString("%1").arg(rowData.Value));
            QTableWidgetItem* previousItem = new QTableWidgetItem(QString("%1").arg(rowData.Previous));

            addressItem->setFlags(addressItem->flags() & ~Qt::ItemIsEditable);
            valueItem->setFlags(valueItem->flags() | Qt::ItemIsEditable);
            previousItem->setFlags(previousItem->flags() & ~Qt::ItemIsEditable);

            ui->ramTable->setItem(row, ramInfo_Address, addressItem);
            ui->ramTable->setItem(row, ramInfo_Value, valueItem);
            ui->ramTable->setItem(row, ramInfo_Previous, previousItem);
        }
        else
        {
            // A row that exists
            ui->ramTable->item(row, ramInfo_Address)->setText(QString("%1").arg(rowData.Address, 8, 16));
            ui->ramTable->item(row, ramInfo_Value)->setText(QString("%1").arg(rowData.Value));
            ui->ramTable->item(row, ramInfo_Previous)->setText(QString("%1").arg(rowData.Previous));
            if (rowData.Value != rowData.Previous) 
                ui->ramTable->item(row, ramInfo_Previous)->setForeground(Qt::red);
        }
    }
}

void RAMInfoDialog::ClearTableContents()
{
    ui->ramTable->clearContents();
    ui->ramTable->setRowCount(0);
}

void RAMInfoDialog::SetProgressbarValue(const u32& value)
{
    ui->progressBar->setValue(value);
}

void RAMInfoDialog::done(int r)
{
    QDialog::done(r);
    closeDlg();
}

void RAMInfoDialog::on_btnSearch_clicked()
{
    ui->btnSearch->setEnabled(false);
    ui->radiobtn1byte->setEnabled(false);
    ui->radiobtn2bytes->setEnabled(false);
    ui->radiobtn4bytes->setEnabled(false);

    if (ui->txtSearch->text().isEmpty())
        SearchThread->Start(ramInfoSTh_SearchAll);
    else
        SearchThread->Start(ui->txtSearch->text().toInt());
    
    if (!TableUpdater->isActive())
        TableUpdater->start();
}

void RAMInfoDialog::on_btnClear_clicked()
{
    SearchThread->Stop();
    TableUpdater->stop();

    ui->radiobtn1byte->setEnabled(true);
    ui->radiobtn2bytes->setEnabled(true);
    ui->radiobtn4bytes->setEnabled(true);

    OnSearchFinished();
}

void RAMInfoDialog::on_radiobtn1byte_clicked()
{
    SearchThread->SetSearchByteType(ramInfo_OneByte);
}

void RAMInfoDialog::on_radiobtn2bytes_clicked()
{
    SearchThread->SetSearchByteType(ramInfo_TwoBytes);
}

void RAMInfoDialog::on_radiobtn4bytes_clicked()
{
    SearchThread->SetSearchByteType(ramInfo_FourBytes);
}

void RAMInfoDialog::on_ramTable_itemChanged(QTableWidgetItem *item)
{
    ramInfo_RowData& rowData = SearchThread->GetResults()->at(item->row());
    s32 itemValue = item->text().toInt();

    if (rowData.Value != itemValue)
        rowData.SetValue(*emuThread->NDS, itemValue);
}

/**
 * RAMSearchThread 
 */

RAMSearchThread::RAMSearchThread(RAMInfoDialog* dialog) : Dialog(dialog)
{
    RowDataVector = new std::vector<ramInfo_RowData>();
}

RAMSearchThread::~RAMSearchThread()
{
    Stop();
    if (RowDataVector)
    {
        delete RowDataVector;
        RowDataVector = nullptr;
    }
}

void RAMSearchThread::Start(const s32& searchValue, const ramInfoSTh_SearchMode& searchMode)
{
    SearchValue = searchValue;
    SearchMode = searchMode;
    start();
}

void RAMSearchThread::Start(const ramInfoSTh_SearchMode& searchMode)
{
    SearchMode = searchMode;
    start();
}

void RAMSearchThread::Stop()
{
    SearchRunning = false;
    RowDataVector->clear();
    quit();
    wait();
}

void RAMSearchThread::run()
{
    SearchRunning = true;
    u32 progress = 0;

    // Pause game running
    emuThread->emuPause();

    // For following search modes below, RowDataVector must be filled.
    if (SearchMode == ramInfoSTh_SearchAll || RowDataVector->size() == 0)
    {
        // First search mode
        for (u32 addr = 0x02000000; SearchRunning && addr < 0x02000000+MainRAMMaxSize; addr += SearchByteType)
        {
            const s32& value = GetMainRAMValue(*emuThread->NDS, addr, SearchByteType);

            RowDataVector->push_back({ addr, value, value });

            // A solution to prevent to call too many slot.
            u32 newProgress = (int)((addr-0x02000000) / (MainRAMMaxSize-1.0f) * 100);
            if (progress < newProgress)
            {
                progress = newProgress;
                emit SetProgressbarValue(progress);
            }
        }
    }
    
    if (SearchMode == ramInfoSTh_Default)
    {
        // Next search mode
        std::vector<ramInfo_RowData>* newRowDataVector = new std::vector<ramInfo_RowData>();
        for (u32 row = 0; SearchRunning && row < RowDataVector->size(); row++)
        {
            const u32& addr = RowDataVector->at(row).Address;
            const s32& value = GetMainRAMValue(*emuThread->NDS, addr, SearchByteType);

            if (SearchValue == value)
                newRowDataVector->push_back({ addr, value, value });

            // A solution to prevent to call too many slot.
            u32 newProgress = (int)(row / (RowDataVector->size()-1.0f) * 100);
            if (progress < newProgress)
            {
                progress = newProgress;
                emit SetProgressbarValue(progress);
            }
        }
        delete RowDataVector;
        RowDataVector = newRowDataVector;
    }

    // Unpause game running
    emuThread->emuUnpause();

    SearchRunning = false;
}

void RAMSearchThread::SetSearchByteType(const ramInfo_ByteType& bytetype)
{
    SearchByteType = bytetype;
}

ramInfo_ByteType RAMSearchThread::GetSearchByteType() const
{
    return SearchByteType;
}

std::vector<ramInfo_RowData>* RAMSearchThread::GetResults()
{
    return RowDataVector;
}