Ejemplo n.º 1
0
        private void Form1_Load(object sender, EventArgs e)
        {
            var sna = new SNAFile(File.ReadAllBytes(@"D:\GOG Games\Rayman 2\Data\World\Levels\Learn_30\Learn_30.sna"));

            richTextBox1.AppendText(sna.ToString());

            //Close();
        }
Ejemplo n.º 2
0
            public static SNAFile Load(string FileName)
            {
                byte[] bytes;

                try
                {
                    bytes = File.ReadAllBytes(FileName);
                }
                catch { return(null); }

                if (bytes.Length != (48 * 1024) + 27)
                {
                    return(null);
                }

                SNAFile file = new SNAFile();

                Buffer.BlockCopy(bytes, 0, file.Header, 0, 27);
                Buffer.BlockCopy(bytes, 27, file.Data, 16384, 48 * 1024);

                return(file);
            }
Ejemplo n.º 3
0
        public ZXSerialLoaderResult LoadFile(string SerialPort, string FileName, int BlockSize, Action <int> Progress)
        {
            string ext = Path.GetExtension(FileName).ToLower();

            SpectrumFile program;

            switch (ext)
            {
            case ".hex":
                program = HEXFile.Load(FileName);
                break;

            case ".sna":
                program = SNAFile.Load(FileName);
                break;

            case ".z80":
                program = Z80File.Load(FileName);
                break;

            case ".tap":
                program = TAPFile.Load(FileName);
                break;

            default:
                return(ZXSerialLoaderResult.Unsupported);
            }

            if (program == null)
            {
                return(ZXSerialLoaderResult.FileError);
            }

            try
            {
                string dvResponse;

                using (SerialPort serial = new SerialPort(SerialPort, serialSpeed, Parity.None, 8, StopBits.One))
                {
                    serial.Open();

                    serial.ReadTimeout  = 5000;
                    serial.WriteTimeout = 5000;

                    serial.Write(program.Operation);

                    if ((dvResponse = serial.ReadLine()) != "RDY")
                    {
                        return(ZXSerialLoaderResult.UnknownResponse);
                    }

                    if (program.Header != null)
                    {
                        serial.Write(program.Header, 0, program.Header.Length);
                    }

                    int  pos      = program.StartAddress;
                    bool finished = false;

                    double passPercent    = BlockSize * 100 / (program.EndAddress - program.StartAddress);
                    double currentPercent = 0;

                    while (pos < program.EndAddress)
                    {
                        int segLen = Math.Min(BlockSize, program.EndAddress - pos);

                        byte[] tmpBuffer = new byte[2];

                        if (pos + segLen >= program.EndAddress) //Is last segment?
                        {
                            finished     = true;
                            tmpBuffer[0] = 1;
                        }

                        serial.Write(tmpBuffer, 0, 1);  //Send last segment

                        tmpBuffer[0] = (byte)(pos & 0xFF);
                        tmpBuffer[1] = (byte)((pos >> 8) & 0xFF);
                        serial.Write(tmpBuffer, 0, 2); //Send segment address


                        tmpBuffer[0] = (byte)(segLen & 0xFF);
                        tmpBuffer[1] = (byte)((segLen >> 8) & 0xFF);
                        serial.Write(tmpBuffer, 0, 2); //Send segment size


                        if ((dvResponse = serial.ReadLine()) != "OK")
                        {
                            return(ZXSerialLoaderResult.UnknownResponse);
                        }

                        //Send segment
                        serial.Write(program.Data, pos, segLen);

                        //Wait for acknowledge
                        if (!finished)
                        {
                            if ((dvResponse = serial.ReadLine()) != "NEXT")
                            {
                                return(ZXSerialLoaderResult.UnknownResponse);
                            }
                        }

                        currentPercent += passPercent;

                        if (Progress != null)
                        {
                            Progress((int)Math.Min(100, currentPercent));
                        }

                        pos += segLen;
                    }

                    if (Progress != null)
                    {
                        Progress(1000);
                    }

                    Console.WriteLine(serial.ReadLine());
                    if (Progress != null)
                    {
                        Progress(2000);
                    }

                    Console.WriteLine(serial.ReadLine());
                    if (Progress != null)
                    {
                        Progress(3000);
                    }

                    serial.Close();

                    return(ZXSerialLoaderResult.Success);
                }
            }
            catch
            {
                return(ZXSerialLoaderResult.SerialPortError);
            }
        }