media.c 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330
  1. /*
  2. * PicoDrive
  3. * (C) notaz, 2006-2010,2013
  4. *
  5. * This work is licensed under the terms of MAME license.
  6. * See COPYING file in the top-level directory.
  7. */
  8. #include <string.h>
  9. #include "pico_int.h"
  10. #include "cd/cd_parse.h"
  11. unsigned char media_id_header[0x100];
  12. static void strlwr_(char *string)
  13. {
  14. char *p;
  15. for (p = string; *p; p++)
  16. if ('A' <= *p && *p <= 'Z')
  17. *p += 'a' - 'A';
  18. }
  19. static void get_ext(const char *file, char *ext)
  20. {
  21. const char *p;
  22. p = file + strlen(file) - 4;
  23. if (p < file) p = file;
  24. strncpy(ext, p, 4);
  25. ext[4] = 0;
  26. strlwr_(ext);
  27. }
  28. static int detect_media(const char *fname)
  29. {
  30. static const short sms_offsets[] = { 0x7ff0, 0x3ff0, 0x1ff0 };
  31. static const char *sms_exts[] = { "sms", "gg", "sg" };
  32. static const char *md_exts[] = { "gen", "smd" };
  33. char buff0[512], buff[32];
  34. unsigned short *d16;
  35. pm_file *pmf;
  36. char ext[5];
  37. int i;
  38. get_ext(fname, ext);
  39. // detect wrong extensions
  40. if (!strcmp(ext, ".srm") || !strcmp(ext, "s.gz") || !strcmp(ext, ".mds")) // s.gz ~ .mds.gz
  41. return PM_BAD_DETECT;
  42. /* don't believe in extensions, except .cue */
  43. if (strcasecmp(ext, ".cue") == 0 || strcasecmp(ext, ".chd") == 0)
  44. return PM_CD;
  45. pmf = pm_open(fname);
  46. if (pmf == NULL)
  47. return PM_BAD_DETECT;
  48. if (pm_read(buff0, 512, pmf) != 512) {
  49. pm_close(pmf);
  50. return PM_BAD_DETECT;
  51. }
  52. if (strncasecmp("SEGADISCSYSTEM", buff0 + 0x00, 14) == 0 ||
  53. strncasecmp("SEGADISCSYSTEM", buff0 + 0x10, 14) == 0) {
  54. pm_close(pmf);
  55. return PM_CD;
  56. }
  57. /* check for SMD evil */
  58. if (pmf->size >= 0x4200 && (pmf->size & 0x3fff) == 0x200) {
  59. if (pm_seek(pmf, sms_offsets[0] + 0x200, SEEK_SET) == sms_offsets[0] + 0x200 &&
  60. pm_read(buff, 16, pmf) == 16 &&
  61. strncmp("TMR SEGA", buff, 8) == 0)
  62. goto looks_like_sms;
  63. /* could parse further but don't bother */
  64. goto extension_check;
  65. }
  66. /* MD header? Act as TMSS BIOS here */
  67. if (pm_seek(pmf, 0x100, SEEK_SET) == 0x100 && pm_read(buff, 16, pmf) == 16) {
  68. if (strncmp(buff, "SEGA", 4) == 0 || strncmp(buff, " SEG", 4) == 0)
  69. goto looks_like_md;
  70. }
  71. for (i = 0; i < ARRAY_SIZE(sms_offsets); i++) {
  72. if (pm_seek(pmf, sms_offsets[i], SEEK_SET) != sms_offsets[i])
  73. continue;
  74. if (pm_read(buff, 16, pmf) != 16)
  75. continue;
  76. if (strncmp("TMR SEGA", buff, 8) == 0)
  77. goto looks_like_sms;
  78. }
  79. extension_check:
  80. /* probably some headerless thing. Maybe check the extension after all. */
  81. for (i = 0; i < ARRAY_SIZE(md_exts); i++)
  82. if (strcasecmp(pmf->ext, md_exts[i]) == 0)
  83. goto looks_like_md;
  84. for (i = 0; i < ARRAY_SIZE(sms_exts); i++)
  85. if (strcasecmp(pmf->ext, sms_exts[i]) == 0)
  86. goto looks_like_sms;
  87. /* If everything else fails, make a guess on the reset vector */
  88. d16 = (unsigned short *)(buff0 + 4);
  89. if ((((d16[0] << 16) | d16[1]) & 0xffffff) >= pmf->size) {
  90. lprintf("bad MD reset vector, assuming SMS\n");
  91. goto looks_like_sms;
  92. }
  93. d16 = (unsigned short *)(buff0 + 0x1a0);
  94. if ((((d16[0] << 16) | d16[1]) & 0xffffff) != 0) {
  95. lprintf("bad MD rom start, assuming SMS\n");
  96. goto looks_like_sms;
  97. }
  98. looks_like_md:
  99. pm_close(pmf);
  100. return PM_MD_CART;
  101. looks_like_sms:
  102. pm_close(pmf);
  103. return PM_MARK3;
  104. }
  105. /* checks if fname points to valid MegaCD image */
  106. int PicoCdCheck(const char *fname_in, int *pregion)
  107. {
  108. const char *fname = fname_in;
  109. unsigned char buf[32];
  110. pm_file *cd_f;
  111. int region = 4; // 1: Japan, 4: US, 8: Europe
  112. char ext[5];
  113. enum cd_track_type type = CT_UNKNOWN;
  114. cd_data_t *cd_data = NULL;
  115. // opens a cue, or searches for one
  116. if (!cd_data && (cd_data = cue_parse(fname_in)) == NULL) {
  117. get_ext(fname_in, ext);
  118. if (strcasecmp(ext, ".cue") == 0)
  119. return -1;
  120. }
  121. // opens a chd
  122. if (!cd_data && (cd_data = chd_parse(fname_in)) == NULL) {
  123. get_ext(fname_in, ext);
  124. if (strcasecmp(ext, ".chd") == 0)
  125. return -1;
  126. }
  127. if (cd_data != NULL) {
  128. // 1st track contains the code
  129. fname = cd_data->tracks[1].fname;
  130. type = cd_data->tracks[1].type;
  131. }
  132. cd_f = pm_open(fname);
  133. cdparse_destroy(cd_data);
  134. if (cd_f == NULL) return CT_UNKNOWN; // let the upper level handle this
  135. if (pm_read(buf, 32, cd_f) != 32) {
  136. pm_close(cd_f);
  137. return -1;
  138. }
  139. if (!strncasecmp("SEGADISCSYSTEM", (char *)buf+0x00, 14)) {
  140. if (type && type != CT_ISO)
  141. elprintf(EL_STATUS, ".cue has wrong type: %i", type);
  142. type = CT_ISO; // Sega CD (ISO)
  143. }
  144. if (!strncasecmp("SEGADISCSYSTEM", (char *)buf+0x10, 14)) {
  145. if (type && type != CT_BIN)
  146. elprintf(EL_STATUS, ".cue has wrong type: %i", type);
  147. type = CT_BIN; // Sega CD (BIN)
  148. }
  149. if (type == CT_UNKNOWN) {
  150. pm_close(cd_f);
  151. return 0;
  152. }
  153. pm_seek(cd_f, (type == CT_ISO) ? 0x100 : 0x110, SEEK_SET);
  154. pm_read(media_id_header, sizeof(media_id_header), cd_f);
  155. /* it seems we have a CD image here. Try to detect region now.. */
  156. pm_seek(cd_f, (type == CT_ISO) ? 0x100+0x10B : 0x110+0x10B, SEEK_SET);
  157. pm_read(buf, 1, cd_f);
  158. pm_close(cd_f);
  159. if (buf[0] == 0x64) region = 8; // EU
  160. if (buf[0] == 0xa1) region = 1; // JAP
  161. lprintf("detected %s Sega/Mega CD image with %s region\n",
  162. type == CT_BIN ? "BIN" : "ISO", region != 4 ? (region == 8 ? "EU" : "JAP") : "USA");
  163. if (pregion != NULL)
  164. *pregion = region;
  165. return type;
  166. }
  167. enum media_type_e PicoLoadMedia(const char *filename,
  168. const char *carthw_cfg_fname,
  169. const char *(*get_bios_filename)(int *region, const char *cd_fname),
  170. void (*do_region_override)(const char *media_filename))
  171. {
  172. const char *rom_fname = filename;
  173. enum media_type_e media_type;
  174. enum cd_track_type cd_img_type = CT_UNKNOWN;
  175. unsigned char *rom_data = NULL;
  176. unsigned int rom_size = 0;
  177. pm_file *rom = NULL;
  178. int cd_region = 0;
  179. int ret;
  180. media_type = detect_media(filename);
  181. if (media_type == PM_BAD_DETECT)
  182. goto out;
  183. if ((PicoIn.AHW & PAHW_MCD) && Pico_mcd != NULL)
  184. cdd_unload();
  185. PicoCartUnload();
  186. PicoIn.AHW = 0;
  187. PicoIn.quirks = 0;
  188. if (media_type == PM_CD)
  189. {
  190. // check for MegaCD image
  191. cd_img_type = PicoCdCheck(filename, &cd_region);
  192. if ((int)cd_img_type >= 0 && cd_img_type != CT_UNKNOWN)
  193. {
  194. // valid CD image, ask frontend for BIOS..
  195. rom_fname = NULL;
  196. if (get_bios_filename != NULL)
  197. rom_fname = get_bios_filename(&cd_region, filename);
  198. if (rom_fname == NULL) {
  199. media_type = PM_BAD_CD_NO_BIOS;
  200. goto out;
  201. }
  202. PicoIn.AHW |= PAHW_MCD;
  203. }
  204. else {
  205. media_type = PM_BAD_CD;
  206. goto out;
  207. }
  208. }
  209. else if (media_type == PM_MARK3) {
  210. PicoIn.AHW = PAHW_SMS;
  211. }
  212. rom = pm_open(rom_fname);
  213. if (rom == NULL) {
  214. lprintf("Failed to open ROM\n");
  215. media_type = PM_ERROR;
  216. goto out;
  217. }
  218. ret = PicoCartLoad(rom, &rom_data, &rom_size, (PicoIn.AHW & PAHW_SMS) ? 1 : 0);
  219. pm_close(rom);
  220. if (ret != 0) {
  221. if (ret == 2) lprintf("Out of memory\n");
  222. else if (ret == 3) lprintf("Read failed\n");
  223. else lprintf("PicoCartLoad() failed.\n");
  224. media_type = PM_ERROR;
  225. goto out;
  226. }
  227. // detect wrong files
  228. if (strncmp((char *)rom_data, "Pico", 4) == 0) {
  229. lprintf("savestate selected?\n");
  230. media_type = PM_BAD_DETECT;
  231. goto out;
  232. }
  233. if (!(PicoIn.AHW & PAHW_SMS)) {
  234. unsigned short *d = (unsigned short *)(rom_data + 4);
  235. if ((((d[0] << 16) | d[1]) & 0xffffff) >= (int)rom_size) {
  236. lprintf("bad reset vector\n");
  237. media_type = PM_BAD_DETECT;
  238. goto out;
  239. }
  240. }
  241. // load config for this ROM (do this before insert to get correct region)
  242. if (!(PicoIn.AHW & PAHW_MCD)) {
  243. memcpy(media_id_header, rom_data + 0x100, sizeof(media_id_header));
  244. if (do_region_override != NULL)
  245. do_region_override(filename);
  246. }
  247. if (PicoCartInsert(rom_data, rom_size, carthw_cfg_fname)) {
  248. media_type = PM_ERROR;
  249. goto out;
  250. }
  251. rom_data = NULL; // now belongs to PicoCart
  252. // simple test for GG. Do this here since m.hardware is nulled in Insert
  253. if (PicoIn.AHW & PAHW_SMS) {
  254. if (strstr(filename,".gg")) {
  255. Pico.m.hardware |= 0x1;
  256. lprintf("detected GG ROM\n");
  257. } else
  258. lprintf("detected SMS ROM\n");
  259. }
  260. // insert CD if it was detected
  261. Pico.m.ncart_in = 0;
  262. if (cd_img_type != CT_UNKNOWN) {
  263. ret = cdd_load(filename, cd_img_type);
  264. if (ret != 0) {
  265. PicoCartUnload();
  266. media_type = PM_BAD_CD;
  267. goto out;
  268. }
  269. Pico.m.ncart_in = 1;
  270. }
  271. if (PicoIn.quirks & PQUIRK_FORCE_6BTN)
  272. PicoSetInputDevice(0, PICO_INPUT_PAD_6BTN);
  273. out:
  274. if (rom_data)
  275. free(rom_data);
  276. return media_type;
  277. }
  278. // vim:shiftwidth=2:ts=2:expandtab