2 famicom ROM cartridge utility - unagi
3 command line interface, config file pharser
5 Copyright (C) 2008 sato_tiff
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]);
92 printf("%s unknown config title %s", PREFIX_CONFIG_ERROR, word[1]);
100 if(c->reader == NULL){
101 printf("%s hardware not selected or not found\n", PREFIX_CONFIG_ERROR);
109 static const struct flash_driver DRIVER_UNDEF = {
115 .productid_check = NULL,
122 static int flash_pointer_init(const char *device, const struct flash_driver **f)
124 *f = flash_driver_get(device);
126 printf("%s unknown flash device %s\n", PREFIX_CONFIG_ERROR, device);
136 ARGC_DUMP_OUT_NESFILE,
140 ARGC_READ_OUT_RAMFILE = ARGC_DUMP_OUT_NESFILE,
141 ARGC_WRITE_IN_RAMFILE = ARGC_DUMP_OUT_NESFILE,
144 ARGC_PROGRAM_IN_NESFILE = ARGC_DUMP_OUT_NESFILE,
145 ARGC_PROGRAM_CPU_DEVICE,
146 ARGC_PROGRAM_PPU_DEVICE,
148 ARGC_TEST_OPTION = ARGC_SCRIPTFILE,
153 static int config_init(const int argc, const char **argv, struct st_config *c)
155 c->script = argv[ARGC_SCRIPTFILE];
158 c->mapper = CONFIG_OVERRIDE_UNDEF;
159 c->mirror = CONFIG_OVERRIDE_UNDEF;
160 c->backupram = CONFIG_OVERRIDE_UNDEF;
161 c->mapper = CONFIG_OVERRIDE_UNDEF;
163 c->cpu_flash_driver = &DRIVER_UNDEF;
164 c->ppu_flash_driver = &DRIVER_UNDEF;
165 //mode ÊÌ target file ½é´ü²½
166 switch(argv[ARGC_MODE][0]){
168 c->mode = MODE_ROM_DUMP;
169 c->romimage = argv[ARGC_DUMP_OUT_NESFILE];
172 c->mode = MODE_RAM_READ;
173 c->ramimage = argv[ARGC_READ_OUT_RAMFILE];
176 c->mode = MODE_RAM_WRITE;
177 c->ramimage = argv[ARGC_WRITE_IN_RAMFILE];
180 c->mode = MODE_ROM_PROGRAM;
181 c->romimage = argv[ARGC_PROGRAM_IN_NESFILE];
190 printf("%s unkown mode %s\n", PREFIX_CONFIG_ERROR, argv[ARGC_MODE]);
193 if(config_file_load(c) == NG){
196 /*mode ÊÌ argc check.
198 DEBUG==1 argc: 3,4,5,6
202 int flag_error = OK, mapper_error = OK;
207 flag_error = flag_get(argv[ARGC_DUMP_FLAG], c);
210 flag_error = flag_get(argv[ARGC_DUMP_FLAG], c);
211 mapper_error = value_get(argv[ARGC_DUMP_MAPPER], &c->mapper);
214 if(flag_error != OK){
215 printf("%s unknown flag %s\n", PREFIX_CONFIG_ERROR, argv[ARGC_DUMP_FLAG]);
218 if(mapper_error != OK){
219 printf("%s unknown mapper %s\n", PREFIX_CONFIG_ERROR, argv[ARGC_DUMP_MAPPER]);
225 if(argc != ARGC_RW_NUM){
226 printf("%s too many argument\n", PREFIX_CONFIG_ERROR);
230 case MODE_ROM_PROGRAM:
234 printf("%s few argument\n", PREFIX_CONFIG_ERROR);
237 if(flash_pointer_init(argv[ARGC_PROGRAM_CPU_DEVICE], &(c->cpu_flash_driver)) == NG){
242 if(flash_pointer_init(argv[ARGC_PROGRAM_CPU_DEVICE], &(c->cpu_flash_driver)) == NG){
245 if(flash_pointer_init(argv[ARGC_PROGRAM_PPU_DEVICE], &(c->ppu_flash_driver)) == NG){
258 client_test(c->reader, argv[ARGC_TEST_OPTION], NULL);
261 client_test(c->reader, argv[ARGC_TEST_OPTION], argv[ARGC_TEST_FILE]);
264 printf("%s test argc error\n", PREFIX_CONFIG_ERROR);
273 int main(int c, char **v)
275 struct st_config config;
276 int config_result = NG;
278 case 3: //t testmode target
283 case 4: //mode script target
285 //mode script target flag
286 //mode script target cpu_flash_device
288 //mode script target flag mapper
289 //mode script target cpu_flash_device ppu_flash_device
290 config_result = config_init(c, (const char **) v, &config);
294 printf("famicom ROM cartridge utility - unagi version %s\n", STR_VERSION);
295 printf("%s [mode] [mapper script] [target file] [flag]\n", v[0]);
296 printf("mode - [d]ump ROM / [r]ead RAM/ [w]rite RAM/ program [f]lash memory\n");
299 if((config.mode != MODE_TEST) && (config_result == OK)){
300 script_load(&config);