bugfix
[picodrive.git] / platform / common / main.c
1 // (c) Copyright 2006-2009 notaz, All rights reserved.\r
2 // Free for non-commercial use.\r
3 \r
4 // For commercial use, separate licencing terms must be obtained.\r
5 \r
6 #include <stdio.h>\r
7 #include <stdlib.h>\r
8 #include <string.h>\r
9 #include <strings.h>\r
10 \r
11 #include "menu.h"\r
12 #include "emu.h"\r
13 #include "config.h"\r
14 #include "input.h"\r
15 #include "plat.h"\r
16 #include <version.h>\r
17 \r
18 \r
19 extern char *PicoConfigFile;\r
20 static int load_state_slot = -1;\r
21 char **g_argv;\r
22 \r
23 void parse_cmd_line(int argc, char *argv[])\r
24 {\r
25         int x, unrecognized = 0;\r
26 \r
27         for (x = 1; x < argc; x++)\r
28         {\r
29                 if (argv[x][0] == '-')\r
30                 {\r
31                         if (strcasecmp(argv[x], "-config") == 0) {\r
32                                 if (x+1 < argc) { ++x; PicoConfigFile = argv[x]; }\r
33                         }\r
34                         else if (strcasecmp(argv[x], "-loadstate") == 0) {\r
35                                 if (x+1 < argc) { ++x; load_state_slot = atoi(argv[x]); }\r
36                         }\r
37                         else {\r
38                                 unrecognized = 1;\r
39                                 break;\r
40                         }\r
41                 } else {\r
42                         /* External Frontend: ROM Name */\r
43                         FILE *f;\r
44                         strncpy(rom_fname_reload, argv[x], sizeof(rom_fname_reload));\r
45                         rom_fname_reload[sizeof(rom_fname_reload) - 1] = 0;\r
46                         f = fopen(rom_fname_reload, "rb");\r
47                         if (f) fclose(f);\r
48                         else unrecognized = 1;\r
49                         engineState = PGS_ReloadRom;\r
50                         break;\r
51                 }\r
52         }\r
53 \r
54         if (unrecognized) {\r
55                 printf("\n\n\nPicoDrive v" VERSION " (c) notaz, 2006-2009\n");\r
56                 printf("usage: %s [options] [romfile]\n", argv[0]);\r
57                 printf("options:\n"\r
58                         " -config <file>    use specified config file instead of default 'config.cfg'\n"\r
59                         " -loadstate <num>  if ROM is specified, try loading slot <num>\n");\r
60         }\r
61 }\r
62 \r
63 \r
64 int main(int argc, char *argv[])\r
65 {\r
66         g_argv = argv;\r
67 \r
68         plat_early_init();\r
69 \r
70         /* in_init() must go before config, config accesses in_ fwk */\r
71         in_init();\r
72         pemu_prep_defconfig();\r
73         emu_read_config(0, 0);\r
74         config_readlrom(PicoConfigFile);\r
75 \r
76         plat_init();\r
77         in_probe();\r
78         in_debug_dump();\r
79 \r
80         emu_init();\r
81         menu_init();\r
82 \r
83         engineState = PGS_Menu;\r
84 \r
85         if (argc > 1)\r
86                 parse_cmd_line(argc, argv);\r
87 \r
88         if (engineState == PGS_ReloadRom)\r
89         {\r
90                 if (emu_reload_rom(rom_fname_reload)) {\r
91                         engineState = PGS_Running;\r
92                         if (load_state_slot >= 0) {\r
93                                 state_slot = load_state_slot;\r
94                                 emu_save_load_game(1, 0);\r
95                         }\r
96                 }\r
97         }\r
98 \r
99         for (;;)\r
100         {\r
101                 switch (engineState)\r
102                 {\r
103                         case PGS_Menu:\r
104                                 menu_loop();\r
105                                 break;\r
106 \r
107                         case PGS_TrayMenu:\r
108                                 menu_loop_tray();\r
109                                 break;\r
110 \r
111                         case PGS_ReloadRom:\r
112                                 if (emu_reload_rom(rom_fname_reload))\r
113                                         engineState = PGS_Running;\r
114                                 else {\r
115                                         printf("PGS_ReloadRom == 0\n");\r
116                                         engineState = PGS_Menu;\r
117                                 }\r
118                                 break;\r
119 \r
120                         case PGS_RestartRun:\r
121                                 engineState = PGS_Running;\r
122                                 /* vvv fallthrough */\r
123 \r
124                         case PGS_Running:\r
125                                 emu_loop();\r
126                                 break;\r
127 \r
128                         case PGS_Quit:\r
129                                 goto endloop;\r
130 \r
131                         default:\r
132                                 printf("engine got into unknown state (%i), exitting\n", engineState);\r
133                                 goto endloop;\r
134                 }\r
135         }\r
136 \r
137         endloop:\r
138 \r
139         emu_finish();\r
140         plat_finish();\r
141 \r
142         return 0;\r
143 }\r