diff --git a/MicroProcessor/Cpu6502/Disassembler.cs b/MicroProcessor/Cpu6502/Disassembler.cs new file mode 100644 index 0000000..87d32c7 --- /dev/null +++ b/MicroProcessor/Cpu6502/Disassembler.cs @@ -0,0 +1,41 @@ +using MicroProcessor.Cpu6502.Attributes; +using System.Collections.Generic; +using System.Linq; + +namespace MicroProcessor.Cpu6502 { + + public class Disassembler { + + public List OpCodes { get; private set; } + public Dictionary 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 Disassemble(byte[] machineCode) { + var disassembly = new Dictionary(); + + 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; + } + } + +}