summaryrefslogtreecommitdiff
path: root/instruction.h
blob: bdb337c7d334cc9053b9ae00bda618bfca35eca6 (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
//void fXXX(ADDRESSING addr, int val)

// Considering making instruction functions return an int of
// how many cycles passed

struct instruction_data {
	int length;
	int cycles;
};

int getLength(Addressing addr){
	switch(addr){
		case eZeroPage: case eZeroPageIndexedX: case eZeroPageIndexedY:
			return 3;
		default:
			return 2;
	}
}


// Load and Store Instructions

instruction_data fLDA(Addressing addr, address val){
	acc = fAddressing(addr, val);
	instruction_data d; d.length = getLength(addr);
	
}

instruction_data fLDX(Addressing addr, address val){
	X = fAddressing(addr, val);
}

instruction_data fLDY(Addressing addr, address val){
	Y = fAddressing(addr, val);
}

instruction_data fSTA(Addressing addr, address val){
	Memory[(fAddressing(addr, val))] = acc;
}

instruction_data fSTX(Addressing addr, address val){
	Memory[(fAddressing(addr, val))] = X;
}

instruction_data fSTY(Addressing addr, address val){
	Memory[(fAddressing(addr, val))] = Y;
}

// Arithmetic Instructions

instruction_data fADC(Addressing addr, address val){
	int buffer = acc + fAddressing(addr, val);
	setFlagV(buffer, acc);

	if (buffer > 255){
		flagSet(flag_C);
	}else{
		flagClear(flag_C);
	}

	acc += fAddressing(addr, val);
	setFlagN(acc);
	setFlagZ(acc);
}

instruction_data fSBC(Addressing addr, address val){
	int buffer = acc - fAddressing(addr, val);
	setFlagV(buffer, acc);

	if (buffer < 0){
		flagSet(flag_C);
	}else{
		flagClear(flag_C);
	}
	acc -= fAddressing(addr, val);
	setFlagN(acc);
	setFlagZ(acc);
}

//Increment and Decrement Instructions

instruction_data fINC(Addressing addr, address val){
	Memory[x]++;
	setFlagD(Memory[x]);
	setFlagZ(Memory[x]);
}

instruction_data fINX(){
	X++;
	setFlagD(X);
	setFlagZ(X);
}

instruction_data fINY(){
	Y++;
	setFlagD(Y);
	setFlagZ(Y);
}

instruction_data fDEC(Addressing addr, address val){
	Memory[x]--;
	setFlagD(Memory[x]);
	setFlagZ(Memory[x]);
}

instruction_data fDEX(){
	X--;
	setFlagD(X);
	setFlagZ(X);
}

instruction_data fDEY(){
	Y--;
	setFlagD(Y);
	setFlagZ(Y);
}

// Logical Instructions

instruction_data fAND(Addressing addr, address val){
	acc = acc & fAddressing(addr, val);
	setFlagN();
	setFlagZ(acc);
}

instruction_data fORA(Addressing addr, address val){
	acc = acc | fAddressing(addr, val);
	setFlagN();
	setFlagZ(acc);
}

instruction_data fEOR(Addressing addr, address val){
	acc = acc ^ fAddressing(addr, val);
	setFlagN(acc);
	setFlagZ(acc);
}

// Jump, Branch, Compare, and Test Bits

instruction_data fJMP(address val){
	S = val;	
}

instruction_data fBCC(signed char val){
	if (getFlag(flag_C) == 0) S += val;
}

instruction_data fBCS(signed char val){
	if (getFlag(flag_C) == 1) S += val;
}

instruction_data fBEQ(signed char val){
	if (getFlag(flag_Z) == 1) S += val;
}

instruction_data fBNE(signed char val){
	if (getFlag(flag_Z) == 0) S += val;
}

instruction_data fBMI(signed char val){
	if (getFlag(flag_N) == 1) S += val;
}

instruction_data fBPL(signed char val){
	if (getFlag(flag_N) == 0) S += val;
}

instruction_data fBVS(signed char val){
	if (getFlag(flag_V) == 1) S += val;
}

instruction_data fBVC(signed char val){
	if (getFlag(flag_V) == 0) S += val;
}

instruction_data fCMP(address val){
	if (acc < Memory[val]){
		flagSet(flag_N);	flagClear(flag_Z);	flagClear(flag_C);
	}if (acc == Memory[val]){
		flagClear(flag_N);	flagSet(flag_Z);	flagClear(flag_C);
	}if (acc > Memory[val]){
		flagClear(flag_N);	flagClear(flag_Z);	flagSet(flag_C);
	}
}

instruction_data fCPX(address val){
	if (X < Memory[val]){
		flagSet(flag_N);	flagClear(flag_Z);	flagClear(flag_C);
	}if (X == Memory[val]){
		flagClear(flag_N);	flagSet(flag_Z);	flagClear(flag_C);
	}if (X > Memory[val]){
		flagClear(flag_N);	flagClear(flag_Z);	flagSet(flag_C);
	}
}

instruction_data fCPY(address val){
	if (Y < Memory[val]){
		flagSet(flag_N);	flagClear(flag_Z);	flagClear(flag_C);
	}if (Y == Memory[val]){
		flagClear(flag_N);	flagSet(flag_Z);	flagClear(flag_C);
	}if (Y > Memory[val]){
		flagClear(flag_N);	flagClear(flag_Z);	flagSet(flag_C);
	}
}

instruction_data fBIT(address 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

instruction_data fASL(Addressing addr, address val){
	setFlag(flag_C, (val & 0x80));
	acc = (val << 1);
	setFlagN(acc);
	setFlagZ(acc);
}

instruction_data fASL(Addressing addr, address val){
	setFlag(flag_C, (val & 0x01));
	acc = (val >> 1);
	setFlagN(acc);
	setFlagZ(acc);
}

instruction_data fROL(Addressing addr, address val){
	setFlag(flag_C, (val & 0x80));
	acc = (val << 1);
	acc |= (getFlag(flag_C) * 0x01);
	setFlagN(acc);
	setFlagZ(acc);
}

instruction_data fROR(Addressing addr, address val){
	setFlag(flag_C, (val & 0x01));
	acc = (val >> 1);
	acc |= (getFlag(flag_C) * 0x80);
	setFlagN(acc);
	setFlagZ(acc);
}

// Transfer Instructions

instruction_data fTAX(){
	X = acc;
	setFlagN(X);
	setFlagZ(X);
}

instruction_data fTAY(){
	Y = acc;
	setFlagN(Y);
	setFlagZ(Y);
}

instruction_data fTXA(){
	acc = X;
	setFlagN(acc);
	setFlagZ(acc);
}

instruction_data fTYA(){
	acc = Y;
	setFlagN(acc);
	setFlagZ(acc);
}

// Stack Instructions

instruction_data fTSX(){
	X = S;
}

instruction_data fTXS(){
	S = X;
}

instruction_data fPHA(){
	
}

instruction_data fPHP(){
	
}

instruction_data fPLA(){
	
}

instruction_data fPLP(){
	
}

// Subroutine Instructions



// Set/Reset Insutrctions

instruction_data fCLC(){
	flagClear(flag_C);
	return {1, 2};
}

instruction_data fCLD(){
	flagClear(flag_D);
	return {1, 2};
}

instruction_data fCLI(){
	flagClear(flag_I);
	return {1, 2};
}

instruction_data fCLV(){
	flagClear(flag_V);
	return {1, 2};
}

instruction_data fSEC(){
	flagSet(flag_C);
	return {1, 2};
}

instruction_data fSED(){
	flagSet(flag_D);
	return {1, 2};
}

instruction_data fSEI(){
	flagSet(flag_I);
	return {1, 2};
}

// NOP/BRK Instructions

instruction_data fNOP(){
	return {1, 2};
}

instruction_data fBRK(){
	flagSet(flag_B);
	return {1, 7};
}



/*
void runInstruction(int code, int val){
	switch(code){
		
	}

}

*/