mirror of
https://github.com/MoleskiCoder/EightBitNet.git
synced 2025-02-03 03:30:58 +00:00
Refactor the Intel hex file parser. The new parser is isolated and memory efficient.
Signed-off-by: Adrian Conlon <Adrian.conlon@gmail.com>
This commit is contained in:
parent
69122eb233
commit
7b5af28f5d
@ -103,69 +103,6 @@ namespace EightBit
|
|||||||
|
|
||||||
public abstract void Initialize();
|
public abstract void Initialize();
|
||||||
|
|
||||||
protected static Dictionary<ushort, List<byte>> ParseHexFile(string path)
|
|
||||||
{
|
|
||||||
var returned = new Dictionary<ushort, List<byte>>();
|
|
||||||
|
|
||||||
using (var reader = File.OpenText(path))
|
|
||||||
{
|
|
||||||
var eof = false;
|
|
||||||
while (!reader.EndOfStream && !eof)
|
|
||||||
{
|
|
||||||
var line = reader.ReadLine();
|
|
||||||
|
|
||||||
var colon = line.Substring(0, 1);
|
|
||||||
if (colon != ":")
|
|
||||||
{
|
|
||||||
throw new System.InvalidOperationException("Invalid hex file: line does not begin with a colon");
|
|
||||||
}
|
|
||||||
|
|
||||||
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:
|
|
||||||
{
|
|
||||||
var data = new List<byte>(count);
|
|
||||||
var requiredLength = 9 + 2 + (count * 2);
|
|
||||||
if (line.Length != requiredLength)
|
|
||||||
{
|
|
||||||
throw new InvalidOperationException("Invalid hex file: line is not the required length");
|
|
||||||
}
|
|
||||||
|
|
||||||
for (var i = 0; i < count; ++i)
|
|
||||||
{
|
|
||||||
var position = 9 + (i * 2);
|
|
||||||
var datumString = line.Substring(position, 2);
|
|
||||||
var datum = Convert.ToByte(datumString, 16);
|
|
||||||
data.Add(datum);
|
|
||||||
}
|
|
||||||
|
|
||||||
returned[address] = data;
|
|
||||||
}
|
|
||||||
|
|
||||||
break;
|
|
||||||
|
|
||||||
case 0x01:
|
|
||||||
eof = true;
|
|
||||||
break;
|
|
||||||
|
|
||||||
default:
|
|
||||||
throw new InvalidOperationException("Unhandled hex file record.");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return returned;
|
|
||||||
}
|
|
||||||
|
|
||||||
protected virtual void OnWritingByte() => this.WritingByte?.Invoke(this, EventArgs.Empty);
|
protected virtual void OnWritingByte() => this.WritingByte?.Invoke(this, EventArgs.Empty);
|
||||||
|
|
||||||
protected virtual void OnWrittenByte() => this.WrittenByte?.Invoke(this, EventArgs.Empty);
|
protected virtual void OnWrittenByte() => this.WrittenByte?.Invoke(this, EventArgs.Empty);
|
||||||
@ -195,14 +132,16 @@ namespace EightBit
|
|||||||
|
|
||||||
protected void LoadHexFile(string path)
|
protected void LoadHexFile(string path)
|
||||||
{
|
{
|
||||||
var chunks = ParseHexFile(path);
|
using (var file = new IntelHexFile(path))
|
||||||
foreach (var chunk in chunks)
|
|
||||||
{
|
{
|
||||||
var address = chunk.Key;
|
foreach (var chunk in file.Parse())
|
||||||
var content = chunk.Value;
|
{
|
||||||
var mapped = this.Mapping(address);
|
var address = chunk.Item1;
|
||||||
var offset = address - mapped.Begin;
|
var content = chunk.Item2;
|
||||||
mapped.Memory.Load(content.ToArray(), offset);
|
var mapped = this.Mapping(address);
|
||||||
|
var offset = address - mapped.Begin;
|
||||||
|
mapped.Memory.Load(content.ToArray(), offset);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -51,6 +51,7 @@
|
|||||||
<Compile Include="Chip.cs" />
|
<Compile Include="Chip.cs" />
|
||||||
<Compile Include="ClockedChip.cs" />
|
<Compile Include="ClockedChip.cs" />
|
||||||
<Compile Include="Device.cs" />
|
<Compile Include="Device.cs" />
|
||||||
|
<Compile Include="IntelHexFile.cs" />
|
||||||
<Compile Include="IntelOpCodeDecoded.cs" />
|
<Compile Include="IntelOpCodeDecoded.cs" />
|
||||||
<Compile Include="Memory.cs" />
|
<Compile Include="Memory.cs" />
|
||||||
<Compile Include="InputOutput.cs" />
|
<Compile Include="InputOutput.cs" />
|
||||||
|
99
EightBit/IntelHexFile.cs
Normal file
99
EightBit/IntelHexFile.cs
Normal file
@ -0,0 +1,99 @@
|
|||||||
|
// <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;
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public IEnumerable<Tuple<ushort, List<byte>>> Parse()
|
||||||
|
{
|
||||||
|
var eof = false;
|
||||||
|
while (!this.reader.EndOfStream && !eof)
|
||||||
|
{
|
||||||
|
var line = this.reader.ReadLine();
|
||||||
|
var parsed = this.Parse(line);
|
||||||
|
eof = parsed == null;
|
||||||
|
if (!eof)
|
||||||
|
{
|
||||||
|
yield return parsed;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private Tuple<ushort, List<byte>> Parse(string line)
|
||||||
|
{
|
||||||
|
var colon = line.Substring(0, 1);
|
||||||
|
if (colon != ":")
|
||||||
|
{
|
||||||
|
throw new System.InvalidOperationException("Invalid hex file: line does not begin with a colon");
|
||||||
|
}
|
||||||
|
|
||||||
|
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:
|
||||||
|
{
|
||||||
|
var data = new List<byte>(count);
|
||||||
|
var requiredLength = 9 + 2 + (count * 2);
|
||||||
|
if (line.Length != requiredLength)
|
||||||
|
{
|
||||||
|
throw new InvalidOperationException("Invalid hex file: line is not the required length");
|
||||||
|
}
|
||||||
|
|
||||||
|
for (var i = 0; i < count; ++i)
|
||||||
|
{
|
||||||
|
var position = 9 + (i * 2);
|
||||||
|
var datumString = line.Substring(position, 2);
|
||||||
|
var datum = Convert.ToByte(datumString, 16);
|
||||||
|
data.Add(datum);
|
||||||
|
}
|
||||||
|
|
||||||
|
return new Tuple<ushort, List<byte>>(address, data);
|
||||||
|
}
|
||||||
|
|
||||||
|
case 0x01:
|
||||||
|
return null;
|
||||||
|
|
||||||
|
default:
|
||||||
|
throw new InvalidOperationException("Unhandled hex file record.");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user