3 #include <stdlib.h> //atoi()
4 #include "memory_manage.h"
6 #include "flash_device.h"
8 #include "reader_master.h"
9 #include "reader_kazzo.h"
10 #include "reader_dummy.h"
11 #include "script_flash.h"
12 #include "script_dump.h"
14 static bool transtype_flash_set(char mode, struct memory *t)
18 t->transtype = TRANSTYPE_TOP;
21 t->transtype = TRANSTYPE_EMPTY;
24 t->transtype = TRANSTYPE_BOTTOM;
27 t->transtype = TRANSTYPE_FULL;
34 static bool transtype_set(const char *mode, struct romimage *t)
37 case 'a': case 'f': case 'F':
39 t->cpu_rom.transtype = TRANSTYPE_FULL;
40 t->ppu_rom.transtype = TRANSTYPE_FULL;
43 if(transtype_flash_set(mode[1], &t->cpu_rom) == false){
47 t->ppu_rom.transtype = TRANSTYPE_FULL;
50 if(transtype_flash_set(mode[2], &t->ppu_rom) == false){
57 static bool config_parse(const char *romimage, const char *device_cpu, const char *device_ppu, struct config_flash *c)
60 if(nesfile_load(__FUNCTION__, romimage, &c->rom) == false){
63 c->rom.cpu_rom.offset = 0;
64 c->rom.ppu_rom.offset = 0;
65 if(flash_device_get(device_cpu, &c->flash_cpu) == false){
66 printf("unkown flash memory device %s\n", device_cpu);
69 if(flash_device_get(device_ppu, &c->flash_ppu) == false){
70 printf("unkown flash memory device %s\n", device_ppu);
73 if(c->flash_cpu.id_device == FLASH_ID_DEVICE_DUMMY){
74 c->rom.cpu_rom.transtype = TRANSTYPE_EMPTY;
75 }else if(c->flash_cpu.capacity < c->rom.cpu_rom.size){
76 puts("cpu area ROM image size is larger than target device");
80 (c->flash_ppu.id_device == FLASH_ID_DEVICE_DUMMY) ||
81 (c->rom.ppu_rom.size == 0)
83 c->rom.ppu_rom.transtype = TRANSTYPE_EMPTY;
84 }else if(c->flash_ppu.capacity < c->rom.ppu_rom.size){
85 puts("ppu area ROM image size is larger than target device");
90 static void program(int c, char **v)
92 struct config_flash config;
93 config.rom.cpu_rom.data = NULL;
94 config.rom.ppu_rom.data = NULL;
96 config.reader = &DRIVER_KAZZO;
97 config.compare = false;
98 config.testrun = false;
101 config.reader = &DRIVER_DUMMY;
102 config.testrun = true;
105 config.compare = true;
108 if(transtype_set(v[1], &config.rom) == false){
109 puts("mode argument error");
113 case 5: //mode script target cpu_flash_device
114 if(config_parse(v[3], v[4], "dummy", &config) == false){
115 nesbuffer_free(&config.rom, 0);
119 case 6: //mode script target cpu_flash_device ppu_flash_device
120 if(config_parse(v[3], v[4], v[5], &config) == false){
121 nesbuffer_free(&config.rom, 0);
126 puts("mode script target cpu_flash_device ppu_flash_device");
129 if(config.reader->open_or_close(READER_OPEN) == NG){
130 puts("reader open error");
131 nesbuffer_free(&config.rom, 0);
134 config.reader->init();
135 script_flash_execute(&config);
136 nesbuffer_free(&config.rom, 0);
137 config.reader->open_or_close(READER_CLOSE);
139 static void dump(int c, char **v)
141 struct config_dump config;
143 puts("argument error");
146 config.increase.cpu = 1;
147 config.increase.ppu = 1;
148 config.progress = true;
151 config.progress = false;
156 config.increase.cpu = 2;
159 config.increase.cpu = 4;
165 config.increase.ppu = 2;
168 config.increase.ppu = 4;
172 config.script = v[2];
173 config.target = v[3];
174 config.reader = &DRIVER_KAZZO;
175 config.mappernum = -1;
177 config.mappernum = atoi(v[4]);
179 if(config.reader->open_or_close(READER_OPEN) == NG){
180 puts("reader open error");
183 config.reader->init();
184 script_dump_execute(&config);
185 config.reader->open_or_close(READER_CLOSE);
187 static void usage(const char *v)
189 puts("famicom bus simluator 'anago'");
190 printf("%s [mode] [script] [target] ....\n", v);
192 int main(int c, char **v)
197 case 'a': case 'f': case 'F':
205 puts("mode are a, d, D, f, g");