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