1 /*
2  * PicoDrive
3  * (C) notaz, 2006-2010
4  *
5  * This work is licensed under the terms of MAME license.
6  * See COPYING file in the top-level directory.
7  */
8 
9 #include <stdio.h>
10 #include <stdlib.h>
11 #include <string.h>
12 #include <strings.h>
13 
14 #include "../libpicofe/input.h"
15 #include "../libpicofe/plat.h"
16 #include "menu_pico.h"
17 #include "emu.h"
18 #include "version.h"
19 #include <cpu/debug.h>
20 
21 #ifdef USE_LIBRETRO_VFS
22 #include "file_stream_transforms.h"
23 #endif
24 
25 static int load_state_slot = -1;
26 char **g_argv;
27 
parse_cmd_line(int argc,char * argv[])28 void parse_cmd_line(int argc, char *argv[])
29 {
30 	int x, unrecognized = 0;
31 
32 	for (x = 1; x < argc; x++)
33 	{
34 		if (argv[x][0] == '-')
35 		{
36 			if (strcasecmp(argv[x], "-config") == 0) {
37 				if (x+1 < argc) { ++x; PicoConfigFile = argv[x]; }
38 			}
39 			else if (strcasecmp(argv[x], "-loadstate") == 0
40 				 || strcasecmp(argv[x], "-load") == 0)
41 			{
42 				if (x+1 < argc) { ++x; load_state_slot = atoi(argv[x]); }
43 			}
44 			else if (strcasecmp(argv[x], "-pdb") == 0) {
45 				if (x+1 < argc) { ++x; pdb_command(argv[x]); }
46 			}
47 			else if (strcasecmp(argv[x], "-pdb_connect") == 0) {
48 				if (x+2 < argc) { pdb_net_connect(argv[x+1], argv[x+2]); x += 2; }
49 			}
50 			else {
51 				unrecognized = 1;
52 				break;
53 			}
54 		} else {
55 			FILE *f = fopen(argv[x], "rb");
56 			if (f) {
57 				fclose(f);
58 				rom_fname_reload = argv[x];
59 				engineState = PGS_ReloadRom;
60 			}
61 			else
62 				unrecognized = 1;
63 			break;
64 		}
65 	}
66 
67 	if (unrecognized) {
68 		printf("\n\n\nPicoDrive v" VERSION " (c) notaz, 2006-2009,2013\n");
69 		printf("usage: %s [options] [romfile]\n", argv[0]);
70 		printf("options:\n"
71 			" -config <file>    use specified config file instead of default 'config.cfg'\n"
72 			" -loadstate <num>  if ROM is specified, try loading savestate slot <num>\n");
73 		exit(1);
74 	}
75 }
76 
77 
main(int argc,char * argv[])78 int main(int argc, char *argv[])
79 {
80 	g_argv = argv;
81 
82 	plat_early_init();
83 
84 	in_init();
85 	//in_probe();
86 
87 	plat_target_init();
88 	plat_init();
89 
90 	emu_prep_defconfig(); // depends on input
91 	emu_read_config(NULL, 0);
92 
93 	emu_init();
94 	menu_init();
95 
96 	engineState = PGS_Menu;
97 
98 	if (argc > 1)
99 		parse_cmd_line(argc, argv);
100 
101 	if (engineState == PGS_ReloadRom)
102 	{
103 		if (emu_reload_rom(rom_fname_reload)) {
104 			engineState = PGS_Running;
105 			if (load_state_slot >= 0) {
106 				state_slot = load_state_slot;
107 				emu_save_load_game(1, 0);
108 			}
109 		}
110 	}
111 
112 	for (;;)
113 	{
114 		switch (engineState)
115 		{
116 			case PGS_Menu:
117 				menu_loop();
118 				break;
119 
120 			case PGS_TrayMenu:
121 				menu_loop_tray();
122 				break;
123 
124 			case PGS_ReloadRom:
125 				if (emu_reload_rom(rom_fname_reload))
126 					engineState = PGS_Running;
127 				else {
128 					printf("PGS_ReloadRom == 0\n");
129 					engineState = PGS_Menu;
130 				}
131 				break;
132 
133 			case PGS_RestartRun:
134 				engineState = PGS_Running;
135 				/* vvv fallthrough */
136 
137 			case PGS_Running:
138 				emu_loop();
139 				break;
140 
141 			case PGS_Quit:
142 				goto endloop;
143 
144 			default:
145 				printf("engine got into unknown state (%i), exitting\n", engineState);
146 				goto endloop;
147 		}
148 	}
149 
150 	endloop:
151 
152 	emu_finish();
153 	plat_finish();
154 	plat_target_finish();
155 
156 	return 0;
157 }
158