Ejemplo n.º 1
0
        private byte CalculateCheckSum(string s)
        {
            UInt32 CalcCS = 0;

            for (int i = 0; i < s.Length; i += 2)
            {
                CalcCS += HexStringConverter.HS2toByte(s.Substring(i, 2));
            }
            return((Byte)((~CalcCS + 1) & 0x000000FF));
        }
Ejemplo n.º 2
0
        private void ReadHex(Object sender, DoWorkEventArgs args)
        {
            List <MemoryRegion> regions = (List <MemoryRegion>)(((object[])(args.Argument))[0]);
            string path = (string)(((object[])(args.Argument))[1]);



            IDictionary <uint, byte[]> readedData = new Dictionary <uint, byte[]>();

            StreamReader file = new StreamReader(path);

            try
            {
                int  stringNum = 0;
                uint highAddr  = 0;
                while (!file.EndOfStream)
                {
                    stringNum++;
                    string sourse = file.ReadLine();
                    sourse = sourse.Trim();
                    if (!((sourse.Substring(0, 1)).Contains(":")) || (sourse.Length < 3))
                    {
                        throw new InvalidDataException(" File : " + path + " Symbol ':' not found in string " + stringNum.ToString());
                    }
                    try
                    {
                        byte   dataSize   = HexStringConverter.HS2toByte(sourse.Substring(1, 2));
                        ushort lowAddr    = HexStringConverter.HS4toUInt16(sourse.Substring(3, 4));
                        byte   recordType = HexStringConverter.HS2toByte(sourse.Substring(7, 2));
                        byte   checkSum   = HexStringConverter.HS2toByte(sourse.Substring(sourse.Length - 2, 2));

                        uint CalcCS = 0;
                        for (int i = 0; i < (dataSize + 4); i++)
                        {
                            CalcCS += HexStringConverter.HS2toByte(sourse.Substring(i * 2 + 1, 2));
                        }
                        CalcCS = (~CalcCS + 1) & 0x000000FF;
                        if (CalcCS != (uint)checkSum)
                        {
                            throw new InvalidDataException(" File : " + path + " Check sum error in string " + stringNum.ToString());
                        }
                        byte[] data = new byte[dataSize];

                        switch (recordType)
                        {
                        case 0x04:
                            if (dataSize == 1)
                            {
                                highAddr = ((uint)(HexStringConverter.HS2toByte(sourse.Substring(9, 2)))) << 16;
                            }
                            else if (dataSize == 2)
                            {
                                highAddr = ((uint)(HexStringConverter.HS4toUInt16(sourse.Substring(9, 4)))) << 16;
                            }
                            else
                            {
                                throw new InvalidDataException(" File : " + path + " Invalid high address record in string " + stringNum.ToString());
                            }
                            break;

                        case 0x01:
                            break;

                        case 0x00:
                            if (dataSize > 0)
                            {
                                for (int i = 0; i < dataSize; i++)
                                {
                                    data[i] = HexStringConverter.HS2toByte(sourse.Substring(9 + i * 2, 2));
                                }
                                readedData.Add(highAddr + HexStringConverter.HS4toUInt16(sourse.Substring(3, 4)), data);
                            }
                            break;
                        }
                    }
                    catch (KeyNotFoundException)
                    {
                        throw new InvalidDataException(" File : " + path + " Incorrect symbol found in string " + stringNum.ToString());
                    }
                }

                foreach (KeyValuePair <uint, byte[]> line in readedData)
                {
                    foreach (MemoryRegion region in regions)
                    {
                        if (region.WriteData(line.Key, line.Value) > 0)
                        {
                            break;
                        }
                    }
                }
            }
            finally
            {
                file.Close();
            }
            args.Result = args.Argument;
        }
Ejemplo n.º 3
0
        public void Read(string path, List <MemoryRegionInfo> regionsInfo, Action <FileWorkerIOCompleteInfo> complete)
        {
            AsyncOperation asyncOp = AsyncOperationManager.CreateOperation(new Guid());

            IDictionary <string, Action> readers = new Dictionary <string, Action>();

            FileWorkerIOCompleteInfo completeInfo = new FileWorkerIOCompleteInfo();



            completeInfo.RegionsInfo = regionsInfo;
            completeInfo.Path        = path;

            List <MemoryRegion> regions = new List <MemoryRegion>();

            foreach (MemoryRegionInfo info in regionsInfo)
            {
                regions.Add(new MemoryRegion(info));
            }

            readers.Add("hex",
                        #region hexdelegate
                        delegate()
            {
                IDictionary <uint, byte[]> readedData = new Dictionary <uint, byte[]>();

                using (StreamReader file = new StreamReader(path))
                {
                    int stringNum = 0;
                    uint highAddr = 0;
                    while (!file.EndOfStream)
                    {
                        stringNum++;
                        string sourse = file.ReadLine();
                        sourse        = sourse.Trim();
                        if (!((sourse.Substring(0, 1)).Contains(":")) || (sourse.Length < 3))
                        {
                            throw new InvalidDataException(" File : " + path + " Symbol ':' not found in string " + stringNum.ToString());
                        }
                        try
                        {
                            byte dataSize   = HexStringConverter.HS2toByte(sourse.Substring(1, 2));
                            ushort lowAddr  = HexStringConverter.HS4toUInt16(sourse.Substring(3, 4));
                            byte recordType = HexStringConverter.HS2toByte(sourse.Substring(7, 2));
                            byte checkSum   = HexStringConverter.HS2toByte(sourse.Substring(sourse.Length - 2, 2));

                            uint CalcCS = 0;
                            for (int i = 0; i < (dataSize + 4); i++)
                            {
                                CalcCS += HexStringConverter.HS2toByte(sourse.Substring(i * 2 + 1, 2));
                            }
                            CalcCS = (~CalcCS + 1) & 0x000000FF;
                            if (CalcCS != (uint)checkSum)
                            {
                                throw new InvalidDataException(" File : " + path + " Check sum error in string " + stringNum.ToString());
                            }
                            byte[] data = new byte[dataSize];

                            switch (recordType)
                            {
                            case 0x04:
                                if (dataSize == 1)
                                {
                                    highAddr = ((uint)(HexStringConverter.HS2toByte(sourse.Substring(9, 2)))) << 16;
                                }
                                else if (dataSize == 2)
                                {
                                    highAddr = ((uint)(HexStringConverter.HS4toUInt16(sourse.Substring(9, 4)))) << 16;
                                }
                                else
                                {
                                    throw new InvalidDataException(" File : " + path + " Invalid high address record in string " + stringNum.ToString());
                                }
                                break;

                            case 0x01:
                                break;

                            case 0x00:
                                if (dataSize > 0)
                                {
                                    for (int i = 0; i < dataSize; i++)
                                    {
                                        data[i] = HexStringConverter.HS2toByte(sourse.Substring(9 + i * 2, 2));
                                    }
                                    readedData.Add(highAddr + HexStringConverter.HS4toUInt16(sourse.Substring(3, 4)), data);
                                }
                                break;
                            }
                        }
                        catch (KeyNotFoundException)
                        {
                            throw new InvalidDataException(" File : " + path + " Incorrect symbol found in string " + stringNum.ToString());
                        }
                    }

                    foreach (KeyValuePair <uint, byte[]> line in readedData)
                    {
                        foreach (MemoryRegion region in regions)
                        {
                            if (region.WriteData(line.Key, line.Value) > 0)
                            {
                                break;
                            }
                        }
                    }
                }
            }
                        #endregion
                        );
            readers.Add("bin",
                        #region bindelegate
                        delegate()
            {
                using (BinaryReader file = new BinaryReader(File.OpenRead(path)))
                {
                    file.BaseStream.Position = 0;
                    try
                    {
                        for (int i = 0; i < regions[0].Data.Length; i++)
                        {
                            regions[0].Data[i] = file.ReadByte();
                        }
                    }
                    catch (EndOfStreamException)
                    {
                        throw new InvalidDataException(" File : " + path + " Size too small");
                    }
                }
            }
                        #endregion
                        );
            readers.Add("eep",
                        #region ponydelegate
                        delegate()
            {
                using (BinaryReader file = new BinaryReader(File.OpenRead(path)))
                {
                    file.BaseStream.Position = 152;
                    try
                    {
                        for (int i = 0; i < regions[0].Data.Length; i++)
                        {
                            regions[0].Data[i] = file.ReadByte();
                        }
                    }
                    catch (EndOfStreamException)
                    {
                        throw new InvalidDataException(" File : " + path + " Size too small");
                    }
                }
            }
                        #endregion
                        );
            readers.Add("e2p", readers["eep"]);
            readers.Add("rom", readers["eep"]);

            ThreadStart start = delegate()
            {
                try
                {
                    readers[path.Substring(path.Length - 3, 3).ToLower()]();
                    completeInfo.Regions = regions;
                }
                catch (KeyNotFoundException)
                {
                    completeInfo.Error = new Exception("Not supported file type");
                }
                catch (Exception e)
                {
                    completeInfo.Error = e;
                }

                asyncOp.PostOperationCompleted(delegate(object arg) { complete(completeInfo); }, null);
            };
            (new Thread(start)).Start();
        }