summaryrefslogtreecommitdiff
path: root/opcode.c
blob: ea3f726d1ca4ff19cadab250e94d6584bd069db8 (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
#include "opcode.h"
void setps(struct sux *cpu, uint8_t thread) {
	(cpu->c[thread]) ? (cpu->ps |= (C << 8*thread)) : (cpu->ps &= ~(C << 8*thread));
	(cpu->z[thread]) ? (cpu->ps |= (Z << 8*thread)) : (cpu->ps &= ~(Z << 8*thread));
	(cpu->i[thread]) ? (cpu->ps |= (I << 8*thread)) : (cpu->ps &= ~(I << 8*thread));
	(cpu->s[thread]) ? (cpu->ps |= (S << 8*thread)) : (cpu->ps &= ~(S << 8*thread));
	(cpu->v[thread]) ? (cpu->ps |= (V << 8*thread)) : (cpu->ps &= ~(V << 8*thread));
	(cpu->n[thread]) ? (cpu->ps |= (N << 8*thread)) : (cpu->ps &= ~(N << 8*thread));
}

void adc(struct sux *cpu, uint64_t adr, uint8_t thread) {
	uint64_t value = (uint64_t)addr[adr]
			| (uint64_t)addr[adr+1] << 8
			| (uint64_t)addr[adr+2] << 16
			| (uint64_t)addr[adr+3] << 24
			| (uint64_t)addr[adr+4] << 32
			| (uint64_t)addr[adr+5] << 40
			| (uint64_t)addr[adr+6] << 48
			| (uint64_t)addr[adr+7] << 56;
	uint64_t sum = cpu->a[thread]+value+cpu->c[thread];
	cpu->z[thread] = (sum == 0);
	cpu->n[thread] = (sum >> 63);
	cpu->v[thread] = !((cpu->a[thread]^value) & 0x8000000000000000) && ((cpu->a[thread]^sum) & 0x8000000000000000);
	cpu->c[thread] = (sum < value);
	cpu->a[thread] = sum;
	setps(cpu, thread);
}

void sbc(struct sux *cpu, uint64_t adr, uint8_t thread) {
	uint64_t value = (uint64_t)addr[adr]
			| (uint64_t)addr[adr+1] << 8
			| (uint64_t)addr[adr+2] << 16
			| (uint64_t)addr[adr+3] << 24
			| (uint64_t)addr[adr+4] << 32
			| (uint64_t)addr[adr+5] << 40
			| (uint64_t)addr[adr+6] << 48
			| (uint64_t)addr[adr+7] << 56;
	uint64_t sum = cpu->a[thread]-value-!cpu->c[thread];
	cpu->z[thread] = (sum == 0);
	cpu->n[thread] = (sum >> 63);
	cpu->v[thread] = ((cpu->a[thread]^value) & 0x8000000000000000) && ((cpu->a[thread]^sum) & 0x8000000000000000);
	cpu->c[thread] = (sum > value);
	cpu->a[thread] = sum;
	setps(cpu, thread);
}

void mul(struct sux *cpu, uint64_t adr, uint8_t thread) {
	uint64_t value = (uint64_t)addr[adr]
			| (uint64_t)addr[adr+1] << 8
			| (uint64_t)addr[adr+2] << 16
			| (uint64_t)addr[adr+3] << 24
			| (uint64_t)addr[adr+4] << 32
			| (uint64_t)addr[adr+5] << 40
			| (uint64_t)addr[adr+6] << 48
			| (uint64_t)addr[adr+7] << 56;
	uint64_t sum = cpu->a[thread]*value+cpu->c[thread];
	cpu->z[thread] = (sum == 0);
	cpu->n[thread] = (sum >> 63);
	cpu->v[thread] = !((cpu->a[thread]^value) & 0x8000000000000000) && ((cpu->a[thread]^sum) & 0x8000000000000000);
	cpu->c[thread] = (!((cpu->a[thread]^sum) && (cpu->a[thread]^value)) || (cpu->a[thread] >= ((uint64_t)1 << 32) && value >= ((uint64_t)1 << 32)));
	cpu->a[thread] = sum;
	setps(cpu, thread);
}

void divd(struct sux *cpu, uint64_t adr, uint8_t thread) {
	uint64_t value = (uint64_t)addr[adr]
			| (uint64_t)addr[adr+1] << 8
			| (uint64_t)addr[adr+2] << 16
			| (uint64_t)addr[adr+3] << 24
			| (uint64_t)addr[adr+4] << 32
			| (uint64_t)addr[adr+5] << 40
			| (uint64_t)addr[adr+6] << 48
			| (uint64_t)addr[adr+7] << 56;
	uint64_t sum = cpu->a[thread]/value;
	cpu->z[thread] = (sum == 0);
	cpu->v[thread] = !((cpu->a[thread]^value) & 0x8000000000000000) && ((cpu->a[thread]^sum) & 0x8000000000000000);
	cpu->a[thread] = sum;
	setps(cpu, thread);
}

uint64_t and(struct sux *cpu, uint64_t value, uint8_t thread) {
	uint64_t sum;
	sum &= value;
	cpu->z[thread] = (sum == 0);
	cpu->n[thread] = (sum >> 63);
	setps(cpu, thread);
	return sum;
}

void and_addr(struct sux *cpu, uint64_t* const reg, uint64_t adr, uint8_t thread) {
	uint64_t value = (uint64_t)addr[adr]
			| (uint64_t)addr[adr+1] << 8
			| (uint64_t)addr[adr+2] << 16
			| (uint64_t)addr[adr+3] << 24
			| (uint64_t)addr[adr+4] << 32
			| (uint64_t)addr[adr+5] << 40
			| (uint64_t)addr[adr+6] << 48
			| (uint64_t)addr[adr+7] << 56;
	*reg &= value;
	cpu->z[thread] = (*reg == 0);
	cpu->n[thread] = (*reg >> 63);
	setps(cpu, thread);
}

uint64_t or(struct sux *cpu, uint64_t value, uint8_t thread) {
	uint64_t sum;
	sum |= value;
	cpu->z[thread] = (sum == 0);
	cpu->n[thread] = (sum >> 63);
	setps(cpu, thread);
	return sum;
}

void or_addr(struct sux *cpu, uint64_t* const reg, uint64_t adr, uint8_t thread) {
	uint64_t value = (uint64_t)addr[adr]
			| (uint64_t)addr[adr+1] << 8
			| (uint64_t)addr[adr+2] << 16
			| (uint64_t)addr[adr+3] << 24
			| (uint64_t)addr[adr+4] << 32
			| (uint64_t)addr[adr+5] << 40
			| (uint64_t)addr[adr+6] << 48
			| (uint64_t)addr[adr+7] << 56;
	*reg |= value;
	cpu->z[thread] = (*reg == 0);
	cpu->n[thread] = (*reg >> 63);
	setps(cpu, thread);
}

uint64_t xor(struct sux *cpu, uint64_t value, uint8_t thread) {
	uint64_t sum;
	sum ^= value;
	cpu->z[thread] = (sum == 0);
	cpu->n[thread] = (sum >> 63);
	setps(cpu, thread);
	return sum;
}

void xor_addr(struct sux *cpu, uint64_t* const reg, uint64_t adr, uint8_t thread) {
	uint64_t value = (uint64_t)addr[adr]
			| (uint64_t)addr[adr+1] << 8
			| (uint64_t)addr[adr+2] << 16
			| (uint64_t)addr[adr+3] << 24
			| (uint64_t)addr[adr+4] << 32
			| (uint64_t)addr[adr+5] << 40
			| (uint64_t)addr[adr+6] << 48
			| (uint64_t)addr[adr+7] << 56;
	*reg ^= value;
	cpu->z[thread] = (*reg == 0);
	cpu->n[thread] = (*reg >> 63);
	setps(cpu, thread);
}

void rol(struct sux *cpu, uint64_t adr, uint8_t thread) {
	uint64_t value = addr[adr];
	uint64_t sum = cpu->a[thread] << value;
	sum |= cpu->c[thread];
	cpu->z[thread] = (sum == 0);
	cpu->n[thread] = (sum >> 63);
	cpu->c[thread] = cpu->a[thread] >> (uint64_t)64-value;
	cpu->a[thread] = sum;
	setps(cpu, thread);
}

void ror(struct sux *cpu, uint64_t adr, uint8_t thread) {
	uint64_t value = addr[adr];
	uint64_t sum = cpu->a[thread] >> value;
	sum |= (uint64_t)cpu->c[thread] << (uint64_t)64-value;
	cpu->c[thread] = cpu->a[thread] & 1;
	cpu->a[thread] = sum;
	setps(cpu, thread);
}

void lsl(struct sux *cpu, uint64_t adr, uint8_t thread) {
	uint64_t value = addr[adr];
	uint64_t sum = (value < 64) ? cpu->a[thread] << value : 0;
	cpu->c[thread] = cpu->a[thread] >> 64-value;
	cpu->a[thread] = sum;
	setps(cpu, thread);
}

void lsr(struct sux *cpu, uint64_t adr, uint8_t thread) {
	uint64_t value = addr[adr];
	uint64_t sum = (value < 64) ? cpu->a[thread] >> value : 0;
	cpu->c[thread] = cpu->a[thread] & 1;
	cpu->a[thread] = sum;
	setps(cpu, thread);
}

void inc(struct sux *cpu, uint64_t *reg, uint8_t thread) {
	*reg += 1;
	cpu->z[thread] = (*reg == 0);
	cpu->n[thread] = (*reg >> 63);
	setps(cpu, thread);
}

void inc_addr(struct sux *cpu, uint64_t adr, uint8_t thread) {
	addr[adr]++;
}

void dec(struct sux *cpu, uint64_t *reg, uint8_t thread) {
	*reg -= 1;
	cpu->z[thread] = (*reg == 0);
	cpu->n[thread] = (*reg >> 63);
	setps(cpu, thread);
}

void dec_addr(struct sux *cpu, uint64_t adr, uint8_t thread) {
	addr[adr]--;
}

void stt(struct sux* const cpu, uint8_t value) {
	uint16_t tv = 0xFF50; /* Thread Vector. */
	uint8_t t = addr[value];
	cpu->crt |= t;
	for (uint8_t i = 0; i < 7; i++)
		if ((t >> i) & 1) {
			uint64_t adr = (uint64_t)addr[tv+(8*i)]
					| (uint64_t)addr[tv+1+(8*i)] << 8
					| (uint64_t)addr[tv+2+(8*i)] << 16
					| (uint64_t)addr[tv+3+(8*i)] << 24
					| (uint64_t)addr[tv+4+(8*i)] << 32
					| (uint64_t)addr[tv+5+(8*i)] << 40
					| (uint64_t)addr[tv+6+(8*i)] << 48
					| (uint64_t)addr[tv+7+(8*i)] << 56;
			cpu->pc[i+1] = adr;
		}
}

void ent(struct sux *cpu, uint8_t value) {
	uint8_t t = addr[value];
	cpu->crt &= ~t;
	for (uint8_t i = 0; i < 7; i++)
		if ((t >> i) & 1)
			cpu->pc[i+1] = cpu->pc[0]+(i+1);
}

void ld(struct sux *cpu, uint64_t *reg, uint64_t adr, uint8_t thread) {
	*reg = (uint64_t)addr[adr]
		| (uint64_t)addr[adr+1] << 8
		| (uint64_t)addr[adr+2] << 16
		| (uint64_t)addr[adr+3] << 24
		| (uint64_t)addr[adr+4] << 32
		| (uint64_t)addr[adr+5] << 40
		| (uint64_t)addr[adr+6] << 48
		| (uint64_t)addr[adr+7] << 56;
	cpu->z[thread] = (*reg == 0);
	cpu->n[thread] = (*reg >> 63);
	setps(cpu, thread);
}

void st(struct sux *cpu, uint64_t *reg, uint64_t adr, uint8_t thread) {
	addr[adr] = *reg & 0xFF;
	addr[adr+1] = *reg >> 8;
	addr[adr+2] = *reg >> 16;
	addr[adr+3] = *reg >> 24;
	addr[adr+4] = *reg >> 32;
	addr[adr+5] = *reg >> 40;
	addr[adr+6] = *reg >> 48;
	addr[adr+7] = *reg >> 56;
}

void push(struct sux *cpu, uint8_t value) {
	addr[STK_STADDR+cpu->sp] = value;
	cpu->sp--;
	addr[0xFF90] = cpu->sp & 0xFF;
	addr[0xFF91] = cpu->sp >> 8;
}

uint8_t pull(struct sux *cpu) {
	cpu->sp++;
	addr[0xFF90] = cpu->sp & 0xFF;
	addr[0xFF91] = cpu->sp >> 8;
	return addr[STK_STADDR+cpu->sp];
}

void cmp_addr(struct sux *cpu, uint64_t reg, uint64_t adr, uint8_t thread) {
	uint64_t value = (uint64_t)addr[adr]
			| (uint64_t)addr[adr+1] << 8
			| (uint64_t)addr[adr+2] << 16
			| (uint64_t)addr[adr+3] << 24
			| (uint64_t)addr[adr+4] << 32
			| (uint64_t)addr[adr+5] << 40
			| (uint64_t)addr[adr+6] << 48
			| (uint64_t)addr[adr+7] << 56;
	uint64_t sum = reg-value;
	cpu->n[thread] = (sum & 0x8000000000000000) ? 1 : 0;
	cpu->z[thread] = (sum == 0) ? 1 : 0;
	cpu->c[thread] = (sum > value) ? 1 : 0;
	setps(cpu, thread);
}

void cmp(struct sux *cpu, uint64_t reg1, uint64_t reg2, uint8_t thread) {
	uint64_t sum = reg1-reg2;
	cpu->n[thread] = (sum & 0x8000000000000000) ? 1 : 0;
	cpu->z[thread] = (sum == 0) ? 1 : 0;
	cpu->c[thread] = (sum > reg2) ? 1 : 0;
	setps(cpu, thread);
}

/* Branch if Flag Set. */
void bfs(struct sux *cpu, uint8_t flag, uint64_t adr, uint8_t thread) {
	if (flag)
		cpu->pc[thread] = adr;
}

/* Branch if Flag Clear. */
void bfc(struct sux *cpu, uint8_t flag, uint64_t adr, uint8_t thread) {
	if (!flag)
		cpu->pc[thread] = adr;
}

uint64_t immaddr(struct sux *cpu, uint8_t thread, uint8_t size) {
	uint64_t adr = cpu->pc[thread];
	cpu->pc[thread]+=size;
	return adr;
}

uint64_t absaddr(struct sux *cpu, uint8_t thread) {
	uint64_t adr =	(uint64_t)addr[cpu->pc[thread]]
			| (uint64_t)addr[cpu->pc[thread]+1] << 8
			| (uint64_t)addr[cpu->pc[thread]+2] << 16
			| (uint64_t)addr[cpu->pc[thread]+3] << 24
			| (uint64_t)addr[cpu->pc[thread]+4] << 32
			| (uint64_t)addr[cpu->pc[thread]+5] << 40
			| (uint64_t)addr[cpu->pc[thread]+6] << 48
			| (uint64_t)addr[cpu->pc[thread]+7] << 56;
	cpu->pc[thread]+=8;
	return adr;
}

uint32_t zeromtx(struct sux *cpu, uint8_t thread) {
	uint32_t adr =	(uint32_t)addr[cpu->pc[thread]]
			| (uint32_t)addr[cpu->pc[thread]+1] << 8
			| (uint32_t)addr[cpu->pc[thread]+2] << 16
			| (uint32_t)addr[cpu->pc[thread]+3] << 24;
	cpu->pc[thread]+=4;
	return adr;
}

uint32_t zeromx(struct sux *cpu, uint8_t thread) {
	uint32_t adr =	(uint32_t)addr[cpu->pc[thread]]
			| (uint32_t)addr[cpu->pc[thread]+1] << 8
			| (uint32_t)addr[cpu->pc[thread]+2] << 16
			| (uint32_t)addr[cpu->pc[thread]+3] << 24;
	adr += cpu->x[thread];
	cpu->pc[thread]+=4;
	return adr;
}

uint32_t zeromy(struct sux *cpu, uint8_t thread) {
	uint32_t adr =	(uint32_t)addr[cpu->pc[thread]]
			| (uint32_t)addr[cpu->pc[thread]+1] << 8
			| (uint32_t)addr[cpu->pc[thread]+2] << 16
			| (uint32_t)addr[cpu->pc[thread]+3] << 24;
	adr += cpu->y[thread];
	cpu->pc[thread]+=4;
	return adr;
}