48f218fccd7226310ec59d10a7e2902ba7beb355
[picodrive.git] / 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 <cpu/debug.h>\r
17 #include <version.h>\r
18 \r
19 \r
20 extern char *PicoConfigFile;\r
21 static int load_state_slot = -1;\r
22 char **g_argv;\r
23 \r
24 void parse_cmd_line(int argc, char *argv[])\r
25 {\r
26         int x, unrecognized = 0;\r
27 \r
28         for (x = 1; x < argc; x++)\r
29         {\r
30                 if (argv[x][0] == '-')\r
31                 {\r
32                         if (strcasecmp(argv[x], "-config") == 0) {\r
33                                 if (x+1 < argc) { ++x; PicoConfigFile = argv[x]; }\r
34                         }\r
35                         else if (strcasecmp(argv[x], "-loadstate") == 0) {\r
36                                 if (x+1 < argc) { ++x; load_state_slot = atoi(argv[x]); }\r
37                         }\r
38                         else if (strcasecmp(argv[x], "-pdb") == 0) {\r
39                                 if (x+1 < argc) { ++x; pdb_command(argv[x]); }\r
40                         }\r
41                         else if (strcasecmp(argv[x], "-pdb_connect") == 0) {\r
42                                 if (x+2 < argc) { pdb_net_connect(argv[x+1], argv[x+2]); x += 2; }\r
43                         }\r
44                         else {\r
45                                 unrecognized = 1;\r
46                                 break;\r
47                         }\r
48                 } else {\r
49                         /* External Frontend: ROM Name */\r
50                         FILE *f;\r
51                         strncpy(rom_fname_reload, argv[x], sizeof(rom_fname_reload));\r
52                         rom_fname_reload[sizeof(rom_fname_reload) - 1] = 0;\r
53                         f = fopen(rom_fname_reload, "rb");\r
54                         if (f) fclose(f);\r
55                         else unrecognized = 1;\r
56                         engineState = PGS_ReloadRom;\r
57                         break;\r
58                 }\r
59         }\r
60 \r
61         if (unrecognized) {\r
62                 printf("\n\n\nPicoDrive v" VERSION " (c) notaz, 2006-2009\n");\r
63                 printf("usage: %s [options] [romfile]\n", argv[0]);\r
64                 printf("options:\n"\r
65                         " -config <file>    use specified config file instead of default 'config.cfg'\n"\r
66                         " -loadstate <num>  if ROM is specified, try loading slot <num>\n");\r
67         }\r
68 }\r
69 \r
70 \r
71 int main(int argc, char *argv[])\r
72 {\r
73         g_argv = argv;\r
74 \r
75         plat_early_init();\r
76 \r
77         in_init();\r
78         in_probe();\r
79 \r
80         plat_init();\r
81 \r
82         emu_prep_defconfig(); // depends on input\r
83         emu_read_config(NULL, 0);\r
84 \r
85         emu_init();\r
86         menu_init();\r
87 \r
88         engineState = PGS_Menu;\r
89 \r
90         if (argc > 1)\r
91                 parse_cmd_line(argc, argv);\r
92 \r
93         if (engineState == PGS_ReloadRom)\r
94         {\r
95                 if (emu_reload_rom(rom_fname_reload)) {\r
96                         engineState = PGS_Running;\r
97                         if (load_state_slot >= 0) {\r
98                                 state_slot = load_state_slot;\r
99                                 emu_save_load_game(1, 0);\r
100                         }\r
101                 }\r
102         }\r
103 \r
104         for (;;)\r
105         {\r
106                 switch (engineState)\r
107                 {\r
108                         case PGS_Menu:\r
109                                 menu_loop();\r
110                                 break;\r
111 \r
112                         case PGS_TrayMenu:\r
113                                 menu_loop_tray();\r
114                                 break;\r
115 \r
116                         case PGS_ReloadRom:\r
117                                 if (emu_reload_rom(rom_fname_reload))\r
118                                         engineState = PGS_Running;\r
119                                 else {\r
120                                         printf("PGS_ReloadRom == 0\n");\r
121                                         engineState = PGS_Menu;\r
122                                 }\r
123                                 break;\r
124 \r
125                         case PGS_RestartRun:\r
126                                 engineState = PGS_Running;\r
127                                 /* vvv fallthrough */\r
128 \r
129                         case PGS_Running:\r
130                                 emu_loop();\r
131                                 break;\r
132 \r
133                         case PGS_Quit:\r
134                                 goto endloop;\r
135 \r
136                         default:\r
137                                 printf("engine got into unknown state (%i), exitting\n", engineState);\r
138                                 goto endloop;\r
139                 }\r
140         }\r
141 \r
142         endloop:\r
143 \r
144         emu_finish();\r
145         plat_finish();\r
146 \r
147         return 0;\r
148 }\r