summaryrefslogtreecommitdiff
path: root/src/cpu/instructions.c
blob: 0f002d39d14ef2e49d355eccac7c324b66341ad8 (plain)
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
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
//	instructions.c
//	Definition of all instruction functions, handling effect of instruction and flags.

#include"instructions.h"

/*	TO DO

!!!!!!!! CHECK THAT idata.value IS USED ACROSS ALL FUNCTIONS, NOT VAL !!!!!!!!!!!!!!
						~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Fix all functions before performing testing

*/


AddData idata;

// Load and Store Instructions

void fLDA(Addressing addr){
	acc = idata.value;
	SetFlagN(acc);
	SetFlagZ(acc);
}

void fLDX(Addressing addr){
	X = idata.value;
	SetFlagN(X);
	SetFlagZ(X);
}

void fLDY(Addressing addr){
	Y = idata.value;
	SetFlagN(Y);
	SetFlagZ(Y);
}

void fSTA(Addressing addr){
	SetMemory(idata.add, acc);
}

void fSTX(Addressing addr){
	SetMemory(idata.add, X);
}

void fSTY(Addressing addr){
	SetMemory(idata.add, Y);
}

// Arithmetic Instructions

void fADC(Addressing addr){
	byte buffer = acc + idata.value + getFlag(flag_C);

	SetFlagV(buffer, acc);

	SetFlag(flag_C, (buffer < acc) ? 1 : 0);
	
	acc = buffer;
	SetFlagN(acc);
	SetFlagZ(acc);
}

void fSBC(Addressing addr){
	byte buffer = acc - idata.value - !getFlag(flag_C);
        
	SetFlagV(buffer, acc);

	SetFlag(flag_C, (buffer > acc) ? 0 : 1);

	acc = buffer;
	
	SetFlagN(acc);
	SetFlagZ(acc);
}

//Increment and Decrement Instructions

void fINC(Addressing addr){
	byte a = idata.value;
	SetMemory(idata.add, ++a);
	SetFlagN(a);
	SetFlagZ(a);
}

void fINX(Addressing addr){
	X++;
	SetFlagN(X);
	SetFlagZ(X);
}

void fINY(Addressing addr){
	Y++;
	SetFlagN(Y);
	SetFlagZ(Y);
}

void fDEC(Addressing addr){
	byte a = idata.value;
	SetMemory(idata.add, --a);
	SetFlagN(a);
	SetFlagZ(a);
}

void fDEX(Addressing addr){
	X--;
	SetFlagN(X);
	SetFlagZ(X);
}

void fDEY(Addressing addr){
	Y--;
	SetFlagN(Y);
	SetFlagZ(Y);
}

// Logical Instructions

void fAND(Addressing addr){
	acc &= idata.value;
	SetFlagN(acc);
	SetFlagZ(acc);
}

void fORA(Addressing addr){
	acc |= idata.value;
	SetFlagN(acc);
	SetFlagZ(acc);
}

void fEOR(Addressing addr){
	acc ^= idata.value;
	SetFlagN(acc);
	SetFlagZ(acc);
}

// Jump, Branch, Compare, and Test Bits

void fJMP(Addressing addr){
	PC = idata.add - idata.length;
}

void fBCC(Addressing addr){
	if (getFlag(flag_C) == 0) PC += (char)idata.value; 
}

void fBCS(Addressing addr){
	if (getFlag(flag_C) == 1) PC += (char)idata.value;
}

void fBEQ(Addressing addr){
	if (getFlag(flag_Z) == 1) PC += (char)idata.value;
}

void fBNE(Addressing addr){
	if (getFlag(flag_Z) == 0) PC += (char)idata.value;
}

void fBMI(Addressing addr){
	if (getFlag(flag_N) == 1) PC += (char)idata.value;
}

void fBPL(Addressing addr){
	if (getFlag(flag_N) == 0) PC += (char)idata.value;
}

void fBVS(Addressing addr){
	if (getFlag(flag_V) == 1) PC += (char)idata.value;
}

void fBVC(Addressing addr){
	if (getFlag(flag_V) == 0) PC += (char)idata.value;
}

void fCMP(Addressing addr){
	SetFlag(flag_C, (acc >= idata.value) ? 1 : 0);
	SetFlag(flag_Z, (acc == idata.value) ? 1 : 0);
	SetFlag(flag_N, (acc  < idata.value) ? 1 : 0);
}

void fCPX(Addressing addr){
	SetFlag(flag_C, (X >= idata.value) ? 1 : 0);
	SetFlag(flag_Z, (X == idata.value) ? 1 : 0);
	SetFlag(flag_N, (X  < idata.value) ? 1 : 0);
}

void fCPY(Addressing addr){
	SetFlag(flag_C, (Y >= idata.value) ? 1 : 0);
	SetFlag(flag_Z, (Y == idata.value) ? 1 : 0);
	SetFlag(flag_N, (Y  < idata.value) ? 1 : 0);
}

void fBIT(Addressing addr){
	SetFlag(flag_N, ((idata.value & flag_N) != 0));
	SetFlag(flag_V, ((idata.value & flag_V) != 0));
	SetFlag(flag_Z, ((idata.value &    acc) == 0));
}

// Shift and Rotate Instructions

void fASL(Addressing addr){
	byte m = (addr == eAccumulator) ? acc : idata.value;
	
	SetFlag(flag_C, (m & 0b10000000));
	m = (m << 1) & 0b11111110;
	SetFlagN(m);
	SetFlagZ(m);
	
	(addr == eAccumulator)
	?	acc = m
	:	SetMemory(idata.add, m);
}

void fLSR(Addressing addr) {
	byte m = (addr == eAccumulator) ? acc : idata.value;
	
	SetFlag(flag_C, (m & 0b00000001));
	m = (m >> 1) & 0b01111111;
	SetFlagN(m);
	SetFlagZ(m);
	
	(addr == eAccumulator)
	?	acc = m
	:	SetMemory(idata.add, m);
}

void fROL(Addressing addr){
	byte m = (addr == eAccumulator) ? acc : idata.value;
	
	byte flag_store = (m & 0b10000000);
	//SetFlag(flag_C, (m & 0b10000000));
	m = (m << 1) & 0b11111110;
	m |= (getFlag(flag_C)) ? 0b00000001 : 0;
	SetFlag(flag_C, flag_store);
	SetFlagN(m);
	SetFlagZ(m);
	
	(addr == eAccumulator)
	?	acc = m
	:	SetMemory(idata.add, m);
}

void fROR(Addressing addr){
	byte m = (addr == eAccumulator) ? acc : idata.value;

	byte flag_store = (m & 0b00000001);
	//SetFlag(flag_C, (m & 0b00000001));
	m = (m >> 1) & 0b01111111;
	m |= (getFlag(flag_C)) ? 0b10000000 : 0;
	SetFlag(flag_C, flag_store);
	SetFlagN(m);
	SetFlagZ(m);
	
	
	(addr == eAccumulator)
	?	acc = m
	:	SetMemory(idata.add, m);
}

// Transfer Instructions

void fTAX(Addressing addr){
	X = acc;
	SetFlagN(X);
	SetFlagZ(X);
}

void fTAY(Addressing addr){
	Y = acc;
	SetFlagN(Y);
	SetFlagZ(Y);
}

void fTXA(Addressing addr){
	acc = X;
	SetFlagN(acc);
	SetFlagZ(acc);
}

void fTYA(Addressing addr){
	acc = Y;
	SetFlagN(acc);
	SetFlagZ(acc);
}

// Stack Instructions

void fTSX(Addressing addr){
	X = S;
}

void fTXS(Addressing addr){
	S = X;
}

void fPHA(Addressing addr){
	SetStack(acc);
}

void fPHP(Addressing addr){
	SetStack(P);
}

void fPLA(Addressing addr){
	acc = GetStack();
}

void fPLP(Addressing addr){
	P = GetStack();
}

// Subroutine Instructions

void fJSR(Addressing addr){
	SetStack ((PC+idata.length) >> 8);
	SetStack(((PC+idata.length) & 0x00FF) - 1);
	PC = idata.add;
	PC -= idata.length;
}

void fRTS(Addressing addr){
	PC  =  (address)(GetStack()) + 1;
	PC += ((address)(GetStack())) << 8;
	PC -= idata.length;
}

void fRTI(Addressing addr){
	P = GetStack(); //NEED TO FIX
	PC  = (address)(GetStack());
	PC += (address)(GetStack() << 8);
}

// Set/Reset Insutrctions

void fCLC(Addressing addr){
	SetFlag(flag_C, 0);
}

void fCLD(Addressing addr){
	SetFlag(flag_D, 0);
}

void fCLI(Addressing addr){
	SetFlag(flag_I, 0);
}

void fCLV(Addressing addr){
	SetFlag(flag_V, 0);
}

void fSEC(Addressing addr){
	SetFlag(flag_C, 1);
}

void fSED(Addressing addr){
	SetFlag(flag_D, 1);
}

void fSEI(Addressing addr){
	SetFlag(flag_I, 1);
}

// NOP/BRK Instructions

void fNOP(Addressing addr){
}

void fBRK(Addressing addr){
	SetStack((((PC+2) & 0xFF00) >> 8));
	SetStack((PC+2) & 0x00FF);
	SetStack(P);
	PC = (address)(GetMemory(0xFFFE));
	PC += ((address)(GetMemory(0xFFFF)) << 8);
}