OSDN Git Service

DEBUG==0 での config data の読み方の修正
[unagi/old-svn-converted.git] / client / trunk / unagi.c
1 /*
2 famicom ROM cartridge utility - unagi
3 command line interface, config file parser
4
5 Copyright (C) 2008 ±·³«È¯¶¨Æ±Áȹç
6
7 This library is free software; you can redistribute it and/or
8 modify it under the terms of the GNU Lesser General Public
9 License as published by the Free Software Foundation; either
10 version 2.1 of the License any later version.
11
12 This library is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15 Lesser General Public License for more details.
16
17 You should have received a copy of the GNU Lesser General Public
18 License along with this library; if not, write to the Free Software
19 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
20 */
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <string.h>
24 #include "type.h"
25 #include "reader_master.h"
26 #include "giveio.h"
27 #include "file.h"
28 #include "script.h"
29 #include "header.h"
30 #include "textutil.h"
31 #include "config.h"
32 #include "flashmemory.h"
33 #include "client_test.h"
34
35 static int flag_get(const char *flag, struct st_config *c)
36 {
37         while(*flag != '\0'){
38                 switch(*flag){
39                 case 'S': case 's':
40                         c->backupram = CONFIG_OVERRIDE_TRUE;
41                         break;
42                 case 'H': case 'h':
43                         c->mirror = MIRROR_HORIZONAL;
44                         break;
45                 case 'V': case 'v':
46                         c->mirror = MIRROR_VERTICAL;
47                         break;
48                 case '_':
49                         break;
50                 case 'r':
51                         c->syntaxtest = 1;
52                         break;
53                 default:
54                         return NG;
55                 }
56                 flag++;
57         }
58         return OK;
59 }
60
61 static const char PREFIX_CONFIG_ERROR[] = "config error:";
62 static int config_file_load(struct st_config *c)
63 {
64         char *buf;
65         int size = 0;
66         c->reader = NULL;
67         buf = buf_load_full("unagi.cfg", &size);
68         if(buf == NULL){
69                 printf("%s config file open error\n", PREFIX_CONFIG_ERROR);
70                 return NG;
71         }
72         char **text;
73         text = malloc(sizeof(char*) * TEXT_MAXLINE);
74         const int text_num = text_load(buf, size, text);
75         if(text_num == 0){
76                 printf("%s script line too much\n", PREFIX_CONFIG_ERROR);
77                 free(buf);
78                 free(text);
79                 return NG;
80         }
81         int i;
82         for(i=0; i<text_num; i++){
83                 char *word[TEXT_MAXWORD];
84                 word_load(text[i], word);
85                 if(word[0][0] == '#'){
86                         continue;
87                 }
88                 if(strcmp("DRIVER", word[0]) == 0){
89                         c->reader = reader_driver_get(word[1]);
90                 }else if((DEBUG == 1) && (strcmp("WRITE_WAIT", word[0]) == 0)){
91                         if(value_get(word[1], &(c->write_wait)) == NG){
92                                 printf("%s write_wait parameter is illigal", PREFIX_CONFIG_ERROR);
93                                 free(buf);
94                                 free(text);
95                                 return NG;
96                         }
97                 }else if(strcmp("TEST_DEVICE", word[0]) == 0){
98                         if(DEBUG == 1){
99                                 strncpy(c->flash_test_device, word[1], 20);
100                         }
101                 }else if(strcmp("TEST_MAPPER", word[0]) == 0){
102                         if(DEBUG == 1){
103                                 strncpy(c->flash_test_mapper, word[1], 20);
104                         }
105                 }else{
106                         printf("%s unknown config title %s", PREFIX_CONFIG_ERROR, word[1]);
107                         free(buf);
108                         free(text);
109                         return NG;
110                 }
111         }
112         
113         free(text);
114         if(c->reader == NULL){
115                 printf("%s hardware not selected or not found\n", PREFIX_CONFIG_ERROR);
116                 free(buf);
117                 return NG;
118         }
119         free(buf);
120         return OK;
121 }
122
123 static const struct flash_driver DRIVER_UNDEF = {
124         .name = "undefined",
125         .capacity = 0,
126         .pagesize = 1,
127         .erase_wait = 0,
128         .command_mask = 0,
129         .id_manufacurer = 0,
130         .id_device = 0,
131         .productid_check = NULL,
132 #if DEBUG==1
133         .erase = NULL,
134 #endif
135         .init = NULL,
136         .write = NULL
137 };
138 static int flash_pointer_init(const char *device, const struct flash_driver **f)
139 {
140         *f = flash_driver_get(device);
141         if(*f == NULL){
142                 printf("%s unknown flash device %s\n", PREFIX_CONFIG_ERROR, device);
143                 return NG;
144         }
145         return OK;
146 }
147
148 enum{
149         ARGC_MODE = 1,
150         ARGC_SCRIPTFILE,
151         //DUMP MODE
152         ARGC_DUMP_OUT_NESFILE,
153         ARGC_DUMP_FLAG,
154         ARGC_DUMP_MAPPER,
155         //RAM RW MODE
156         ARGC_READ_OUT_RAMFILE = ARGC_DUMP_OUT_NESFILE,
157         ARGC_WRITE_IN_RAMFILE = ARGC_DUMP_OUT_NESFILE,
158         ARGC_RW_NUM,
159         //PROGRAM MODE
160         ARGC_PROGRAM_IN_NESFILE = ARGC_DUMP_OUT_NESFILE,
161         ARGC_PROGRAM_CPU_DEVICE,
162         ARGC_PROGRAM_PPU_DEVICE,
163         //TESTMODE
164         ARGC_TEST_OPTION = ARGC_SCRIPTFILE,
165         ARGC_TEST_FILE,
166         ARGC_TEST_NUM
167 };
168
169 static int config_init(const int argc, const char **argv, struct st_config *c)
170 {
171         c->script = argv[ARGC_SCRIPTFILE];
172         c->romimage = NULL;
173         c->ramimage = NULL;
174         c->mapper = CONFIG_OVERRIDE_UNDEF;
175         c->mirror = CONFIG_OVERRIDE_UNDEF;
176         c->backupram = CONFIG_OVERRIDE_UNDEF;
177         c->mapper = CONFIG_OVERRIDE_UNDEF;
178         c->syntaxtest = 0;
179         c->cpu_flash_driver = &DRIVER_UNDEF;
180         c->ppu_flash_driver = &DRIVER_UNDEF;
181         c->write_wait = 0;
182         //mode ÊÌ target file ½é´ü²½
183         switch(argv[ARGC_MODE][0]){
184         case 'd':
185                 c->mode = MODE_ROM_DUMP;
186                 c->romimage = argv[ARGC_DUMP_OUT_NESFILE];
187                 break;
188         case 'r':
189                 c->mode = MODE_RAM_READ;
190                 c->ramimage = argv[ARGC_READ_OUT_RAMFILE];
191                 break;
192         case 'w':
193                 c->mode = MODE_RAM_WRITE;
194                 c->ramimage = argv[ARGC_WRITE_IN_RAMFILE];
195                 break;
196         case 'f':
197                 c->mode = MODE_ROM_PROGRAM;
198                 c->romimage = argv[ARGC_PROGRAM_IN_NESFILE];
199                 break;
200         case 't':
201                 if(DEBUG == 1){
202                         c->mode = MODE_TEST;
203                         break;
204                 }
205                 //²¼¤Ø
206         default:
207                 printf("%s unkown mode %s\n", PREFIX_CONFIG_ERROR, argv[ARGC_MODE]);
208                 return NG;
209         };
210         if(config_file_load(c) == NG){
211                 return NG;
212         }
213         /*mode ÊÌ argc check. 
214         DEBUG==0  argc: 4,5,6
215         DEBUG==1  argc: 3,4,5,6
216         */
217         switch(c->mode){
218         case MODE_ROM_DUMP:{
219                 int flag_error = OK, mapper_error = OK;
220                 switch(argc){
221                 case 3:
222                         return NG;
223                 case 5:
224                         flag_error = flag_get(argv[ARGC_DUMP_FLAG], c);
225                         break;
226                 case 6:
227                         flag_error = flag_get(argv[ARGC_DUMP_FLAG], c);
228                         mapper_error = value_get(argv[ARGC_DUMP_MAPPER], &c->mapper);
229                         break;
230                 }
231                 if(flag_error != OK){
232                         printf("%s unknown flag %s\n", PREFIX_CONFIG_ERROR, argv[ARGC_DUMP_FLAG]);
233                         return NG;
234                 }
235                 if(mapper_error != OK){
236                         printf("%s unknown mapper %s\n", PREFIX_CONFIG_ERROR, argv[ARGC_DUMP_MAPPER]);
237                         return NG;
238                 }
239                 }break;
240         case MODE_RAM_READ:
241         case MODE_RAM_WRITE:
242                 if(argc != ARGC_RW_NUM){
243                         printf("%s too many argument\n", PREFIX_CONFIG_ERROR);
244                         return NG;
245                 }
246                 break;
247         case MODE_ROM_PROGRAM:
248                 switch(argc){
249                 case 3:
250                 case 4:
251                         printf("%s few argument\n", PREFIX_CONFIG_ERROR);
252                         return NG;
253                 case 5:
254                         if(flash_pointer_init(argv[ARGC_PROGRAM_CPU_DEVICE], &(c->cpu_flash_driver)) == NG){
255                                 return NG;
256                         }
257                         break;
258                 case 6:
259                         if(flash_pointer_init(argv[ARGC_PROGRAM_CPU_DEVICE], &(c->cpu_flash_driver)) == NG){
260                                 return NG;
261                         }
262                         if(flash_pointer_init(argv[ARGC_PROGRAM_PPU_DEVICE], &(c->ppu_flash_driver)) == NG){
263                                 return NG;
264                         }
265                         break;
266                 }
267                 break;
268         
269         case MODE_TEST:
270                 if(DEBUG == 0){
271                         break;
272                 }
273                 switch(argc){
274                 case 3:
275                         client_test(c->reader, argv[ARGC_TEST_OPTION], NULL, c->flash_test_device, c->flash_test_mapper);
276                         break;
277                 case 4:
278                         client_test(c->reader, argv[ARGC_TEST_OPTION], argv[ARGC_TEST_FILE], c->flash_test_device, c->flash_test_mapper);
279                         break;
280                 default:
281                         printf("%s test argc error\n", PREFIX_CONFIG_ERROR);
282                 }
283                 break;
284         }
285
286         return OK;
287 }
288
289 #include "version.h"
290 int main(int c, char **v)
291 {
292         struct st_config config;
293         int config_result = NG;
294         switch(c){
295         case 3: //t testmode target
296                 if(DEBUG == 0){
297                         goto usage;
298                 }
299                 //²¼¤Ø
300         case 4: //mode script target
301         case 5:
302                 //mode script target flag
303                 //mode script target cpu_flash_device
304         case 6:
305                 //mode script target flag mapper
306                 //mode script target cpu_flash_device ppu_flash_device
307                 config_result = config_init(c, (const char **) v, &config);
308                 break;
309         usage:
310         default:
311                 printf("famicom ROM cartridge utility - unagi version %s\n", STR_VERSION);
312                 printf("%s [mode] [mapper script] [target file] [flag]\n", v[0]);
313                 printf("mode - [d]ump ROM / [r]ead RAM/ [w]rite RAM/ [f]lash memory program\n");
314                 break;
315         }
316         if((config.mode != MODE_TEST) && (config_result == OK)){
317                 script_load(&config);
318         }
319         return 0;
320 }