blob: de32a14cadd54ec76da0590febb10a78b09bea70 (
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
|
#include "opcode.h"
#include <stdint.h>
uint64_t a;
uint64_t y;
uint64_t x;
uint8_t crt;
uint64_t pc[8];
uint64_t ps;
uint64_t rol(uint64_t a, uint64_t value) {
const uint64_t mask = 8 * sizeof(a) - 1;
value &= mask;
return (a << value) | (a >> (-value & mask));
}
uint64_t ror(uint64_t a, uint64_t value) {
const uint64_t mask = 8 * sizeof(a) - 1;
value &= mask;
return (a >> value) | (a << (-value & mask));
}
int alu(uint8_t opcode, uint64_t value) {
uint64_t sum;
uint8_t c = 0;
uint8_t v = 0;
uint8_t z = 0;
uint8_t n = 0;
switch(opcode) {
case ADC:
c = ps & 1;
sum = a+value+c;
v = !((a^value) & 0x8000000000000000) && ((a^sum) & 0x8000000000000000);
c = (sum < value);
a = sum;
break;
case SBC:
c = !(ps & 1);
sum = a-value-c;
v = ((a^value) & 0x8000000000000000) && ((a^sum) & 0x8000000000000000);
c = (sum > value);
a = sum;
break;
case MUL:
c = ps & 1;
sum = a*value+c;
v = !((a^value) & 0x8000000000000000) && ((a^sum) & 0x8000000000000000);
c = (!((a^sum) && (a^value)) || (a >= ((uint64_t)1 << 32) && value >= ((uint64_t)1 << 32)));
a = sum;
break;
case DIV:
sum = a/value;
v = ((a^value) & 0x8000000000000000) && ((a^sum) & 0x8000000000000000);
a = sum;
break;
case AND:
case AAY:
case AAX:
a &= value;
if (opcode == AAY)
y &= value;
if (opcode == AAX)
x &= value;
break;
case ANY:
y &= value;
break;
case ANX:
x &= value;
break;
case ORA:
case OAY:
case OAX:
a |= value;
if (opcode == OAY)
y |= value;
if (opcode == OAX)
x |= value;
break;
case ORY:
y |= value;
break;
case ORX:
x |= value;
break;
case XOR:
case XAY:
case XAX:
a ^= value;
if (opcode == XAY)
y ^= value;
if (opcode == XAX)
x ^= value;
break;
case XRY:
y ^= value;
break;
case XRX:
x ^= value;
break;
case SLA:
sum = (value < 64) ? a << value : 0;
c = (sum < a << value && value >= 64);
a = sum;
break;
case SRA:
sum = (value < 64) ? a << value : 0;
c = (sum < a >> value && value >= 64);
a = sum;
break;
case ROL:
a = rol(a, value);
break;
case ROR:
a = ror(a, value);
break;
}
z = (a == 0);
n = (a >> 63);
ps = (n) ? (ps | 1 << 7) : (ps & 1 << 7);
ps = (v) ? (ps | 1 << 6) : (ps & 1 << 6);
ps = (z) ? (ps | 1 << 1) : (ps & 1 << 1);
ps = (c) ? (ps | 1 << 0) : (ps & 1 << 0);
return 1;
}
int threads(uint8_t opcode, uint64_t value) {
uint8_t t = value & 0xFF;
switch(opcode) {
case STT:
crt |= t;
for (uint8_t i = 0; i < 8; i++)
if ((t >> i) & 1)
pc[i] = value >> 8;
break;
case ENT:
crt &= ~t;
for (uint8_t i = 0; i < 8; i++)
if ((t >> i) & 1)
pc[i] = 0;
break;
}
}
int branch(uint8_t opcode, uint64_t value, uint8_t thread) {
switch (opcode) {
case JMP:
pc[thread] = value;
break;
case JSR:
pc[thread] = value;
pushaddr(value);
break;
case RTS:
pc[thread] = pulladdr();
break;
}
}
}
int memory(uint8_t opcode, uint64_t value) {
}
|