public static bool SendNext(SerialPort mySerialPort, string file) { using (BinaryReader br = new BinaryReader(File.Open(file, FileMode.Open))) { List <byte> b = new List <byte>(); NexReader nex = new NexReader(br); NexReader.Header header = nex.ReadHeader(); Console.WriteLine("Opening nex file = " + header.Next + " " + header.VersionNumber); Console.WriteLine("PC Address : 0x" + header.PC.ToString("X")); Console.WriteLine("SP Address : 0x" + header.SP.ToString("X")); AddCommand(ref b, 186); SendData(mySerialPort, ref b); Thread.Sleep(50); for (int i = 0; i < header.Banks.Length; i++) { int bank = GetRealBank(i); if (header.Banks[bank] != 0) { bank *= 2; for (int j = 0; j < 2; j++) { Console.WriteLine("Sending 8k mmu bank : " + bank); //AddCommand(ref b,180); //Add8Value(ref b,7); //Add8Value(ref b,bank); //SendData(ref b); AddCommand(ref b, 181); Add8Value(ref b, bank); Add16Value(ref b, 0xE000); Add16Value(ref b, 8192); byte checksum = nex.AddData(ref b, 8192); SendData(mySerialPort, ref b); //int cs = ReadByteData(); int ok = ReadByteData(mySerialPort); int clo = ReadByteData(mySerialPort); int chi = ReadByteData(mySerialPort); //if (cs != checksum) //{ // Console.WriteLine("Checksum Error "+checksum+" != "+cs); // throw new Exception(); //} if (clo != 0 || chi != 0) { Console.WriteLine("Send Error "); throw new Exception(); } if (ok != 0) { Console.WriteLine("Timeout Error!"); throw new Exception(); } bank++; } } } Console.WriteLine("Setting entry bank to " + header.EntryBank); AddCommand(ref b, 180); Add8Value(ref b, 6); Add8Value(ref b, header.EntryBank); //SendData(ref b); AddCommand(ref b, 180); Add8Value(ref b, 7); Add8Value(ref b, header.EntryBank + 1); SendData(mySerialPort, ref b); AddCommand(ref b, 182); //execute! Add16Value(ref b, header.SP); Add16Value(ref b, header.PC); SendData(mySerialPort, ref b); Console.WriteLine("Finished!"); } return(true); }
// ------------------------------------------------------------------------------------------------- // Main entry-point for this application // // \param args An array of command-line argument strings. // ------------------------------------------------------------------------------------------------- static void Main(string[] args) { Console.WriteLine("NDS SendNex by C.Kirby 0.1"); OptionSet p = new OptionSet { { "c|com=", "com port", v => com = v }, { "s|speed=", "Serial Speed e.g 115200,1958400 etc Defaults to 1958400", v => speed = int.Parse(v) }, { "f|file=", "nex filename", v => file = v }, { "h|help", "show this message and exit", v => show_help = v != null } }; List <string> extra; try { extra = p.Parse(args); } catch (OptionException e) { Console.WriteLine(e.Message); Console.WriteLine("Try `SendNex --help' for more information."); return; } if (show_help) { ShowHelp(p); return; } mySerialPort = new SerialPort(com, speed, Parity.None, 8, StopBits.One); mySerialPort.Open(); Console.WriteLine("Write Buffer=" + mySerialPort.WriteBufferSize + " | Read Buffer=" + mySerialPort.ReadBufferSize); if (!mySerialPort.IsOpen) { Console.WriteLine("Failed to open port."); return; } Stopwatch sw = new Stopwatch(); sw.Restart(); //So you dont have to call sw.Reset() NexReader.SendNext(mySerialPort, file); sw.Stop(); Thread.Sleep(10); mySerialPort.Close(); Console.WriteLine("Sent " + (bytesent / 1024) + "k in " + (sw.ElapsedMilliseconds / 1000.0f) + " seconds"); }