main.c 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  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. #include <stdio.h>
  9. #include <stdlib.h>
  10. #include <unistd.h>
  11. #include <string.h>
  12. #include <strings.h>
  13. #include <signal.h>
  14. #ifdef USE_SDL
  15. #include <SDL.h>
  16. #endif
  17. #include "../libpicofe/input.h"
  18. #include "../libpicofe/plat.h"
  19. #include "menu_pico.h"
  20. #include "emu.h"
  21. #include "configfile_fk.h"
  22. #include "version.h"
  23. #include <cpu/debug.h>
  24. #ifdef USE_LIBRETRO_VFS
  25. #include "file_stream_transforms.h"
  26. #endif
  27. char **g_argv;
  28. char *prog_name;
  29. char *load_state_file = NULL;
  30. int load_state_slot = -1;
  31. static char *quick_save_file_extension = "quicksave";
  32. char *mRomName = NULL;
  33. char *mRomPath = NULL;
  34. char *quick_save_file = NULL;
  35. int need_quick_load = -1;
  36. char *cfg_file_default = NULL;
  37. char *cfg_file_rom = NULL;
  38. static char *cfg_file_default_name = "default_config";
  39. static char *cfg_file_extension = "fkcfg";
  40. int mQuickSaveAndPoweroff=0;
  41. void usage(){
  42. printf("\n\n\nPicoDrive v" VERSION " (c) notaz, 2006-2009,2013\n");
  43. printf("usage: PicoDriveBin [options] [romfile]\n");
  44. printf("options:\n"
  45. " -config <file> use specified config file instead of default 'config.cfg'\n"
  46. " -fps use to show fps\n"
  47. " -loadStateSlot <num> if ROM is specified, try loading savestate slot <num>\n"
  48. " -loadStateFile <filePath> if ROM is specified, try loading savestate file <filePath>\n");
  49. exit(1);
  50. }
  51. /* Handler for SIGUSR1, caused by closing the console */
  52. void handle_sigusr1(int sig)
  53. {
  54. //printf("Caught signal USR1 %d\n", sig);
  55. /* Exit menu if it was launched */
  56. stop_menu_loop = 1;
  57. /* Signal to quick save and poweoff after next loop */
  58. mQuickSaveAndPoweroff = 1;
  59. }
  60. void parse_cmd_line(int argc, char *argv[])
  61. {
  62. int x, unrecognized = 0;
  63. for (x = 1; x < argc; x++)
  64. {
  65. if (argv[x][0] == '-')
  66. {
  67. if (strcasecmp(argv[x], "-config") == 0) {
  68. if (x+1 < argc) { ++x; PicoConfigFile = argv[x]; }
  69. }
  70. else if (strcasecmp(argv[x], "-loadStateSlot") == 0
  71. || strcasecmp(argv[x], "-load") == 0)
  72. {
  73. if (x+1 < argc) { ++x; load_state_slot = atoi(argv[x]); }
  74. }
  75. else if (strcasecmp(argv[x], "-loadStateFile") == 0)
  76. {
  77. if (x+1 < argc) { ++x; load_state_file = argv[x]; }
  78. }
  79. else if (strcasecmp(argv[x], "-fps") == 0) {
  80. currentConfig.EmuOpt |= EOPT_SHOW_FPS;
  81. show_fps_bypass = 1;
  82. }
  83. else if (strcasecmp(argv[x], "-pdb") == 0) {
  84. if (x+1 < argc) { ++x; pdb_command(argv[x]); }
  85. }
  86. else if (strcasecmp(argv[x], "-pdb_connect") == 0) {
  87. if (x+2 < argc) { pdb_net_connect(argv[x+1], argv[x+2]); x += 2; }
  88. }
  89. else {
  90. unrecognized = 1;
  91. break;
  92. }
  93. }
  94. /* Check if file exists, Save ROM name, and ROM path */
  95. else {
  96. mRomName = argv[x];
  97. FILE *f = fopen(argv[x], "rb");
  98. if (f) {
  99. /* Save Rom path */
  100. mRomPath = (char *)malloc(strlen(mRomName)+1);
  101. strcpy(mRomPath, mRomName);
  102. char *slash = strrchr ((char*)mRomPath, '/');
  103. *slash = 0;
  104. /* Rom name without extension */
  105. char *point = strrchr ((char*)slash+1, '.');
  106. *point = 0;
  107. /* Set quicksave filename */
  108. quick_save_file = (char *)malloc(strlen(mRomPath) + strlen(slash+1) +
  109. strlen(quick_save_file_extension) + 2 + 1);
  110. sprintf(quick_save_file, "%s/%s.%s",
  111. mRomPath, slash+1, quick_save_file_extension);
  112. printf("Quick_save_file: %s\n", quick_save_file);
  113. /* Set rom cfg filepath */
  114. cfg_file_rom = (char *)malloc(strlen(mRomPath) + strlen(slash+1) +
  115. strlen(cfg_file_extension) + 2 + 1);
  116. sprintf(cfg_file_rom, "%s/%s.%s",
  117. mRomPath, slash+1, cfg_file_extension);
  118. printf("cfg_file_rom: %s\n", cfg_file_rom);
  119. /* Set console cfg filepath */
  120. cfg_file_default = (char *)malloc(strlen(mRomPath) + strlen(cfg_file_default_name) +
  121. strlen(cfg_file_extension) + 2 + 1);
  122. sprintf(cfg_file_default, "%s/%s.%s",
  123. mRomPath, cfg_file_default_name, cfg_file_extension);
  124. printf("cfg_file_default: %s\n", cfg_file_default);
  125. /** Load config files */
  126. configfile_load(cfg_file_default);
  127. configfile_load(cfg_file_rom);
  128. /* Close file*/
  129. fclose(f);
  130. rom_fname_reload = argv[x];
  131. engineState = PGS_ReloadRom;
  132. }
  133. else{
  134. printf("Rom %s not found \n", mRomName);
  135. unrecognized = 1;
  136. }
  137. break;
  138. }
  139. }
  140. if (unrecognized) {
  141. usage();
  142. }
  143. }
  144. int main(int argc, char *argv[])
  145. {
  146. g_argv = argv;
  147. /* Save program name */
  148. prog_name = argv[0];
  149. /* Engine initial state */
  150. engineState = PGS_Menu;
  151. /* Parse arguments */
  152. if (argc > 1){
  153. parse_cmd_line(argc, argv);
  154. }
  155. else{
  156. usage();
  157. }
  158. /* Init Signals */
  159. signal(SIGUSR1, handle_sigusr1);
  160. /* Set env var for no mouse */
  161. putenv(strdup("SDL_NOMOUSE=1"));
  162. plat_early_init();
  163. in_init();
  164. //in_probe();
  165. plat_target_init();
  166. plat_init();
  167. //menu_init();
  168. emu_prep_defconfig(); // depends on input
  169. emu_read_config(NULL, 0);
  170. emu_init();
  171. menu_init();
  172. if (engineState == PGS_ReloadRom)
  173. {
  174. if (emu_reload_rom(rom_fname_reload)) {
  175. engineState = PGS_Running;
  176. /* Load slot (if NOT sega CD) */
  177. if(load_state_slot != -1){
  178. /* DO NOT put this if in the previous one, we need this here not to enter the next else */
  179. if(!emu_is_segaCD()){
  180. printf("LOADING FROM SLOT %d...\n", load_state_slot+1);
  181. char fname[1024];
  182. emu_save_load_game(1, 0);
  183. printf("LOADED FROM SLOT %d\n", load_state_slot+1);
  184. load_state_slot = -1;
  185. }
  186. }
  187. /* Load file (if NOT sega CD) */
  188. else if(load_state_file != NULL){
  189. /* DO NOT put this if in the previous one, we need this here not to enter the next else */
  190. if(!emu_is_segaCD()){
  191. printf("LOADING FROM FILE %s...\n", load_state_file);
  192. emu_save_load_game_from_file(1, load_state_file);
  193. printf("LOADED FROM SLOT %s\n", load_state_file);
  194. load_state_file = NULL;
  195. }
  196. }
  197. /* Load quick save file (if NOT sega CD) */
  198. else if(access( quick_save_file, F_OK ) != -1){
  199. printf("Found quick save file: %s\n", quick_save_file);
  200. int resume = launch_resume_menu_loop();
  201. if(resume == RESUME_YES){
  202. printf("Resume game from quick save file: %s\n", quick_save_file);
  203. if(emu_is_segaCD()){
  204. need_quick_load = 1;
  205. }
  206. else{
  207. emu_save_load_game_from_file(1, quick_save_file);
  208. }
  209. }
  210. else{
  211. printf("Reset game\n");
  212. /* Remove quicksave file if present */
  213. if (remove(quick_save_file) == 0){
  214. printf("Deleted successfully: %s\n", quick_save_file);
  215. }
  216. else{
  217. printf("Unable to delete the file: %s\n", quick_save_file);
  218. }
  219. }
  220. }
  221. }
  222. }
  223. for (;;)
  224. {
  225. /** Debug */
  226. /*static int prev_engine_state = -1;
  227. if(prev_engine_state != engineState){
  228. printf("current engine state: %d, prev = %d\n", engineState, prev_engine_state);
  229. prev_engine_state = engineState;
  230. }*/
  231. switch (engineState)
  232. {
  233. case PGS_Menu:
  234. //menu_loop();
  235. menu_loop_funkey();
  236. break;
  237. case PGS_TrayMenu:
  238. menu_loop_tray();
  239. break;
  240. case PGS_ReloadRom:
  241. if (emu_reload_rom(rom_fname_reload))
  242. engineState = PGS_Running;
  243. else {
  244. printf("PGS_ReloadRom == 0\n");
  245. engineState = PGS_Menu;
  246. }
  247. break;
  248. case PGS_RestartRun:
  249. engineState = PGS_Running;
  250. /* vvv fallthrough */
  251. case PGS_Running:
  252. #ifdef GPERF
  253. ProfilerStart("gperf.out");
  254. #endif
  255. emu_loop();
  256. #ifdef GPERF
  257. ProfilerStop();
  258. #endif
  259. break;
  260. case PGS_Quit:
  261. goto endloop;
  262. default:
  263. printf("engine got into unknown state (%i), exitting\n", engineState);
  264. goto endloop;
  265. }
  266. }
  267. endloop:
  268. emu_finish();
  269. plat_finish();
  270. plat_target_finish();
  271. return 0;
  272. }