summaryrefslogtreecommitdiff
path: root/instruction.h
blob: c1b4843555951f7ff7b3752d68883a25cc0e09ad (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
// array/map of pointers which all point
// to the functions which the index corresponds to.
// use that like a sort of map


AddData idata;	//Instruction Data

// Load and Store Instructions
// the load and store instructions are currently
// used to express how data structures will work eventually.

void fLDA(Addressing addr, address val){ idata = fAddress(addr, val);
	acc = idata.val;
}

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.value] = acc;
}

void fSTX(Addressing addr, address val){ idata = fAddress(addr, val);
	Memory[idata.value] = X;
}

void fSTY(Addressing addr, address val){ idata = fAddress(addr, val);
	Memory[idata.value] = Y;
}

// Arithmetic Instructions

void fADC(Addressing addr, address val){ idata = fAddress(addr, val);
	int buffer = acc + fAddress(addr, val);
	setFlagV(buffer, acc);

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

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

void fSBC(Addressing addr, address val){ idata = fAddress(addr, val);
	int buffer = acc - fAddress(addr, val);
	setFlagV(buffer, acc);

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

//Increment and Decrement Instructions

void fINC(Addressing addr, address val){ idata = fAddress(addr, val);
	Memory[x]++;
	setFlagD(Memory[x]);
	setFlagZ(Memory[x]);
}

void fINX(Addressing addr, address val){ idata = fAddress(addr, val);
	X++;
	setFlagD(X);
	setFlagZ(X);
}

void fINY(Addressing addr, address val){ idata = fAddress(addr, val);
	Y++;
	setFlagD(Y);
	setFlagZ(Y);
}

void fDEC(Addressing addr, address val){ idata = fAddress(addr, val);
	Memory[x]--;
	setFlagD(Memory[x]);
	setFlagZ(Memory[x]);
}

void fDEX(Addressing addr, address val){ idata = fAddress(addr, val);
	X--;
	setFlagD(X);
	setFlagZ(X);
}

void fDEY(Addressing addr, address val){ idata = fAddress(addr, val);
	Y--;
	setFlagD(Y);
	setFlagZ(Y);
}

// Logical Instructions

void fAND(Addressing addr, address val){ idata = fAddress(addr, val);
	acc = acc & fAddress(addr, val);
	setFlagN();
	setFlagZ(acc);
}

void fORA(Addressing addr, address val){ idata = fAddress(addr, val);
	acc = acc | fAddress(addr, val);
	setFlagN();
	setFlagZ(acc);
}

void fEOR(Addressing addr, address val){ idata = fAddress(addr, val);
	acc = acc ^ fAddress(addr, val);
	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);
	//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 < 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);
	}
}

void fCPX(Addressing addr, address val){ idata = fAddress(addr, 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);
	}
}

void fCPY(Addressing addr, address val){ idata = fAddress(addr, 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);
	}
}

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 fASL(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);
	
}

void fPHP(Addressing addr, address val){ idata = fAddress(addr, val);
	
}

void fPLA(Addressing addr, address val){ idata = fAddress(addr, val);
	
}

void fPLP(Addressing addr, address val){ idata = fAddress(addr, val);
	
}

// Subroutine Instructions



// Set/Reset Insutrctions

void fCLC(Addressing addr, address val){ idata = fAddress(addr, val);
	flagClear(flag_C);
	return {1, 2};
}

void fCLD(Addressing addr, address val){ idata = fAddress(addr, val);
	flagClear(flag_D);
	return {1, 2};
}

void fCLI(Addressing addr, address val){ idata = fAddress(addr, val);
	flagClear(flag_I);
	return {1, 2};
}

void fCLV(Addressing addr, address val){ idata = fAddress(addr, val);
	flagClear(flag_V);
	return {1, 2};
}

void fSEC(Addressing addr, address val){ idata = fAddress(addr, val);
	flagSet(flag_C);
	return {1, 2};
}

void fSED(Addressing addr, address val){ idata = fAddress(addr, val);
	flagSet(flag_D);
	return {1, 2};
}

void fSEI(Addressing addr, address val){ idata = fAddress(addr, val);
	flagSet(flag_I);
	return {1, 2};
}

// NOP/BRK Instructions

void fNOP(Addressing addr, address val){ idata = fAddress(addr, val);
	return {1, 2};
}

void fBRK(Addressing addr, address val){ idata = fAddress(addr, val);
	flagSet(flag_B);
	return {1, 7};
}