OSDN Git Service

script 実行結果をみて音を出すようにした
[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 "memory_manage.h"
6 #include "type.h"
7 #include "widget.h"
8 #include "cui_gauge.h"
9 #include "header.h"
10 #include "reader_master.h"
11 #include "reader_kazzo.h"
12 #include "script_dump.h"
13 #include "flash_device.h"
14 #include "script_program.h"
15
16 #ifdef _UNICODE
17   #define PUTS _putws
18   #define PRINTF wprintf
19 #else
20   #define PUTS puts
21   #define PRINTF printf
22 #endif
23
24 static void text_append_va(void *obj, const wgChar *format, va_list list)
25 {
26 #ifdef _UNICODE
27         vwprintf(format, list);
28 #else
29         vprintf(format, list);
30 #endif
31 }
32
33 static void text_append(void *obj, const wgChar *format, ...)
34 {
35         va_list list;
36         va_start(list, format);
37         text_append_va(obj, format, list);
38         va_end(list);
39 }
40
41 static void log_set(struct textcontrol *log)
42 {
43         log->object = NULL;
44         log->append = text_append;
45         log->append_va = text_append_va;
46 }
47
48 static void except(const wgChar *str)
49 {
50         PUTS(str);
51         exit(0);
52 }
53
54 static bool program_rom_set(const wgChar *device, wgChar trans, struct memory *m, struct flash_device *f)
55 {
56         m->offset = 0;
57         if(flash_device_get(device, f) == false){
58                 PRINTF(wgT("unknown flash memory device %s\n"), device);
59                 return false;
60         }
61         switch(trans){
62         case 'f':
63                 m->transtype = TRANSTYPE_FULL;
64                 break;
65         case 't':
66                 m->transtype = TRANSTYPE_TOP;
67                 break;
68         case 'b':
69                 m->transtype = TRANSTYPE_BOTTOM;
70                 break;
71         case 'e':
72         default: 
73                 m->transtype = TRANSTYPE_EMPTY;
74                 break;
75         }
76         return true;
77 }
78
79 static void program(int c, wgChar **v)
80 {
81         struct program_config config;
82         config.cpu.memory.data = NULL;
83         config.ppu.memory.data = NULL;
84         config.script = v[2];
85         config.target = v[3];
86         config.control = &DRIVER_KAZZO.control;
87         config.cpu.access = &DRIVER_KAZZO.cpu;
88         config.ppu.access = &DRIVER_KAZZO.ppu;
89         config.compare = false;
90         config.testrun = false;
91         switch(v[1][0]){
92         case 'a':
93 //              config.reader = &DRIVER_DUMMY;
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)
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.progress = false;
157                 break;
158         }
159         switch(v[1][1]){
160         case wgT('2'):
161                 config.cpu.increase = 2;
162                 break;
163         case wgT('4'):
164                 config.cpu.increase = 4;
165                 break;
166         }
167         if(v[1][1] != wgT('\0')){
168                 switch(v[1][2]){
169                 case wgT('2'):
170                         config.ppu.increase = 2;
171                         break;
172                 case wgT('4'):
173                         config.ppu.increase = 4;
174                         break;
175                 }
176         }
177         config.script = v[2];
178         config.target = v[3];
179         config.control = &DRIVER_KAZZO.control;
180         config.cpu.access = &DRIVER_KAZZO.cpu;
181         config.ppu.access = &DRIVER_KAZZO.ppu;
182         cui_gauge_new(&config.cpu.gauge, wgT("Program  ROM"), 2, -2);
183         cui_gauge_new(&config.ppu.gauge, wgT("Charcter ROM"), 1, -1);
184         config.except = except;
185         config.mappernum = -1;
186         if(c == 5){
187 #ifdef _UNICODE
188                 config.mappernum = _wtoi(v[4]);
189 #else
190                 config.mappernum = atoi(v[4]);
191 #endif
192         }
193         config.battery = false;
194         log_set(&config.log);
195         script_dump_execute(&config);
196         cui_gauge_destory(&config.cpu.gauge);
197         cui_gauge_destory(&config.ppu.gauge);
198 }
199
200 static void usage(const wgChar *v)
201 {
202         PUTS(wgT("famicom bus simluator 'anago'"));
203         PRINTF(wgT("%s [mode] [script] [target] ....\n"), v);
204 }
205
206 #ifdef WIN32
207 int main(int c, char **vv)
208 #else
209 int anago_cui(int c, wgChar **v)
210 #endif
211 {
212         mm_init();
213         if(c >= 2){
214 #ifdef _UNICODE
215                 int i;
216                 wchar_t **v;
217                 v = Malloc(sizeof(wchar_t *) * c);
218                 for(i = 0; i < c; i++){
219                         size_t len = strlen(vv[i]) + 1;
220                         v[i] = Malloc(sizeof(wchar_t) * len);
221                         mbstowcs(v[i], vv[i], len);
222                 }
223 #endif
224                 switch(v[1][0]){
225                 case wgT('a'): case wgT('f'): case wgT('F'):
226                         program(c, v);
227                         break;
228                 case wgT('d'): case wgT('D'):
229                         dump(c,v);
230                         break;
231                 default:
232                         usage(v[0]);
233                         PUTS(wgT("mode are d, D, f, g"));
234                         break;
235                 }
236 #ifdef _UNICODE
237                 for(i = 0; i < c; i++){
238                         Free(v[i]);
239                 }
240                 Free(v);
241 #endif
242         }else{ //usage
243                 size_t len = strlen(vv[0]) + 1;
244                 wchar_t *t = Malloc(sizeof(wchar_t) * len);
245                 mbstowcs(t, vv[0], len);
246                 usage(t);
247                 Free(t);
248         }
249         mm_end();
250         return 0;
251 }