Cart.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  1. // This is part of Pico Library
  2. // (c) Copyright 2004 Dave, All rights reserved.
  3. // (c) Copyright 2006 notaz, All rights reserved.
  4. // Free for non-commercial use.
  5. // For commercial use, separate licencing terms must be obtained.
  6. #include "PicoInt.h"
  7. #include "../zlib/zlib.h"
  8. #include "../unzip/unzip.h"
  9. #include "../unzip/unzip_stream.h"
  10. static char *rom_exts[] = { "bin", "gen", "smd", "iso" };
  11. pm_file *pm_open(const char *path)
  12. {
  13. pm_file *file = NULL;
  14. const char *ext;
  15. FILE *f;
  16. if (path == NULL) return NULL;
  17. if (strlen(path) < 5) ext = NULL; // no ext
  18. else ext = path + strlen(path) - 3;
  19. if (ext && strcasecmp(ext, "zip") == 0)
  20. {
  21. struct zipent *zipentry;
  22. gzFile gzf = NULL;
  23. ZIP *zipfile;
  24. int i;
  25. zipfile = openzip(path);
  26. if (zipfile != NULL)
  27. {
  28. /* search for suitable file (right extension or large enough file) */
  29. while ((zipentry = readzip(zipfile)) != NULL)
  30. {
  31. if (zipentry->uncompressed_size >= 128*1024) goto found_rom_zip;
  32. if (strlen(zipentry->name) < 5) continue;
  33. ext = zipentry->name+strlen(zipentry->name)-3;
  34. for (i = 0; i < sizeof(rom_exts)/sizeof(rom_exts[0]); i++)
  35. if (!strcasecmp(ext, rom_exts[i]) == 0) goto found_rom_zip;
  36. }
  37. /* zipfile given, but nothing found suitable for us inside */
  38. goto zip_failed;
  39. found_rom_zip:
  40. /* try to convert to gzip stream, so we could use standard gzio functions from zlib */
  41. gzf = zip2gz(zipfile, zipentry);
  42. if (gzf == NULL) goto zip_failed;
  43. file = malloc(sizeof(*file));
  44. if (file == NULL) goto zip_failed;
  45. file->file = zipfile;
  46. file->param = gzf;
  47. file->size = zipentry->uncompressed_size;
  48. file->type = PMT_ZIP;
  49. return file;
  50. zip_failed:
  51. if (gzf) {
  52. gzclose(gzf);
  53. zipfile->fp = NULL; // gzclose() closed it
  54. }
  55. closezip(zipfile);
  56. return NULL;
  57. }
  58. }
  59. /* not a zip, treat as uncompressed file */
  60. f = fopen(path, "rb");
  61. if (f == NULL) return NULL;
  62. file = malloc(sizeof(*file));
  63. if (file == NULL) {
  64. fclose(f);
  65. return NULL;
  66. }
  67. fseek(f, 0, SEEK_END);
  68. file->file = f;
  69. file->param = NULL;
  70. file->size = ftell(f);
  71. file->type = PMT_UNCOMPRESSED;
  72. fseek(f, 0, SEEK_SET);
  73. return file;
  74. }
  75. size_t pm_read(void *ptr, size_t bytes, pm_file *stream)
  76. {
  77. int ret;
  78. if (stream->type == PMT_UNCOMPRESSED)
  79. {
  80. ret = fread(ptr, 1, bytes, stream->file);
  81. }
  82. else if (stream->type == PMT_ZIP)
  83. {
  84. gzFile gf = stream->param;
  85. int err;
  86. ret = gzread(gf, ptr, bytes);
  87. err = gzerror2(gf);
  88. if (ret > 0 && (err == Z_DATA_ERROR || err == Z_STREAM_END))
  89. /* we must reset stream pointer or else next seek/read fails */
  90. gzrewind(gf);
  91. }
  92. else
  93. ret = 0;
  94. return ret;
  95. }
  96. int pm_seek(pm_file *stream, long offset, int whence)
  97. {
  98. if (stream->type == PMT_UNCOMPRESSED)
  99. {
  100. return fseek(stream->file, offset, whence);
  101. }
  102. else if (stream->type == PMT_ZIP)
  103. {
  104. return gzseek((gzFile) stream->param, offset, whence);
  105. }
  106. else
  107. return -1;
  108. }
  109. int pm_close(pm_file *fp)
  110. {
  111. int ret = 0;
  112. if (fp == NULL) return EOF;
  113. if (fp->type == PMT_UNCOMPRESSED)
  114. {
  115. fclose(fp->file);
  116. }
  117. else if (fp->type == PMT_ZIP)
  118. {
  119. ZIP *zipfile = fp->file;
  120. gzclose((gzFile) fp->param);
  121. zipfile->fp = NULL; // gzclose() closed it
  122. closezip(zipfile);
  123. }
  124. else
  125. ret = EOF;
  126. free(fp);
  127. return ret;
  128. }
  129. void Byteswap(unsigned char *data,int len)
  130. {
  131. int i=0;
  132. if (len<2) return; // Too short
  133. do
  134. {
  135. unsigned short *pd=(unsigned short *)(data+i);
  136. int value=*pd; // Get 2 bytes
  137. value=(value<<8)|(value>>8); // Byteswap it
  138. *pd=(unsigned short)value; // Put 2b ytes
  139. i+=2;
  140. }
  141. while (i+2<=len);
  142. }
  143. // Interleve a 16k block and byteswap
  144. static int InterleveBlock(unsigned char *dest,unsigned char *src)
  145. {
  146. int i=0;
  147. for (i=0;i<0x2000;i++) dest[(i<<1) ]=src[ i]; // Odd
  148. for (i=0;i<0x2000;i++) dest[(i<<1)+1]=src[0x2000+i]; // Even
  149. return 0;
  150. }
  151. // Decode a SMD file
  152. static int DecodeSmd(unsigned char *data,int len)
  153. {
  154. unsigned char *temp=NULL;
  155. int i=0;
  156. temp=(unsigned char *)malloc(0x4000);
  157. if (temp==NULL) return 1;
  158. memset(temp,0,0x4000);
  159. // Interleve each 16k block and shift down by 0x200:
  160. for (i=0; i+0x4200<=len; i+=0x4000)
  161. {
  162. InterleveBlock(temp,data+0x200+i); // Interleve 16k to temporary buffer
  163. memcpy(data+i,temp,0x4000); // Copy back in
  164. }
  165. free(temp);
  166. return 0;
  167. }
  168. static unsigned char *cd_realloc(void *old, int filesize)
  169. {
  170. unsigned char *rom;
  171. dprintf("sizeof(mcd_state): %i", sizeof(mcd_state));
  172. rom=realloc(old, sizeof(mcd_state));
  173. if (rom) memset(rom+0x20000, 0, sizeof(mcd_state)-0x20000);
  174. return rom;
  175. }
  176. static unsigned char *PicoCartAlloc(int filesize)
  177. {
  178. int alloc_size;
  179. unsigned char *rom;
  180. if (PicoMCD & 1) return cd_realloc(NULL, filesize);
  181. alloc_size=filesize+0x7ffff;
  182. if((filesize&0x3fff)==0x200) alloc_size-=0x200;
  183. alloc_size&=~0x7ffff; // use alloc size of multiples of 512K, so that memhandlers could be set up more efficiently
  184. if((filesize&0x3fff)==0x200) alloc_size+=0x200;
  185. else if(alloc_size-filesize < 4) alloc_size+=4; // padding for out-of-bound exec protection
  186. //dprintf("alloc_size: %x\n", alloc_size);
  187. // Allocate space for the rom plus padding
  188. rom=(unsigned char *)malloc(alloc_size);
  189. if(rom) memset(rom+alloc_size-0x80000,0,0x80000);
  190. return rom;
  191. }
  192. int PicoCartLoad(pm_file *f,unsigned char **prom,unsigned int *psize)
  193. {
  194. unsigned char *rom=NULL; int size;
  195. if (f==NULL) return 1;
  196. size=f->size;
  197. if (size <= 0) return 1;
  198. size=(size+3)&~3; // Round up to a multiple of 4
  199. // Allocate space for the rom plus padding
  200. rom=PicoCartAlloc(size);
  201. if (rom==NULL) return 1; // { fclose(f); return 1; }
  202. pm_read(rom,size,f); // Load up the rom
  203. // maybe we are loading MegaCD BIOS?
  204. if (!(PicoMCD&1) && size == 0x20000 && (!strncmp((char *)rom+0x124, "BOOT", 4) || !strncmp((char *)rom+0x128, "BOOT", 4))) {
  205. PicoMCD |= 1;
  206. rom = cd_realloc(rom, size);
  207. }
  208. // Check for SMD:
  209. if ((size&0x3fff)==0x200) { DecodeSmd(rom,size); size-=0x200; } // Decode and byteswap SMD
  210. else Byteswap(rom,size); // Just byteswap
  211. if (prom) *prom=rom;
  212. if (psize) *psize=size;
  213. return 0;
  214. }
  215. // Insert/remove a cartridge:
  216. int PicoCartInsert(unsigned char *rom,unsigned int romsize)
  217. {
  218. // notaz: add a 68k "jump one op back" opcode to the end of ROM.
  219. // This will hang the emu, but will prevent nasty crashes.
  220. // note: 4 bytes are padded to every ROM
  221. if(rom != NULL)
  222. *(unsigned long *)(rom+romsize) = 0xFFFE4EFA; // 4EFA FFFE byteswapped
  223. SRam.resize=1;
  224. Pico.rom=rom;
  225. Pico.romsize=romsize;
  226. return PicoReset(1);
  227. }
  228. int PicoUnloadCart(unsigned char* romdata)
  229. {
  230. free(romdata);
  231. return 0;
  232. }