6 #include "memory_manage.h"
11 #include "reader_master.h"
12 #include "reader_kazzo.h"
13 #include "reader_dummy.h"
14 #include "script_dump.h"
15 #include "flash_device.h"
16 #include "script_program.h"
20 #define PRINTF wprintf
26 static void text_append_va(void *obj, const wgChar *format, va_list list)
29 vwprintf(format, list);
31 vprintf(format, list);
35 static void text_append(void *obj, const wgChar *format, ...)
38 va_start(list, format);
39 text_append_va(obj, format, list);
43 static void log_set(struct textcontrol *log)
46 log->append = text_append;
47 log->append_va = text_append_va;
50 static void except(const wgChar *str)
56 static bool program_rom_set(const wgChar *device, wgChar trans, struct memory *m, struct flash_device *f)
59 if(flash_device_get(device, f) == false){
60 PRINTF(wgT("unknown flash memory device %s\n"), device);
65 m->transtype = TRANSTYPE_FULL;
68 m->transtype = TRANSTYPE_TOP;
71 m->transtype = TRANSTYPE_BOTTOM;
75 m->transtype = TRANSTYPE_EMPTY;
81 static void program(int c, wgChar **v, const struct reader_driver *r)
83 struct program_config config;
84 config.cpu.memory.data = NULL;
85 config.ppu.memory.data = NULL;
88 config.control = &r->control;
89 config.cpu.access = &r->cpu;
90 config.ppu.access = &r->ppu;
91 config.compare = false;
93 case wgT('F'): case wgT('X'):
94 config.compare = true;
99 case 5: {//mode script target cpu_flash_device
100 wgChar trans = wgT('f');
101 if(v[1][1] != wgT('\0')){
104 if(program_rom_set(v[4], trans, &config.cpu.memory, &config.cpu.flash) == false){
107 if(program_rom_set(wgT("dummy"), wgT('e'), &config.ppu.memory, &config.ppu.flash) == false){
112 case 6: { //mode script target cpu_flash_device ppu_flash_device
113 wgChar trans = wgT('f');
114 if(v[1][1] != wgT('\0')){
117 if(program_rom_set(v[4], trans, &config.cpu.memory, &config.cpu.flash) == false){
121 if(v[1][1] != wgT('\0') && v[1][2] != wgT('\0')){
124 if(program_rom_set(v[5], trans, &config.ppu.memory, &config.ppu.flash) == false){
129 PUTS(wgT("mode script target cpu_flash_device ppu_flash_device"));
132 log_set(&config.log);
133 cui_gauge_new(&config.cpu.gauge, wgT("Program Flash"), 2, -2);
134 cui_gauge_new(&config.ppu.gauge, wgT("Charcter Flash"), 1, -1);
135 config.except = except;
136 script_program_execute(&config);
137 cui_gauge_destory(&config.cpu.gauge);
138 cui_gauge_destory(&config.ppu.gauge);
141 static void dump(int c, wgChar **v, const struct reader_driver *r)
143 struct dump_config config;
145 PUTS(wgT("argument error"));
148 config.cpu.increase = INCREASE_AUTO;
149 config.ppu.increase = 1;
150 config.progress = true;
152 case wgT('d'): case wgT('z'):
153 config.mode = MODE_ROM_DUMP;
156 config.mode = MODE_ROM_DUMP;
157 config.progress = false;
159 case wgT('r'): case wgT('R'):
160 config.mode = MODE_RAM_READ;
162 case wgT('w'): case wgT('W'):
163 config.mode = MODE_RAM_WRITE;
168 config.cpu.increase = 1;
171 config.cpu.increase = 2;
174 config.cpu.increase = 4;
177 if(v[1][1] != wgT('\0')){
180 config.ppu.increase = 2;
183 config.ppu.increase = 4;
187 config.script = v[2];
188 config.target = v[3];
189 config.control = &r->control;
190 config.cpu.access = &r->cpu;
191 config.ppu.access = &r->ppu;
192 if(config.mode == MODE_ROM_DUMP){
193 cui_gauge_new(&config.cpu.gauge, wgT("Program ROM"), 2, -2);
195 cui_gauge_new(&config.cpu.gauge, wgT("Work RAM"), 2, -2);
197 cui_gauge_new(&config.ppu.gauge, wgT("Charcter ROM"), 1, -1);
198 config.except = except;
199 config.mappernum = -1;
200 config.battery = false;
202 const wgChar *t = v[4];
203 if(*t == 'b' || *t == 'B'){
204 config.battery = true;
209 config.mappernum = _wtoi(t);
211 config.mappernum = atoi(t);
215 log_set(&config.log);
216 if(config.mode == MODE_ROM_DUMP){
217 script_dump_execute(&config);
219 script_workram_execute(&config);
221 cui_gauge_destory(&config.cpu.gauge);
222 cui_gauge_destory(&config.ppu.gauge);
225 static void usage(const wgChar *v)
227 PUTS(wgT("famicom bus simluator 'anago'"));
228 PRINTF(wgT("%s [mode] [script] [target] ....\n"), v);
229 PUTS(wgT("d - ROM dump with kazzo"));
230 PUTS(wgT("fF- flash program with kazzo"));
231 PUTS(wgT("r - workram read with kazzo"));
232 PUTS(wgT("w - workram write with kazzo"));
234 PUTS(wgT("z - ROM dump for test"));
235 PUTS(wgT("xX- flash program for test"));
236 PUTS(wgT("R - workram read for test"));
237 PUTS(wgT("W - workram write for test"));
242 int main(int c, char **vv)
244 int anago_cui(int c, wgChar **v)
249 const struct reader_driver *r = &DRIVER_KAZZO;
253 v = Malloc(sizeof(wchar_t *) * c);
254 for(i = 0; i < c; i++){
255 size_t len = strlen(vv[i]) + 1;
256 v[i] = Malloc(sizeof(wchar_t) * len);
257 mbstowcs(v[i], vv[i], len);
261 case wgT('x'): case wgT('X'):
262 r = &DRIVER_DUMMY; //though down
263 case wgT('f'): case wgT('F'):
266 case wgT('z'): case wgT('R'): case wgT('W'):
267 r = &DRIVER_DUMMY; //though down
268 case wgT('d'): case wgT('D'):
269 case wgT('r'): case wgT('w'):
274 PUTS(wgT("mode are d, D, f, g"));
278 for(i = 0; i < c; i++){
285 size_t len = strlen(vv[0]) + 1;
286 wchar_t *t = Malloc(sizeof(wchar_t) * len);
287 mbstowcs(t, vv[0], len);