-
Notifications
You must be signed in to change notification settings - Fork 3
/
script_dump.c
371 lines (354 loc) · 9.64 KB
/
script_dump.c
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
#include <assert.h>
#include <stdio.h>
#include <string.h>
#include <squirrel.h>
#include <sqstdio.h>
#include <sqstdaux.h>
#include "file.h"
#include "type.h"
#include "header.h"
#include "progress.h"
#include "memory_manage.h"
#include "reader_master.h"
#include "squirrel_wrap.h"
#include "script_common.h"
#include "script_dump.h"
struct dump_driver{
const char *target;
struct memory_driver{
struct memory memory;
long read_count;
void (*const write)(long address, long length, const uint8_t *data);
void (*const read)(long address, long length, u8 *data);
}cpu, ppu;
uint8_t (*const vram_connection)(void);
bool progress;
};
static SQInteger write_data(HSQUIRRELVM v, struct memory_driver *t)
{
long address, data;
SQRESULT r = qr_argument_get(v, 2, &address, &data);
if(SQ_FAILED(r)){
return r;
}
uint8_t d8 = (uint8_t) data;
t->write(address, 1, &d8);
return 0;
}
static SQInteger cpu_write(HSQUIRRELVM v)
{
struct dump_driver *d;
SQRESULT r = qr_userpointer_get(v, (SQUserPointer) &d);
if(SQ_FAILED(r)){
return r;
}
return write_data(v, &d->cpu);
}
static void buffer_show(struct memory *t, long length)
{
int i;
const uint8_t *buf = t->data + t->offset;
printf("%s 0x%06x:", t->name, t->offset);
for(i = 0; i < 0x10; i++){
char dump[3+1];
sprintf(dump, "%02x", buf[i]);
switch(i){
case 7:
dump[2] = '-';
break;
case 0x0f:
dump[2] = '\0';
break;
default:
dump[2] = ' ';
break;
}
dump[3] = '\0';
printf("%s", dump);
}
int sum = 0;
while(length != 0){
sum += (int) *buf;
buf++;
length--;
}
printf(":0x%06x\n", sum);
fflush(stdout);
}
static void progress_show(struct dump_driver *d)
{
if(d->progress == true){
progress_draw(d->cpu.memory.offset, d->cpu.memory.size, d->ppu.memory.offset, d->ppu.memory.size);
}
}
static SQInteger read_data(HSQUIRRELVM v, struct memory_driver *t, bool progress)
{
long address, length;
SQRESULT r = qr_argument_get(v, 2, &address, &length);
if(SQ_FAILED(r)){
return r;
}
t->read(address, length == 0 ? 1: length, t->memory.data + t->memory.offset);
if((length != 0) && (progress == false)){
buffer_show(&t->memory, length);
}
t->memory.offset += length;
return 0;
}
static SQInteger cpu_read(HSQUIRRELVM v)
{
struct dump_driver *d;
SQRESULT r = qr_userpointer_get(v, (SQUserPointer) &d);
if(SQ_FAILED(r)){
return r;
}
r = read_data(v, &d->cpu, d->progress);
progress_show(d);
return r;
}
static SQInteger ppu_read(HSQUIRRELVM v)
{
struct dump_driver *d;
SQRESULT r = qr_userpointer_get(v, (SQUserPointer) &d);
if(SQ_FAILED(r)){
return r;
}
r = read_data(v, &d->ppu, d->progress);
progress_show(d);
return r;
}
static SQInteger ppu_ramfind(HSQUIRRELVM v)
{
struct dump_driver *d;
enum{
testsize = 8,
testaddress = 1234
};
static const uint8_t test_val[testsize] = {0xaa, 0x55, 0, 0xff, 0x46, 0x49, 0x07, 0x21};
static const uint8_t test_str[testsize] = "pputest";
uint8_t test_result[testsize];
SQRESULT r = qr_userpointer_get(v, (SQUserPointer) &d);
if(SQ_FAILED(r)){
return r;
}
d->ppu.write(testaddress, testsize, test_val);
d->ppu.read(testaddress, testsize, test_result);
if(memcmp(test_val, test_result, testsize) != 0){
sq_pushbool(v, SQFalse);
return 1;
}
d->ppu.write(testaddress, testsize, test_str);
d->ppu.read(testaddress, testsize, test_result);
if(memcmp(test_str, test_result, testsize) != 0){
sq_pushbool(v, SQFalse);
return 1;
}
d->ppu.memory.offset = 0;
d->ppu.memory.size = 0;
sq_pushbool(v, SQTrue);
return 1;
}
//test 時/1度目の call で使用
static SQInteger memory_new(HSQUIRRELVM v)
{
struct dump_driver *d;
SQRESULT r = qr_userpointer_get(v, (SQUserPointer) &d);
if(SQ_FAILED(r)){
return r;
}
r = qr_argument_get(v, 2, &d->cpu.memory.size, &d->ppu.memory.size);
if(SQ_FAILED(r)){
return r;
}
d->cpu.memory.offset = 0;
d->cpu.memory.data = Malloc(d->cpu.memory.size);
d->ppu.memory.offset = 0;
d->ppu.memory.data = Malloc(d->ppu.memory.size);
return 0;
}
//dump 時/2度目の call で nesfile_save として使用
static SQInteger nesfile_save(HSQUIRRELVM v)
{
struct dump_driver *d;
SQRESULT r = qr_userpointer_get(v, (SQUserPointer) &d);
if(SQ_FAILED(r)){
return r;
}
struct romimage image;
long mirrorfind;
r = qr_argument_get(v, 2, &image.mappernum, &mirrorfind);
if(SQ_FAILED(r)){
return r;
}
image.cpu_rom = d->cpu.memory;
image.cpu_ram.data = NULL;
image.ppu_rom = d->ppu.memory;
image.mirror = MIRROR_PROGRAMABLE;
if(mirrorfind == 1){
if(d->vram_connection() == 0x05){
image.mirror = MIRROR_VERTICAL;
}else{
image.mirror = MIRROR_HORIZONAL;
}
}
image.backupram = 0;
nesfile_create(&image, d->target);
nesbuffer_free(&image, 0); //0 is MODE_xxx_xxxx
d->cpu.memory.data = NULL;
d->ppu.memory.data = NULL;
return 0;
}
//dump 時/1度目の call で nesfile_save として使用
static SQInteger length_check(HSQUIRRELVM v)
{
struct dump_driver *d;
SQRESULT r = qr_userpointer_get(v, (SQUserPointer) &d);
if(SQ_FAILED(r)){
return r;
}
bool cpu = true, ppu = true;
r = 0;
if(d->cpu.memory.size != d->cpu.read_count){
cpu = false;
}
if(cpu == false){
printf("cpu_romsize is not connected 0x%06x/0x%06x\n", (int) d->cpu.read_count, (int) d->cpu.memory.size);
}
if(d->ppu.memory.size != d->ppu.read_count){
ppu = false;
}
if(ppu == false){
printf("ppu_romsize is not connected 0x%06x/0x%06x\n", (int) d->ppu.read_count, (int) d->ppu.memory.size);
}
if(cpu == false || ppu == false){
r = sq_throwerror(v, "script logical error");
}
return r;
}
static SQInteger read_count(HSQUIRRELVM v, struct memory_driver *t, const struct range *range_address, const struct range *range_length)
{
long address, length;
SQRESULT r = qr_argument_get(v, 2, &address, &length);
if(SQ_FAILED(r)){
return r;
}
r = range_check(v, "length", length, range_length);
if(SQ_FAILED(r)){
return r;
}
if((address < range_address->start) || ((address + length) > range_address->end)){
printf("address range must be 0x%06x to 0x%06x", (int) range_address->start, (int) range_address->end);
return sq_throwerror(v, "script logical error");;
}
t->read_count += length;
return 0;
}
static SQInteger cpu_read_count(HSQUIRRELVM v)
{
static const struct range range_address = {0x8000, 0x10000};
//length == 0 は 対象アドレスを呼んで、バッファにいれない。mmc2, mmc4 で使用する。
static const struct range range_length = {0x0000, 0x4000};
struct dump_driver *d;
SQRESULT r = qr_userpointer_get(v, (SQUserPointer) &d);
if(SQ_FAILED(r)){
return r;
}
return read_count(v, &d->cpu, &range_address, &range_length);
}
static SQInteger ppu_read_count(HSQUIRRELVM v)
{
static const struct range range_address = {0x0000, 0x2000};
static const struct range range_length = {0x0001, 0x2000};
struct dump_driver *d;
SQRESULT r = qr_userpointer_get(v, (SQUserPointer) &d);
if(SQ_FAILED(r)){
return r;
}
return read_count(v, &d->ppu, &range_address, &range_length);
}
static bool script_execute(HSQUIRRELVM v, struct config_dump *c, struct dump_driver *d)
{
bool ret = true;
if(SQ_FAILED(sqstd_dofile(v, _SC(find_script("dumpcore.nut")), SQFalse, SQTrue))){
printf("dump core script error\n");
ret = false;
}else if(SQ_FAILED(sqstd_dofile(v, _SC(find_script(c->script)), SQFalse, SQTrue))){
printf("%s open error\n", c->script);
ret = false;
}else{
SQRESULT r = qr_call(
v, "dump", (SQUserPointer) d, true,
3, c->mappernum, c->increase.cpu, c->increase.ppu
);
if(SQ_FAILED(r)){
ret = false;
Free(d->cpu.memory.data);
Free(d->ppu.memory.data);
d->cpu.memory.data = NULL;
d->ppu.memory.data = NULL;
}
}
return ret;
}
void script_dump_execute(struct config_dump *c)
{
struct dump_driver d = {
.cpu = {
.memory = {
.name = "program",
.size = 0, .offset = 0,
.attribute = MEMORY_ATTR_WRITE,
.transtype = TRANSTYPE_FULL,
.data = NULL
},
.read_count = 0,
.write = c->reader->cpu_write_6502,
.read = c->reader->cpu_read
},
.ppu = {
.memory = {
.name = "charcter",
.size = 0, .offset = 0,
.attribute = MEMORY_ATTR_WRITE,
.transtype = TRANSTYPE_FULL,
.data = NULL
},
.read_count = 0,
.write = c->reader->ppu_write,
.read = c->reader->ppu_read
},
.vram_connection = c->reader->vram_connection,
.target = c->target,
.progress = c->progress
};
{
HSQUIRRELVM v = qr_open();
qr_function_register_global(v, "ppu_ramfind", script_nop);
qr_function_register_global(v, "cpu_write", cpu_write_check);
qr_function_register_global(v, "memory_new", memory_new);
qr_function_register_global(v, "nesfile_save", length_check);
qr_function_register_global(v, "cpu_read", cpu_read_count);
qr_function_register_global(v, "ppu_read", ppu_read_count);
qr_function_register_global(v, "require", script_require);
if(script_execute(v, c, &d) == false){
qr_close(v);
return;
}
qr_close(v);
}
if(c->progress == true){
progress_init();
}
{
HSQUIRRELVM v = qr_open();
qr_function_register_global(v, "memory_new", script_nop);
qr_function_register_global(v, "nesfile_save", nesfile_save);
qr_function_register_global(v, "cpu_write", cpu_write);
qr_function_register_global(v, "cpu_read", cpu_read);
qr_function_register_global(v, "ppu_read", ppu_read);
qr_function_register_global(v, "ppu_ramfind", ppu_ramfind);
qr_function_register_global(v, "require", script_require);
script_execute(v, c, &d);
qr_close(v);
}
}