cart.c 33 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339
  1. /*
  2. * PicoDrive
  3. * (c) Copyright Dave, 2004
  4. * (C) notaz, 2006-2010
  5. *
  6. * This work is licensed under the terms of MAME license.
  7. * See COPYING file in the top-level directory.
  8. */
  9. #include "pico_int.h"
  10. #include <cpu/debug.h>
  11. #ifdef USE_LIBRETRO_VFS
  12. #include "file_stream_transforms.h"
  13. #endif
  14. #if defined(USE_LIBCHDR)
  15. #include "libchdr/chd.h"
  16. #include "libchdr/cdrom.h"
  17. #endif
  18. #include <unzip/unzip.h>
  19. #include <zlib.h>
  20. static int rom_alloc_size;
  21. static const char *rom_exts[] = { "bin", "gen", "smd", "iso", "sms", "gg", "sg" };
  22. void (*PicoCartUnloadHook)(void);
  23. void (*PicoCartMemSetup)(void);
  24. void (*PicoCartLoadProgressCB)(int percent) = NULL;
  25. void (*PicoCDLoadProgressCB)(const char *fname, int percent) = NULL; // handled in Pico/cd/cd_file.c
  26. int PicoGameLoaded;
  27. static void PicoCartDetect(const char *carthw_cfg);
  28. static void PicoCartDetectMS(void);
  29. /* cso struct */
  30. typedef struct _cso_struct
  31. {
  32. unsigned char in_buff[2*2048];
  33. unsigned char out_buff[2048];
  34. struct {
  35. char magic[4];
  36. unsigned int unused;
  37. unsigned int total_bytes;
  38. unsigned int total_bytes_high; // ignored here
  39. unsigned int block_size; // 10h
  40. unsigned char ver;
  41. unsigned char align;
  42. unsigned char reserved[2];
  43. } header;
  44. unsigned int fpos_in; // input file read pointer
  45. unsigned int fpos_out; // pos in virtual decompressed file
  46. int block_in_buff; // block which we have read in in_buff
  47. int pad;
  48. int index[0];
  49. }
  50. cso_struct;
  51. static int uncompress_buf(void *dest, int destLen, void *source, int sourceLen)
  52. {
  53. z_stream stream;
  54. int err;
  55. stream.next_in = (Bytef*)source;
  56. stream.avail_in = (uInt)sourceLen;
  57. stream.next_out = dest;
  58. stream.avail_out = (uInt)destLen;
  59. stream.zalloc = NULL;
  60. stream.zfree = NULL;
  61. err = inflateInit2(&stream, -15);
  62. if (err != Z_OK) return err;
  63. err = inflate(&stream, Z_FINISH);
  64. if (err != Z_STREAM_END) {
  65. inflateEnd(&stream);
  66. return err;
  67. }
  68. //*destLen = stream.total_out;
  69. return inflateEnd(&stream);
  70. }
  71. static const char *get_ext(const char *path)
  72. {
  73. const char *ext;
  74. if (strlen(path) < 4)
  75. return ""; // no ext
  76. // allow 2 or 3 char extensions for now
  77. ext = path + strlen(path) - 2;
  78. if (ext[-1] != '.') ext--;
  79. if (ext[-1] != '.')
  80. return "";
  81. return ext;
  82. }
  83. struct zip_file {
  84. pm_file file;
  85. ZIP *zip;
  86. struct zipent *entry;
  87. z_stream stream;
  88. unsigned char inbuf[16384];
  89. long start;
  90. unsigned int pos;
  91. };
  92. #if defined(USE_LIBCHDR)
  93. struct chd_struct {
  94. pm_file file;
  95. int fpos;
  96. int sectorsize;
  97. chd_file *chd;
  98. int unitbytes;
  99. int hunkunits;
  100. u8 *hunk;
  101. int hunknum;
  102. };
  103. #endif
  104. pm_file *pm_open(const char *path)
  105. {
  106. pm_file *file = NULL;
  107. const char *ext;
  108. FILE *f;
  109. if (path == NULL)
  110. return NULL;
  111. ext = get_ext(path);
  112. if (strcasecmp(ext, "zip") == 0)
  113. {
  114. struct zip_file *zfile = NULL;
  115. struct zipent *zipentry;
  116. ZIP *zipfile;
  117. int i, ret;
  118. zipfile = openzip(path);
  119. if (zipfile != NULL)
  120. {
  121. /* search for suitable file (right extension or large enough file) */
  122. while ((zipentry = readzip(zipfile)) != NULL)
  123. {
  124. ext = get_ext(zipentry->name);
  125. if (zipentry->uncompressed_size >= 32*1024)
  126. goto found_rom_zip;
  127. for (i = 0; i < sizeof(rom_exts)/sizeof(rom_exts[0]); i++)
  128. if (strcasecmp(ext, rom_exts[i]) == 0)
  129. goto found_rom_zip;
  130. }
  131. /* zipfile given, but nothing found suitable for us inside */
  132. goto zip_failed;
  133. found_rom_zip:
  134. zfile = calloc(1, sizeof(*zfile));
  135. if (zfile == NULL)
  136. goto zip_failed;
  137. ret = seekcompresszip(zipfile, zipentry);
  138. if (ret != 0)
  139. goto zip_failed;
  140. ret = inflateInit2(&zfile->stream, -15);
  141. if (ret != Z_OK) {
  142. elprintf(EL_STATUS, "zip: inflateInit2 %d", ret);
  143. goto zip_failed;
  144. }
  145. zfile->zip = zipfile;
  146. zfile->entry = zipentry;
  147. zfile->start = ftell(zipfile->fp);
  148. zfile->file.file = zfile;
  149. zfile->file.size = zipentry->uncompressed_size;
  150. zfile->file.type = PMT_ZIP;
  151. strncpy(zfile->file.ext, ext, sizeof(zfile->file.ext) - 1);
  152. return &zfile->file;
  153. zip_failed:
  154. closezip(zipfile);
  155. free(zfile);
  156. return NULL;
  157. }
  158. }
  159. else if (strcasecmp(ext, "cso") == 0)
  160. {
  161. cso_struct *cso = NULL, *tmp = NULL;
  162. int i, size;
  163. f = fopen(path, "rb");
  164. if (f == NULL)
  165. goto cso_failed;
  166. #ifdef __GP2X__
  167. /* we use our own buffering */
  168. setvbuf(f, NULL, _IONBF, 0);
  169. #endif
  170. cso = malloc(sizeof(*cso));
  171. if (cso == NULL)
  172. goto cso_failed;
  173. if (fread(&cso->header, 1, sizeof(cso->header), f) != sizeof(cso->header))
  174. goto cso_failed;
  175. cso->header.block_size = CPU_LE4(cso->header.block_size);
  176. cso->header.total_bytes = CPU_LE4(cso->header.total_bytes);
  177. cso->header.total_bytes_high = CPU_LE4(cso->header.total_bytes_high);
  178. if (strncmp(cso->header.magic, "CISO", 4) != 0) {
  179. elprintf(EL_STATUS, "cso: bad header");
  180. goto cso_failed;
  181. }
  182. if (cso->header.block_size != 2048) {
  183. elprintf(EL_STATUS, "cso: bad block size (%u)", cso->header.block_size);
  184. goto cso_failed;
  185. }
  186. size = ((cso->header.total_bytes >> 11) + 1)*4 + sizeof(*cso);
  187. tmp = realloc(cso, size);
  188. if (tmp == NULL)
  189. goto cso_failed;
  190. cso = tmp;
  191. elprintf(EL_STATUS, "allocated %i bytes for CSO struct", size);
  192. size -= sizeof(*cso); // index size
  193. if (fread(cso->index, 1, size, f) != size) {
  194. elprintf(EL_STATUS, "cso: premature EOF");
  195. goto cso_failed;
  196. }
  197. for (i = 0; i < size/4; i++)
  198. cso->index[i] = CPU_LE4(cso->index[i]);
  199. // all ok
  200. cso->fpos_in = ftell(f);
  201. cso->fpos_out = 0;
  202. cso->block_in_buff = -1;
  203. file = calloc(1, sizeof(*file));
  204. if (file == NULL) goto cso_failed;
  205. file->file = f;
  206. file->param = cso;
  207. file->size = cso->header.total_bytes;
  208. file->type = PMT_CSO;
  209. return file;
  210. cso_failed:
  211. if (cso != NULL) free(cso);
  212. if (f != NULL) fclose(f);
  213. return NULL;
  214. }
  215. #if defined(USE_LIBCHDR)
  216. else if (strcasecmp(ext, "chd") == 0)
  217. {
  218. struct chd_struct *chd = NULL;
  219. chd_file *cf = NULL;
  220. const chd_header *head;
  221. if (chd_open(path, CHD_OPEN_READ, NULL, &cf) != CHDERR_NONE)
  222. goto chd_failed;
  223. // sanity check
  224. head = chd_get_header(cf);
  225. if ((head->hunkbytes == 0) || (head->hunkbytes % CD_FRAME_SIZE))
  226. goto chd_failed;
  227. chd = calloc(1, sizeof(*chd));
  228. if (chd == NULL)
  229. goto chd_failed;
  230. chd->hunk = (u8 *)malloc(head->hunkbytes);
  231. if (!chd->hunk)
  232. goto chd_failed;
  233. chd->chd = cf;
  234. chd->unitbytes = head->unitbytes;
  235. chd->hunkunits = head->hunkbytes / head->unitbytes;
  236. chd->sectorsize = CD_MAX_SECTOR_DATA; // default to RAW mode
  237. chd->fpos = 0;
  238. chd->hunknum = -1;
  239. chd->file.file = chd;
  240. chd->file.type = PMT_CHD;
  241. // subchannel data is skipped, remove it from total size
  242. chd->file.size = head->logicalbytes / CD_FRAME_SIZE * CD_MAX_SECTOR_DATA;
  243. strncpy(chd->file.ext, ext, sizeof(chd->file.ext) - 1);
  244. return &chd->file;
  245. chd_failed:
  246. /* invalid CHD file */
  247. if (chd != NULL) free(chd);
  248. if (cf != NULL) chd_close(cf);
  249. return NULL;
  250. }
  251. #endif
  252. /* not a zip, treat as uncompressed file */
  253. f = fopen(path, "rb");
  254. if (f == NULL) return NULL;
  255. file = calloc(1, sizeof(*file));
  256. if (file == NULL) {
  257. fclose(f);
  258. return NULL;
  259. }
  260. fseek(f, 0, SEEK_END);
  261. file->file = f;
  262. file->param = NULL;
  263. file->size = ftell(f);
  264. file->type = PMT_UNCOMPRESSED;
  265. strncpy(file->ext, ext, sizeof(file->ext) - 1);
  266. fseek(f, 0, SEEK_SET);
  267. #ifdef __GP2X__
  268. if (file->size > 0x400000)
  269. /* we use our own buffering */
  270. setvbuf(f, NULL, _IONBF, 0);
  271. #endif
  272. return file;
  273. }
  274. void pm_sectorsize(int length, pm_file *stream)
  275. {
  276. // CHD reading needs to know how much binary data is in one data sector(=unit)
  277. #if defined(USE_LIBCHDR)
  278. if (stream->type == PMT_CHD) {
  279. struct chd_struct *chd = stream->file;
  280. chd->sectorsize = length;
  281. if (chd->sectorsize > chd->unitbytes)
  282. elprintf(EL_STATUS|EL_ANOMALY, "cd: sector size %d too large for unit %d", chd->sectorsize, chd->unitbytes);
  283. }
  284. #endif
  285. }
  286. #if defined(USE_LIBCHDR)
  287. static size_t _pm_read_chd(void *ptr, size_t bytes, pm_file *stream, int is_audio)
  288. {
  289. int ret = 0;
  290. if (stream->type == PMT_CHD) {
  291. struct chd_struct *chd = stream->file;
  292. // calculate sector and offset in sector
  293. int sectsz = is_audio ? CD_MAX_SECTOR_DATA : chd->sectorsize;
  294. int sector = chd->fpos / sectsz;
  295. int offset = chd->fpos - (sector * sectsz);
  296. // calculate hunk and sector offset in hunk
  297. int hunknum = sector / chd->hunkunits;
  298. int hunksec = sector - (hunknum * chd->hunkunits);
  299. int hunkofs = hunksec * chd->unitbytes;
  300. while (bytes != 0) {
  301. // data left in current sector
  302. int len = sectsz - offset;
  303. // update hunk cache if needed
  304. if (hunknum != chd->hunknum) {
  305. chd_read(chd->chd, hunknum, chd->hunk);
  306. chd->hunknum = hunknum;
  307. }
  308. if (len > bytes)
  309. len = bytes;
  310. #if CPU_IS_LE
  311. if (is_audio) {
  312. // convert big endian audio samples
  313. u16 *dst = ptr, v;
  314. u8 *src = chd->hunk + hunkofs + offset;
  315. int i;
  316. for (i = 0; i < len; i += 4) {
  317. v = *src++ << 8; *dst++ = v | *src++;
  318. v = *src++ << 8; *dst++ = v | *src++;
  319. }
  320. } else
  321. #endif
  322. memcpy(ptr, chd->hunk + hunkofs + offset, len);
  323. // house keeping
  324. ret += len;
  325. chd->fpos += len;
  326. bytes -= len;
  327. // no need to advance internals if there's no more data to read
  328. if (bytes) {
  329. ptr += len;
  330. offset = 0;
  331. sector ++;
  332. hunksec ++;
  333. hunkofs += chd->unitbytes;
  334. if (hunksec >= chd->hunkunits) {
  335. hunksec = 0;
  336. hunkofs = 0;
  337. hunknum ++;
  338. }
  339. }
  340. }
  341. }
  342. return ret;
  343. }
  344. #endif
  345. size_t pm_read(void *ptr, size_t bytes, pm_file *stream)
  346. {
  347. int ret;
  348. if (stream->type == PMT_UNCOMPRESSED)
  349. {
  350. ret = fread(ptr, 1, bytes, stream->file);
  351. }
  352. else if (stream->type == PMT_ZIP)
  353. {
  354. struct zip_file *z = stream->file;
  355. if (z->entry->compression_method == 0) {
  356. int ret = fread(ptr, 1, bytes, z->zip->fp);
  357. z->pos += ret;
  358. return ret;
  359. }
  360. z->stream.next_out = ptr;
  361. z->stream.avail_out = bytes;
  362. while (z->stream.avail_out != 0) {
  363. if (z->stream.avail_in == 0) {
  364. z->stream.avail_in = fread(z->inbuf, 1, sizeof(z->inbuf), z->zip->fp);
  365. if (z->stream.avail_in == 0)
  366. break;
  367. z->stream.next_in = z->inbuf;
  368. }
  369. ret = inflate(&z->stream, Z_NO_FLUSH);
  370. if (ret == Z_STREAM_END)
  371. break;
  372. if (ret != Z_OK) {
  373. elprintf(EL_STATUS, "zip: inflate: %d", ret);
  374. return 0;
  375. }
  376. }
  377. z->pos += bytes - z->stream.avail_out;
  378. return bytes - z->stream.avail_out;
  379. }
  380. else if (stream->type == PMT_CSO)
  381. {
  382. cso_struct *cso = stream->param;
  383. int read_pos, read_len, out_offs, rret;
  384. int block = cso->fpos_out >> 11;
  385. int index = cso->index[block];
  386. int index_end = cso->index[block+1];
  387. unsigned char *out = ptr, *tmp_dst;
  388. ret = 0;
  389. while (bytes != 0)
  390. {
  391. out_offs = cso->fpos_out&0x7ff;
  392. if (out_offs == 0 && bytes >= 2048)
  393. tmp_dst = out;
  394. else tmp_dst = cso->out_buff;
  395. read_pos = (index&0x7fffffff) << cso->header.align;
  396. if (index < 0) {
  397. if (read_pos != cso->fpos_in)
  398. fseek(stream->file, read_pos, SEEK_SET);
  399. rret = fread(tmp_dst, 1, 2048, stream->file);
  400. cso->fpos_in = read_pos + rret;
  401. if (rret != 2048) break;
  402. } else {
  403. read_len = (((index_end&0x7fffffff) << cso->header.align) - read_pos) & 0xfff;
  404. if (block != cso->block_in_buff)
  405. {
  406. if (read_pos != cso->fpos_in)
  407. fseek(stream->file, read_pos, SEEK_SET);
  408. rret = fread(cso->in_buff, 1, read_len, stream->file);
  409. cso->fpos_in = read_pos + rret;
  410. if (rret != read_len) {
  411. elprintf(EL_STATUS, "cso: read failed @ %08x", read_pos);
  412. break;
  413. }
  414. cso->block_in_buff = block;
  415. }
  416. rret = uncompress_buf(tmp_dst, 2048, cso->in_buff, read_len);
  417. if (rret != 0) {
  418. elprintf(EL_STATUS, "cso: uncompress failed @ %08x with %i", read_pos, rret);
  419. break;
  420. }
  421. }
  422. rret = 2048;
  423. if (out_offs != 0 || bytes < 2048) {
  424. //elprintf(EL_STATUS, "cso: unaligned/nonfull @ %08x, offs=%i, len=%u", cso->fpos_out, out_offs, bytes);
  425. if (bytes < rret) rret = bytes;
  426. if (2048 - out_offs < rret) rret = 2048 - out_offs;
  427. memcpy(out, tmp_dst + out_offs, rret);
  428. }
  429. ret += rret;
  430. out += rret;
  431. cso->fpos_out += rret;
  432. bytes -= rret;
  433. block++;
  434. index = index_end;
  435. index_end = cso->index[block+1];
  436. }
  437. }
  438. #if defined(USE_LIBCHDR)
  439. else if (stream->type == PMT_CHD)
  440. {
  441. ret = _pm_read_chd(ptr, bytes, stream, 0);
  442. }
  443. #endif
  444. else
  445. ret = 0;
  446. return ret;
  447. }
  448. size_t pm_read_audio(void *ptr, size_t bytes, pm_file *stream)
  449. {
  450. #if !(CPU_IS_LE)
  451. if (stream->type == PMT_UNCOMPRESSED)
  452. {
  453. // convert little endian audio samples from WAV file
  454. int ret = pm_read(ptr, bytes, stream);
  455. u16 *dst = ptr, v;
  456. u8 *src = ptr;
  457. int i;
  458. for (i = 0; i < ret; i += 4) {
  459. v = *src++; *dst++ = v | (*src++ << 8);
  460. v = *src++; *dst++ = v | (*src++ << 8);
  461. }
  462. return ret;
  463. }
  464. else
  465. #endif
  466. #if defined(USE_LIBCHDR)
  467. if (stream->type == PMT_CHD)
  468. {
  469. return _pm_read_chd(ptr, bytes, stream, 1);
  470. }
  471. #endif
  472. return pm_read(ptr, bytes, stream);
  473. }
  474. int pm_seek(pm_file *stream, long offset, int whence)
  475. {
  476. if (stream->type == PMT_UNCOMPRESSED)
  477. {
  478. fseek(stream->file, offset, whence);
  479. return ftell(stream->file);
  480. }
  481. else if (stream->type == PMT_ZIP)
  482. {
  483. struct zip_file *z = stream->file;
  484. unsigned int pos = z->pos;
  485. int ret;
  486. switch (whence)
  487. {
  488. case SEEK_CUR: pos += offset; break;
  489. case SEEK_SET: pos = offset; break;
  490. case SEEK_END: pos = stream->size - offset; break;
  491. }
  492. if (z->entry->compression_method == 0) {
  493. ret = fseek(z->zip->fp, z->start + pos, SEEK_SET);
  494. if (ret == 0)
  495. return (z->pos = pos);
  496. return -1;
  497. }
  498. offset = pos - z->pos;
  499. if (pos < z->pos) {
  500. // full decompress from the start
  501. fseek(z->zip->fp, z->start, SEEK_SET);
  502. z->stream.avail_in = 0;
  503. z->stream.next_in = z->inbuf;
  504. inflateReset(&z->stream);
  505. z->pos = 0;
  506. offset = pos;
  507. }
  508. if (PicoIn.osdMessage != NULL && offset > 4 * 1024 * 1024)
  509. PicoIn.osdMessage("Decompressing data...");
  510. while (offset > 0) {
  511. char buf[16 * 1024];
  512. size_t l = offset > sizeof(buf) ? sizeof(buf) : offset;
  513. ret = pm_read(buf, l, stream);
  514. if (ret != l)
  515. break;
  516. offset -= l;
  517. }
  518. return z->pos;
  519. }
  520. else if (stream->type == PMT_CSO)
  521. {
  522. cso_struct *cso = stream->param;
  523. switch (whence)
  524. {
  525. case SEEK_CUR: cso->fpos_out += offset; break;
  526. case SEEK_SET: cso->fpos_out = offset; break;
  527. case SEEK_END: cso->fpos_out = cso->header.total_bytes - offset; break;
  528. }
  529. return cso->fpos_out;
  530. }
  531. #if defined(USE_LIBCHDR)
  532. else if (stream->type == PMT_CHD)
  533. {
  534. struct chd_struct *chd = stream->file;
  535. switch (whence)
  536. {
  537. case SEEK_CUR: chd->fpos += offset; break;
  538. case SEEK_SET: chd->fpos = offset; break;
  539. case SEEK_END: chd->fpos = stream->size - offset; break;
  540. }
  541. return chd->fpos;
  542. }
  543. #endif
  544. else
  545. return -1;
  546. }
  547. int pm_close(pm_file *fp)
  548. {
  549. int ret = 0;
  550. if (fp == NULL) return EOF;
  551. if (fp->type == PMT_UNCOMPRESSED)
  552. {
  553. fclose(fp->file);
  554. }
  555. else if (fp->type == PMT_ZIP)
  556. {
  557. struct zip_file *z = fp->file;
  558. inflateEnd(&z->stream);
  559. closezip(z->zip);
  560. }
  561. else if (fp->type == PMT_CSO)
  562. {
  563. free(fp->param);
  564. fclose(fp->file);
  565. }
  566. #if defined(USE_LIBCHDR)
  567. else if (fp->type == PMT_CHD)
  568. {
  569. struct chd_struct *chd = fp->file;
  570. chd_close(chd->chd);
  571. if (chd->hunk)
  572. free(chd->hunk);
  573. }
  574. #endif
  575. else
  576. ret = EOF;
  577. free(fp);
  578. return ret;
  579. }
  580. // byteswap, data needs to be int aligned, src can match dst
  581. void Byteswap(void *dst, const void *src, int len)
  582. {
  583. #if CPU_IS_LE
  584. const unsigned int *ps = src;
  585. unsigned int *pd = dst;
  586. int i, m;
  587. if (len < 2)
  588. return;
  589. m = 0x00ff00ff;
  590. for (i = 0; i < len / 4; i++) {
  591. unsigned int t = ps[i];
  592. pd[i] = ((t & m) << 8) | ((t & ~m) >> 8);
  593. }
  594. #endif
  595. }
  596. // Interleve a 16k block and byteswap
  597. static int InterleveBlock(unsigned char *dest,unsigned char *src)
  598. {
  599. int i=0;
  600. for (i=0;i<0x2000;i++) dest[(i<<1)+MEM_BE2(1)]=src[ i]; // Odd
  601. for (i=0;i<0x2000;i++) dest[(i<<1)+MEM_BE2(0)]=src[0x2000+i]; // Even
  602. return 0;
  603. }
  604. // Decode a SMD file
  605. static int DecodeSmd(unsigned char *data,int len)
  606. {
  607. unsigned char *temp=NULL;
  608. int i=0;
  609. temp=(unsigned char *)malloc(0x4000);
  610. if (temp==NULL) return 1;
  611. memset(temp,0,0x4000);
  612. // Interleve each 16k block and shift down by 0x200:
  613. for (i=0; i+0x4200<=len; i+=0x4000)
  614. {
  615. InterleveBlock(temp,data+0x200+i); // Interleve 16k to temporary buffer
  616. memcpy(data+i,temp,0x4000); // Copy back in
  617. }
  618. free(temp);
  619. return 0;
  620. }
  621. static unsigned char *PicoCartAlloc(int filesize, int is_sms)
  622. {
  623. unsigned char *rom;
  624. if (is_sms) {
  625. // make size power of 2 for easier banking handling
  626. int s = 0, tmp = filesize;
  627. while ((tmp >>= 1) != 0)
  628. s++;
  629. if (filesize > (1 << s))
  630. s++;
  631. rom_alloc_size = 1 << s;
  632. // be sure we can cover all address space
  633. if (rom_alloc_size < 0x10000)
  634. rom_alloc_size = 0x10000;
  635. }
  636. else {
  637. // make alloc size at least sizeof(mcd_state),
  638. // in case we want to switch to CD mode
  639. if (filesize < sizeof(mcd_state))
  640. filesize = sizeof(mcd_state);
  641. // align to 512K for memhandlers
  642. rom_alloc_size = (filesize + 0x7ffff) & ~0x7ffff;
  643. }
  644. if (rom_alloc_size - filesize < 4)
  645. rom_alloc_size += 4; // padding for out-of-bound exec protection
  646. // Allocate space for the rom plus padding
  647. // use special address for 32x dynarec
  648. rom = plat_mmap(0x02000000, rom_alloc_size, 0, 0);
  649. return rom;
  650. }
  651. int PicoCartLoad(pm_file *f,unsigned char **prom,unsigned int *psize,int is_sms)
  652. {
  653. unsigned char *rom;
  654. int size, bytes_read;
  655. if (f == NULL)
  656. return 1;
  657. size = f->size;
  658. if (size <= 0) return 1;
  659. size = (size+3)&~3; // Round up to a multiple of 4
  660. // Allocate space for the rom plus padding
  661. rom = PicoCartAlloc(size, is_sms);
  662. if (rom == NULL) {
  663. elprintf(EL_STATUS, "out of memory (wanted %i)", size);
  664. return 2;
  665. }
  666. if (PicoCartLoadProgressCB != NULL)
  667. {
  668. // read ROM in blocks, just for fun
  669. int ret;
  670. unsigned char *p = rom;
  671. bytes_read=0;
  672. do
  673. {
  674. int todo = size - bytes_read;
  675. if (todo > 256*1024) todo = 256*1024;
  676. ret = pm_read(p,todo,f);
  677. bytes_read += ret;
  678. p += ret;
  679. PicoCartLoadProgressCB(bytes_read * 100LL / size);
  680. }
  681. while (ret > 0);
  682. }
  683. else
  684. bytes_read = pm_read(rom,size,f); // Load up the rom
  685. if (bytes_read <= 0) {
  686. elprintf(EL_STATUS, "read failed");
  687. plat_munmap(rom, rom_alloc_size);
  688. return 3;
  689. }
  690. if (!is_sms)
  691. {
  692. // maybe we are loading MegaCD BIOS?
  693. if (!(PicoIn.AHW & PAHW_MCD) && size == 0x20000 && (!strncmp((char *)rom+0x124, "BOOT", 4) ||
  694. !strncmp((char *)rom+0x128, "BOOT", 4))) {
  695. PicoIn.AHW |= PAHW_MCD;
  696. }
  697. // Check for SMD:
  698. if (size >= 0x4200 && (size&0x3fff) == 0x200 &&
  699. ((rom[0x2280] == 'S' && rom[0x280] == 'E') || (rom[0x280] == 'S' && rom[0x2281] == 'E'))) {
  700. elprintf(EL_STATUS, "SMD format detected.");
  701. DecodeSmd(rom,size); size-=0x200; // Decode and byteswap SMD
  702. }
  703. else Byteswap(rom, rom, size); // Just byteswap
  704. }
  705. else
  706. {
  707. if (size >= 0x4200 && (size&0x3fff) == 0x200) {
  708. elprintf(EL_STATUS, "SMD format detected.");
  709. // at least here it's not interleaved
  710. size -= 0x200;
  711. memmove(rom, rom + 0x200, size);
  712. }
  713. }
  714. if (prom) *prom = rom;
  715. if (psize) *psize = size;
  716. return 0;
  717. }
  718. // Insert a cartridge:
  719. int PicoCartInsert(unsigned char *rom, unsigned int romsize, const char *carthw_cfg)
  720. {
  721. // notaz: add a 68k "jump one op back" opcode to the end of ROM.
  722. // This will hang the emu, but will prevent nasty crashes.
  723. // note: 4 bytes are padded to every ROM
  724. if (rom != NULL)
  725. *(u32 *)(rom+romsize) = CPU_BE2(0x4EFAFFFE);
  726. Pico.rom=rom;
  727. Pico.romsize=romsize;
  728. if (Pico.sv.data) {
  729. free(Pico.sv.data);
  730. Pico.sv.data = NULL;
  731. }
  732. if (PicoCartUnloadHook != NULL) {
  733. PicoCartUnloadHook();
  734. PicoCartUnloadHook = NULL;
  735. }
  736. pdb_cleanup();
  737. PicoIn.AHW &= PAHW_MCD|PAHW_SMS;
  738. PicoCartMemSetup = NULL;
  739. PicoDmaHook = NULL;
  740. PicoResetHook = NULL;
  741. PicoLineHook = NULL;
  742. PicoLoadStateHook = NULL;
  743. carthw_chunks = NULL;
  744. if (!(PicoIn.AHW & (PAHW_MCD|PAHW_SMS)))
  745. PicoCartDetect(carthw_cfg);
  746. else if (PicoIn.AHW & PAHW_SMS)
  747. PicoCartDetectMS();
  748. // setup correct memory map for loaded ROM
  749. switch (PicoIn.AHW) {
  750. default:
  751. elprintf(EL_STATUS|EL_ANOMALY, "starting in unknown hw configuration: %x", PicoIn.AHW);
  752. case 0:
  753. case PAHW_SVP: PicoMemSetup(); break;
  754. case PAHW_MCD: PicoMemSetupCD(); break;
  755. case PAHW_PICO: PicoMemSetupPico(); break;
  756. case PAHW_SMS: PicoMemSetupMS(); break;
  757. }
  758. if (PicoCartMemSetup != NULL)
  759. PicoCartMemSetup();
  760. if (PicoIn.AHW & PAHW_SMS)
  761. PicoPowerMS();
  762. else
  763. PicoPower();
  764. PicoGameLoaded = 1;
  765. return 0;
  766. }
  767. int PicoCartResize(int newsize)
  768. {
  769. void *tmp = plat_mremap(Pico.rom, rom_alloc_size, newsize);
  770. if (tmp == NULL)
  771. return -1;
  772. Pico.rom = tmp;
  773. rom_alloc_size = newsize;
  774. return 0;
  775. }
  776. void PicoCartUnload(void)
  777. {
  778. if (PicoCartUnloadHook != NULL) {
  779. PicoCartUnloadHook();
  780. PicoCartUnloadHook = NULL;
  781. }
  782. if (PicoIn.AHW & PAHW_32X)
  783. PicoUnload32x();
  784. if (Pico.rom != NULL) {
  785. SekFinishIdleDet();
  786. plat_munmap(Pico.rom, rom_alloc_size);
  787. Pico.rom = NULL;
  788. }
  789. PicoGameLoaded = 0;
  790. }
  791. static unsigned int rom_crc32(void)
  792. {
  793. unsigned int crc;
  794. elprintf(EL_STATUS, "caclulating CRC32..");
  795. // have to unbyteswap for calculation..
  796. Byteswap(Pico.rom, Pico.rom, Pico.romsize);
  797. crc = crc32(0, Pico.rom, Pico.romsize);
  798. Byteswap(Pico.rom, Pico.rom, Pico.romsize);
  799. return crc;
  800. }
  801. static int rom_strcmp(int rom_offset, const char *s1)
  802. {
  803. int i, len = strlen(s1);
  804. const char *s_rom = (const char *)Pico.rom;
  805. if (rom_offset + len > Pico.romsize)
  806. return 0;
  807. for (i = 0; i < len; i++)
  808. if (s1[i] != s_rom[MEM_BE2(i + rom_offset)])
  809. return 1;
  810. return 0;
  811. }
  812. static unsigned int rom_read32(int addr)
  813. {
  814. unsigned short *m = (unsigned short *)(Pico.rom + addr);
  815. return (m[0] << 16) | m[1];
  816. }
  817. static char *sskip(char *s)
  818. {
  819. while (*s && isspace_(*s))
  820. s++;
  821. return s;
  822. }
  823. static void rstrip(char *s)
  824. {
  825. char *p;
  826. for (p = s + strlen(s) - 1; p >= s; p--)
  827. if (isspace_(*p))
  828. *p = 0;
  829. }
  830. static int parse_3_vals(char *p, int *val0, int *val1, int *val2)
  831. {
  832. char *r;
  833. *val0 = strtoul(p, &r, 0);
  834. if (r == p)
  835. goto bad;
  836. p = sskip(r);
  837. if (*p++ != ',')
  838. goto bad;
  839. *val1 = strtoul(p, &r, 0);
  840. if (r == p)
  841. goto bad;
  842. p = sskip(r);
  843. if (*p++ != ',')
  844. goto bad;
  845. *val2 = strtoul(p, &r, 0);
  846. if (r == p)
  847. goto bad;
  848. return 1;
  849. bad:
  850. return 0;
  851. }
  852. static int is_expr(const char *expr, char **pr)
  853. {
  854. int len = strlen(expr);
  855. char *p = *pr;
  856. if (strncmp(expr, p, len) != 0)
  857. return 0;
  858. p = sskip(p + len);
  859. if (*p != '=')
  860. return 0; // wrong or malformed
  861. *pr = sskip(p + 1);
  862. return 1;
  863. }
  864. #include "carthw_cfg.c"
  865. static void parse_carthw(const char *carthw_cfg, int *fill_sram,
  866. int *hw_detected)
  867. {
  868. int line = 0, any_checks_passed = 0, skip_sect = 0;
  869. const char *s, *builtin = builtin_carthw_cfg;
  870. int tmp, rom_crc = 0;
  871. char buff[256], *p, *r;
  872. FILE *f;
  873. f = fopen(carthw_cfg, "r");
  874. if (f == NULL)
  875. f = fopen("pico/carthw.cfg", "r");
  876. if (f == NULL)
  877. elprintf(EL_STATUS, "couldn't open carthw.cfg!");
  878. for (;;)
  879. {
  880. if (f != NULL) {
  881. p = fgets(buff, sizeof(buff), f);
  882. if (p == NULL)
  883. break;
  884. }
  885. else {
  886. if (*builtin == 0)
  887. break;
  888. for (s = builtin; *s != 0 && *s != '\n'; s++)
  889. ;
  890. while (*s == '\n')
  891. s++;
  892. tmp = s - builtin;
  893. if (tmp > sizeof(buff) - 1)
  894. tmp = sizeof(buff) - 1;
  895. memcpy(buff, builtin, tmp);
  896. buff[tmp] = 0;
  897. p = buff;
  898. builtin = s;
  899. }
  900. line++;
  901. p = sskip(p);
  902. if (*p == 0 || *p == '#')
  903. continue;
  904. if (*p == '[') {
  905. any_checks_passed = 0;
  906. skip_sect = 0;
  907. continue;
  908. }
  909. if (skip_sect)
  910. continue;
  911. /* look for checks */
  912. if (is_expr("check_str", &p))
  913. {
  914. int offs;
  915. offs = strtoul(p, &r, 0);
  916. if (offs < 0 || offs > Pico.romsize) {
  917. elprintf(EL_STATUS, "carthw:%d: check_str offs out of range: %d\n", line, offs);
  918. goto bad;
  919. }
  920. p = sskip(r);
  921. if (*p != ',')
  922. goto bad;
  923. p = sskip(p + 1);
  924. if (*p != '"')
  925. goto bad;
  926. p++;
  927. r = strchr(p, '"');
  928. if (r == NULL)
  929. goto bad;
  930. *r = 0;
  931. if (rom_strcmp(offs, p) == 0)
  932. any_checks_passed = 1;
  933. else
  934. skip_sect = 1;
  935. continue;
  936. }
  937. else if (is_expr("check_size_gt", &p))
  938. {
  939. int size;
  940. size = strtoul(p, &r, 0);
  941. if (r == p || size < 0)
  942. goto bad;
  943. if (Pico.romsize > size)
  944. any_checks_passed = 1;
  945. else
  946. skip_sect = 1;
  947. continue;
  948. }
  949. else if (is_expr("check_csum", &p))
  950. {
  951. int csum;
  952. csum = strtoul(p, &r, 0);
  953. if (r == p || (csum & 0xffff0000))
  954. goto bad;
  955. if (csum == (rom_read32(0x18c) & 0xffff))
  956. any_checks_passed = 1;
  957. else
  958. skip_sect = 1;
  959. continue;
  960. }
  961. else if (is_expr("check_crc32", &p))
  962. {
  963. unsigned int crc;
  964. crc = strtoul(p, &r, 0);
  965. if (r == p)
  966. goto bad;
  967. if (rom_crc == 0)
  968. rom_crc = rom_crc32();
  969. if (crc == rom_crc)
  970. any_checks_passed = 1;
  971. else
  972. skip_sect = 1;
  973. continue;
  974. }
  975. /* now time for actions */
  976. if (is_expr("hw", &p)) {
  977. if (!any_checks_passed)
  978. goto no_checks;
  979. *hw_detected = 1;
  980. rstrip(p);
  981. if (strcmp(p, "svp") == 0)
  982. PicoSVPStartup();
  983. else if (strcmp(p, "pico") == 0)
  984. PicoInitPico();
  985. else if (strcmp(p, "prot") == 0)
  986. carthw_sprot_startup();
  987. else if (strcmp(p, "ssf2_mapper") == 0)
  988. carthw_ssf2_startup();
  989. else if (strcmp(p, "x_in_1_mapper") == 0)
  990. carthw_Xin1_startup();
  991. else if (strcmp(p, "realtec_mapper") == 0)
  992. carthw_realtec_startup();
  993. else if (strcmp(p, "radica_mapper") == 0)
  994. carthw_radica_startup();
  995. else if (strcmp(p, "piersolar_mapper") == 0)
  996. carthw_pier_startup();
  997. else if (strcmp(p, "sf001_mapper") == 0)
  998. carthw_sf001_startup();
  999. else if (strcmp(p, "sf002_mapper") == 0)
  1000. carthw_sf002_startup();
  1001. else if (strcmp(p, "sf004_mapper") == 0)
  1002. carthw_sf004_startup();
  1003. else if (strcmp(p, "prot_lk3") == 0)
  1004. carthw_prot_lk3_startup();
  1005. else {
  1006. elprintf(EL_STATUS, "carthw:%d: unsupported mapper: %s", line, p);
  1007. skip_sect = 1;
  1008. *hw_detected = 0;
  1009. }
  1010. continue;
  1011. }
  1012. if (is_expr("sram_range", &p)) {
  1013. int start, end;
  1014. if (!any_checks_passed)
  1015. goto no_checks;
  1016. rstrip(p);
  1017. start = strtoul(p, &r, 0);
  1018. if (r == p)
  1019. goto bad;
  1020. p = sskip(r);
  1021. if (*p != ',')
  1022. goto bad;
  1023. p = sskip(p + 1);
  1024. end = strtoul(p, &r, 0);
  1025. if (r == p)
  1026. goto bad;
  1027. if (((start | end) & 0xff000000) || start > end) {
  1028. elprintf(EL_STATUS, "carthw:%d: bad sram_range: %08x - %08x", line, start, end);
  1029. goto bad_nomsg;
  1030. }
  1031. Pico.sv.start = start;
  1032. Pico.sv.end = end;
  1033. continue;
  1034. }
  1035. else if (is_expr("prop", &p)) {
  1036. if (!any_checks_passed)
  1037. goto no_checks;
  1038. rstrip(p);
  1039. if (strcmp(p, "no_sram") == 0)
  1040. Pico.sv.flags &= ~SRF_ENABLED;
  1041. else if (strcmp(p, "no_eeprom") == 0)
  1042. Pico.sv.flags &= ~SRF_EEPROM;
  1043. else if (strcmp(p, "filled_sram") == 0)
  1044. *fill_sram = 1;
  1045. else if (strcmp(p, "force_6btn") == 0)
  1046. PicoIn.quirks |= PQUIRK_FORCE_6BTN;
  1047. else {
  1048. elprintf(EL_STATUS, "carthw:%d: unsupported prop: %s", line, p);
  1049. goto bad_nomsg;
  1050. }
  1051. elprintf(EL_STATUS, "game prop: %s", p);
  1052. continue;
  1053. }
  1054. else if (is_expr("eeprom_type", &p)) {
  1055. int type;
  1056. if (!any_checks_passed)
  1057. goto no_checks;
  1058. rstrip(p);
  1059. type = strtoul(p, &r, 0);
  1060. if (r == p || type < 0)
  1061. goto bad;
  1062. Pico.sv.eeprom_type = type;
  1063. Pico.sv.flags |= SRF_EEPROM;
  1064. continue;
  1065. }
  1066. else if (is_expr("eeprom_lines", &p)) {
  1067. int scl, sda_in, sda_out;
  1068. if (!any_checks_passed)
  1069. goto no_checks;
  1070. rstrip(p);
  1071. if (!parse_3_vals(p, &scl, &sda_in, &sda_out))
  1072. goto bad;
  1073. if (scl < 0 || scl > 15 || sda_in < 0 || sda_in > 15 ||
  1074. sda_out < 0 || sda_out > 15)
  1075. goto bad;
  1076. Pico.sv.eeprom_bit_cl = scl;
  1077. Pico.sv.eeprom_bit_in = sda_in;
  1078. Pico.sv.eeprom_bit_out= sda_out;
  1079. continue;
  1080. }
  1081. else if ((tmp = is_expr("prot_ro_value16", &p)) || is_expr("prot_rw_value16", &p)) {
  1082. int addr, mask, val;
  1083. if (!any_checks_passed)
  1084. goto no_checks;
  1085. rstrip(p);
  1086. if (!parse_3_vals(p, &addr, &mask, &val))
  1087. goto bad;
  1088. carthw_sprot_new_location(addr, mask, val, tmp ? 1 : 0);
  1089. continue;
  1090. }
  1091. bad:
  1092. elprintf(EL_STATUS, "carthw:%d: unrecognized expression: %s", line, buff);
  1093. bad_nomsg:
  1094. skip_sect = 1;
  1095. continue;
  1096. no_checks:
  1097. elprintf(EL_STATUS, "carthw:%d: command without any checks before it: %s", line, buff);
  1098. skip_sect = 1;
  1099. continue;
  1100. }
  1101. if (f != NULL)
  1102. fclose(f);
  1103. }
  1104. /*
  1105. * various cart-specific things, which can't be handled by generic code
  1106. */
  1107. static void PicoCartDetect(const char *carthw_cfg)
  1108. {
  1109. int carthw_detected = 0;
  1110. int fill_sram = 0;
  1111. memset(&Pico.sv, 0, sizeof(Pico.sv));
  1112. if (Pico.rom[MEM_BE2(0x1B0)] == 'R' && Pico.rom[MEM_BE2(0x1B1)] == 'A')
  1113. {
  1114. Pico.sv.start = rom_read32(0x1B4) & ~0xff000001; // align
  1115. Pico.sv.end = (rom_read32(0x1B8) & ~0xff000000) | 1;
  1116. if (Pico.rom[MEM_BE2(0x1B3)] & 0x40)
  1117. // EEPROM
  1118. Pico.sv.flags |= SRF_EEPROM;
  1119. Pico.sv.flags |= SRF_ENABLED;
  1120. }
  1121. if (Pico.sv.end == 0 || Pico.sv.start > Pico.sv.end)
  1122. {
  1123. // some games may have bad headers, like S&K and Sonic3
  1124. // note: majority games use 0x200000 as starting address, but there are some which
  1125. // use something else (0x300000 by HardBall '95). Luckily they have good headers.
  1126. Pico.sv.start = 0x200000;
  1127. Pico.sv.end = 0x203FFF;
  1128. Pico.sv.flags |= SRF_ENABLED;
  1129. }
  1130. // set EEPROM defaults, in case it gets detected
  1131. Pico.sv.eeprom_type = 0; // 7bit (24C01)
  1132. Pico.sv.eeprom_bit_cl = 1;
  1133. Pico.sv.eeprom_bit_in = 0;
  1134. Pico.sv.eeprom_bit_out= 0;
  1135. if (carthw_cfg != NULL)
  1136. parse_carthw(carthw_cfg, &fill_sram, &carthw_detected);
  1137. // assume the standard mapper for large roms
  1138. if (!carthw_detected && Pico.romsize > 0x400000)
  1139. carthw_ssf2_startup();
  1140. if (Pico.sv.flags & SRF_ENABLED)
  1141. {
  1142. if (Pico.sv.flags & SRF_EEPROM)
  1143. Pico.sv.size = 0x2000;
  1144. else
  1145. Pico.sv.size = Pico.sv.end - Pico.sv.start + 1;
  1146. Pico.sv.data = calloc(Pico.sv.size, 1);
  1147. if (Pico.sv.data == NULL)
  1148. Pico.sv.flags &= ~SRF_ENABLED;
  1149. if (Pico.sv.eeprom_type == 1) // 1 == 0 in PD EEPROM code
  1150. Pico.sv.eeprom_type = 0;
  1151. }
  1152. if ((Pico.sv.flags & SRF_ENABLED) && fill_sram)
  1153. {
  1154. elprintf(EL_STATUS, "SRAM fill");
  1155. memset(Pico.sv.data, 0xff, Pico.sv.size);
  1156. }
  1157. // Unusual region 'code'
  1158. if (rom_strcmp(0x1f0, "EUROPE") == 0 || rom_strcmp(0x1f0, "Europe") == 0)
  1159. *(u32 *) (Pico.rom + 0x1f0) = CPU_LE4(0x20204520);
  1160. }
  1161. static void PicoCartDetectMS(void)
  1162. {
  1163. memset(&Pico.sv, 0, sizeof(Pico.sv));
  1164. // Always map SRAM, since there's no indicator in ROM if it's needed or not
  1165. // TODO: this should somehow be coming from a cart database!
  1166. Pico.sv.size = 0x8000; // Sega mapper, 2 banks of 16 KB each
  1167. Pico.sv.flags |= SRF_ENABLED;
  1168. Pico.sv.data = calloc(Pico.sv.size, 1);
  1169. if (Pico.sv.data == NULL)
  1170. Pico.sv.flags &= ~SRF_ENABLED;
  1171. }
  1172. // vim:shiftwidth=2:expandtab