1 /***************************************************************************
2 * Copyright (C) 2007 Ryan Schultz, PCSX-df Team, PCSX team *
4 * This program is free software; you can redistribute it and/or modify *
5 * it under the terms of the GNU General Public License as published by *
6 * the Free Software Foundation; either version 2 of the License, or *
7 * (at your option) any later version. *
9 * This program is distributed in the hope that it will be useful, *
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
12 * GNU General Public License for more details. *
14 * You should have received a copy of the GNU General Public License *
15 * along with this program; if not, write to the *
16 * Free Software Foundation, Inc., *
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02111-1307 USA. *
18 ***************************************************************************/
30 #define TX_EMPTY 0x0004
31 #define PARITY_ERR 0x0008
32 #define RX_OVERRUN 0x0010
33 #define FRAMING_ERR 0x0020
34 #define SYNC_DETECT 0x0040
40 #define TX_PERM 0x0001
42 #define RX_PERM 0x0004
44 #define RESET_ERR 0x0010
46 #define SIO_RESET 0x0040
48 // *** FOR WORKS ON PADS AND MEMORY CARDS *****
50 static unsigned char buf[256];
51 unsigned char cardh[4] = { 0x00, 0x00, 0x5a, 0x5d };
53 // Transfer Ready and the Buffer is Empty
54 // static unsigned short StatReg = 0x002b;
55 static unsigned short StatReg = TX_RDY | TX_EMPTY;
56 static unsigned short ModeReg;
57 static unsigned short CtrlReg;
58 static unsigned short BaudReg;
60 static unsigned int bufcount;
61 static unsigned int parp;
62 static unsigned int mcdst, rdwr;
63 static unsigned char adrH, adrL;
64 static unsigned int padst;
66 char Mcd1Data[MCD_SIZE], Mcd2Data[MCD_SIZE];
69 // 4us * 8bits = (PSXCLK / 1000000) * 32; (linuzappz)
70 // TODO: add SioModePrescaler and BaudReg
71 static inline void SIO_INT() {
73 psxRegs.interrupt |= 0x80;
74 psxRegs.intCycle[7 + 1] = 400;
75 psxRegs.intCycle[7] = psxRegs.cycle;
79 void sioWrite8(unsigned char value) {
81 PAD_LOG("sio write8 %x\n", value);
85 if ((value & 0x40) == 0x40) {
88 switch (CtrlReg & 0x2002) {
90 buf[parp] = PAD1_poll(value);
93 buf[parp] = PAD2_poll(value);
97 // SysPrintf("%x: %x, %x, %x, %x\n", CtrlReg&0x2002, buf[2], buf[3], buf[4], buf[5]);
100 if (!(buf[parp] & 0x0f)) {
103 bufcount = 2 + (buf[parp] & 0x0f) * 2;
105 if (buf[parp] == 0x41) {
120 /* if (buf[1] == 0x45) {
125 if (!Config.UseNet) {
126 switch (CtrlReg & 0x2002) {
127 case 0x0002: buf[parp] = PAD1_poll(value); break;
128 case 0x2002: buf[parp] = PAD2_poll(value); break;
132 if (parp == bufcount) { padst = 0; return; }
140 if (rdwr) { parp++; return; }
143 case 0x52: rdwr = 1; break;
144 case 0x57: rdwr = 2; break;
173 switch (CtrlReg & 0x2002) {
175 memcpy(&buf[4], Mcd1Data + (adrL | (adrH << 8)) * 128, 128);
178 memcpy(&buf[4], Mcd2Data + (adrL | (adrH << 8)) * 128, 128);
184 for (i = 2; i < 128 + 4; i++)
205 if (parp < 128) buf[parp + 1] = value;
212 case 0x01: // start pad
213 StatReg |= RX_RDY; // Transfer is Ready
215 if (!Config.UseNet) {
216 switch (CtrlReg & 0x2002) {
217 case 0x0002: buf[0] = PAD1_startPoll(1); break;
218 case 0x2002: buf[0] = PAD2_startPoll(2); break;
221 if ((CtrlReg & 0x2002) == 0x0002) {
226 buf[1] = PAD1_poll(0x42);
227 if (!(buf[1] & 0x0f)) {
230 bufcount = (buf[1] & 0x0f) * 2;
232 buf[2] = PAD1_poll(0);
236 buf[i++] = PAD1_poll(0);
240 if (NET_sendPadData(buf, bufcount) == -1)
243 if (NET_recvPadData(buf, 1) == -1)
245 if (NET_recvPadData(buf + 128, 2) == -1)
248 memcpy(buf, buf + 128, 32);
257 case 0x81: // start memcard
259 memcpy(buf, cardh, 4);
269 void sioWriteStat16(unsigned short value) {
272 void sioWriteMode16(unsigned short value) {
276 void sioWriteCtrl16(unsigned short value) {
277 CtrlReg = value & ~RESET_ERR;
278 if (value & RESET_ERR) StatReg &= ~IRQ;
279 if ((CtrlReg & SIO_RESET) || (!CtrlReg)) {
280 padst = 0; mcdst = 0; parp = 0;
281 StatReg = TX_RDY | TX_EMPTY;
282 psxRegs.interrupt &= ~0x80;
286 void sioWriteBaud16(unsigned short value) {
290 unsigned char sioRead8() {
291 unsigned char ret = 0;
293 if ((StatReg & RX_RDY)/* && (CtrlReg & RX_PERM)*/) {
294 // StatReg &= ~RX_OVERRUN;
296 if (parp == bufcount) {
297 StatReg &= ~RX_RDY; // Receive is not Ready now
301 switch (CtrlReg & 0x2002) {
303 memcpy(Mcd1Data + (adrL | (adrH << 8)) * 128, &buf[1], 128);
304 SaveMcd(Config.Mcd1, Mcd1Data, (adrL | (adrH << 8)) * 128, 128);
307 memcpy(Mcd2Data + (adrL | (adrH << 8)) * 128, &buf[1], 128);
308 SaveMcd(Config.Mcd2, Mcd2Data, (adrL | (adrH << 8)) * 128, 128);
313 if (padst == 2) padst = 0;
322 PAD_LOG("sio read8 ;ret = %x\n", ret);
327 unsigned short sioReadStat16() {
331 unsigned short sioReadMode16() {
335 unsigned short sioReadCtrl16() {
339 unsigned short sioReadBaud16() {
345 SysMessage(_("Connection closed!\n"));
348 CdromLabel[0] = '\0';
353 void sioInterrupt() {
355 PAD_LOG("Sio Interrupt (CP0.Status = %x)\n", psxRegs.CP0.n.Status);
357 // SysPrintf("Sio Interrupt\n");
359 psxHu32ref(0x1070) |= SWAPu32(0x80);
362 void LoadMcd(int mcd, char *str) {
366 if (mcd == 1) data = Mcd1Data;
367 if (mcd == 2) data = Mcd2Data;
370 sprintf(str, "memcards/card%d.mcd", mcd);
371 SysPrintf(_("No memory card value was specified - creating a default card %s\n"), str);
373 f = fopen(str, "rb");
375 SysPrintf(_("The memory card %s doesn't exist - creating it\n"), str);
377 f = fopen(str, "rb");
381 if (stat(str, &buf) != -1) {
382 if (buf.st_size == MCD_SIZE + 64)
383 fseek(f, 64, SEEK_SET);
384 else if(buf.st_size == MCD_SIZE + 3904)
385 fseek(f, 3904, SEEK_SET);
387 fread(data, 1, MCD_SIZE, f);
391 SysMessage(_("Memory card %s failed to load!\n"), str);
395 SysPrintf(_("Loading memory card %s\n"), str);
396 if (stat(str, &buf) != -1) {
397 if (buf.st_size == MCD_SIZE + 64)
398 fseek(f, 64, SEEK_SET);
399 else if(buf.st_size == MCD_SIZE + 3904)
400 fseek(f, 3904, SEEK_SET);
402 fread(data, 1, MCD_SIZE, f);
407 void LoadMcds(char *mcd1, char *mcd2) {
412 void SaveMcd(char *mcd, char *data, uint32_t adr, int size) {
415 f = fopen(mcd, "r+b");
419 if (stat(mcd, &buf) != -1) {
420 if (buf.st_size == MCD_SIZE + 64)
421 fseek(f, adr + 64, SEEK_SET);
422 else if (buf.st_size == MCD_SIZE + 3904)
423 fseek(f, adr + 3904, SEEK_SET);
425 fseek(f, adr, SEEK_SET);
427 fseek(f, adr, SEEK_SET);
429 fwrite(data + adr, 1, size, f);
435 // try to create it again if we can't open it
436 f = fopen(mcd, "wb");
438 fwrite(data, 1, MCD_SIZE, f);
443 ConvertMcd(mcd, data);
446 void CreateMcd(char *mcd) {
452 f = fopen(mcd, "wb");
456 if (stat(mcd, &buf) != -1) {
457 if ((buf.st_size == MCD_SIZE + 3904) || strstr(mcd, ".gme")) {
481 for (i = 0; i < 7; i++) {
495 for (i = 0; i < 14; i++) {
502 while (s-- > (MCD_SIZE + 1))
504 } else if ((buf.st_size == MCD_SIZE + 64) || strstr(mcd, ".mem") || strstr(mcd, ".vgs")) {
514 for (i = 0; i < 3; i++) {
527 while (s-- > (MCD_SIZE + 1))
535 while (s-- > (MCD_SIZE - 127))
540 for (i = 0; i < 15; i++) { // 15 blocks
561 for (j = 0; j < 117; j++) {
569 for (i = 0; i < 20; i++) {
590 for (j = 0; j < 118; j++) {
602 void ConvertMcd(char *mcd, char *data) {
607 if (strstr(mcd, ".gme")) {
608 f = fopen(mcd, "wb");
610 fwrite(data - 3904, 1, MCD_SIZE + 3904, f);
613 f = fopen(mcd, "r+");
626 for (i = 0; i < 7; i++) {
639 while (s-- > (MCD_SIZE+1)) fputc(0, f);
641 } else if(strstr(mcd, ".mem") || strstr(mcd,".vgs")) {
642 f = fopen(mcd, "wb");
644 fwrite(data-64, 1, MCD_SIZE+64, f);
647 f = fopen(mcd, "r+");
661 while (s-- > (MCD_SIZE+1)) fputc(0, f);
664 f = fopen(mcd, "wb");
666 fwrite(data, 1, MCD_SIZE, f);
672 void GetMcdBlockInfo(int mcd, int block, McdBlock *Info) {
673 unsigned char *data = NULL, *ptr, *str, *sstr;
674 unsigned short clut[16];
678 memset(Info, 0, sizeof(McdBlock));
680 if (mcd == 1) data = Mcd1Data;
681 if (mcd == 2) data = Mcd2Data;
683 ptr = data + block * 8192 + 2;
685 Info->IconCount = *ptr & 0x3;
694 for (i = 0; i < 48; i++) {
699 // Convert ASCII characters to half-width
700 if (c >= 0x8281 && c <= 0x829A)
701 c = (c - 0x8281) + 'a';
702 else if (c >= 0x824F && c <= 0x827A)
703 c = (c - 0x824F) + '0';
704 else if (c == 0x8140) c = ' ';
705 else if (c == 0x8143) c = ',';
706 else if (c == 0x8144) c = '.';
707 else if (c == 0x8146) c = ':';
708 else if (c == 0x8147) c = ';';
709 else if (c == 0x8148) c = '?';
710 else if (c == 0x8149) c = '!';
711 else if (c == 0x815E) c = '/';
712 else if (c == 0x8168) c = '"';
713 else if (c == 0x8169) c = '(';
714 else if (c == 0x816A) c = ')';
715 else if (c == 0x816D) c = '[';
716 else if (c == 0x816E) c = ']';
717 else if (c == 0x817C) c = '-';
720 sstr[x++] = *ptr++; sstr[x++] = *ptr++;
724 str[i] = sstr[x++] = c;
731 ptr = data + block * 8192 + 0x60; // icon palette data
733 for (i = 0; i < 16; i++) {
734 clut[i] = *((unsigned short *)ptr);
738 for (i = 0; i < Info->IconCount; i++) {
739 short *icon = &Info->Icon[i * 16 * 16];
741 ptr = data + block * 8192 + 128 + 128 * i; // icon data
743 for (x = 0; x < 16 * 16; x++) {
744 icon[x++] = clut[*ptr & 0xf];
745 icon[x] = clut[*ptr >> 4];
750 ptr = data + block * 128;
755 strncpy(Info->ID, ptr, 12);
757 strncpy(Info->Name, ptr, 16);
760 int sioFreeze(gzFile f, int Mode) {
761 gzfreeze(buf, sizeof(buf));
762 gzfreeze(&StatReg, sizeof(StatReg));
763 gzfreeze(&ModeReg, sizeof(ModeReg));
764 gzfreeze(&CtrlReg, sizeof(CtrlReg));
765 gzfreeze(&BaudReg, sizeof(BaudReg));
766 gzfreeze(&bufcount, sizeof(bufcount));
767 gzfreeze(&parp, sizeof(parp));
768 gzfreeze(&mcdst, sizeof(mcdst));
769 gzfreeze(&rdwr, sizeof(rdwr));
770 gzfreeze(&adrH, sizeof(adrH));
771 gzfreeze(&adrL, sizeof(adrL));
772 gzfreeze(&padst, sizeof(padst));