cart.c 33 KB

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