OSDN Git Service

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