Files
EightBitNet/EightBit/IntelHexFile.cs
T

78 lines
2.5 KiB
C#

// <copyright file="IntelHexFile.cs" company="Adrian Conlon">
// Copyright (c) Adrian Conlon. All rights reserved.
// </copyright>
namespace EightBit
{
public class IntelHexFile(string path)
{
private readonly string _path = path;
private bool _eof;
public IEnumerable<Tuple<ushort, byte[]>> Parse()
{
this._eof = false;
using var reader = File.OpenText(this._path);
while (!reader.EndOfStream && !this._eof)
{
var line = reader.ReadLine() ?? throw new InvalidOperationException("Early EOF detected");
var parsed = this.Parse(line);
if (parsed is not null)
{
yield return parsed;
}
}
if (!this._eof)
{
throw new InvalidOperationException("File is missing an EOF record");
}
}
private static byte[] ParseDataRecord(string line, byte count)
{
ArgumentNullException.ThrowIfNullOrEmpty(line);
var requiredLength = 9 + 2 + (count * 2);
ArgumentOutOfRangeException.ThrowIfNotEqual(line.Length, requiredLength, nameof(line));
var data = new byte[count];
for (var i = 0; i < count; ++i)
{
var position = 9 + (i * 2);
var extracted = line.Substring(position, 2);
data[i] = Convert.ToByte(extracted, 16);
}
return data;
}
private Tuple<ushort, byte[]>? Parse(string line)
{
ArgumentNullException.ThrowIfNullOrEmpty(line);
var colon = line[..1];
ArgumentOutOfRangeException.ThrowIfNotEqual(colon, ":", nameof(line));
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);
switch (Convert.ToByte(recordTypeString, 16))
{
case 0x00:
return new Tuple<ushort, byte[]>(address, ParseDataRecord(line, count));
case 0x01:
this._eof = true;
return null;
default:
throw new InvalidOperationException("Unhandled hex file record.");
}
}
}
}