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 static unsigned char cardh1[4] = { 0xff, 0x08, 0x5a, 0x5d };
52 static unsigned char cardh2[4] = { 0xff, 0x08, 0x5a, 0x5d };
54 // Transfer Ready and the Buffer is Empty
55 // static unsigned short StatReg = 0x002b;
56 static unsigned short StatReg = TX_RDY | TX_EMPTY;
57 static unsigned short ModeReg;
58 static unsigned short CtrlReg;
59 static unsigned short BaudReg;
61 static unsigned int bufcount;
62 static unsigned int parp;
63 static unsigned int mcdst, rdwr;
64 static unsigned char adrH, adrL;
65 static unsigned int padst;
67 char Mcd1Data[MCD_SIZE], Mcd2Data[MCD_SIZE];
70 #define SIO_INT(eCycle) { \
71 psxRegs.interrupt |= (1 << PSXINT_SIO); \
72 psxRegs.intCycle[PSXINT_SIO].cycle = eCycle; \
73 psxRegs.intCycle[PSXINT_SIO].sCycle = psxRegs.cycle; \
74 new_dyna_set_event(PSXINT_SIO, eCycle); \
78 // 4us * 8bits = (PSXCLK / 1000000) * 32; (linuzappz)
79 // TODO: add SioModePrescaler and BaudReg
80 #define SIO_CYCLES 535
82 void sioWrite8(unsigned char value) {
84 PAD_LOG("sio write8 %x\n", value);
87 case 1: SIO_INT(SIO_CYCLES);
88 if ((value & 0x40) == 0x40) {
91 switch (CtrlReg & 0x2002) {
93 buf[parp] = PAD1_poll(value);
96 buf[parp] = PAD2_poll(value);
100 // SysPrintf("%x: %x, %x, %x, %x\n", CtrlReg&0x2002, buf[2], buf[3], buf[4], buf[5]);
103 if (!(buf[parp] & 0x0f)) {
106 bufcount = 2 + (buf[parp] & 0x0f) * 2;
108 if (buf[parp] == 0x41) {
123 /* if (buf[1] == 0x45) {
128 if (!Config.UseNet) {
129 switch (CtrlReg & 0x2002) {
130 case 0x0002: buf[parp] = PAD1_poll(value); break;
131 case 0x2002: buf[parp] = PAD2_poll(value); break;
135 if (parp == bufcount) { padst = 0; return; }
143 if (rdwr) { parp++; return; }
146 case 0x52: rdwr = 1; break;
147 case 0x57: rdwr = 2; break;
176 switch (CtrlReg & 0x2002) {
178 memcpy(&buf[4], Mcd1Data + (adrL | (adrH << 8)) * 128, 128);
181 memcpy(&buf[4], Mcd2Data + (adrL | (adrH << 8)) * 128, 128);
187 for (i = 2; i < 128 + 4; i++)
207 if ((rdwr == 1 && parp == 132) ||
208 (rdwr == 2 && parp == 129)) {
209 // clear "new card" flags
210 if (CtrlReg & 0x2000)
216 if (parp < 128) buf[parp + 1] = value;
223 case 0x01: // start pad
224 StatReg |= RX_RDY; // Transfer is Ready
226 if (!Config.UseNet) {
227 switch (CtrlReg & 0x2002) {
228 case 0x0002: buf[0] = PAD1_startPoll(1); break;
229 case 0x2002: buf[0] = PAD2_startPoll(2); break;
232 if ((CtrlReg & 0x2002) == 0x0002) {
237 buf[1] = PAD1_poll(0x42);
238 if (!(buf[1] & 0x0f)) {
241 bufcount = (buf[1] & 0x0f) * 2;
243 buf[2] = PAD1_poll(0);
247 buf[i++] = PAD1_poll(0);
251 if (NET_sendPadData(buf, bufcount) == -1)
254 if (NET_recvPadData(buf, 1) == -1)
256 if (NET_recvPadData(buf + 128, 2) == -1)
259 memcpy(buf, buf + 128, 32);
268 case 0x81: // start memcard
269 if (CtrlReg & 0x2000)
273 memcpy(buf, cardh2, 4);
279 memcpy(buf, cardh1, 4);
298 void sioWriteStat16(unsigned short value) {
301 void sioWriteMode16(unsigned short value) {
305 void sioWriteCtrl16(unsigned short value) {
306 CtrlReg = value & ~RESET_ERR;
307 if (value & RESET_ERR) StatReg &= ~IRQ;
308 if ((CtrlReg & SIO_RESET) || !(CtrlReg & DTR)) {
309 padst = 0; mcdst = 0; parp = 0;
310 StatReg = TX_RDY | TX_EMPTY;
311 psxRegs.interrupt &= ~(1 << PSXINT_SIO);
315 void sioWriteBaud16(unsigned short value) {
319 unsigned char sioRead8() {
320 unsigned char ret = 0;
322 if ((StatReg & RX_RDY)/* && (CtrlReg & RX_PERM)*/) {
323 // StatReg &= ~RX_OVERRUN;
325 if (parp == bufcount) {
326 StatReg &= ~RX_RDY; // Receive is not Ready now
330 switch (CtrlReg & 0x2002) {
332 memcpy(Mcd1Data + (adrL | (adrH << 8)) * 128, &buf[1], 128);
333 SaveMcd(Config.Mcd1, Mcd1Data, (adrL | (adrH << 8)) * 128, 128);
336 memcpy(Mcd2Data + (adrL | (adrH << 8)) * 128, &buf[1], 128);
337 SaveMcd(Config.Mcd2, Mcd2Data, (adrL | (adrH << 8)) * 128, 128);
342 if (padst == 2) padst = 0;
351 PAD_LOG("sio read8 ;ret = %x\n", ret);
356 unsigned short sioReadStat16() {
360 unsigned short sioReadMode16() {
364 unsigned short sioReadCtrl16() {
368 unsigned short sioReadBaud16() {
374 SysMessage(_("Connection closed!\n"));
377 CdromLabel[0] = '\0';
382 void sioInterrupt() {
384 PAD_LOG("Sio Interrupt (CP0.Status = %x)\n", psxRegs.CP0.n.Status);
386 // SysPrintf("Sio Interrupt\n");
387 if (!(StatReg & IRQ)) {
389 psxHu32ref(0x1070) |= SWAPu32(0x80);
393 void LoadMcd(int mcd, char *str) {
397 if (mcd != 1 && mcd != 2)
402 cardh1[1] |= 8; // mark as new
409 McdDisable[mcd - 1] = 0;
410 if (str == NULL || strcmp(str, "none") == 0) {
411 McdDisable[mcd - 1] = 1;
417 f = fopen(str, "rb");
419 SysPrintf(_("The memory card %s doesn't exist - creating it\n"), str);
421 f = fopen(str, "rb");
425 if (stat(str, &buf) != -1) {
426 if (buf.st_size == MCD_SIZE + 64)
427 fseek(f, 64, SEEK_SET);
428 else if(buf.st_size == MCD_SIZE + 3904)
429 fseek(f, 3904, SEEK_SET);
431 fread(data, 1, MCD_SIZE, f);
435 SysMessage(_("Memory card %s failed to load!\n"), str);
439 SysPrintf(_("Loading memory card %s\n"), str);
440 if (stat(str, &buf) != -1) {
441 if (buf.st_size == MCD_SIZE + 64)
442 fseek(f, 64, SEEK_SET);
443 else if(buf.st_size == MCD_SIZE + 3904)
444 fseek(f, 3904, SEEK_SET);
446 fread(data, 1, MCD_SIZE, f);
451 void LoadMcds(char *mcd1, char *mcd2) {
456 void SaveMcd(char *mcd, char *data, uint32_t adr, int size) {
459 if (mcd == NULL || *mcd == 0 || strcmp(mcd, "none") == 0)
462 f = fopen(mcd, "r+b");
466 if (stat(mcd, &buf) != -1) {
467 if (buf.st_size == MCD_SIZE + 64)
468 fseek(f, adr + 64, SEEK_SET);
469 else if (buf.st_size == MCD_SIZE + 3904)
470 fseek(f, adr + 3904, SEEK_SET);
472 fseek(f, adr, SEEK_SET);
474 fseek(f, adr, SEEK_SET);
476 fwrite(data + adr, 1, size, f);
482 // try to create it again if we can't open it
483 f = fopen(mcd, "wb");
485 fwrite(data, 1, MCD_SIZE, f);
490 ConvertMcd(mcd, data);
493 void CreateMcd(char *mcd) {
499 f = fopen(mcd, "wb");
503 if (stat(mcd, &buf) != -1) {
504 if ((buf.st_size == MCD_SIZE + 3904) || strstr(mcd, ".gme")) {
528 for (i = 0; i < 7; i++) {
542 for (i = 0; i < 14; i++) {
549 while (s-- > (MCD_SIZE + 1))
551 } else if ((buf.st_size == MCD_SIZE + 64) || strstr(mcd, ".mem") || strstr(mcd, ".vgs")) {
561 for (i = 0; i < 3; i++) {
574 while (s-- > (MCD_SIZE + 1))
582 while (s-- > (MCD_SIZE - 127))
587 for (i = 0; i < 15; i++) { // 15 blocks
608 for (j = 0; j < 117; j++) {
616 for (i = 0; i < 20; i++) {
637 for (j = 0; j < 118; j++) {
649 void ConvertMcd(char *mcd, char *data) {
654 if (strstr(mcd, ".gme")) {
655 f = fopen(mcd, "wb");
657 fwrite(data - 3904, 1, MCD_SIZE + 3904, f);
660 f = fopen(mcd, "r+");
673 for (i = 0; i < 7; i++) {
686 while (s-- > (MCD_SIZE+1)) fputc(0, f);
688 } else if(strstr(mcd, ".mem") || strstr(mcd,".vgs")) {
689 f = fopen(mcd, "wb");
691 fwrite(data-64, 1, MCD_SIZE+64, f);
694 f = fopen(mcd, "r+");
708 while (s-- > (MCD_SIZE+1)) fputc(0, f);
711 f = fopen(mcd, "wb");
713 fwrite(data, 1, MCD_SIZE, f);
719 void GetMcdBlockInfo(int mcd, int block, McdBlock *Info) {
720 char *data = NULL, *ptr, *str, *sstr;
721 unsigned short clut[16];
725 memset(Info, 0, sizeof(McdBlock));
727 if (mcd != 1 && mcd != 2)
730 if (McdDisable[mcd - 1])
733 if (mcd == 1) data = Mcd1Data;
734 if (mcd == 2) data = Mcd2Data;
736 ptr = data + block * 8192 + 2;
738 Info->IconCount = *ptr & 0x3;
747 for (i = 0; i < 48; i++) {
752 // Convert ASCII characters to half-width
753 if (c >= 0x8281 && c <= 0x829A)
754 c = (c - 0x8281) + 'a';
755 else if (c >= 0x824F && c <= 0x827A)
756 c = (c - 0x824F) + '0';
757 else if (c == 0x8140) c = ' ';
758 else if (c == 0x8143) c = ',';
759 else if (c == 0x8144) c = '.';
760 else if (c == 0x8146) c = ':';
761 else if (c == 0x8147) c = ';';
762 else if (c == 0x8148) c = '?';
763 else if (c == 0x8149) c = '!';
764 else if (c == 0x815E) c = '/';
765 else if (c == 0x8168) c = '"';
766 else if (c == 0x8169) c = '(';
767 else if (c == 0x816A) c = ')';
768 else if (c == 0x816D) c = '[';
769 else if (c == 0x816E) c = ']';
770 else if (c == 0x817C) c = '-';
773 sstr[x++] = *ptr++; sstr[x++] = *ptr++;
777 str[i] = sstr[x++] = c;
784 ptr = data + block * 8192 + 0x60; // icon palette data
786 for (i = 0; i < 16; i++) {
787 clut[i] = *((unsigned short *)ptr);
791 for (i = 0; i < Info->IconCount; i++) {
792 short *icon = &Info->Icon[i * 16 * 16];
794 ptr = data + block * 8192 + 128 + 128 * i; // icon data
796 for (x = 0; x < 16 * 16; x++) {
797 icon[x++] = clut[*ptr & 0xf];
798 icon[x] = clut[*ptr >> 4];
803 ptr = data + block * 128;
808 strncpy(Info->ID, ptr, 12);
810 strncpy(Info->Name, ptr, 16);
813 int sioFreeze(void *f, int Mode) {
814 gzfreeze(buf, sizeof(buf));
815 gzfreeze(&StatReg, sizeof(StatReg));
816 gzfreeze(&ModeReg, sizeof(ModeReg));
817 gzfreeze(&CtrlReg, sizeof(CtrlReg));
818 gzfreeze(&BaudReg, sizeof(BaudReg));
819 gzfreeze(&bufcount, sizeof(bufcount));
820 gzfreeze(&parp, sizeof(parp));
821 gzfreeze(&mcdst, sizeof(mcdst));
822 gzfreeze(&rdwr, sizeof(rdwr));
823 gzfreeze(&adrH, sizeof(adrH));
824 gzfreeze(&adrL, sizeof(adrL));
825 gzfreeze(&padst, sizeof(padst));