static int doExtensionMcp23s17 (char *progName, int pinBase, char *params) { int spi, port ; if ((params = extractInt (progName, params, &spi)) == NULL) return FALSE ; if ((spi < 0) || (spi > 1)) { verbError ("%s: SPI address (%d) out of range", progName, spi) ; return FALSE ; } if ((params = extractInt (progName, params, &port)) == NULL) return FALSE ; if ((port < 0) || (port > 7)) { verbError ("%s: port address (%d) out of range", progName, port) ; return FALSE ; } mcp23s17Setup (pinBase, spi, port) ; return TRUE ; }
static int doModuleMcp23s17 (char *progName, int pinBase, char *params) { int spi, port ; // Extract the SPI address: if (*params != ':') { fprintf (stderr, "%s: colon expected after pin-base number\n", progName) ; return FALSE ; } ++params ; if (!isdigit (*params)) { fprintf (stderr, "%s: digit expected after pin-base number\n", progName) ; return FALSE ; } spi = *params - '0' ; if ((spi < 0) || (spi > 1)) { fprintf (stderr, "%s: SPI address (%d) out of range\n", progName, spi) ; return FALSE ; } // Extract the port: if (*++params != ':') { fprintf (stderr, "%s: colon expected after SPI address\n", progName) ; return FALSE ; } ++params ; if (!isdigit (*params)) { fprintf (stderr, "%s: digit expected after SPI address\n", progName) ; return FALSE ; } port = strtol (params, NULL, 0) ; if ((port < 0) || (port > 7)) { fprintf (stderr, "%s: port address (%d) out of range\n", progName, port) ; return FALSE ; } mcp23s17Setup (pinBase, spi, port) ; return TRUE ; }
int piFaceSetup (const int pinBase) { int i ; struct wiringPiNodeStruct *node ; // Create an mcp23s17 instance: mcp23s17Setup (pinBase + 16, 0, 0) ; // Set the direction bits for (i = 0 ; i < 8 ; ++i) { pinMode (pinBase + 16 + i, OUTPUT) ; // Port A is the outputs pinMode (pinBase + 16 + 8 + i, INPUT) ; // Port B inputs. } node = wiringPiNewNode (pinBase, 16) ; node->digitalRead = myDigitalRead ; node->digitalWrite = myDigitalWrite ; node->pullUpDnControl = myPullUpDnControl ; return 0 ; }
int main(void){ printf("START\n"); int readCart = 1; uint8_t ROMmakeup; uint8_t ROMspeed; uint8_t bankSize; uint8_t ROMtype; uint8_t ROMsize; uint8_t SRAMsize; uint8_t country; uint8_t license; uint8_t version; uint32_t VBLvector; uint32_t resetVector; uint32_t inverseChecksum; int i = 0; int x = 0; // ------------- Set Registers ----------------------------------------------------- wiringPiSetup () ; mcp23s17Setup (BASE, 0, _IOControls) ; mcp23s17Setup (BASE+100, 0,_SNESAddressPins) ; mcp23s17Setup (BASE+200, 0, _SNESBankAndData) ; initInterface_SPI(); //---------------------------------------------------------------------------------------------------- /* # GPA0: /RD # GPA1: /RESET # GPA2: /WR # GPA3: /CS # GPA4: CART MOSFET # GPA7: /IRQ */ setIOControl(_RD + _CS + _POWER); //time.sleep(.25) //----------------------------------------------------- char cartname[21] = ""; uint32_t headerAddr =32704; int isLowROM = 1; int isValid = 0; if (compareROMchecksums(32704,1) == 1){ printf("Checksums matched\n"); ROMmakeup = readOffset(headerAddr + 21,isLowROM); ROMspeed = getUpNibble(ROMmakeup); bankSize = getLowNibble(ROMmakeup); if (bankSize == 0){ printf("ROM Makeup match for LoROM. Assuming this is the case!\n"); isLowROM = 1; isValid = 1; } else if (bankSize == 1){ printf("ROM Makeup match for HiROM. Assuming this is the case!\n"); headerAddr = 65472; isLowROM = 0; isValid = 1; } else printf("Bank Configuration Read Error\n"); } else printf("Checksums did not match. Either no cart, or cart read error\n"); //#--- Debug. Manually set bank size ---------- //#isLowROM = 1 //#------------------------------------------- uint32_t currentAddr = headerAddr; gotoOffset(headerAddr, isLowROM); for (i = headerAddr; i <(headerAddr + 20); i++ ) cartname[x++] = readOffset(i,isLowROM); //cartname = cartname.rstrip() ROMmakeup = readAddr(headerAddr + 21,isLowROM); ROMspeed = getUpNibble(ROMmakeup); bankSize = getLowNibble(ROMmakeup); ROMtype = readAddr(headerAddr + 22,isLowROM); ROMsize = getROMsize(headerAddr + 23, isLowROM); SRAMsize = readAddr(headerAddr + 24,isLowROM); country = readAddr(headerAddr + 25,isLowROM); license = readAddr(headerAddr + 26,isLowROM); version = readAddr(headerAddr + 27,isLowROM); currentAddr = headerAddr + 28; inverseChecksum = readAddr(currentAddr,isLowROM); inverseChecksum += readAddr(currentAddr+1,isLowROM) * 256; currentAddr = headerAddr + 30; ROMchecksum = readAddr(currentAddr,isLowROM); ROMchecksum += readAddr(currentAddr+1,isLowROM) * 256; currentAddr = headerAddr + 32; VBLvector = readAddr(currentAddr,isLowROM); VBLvector += readAddr(currentAddr+1,isLowROM) * 256; currentAddr = headerAddr + 34; resetVector = readAddr(currentAddr,isLowROM); resetVector += readAddr(currentAddr+1,isLowROM) * 256; int16_t numberOfPages = getNumberOfPages(ROMsize,isLowROM); printf("Game Title: %s\n", cartname); printf("ROM Makeup: %d\n", ROMmakeup); printf(" - ROM Speed: %d\n", ROMspeed); printf(" - Bank Size: %d\n", bankSize); printf("ROM Type: %d\n", ROMtype); if (ROMtype == 243){ printf("\nCapcom CX4 ROM Type detected!"); CX4setROMsize(ROMsize); printf("\n"); } printf("ROM Size: %d MBits\n", ROMsize); int convertedSRAMsize = 0; printf("SRAM Size: Value: %d",SRAMsize); if (convertedSRAMsize == 0) if (SRAMsize <= 12 && SRAMsize > 0) convertedSRAMsize = 1<<(SRAMsize +3); printf(" | %d KBits\n", convertedSRAMsize); printf("Country: %d\n", country); printf( "License: %d\n", license); printf( "Version: 1.%d\n",version); printf( "Inverse Checksum: %x\n", inverseChecksum); printf( "ROM Checksum: %x\n", + ROMchecksum); printf( " - Checksums xOr'ed: %x\n", (inverseChecksum | ROMchecksum) ); printf( "\n"); printf( "VBL Vector: %d\n", VBLvector); printf( "Reset Vector: %d\n", resetVector); printf( "\n"); printf( "Number of pages: %d\n", numberOfPages ); printf( "\n"); uint8_t *dump; char fileName[30]; //dump = returnNULLheader() int y = 0; uint32_t sizeOfCartInBytes = 0; uint32_t pageChecksum = 0; //uint32_t totalChecksum = 0; uint32_t currentByte = 0; uint32_t numberOfRemainPages = 0; uint32_t firstNumberOfPages = 0; time_t timeStart = 0; time_t timeEnd = 0; /* if directory != "" : if directory[len(directory)-1] != "/": directory += "/" g = open("/tmp/insertedCart",'w')*/ if (isValid == 1){ //g.write(cartname) if (readCart == 1){ //if os.path.exists(directory + cartname + '.smc'){ // printf("Cart has already been ripped, not ripping again!"); // readCart = 0; } else if (readCart == 0) printf("Will not rip cart due to OPTs"); if (readCart == 1){ numberOfRemainPages = 0; firstNumberOfPages = numberOfPages; timeStart = time(NULL); //f = open(directory + cartname + '.smc','w') FILE *romfile; stpcpy(fileName, cartname); strcat(fileName, ".smc"); romfile = fopen( fileName, "wb"); if (isLowROM == 1){ sizeOfCartInBytes = numberOfPages * 32768; dump = calloc(sizeOfCartInBytes, sizeof(uint8_t) ); printf("Reading %d Low ROM pages.\n", numberOfPages); //ROM Ripper ripROM(0x00, isLowROM, firstNumberOfPages, dump); } else{ sizeOfCartInBytes = numberOfPages * 65536; dump = calloc(sizeOfCartInBytes, sizeof(uint8_t) ); if (numberOfPages > 64){ numberOfRemainPages = ( numberOfPages - 64 ); //# number of pages over 64 printf("Reading first 64 of %d Hi ROM pages.\n", numberOfPages); firstNumberOfPages = 64; } else printf("Reading %d Hi ROM pages.\n", numberOfPages); /*dump =*/ ripROM(0xC0, isLowROM, firstNumberOfPages, dump); if (numberOfRemainPages > 0){ printf("Reading last %d of High ROM pages.\n", numberOfRemainPages); /*dump +=*/ ripROM(0x40, isLowROM, numberOfRemainPages, dump); } } printf("\n"); printf("Entire Checksum: %x\n", totalChecksum); printf("\n"); printf("Header Checksum: %x\n", ROMchecksum ); totalChecksum = ( totalChecksum & 0xFFFF ); printf("16-bit Generated Checksum: %x\n", totalChecksum); if (totalChecksum == ROMchecksum) printf("-------------------------- CHECKSUMS MATCH!\n"); else printf("----------WARNING: CHECKSUMS DO NOT MATCH: %x != %x\n", totalChecksum, ROMchecksum); timeEnd = time(NULL); //print "" printf("Address Writes - LowByte: %d HighByte: %d | Bank Writes: %d | Data Reads: %d\n", LowByteWrites, HighByteWrites, BankWrites, DataReads); printf("\nIt took %d seconds to read cart\n", timeEnd - timeStart); printf("Size of Cart in Bytes: %d\n", sizeOfCartInBytes); fwrite(dump, sizeof(uint8_t), sizeOfCartInBytes, romfile); fclose(romfile); free(dump); } /*if (readSRAM == 1){ f = open(directory + cartname + '.srm','w') timeStart = time.time() dump = ripSRAM(convertedSRAMsize,ROMsize,isLowROM) timeEnd = time.time() print "" print "It took " + str(timeEnd - timeStart) + "seconds to read SRAM Data" f.write(dump) f.close }*/ } else{ //g.write("NULL") //g.close } //#--- Clean Up & End Script ------------------------------------------------------ shutdownInterface_SPI(); }