5 unsigned char *RomData=NULL;
\r
7 char RomName[260]="";
\r
10 static int Byteswap(unsigned char *data,int len)
\r
14 if (len<2) return 1; // Too short
\r
18 unsigned short *pd=(unsigned short *)(data+i);
\r
19 int word=*pd; // Get word
\r
21 word=(word<<8)|(word>>8); // Byteswap it
\r
22 *pd=(unsigned short)word; // Put word
\r
30 // Interleve a 16k block and byteswap
\r
31 static int InterleveBlock(unsigned char *dest,unsigned char *src)
\r
34 for (i=0;i<0x2000;i++) dest[(i<<1) ]=src[ i]; // Odd
\r
35 for (i=0;i<0x2000;i++) dest[(i<<1)+1]=src[0x2000+i]; // Even
\r
39 // Decode a SMD file
\r
40 static int DecodeSmd(unsigned char *data,int len)
\r
42 unsigned char *temp=NULL;
\r
45 temp=(unsigned char *)malloc(0x4000);
\r
46 if (temp==NULL) return 1;
\r
47 memset(temp,0,0x4000);
\r
49 // Interleve each 16k block and shift down by 0x200:
\r
50 for (i=0; i+0x4200<=len; i+=0x4000)
\r
52 InterleveBlock(temp,data+0x200+i); // Interleve 16k to temporary buffer
\r
53 memcpy(data+i,temp,0x4000); // Copy back in
\r
65 int fileLen=0,space=0;
\r
70 file=fopen(name,"rb"); if (file==NULL) return 1;
\r
72 nameLen=strlen(name);
\r
73 if (stricmp(name+nameLen-4,".zip")==0) unzip.file=file; // Open as zip file
\r
79 ret=unzip.fileOpen(); // Get first entry
\r
82 fileLen=unzip.dataLen; // Length of file
\r
83 // Switch to using the name in the zip file:
\r
84 name=unzip.name; nameLen=strlen(name);
\r
94 // Find out the length of the file:
\r
95 fseek(file,0,SEEK_END); fileLen=ftell(file);
\r
96 fseek(file,0,SEEK_SET);
\r
99 // Allocate space for it:
\r
100 space=(fileLen+0x3fff)&~0x3fff;
\r
102 RomData=(unsigned char *)malloc(space);
\r
103 if (RomData==NULL) { fclose(file); return 1; }
\r
104 memset(RomData,0,space);
\r
107 if (unzip.file) unzip.fileDecode(RomData);
\r
108 else fread(RomData,1,fileLen,file);
\r
113 unzip.file=file=NULL;
\r
118 if ((fileLen&0x3fff)==0x200)
\r
120 // Decode and byteswap:
\r
121 DecodeSmd(RomData,RomLen);
\r
127 Byteswap(RomData,RomLen);
\r
130 PicoCartInsert(RomData,RomLen);
\r
137 // PicoCartInsert(NULL,0); // Unplug rom
\r
139 if (RomData) free(RomData);
\r
140 RomData=NULL; RomLen=0;
\r
141 memset(RomName,0,sizeof(RomName));
\r