コード例 #1
0
        public HexFileReader(string fileName)
        {
            if (!File.Exists(fileName))
            {
                throw new ArgumentException(string.Format("File {0} does not exist!", fileName));
            }

            memorySize = 0;

            using (Stream stream = new FileStream(fileName, FileMode.Open))
            {
                StreamReader reader = new StreamReader(stream);
                while (reader.Peek() >= 0)
                {
                    string hexRecordLine = reader.ReadLine();

                    //var hexRecord = HexFileLineParser.ParseLine(hexRecordLine);
                    IntelHexRecord hexRecord = HexFileLineParser.ParseLine(hexRecordLine);
                    if (hexRecord != null)
                    {
                        switch (hexRecord.RecordType)
                        {
                        case RecordType.Data:
                        {
                            memorySize += hexRecord.ByteCount;
                            break;
                        }
                        }
                    }
                }
            }
            Initialize(File.ReadLines(fileName), memorySize);
        }
コード例 #2
0
 /// <summary>
 /// Verify an assertion about an IntelHexRecord, and throw an IOException if the predicate is not true.
 /// </summary>
 /// <param name="record">The record to verify the assertion for.</param>
 /// <param name="predicate">The assertion to verify.</param>
 /// <param name="message">The message to show in the exception if the assertion fails.</param>
 public static void Assert(this IntelHexRecord record, Func <IntelHexRecord, bool> predicate, string message)
 {
     if (!predicate(record))
     {
         throw new IOException(
                   string.Format("{0} -- record {1}!", message, record));
     }
 }
コード例 #3
0
        public void LoadNotSupportedTest()
        {
            string filename = "test.hex";

            File.WriteAllText(filename, IntelHexRecord.EncodeLine((eRecordType)8, 0x1000, null));

            IntelHexFile hf = new IntelHexFile();

            hf.Load(filename);
        }
コード例 #4
0
        public static ParsedIntelHexFile Parse(string hexFile)
        {
            uint        segmentBase = 0;
            List <byte> data        = new List <byte>();
            uint?       start       = null;

            foreach (var line in File.ReadAllLines(hexFile))
            {
                var parsedLine = IntelHexRecord.Parse(line);

                if (parsedLine.RecordType == 2)
                {
                    segmentBase = IntelHexRecord.SwapBytes(BitConverter.ToUInt16(parsedLine.Data, 0)) * 16U;
                }
                else if (parsedLine.RecordType == 4)
                {
                    segmentBase = (uint)IntelHexRecord.SwapBytes(BitConverter.ToUInt16(parsedLine.Data, 0)) << 16;
                }
                else if (parsedLine.RecordType == 0)
                {
                    uint addr = parsedLine.Address + segmentBase;
                    if (!start.HasValue)
                    {
                        start = addr;
                    }

                    if (addr != (start.Value + data.Count))
                    {
                        int padding = (int)(addr - (start.Value + data.Count));
                        if (padding < 0 || padding > 4096)
                        {
                            throw new Exception("Unexpected gap in " + hexFile);
                        }
                        for (int i = 0; i < padding; i++)
                        {
                            data.Add(0);
                        }
                    }
                    data.AddRange(parsedLine.Data);
                }
                else if (parsedLine.RecordType == 1)
                {
                    break;
                }
                else
                {
                    throw new Exception($"Unexpected record type {parsedLine.RecordType} in {hexFile}");
                }
            }

            return(new ParsedIntelHexFile {
                LoadAddress = start.Value, Data = data.ToArray()
            });
        }
コード例 #5
0
        private static short CalculateOffset(IntelHexRecord first, IntelHexRecord second)
        {
            var offset = second.Address - (first.Address + first.ByteCount);

            if (offset > short.MaxValue)
            {
                throw new NotImplementedException("an offset higher than 2byte cannot be handeled");
            }

            return((short)offset);
        }
コード例 #6
0
        public void IntelHexRecordConstructorTest()
        {
            string      line         = ":08000000006C072095B8100800";
            ushort      expectedAddr = 0x0000;
            eRecordType expectedType = eRecordType.Data;

            byte[] expecteddata = new byte[] { 0x00, 0x6C, 0x07, 0x20, 0x95, 0xB8, 0x10, 0x08 };

            IntelHexRecord tr = new IntelHexRecord(line);

            Assert.AreEqual(expectedAddr, tr.Address);
            Assert.AreEqual(expectedType, tr.RecordType);
            Assert.IsTrue(expecteddata.SequenceEqual(tr.Bytes));
        }
コード例 #7
0
        public static uint GetLoadAddress(string ihexFile)
        {
            using (var fs = File.OpenText(ihexFile))
            {
                var  line0 = IntelHexRecord.Parse(fs.ReadLine());
                var  line1 = IntelHexRecord.Parse(fs.ReadLine());
                uint segmentBase;
                if (line0.RecordType == 2)
                {
                    segmentBase = IntelHexRecord.SwapBytes(BitConverter.ToUInt16(line0.Data, 0)) * 16U;
                }
                else if (line0.RecordType == 4)
                {
                    segmentBase = (uint)IntelHexRecord.SwapBytes(BitConverter.ToUInt16(line0.Data, 0)) << 16;
                }
                else
                {
                    throw new Exception($"{ihexFile} does not start with a record of type 2");
                }

                return(segmentBase + line1.Address);
            }
        }
コード例 #8
0
 public void EncodeExtAddrLineTest()
 {
     Assert.AreEqual(IntelHexRecord.EncodeLine(eRecordType.ExtendedLinearAddress, 0, new byte[] { 0x08, 0x00 }), ":020000040800F2");
 }
コード例 #9
0
 public void IntelHexRecordConstructorInvalidInputTest()
 {
     IntelHexRecord tr = new IntelHexRecord(null);
 }
コード例 #10
0
 private static bool RecordIsOffset(IntelHexRecord record)
 {
     return(record.Data.All(b => b == 0xFF));
 }
コード例 #11
0
        public void IntelHexRecordConstructorInvalidChecksumTest()
        {
            string line = ":08000000006C072095B8100801";

            IntelHexRecord tr = new IntelHexRecord(line);
        }
コード例 #12
0
        public void IntelHexRecordConstructorInvalidBegomTest()
        {
            string line = "08000000006C072095B8100801ABCDE";

            IntelHexRecord tr = new IntelHexRecord(line);
        }
コード例 #13
0
 public void IntelHexRecordConstructorInvalidShortLineTest()
 {
     IntelHexRecord tr = new IntelHexRecord(":09000000");
 }
コード例 #14
0
 public void EncodeDataLineOutofRangeTest()
 {
     IntelHexRecord.EncodeLine(eRecordType.Data, 0, new byte[256]);
 }
コード例 #15
0
 public void EncodeEndLineTest()
 {
     Assert.AreEqual(IntelHexRecord.EncodeLine(eRecordType.EndOfFile, 0, null), ":00000001FF");
 }
コード例 #16
0
        /// <summary>
        /// Create a new IntelHexFile out of the binary data
        /// </summary>
        /// <param name="binaryData">The binary source code representation</param>
        /// <returns>An new IntelHexFile representation</returns>
        public static IntelHexFile CreateFrom(byte[] binaryData, int baseAddress = 0x00000000)
        {
            var hexFile = new IntelHexFile(baseAddress);

            hexFile._records.AddRange(CreateDataRecords(binaryData));
            hexFile._records.ForEach(record => record.AddAddress(baseAddress));

            var seekResult = SeekForOffset(hexFile);

            while (seekResult.Match)
            {
                var fromRecord = hexFile._records[seekResult.FromIndex];
                var toRecord   = hexFile._records[seekResult.ToIndex];

                hexFile._records.RemoveRange(seekResult.FromIndex, seekResult.ToIndex - seekResult.FromIndex);

                var addressDelta = toRecord.Address;
                if (addressDelta > short.MaxValue)
                {
                    var highOffsetPart = (short)((toRecord.Address >> 16) & 0xFFFF);
                    hexFile._records.Insert(seekResult.FromIndex, new OffsetRecord(highOffsetPart, 0));
                }

                seekResult = SeekForOffset(hexFile);
            }

            foreach (var dataRecord in hexFile._records.OfType <DataRecord>())
            {
                dataRecord.TrimEnd();
            }

            var insertCount = 0;

            for (var index = 0; index + insertCount + 1 < hexFile._records.Count; index++)
            {
                IntelHexRecord previousRecord = null;
                if (index != 0)
                {
                    previousRecord = hexFile._records[index + insertCount - 1];
                }

                var nextRecord = hexFile._records[index + insertCount];

                if (nextRecord is OffsetRecord || previousRecord is OffsetRecord)
                {
                    continue;
                }

                var previousOffsetAddressPart = (short)((previousRecord?.Address ?? 0) >> 16);
                var nextOffsetAddressPart     = (short)(nextRecord.Address >> 16);

                if (previousOffsetAddressPart == nextOffsetAddressPart)
                {
                    continue;
                }
                hexFile._records.Insert(index + insertCount, new OffsetRecord(nextOffsetAddressPart, 0));
                insertCount++;
            }

            return(hexFile);
        }
コード例 #17
0
 public void EncodeDataLineTest()
 {
     Assert.AreEqual(IntelHexRecord.EncodeLine(eRecordType.Data, 0, new byte[] { 0x00, 0x6C, 0x07, 0x20, 0x95, 0xB8, 0x10, 0x08 }), ":08000000006C072095B8100800");
 }