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
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
|
// instruction.h
// Definition of all instruction functions, handling effect of instruction and flags.
// array/map of pointers which all point
// to the functions which the index corresponds to.
// use that like a sort of map
//Instruction Data
AddData idata;
// Load and Store Instructions
void fLDA(Addressing addr, address val){ idata = fAddress(addr, val);
acc = idata.value;
}
void fLDX(Addressing addr, address val){ idata = fAddress(addr, val);
X = idata.value;
}
void fLDY(Addressing addr, address val){ idata = fAddress(addr, val);
Y = idata.value;
}
void fSTA(Addressing addr, address val){ idata = fAddress(addr, val);
Memory[idata.add] = acc;
}
void fSTX(Addressing addr, address val){ idata = fAddress(addr, val);
Memory[idata.add] = X;
}
void fSTY(Addressing addr, address val){ idata = fAddress(addr, val);
Memory[idata.add] = Y;
}
// Arithmetic Instructions
void fADC(Addressing addr, address val){ idata = fAddress(addr, val);
int buffer = acc + idata.value;
setFlagV(buffer, acc);
if (buffer > 255)
flagSet(flag_C);
else
flagClear(flag_C);
acc += idata.value;
setFlagN(acc);
setFlagZ(acc);
}
void fSBC(Addressing addr, address val){ idata = fAddress(addr, val);
int buffer = acc - idata.value;
setFlagV(buffer, acc);
if (buffer < 0)
flagSet(flag_C);
else
flagClear(flag_C);
acc -= idata.value;
setFlagN(acc);
setFlagZ(acc);
}
//Increment and Decrement Instructions
void fINC(Addressing addr, address val){ idata = fAddress(addr, val);
Memory[idata.add]++;
setFlagN(Memory[idata.add]);
setFlagZ(Memory[idata.add]);
}
void fINX(Addressing addr, address val){ idata = fAddress(addr, val);
X++;
setFlagN(X);
setFlagZ(X);
}
void fINY(Addressing addr, address val){ idata = fAddress(addr, val);
Y++;
setFlagN(Y);
setFlagZ(Y);
}
void fDEC(Addressing addr, address val){ idata = fAddress(addr, val);
Memory[idata.add]--;
setFlagN(Memory[idata.add]);
setFlagZ(Memory[idata.add]);
}
void fDEX(Addressing addr, address val){ idata = fAddress(addr, val);
X--;
setFlagN(X);
setFlagZ(X);
}
void fDEY(Addressing addr, address val){ idata = fAddress(addr, val);
Y--;
setFlagN(Y);
setFlagZ(Y);
}
// Logical Instructions
void fAND(Addressing addr, address val){ idata = fAddress(addr, val);
acc &= idata.value;
setFlagN(acc);
setFlagZ(acc);
}
void fORA(Addressing addr, address val){ idata = fAddress(addr, val);
acc |= idata.value;
setFlagN(acc);
setFlagZ(acc);
}
void fEOR(Addressing addr, address val){ idata = fAddress(addr, val);
acc ^= idata.value;
setFlagN(acc);
setFlagZ(acc);
}
// Jump, Branch, Compare, and Test Bits
void fJMP(Addressing addr, address val){ idata = fAddress(addr, val);
PC = val;
}
void fBCC(Addressing addr, address val){ idata = fAddress(addr, val); //FINISH ALL BRANCH INSTRUCTIONS
//signed char val down to BVC
if (getFlag(flag_C) == 0) PC += val;
}
void fBCS(Addressing addr, address val){ idata = fAddress(addr, val);
if (getFlag(flag_C) == 1) PC += val;
}
void fBEQ(Addressing addr, address val){ idata = fAddress(addr, val);
if (getFlag(flag_Z) == 1) PC += val;
}
void fBNE(Addressing addr, address val){ idata = fAddress(addr, val);
if (getFlag(flag_Z) == 0) PC += val;
}
void fBMI(Addressing addr, address val){ idata = fAddress(addr, val);
if (getFlag(flag_N) == 1) PC += val;
}
void fBPL(Addressing addr, address val){ idata = fAddress(addr, val);
if (getFlag(flag_N) == 0) PC += val;
}
void fBVS(Addressing addr, address val){ idata = fAddress(addr, val);
if (getFlag(flag_V) == 1) PC += val;
}
void fBVC(Addressing addr, address val){ idata = fAddress(addr, val);
if (getFlag(flag_V) == 0) PC += val;
}
void fCMP(Addressing addr, address val){ idata = fAddress(addr, val);
if (acc < idata.value){
flagSet(flag_N); flagClear(flag_Z); flagClear(flag_C);
}if (acc == idata.value){
flagClear(flag_N); flagSet(flag_Z); flagClear(flag_C);
}if (acc > idata.value){
flagClear(flag_N); flagClear(flag_Z); flagSet(flag_C);
}
}
void fCPX(Addressing addr, address val){ idata = fAddress(addr, val);
if (X < idata.value){
flagSet(flag_N); flagClear(flag_Z); flagClear(flag_C);
}if (X == idata.value){
flagClear(flag_N); flagSet(flag_Z); flagClear(flag_C);
}if (X > idata.value){
flagClear(flag_N); flagClear(flag_Z); flagSet(flag_C);
}
}
void fCPY(Addressing addr, address val){ idata = fAddress(addr, val);
if (Y < idata.value){
flagSet(flag_N); flagClear(flag_Z); flagClear(flag_C);
}if (Y == idata.value){
flagClear(flag_N); flagSet(flag_Z); flagClear(flag_C);
}if (Y > idata.value){
flagClear(flag_N); flagClear(flag_Z); flagSet(flag_C);
}
}
//Need to double check the function of this instruction
void fBIT(Addressing addr, address val){ idata = fAddress(addr, val);
setFlag(flag_N, (Memory[val] & flag_N));
setFlag(flag_V, (Memory[val] & flag_V));
if (((Memory[val] & flag_N) & (Memory[val] & flag_V)) == 0) {
flagSet(flag_Z);
} else {
flagSet(flag_Z);
}
}
// Shift and Rotate Instructions
void fASL(Addressing addr, address val){ idata = fAddress(addr, val);
setFlag(flag_C, (val & 0x80));
acc = (val << 1);
setFlagN(acc);
setFlagZ(acc);
}
void fLSR(Addressing addr, address val){ idata = fAddress(addr, val);
setFlag(flag_C, (val & 0x01));
acc = (val >> 1);
setFlagN(acc);
setFlagZ(acc);
}
void fROL(Addressing addr, address val){ idata = fAddress(addr, val);
setFlag(flag_C, (val & 0x80));
acc = (val << 1);
acc |= (getFlag(flag_C) * 0x01);
setFlagN(acc);
setFlagZ(acc);
}
void fROR(Addressing addr, address val){ idata = fAddress(addr, val);
setFlag(flag_C, (val & 0x01));
acc = (val >> 1);
acc |= (getFlag(flag_C) * 0x80);
setFlagN(acc);
setFlagZ(acc);
}
// Transfer Instructions
void fTAX(Addressing addr, address val){ idata = fAddress(addr, val);
X = acc;
//setFlagN(X);
//setFlagZ(X);
}
void fTAY(Addressing addr, address val){ idata = fAddress(addr, val);
Y = acc;
//setFlagN(Y);
//setFlagZ(Y);
}
void fTXA(Addressing addr, address val){ idata = fAddress(addr, val);
acc = X;
setFlagN(acc);
setFlagZ(acc);
}
void fTYA(Addressing addr, address val){ idata = fAddress(addr, val);
acc = Y;
setFlagN(acc);
setFlagZ(acc);
}
// Stack Instructions
void fTSX(Addressing addr, address val){ idata = fAddress(addr, val);
X = S;
}
void fTXS(Addressing addr, address val){ idata = fAddress(addr, val);
S = X;
}
void fPHA(Addressing addr, address val){ idata = fAddress(addr, val);
Memory[0x01FF-S] = acc;
S++;
}
void fPHP(Addressing addr, address val){ idata = fAddress(addr, val);
Memory[0x01FF-S] = P;
S++;
}
void fPLA(Addressing addr, address val){ idata = fAddress(addr, val);
S--;
acc = Memory[0x01FF-S];
}
void fPLP(Addressing addr, address val){ idata = fAddress(addr, val);
S--;
P = Memory[0x01FF-S];
}
// Subroutine Instructions
void fJSR(Addressing addr, address val){ idata = fAddress(addr, val);
Memory[0x01FF-S] = (idata.add-1);
S++;
PC = idata.add;
}
void fRTS(Addressing addr, address val){ idata = fAddress(addr, val);
}
void fRTI(Addressing addr, address val){ idata = fAddress(addr, val);
}
// Set/Reset Insutrctions
void fCLC(Addressing addr, address val){ idata = fAddress(addr, val);
flagClear(flag_C);
}
void fCLD(Addressing addr, address val){ idata = fAddress(addr, val);
flagClear(flag_D);
}
void fCLI(Addressing addr, address val){ idata = fAddress(addr, val);
flagClear(flag_I);
}
void fCLV(Addressing addr, address val){ idata = fAddress(addr, val);
flagClear(flag_V);
}
void fSEC(Addressing addr, address val){ idata = fAddress(addr, val);
flagSet(flag_C);
}
void fSED(Addressing addr, address val){ idata = fAddress(addr, val);
flagSet(flag_D);
}
void fSEI(Addressing addr, address val){ idata = fAddress(addr, val);
flagSet(flag_I);
}
// NOP/BRK Instructions
void fNOP(Addressing addr, address val){ idata = fAddress(addr, val);
}
void fBRK(Addressing addr, address val){ idata = fAddress(addr, val);
flagSet(flag_B);
}
|