2 // This file is part of the PicoDrive Megadrive Emulator
\r
4 // This code is licensed under the GNU General Public License version 2.0 and the MAME License.
\r
5 // You can choose the license that has the most advantages for you.
\r
7 // SVN repository can be found at http://code.google.com/p/cyclone68000/
\r
11 static void Byteswap(unsigned char *data,int len)
\r
15 if (len<2) return; // Too short
\r
19 unsigned short *pd=(unsigned short *)(data+i);
\r
20 int value=*pd; // Get 2 bytes
\r
22 value=(value<<8)|(value>>8); // Byteswap it
\r
23 *pd=(unsigned short)value; // Put 2b ytes
\r
29 // Interleve a 16k block and byteswap
\r
30 static int InterleveBlock(unsigned char *dest,unsigned char *src)
\r
33 for (i=0;i<0x2000;i++) dest[(i<<1) ]=src[ i]; // Odd
\r
34 for (i=0;i<0x2000;i++) dest[(i<<1)+1]=src[0x2000+i]; // Even
\r
38 // Decode a SMD file
\r
39 static int DecodeSmd(unsigned char *data,int len)
\r
41 unsigned char *temp=NULL;
\r
44 temp=(unsigned char *)malloc(0x4000);
\r
45 if (temp==NULL) return 1;
\r
46 memset(temp,0,0x4000);
\r
48 // Interleve each 16k block and shift down by 0x200:
\r
49 for (i=0; i+0x4200<=len; i+=0x4000)
\r
51 InterleveBlock(temp,data+0x200+i); // Interleve 16k to temporary buffer
\r
52 memcpy(data+i,temp,0x4000); // Copy back in
\r
59 int PicoCartLoad(FILE *f,unsigned char **prom,unsigned int *psize)
\r
61 unsigned char *rom=NULL; int size=0;
\r
62 if (f==NULL) return 1;
\r
64 fseek(f,0,SEEK_END); size=ftell(f); fseek(f,0,SEEK_SET);
\r
66 size=(size+3)&~3; // Round up to a multiple of 4
\r
68 // Allocate space for the rom plus padding
\r
69 rom=(unsigned char *)malloc(size+4);
\r
70 if (rom==NULL) { fclose(f); return 1; }
\r
71 memset(rom,0,size+4);
\r
73 fread(rom,1,size,f); // Load up the rom
\r
77 if ((size&0x3fff)==0x200) { DecodeSmd(rom,size); size-=0x200; } // Decode and byteswap SMD
\r
78 else Byteswap(rom,size); // Just byteswap
\r
80 if (prom) *prom=rom;
\r
81 if (psize) *psize=size;
\r
86 // Insert/remove a cartridge:
\r
87 int PicoCartInsert(unsigned char *rom,unsigned int romsize)
\r
89 // Make sure movie playing/recording is stopped:
\r
90 if (PmovFile) fclose(PmovFile);
\r
91 PmovFile=NULL; PmovAction=0;
\r
93 memset(&Pico,0,sizeof(Pico)); // Blank Pico state
\r
95 Pico.romsize=romsize;
\r