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 ***************************************************************************/
26 #include "psxcounters.h"
27 #include "psxevents.h"
31 #ifdef USE_LIBRETRO_VFS
32 #include <streams/file_stream_transforms.h>
38 #define TX_EMPTY 0x0004
39 #define PARITY_ERR 0x0008
40 #define RX_OVERRUN 0x0010
41 #define FRAMING_ERR 0x0020
42 #define SYNC_DETECT 0x0040
48 #define TX_PERM 0x0001
50 #define RX_PERM 0x0004
52 #define RESET_ERR 0x0010
54 #define SIO_RESET 0x0040
56 // *** FOR WORKS ON PADS AND MEMORY CARDS *****
58 static unsigned char buf[256];
59 static unsigned char cardh1[4] = { 0xff, 0x08, 0x5a, 0x5d };
60 static unsigned char cardh2[4] = { 0xff, 0x08, 0x5a, 0x5d };
62 // Transfer Ready and the Buffer is Empty
63 // static unsigned short StatReg = 0x002b;
64 static unsigned short StatReg = TX_RDY | TX_EMPTY;
65 static unsigned short ModeReg;
66 static unsigned short CtrlReg;
67 static unsigned short BaudReg;
69 static unsigned int bufcount;
70 static unsigned int parp;
71 static unsigned int mcdst, rdwr;
72 static unsigned char adrH, adrL;
73 static unsigned int padst;
75 char Mcd1Data[MCD_SIZE], Mcd2Data[MCD_SIZE];
79 // 4us * 8bits = (PSXCLK / 1000000) * 32; (linuzappz)
80 // TODO: add SioModePrescaler and BaudReg
81 #define SIO_CYCLES 535
83 void sioWrite8(unsigned char value) {
86 s32 framec = psxRegs.cycle - rcnts[3].cycleStart;
87 printf("%d:%03d sio write8 %04x %02x\n", frame_counter,
88 (s32)(framec / (PSXCLK / 60 / 263.0f)), CtrlReg, value);
92 if ((value & 0x40) == 0x40) {
94 switch (CtrlReg & 0x2002) {
96 buf[parp] = PAD1_poll(value, &more_data);
99 buf[parp] = PAD2_poll(value, &more_data);
105 set_event(PSXINT_SIO, SIO_CYCLES);
112 switch (CtrlReg & 0x2002) {
113 case 0x0002: buf[parp] = PAD1_poll(value, &more_data); break;
114 case 0x2002: buf[parp] = PAD2_poll(value, &more_data); break;
119 set_event(PSXINT_SIO, SIO_CYCLES);
126 set_event(PSXINT_SIO, SIO_CYCLES);
127 if (rdwr) { parp++; return; }
130 case 0x52: rdwr = 1; break;
131 case 0x57: rdwr = 2; break;
136 set_event(PSXINT_SIO, SIO_CYCLES);
144 set_event(PSXINT_SIO, SIO_CYCLES);
152 set_event(PSXINT_SIO, SIO_CYCLES);
160 switch (CtrlReg & 0x2002) {
162 memcpy(&buf[4], Mcd1Data + (adrL | (adrH << 8)) * 128, 128);
165 memcpy(&buf[4], Mcd2Data + (adrL | (adrH << 8)) * 128, 128);
171 for (i = 2; i < 128 + 4; i++)
191 if ((rdwr == 1 && parp == 132) ||
192 (rdwr == 2 && parp == 129)) {
193 // clear "new card" flags
194 if (CtrlReg & 0x2000)
200 if (parp < 128) buf[parp + 1] = value;
202 set_event(PSXINT_SIO, SIO_CYCLES);
207 case 0x01: // start pad
208 StatReg |= RX_RDY; // Transfer is Ready
210 switch (CtrlReg & 0x2002) {
211 case 0x0002: buf[0] = PAD1_startPoll(1); break;
212 case 0x2002: buf[0] = PAD2_startPoll(2); break;
217 set_event(PSXINT_SIO, SIO_CYCLES);
219 case 0x81: // start memcard
220 if (CtrlReg & 0x2000)
224 memcpy(buf, cardh2, 4);
230 memcpy(buf, cardh1, 4);
237 set_event(PSXINT_SIO, SIO_CYCLES);
249 void sioWriteStat16(unsigned short value) {
252 void sioWriteMode16(unsigned short value) {
256 void sioWriteCtrl16(unsigned short value) {
257 CtrlReg = value & ~RESET_ERR;
258 if (value & RESET_ERR) StatReg &= ~IRQ;
259 if ((CtrlReg & SIO_RESET) || !(CtrlReg & DTR)) {
260 padst = 0; mcdst = 0; parp = 0;
261 StatReg = TX_RDY | TX_EMPTY;
262 psxRegs.interrupt &= ~(1 << PSXINT_SIO);
266 void sioWriteBaud16(unsigned short value) {
270 unsigned char sioRead8() {
271 unsigned char ret = 0;
273 if ((StatReg & RX_RDY)/* && (CtrlReg & RX_PERM)*/) {
274 // StatReg &= ~RX_OVERRUN;
276 if (parp == bufcount) {
277 StatReg &= ~RX_RDY; // Receive is not Ready now
281 switch (CtrlReg & 0x2002) {
283 memcpy(Mcd1Data + (adrL | (adrH << 8)) * 128, &buf[1], 128);
284 SaveMcd(Config.Mcd1, Mcd1Data, (adrL | (adrH << 8)) * 128, 128);
287 memcpy(Mcd2Data + (adrL | (adrH << 8)) * 128, &buf[1], 128);
288 SaveMcd(Config.Mcd2, Mcd2Data, (adrL | (adrH << 8)) * 128, 128);
293 if (padst == 2) padst = 0;
302 s32 framec = psxRegs.cycle - rcnts[3].cycleStart;
303 printf("%d:%03d sio read8 %04x %02x\n", frame_counter,
304 (s32)((float)framec / (PSXCLK / 60 / 263.0f)), CtrlReg, ret);
309 unsigned short sioReadStat16() {
313 unsigned short sioReadMode16() {
317 unsigned short sioReadCtrl16() {
321 unsigned short sioReadBaud16() {
325 void sioInterrupt() {
327 PAD_LOG("Sio Interrupt (CP0.Status = %x)\n", psxRegs.CP0.n.Status);
329 // SysPrintf("Sio Interrupt\n");
330 if (!(StatReg & IRQ)) {
332 psxHu32ref(0x1070) |= SWAPu32(0x80);
336 void LoadMcd(int mcd, char *str) {
340 if (mcd != 1 && mcd != 2)
345 cardh1[1] |= 8; // mark as new
352 McdDisable[mcd - 1] = 0;
354 // memcard1 is handled by libretro
359 if (str == NULL || strcmp(str, "none") == 0) {
360 McdDisable[mcd - 1] = 1;
366 f = fopen(str, "rb");
368 SysPrintf(_("The memory card %s doesn't exist - creating it\n"), str);
370 f = fopen(str, "rb");
374 if (stat(str, &buf) != -1) {
375 if (buf.st_size == MCD_SIZE + 64)
376 fseek(f, 64, SEEK_SET);
377 else if(buf.st_size == MCD_SIZE + 3904)
378 fseek(f, 3904, SEEK_SET);
380 if (fread(data, 1, MCD_SIZE, f) != MCD_SIZE) {
382 SysPrintf(_("File IO error in <%s:%s>.\n"), __FILE__, __func__);
384 memset(data, 0x00, MCD_SIZE);
389 SysMessage(_("Memory card %s failed to load!\n"), str);
393 SysPrintf(_("Loading memory card %s\n"), str);
394 if (stat(str, &buf) != -1) {
395 if (buf.st_size == MCD_SIZE + 64)
396 fseek(f, 64, SEEK_SET);
397 else if(buf.st_size == MCD_SIZE + 3904)
398 fseek(f, 3904, SEEK_SET);
400 if (fread(data, 1, MCD_SIZE, f) != MCD_SIZE) {
402 SysPrintf(_("File IO error in <%s:%s>.\n"), __FILE__, __func__);
404 memset(data, 0x00, MCD_SIZE);
410 void LoadMcds(char *mcd1, char *mcd2) {
415 void SaveMcd(char *mcd, char *data, uint32_t adr, int size) {
418 if (mcd == NULL || *mcd == 0 || strcmp(mcd, "none") == 0)
421 f = fopen(mcd, "r+b");
425 if (stat(mcd, &buf) != -1) {
426 if (buf.st_size == MCD_SIZE + 64)
427 fseek(f, adr + 64, SEEK_SET);
428 else if (buf.st_size == MCD_SIZE + 3904)
429 fseek(f, adr + 3904, SEEK_SET);
431 fseek(f, adr, SEEK_SET);
433 fseek(f, adr, SEEK_SET);
435 fwrite(data + adr, 1, size, f);
441 // try to create it again if we can't open it
442 f = fopen(mcd, "wb");
444 fwrite(data, 1, MCD_SIZE, f);
449 ConvertMcd(mcd, data);
452 void CreateMcd(char *mcd) {
458 f = fopen(mcd, "wb");
460 SysPrintf("CreateMcd: couldn't open %s\n", mcd);
464 if (stat(mcd, &buf) != -1) {
465 if ((buf.st_size == MCD_SIZE + 3904) || strstr(mcd, ".gme")) {
489 for (i = 0; i < 7; i++) {
503 for (i = 0; i < 14; i++) {
510 while (s-- > (MCD_SIZE + 1))
512 } else if ((buf.st_size == MCD_SIZE + 64) || strstr(mcd, ".mem") || strstr(mcd, ".vgs")) {
522 for (i = 0; i < 3; i++) {
535 while (s-- > (MCD_SIZE + 1))
543 while (s-- > (MCD_SIZE - 127))
548 for (i = 0; i < 15; i++) { // 15 blocks
569 for (j = 0; j < 117; j++) {
577 for (i = 0; i < 20; i++) {
598 for (j = 0; j < 118; j++) {
610 void ConvertMcd(char *mcd, char *data) {
615 if (strstr(mcd, ".gme")) {
616 f = fopen(mcd, "wb");
618 fwrite(data - 3904, 1, MCD_SIZE + 3904, f);
621 f = fopen(mcd, "r+");
622 if (f == NULL) return;
635 for (i = 0; i < 7; i++) {
648 while (s-- > (MCD_SIZE+1)) fputc(0, f);
650 } else if(strstr(mcd, ".mem") || strstr(mcd,".vgs")) {
651 f = fopen(mcd, "wb");
653 fwrite(data-64, 1, MCD_SIZE+64, f);
656 f = fopen(mcd, "r+");
657 if (f == NULL) return;
671 while (s-- > (MCD_SIZE+1)) fputc(0, f);
674 f = fopen(mcd, "wb");
676 fwrite(data, 1, MCD_SIZE, f);
682 void GetMcdBlockInfo(int mcd, int block, McdBlock *Info) {
683 unsigned char *data = NULL, *ptr;
685 unsigned short clut[16];
689 memset(Info, 0, sizeof(McdBlock));
691 if (mcd != 1 && mcd != 2)
694 if (McdDisable[mcd - 1])
697 if (mcd == 1) data = (unsigned char *)Mcd1Data;
698 if (mcd == 2) data = (unsigned char *)Mcd2Data;
700 ptr = data + block * 8192 + 2;
702 Info->IconCount = *ptr & 0x3;
712 for (i = 0; i < 48; i++) {
717 // Convert ASCII characters to half-width
718 if (c >= 0x8281 && c <= 0x829A)
719 c = (c - 0x8281) + 'a';
720 else if (c >= 0x824F && c <= 0x827A)
721 c = (c - 0x824F) + '0';
722 else if (c == 0x8140) c = ' ';
723 else if (c == 0x8143) c = ',';
724 else if (c == 0x8144) c = '.';
725 else if (c == 0x8146) c = ':';
726 else if (c == 0x8147) c = ';';
727 else if (c == 0x8148) c = '?';
728 else if (c == 0x8149) c = '!';
729 else if (c == 0x815E) c = '/';
730 else if (c == 0x8168) c = '"';
731 else if (c == 0x8169) c = '(';
732 else if (c == 0x816A) c = ')';
733 else if (c == 0x816D) c = '[';
734 else if (c == 0x816E) c = ']';
735 else if (c == 0x817C) c = '-';
739 sstr[x++] = *ptr++; sstr[x++] = *ptr++;
744 str[s++] = sstr[x++] = c;
751 ptr = data + block * 8192 + 0x60; // icon palette data
752 memcpy(clut, ptr, 16*2);
754 for (i = 0; i < Info->IconCount; i++) {
755 short *icon = &Info->Icon[i * 16 * 16];
757 ptr = data + block * 8192 + 128 + 128 * i; // icon data
759 for (x = 0; x < 16 * 16; ) {
760 icon[x++] = clut[*ptr & 0xf];
761 icon[x++] = clut[*ptr >> 4];
766 ptr = data + block * 128;
771 strncpy(Info->ID, (char *)ptr, 12);
773 strncpy(Info->Name, (char *)ptr, 16);
776 int sioFreeze(void *f, int Mode) {
777 gzfreeze(buf, sizeof(buf));
778 gzfreeze(&StatReg, sizeof(StatReg));
779 gzfreeze(&ModeReg, sizeof(ModeReg));
780 gzfreeze(&CtrlReg, sizeof(CtrlReg));
781 gzfreeze(&BaudReg, sizeof(BaudReg));
782 gzfreeze(&bufcount, sizeof(bufcount));
783 gzfreeze(&parp, sizeof(parp));
784 gzfreeze(&mcdst, sizeof(mcdst));
785 gzfreeze(&rdwr, sizeof(rdwr));
786 gzfreeze(&adrH, sizeof(adrH));
787 gzfreeze(&adrL, sizeof(adrL));
788 gzfreeze(&padst, sizeof(padst));