-
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
#8 Started implementing a 6502 disassembler
- Loading branch information
1 parent
037077d
commit f58b237
Showing
1 changed file
with
41 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} | ||
|
||
} |