-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patharith.c
executable file
·232 lines (187 loc) · 6.2 KB
/
arith.c
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
static inline void op_mul8(uint8_t oper1b, uint8_t oper2b) {
uint32_t temp1 = (uint32_t)oper1b * (uint32_t)oper2b;
regs.wordregs[regax] = temp1;
//flag_szp16((uint16_t) temp1);
if (regs.byteregs[regah]) {
cf = 1;
of = 1;
} else {
cf = 0;
of = 0;
}
zf = 0;
}
static inline void op_imul8(uint8_t oper1b, uint8_t oper2b) {
int32_t temp1 = signext32(signext16(oper1b));
int32_t temp2 = signext32(signext16(oper2b));
int32_t temp3 = (temp1 * temp2) & 0xFFFF;
regs.wordregs[regax] = temp3;
if (regs.byteregs[regah]) {
cf = 1;
of = 1;
} else {
cf = 0;
of = 0;
}
zf = 0;
}
static inline void op_mul16(uint16_t oper1, uint16_t oper2) {
uint32_t temp1 = (uint32_t)oper1 * (uint32_t)oper2;
regs.wordregs[regax] = temp1 & 0xFFFF;
regs.wordregs[regdx] = temp1 >> 16;
if (regs.wordregs[regdx]) {
cf = 1;
of = 1;
} else {
cf = 0;
of = 0;
}
zf = 0;
}
static inline void op_imul16(uint16_t oper1, uint16_t oper2) {
int32_t temp1 = signext32(oper1);
int32_t temp2 = signext32(oper2);
int32_t temp3 = temp1 * temp2;
regs.wordregs[regax] = temp3 & 0xFFFF;
regs.wordregs[regdx] = (temp3 >> 16) & 0xFFFF;
if (regs.wordregs[regdx]) {
cf = 1;
of = 1;
} else {
cf = 0;
of = 0;
}
zf = 0;
}
static inline void op_div8(uint16_t valdiv, uint8_t divisor) {
if (divisor == 0) {
intcall86(0);
return;
}
if ((valdiv / (uint16_t) divisor) > 0xFF) {
intcall86(0);
return;
}
regs.byteregs[regah] = valdiv % (uint16_t) divisor;
regs.byteregs[regal] = valdiv / (uint16_t) divisor;
}
static inline void op_idiv8(uint16_t valdiv, uint8_t divisor) {
if (divisor == 0) {
intcall86(0);
return;
}
int16_t temp = (int16_t) valdiv / signext16(divisor);
if (temp > 0x7F || temp < -0x7F) {
intcall86(0);
return;
}
regs.byteregs[regah] = (int16_t) valdiv % signext16(divisor);
regs.byteregs[regal] = temp;
}
static inline void op_div16(uint32_t valdiv, uint16_t divisor) {
if (divisor == 0) {
intcall86(0);
return;
}
//printf("div %i %i\n", valdiv, divisor);
if ((valdiv / (uint32_t) divisor) > 0xFFFF) {
intcall86(0);
return;
}
regs.wordregs[regdx] = valdiv % (uint32_t) divisor;
regs.wordregs[regax] = valdiv / (uint32_t) divisor;
}
static inline void op_idiv16(int32_t valdiv, uint16_t divisor) {
if (divisor == 0) {
intcall86(0);
return;
}
int32_t temp = (int32_t) valdiv / signext32(divisor);
if (temp > 0x7FFF || temp < -0x7FFF) {
intcall86(0);
return;
}
regs.wordregs[regdx] = (int32_t) valdiv % signext32(divisor);
regs.wordregs[regax] = temp;
}
static inline void flag_adc8(uint8_t v1, uint8_t v2, uint8_t v3) {
/* v1 = destination operand, v2 = source operand, v3 = carry flag */
uint16_t dst = (uint16_t) v1 + (uint16_t) v2 + (uint16_t) v3;
flag_szp8((uint8_t) dst);
cf = (dst & 0xFF00)?1:0;
of = ((dst ^ v1) & (dst ^ v2) & 0x80)?1:0;
af = (((v1 ^ v2 ^ dst) & 0x10) == 0x10)?1:0;
}
static void inline flag_adc16(uint16_t v1, uint16_t v2, uint16_t v3) {
uint32_t dst = (uint32_t) v1 + (uint32_t) v2 + (uint32_t) v3;
flag_szp16((uint16_t) dst);
cf = (dst & 0xFFFF0000)?1:0;
of = (((dst ^ v1) & (dst ^ v2)) & 0x8000)?1:0;
af = (((v1 ^ v2 ^ dst) & 0x10) == 0x10)?1:0;
}
static void inline flag_add8(uint8_t v1, uint8_t v2) {
/* v1 = destination operand, v2 = source operand */
uint16_t dst = (uint16_t) v1 + (uint16_t) v2;
flag_szp8((uint8_t) dst);
cf = (dst & 0xFF00)?1:0; /* set or clear carry flag */
of = ((dst ^ v1) & (dst ^ v2) & 0x80)?1:0;
af = (((v1 ^ v2 ^ dst) & 0x10) == 0x10)?1:0;
}
static inline void flag_add16(uint16_t v1, uint16_t v2) {
/* v1 = destination operand, v2 = source operand */
uint32_t dst = (uint32_t)v1 + (uint32_t)v2;
flag_szp16((uint16_t) dst);
cf = (dst & 0xFFFF0000)?1:0; /* set or clear carry flag */
of = ((dst ^ v1) & (dst ^ v2) & 0x8000u)?1:0;
af = (((v1 ^ v2 ^ dst) & 0x10u) == 0x10)?1:0;
}
static inline void flag_add32(uint32_t v1, uint32_t v2) {
/* v1 = destination operand, v2 = source operand */
uint64_t dst = (uint64_t)v1 + (uint64_t)v2;
flag_szp32((uint32_t) dst);
cf = (dst & 0xFFFFFFFF00000000l)?1:0; /* set or clear carry flag */
of = ((dst ^ v1) & (dst ^ v2) & 0x80000000u)?1:0;
af = (((v1 ^ v2 ^ dst) & 0x10u) == 0x10)?1:0;
}
static inline void flag_sbb8(uint8_t v1, uint16_t v2, uint8_t v3) {
/* v1 = destination operand, v2 = source operand, v3 = carry flag */
v2 += v3;
uint16_t dst = (uint16_t)v1 - v2;
flag_szp8((uint8_t) dst);
cf = (dst & 0x8000u)?1:0;
of = ((dst ^ v1) & (v1 ^ v2) & 0x80u)?1:0;
af = ((v1 ^ v2 ^ dst) & 0x10u)?1:0;
}
static inline void flag_sbb16(uint16_t v1, uint32_t v2, uint16_t v3) {
/* v1 = destination operand, v2 = source operand, v3 = carry flag */
v2 += v3;
uint32_t dst = (uint32_t)v1 - v2;
flag_szp16((uint16_t)dst);
cf = (dst & 0x80000000)?1:0;
of = ((dst ^ v1) & (v1 ^ v2) & 0x8000u)?1:0;
af= ((v1 ^ v2 ^ dst) & 0x10u)?1:0;
}
static inline void flag_sub8(uint8_t v1, uint8_t v2) {
/* v1 = destination operand, v2 = source operand */
uint16_t dst = (uint16_t)v1 - (uint16_t)v2;
flag_szp8((uint8_t) dst);
cf = (dst & 0x8000u) ? 1 : 0;
of = ((dst ^ v1) & (v1 ^ v2) & 0x80u)?1:0;
af = ((v1 ^ v2 ^ dst) & 0x10u)?1:0;
}
static inline void flag_sub16(uint16_t v1, uint16_t v2) {
/* v1 = destination operand, v2 = source operand */
uint32_t dst = (uint32_t)v1 - (uint32_t)v2;
flag_szp16((uint16_t) dst);
cf = (dst & 0x80000000) ? 1 : 0;
of = ((dst ^ v1) & (v1 ^ v2) & 0x8000u)?1:0;
af = ((v1 ^ v2 ^ dst) & 0x10u)?1:0;
}
static inline void flag_sub32(uint32_t v1, uint32_t v2) {
/* v1 = destination operand, v2 = source operand */
uint64_t dst = (uint64_t)v1 - (uint64_t)v2;
flag_szp32((uint32_t) dst);
cf = (dst & 0x8000000000000000) ? 1 : 0;
of = ((dst ^ v1) & (v1 ^ v2) & 0x80000000u)?1:0;
af = ((v1 ^ v2 ^ dst) & 0x10u)?1:0;
}