PageRenderTime 60ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/src/wiz/wiz.cpp

https://github.com/fanwen/mame4iphone-private
C++ | 373 lines | 278 code | 46 blank | 49 comment | 122 complexity | 72bacce3449411c66d388b094b8cc984 MD5 | raw file
  1. /***************************************************************************
  2. osdepend.c
  3. OS dependant stuff (display handling, keyboard scan...)
  4. This is the only file which should me modified in order to port the
  5. emulator to a different system.
  6. ***************************************************************************/
  7. #include "driver.h"
  8. #include <signal.h>
  9. #include <time.h>
  10. #include <ctype.h>
  11. #include "stdarg.h"
  12. #include "string.h"
  13. #include "wiz_lib.h"
  14. int msdos_init_sound(void);
  15. void msdos_init_input(void);
  16. void msdos_shutdown_sound(void);
  17. void msdos_shutdown_input(void);
  18. int frontend_help (int argc, char **argv);
  19. void parse_cmdline (int argc, char **argv, int game);
  20. void init_inpdir(void);
  21. static FILE *errorlog;
  22. /* put here anything you need to do when the program is started. Return 0 if */
  23. /* initialization was successful, nonzero otherwise. */
  24. int osd_init(void)
  25. {
  26. if (msdos_init_sound())
  27. return 1;
  28. msdos_init_input();
  29. return 0;
  30. }
  31. /* put here cleanup routines to be executed when the program is terminated. */
  32. void osd_exit(void)
  33. {
  34. msdos_shutdown_sound();
  35. msdos_shutdown_input();
  36. }
  37. /* fuzzy string compare, compare short string against long string */
  38. /* e.g. astdel == "Asteroids Deluxe". The return code is the fuzz index, */
  39. /* we simply count the gaps between maching chars. */
  40. int fuzzycmp (const char *s, const char *l)
  41. {
  42. int gaps = 0;
  43. int match = 0;
  44. int last = 1;
  45. for (; *s && *l; l++)
  46. {
  47. if (*s == *l)
  48. match = 1;
  49. else if (*s >= 'a' && *s <= 'z' && (*s - 'a') == (*l - 'A'))
  50. match = 1;
  51. else if (*s >= 'A' && *s <= 'Z' && (*s - 'A') == (*l - 'a'))
  52. match = 1;
  53. else
  54. match = 0;
  55. if (match)
  56. s++;
  57. if (match != last)
  58. {
  59. last = match;
  60. if (!match)
  61. gaps++;
  62. }
  63. }
  64. /* penalty if short string does not completely fit in */
  65. for (; *s; s++)
  66. gaps++;
  67. return gaps;
  68. }
  69. int main (int argc, char **argv)
  70. {
  71. int res, i, j = 0, game_index;
  72. char *playbackname = NULL;
  73. int use_cyclone=0;
  74. int use_drz80=0;
  75. extern int video_scale;
  76. extern int video_border;
  77. extern int video_aspect;
  78. extern int throttle;
  79. extern int wiz_ram_tweaks;
  80. extern int wiz_rotated_video;
  81. memset(&options,0,sizeof(options));
  82. /* these two are not available in mame.cfg */
  83. errorlog = 0;
  84. game_index = -1;
  85. for (i = 1;i < argc;i++) /* V.V_121997 */
  86. {
  87. if (strcasecmp(argv[i],"-log") == 0)
  88. errorlog = fopen("error.log","wa");
  89. if (strcasecmp(argv[i],"-cyclone") == 0)
  90. use_cyclone=1;
  91. if (strcasecmp(argv[i],"-drz80") == 0)
  92. use_drz80=1;
  93. if (strcasecmp(argv[i],"-scale") == 0)
  94. video_scale=1;
  95. if (strcasecmp(argv[i],"-border") == 0)
  96. video_border=1;
  97. if (strcasecmp(argv[i],"-aspect") == 0)
  98. video_aspect=1;
  99. if (strcasecmp(argv[i],"-nothrottle") == 0)
  100. throttle=0;
  101. if (strcasecmp(argv[i],"-ramtweaks") == 0)
  102. wiz_ram_tweaks=1;
  103. if ((strcasecmp(argv[i],"-clock") == 0) && (i<argc-1))
  104. wiz_clock=atoi(argv[i+1]);
  105. if (strcasecmp(argv[i],"-wiz_rotated_video") == 0)
  106. wiz_rotated_video=1;
  107. if (strcasecmp(argv[i],"-playback") == 0)
  108. {
  109. i++;
  110. if (i < argc) /* point to inp file name */
  111. playbackname = argv[i];
  112. }
  113. }
  114. /* WIZ Initialization */
  115. wiz_init(8,22050,16,0);
  116. /* check for frontend options */
  117. res = frontend_help (argc, argv);
  118. /* if frontend options were used, return to DOS with the error code */
  119. if (res != 1234)
  120. {
  121. wiz_deinit();
  122. execl("mame.gpe", "mame.gpe", "cache", NULL);
  123. exit (res);
  124. }
  125. /* handle playback which is not available in mame.cfg */
  126. init_inpdir(); /* Init input directory for opening .inp for playback */
  127. if (playbackname != NULL)
  128. options.playback = osd_fopen(playbackname,0,OSD_FILETYPE_INPUTLOG,0);
  129. /* check for game name embedded in .inp header */
  130. if (options.playback)
  131. {
  132. INP_HEADER inp_header;
  133. /* read playback header */
  134. osd_fread(options.playback, &inp_header, sizeof(INP_HEADER));
  135. if (!isalnum(inp_header.name[0])) /* If first byte is not alpha-numeric */
  136. osd_fseek(options.playback, 0, SEEK_SET); /* old .inp file - no header */
  137. else
  138. {
  139. for (i = 0; (drivers[i] != 0); i++) /* find game and play it */
  140. {
  141. if (strcmp(drivers[i]->name, inp_header.name) == 0)
  142. {
  143. game_index = i;
  144. printf("Playing back previously recorded game %s (%s) [press return]\n",
  145. drivers[game_index]->name,drivers[game_index]->description);
  146. getchar();
  147. break;
  148. }
  149. }
  150. }
  151. }
  152. /* If not playing back a new .inp file */
  153. if (game_index == -1)
  154. {
  155. /* take the first commandline argument without "-" as the game name */
  156. for (j = 1; j < argc; j++)
  157. {
  158. if (argv[j][0] != '-') break;
  159. }
  160. {
  161. for (i = 0; drivers[i] && (game_index == -1); i++)
  162. {
  163. if (strcasecmp(argv[j],drivers[i]->name) == 0)
  164. {
  165. game_index = i;
  166. break;
  167. }
  168. }
  169. /* educated guess on what the user wants to play */
  170. if (game_index == -1)
  171. {
  172. int fuzz = 9999; /* best fuzz factor so far */
  173. for (i = 0; (drivers[i] != 0); i++)
  174. {
  175. int tmp;
  176. tmp = fuzzycmp(argv[j], drivers[i]->description);
  177. /* continue if the fuzz index is worse */
  178. if (tmp > fuzz)
  179. continue;
  180. /* on equal fuzz index, we prefer working, original games */
  181. if (tmp == fuzz)
  182. {
  183. /* game is a clone */
  184. if (drivers[i]->clone_of != 0
  185. && !(drivers[i]->clone_of->flags & NOT_A_DRIVER))
  186. {
  187. /* if the game we already found works, why bother. */
  188. /* and broken clones aren't very helpful either */
  189. if ((!drivers[game_index]->flags & GAME_NOT_WORKING) ||
  190. (drivers[i]->flags & GAME_NOT_WORKING))
  191. continue;
  192. }
  193. else continue;
  194. }
  195. /* we found a better match */
  196. game_index = i;
  197. fuzz = tmp;
  198. }
  199. if (game_index != -1)
  200. printf("fuzzy name compare, running %s\n",drivers[game_index]->name);
  201. }
  202. }
  203. if (game_index == -1)
  204. {
  205. printf("Game \"%s\" not supported\n", argv[j]);
  206. return 1;
  207. }
  208. }
  209. /* parse generic (os-independent) options */
  210. parse_cmdline (argc, argv, game_index);
  211. { /* Mish: I need sample rate initialised _before_ rom loading for optional rom regions */
  212. extern int soundcard;
  213. if (soundcard == 0) { /* silence, this would be -1 if unknown in which case all roms are loaded */
  214. Machine->sample_rate = 0; /* update the Machine structure to show that sound is disabled */
  215. options.samplerate=0;
  216. }
  217. }
  218. /* handle record which is not available in mame.cfg */
  219. for (i = 1; i < argc; i++)
  220. {
  221. if (strcasecmp(argv[i],"-record") == 0)
  222. {
  223. i++;
  224. if (i < argc)
  225. options.record = osd_fopen(argv[i],0,OSD_FILETYPE_INPUTLOG,1);
  226. }
  227. }
  228. if (options.record)
  229. {
  230. INP_HEADER inp_header;
  231. memset(&inp_header, '\0', sizeof(INP_HEADER));
  232. strcpy(inp_header.name, drivers[game_index]->name);
  233. /* MAME32 stores the MAME version numbers at bytes 9 - 11
  234. * MAME DOS keeps this information in a string, the
  235. * Windows code defines them in the Makefile.
  236. */
  237. /*
  238. inp_header.version[0] = 0;
  239. inp_header.version[1] = VERSION;
  240. inp_header.version[2] = BETA_VERSION;
  241. */
  242. osd_fwrite(options.record, &inp_header, sizeof(INP_HEADER));
  243. }
  244. /* Replace M68000 by CYCLONE */
  245. if (use_cyclone)
  246. {
  247. for (i=0;i<MAX_CPU;i++)
  248. {
  249. int *type=(int*)&(drivers[game_index]->drv->cpu[i].cpu_type);
  250. #ifdef NEOMAME
  251. if (((*type)&0xff)==CPU_M68000)
  252. #else
  253. if (((*type)&0xff)==CPU_M68000 || ((*type)&0xff)==CPU_M68010 )
  254. #endif
  255. {
  256. *type=((*type)&(~0xff))|CPU_CYCLONE;
  257. }
  258. }
  259. }
  260. /* Replace Z80 by DRZ80 */
  261. if (use_drz80)
  262. {
  263. for (i=0;i<MAX_CPU;i++)
  264. {
  265. int *type=(int*)&(drivers[game_index]->drv->cpu[i].cpu_type);
  266. if (((*type)&0xff)==CPU_Z80)
  267. {
  268. *type=((*type)&(~0xff))|CPU_DRZ80;
  269. }
  270. }
  271. }
  272. // Remove the mouse usage for certain games
  273. if ( (strcasecmp(drivers[game_index]->name,"hbarrel")==0) || (strcasecmp(drivers[game_index]->name,"hbarrelw")==0) ||
  274. (strcasecmp(drivers[game_index]->name,"midres")==0) || (strcasecmp(drivers[game_index]->name,"midresu")==0) ||
  275. (strcasecmp(drivers[game_index]->name,"midresj")==0) || (strcasecmp(drivers[game_index]->name,"tnk3")==0) ||
  276. (strcasecmp(drivers[game_index]->name,"tnk3j")==0) || (strcasecmp(drivers[game_index]->name,"ikari")==0) ||
  277. (strcasecmp(drivers[game_index]->name,"ikarijp")==0) || (strcasecmp(drivers[game_index]->name,"ikarijpb")==0) ||
  278. (strcasecmp(drivers[game_index]->name,"victroad")==0) || (strcasecmp(drivers[game_index]->name,"dogosoke")==0) ||
  279. (strcasecmp(drivers[game_index]->name,"gwar")==0) || (strcasecmp(drivers[game_index]->name,"gwarj")==0) ||
  280. (strcasecmp(drivers[game_index]->name,"gwara")==0) || (strcasecmp(drivers[game_index]->name,"gwarb")==0) ||
  281. (strcasecmp(drivers[game_index]->name,"bermudat")==0) || (strcasecmp(drivers[game_index]->name,"bermudaj")==0) ||
  282. (strcasecmp(drivers[game_index]->name,"bermudaa")==0) || (strcasecmp(drivers[game_index]->name,"mplanets")==0) ||
  283. (strcasecmp(drivers[game_index]->name,"forgottn")==0) || (strcasecmp(drivers[game_index]->name,"lostwrld")==0) ||
  284. (strcasecmp(drivers[game_index]->name,"gondo")==0) || (strcasecmp(drivers[game_index]->name,"makyosen")==0) ||
  285. (strcasecmp(drivers[game_index]->name,"topgunr")==0) || (strcasecmp(drivers[game_index]->name,"topgunbl")==0) ||
  286. (strcasecmp(drivers[game_index]->name,"tron")==0) || (strcasecmp(drivers[game_index]->name,"tron2")==0) ||
  287. (strcasecmp(drivers[game_index]->name,"kroozr")==0) ||(strcasecmp(drivers[game_index]->name,"crater")==0) ||
  288. (strcasecmp(drivers[game_index]->name,"dotron")==0) || (strcasecmp(drivers[game_index]->name,"dotrone")==0) ||
  289. (strcasecmp(drivers[game_index]->name,"zwackery")==0) || (strcasecmp(drivers[game_index]->name,"ikari3")==0) ||
  290. (strcasecmp(drivers[game_index]->name,"searchar")==0) || (strcasecmp(drivers[game_index]->name,"sercharu")==0) ||
  291. (strcasecmp(drivers[game_index]->name,"timesold")==0) || (strcasecmp(drivers[game_index]->name,"timesol1")==0) ||
  292. (strcasecmp(drivers[game_index]->name,"btlfield")==0) || (strcasecmp(drivers[game_index]->name,"aztarac")==0))
  293. {
  294. extern int use_mouse;
  295. use_mouse=0;
  296. }
  297. /* go for it */
  298. printf ("%s (%s)...\n",drivers[game_index]->description,drivers[game_index]->name);
  299. res = run_game (game_index);
  300. /* close open files */
  301. if (errorlog) fclose (errorlog);
  302. if (options.playback) osd_fclose (options.playback);
  303. if (options.record) osd_fclose (options.record);
  304. if (options.language_file) osd_fclose (options.language_file);
  305. if (res!=0)
  306. {
  307. /* wait a key press */
  308. wiz_video_flip_single();
  309. wiz_joystick_press(0);
  310. }
  311. wiz_deinit();
  312. execl("mame.gpe", "mame.gpe", "cache", NULL);
  313. exit (res);
  314. }
  315. void CLIB_DECL logerror(const char *text,...)
  316. {
  317. if (errorlog)
  318. {
  319. va_list arg;
  320. va_start(arg,text);
  321. vfprintf(errorlog,text,arg);
  322. va_end(arg);
  323. }
  324. }