fixed a possible deadlock in DrZ80
[picodrive.git] / platform / gp2x / main.c
1 // (c) Copyright 2006 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 <string.h>\r
8 #include <unistd.h>\r
9 #include <strings.h>\r
10 #include <linux/limits.h>\r
11 \r
12 #include "gp2x.h"\r
13 #include "menu.h"\r
14 #include "../common/menu.h"\r
15 #include "emu.h"\r
16 #include "940ctl.h"\r
17 #include "version.h"\r
18 \r
19 #include "squidgehack.h"\r
20 #include "cpuctrl.h"\r
21 \r
22 \r
23 extern char *ext_menu, *ext_state;\r
24 extern int select_exits;\r
25 extern char *PicoConfigFile;\r
26 int mmuhack_status = 0;\r
27 char **g_argv;\r
28 \r
29 void parse_cmd_line(int argc, char *argv[])\r
30 {\r
31         int x, unrecognized = 0;\r
32 \r
33         for(x = 1; x < argc; x++)\r
34         {\r
35                 if(argv[x][0] == '-')\r
36                 {\r
37                         if(strcasecmp(argv[x], "-menu") == 0) {\r
38                                 if(x+1 < argc) { ++x; ext_menu = argv[x]; } /* External Frontend: Program Name */\r
39                         }\r
40                         else if(strcasecmp(argv[x], "-state") == 0) {\r
41                                 if(x+1 < argc) { ++x; ext_state = argv[x]; } /* External Frontend: Arguments */\r
42                         }\r
43                         else if(strcasecmp(argv[x], "-config") == 0) {\r
44                                 if(x+1 < argc) { ++x; PicoConfigFile = argv[x]; }\r
45                         }\r
46                         else if(strcasecmp(argv[x], "-selectexit") == 0) {\r
47                                 select_exits = 1;\r
48                         }\r
49                         else {\r
50                                 unrecognized = 1;\r
51                                 break;\r
52                         }\r
53                 } else {\r
54                         /* External Frontend: ROM Name */\r
55                         FILE *f;\r
56                         strncpy(romFileName, argv[x], PATH_MAX);\r
57                         romFileName[PATH_MAX-1] = 0;\r
58                         f = fopen(romFileName, "rb");\r
59                         if (f) fclose(f);\r
60                         else unrecognized = 1;\r
61                         engineState = PGS_ReloadRom;\r
62                         break;\r
63                 }\r
64         }\r
65 \r
66         if (unrecognized) {\r
67                 printf("\n\n\nPicoDrive v" VERSION " (c) notaz, 2006-2007\n");\r
68                 printf("usage: %s [options] [romfile]\n", argv[0]);\r
69                 printf( "options:\n"\r
70                                 "-menu <menu_path> launch a custom program on exit instead of default gp2xmenu\n"\r
71                                 "-state <param>    pass '-state param' to the menu program\n"\r
72                                 "-config <file>    use specified config file instead of default 'picoconfig.bin'\n"\r
73                                 "                  see currentConfig_t structure in emu.h for the file format\n"\r
74                                 "-selectexit       pressing SELECT will exit the emu and start 'menu_path'\n");\r
75         }\r
76 }\r
77 \r
78 \r
79 int main(int argc, char *argv[])\r
80 {\r
81         g_argv = argv;\r
82 \r
83         emu_ReadConfig(0, 0);\r
84         gp2x_init();\r
85         if (currentConfig.EmuOpt&0x10) {\r
86                 int ret = mmuhack();\r
87                 printf("squidge hack code finished and returned %i\n", ret); fflush(stdout);\r
88                 mmuhack_status = ret;\r
89         }\r
90         cpuctrl_init();\r
91         // Reset940(1);\r
92         // Pause940(1);\r
93         if (currentConfig.EmuOpt&0x100) {\r
94                 printf("setting RAM timings.. "); fflush(stdout);\r
95                 // craigix: --trc 6 --tras 4 --twr 1 --tmrd 1 --trfc 1 --trp 2 --trcd 2\r
96                 set_RAM_Timings(6, 4, 1, 1, 1, 2, 2);\r
97                 printf("done.\n"); fflush(stdout);\r
98         }\r
99         sharedmem_init();\r
100         emu_Init();\r
101         menu_init();\r
102 \r
103         engineState = PGS_Menu;\r
104 \r
105         if (argc > 1)\r
106                 parse_cmd_line(argc, argv);\r
107 \r
108         for (;;)\r
109         {\r
110                 switch (engineState)\r
111                 {\r
112                         case PGS_Menu:\r
113                                 menu_loop();\r
114                                 break;\r
115 \r
116                         case PGS_ReloadRom:\r
117                                 if (emu_ReloadRom())\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 \r
128                         case PGS_Running:\r
129                                 emu_Loop();\r
130                                 break;\r
131 \r
132                         case PGS_Quit:\r
133                                 goto endloop;\r
134 \r
135                         default:\r
136                                 printf("engine got into unknown state (%i), exitting\n", engineState);\r
137                                 goto endloop;\r
138                 }\r
139         }\r
140 \r
141         endloop:\r
142 \r
143         emu_Deinit();\r
144         sharedmem_deinit();\r
145         cpuctrl_deinit();\r
146         gp2x_deinit();\r
147         if(mmuhack_status)\r
148                 mmuunhack();\r
149 \r
150         return 0;\r
151 }\r