-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathrom_buffer.cpp
More file actions
348 lines (320 loc) · 8.76 KB
/
rom_buffer.cpp
File metadata and controls
348 lines (320 loc) · 8.76 KB
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
#include "rom_buffer.h"
#include "undo_commands.h"
#include "debug.h"
#include "utility.h"
#include "character_mapper.h"
ROM_buffer::ROM_buffer(QString file_name, bool new_file)
{
if(!new_file){
open(file_name);
get_rats_tags();
}else{
buffer.fill(0x00, 0x8000);
}
clipboard = QApplication::clipboard(); //shared by all, but work around static initialization order
qDebug() << ENUM_STRING(memory_mapper, get_mapper());
}
void ROM_buffer::remove_copy_header()
{
header_buffer = buffer.mid(0, header_size());
buffer.remove(0, header_size());
}
void ROM_buffer::open(QString path)
{
if(ROM.isOpen()){
ROM.close();
}
ROM.setFileName(path);
ROM.open(QFile::ReadWrite);
buffer = ROM.readAll();
if(ROM.size() < 0x8000){
ROM_error = "The ROM is too small to be valid.";
return;
}
analyze();
}
void ROM_buffer::save(QString path)
{
QFileInfo info(ROM);
if(path != "" && path != info.absolutePath()){
ROM.close();
ROM.setFileName(path);
ROM.open(QFile::ReadWrite);
}
if(header_size()){
ROM.seek(0);
ROM.write(header_buffer);
}
ROM.seek(header_size());
ROM.write(buffer);
}
void ROM_buffer::initialize_undo(QUndoGroup *undo_group)
{
undo_stack = new QUndoStack(undo_group);
undo_stack->setActive();
}
void ROM_buffer::cut(int start, int end, bool ascii_mode)
{
undo_stack->beginMacro("Cut");
copy(start, end, ascii_mode);
delete_text(start, end);
undo_stack->endMacro();
}
void ROM_buffer::copy(int start, int end, bool ascii_mode)
{
if(ascii_mode){
QByteArray text_data = character_mapper::encode(buffer.mid(start, end-start));
for(int i = 0; i < text_data.length(); i++){
if(!isprint(character_mapper::encode(text_data.at(i)))){
text_data[i] = '.';
}
}
clipboard->setText(text_data);
return;
}
QString copy_data = copy_format(start, end, copy_type);
clipboard->setText(copy_data);
}
QString ROM_buffer::copy_format(int start, int end, copy_style style)
{
QByteArray hex_data = buffer.mid(start, end-start).toHex().toUpper();
int nibble_count = hex_data.length();
QString copy_data;
QTextStream stream(©_data);
switch(style){
case NO_SPACES:
copy_data = hex_data;
break;
case SPACES:
for(int i = 0; i < nibble_count; i+=2){
stream << hex_data[i] << hex_data[i+1] << ' ';
}
copy_data.chop(1);
break;
case HEX_FORMAT:
for(int i = 0; i < nibble_count; i+=2){
stream << '$' << hex_data[i] << hex_data[i+1] << ", ";
}
copy_data.chop(2);
break;
case ASM_BYTE_TABLE:
for(int i = 0; i < nibble_count;){
stream << "db ";
for(int j = 0; j < 16 && i < nibble_count; j += 2, i += 2){
stream << '$' << hex_data[i] << hex_data[i+1] << ", ";
}
copy_data.chop(2);
stream << '\n';
}
copy_data.chop(1);
break;
case ASM_WORD_TABLE:
for(int i = 0; nibble_count - i >= 4; i += 4){
stream << "dw $"
<< hex_data[i+2] << hex_data[i+3]
<< hex_data[i] << hex_data[i+1] << '\n';
}
copy_data.chop(1);
break;
case ASM_LONG_TABLE:
for(int i = 0; nibble_count - i >= 6; i += 6){
stream << "dl $"
<< hex_data[i+4] << hex_data[i+5]
<< hex_data[i+2] << hex_data[i+3]
<< hex_data[i] << hex_data[i+1] << '\n';
}
copy_data.chop(1);
break;
case C_SOURCE:
stream << "const unsigned char hex_data[] = {\n";
for(int i = 0; i < nibble_count;){
stream << '\t';
for(int j = 0; j < 24 && i < nibble_count; j+=2, i+=2){
stream << "0x" << hex_data[i] << hex_data[i+1] << ",";
}
copy_data.chop(1);
stream << ",\n";
}
copy_data.chop(2);
stream << "\n};";
break;
}
return copy_data;
}
int ROM_buffer::paste(int start, int end, bool raw)
{
if(!check_paste_data()){
return 0;
}
QString copy_data = clipboard->text().toUtf8().trimmed();
QByteArray hex_data;
if(!raw){
if(copy_data.indexOf("const unsigned char") != -1){
copy_data.remove(0, copy_data.indexOf('{'));
}else if(copy_data.indexOf("db $") == 0){
copy_data.remove(0, 3);
}
copy_data.remove(QRegExp("(0x|[\\t ])"));
copy_data.remove(QRegExp("([\\n\\r]db|dw|dl|[^0-9A-Fa-f])"));
hex_data = hex_data.fromHex(copy_data.toUtf8());
}else{
hex_data = hex_data.fromHex(copy_data.toUtf8().toHex());
}
undo_stack->beginMacro("Paste");
if(end){
delete_text(start, end);
}
buffer.insert(start, hex_data);
undo_stack->push(new undo_paste_command(&buffer, start, new QByteArray(hex_data)));
undo_stack->endMacro();
return hex_data.size();
}
void ROM_buffer::delete_text(int start, int end)
{
if(!end){
end = start + 1;
}
undo_stack->beginMacro("Delete");
QByteArray data(buffer.mid(start, end-start));
undo_stack->push(new undo_delete_command(&buffer, start, new QByteArray(data)));
undo_stack->endMacro();
buffer.remove(start, end-start);
}
void ROM_buffer::update_nibble(char byte, int position, int delete_start, int delete_end)
{
bool remove = false;
if(position/2 == buffer.size()){
buffer[position/2] = 0;
remove = true;
}
undo_stack->beginMacro("Typing");
if(delete_end){
delete_text(delete_start, delete_end);
}
unsigned char data[2] = {(unsigned char)buffer[position/2], 0};
buffer[position/2] = (buffer.at(position/2) &
((0x0F >> ((position & 1) << 2)) | (0x0F << ((position & 1) << 2)))) |
(byte << (((position & 1)^1) << 2));
data[1] = buffer[position/2];
undo_stack->push(new undo_nibble_command(&buffer, position/2, data, remove));
undo_stack->endMacro();
}
void ROM_buffer::update_byte(char byte, int position, int delete_start, int delete_end)
{
bool remove = false;
if(position == buffer.size()){
buffer[position] = 0;
remove = true;
}
undo_stack->beginMacro("Typing");
if(delete_end){
delete_text(delete_start, delete_end);
}
unsigned char data[2] = {(unsigned char)buffer[position],(unsigned char)byte};
undo_stack->push(new undo_byte_command(&buffer, position, data, remove));
buffer[position] = byte;
undo_stack->endMacro();
}
QString ROM_buffer::get_formatted_address(int address) const
{
address = pc_to_snes(address);
int bank = address >> 16;
int word = address & 0xFFFF;
if(address < 0){
return QString("NOT:ROM");
}
return '$' + to_hex(bank) + ':' + to_hex(word, 4);
}
int ROM_buffer::count(QString find, bool mode)
{
QByteArray search_for = input_to_byte_array(find, mode);
if(search_for.isEmpty()){
return INVALID_FIND;
}
return buffer.count(search_for);
}
int ROM_buffer::search(QString find, int position, bool direction, bool mode)
{
position /= 2;
QByteArray search_for = input_to_byte_array(find, mode);
if(search_for.isEmpty()){
return INVALID_FIND;
}
int found_at = NOT_FOUND;
for(int pass = 0; pass < 2 && found_at == NOT_FOUND; pass++){
if(direction){
found_at = buffer.indexOf(search_for, position);
position = 0;
}else{
found_at = buffer.lastIndexOf(search_for, position);
position = -1;
}
}
return found_at;
}
int ROM_buffer::replace(QString find, QString replace, int position, bool direction, bool mode)
{
position /= 2;
int result = search(find, position, direction, mode);
if(result < 0){
return result;
}
QByteArray replace_with = input_to_byte_array(replace, mode);
if(replace_with.isEmpty() && !replace.isEmpty()){
return INVALID_REPLACE;
}
undo_stack->beginMacro("Replace");
delete_text(result, result+input_to_byte_array(find, mode).length());
undo_stack->push(new undo_paste_command(&buffer, result, new QByteArray(replace_with)));
buffer.insert(result, replace_with);
undo_stack->endMacro();
return result;
}
int ROM_buffer::replace_all(QString find, QString replace, bool mode)
{
QByteArray search_for = input_to_byte_array(find, mode);
QByteArray replace_with = input_to_byte_array(replace, mode);
if(search_for.isEmpty()){
return INVALID_FIND;
}else if(replace_with.isEmpty() && !replace.isEmpty()){
return INVALID_REPLACE;
}
int results = count(find, mode);
undo_stack->beginMacro("Replace All");
int next = 0;
for(int i = 0; i < results; i++){
next = buffer.indexOf(search_for, next);;
delete_text(next, next+search_for.length());
undo_stack->push(new undo_paste_command(&buffer, next, new QByteArray(replace_with)));
buffer.insert(next, replace_with);
}
undo_stack->endMacro();
return results;
}
QVector<int> ROM_buffer::get_rats_tags() const
{
QVector<int> offsets;
int position = 0;
do{
position = buffer.indexOf("STAR", position);
if((read_word(buffer, position+4) ^ read_word(buffer, position+6)) == 0xFFFF){
offsets.append(position);
}
position++;
}while(position > 0);
return offsets;
}
QByteArray ROM_buffer::input_to_byte_array(QString input, int mode)
{
if(mode){
QString hex = get_hex(input);
if(hex.length() & 1){
return QByteArray();
}else{
return QByteArray::fromHex(hex.toUtf8());
}
}
return character_mapper::decode(input.toUtf8());
}
ROM_buffer::copy_style ROM_buffer::copy_type = ROM_buffer::NO_SPACES;
QClipboard *ROM_buffer::clipboard;