mirror of
https://github.com/MoleskiCoder/EightBitNet.git
synced 2026-04-20 06:16:43 +00:00
Correct some straightforward analysis issues.
Signed-off-by: Adrian Conlon <Adrian.conlon@gmail.com>
This commit is contained in:
+6
-6
@@ -10,17 +10,17 @@ namespace EightBit
|
||||
{
|
||||
}
|
||||
|
||||
public static byte SetFlag(byte input, byte flag) => (byte)(input | flag);
|
||||
public static byte SetBit(byte input, byte which) => (byte)(input | which);
|
||||
|
||||
public static byte SetFlag(byte input, byte flag, int condition) => SetFlag(input, flag, condition != 0);
|
||||
public static byte SetBit(byte input, byte which, int condition) => SetBit(input, which, condition != 0);
|
||||
|
||||
public static byte SetFlag(byte input, byte flag, bool condition) => condition ? SetFlag(input, flag) : ClearFlag(input, flag);
|
||||
public static byte SetBit(byte input, byte which, bool condition) => condition ? SetBit(input, which) : ClearBit(input, which);
|
||||
|
||||
public static byte ClearFlag(byte input, byte flag) => (byte)(input & (byte)~flag);
|
||||
public static byte ClearBit(byte input, byte which) => (byte)(input & (byte)~which);
|
||||
|
||||
public static byte ClearFlag(byte input, byte flag, int condition) => ClearFlag(input, flag, condition != 0);
|
||||
public static byte ClearBit(byte input, byte which, int condition) => ClearBit(input, which, condition != 0);
|
||||
|
||||
public static byte ClearFlag(byte input, byte flag, bool condition) => SetFlag(input, flag, !condition);
|
||||
public static byte ClearBit(byte input, byte which, bool condition) => SetBit(input, which, !condition);
|
||||
|
||||
public static byte HighByte(int value) => (byte)(value >> 8);
|
||||
|
||||
|
||||
@@ -26,82 +26,82 @@ namespace EightBit
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void TestClearFlag()
|
||||
public void TestClearBit()
|
||||
{
|
||||
byte flags = 0xff;
|
||||
flags = Chip.ClearFlag(flags, 0x80);
|
||||
flags = Chip.ClearBit(flags, 0x80);
|
||||
Assert.AreEqual(0x7f, flags);
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void TestClearFlagNonZero()
|
||||
public void TestClearBitNonZero()
|
||||
{
|
||||
byte flags = 0xff;
|
||||
flags = Chip.ClearFlag(flags, 0x80, 1);
|
||||
flags = Chip.ClearBit(flags, 0x80, 1);
|
||||
Assert.AreEqual(0x7f, flags);
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void TestClearFlagZero()
|
||||
public void TestClearBitZero()
|
||||
{
|
||||
byte flags = 0xff;
|
||||
flags = Chip.ClearFlag(flags, 0x80, 0);
|
||||
flags = Chip.ClearBit(flags, 0x80, 0);
|
||||
Assert.AreEqual(0xff, flags);
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void TestClearFlagFalse()
|
||||
public void TestClearBitFalse()
|
||||
{
|
||||
byte flags = 0xff;
|
||||
flags = Chip.ClearFlag(flags, 0x80, false);
|
||||
flags = Chip.ClearBit(flags, 0x80, false);
|
||||
Assert.AreEqual(0xff, flags);
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void TestClearFlagTrue()
|
||||
public void TestClearBitTrue()
|
||||
{
|
||||
byte flags = 0xff;
|
||||
flags = Chip.ClearFlag(flags, 0x80, true);
|
||||
flags = Chip.ClearBit(flags, 0x80, true);
|
||||
Assert.AreEqual(0x7f, flags);
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void TestSetFlag()
|
||||
public void TestSetBit()
|
||||
{
|
||||
byte flags = 0x7f;
|
||||
flags = Chip.SetFlag(flags, 0x80);
|
||||
flags = Chip.SetBit(flags, 0x80);
|
||||
Assert.AreEqual(0xff, flags);
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void TestSetFlagNonZero()
|
||||
public void TestSetBitNonZero()
|
||||
{
|
||||
byte flags = 0x7f;
|
||||
flags = Chip.SetFlag(flags, 0x80, 1);
|
||||
flags = Chip.SetBit(flags, 0x80, 1);
|
||||
Assert.AreEqual(0xff, flags);
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void TestSetFlagZero()
|
||||
public void TestSetBitZero()
|
||||
{
|
||||
byte flags = 0x7f;
|
||||
flags = Chip.SetFlag(flags, 0x80, 0);
|
||||
flags = Chip.SetBit(flags, 0x80, 0);
|
||||
Assert.AreEqual(0x7f, flags);
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void TestSetFlagFalse()
|
||||
public void TestSetBitFalse()
|
||||
{
|
||||
byte flags = 0x7f;
|
||||
flags = Chip.SetFlag(flags, 0x80, false);
|
||||
flags = Chip.SetBit(flags, 0x80, false);
|
||||
Assert.AreEqual(0x7f, flags);
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void TestSetFlagTrue()
|
||||
public void TestSetBitTrue()
|
||||
{
|
||||
byte flags = 0x7f;
|
||||
flags = Chip.SetFlag(flags, 0x80, true);
|
||||
flags = Chip.SetBit(flags, 0x80, true);
|
||||
Assert.AreEqual(0xff, flags);
|
||||
}
|
||||
|
||||
|
||||
@@ -11,6 +11,7 @@ namespace EightBit
|
||||
public class IntelHexFile : IDisposable
|
||||
{
|
||||
private readonly StreamReader reader;
|
||||
private bool eof;
|
||||
private bool disposed = false;
|
||||
|
||||
public IntelHexFile(string path) => this.reader = File.OpenText(path);
|
||||
@@ -36,25 +37,33 @@ namespace EightBit
|
||||
|
||||
public IEnumerable<Tuple<ushort, byte[]>> Parse()
|
||||
{
|
||||
var eof = false;
|
||||
while (!this.reader.EndOfStream && !eof)
|
||||
this.eof = false;
|
||||
while (!this.reader.EndOfStream && !this.eof)
|
||||
{
|
||||
var line = this.reader.ReadLine();
|
||||
var parsed = this.Parse(line);
|
||||
eof = parsed == null;
|
||||
if (!eof)
|
||||
if (parsed != null)
|
||||
{
|
||||
yield return parsed;
|
||||
}
|
||||
}
|
||||
if (!this.eof)
|
||||
{
|
||||
throw new InvalidOperationException("File is missing an EOF record");
|
||||
}
|
||||
}
|
||||
|
||||
private Tuple<ushort, byte[]> Parse(string line)
|
||||
{
|
||||
if (string.IsNullOrEmpty(line))
|
||||
{
|
||||
throw new ArgumentNullException(nameof(line));
|
||||
}
|
||||
|
||||
var colon = line.Substring(0, 1);
|
||||
if (colon != ":")
|
||||
{
|
||||
throw new System.InvalidOperationException("Invalid hex file: line does not begin with a colon");
|
||||
throw new ArgumentOutOfRangeException(nameof(line), "Invalid hex file: line does not begin with a colon");
|
||||
}
|
||||
|
||||
var countString = line.Substring(1, 2);
|
||||
@@ -72,6 +81,7 @@ namespace EightBit
|
||||
return ParseDataRecord(line, address, count);
|
||||
|
||||
case 0x01:
|
||||
this.eof = true;
|
||||
return null;
|
||||
|
||||
default:
|
||||
@@ -81,18 +91,23 @@ namespace EightBit
|
||||
|
||||
private static Tuple<ushort, byte[]> ParseDataRecord(string line, ushort address, byte count)
|
||||
{
|
||||
var data = new byte[count];
|
||||
if (string.IsNullOrEmpty(line))
|
||||
{
|
||||
throw new ArgumentNullException(nameof(line));
|
||||
}
|
||||
|
||||
var requiredLength = 9 + 2 + (count * 2);
|
||||
if (line.Length != requiredLength)
|
||||
{
|
||||
throw new InvalidOperationException("Invalid hex file: line is not the required length");
|
||||
throw new ArgumentOutOfRangeException(nameof(line), "Invalid hex file: line is not the required length");
|
||||
}
|
||||
|
||||
var data = new byte[count];
|
||||
for (var i = 0; i < count; ++i)
|
||||
{
|
||||
var position = 9 + (i * 2);
|
||||
var datumString = line.Substring(position, 2);
|
||||
data[i] = Convert.ToByte(datumString, 16);
|
||||
var extracted = line.Substring(position, 2);
|
||||
data[i] = Convert.ToByte(extracted, 16);
|
||||
}
|
||||
|
||||
return new Tuple<ushort, byte[]>(address, data);
|
||||
|
||||
@@ -52,7 +52,7 @@ namespace EightBit
|
||||
|
||||
public ushort Word
|
||||
{
|
||||
get => (ushort)(this.Low | Chip.PromoteByte(this.High));
|
||||
get => Chip.MakeWord(this.Low, this.High);
|
||||
|
||||
set
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user