OSDN Git Service

stdout への printf を排除
[unagi/old-svn-converted.git] / client / trunk / anago / anago.c
1 #include <stdio.h>
2 #include <stdbool.h>
3 #include <stdlib.h> //atoi()
4 #include "memory_manage.h"
5 #include "type.h"
6 #include "flash_device.h"
7 #include "header.h"
8 #include "reader_master.h"
9 #include "reader_kazzo.h"
10 #include "reader_dummy.h"
11 #include "script_program.h"
12 #include "script_dump.h"
13
14 static bool transtype_flash_set(char mode, struct memory *t)
15 {
16         switch(mode){
17         case 't':
18                 t->transtype = TRANSTYPE_TOP;
19                 break;
20         case 'e':
21                 t->transtype = TRANSTYPE_EMPTY;
22                 break;
23         case 'b':
24                 t->transtype = TRANSTYPE_BOTTOM;
25                 break;
26         case 'f':
27                 t->transtype = TRANSTYPE_FULL;
28                 break;
29         default:
30                 return false;
31         }
32         return true;
33 }
34 static bool transtype_set(const char *mode, struct romimage *t)
35 {
36         switch(mode[0]){
37         case 'a': case 'f': case 'F':
38                 if(mode[1] == '\0'){
39                         t->cpu_rom.transtype = TRANSTYPE_FULL;
40                         t->ppu_rom.transtype = TRANSTYPE_FULL;
41                         return true;
42                 }
43                 if(transtype_flash_set(mode[1], &t->cpu_rom) == false){
44                         return false;
45                 }
46                 if(mode[2] == '\0'){
47                         t->ppu_rom.transtype = TRANSTYPE_FULL;
48                         return true;
49                 }
50                 if(transtype_flash_set(mode[2], &t->ppu_rom) == false){
51                         return false;
52                 }
53                 return true;
54         }
55         return false;
56 }
57 static bool config_parse(const char *romimage, const char *device_cpu, const char *device_ppu, struct config_flash *c)
58 {
59         c->target = romimage;
60         if(nesfile_load(__FUNCTION__, romimage, &c->rom) == false){
61                 return false;
62         }
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);
67                 return false;
68         }
69         if(flash_device_get(device_ppu, &c->flash_ppu) == false){
70                 printf("unkown flash memory device %s\n", device_ppu);
71                 return false;
72         }
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");
77                 return false;
78         }
79         if(
80                 (c->flash_ppu.id_device == FLASH_ID_DEVICE_DUMMY) ||
81                 (c->rom.ppu_rom.size == 0)
82         ){
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");
86                 return false;
87         }
88         return true;
89 }
90 static void program(int c, char **v)
91 {
92         struct config_flash config;
93         config.rom.cpu_rom.data = NULL;
94         config.rom.ppu_rom.data = NULL;
95         config.script = v[2];
96         config.reader = &DRIVER_KAZZO;
97         config.compare = false;
98         config.testrun = false;
99         switch(v[1][0]){
100         case 'a':
101                 config.reader = &DRIVER_DUMMY;
102                 config.testrun = true;
103                 break;
104         case 'F':
105                 config.compare = true;
106                 break;
107         }
108         if(transtype_set(v[1], &config.rom) == false){
109                 puts("mode argument error");
110                 return;
111         }
112         switch(c){
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);
116                         return;
117                 }
118                 break;
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);
122                         return;
123                 }
124                 break;
125         default:
126                 puts("mode script target cpu_flash_device ppu_flash_device");
127                 return;
128         }
129         if(config.reader->open_or_close(READER_OPEN) == NG){
130                 puts("reader open error");
131                 nesbuffer_free(&config.rom, 0);
132                 return;
133         }
134         config.reader->init();
135         script_flash_execute(&config);
136         nesbuffer_free(&config.rom, 0);
137         config.reader->open_or_close(READER_CLOSE);
138 }
139 static void dump(int c, char **v)
140 {
141         struct config_dump config;
142         if(c < 4){
143                 puts("argument error");
144                 return;
145         }
146         config.increase.cpu = 1;
147         config.increase.ppu = 1;
148         config.progress = true;
149         switch(v[1][0]){
150         case 'D':
151                 config.progress = false;
152                 break;
153         }
154         switch(v[1][1]){
155         case '2':
156                 config.increase.cpu = 2;
157                 break;
158         case '4':
159                 config.increase.cpu = 4;
160                 break;
161         }
162         if(v[1][1] != '\0'){
163                 switch(v[1][2]){
164                 case '2':
165                         config.increase.ppu = 2;
166                         break;
167                 case '4':
168                         config.increase.ppu = 4;
169                         break;
170                 }
171         }
172         config.script = v[2];
173         config.target = v[3];
174         config.reader = &DRIVER_KAZZO;
175         config.mappernum = -1;
176         if(c == 5){
177                 config.mappernum = atoi(v[4]);
178         }
179         if(config.reader->open_or_close(READER_OPEN) == NG){
180                 puts("reader open error");
181                 return;
182         }
183         config.reader->init();
184         script_dump_execute(&config);
185         config.reader->open_or_close(READER_CLOSE);
186 }
187 static void usage(const char *v)
188 {
189         puts("famicom bus simluator 'anago'");
190         printf("%s [mode] [script] [target] ....\n", v);
191 }
192 int main(int c, char **v)
193 {
194         mm_init();
195         if(c >= 2){
196                 switch(v[1][0]){
197                 case 'a': case 'f': case 'F':
198                         program(c, v);
199                         break;
200                 case 'd': case 'D':
201                         dump(c,v);
202                         break;
203                 default:
204                         usage(v[0]);
205                         puts("mode are a, d, D, f, g");
206                         break;
207                 }
208         }else{
209                 usage(v[0]);
210         }
211         mm_end();
212         return 0;
213 }