Skip to content

Commit

Permalink
Refactorings and using bitwise operations
Browse files Browse the repository at this point in the history
  • Loading branch information
fredimachado committed Nov 2, 2016
1 parent def9130 commit ccc4acc
Show file tree
Hide file tree
Showing 9 changed files with 43 additions and 31 deletions.
10 changes: 5 additions & 5 deletions src/PICHexDisassembler/Hex32Record.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ namespace PICHexDisassembler
{
public class Hex32Record
{
public Hex32Record(int byteCount, int address, int recordType, int[][] dataBytes, int checksum)
public Hex32Record(byte byteCount, int address, byte recordType, byte[][] dataBytes, byte checksum)
{
ByteCount = byteCount;
Address = address;
Expand All @@ -20,11 +20,11 @@ public Hex32Record(int byteCount, int address, int recordType, int[][] dataBytes
}
}

public int ByteCount { get; }
public byte ByteCount { get; }
public int Address { get; }
public int RecordType { get; }
public int[][] DataBytes { get; }
public int Checksum { get; }
public byte RecordType { get; }
public byte[][] DataBytes { get; }
public byte Checksum { get; }

public Instruction[] Mnemonics { get; }
}
Expand Down
16 changes: 8 additions & 8 deletions src/PICHexDisassembler/HexParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,13 @@ public Hex32Record ParseLine(string line)
line = line.Substring(1);
}

var byteCount = int.Parse(line.Substring(0, 2), System.Globalization.NumberStyles.HexNumber);
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 recordType = int.Parse(line.Substring(6, 2), System.Globalization.NumberStyles.HexNumber);
var checksum = int.Parse(line.Substring(line.Length - 2), 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 int[wordsCount][];
var dataBytes = new byte[wordsCount][];

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

private int[] GetNextTwoBytesReversed(string line, int startIndex)
private byte[] GetNextTwoBytesReversed(string line, int startIndex)
{
var bytes = new int[2];
bytes[0] = int.Parse(line.Substring(startIndex + 2, 2), System.Globalization.NumberStyles.HexNumber);
bytes[1] = int.Parse(line.Substring(startIndex, 2), System.Globalization.NumberStyles.HexNumber);
var bytes = new byte[2];
bytes[0] = byte.Parse(line.Substring(startIndex + 2, 2), System.Globalization.NumberStyles.HexNumber);
bytes[1] = byte.Parse(line.Substring(startIndex, 2), System.Globalization.NumberStyles.HexNumber);
return bytes;
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/PICHexDisassembler/Instruction.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@
{
public abstract class Instruction
{
protected readonly string data;
protected readonly int data;

public Instruction(string data)
public Instruction(int data)
{
this.data = data;
}
Expand Down
2 changes: 1 addition & 1 deletion src/PICHexDisassembler/Instructions/Call.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
{
public class Call : Goto
{
public Call(string data) : base(data)
public Call(int data) : base(data)
{
}
}
Expand Down
8 changes: 4 additions & 4 deletions src/PICHexDisassembler/Instructions/Goto.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,16 @@ namespace PICHexDisassembler.Instructions
{
public class Goto : Instruction
{
private readonly string address;
private readonly int address;

public Goto(string data) : base(data)
public Goto(int data) : base(data)
{
address = data.Substring(3);
address = data & 0x01FF;
}

public override string ToString()
{
return GetType().Name.ToUpper() + " 0x" + Convert.ToInt32(address, 2).ToString("X2");
return GetType().Name.ToUpper() + " 0x" + address.ToString("X2");
}
}
}
2 changes: 1 addition & 1 deletion src/PICHexDisassembler/Instructions/Retfie.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
{
public class Retfie : Instruction
{
public Retfie(string data) : base(data)
public Retfie(int data) : base(data)
{
}

Expand Down
2 changes: 1 addition & 1 deletion src/PICHexDisassembler/Instructions/Unknown.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ public sealed class Unknown : Instruction
{
private static Unknown instance;

private Unknown() : base(null)
private Unknown() : base(0)
{
}

Expand Down
17 changes: 8 additions & 9 deletions src/PICHexDisassembler/Mnemonic.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,25 +6,24 @@ namespace PICHexDisassembler
{
internal class Mnemonic
{
private static Dictionary<string, Type> mnemonicMapping = new Dictionary<string, Type>
private static MnemonicMapping mnemonicMapping = new MnemonicMapping
{
{ "101", typeof(Goto) },
{ "100", typeof(Call) },
{ "00000000001001", typeof(Retfie) },
{ 0x2800, 0xF800, typeof(Goto) }, // 0010100000000000
{ 0x2000, 0xF800, typeof(Call) }, // 0010000000000000
{ 0x0009, 0xFFFF, typeof(Retfie) }, // 0000000000001001
};

internal static Instruction Parse(int[] dataBytes)
internal static Instruction Parse(byte[] dataBytes)
{
var data1 = dataBytes[0];
var data2 = dataBytes[1];

var data = Convert.ToString(data1, 2).PadLeft(6, '0') + Convert.ToString(data2, 2).PadLeft(8, '0');
var word = (short)(data1 << 8 | data2);

foreach (var item in mnemonicMapping)
{
if (data.StartsWith(item.Key))
if ((word & item.Item2) == item.Item1)
{
return (Instruction)Activator.CreateInstance(item.Value, data);
return (Instruction)Activator.CreateInstance(item.Item3, word);
}
}

Expand Down
13 changes: 13 additions & 0 deletions src/PICHexDisassembler/MnemonicMapping.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
using System;
using System.Collections.Generic;

namespace PICHexDisassembler
{
public class MnemonicMapping : List<Tuple<int, int, Type>>
{
public void Add(int mask, int opcodeMask, Type type)
{
Add(new Tuple<int, int, Type>(mask, opcodeMask, type));
}
}
}

0 comments on commit ccc4acc

Please sign in to comment.