Skip to content

Commit

Permalink
#8 Started implementing a 6502 disassembler
Browse files Browse the repository at this point in the history
  • Loading branch information
hagronnestad committed Oct 3, 2019
1 parent 037077d commit f58b237
Showing 1 changed file with 41 additions and 0 deletions.
41 changes: 41 additions & 0 deletions MicroProcessor/Cpu6502/Disassembler.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
using MicroProcessor.Cpu6502.Attributes;
using System.Collections.Generic;
using System.Linq;

namespace MicroProcessor.Cpu6502 {

public class Disassembler {

public List<OpCodeAttribute> OpCodes { get; private set; }
public Dictionary<byte, OpCodeAttribute> OpCodeCache { get; set; }

public Disassembler() {

OpCodes = typeof(Cpu)
.GetMethods()
.SelectMany(m => m.GetCustomAttributes(typeof(OpCodeAttribute), true)
.Select(a => a as OpCodeAttribute))
.ToList();

OpCodeCache = OpCodes.ToDictionary(x => x.Code, x => x);

}

public Dictionary<int, OpCodeAttribute> Disassemble(byte[] machineCode) {
var disassembly = new Dictionary<int, OpCodeAttribute>();

for (int i = 0; i < machineCode.Length; i++) {

if (!OpCodeCache.ContainsKey(machineCode[i])) continue;

var opCode = OpCodeCache[machineCode[i]];
disassembly.Add(i, opCode);

i += opCode.Length - 1;
}

return disassembly;
}
}

}

0 comments on commit f58b237

Please sign in to comment.