main.c 4.0 KB

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