-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathInstructionBase.cs
329 lines (270 loc) · 10.3 KB
/
InstructionBase.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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
using System;
using M6502.Helpers.Extensions;
namespace M6502.InstructionDecode
{
public abstract class InstructionBase
{
public string Name { get; }
public byte OpCode { get; }
public AddressingMode AddressingMode { get; }
protected M6502Core Core { get; }
private readonly InstructionExecutor _executor;
protected InstructionBase(string name, byte opCode, AddressingMode addressingMode, M6502Core core)
{
Name = name;
OpCode = opCode;
AddressingMode = addressingMode;
Core = core;
_executor = GetExecutor();
}
public void Execute()
{
_executor();
}
public override string ToString()
{
return $"{Name} (0x{OpCode:X2})";
}
/// <summary>
/// Pure instruction, no arguments.
/// </summary>
protected virtual void ExecuteInImplicitMode()
{
ThrowNotImplementedException("Implicit");
}
/// <summary>
/// Argument is stored in the accumulator register.
/// </summary>
protected virtual void ExecuteInAccumulatorMode()
{
ThrowNotImplementedException("Accumulator");
}
/// <summary>
/// Argument is stored as constant next to operation code.
/// </summary>
protected virtual void ExecuteInImmediateMode()
{
ThrowNotImplementedException("Immediate");
}
/// <summary>
/// Arguments length: 1, Cycles: 1.
/// </summary>
protected byte ReadAddressInZeroPageMode()
{
// 1 cycle
var address = Core.Bus.Read(Core.Registers.ProgramCounter);
Core.Registers.ProgramCounter++;
return address;
}
/// <summary>
/// Argument is stored at the specified 8-bit address (0 page).
/// </summary>
protected virtual void ExecuteInZeroPageMode()
{
ThrowNotImplementedException("Zero Page");
}
/// <summary>
/// Arguments length: 1, Cycles: 2.
/// </summary>
protected byte ReadAddressInZeroPageXMode()
{
// 1 cycle
var zeroPageAddress = ReadAddressInZeroPageMode();
// 1 cycle
var address = (byte)(zeroPageAddress + Core.Registers.IndexRegisterX);
Core.YieldCycle();
return address;
}
/// <summary>
/// Argument is stored at the specified 8-bit address (0 page) + X register.
/// </summary>
protected virtual void ExecuteInZeroPageXMode()
{
ThrowNotImplementedException("Zero Page X");
}
/// <summary>
/// Arguments length: 1, Cycles: 2.
/// </summary>
protected byte ReadAddressInZeroPageYMode()
{
// 1 cycle
var zeroPageAddress = ReadAddressInZeroPageMode();
// 1 cycle
var address = (byte)(zeroPageAddress + Core.Registers.IndexRegisterY);
Core.YieldCycle();
return address;
}
/// <summary>
/// Argument is stored at the specified 8-bit address (0 page) + Y register.
/// </summary>
protected virtual void ExecuteInZeroPageYMode()
{
ThrowNotImplementedException("Zero Page Y");
}
/// <summary>
/// Argument contains offset to apply.
/// </summary>
protected virtual void ExecuteInRelativeMode()
{
ThrowNotImplementedException("Relative");
}
/// <summary>
/// Arguments length: 2, Cycles: 2.
/// </summary>
protected ushort ReadAddressInAbsoluteMode()
{
// 1 cycle
var low = Core.Bus.Read(Core.Registers.ProgramCounter);
Core.Registers.ProgramCounter++;
// 1 cycle
var high = Core.Bus.Read(Core.Registers.ProgramCounter) << 8;
Core.Registers.ProgramCounter++;
return (ushort) (high | low);
}
/// <summary>
/// Argument is stored at the specified 16-bit address.
/// </summary>
protected virtual void ExecuteInAbsoluteMode()
{
ThrowNotImplementedException("Absolute");
}
/// <summary>
/// Arguments length: 2, Cycles: 2 + 1B.
/// </summary>
protected ushort ReadAddressInAbsoluteXMode(bool forceBoundaryCheck = false)
{
// 2 cycles
var absoluteAddress = ReadAddressInAbsoluteMode();
// 1 cycle if page boundary crossed
var absoluteXAddress = (ushort)(absoluteAddress + Core.Registers.IndexRegisterX);
if (forceBoundaryCheck || (absoluteAddress & 0xFF00) != (absoluteXAddress & 0xFF00))
{
Core.YieldCycle();
}
return absoluteXAddress;
}
/// <summary>
/// Argument is stored at the specified 16-bit address + X register.
/// </summary>
protected virtual void ExecuteInAbsoluteXMode()
{
ThrowNotImplementedException("Absolute X");
}
/// <summary>
/// Arguments length: 2, Cycles: 2 + 1B.
/// </summary>
protected ushort ReadAddressInAbsoluteYMode(bool forceBoundaryCheck = false)
{
// 2 cycles
var absoluteAddress = ReadAddressInAbsoluteMode();
// 1 cycle if page boundary crossed
var absoluteYAddress = (ushort)(absoluteAddress + Core.Registers.IndexRegisterY);
if (forceBoundaryCheck || (absoluteAddress & 0xFF00) != (absoluteYAddress & 0xFF00))
{
Core.YieldCycle();
}
return absoluteYAddress;
}
/// <summary>
/// Argument is stored at the specified 16-bit address + Y register.
/// </summary>
protected virtual void ExecuteInAbsoluteYMode()
{
ThrowNotImplementedException("Absolute Y");
}
/// <summary>
/// Arguments length: 2, Cycles: 4.
/// </summary>
protected ushort ReadAddressInIndirectMode()
{
// 2 cycles
var indirectAddress = ReadAddressInAbsoluteMode();
// 1 cycle
var low = Core.Bus.Read(indirectAddress);
// 1 cycle
var high = Core.Bus.Read((ushort)(indirectAddress + 1)) << 8;
return (ushort)(high | low);
}
/// <summary>
/// Address of the argument is stored at the specified 16-bit address.
/// </summary>
protected virtual void ExecuteInIndirectMode()
{
ThrowNotImplementedException("Indirect");
}
/// <summary>
/// Arguments length: 1, Cycles: 4.
/// </summary>
protected ushort ReadAddressInIndexedIndirectMode()
{
// 1 cycle
var tableAddress = ReadAddressInZeroPageMode();
// 1 cycle
var indirectAddress = (byte)(tableAddress + Core.Registers.IndexRegisterX);
Core.YieldCycle();
// 1 cycle
var low = Core.Bus.Read(indirectAddress);
// 1 cycle
var high = Core.Bus.Read((ushort)(indirectAddress + 1)) << 8;
return (ushort)(high | low);
}
/// <summary>
/// Address of the argument is stored in the table (0 page) at X register index.
/// </summary>
protected virtual void ExecuteInIndexedIndirectMode()
{
ThrowNotImplementedException("Indexed Indirect");
}
/// <summary>
/// Arguments length: 1, Cycles: 3 + 1B.
/// </summary>
protected ushort ReadAddressInIndirectIndexedMode(bool forceBoundaryCheck = false)
{
// 1 cycle
var indirectAddress = ReadAddressInZeroPageMode();
// 1 cycle
var low = Core.Bus.Read(indirectAddress);
// 1 cycle
var high = Core.Bus.Read((ushort)(indirectAddress + 1)) << 8;
var realAddress = (ushort)(high | low);
// 1 cycle if page boundary crossed
var realYAddress = (ushort)(realAddress + Core.Registers.IndexRegisterY);
if (forceBoundaryCheck || (realAddress & 0xFF00) != (realYAddress & 0xFF00))
{
Core.YieldCycle();
}
return realYAddress;
}
/// <summary>
/// Address of the argument is stored at the specified address (0 page) + Y register index.
/// </summary>
protected virtual void ExecuteInIndirectIndexedMode()
{
ThrowNotImplementedException("Indirect Indexed");
}
private void ThrowNotImplementedException(string requestedAddressingMode)
{
throw new NotImplementedException($"{requestedAddressingMode} mode for {this} instruction is not supported.");
}
private InstructionExecutor GetExecutor()
{
switch (AddressingMode)
{
case AddressingMode.Implicit: return ExecuteInImplicitMode;
case AddressingMode.Accumulator: return ExecuteInAccumulatorMode;
case AddressingMode.Immediate: return ExecuteInImmediateMode;
case AddressingMode.ZeroPage: return ExecuteInZeroPageMode;
case AddressingMode.ZeroPageX: return ExecuteInZeroPageXMode;
case AddressingMode.ZeroPageY: return ExecuteInZeroPageYMode;
case AddressingMode.Relative: return ExecuteInRelativeMode;
case AddressingMode.Absolute: return ExecuteInAbsoluteMode;
case AddressingMode.AbsoluteX: return ExecuteInAbsoluteXMode;
case AddressingMode.AbsoluteY: return ExecuteInAbsoluteYMode;
case AddressingMode.Indirect: return ExecuteInIndirectMode;
case AddressingMode.IndexedIndirect: return ExecuteInIndexedIndirectMode;
case AddressingMode.IndirectIndexed: return ExecuteInIndirectIndexedMode;
}
throw new ArgumentOutOfRangeException($"Can't find proper instruction executor for {AddressingMode.ToString().Smash()} in {this} instruction.");
}
}
}