OSDN Git Service

anago: flash device をスクリプト化
[unagi/old-svn-converted.git] / client / trunk / anago / anago.c
1 #include <stdio.h>
2 #include <stdbool.h>
3 #include "memory_manage.h"
4 #include "type.h"
5 //#include "flashmemory.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.h"
12
13 static bool transtype_flash_set(char mode, struct memory *t)
14 {
15         switch(mode){
16         case 't':
17                 t->transtype = TRANSTYPE_TOP;
18                 break;
19         case 'e':
20                 t->transtype = TRANSTYPE_EMPTY;
21                 break;
22         case 'b':
23                 t->transtype = TRANSTYPE_BOTTOM;
24                 break;
25         case 'f':
26                 t->transtype = TRANSTYPE_FULL;
27                 break;
28         default:
29                 return false;
30         }
31         return true;
32 }
33 static bool transtype_set(const char *mode, struct romimage *t)
34 {
35         switch(mode[0]){
36         case 'd': case 'f':
37                 if(mode[1] == '\0'){
38                         t->cpu_rom.transtype = TRANSTYPE_FULL;
39                         t->ppu_rom.transtype = TRANSTYPE_FULL;
40                         return true;
41                 }
42                 if(transtype_flash_set(mode[1], &t->cpu_rom) == false){
43                         return false;
44                 }
45                 if(mode[2] == '\0'){
46                         t->ppu_rom.transtype = TRANSTYPE_FULL;
47                         return true;
48                 }
49                 if(transtype_flash_set(mode[2], &t->ppu_rom) == false){
50                         return false;
51                 }
52                 return true;
53         }
54         return false;
55 }
56 static bool config_parse(const char *romimage, const char *device_cpu, const char *device_ppu, struct config *c)
57 {
58         c->target = romimage;
59         if(nesfile_load(__FUNCTION__, romimage, &c->rom) == false){
60                 return false;
61         }
62         c->rom.cpu_rom.offset = 0;
63         c->rom.ppu_rom.offset = 0;
64         if(flash_device_get(device_cpu, &c->flash_cpu) == false){
65                 printf("unkown flash memory device %s\n", device_cpu);
66                 return false;
67         }
68         if(flash_device_get(device_ppu, &c->flash_ppu) == false){
69                 printf("unkown flash memory device %s\n", device_ppu);
70                 return false;
71         }
72         if(c->flash_cpu.id_device == FLASH_ID_DEVICE_DUMMY){
73                 c->rom.cpu_rom.transtype = TRANSTYPE_EMPTY;
74         }
75         if(
76                 (c->flash_ppu.id_device == FLASH_ID_DEVICE_DUMMY) ||
77                 (c->rom.ppu_rom.size == 0)
78         ){
79                 c->rom.ppu_rom.transtype = TRANSTYPE_EMPTY;
80         }
81         return true;
82 }
83 static void anago(int c, char **v)
84 {
85         struct config config;
86         config.script = v[2];
87         config.reader = &DRIVER_KAZZO;
88         if(v[1][0] == 'd'){
89                 config.reader = &DRIVER_DUMMY;
90         }       
91         if(transtype_set(v[1], &config.rom) == false){
92                 puts("mode argument error");
93                 return;
94         }
95         switch(c){
96         case 5: //mode script target cpu_flash_device
97                 if(config_parse(v[3], v[4], "dummy", &config) == false){
98                         nesbuffer_free(&config.rom, 0);
99                         return;
100                 }
101                 break;
102         case 6: //mode script target cpu_flash_device ppu_flash_device
103                 if(config_parse(v[3], v[4], v[5], &config) == false){
104                         nesbuffer_free(&config.rom, 0);
105                         return;
106                 }
107                 break;
108         default:
109                 puts("mode script target cpu_flash_device ppu_flash_device");
110                 return;
111         }
112         if(config.reader->open_or_close(READER_OPEN) == NG){
113                 puts("reader open error");
114                 nesbuffer_free(&config.rom, 0);
115                 return;
116         }
117         script_execute(&config);
118         nesbuffer_free(&config.rom, 0);
119         config.reader->open_or_close(READER_CLOSE);
120 }
121 int main(int c, char **v)
122 {
123         mm_init();
124         anago(c, v);
125         mm_end();
126         return 0;
127 }