OSDN Git Service

be88b3f9429add231f008ab8f70ca2da835d3945
[unagi/old-svn-converted.git] / client / trunk / anago / anago_cui.c
1 #include <assert.h>
2 #include <stdio.h>
3 #include <stdarg.h>
4 #include <stdlib.h>
5 #include <string.h>
6 #include "memory_manage.h"
7 #include "type.h"
8 #include "widget.h"
9 #include "cui_gauge.h"
10 #include "romimage.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"
17
18 #ifdef _UNICODE
19   #define PUTS _putws
20   #define PRINTF wprintf
21 #else
22   #define PUTS puts
23   #define PRINTF printf
24 #endif
25
26 static void text_append_va(void *obj, const wgChar *format, va_list list)
27 {
28 #ifdef _UNICODE
29         vwprintf(format, list);
30 #else
31         vprintf(format, list);
32 #endif
33 }
34
35 static void text_append(void *obj, const wgChar *format, ...)
36 {
37         va_list list;
38         va_start(list, format);
39         text_append_va(obj, format, list);
40         va_end(list);
41 }
42
43 static void log_set(struct textcontrol *log)
44 {
45         log->object = NULL;
46         log->append = text_append;
47         log->append_va = text_append_va;
48 }
49
50 static void except(const wgChar *str)
51 {
52         PUTS(str);
53         exit(0);
54 }
55
56 static bool program_rom_set(const wgChar *device, wgChar trans, struct memory *m, struct flash_device *f)
57 {
58         m->offset = 0;
59         if(flash_device_get(device, f) == false){
60                 PRINTF(wgT("unknown flash memory device %s\n"), device);
61                 return false;
62         }
63         switch(trans){
64         case 'f':
65                 m->transtype = TRANSTYPE_FULL;
66                 break;
67         case 't':
68                 m->transtype = TRANSTYPE_TOP;
69                 break;
70         case 'b':
71                 m->transtype = TRANSTYPE_BOTTOM;
72                 break;
73         case 'e':
74         default: 
75                 m->transtype = TRANSTYPE_EMPTY;
76                 break;
77         }
78         return true;
79 }
80
81 static void program(int c, wgChar **v, const struct reader_driver *r)
82 {
83         struct program_config config;
84         config.cpu.memory.data = NULL;
85         config.ppu.memory.data = NULL;
86         config.script = v[2];
87         config.target = v[3];
88         config.control = &r->control;
89         config.cpu.access = &r->cpu;
90         config.ppu.access = &r->ppu;
91         config.compare = false;
92         switch(v[1][0]){
93         case wgT('F'): case wgT('X'):
94                 config.compare = true;
95                 break;
96         }
97
98         switch(c){
99         case 5: {//mode script target cpu_flash_device
100                 wgChar trans = wgT('f');
101                 if(v[1][1] != wgT('\0')){
102                         trans = v[1][1];
103                 }
104                 if(program_rom_set(v[4], trans, &config.cpu.memory, &config.cpu.flash) == false){
105                         return;
106                 }
107                 if(program_rom_set(wgT("dummy"), wgT('e'), &config.ppu.memory, &config.ppu.flash) == false){
108                         assert(0);
109                         return;
110                 }
111                 }break;
112         case 6: { //mode script target cpu_flash_device ppu_flash_device
113                 wgChar trans = wgT('f');
114                 if(v[1][1] != wgT('\0')){
115                         trans = v[1][1];
116                 }
117                 if(program_rom_set(v[4], trans, &config.cpu.memory, &config.cpu.flash) == false){
118                         return;
119                 }
120                 trans = wgT('f');
121                 if(v[1][1] != wgT('\0') && v[1][2] != wgT('\0')){
122                         trans = v[1][2];
123                 }
124                 if(program_rom_set(v[5], trans, &config.ppu.memory, &config.ppu.flash) == false){
125                         return;
126                 }
127                 }break;
128         default:
129                 PUTS(wgT("mode script target cpu_flash_device ppu_flash_device"));
130                 return;
131         }
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);
139 }
140
141 static void dump(int c, wgChar **v, const struct reader_driver *r)
142 {
143         struct dump_config config;
144         if(c < 4){
145                 PUTS(wgT("argument error"));
146                 return;
147         }
148         config.cpu.increase = INCREASE_AUTO;
149         config.ppu.increase = 1;
150         config.progress = true;
151         switch(v[1][0]){
152         case wgT('d'): case wgT('z'):
153                 config.mode = MODE_ROM_DUMP;
154                 break;
155         case wgT('D'):
156                 config.mode = MODE_ROM_DUMP;
157                 config.progress = false;
158                 break;
159         case wgT('r'): case wgT('R'):
160                 config.mode = MODE_RAM_READ;
161                 break;
162         case wgT('w'): case wgT('W'):
163                 config.mode = MODE_RAM_WRITE;
164                 break;
165         }
166         switch(v[1][1]){
167         case wgT('1'):
168                 config.cpu.increase = 1;
169                 break;
170         case wgT('2'):
171                 config.cpu.increase = 2;
172                 break;
173         case wgT('4'):
174                 config.cpu.increase = 4;
175                 break;
176         }
177         if(v[1][1] != wgT('\0')){
178                 switch(v[1][2]){
179                 case wgT('2'):
180                         config.ppu.increase = 2;
181                         break;
182                 case wgT('4'):
183                         config.ppu.increase = 4;
184                         break;
185                 }
186         }
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);
194         }else{
195                 cui_gauge_new(&config.cpu.gauge, wgT("Work RAM"), 2, -2);
196         }
197         cui_gauge_new(&config.ppu.gauge, wgT("Charcter ROM"), 1, -1);
198         config.except = except;
199         config.mappernum = -1;
200         config.battery = false;
201         if(c == 5){
202                 const wgChar *t = v[4];
203                 if(*t == 'b' || *t == 'B'){
204                         config.battery = true;
205                         t += 1;
206                 }
207                 if(*t != '\0'){
208 #ifdef _UNICODE
209                         config.mappernum = _wtoi(t);
210 #else
211                         config.mappernum = atoi(t);
212 #endif
213                 }
214         }
215         log_set(&config.log);
216         if(config.mode == MODE_ROM_DUMP){
217                 script_dump_execute(&config);
218         }else{
219                 script_workram_execute(&config);
220         }
221         cui_gauge_destory(&config.cpu.gauge);
222         cui_gauge_destory(&config.ppu.gauge);
223 }
224
225 static void usage(const wgChar *v)
226 {
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"));
233         if(DEBUG == 1){
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"));
238         }
239 }
240
241 #ifdef WIN32
242 int main(int c, char **vv)
243 #else
244 int anago_cui(int c, wgChar **v)
245 #endif
246 {
247         mm_init();
248         if(c >= 2){
249                 const struct reader_driver *r = &DRIVER_KAZZO;
250 #ifdef _UNICODE
251                 int i;
252                 wchar_t **v;
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);
258                 }
259 #endif
260                 switch(v[1][0]){
261                 case wgT('x'): case wgT('X'):
262                         r = &DRIVER_DUMMY; //though down
263                 case wgT('f'): case wgT('F'):
264                         program(c, v, r);
265                         break;
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'):
270                         dump(c,v, r);
271                         break;
272                 default:
273                         usage(v[0]);
274                         PUTS(wgT("mode are d, D, f, g"));
275                         break;
276                 }
277 #ifdef _UNICODE
278                 for(i = 0; i < c; i++){
279                         Free(v[i]);
280                 }
281                 Free(v);
282 #endif
283         }else{ //usage
284 #ifdef _UNICODE
285                 size_t len = strlen(vv[0]) + 1;
286                 wchar_t *t = Malloc(sizeof(wchar_t) * len);
287                 mbstowcs(t, vv[0], len);
288                 usage(t);
289                 Free(t);
290 #else
291                 usage(v[0]);
292 #endif
293         }
294         mm_end();
295         return 0;
296 }