-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathOpCode.cs
118 lines (88 loc) · 3.37 KB
/
OpCode.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
using MicroProcessor.Cpu6502.Attributes;
using System;
using System.Collections.Generic;
using System.Linq;
namespace MicroProcessor.Cpu6502
{
public class OpCode : OpCodeDefinitionAttribute
{
public int OpCodeAddress { get; set; }
public List<byte> Operands { get; set; } = new List<byte>();
public Action GetAddress { get; set; }
public Action Run { get; set; }
public OpCode() : base()
{
}
public override string ToString()
{
return ToString();
}
public string ToString(bool showComments = false)
{
var operands = $"{string.Join(" ", Operands.Select(x => $"{x:X2}"))}";
var s = $"{OpCodeAddress:X4}\t{Code:X2} {operands.PadRight(5, ' ')}\t{Name}";
switch (AddressingMode)
{
case Enums.AddressingMode.Accumulator:
s = $"{s} A";
break;
case Enums.AddressingMode.Implied:
s = $"{s}";
break;
case Enums.AddressingMode.Absolute:
s = $"{s} ${Operands[1]:X2}{Operands[0]:X2}";
break;
case Enums.AddressingMode.AbsoluteX:
s = $"{s} ${Operands[1]:X2}{Operands[0]:X2}, X";
break;
case Enums.AddressingMode.AbsoluteY:
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 + ((sbyte)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,
Run = action,
AddCycleIfBoundaryCrossed = a.AddCycleIfBoundaryCrossed,
AddressingMode = a.AddressingMode,
Code = a.Code,
Cycles = a.Cycles,
Description = a.Description,
Length = a.Length,
Name = a.Name,
IsIllegal = a.IsIllegal
};
}
}
}