2019-06-29 10:35:08 +00:00
|
|
|
|
// <copyright file="IntelHexFile.cs" company="Adrian Conlon">
|
|
|
|
|
// Copyright (c) Adrian Conlon. All rights reserved.
|
|
|
|
|
// </copyright>
|
|
|
|
|
|
|
|
|
|
namespace EightBit
|
|
|
|
|
{
|
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.IO;
|
|
|
|
|
|
|
|
|
|
public class IntelHexFile : IDisposable
|
|
|
|
|
{
|
|
|
|
|
private readonly StreamReader reader;
|
2019-06-30 23:15:25 +00:00
|
|
|
|
private bool eof;
|
2019-06-29 10:35:08 +00:00
|
|
|
|
private bool disposed = false;
|
|
|
|
|
|
|
|
|
|
public IntelHexFile(string path) => this.reader = File.OpenText(path);
|
|
|
|
|
|
|
|
|
|
public void Dispose()
|
|
|
|
|
{
|
|
|
|
|
this.Dispose(true);
|
|
|
|
|
GC.SuppressFinalize(this);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected virtual void Dispose(bool disposing)
|
|
|
|
|
{
|
|
|
|
|
if (!this.disposed)
|
|
|
|
|
{
|
|
|
|
|
if (disposing)
|
|
|
|
|
{
|
|
|
|
|
this.reader.Dispose();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
this.disposed = true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-06-29 10:50:27 +00:00
|
|
|
|
public IEnumerable<Tuple<ushort, byte[]>> Parse()
|
2019-06-29 10:35:08 +00:00
|
|
|
|
{
|
2019-06-30 23:15:25 +00:00
|
|
|
|
this.eof = false;
|
|
|
|
|
while (!this.reader.EndOfStream && !this.eof)
|
2019-06-29 10:35:08 +00:00
|
|
|
|
{
|
|
|
|
|
var line = this.reader.ReadLine();
|
|
|
|
|
var parsed = this.Parse(line);
|
2019-06-30 23:15:25 +00:00
|
|
|
|
if (parsed != null)
|
2019-06-29 10:35:08 +00:00
|
|
|
|
{
|
|
|
|
|
yield return parsed;
|
|
|
|
|
}
|
|
|
|
|
}
|
2019-06-30 23:15:25 +00:00
|
|
|
|
if (!this.eof)
|
|
|
|
|
{
|
|
|
|
|
throw new InvalidOperationException("File is missing an EOF record");
|
|
|
|
|
}
|
2019-06-29 10:35:08 +00:00
|
|
|
|
}
|
|
|
|
|
|
2019-06-29 10:50:27 +00:00
|
|
|
|
private Tuple<ushort, byte[]> Parse(string line)
|
2019-06-29 10:35:08 +00:00
|
|
|
|
{
|
2019-06-30 23:15:25 +00:00
|
|
|
|
if (string.IsNullOrEmpty(line))
|
|
|
|
|
{
|
|
|
|
|
throw new ArgumentNullException(nameof(line));
|
|
|
|
|
}
|
|
|
|
|
|
2019-06-29 10:35:08 +00:00
|
|
|
|
var colon = line.Substring(0, 1);
|
|
|
|
|
if (colon != ":")
|
|
|
|
|
{
|
2019-06-30 23:15:25 +00:00
|
|
|
|
throw new ArgumentOutOfRangeException(nameof(line), "Invalid hex file: line does not begin with a colon");
|
2019-06-29 10:35:08 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var countString = line.Substring(1, 2);
|
|
|
|
|
var count = Convert.ToByte(countString, 16);
|
|
|
|
|
|
|
|
|
|
var addressString = line.Substring(3, 4);
|
|
|
|
|
var address = Convert.ToUInt16(addressString, 16);
|
|
|
|
|
|
|
|
|
|
var recordTypeString = line.Substring(7, 2);
|
|
|
|
|
var recordType = Convert.ToByte(recordTypeString, 16);
|
|
|
|
|
|
|
|
|
|
switch (recordType)
|
|
|
|
|
{
|
|
|
|
|
case 0x00:
|
2019-06-29 10:50:27 +00:00
|
|
|
|
return ParseDataRecord(line, address, count);
|
2019-06-29 10:35:08 +00:00
|
|
|
|
|
|
|
|
|
case 0x01:
|
2019-06-30 23:15:25 +00:00
|
|
|
|
this.eof = true;
|
2019-06-29 10:35:08 +00:00
|
|
|
|
return null;
|
|
|
|
|
|
|
|
|
|
default:
|
|
|
|
|
throw new InvalidOperationException("Unhandled hex file record.");
|
|
|
|
|
}
|
|
|
|
|
}
|
2019-06-29 10:50:27 +00:00
|
|
|
|
|
|
|
|
|
private static Tuple<ushort, byte[]> ParseDataRecord(string line, ushort address, byte count)
|
|
|
|
|
{
|
2019-06-30 23:15:25 +00:00
|
|
|
|
if (string.IsNullOrEmpty(line))
|
|
|
|
|
{
|
|
|
|
|
throw new ArgumentNullException(nameof(line));
|
|
|
|
|
}
|
|
|
|
|
|
2019-06-29 10:50:27 +00:00
|
|
|
|
var requiredLength = 9 + 2 + (count * 2);
|
|
|
|
|
if (line.Length != requiredLength)
|
|
|
|
|
{
|
2019-06-30 23:15:25 +00:00
|
|
|
|
throw new ArgumentOutOfRangeException(nameof(line), "Invalid hex file: line is not the required length");
|
2019-06-29 10:50:27 +00:00
|
|
|
|
}
|
|
|
|
|
|
2019-06-30 23:15:25 +00:00
|
|
|
|
var data = new byte[count];
|
2019-06-29 10:50:27 +00:00
|
|
|
|
for (var i = 0; i < count; ++i)
|
|
|
|
|
{
|
|
|
|
|
var position = 9 + (i * 2);
|
2019-06-30 23:15:25 +00:00
|
|
|
|
var extracted = line.Substring(position, 2);
|
|
|
|
|
data[i] = Convert.ToByte(extracted, 16);
|
2019-06-29 10:50:27 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return new Tuple<ushort, byte[]>(address, data);
|
|
|
|
|
}
|
2019-06-29 10:35:08 +00:00
|
|
|
|
}
|
|
|
|
|
}
|