Skip to content

Commit

Permalink
Using unsigned short and reversing instruction bytes in the bitwise way
Browse files Browse the repository at this point in the history
  • Loading branch information
fredimachado committed Nov 4, 2016
1 parent 8385099 commit ca81e52
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 8 deletions.
4 changes: 2 additions & 2 deletions src/PICHexDisassembler/Hex32Record.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ namespace PICHexDisassembler
{
public class Hex32Record
{
public Hex32Record(byte byteCount, int address, byte recordType, short[] dataBytes, byte checksum)
public Hex32Record(byte byteCount, ushort address, byte recordType, ushort[] dataBytes, byte checksum)
{
ByteCount = byteCount;
Address = address;
Expand All @@ -24,7 +24,7 @@ public Hex32Record(byte byteCount, int address, byte recordType, short[] dataByt
public byte ByteCount { get; }
public int Address { get; }
public byte RecordType { get; }
public short[] DataBytes { get; }
public ushort[] DataBytes { get; }
public byte Checksum { get; }

public Instruction[] Instructions { get; }
Expand Down
15 changes: 10 additions & 5 deletions src/PICHexDisassembler/HexParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,12 @@ public Hex32Record ParseLine(string line)
}

var byteCount = byte.Parse(line.Substring(0, 2), System.Globalization.NumberStyles.HexNumber);
var address = int.Parse(line.Substring(2, 4), System.Globalization.NumberStyles.HexNumber);
var address = ushort.Parse(line.Substring(2, 4), System.Globalization.NumberStyles.HexNumber);
var recordType = byte.Parse(line.Substring(6, 2), System.Globalization.NumberStyles.HexNumber);
var checksum = byte.Parse(line.Substring(line.Length - 2), System.Globalization.NumberStyles.HexNumber);

var wordsCount = byteCount / 2;
var dataBytes = new short[wordsCount];
var dataBytes = new ushort[wordsCount];

for (int i = 0; i < wordsCount; i++)
{
Expand All @@ -27,10 +27,15 @@ public Hex32Record ParseLine(string line)
return new Hex32Record(byteCount, address, recordType, dataBytes, checksum);
}

private short GetNextTwoBytesReversed(string line, int startIndex)
private ushort GetNextTwoBytesReversed(string line, int startIndex)
{
var data = line.Substring(startIndex + 2, 2) + line.Substring(startIndex, 2);
return short.Parse(data, System.Globalization.NumberStyles.HexNumber);
var value = ushort.Parse(line.Substring(startIndex, 4), System.Globalization.NumberStyles.HexNumber);
return ReverseBytes(value);
}

private static ushort ReverseBytes(ushort value)
{
return (ushort)((value & 0x00FF) << 8 | (value & 0xFF00) >> 8);
}
}
}
2 changes: 1 addition & 1 deletion src/PICHexDisassembler/Instruction.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public override string ToString()
{ 0x1000, 0xFC00, typeof(Bcf) }, // mask: 0001000000000000 opcodeMask: 1111110000000000
};

internal static Instruction Parse(short dataBytes)
internal static Instruction Parse(ushort dataBytes)
{
foreach (var item in instructionMapping)
{
Expand Down

0 comments on commit ca81e52

Please sign in to comment.