| 1 | \r |
| 2 | #include "app.h"\r |
| 3 | #include "Unzip.h"\r |
| 4 | \r |
| 5 | unsigned char *RomData=NULL;\r |
| 6 | int RomLen=0;\r |
| 7 | char RomName[260]="";\r |
| 8 | \r |
| 9 | \r |
| 10 | static int Byteswap(unsigned char *data,int len)\r |
| 11 | {\r |
| 12 | int i=0;\r |
| 13 | \r |
| 14 | if (len<2) return 1; // Too short\r |
| 15 | \r |
| 16 | do\r |
| 17 | {\r |
| 18 | unsigned short *pd=(unsigned short *)(data+i);\r |
| 19 | int word=*pd; // Get word\r |
| 20 | \r |
| 21 | word=(word<<8)|(word>>8); // Byteswap it\r |
| 22 | *pd=(unsigned short)word; // Put word\r |
| 23 | i+=2;\r |
| 24 | } \r |
| 25 | while (i+2<=len);\r |
| 26 | \r |
| 27 | return 0;\r |
| 28 | }\r |
| 29 | \r |
| 30 | // Interleve a 16k block and byteswap\r |
| 31 | static int InterleveBlock(unsigned char *dest,unsigned char *src)\r |
| 32 | {\r |
| 33 | int i=0;\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 |
| 36 | return 0;\r |
| 37 | }\r |
| 38 | \r |
| 39 | // Decode a SMD file\r |
| 40 | static int DecodeSmd(unsigned char *data,int len)\r |
| 41 | {\r |
| 42 | unsigned char *temp=NULL;\r |
| 43 | int i=0;\r |
| 44 | \r |
| 45 | temp=(unsigned char *)malloc(0x4000);\r |
| 46 | if (temp==NULL) return 1;\r |
| 47 | memset(temp,0,0x4000);\r |
| 48 | \r |
| 49 | // Interleve each 16k block and shift down by 0x200:\r |
| 50 | for (i=0; i+0x4200<=len; i+=0x4000)\r |
| 51 | {\r |
| 52 | InterleveBlock(temp,data+0x200+i); // Interleve 16k to temporary buffer\r |
| 53 | memcpy(data+i,temp,0x4000); // Copy back in\r |
| 54 | }\r |
| 55 | \r |
| 56 | free(temp);\r |
| 57 | return 0;\r |
| 58 | }\r |
| 59 | \r |
| 60 | int RomLoad()\r |
| 61 | {\r |
| 62 | FILE *file=NULL;\r |
| 63 | char *name=NULL;\r |
| 64 | int nameLen=0;\r |
| 65 | int fileLen=0,space=0;\r |
| 66 | Unzip unzip;\r |
| 67 | \r |
| 68 | name=RomName;\r |
| 69 | \r |
| 70 | file=fopen(name,"rb"); if (file==NULL) return 1;\r |
| 71 | \r |
| 72 | nameLen=strlen(name);\r |
| 73 | if (stricmp(name+nameLen-4,".zip")==0) unzip.file=file; // Open as zip file\r |
| 74 | \r |
| 75 | if (unzip.file)\r |
| 76 | {\r |
| 77 | int ret=0;\r |
| 78 | \r |
| 79 | ret=unzip.fileOpen(); // Get first entry\r |
| 80 | if (ret==0)\r |
| 81 | {\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 |
| 85 | }\r |
| 86 | else\r |
| 87 | {\r |
| 88 | unzip.file=NULL;\r |
| 89 | }\r |
| 90 | \r |
| 91 | }\r |
| 92 | else\r |
| 93 | {\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 |
| 97 | }\r |
| 98 | \r |
| 99 | // Allocate space for it:\r |
| 100 | space=(fileLen+0x3fff)&~0x3fff;\r |
| 101 | \r |
| 102 | RomData=(unsigned char *)malloc(space);\r |
| 103 | if (RomData==NULL) { fclose(file); return 1; }\r |
| 104 | memset(RomData,0,space);\r |
| 105 | \r |
| 106 | // Read in file:\r |
| 107 | if (unzip.file) unzip.fileDecode(RomData);\r |
| 108 | else fread(RomData,1,fileLen,file);\r |
| 109 | \r |
| 110 | unzip.fileClose();\r |
| 111 | \r |
| 112 | fclose(file);\r |
| 113 | unzip.file=file=NULL;\r |
| 114 | \r |
| 115 | RomLen=fileLen;\r |
| 116 | \r |
| 117 | // Check for SMD:\r |
| 118 | if ((fileLen&0x3fff)==0x200)\r |
| 119 | {\r |
| 120 | // Decode and byteswap:\r |
| 121 | DecodeSmd(RomData,RomLen);\r |
| 122 | RomLen-=0x200;\r |
| 123 | }\r |
| 124 | else\r |
| 125 | {\r |
| 126 | // Just byteswap:\r |
| 127 | Byteswap(RomData,RomLen);\r |
| 128 | }\r |
| 129 | \r |
| 130 | PicoCartInsert(RomData,RomLen);\r |
| 131 | \r |
| 132 | return 0;\r |
| 133 | }\r |
| 134 | \r |
| 135 | void RomFree()\r |
| 136 | {\r |
| 137 | // PicoCartInsert(NULL,0); // Unplug rom\r |
| 138 | \r |
| 139 | if (RomData) free(RomData);\r |
| 140 | RomData=NULL; RomLen=0;\r |
| 141 | memset(RomName,0,sizeof(RomName));\r |
| 142 | }\r |
| 143 | \r |