Skip to content

Commit

Permalink
#8 More work on the disassembler
Browse files Browse the repository at this point in the history
  • Loading branch information
hagronnestad committed Oct 8, 2019
1 parent 4bf50a1 commit 7f9e51d
Showing 1 changed file with 38 additions and 6 deletions.
44 changes: 38 additions & 6 deletions MicroProcessor/Cpu6502/OpCode.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using MicroProcessor.Cpu6502.Attributes;
using System;
using System.Collections.Generic;
using System.Linq;

namespace MicroProcessor.Cpu6502 {

Expand All @@ -12,53 +13,83 @@ public class OpCode : OpCodeDefinitionAttribute {
public Action GetAddress { get; set; }
public Action Run { get; set; }


public OpCode() : base() {

}


public override string ToString() {
var s = $"{OpCodeAddress:X4} {Name}";
return ToString();
}

public string ToString(bool showComments = false) {
var operands = $"{string.Join(" ", Operands.Select(x => $"{x:X2}"))}";
var s = $"{OpCodeAddress:X4}\t{Code:X2}\t{operands.PadRight(5, ' ')}\t{Name}";

switch (AddressingMode) {
case Enums.AddressingMode.Accumulator:
return $"{s} A";
s = $"{s} A";
break;

case Enums.AddressingMode.Implied:
return $"{s}";
s = $"{s}";
break;

case Enums.AddressingMode.Absolute:
return $"{s} 0x{Operands[1]:X2}{Operands[0]:X2}";
s = $"{s} ${Operands[1]:X2}{Operands[0]:X2}";
break;

case Enums.AddressingMode.AbsoluteX:
return $"{s} 0x{Operands[1]:X2}{Operands[0]:X2}, X";
s = $"{s} ${Operands[1]:X2}{Operands[0]:X2}, X";
break;

case Enums.AddressingMode.AbsoluteY:
return $"{s} 0x{Operands[1]:X2}{Operands[0]:X2}, Y";
s = $"{s} ${Operands[1]:X2}{Operands[0]:X2}, Y";
break;

case Enums.AddressingMode.Immediate:
s = $"{s} #${Operands[0]:X2}";
break;

case Enums.AddressingMode.Indirect:
s = $"{s} (${Operands[1]:X2}{Operands[0]:X2})";
break;

case Enums.AddressingMode.XIndirect:
s = $"{s} (${Operands[0]:X2}, X)";
break;

case Enums.AddressingMode.IndirectY:
s = $"{s} (${Operands[0]:X2}), Y";
break;

case Enums.AddressingMode.Relative:
s = $"{s} ${OpCodeAddress + Length + (Operands[0]):X2}";
break;

case Enums.AddressingMode.Zeropage:
s = $"{s} ${Operands[0]:X2}";
break;

case Enums.AddressingMode.ZeropageX:
s = $"{s} ${Operands[0]:X2}, X";
break;

case Enums.AddressingMode.ZeropageY:
s = $"{s} ${Operands[0]:X2}, Y";
break;

default:
break;
}

if (showComments) s = $"{s.PadRight(31, ' ')}\t// {Description}";

return s;
}


public static OpCode FromOpCodeDefinitionAttribute(Action action, Action getAddress, OpCodeDefinitionAttribute a) {
return new OpCode {
GetAddress = getAddress,
Expand All @@ -72,6 +103,7 @@ public static OpCode FromOpCodeDefinitionAttribute(Action action, Action getAddr
Name = a.Name
};
}

}

}

0 comments on commit 7f9e51d

Please sign in to comment.