Example #1
0
    private static bool Transfer()
    {
        PrintUsage(true);


        activeSerial = new SerialPort(argComPort, 115200, Parity.None, 8, StopBits.Two);
        // Required for e.g. SharkLink & Yaroze cable compat. Doesn't interfere with the 3-wire setups
        activeSerial.Handshake = Handshake.None;
        activeSerial.DtrEnable = true;
        activeSerial.RtsEnable = true;

        if (fastMode)
        {
            activeSerial.ReadTimeout  = TIMEOUT;
            activeSerial.WriteTimeout = TIMEOUT;

            try {
                activeSerial.Open();
            } catch (Exception exception) {
                Console.WriteLine("Error opening temporary serial port on " + argComPort + "!");
                Console.WriteLine(exception.Message);

                return(false);
            }

            // The bytes "FAST" with no null terminator
            activeSerial.Write(BitConverter.GetBytes(0x54534146), 0, 4);


            Thread.Sleep(100);


            activeSerial.Close();

            // We need to find a suitable overlap in frequencies which can be divided
            // from the Playstation's clock and from the FTDI/clone, with some wiggle room.
            //
            // PSX/libs uses whole integer divisions of 2073600 for anything over 115200
            // giving us 518400 close to the half-megabyte mark.
            //
            // Most FTDI/clones seem to operate in 2xinteger divisions of 48mhz
            // giving us 510000 or 521000 close to the half-megabyte mark. e.g. (48m/47/2) or (48m/46/2)
            //
            // 5210000 (and even 518400) is a little fast, but the lower end
            // of things (510000 to 518300) seems pretty stable.
            //
            // note: psx @ 518400, pc @ 510000
            //
            activeSerial = new SerialPort(argComPort, 510000, Parity.None, 8, StopBits.Two);
            // Required for e.g. SharkLink & Yaroze cable compat. Doesn't interfere with the 3-wire setups
            activeSerial.Handshake = Handshake.None;
            activeSerial.DtrEnable = true;
            activeSerial.RtsEnable = true;
        }

        // Now the main serial port

        activeSerial.ReadTimeout  = TIMEOUT;
        activeSerial.WriteTimeout = TIMEOUT;

        try {
            activeSerial.Open();
        } catch (Exception exception) {
            Console.WriteLine("Error opening the serial port on " + argComPort + "!");
            Console.WriteLine(exception.Message);

            return(false);
        }



        // just lets us skip a ton of ifs
        if (monitorComms && argCommand == CommandMode.NOT_SET)
        {
            TransferLogic.DoMonitor();
            return(true);
        }


        if (usingCachedComPort)
        {
            Console.WriteLine("Using port " + argComPort + " from comport.txt\n");
        }

        // Clear the SIO buffer incase the last program has been spamming

        Console.WriteLine("Emptying buffer... ");
        while (activeSerial.BytesToRead != 0)
        {
            Console.Write("" + (char)activeSerial.ReadByte());
        }
        Console.WriteLine("...done!\n\n");


        if (argCommand == CommandMode.SEND_EXE)
        {
            TransferLogic.Command_SendEXE(argAddr, inFile, CalculateChecksum(inFile, true));
        }

        if (argCommand == CommandMode.SEND_BIN)
        {
            TransferLogic.Command_SendBin(argAddr, inFile, CalculateChecksum(inFile));
        }

        // Unirom 8 mode - requires a response after checking that
        // things will fit on the cart.
        if (argCommand == CommandMode.SEND_ROM)
        {
            TransferLogic.Command_SendROM(argAddr, inFile, CalculateChecksum(inFile));
        }


        if (argCommand == CommandMode.RESET)
        {
            TransferLogic.WriteChallenge(argCommand.challenge());
        }

        // Unirom 8.0.4 and up, enables kernel-resident SIO
        if (argCommand == CommandMode.DEBUG)
        {
            // if it returns true, we might enter /m (monitor) mode, etc
            if (
                !TransferLogic.ChallengeResponse(argCommand)
                )
            {
                return(false);
            }
        }


        if (argCommand == CommandMode.DUMP)
        {
            lastReadBytes = new byte[argSize];

            TransferLogic.Command_DumpBytes(argAddr, argSize, lastReadBytes);

            string fileName = "DUMP_" + argAddr.ToString("X8") + "_to_" + argSize.ToString("X8") + ".bin";

            if (System.IO.File.Exists(fileName))
            {
                string newFilename = fileName + GetSpan().TotalSeconds.ToString();

                Console.Write("\n\nWARNING: Filename " + fileName + " already exists! - Dumping to " + newFilename + " instead!\n\n");

                fileName = newFilename;
            }

            try{
                File.WriteAllBytes(fileName, lastReadBytes);
            } catch (Exception e) {
                Error("Couldn't write to the output file + " + fileName + " !\nThe error returned was: " + e, false);
                return(false);
            }
        }         // DUMP

        if (argCommand == CommandMode.PING)
        {
            TransferLogic.ChallengeResponse(argCommand);
        }

        if (argCommand == CommandMode.GDB)
        {
            GDB.Init();
        }

        if (argCommand == CommandMode.JUMP_JMP)
        {
            TransferLogic.Command_JumpAddr(argAddr);
        }

        if (argCommand == CommandMode.JUMP_CALL)
        {
            TransferLogic.Command_CallAddr(argAddr);
        }

        if (argCommand == CommandMode.HALT)
        {
            TransferLogic.ChallengeResponse(argCommand);
        }

        if (argCommand == CommandMode.CONT)
        {
            TransferLogic.ChallengeResponse(argCommand);
        }

        if (argCommand == CommandMode.REGS)
        {
            TransferLogic.Command_DumpRegs();
        }

        if (argCommand == CommandMode.SETREG)
        {
            TransferLogic.Command_SetReg(argRegister, argAddr);
        }

        if (argCommand == CommandMode.WATCH)
        {
            TransferLogic.Watch(argAddr, argSize);
            return(true);
        }         // WATCH

        if (monitorComms)
        {
            TransferLogic.DoMonitor();
        }
        else
        {
            Console.WriteLine("\n This is where we part ways!");
            activeSerial.Close();
        }

        return(true);
    }     // void Transfer