2 famicom ROM cartridge utility - unagi
3 command line interface, config file parser
5 Copyright (C) 2008 ±·³«È¯¶¨Æ±Áȹç
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.
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.
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
25 #include "reader_master.h"
32 #include "flashmemory.h"
33 #include "client_test.h"
35 static int flag_get(const char *flag, struct st_config *c)
40 c->backupram = CONFIG_OVERRIDE_TRUE;
43 c->mirror = MIRROR_HORIZONAL;
46 c->mirror = MIRROR_VERTICAL;
61 static const char PREFIX_CONFIG_ERROR[] = "config error:";
62 static int config_file_load(struct st_config *c)
67 buf = buf_load_full("unagi.cfg", &size);
69 printf("%s config file open error\n", PREFIX_CONFIG_ERROR);
73 text = malloc(sizeof(char*) * TEXT_MAXLINE);
74 const int text_num = text_load(buf, size, text);
76 printf("%s script line too much\n", PREFIX_CONFIG_ERROR);
82 for(i=0; i<text_num; i++){
83 char *word[TEXT_MAXWORD];
84 word_load(text[i], word);
85 if(word[0][0] == '#'){
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);
97 }else if(strcmp("TEST_DEVICE", word[0]) == 0){
99 strncpy(c->flash_test_device, word[1], 20);
101 }else if(strcmp("TEST_MAPPER", word[0]) == 0){
103 strncpy(c->flash_test_mapper, word[1], 20);
106 printf("%s unknown config title %s", PREFIX_CONFIG_ERROR, word[1]);
114 if(c->reader == NULL){
115 printf("%s hardware not selected or not found\n", PREFIX_CONFIG_ERROR);
123 static const struct flash_driver DRIVER_UNDEF = {
131 .productid_check = NULL,
138 static int flash_pointer_init(const char *device, const struct flash_driver **f)
140 *f = flash_driver_get(device);
142 printf("%s unknown flash device %s\n", PREFIX_CONFIG_ERROR, device);
152 ARGC_DUMP_OUT_NESFILE,
156 ARGC_READ_OUT_RAMFILE = ARGC_DUMP_OUT_NESFILE,
157 ARGC_WRITE_IN_RAMFILE = ARGC_DUMP_OUT_NESFILE,
160 ARGC_PROGRAM_IN_NESFILE = ARGC_DUMP_OUT_NESFILE,
161 ARGC_PROGRAM_CPU_DEVICE,
162 ARGC_PROGRAM_PPU_DEVICE,
164 ARGC_TEST_OPTION = ARGC_SCRIPTFILE,
169 static int config_init(const int argc, const char **argv, struct st_config *c)
171 c->script = argv[ARGC_SCRIPTFILE];
174 c->mapper = CONFIG_OVERRIDE_UNDEF;
175 c->mirror = CONFIG_OVERRIDE_UNDEF;
176 c->backupram = CONFIG_OVERRIDE_UNDEF;
177 c->mapper = CONFIG_OVERRIDE_UNDEF;
179 c->cpu_flash_driver = &DRIVER_UNDEF;
180 c->ppu_flash_driver = &DRIVER_UNDEF;
182 //mode ÊÌ target file ½é´ü²½
183 switch(argv[ARGC_MODE][0]){
185 c->mode = MODE_ROM_DUMP;
186 c->romimage = argv[ARGC_DUMP_OUT_NESFILE];
189 c->mode = MODE_RAM_READ;
190 c->ramimage = argv[ARGC_READ_OUT_RAMFILE];
193 c->mode = MODE_RAM_WRITE;
194 c->ramimage = argv[ARGC_WRITE_IN_RAMFILE];
197 c->mode = MODE_ROM_PROGRAM;
198 c->romimage = argv[ARGC_PROGRAM_IN_NESFILE];
207 printf("%s unkown mode %s\n", PREFIX_CONFIG_ERROR, argv[ARGC_MODE]);
210 if(config_file_load(c) == NG){
213 /*mode ÊÌ argc check.
215 DEBUG==1 argc: 3,4,5,6
219 int flag_error = OK, mapper_error = OK;
224 flag_error = flag_get(argv[ARGC_DUMP_FLAG], c);
227 flag_error = flag_get(argv[ARGC_DUMP_FLAG], c);
228 mapper_error = value_get(argv[ARGC_DUMP_MAPPER], &c->mapper);
231 if(flag_error != OK){
232 printf("%s unknown flag %s\n", PREFIX_CONFIG_ERROR, argv[ARGC_DUMP_FLAG]);
235 if(mapper_error != OK){
236 printf("%s unknown mapper %s\n", PREFIX_CONFIG_ERROR, argv[ARGC_DUMP_MAPPER]);
242 if(argc != ARGC_RW_NUM){
243 printf("%s too many argument\n", PREFIX_CONFIG_ERROR);
247 case MODE_ROM_PROGRAM:
251 printf("%s few argument\n", PREFIX_CONFIG_ERROR);
254 if(flash_pointer_init(argv[ARGC_PROGRAM_CPU_DEVICE], &(c->cpu_flash_driver)) == NG){
259 if(flash_pointer_init(argv[ARGC_PROGRAM_CPU_DEVICE], &(c->cpu_flash_driver)) == NG){
262 if(flash_pointer_init(argv[ARGC_PROGRAM_PPU_DEVICE], &(c->ppu_flash_driver)) == NG){
275 client_test(c->reader, argv[ARGC_TEST_OPTION], NULL, c->flash_test_device, c->flash_test_mapper);
278 client_test(c->reader, argv[ARGC_TEST_OPTION], argv[ARGC_TEST_FILE], c->flash_test_device, c->flash_test_mapper);
281 printf("%s test argc error\n", PREFIX_CONFIG_ERROR);
290 int main(int c, char **v)
292 struct st_config config;
293 int config_result = NG;
295 case 3: //t testmode target
300 case 4: //mode script target
302 //mode script target flag
303 //mode script target cpu_flash_device
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);
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");
316 if((config.mode != MODE_TEST) && (config_result == OK)){
317 script_load(&config);