Exemple #1
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()
            });
        }
Exemple #2
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);
            }
        }