]>
Commit | Line | Data |
---|---|---|
c97d6d2c SAGDR |
1 | ///////////////////////////////////////////////////////////////////////// |
2 | // | |
3 | // Copyright (C) 2001-2012 The Bochs Project | |
4 | // Copyright (C) 2017 Google Inc. | |
5 | // | |
6 | // This library is free software; you can redistribute it and/or | |
7 | // modify it under the terms of the GNU Lesser General Public | |
8 | // License as published by the Free Software Foundation; either | |
9 | // version 2 of the License, or (at your option) any later version. | |
10 | // | |
11 | // This library is distributed in the hope that it will be useful, | |
12 | // but WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
14 | // Lesser General Public License for more details. | |
15 | // | |
16 | // You should have received a copy of the GNU Lesser General Public | |
17 | // License along with this library; if not, write to the Free Software | |
18 | // Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA B 02110-1301 USA | |
19 | ///////////////////////////////////////////////////////////////////////// | |
20 | /* | |
21 | * flags functions | |
22 | */ | |
23 | ||
24 | #include "qemu/osdep.h" | |
c97d6d2c | 25 | |
f9fea777 | 26 | #include "qemu-common.h" |
895f9fdf | 27 | #include "panic.h" |
c97d6d2c SAGDR |
28 | #include "cpu.h" |
29 | #include "x86_flags.h" | |
30 | #include "x86.h" | |
31 | ||
e8a63257 PB |
32 | |
33 | /* this is basically bocsh code */ | |
34 | ||
35 | #define LF_SIGN_BIT 31 | |
36 | ||
37 | #define LF_BIT_SD (0) /* lazy Sign Flag Delta */ | |
38 | #define LF_BIT_AF (3) /* lazy Adjust flag */ | |
39 | #define LF_BIT_PDB (8) /* lazy Parity Delta Byte (8 bits) */ | |
40 | #define LF_BIT_CF (31) /* lazy Carry Flag */ | |
41 | #define LF_BIT_PO (30) /* lazy Partial Overflow = CF ^ OF */ | |
42 | ||
43 | #define LF_MASK_SD (0x01 << LF_BIT_SD) | |
44 | #define LF_MASK_AF (0x01 << LF_BIT_AF) | |
45 | #define LF_MASK_PDB (0xFF << LF_BIT_PDB) | |
46 | #define LF_MASK_CF (0x01 << LF_BIT_CF) | |
47 | #define LF_MASK_PO (0x01 << LF_BIT_PO) | |
48 | ||
49 | #define ADD_COUT_VEC(op1, op2, result) \ | |
50 | (((op1) & (op2)) | (((op1) | (op2)) & (~(result)))) | |
51 | ||
52 | #define SUB_COUT_VEC(op1, op2, result) \ | |
53 | (((~(op1)) & (op2)) | (((~(op1)) ^ (op2)) & (result))) | |
54 | ||
55 | #define GET_ADD_OVERFLOW(op1, op2, result, mask) \ | |
56 | ((((op1) ^ (result)) & ((op2) ^ (result))) & (mask)) | |
57 | ||
58 | /* ******************* */ | |
59 | /* OSZAPC */ | |
60 | /* ******************* */ | |
61 | ||
62 | /* size, carries, result */ | |
63 | #define SET_FLAGS_OSZAPC_SIZE(size, lf_carries, lf_result) { \ | |
ff2de166 | 64 | target_ulong temp = ((lf_carries) & (LF_MASK_AF)) | \ |
e8a63257 | 65 | (((lf_carries) >> (size - 2)) << LF_BIT_PO); \ |
ff2de166 | 66 | env->hvf_emul->lflags.result = (target_ulong)(int##size##_t)(lf_result); \ |
e8a63257 PB |
67 | if ((size) == 32) { \ |
68 | temp = ((lf_carries) & ~(LF_MASK_PDB | LF_MASK_SD)); \ | |
69 | } else if ((size) == 16) { \ | |
70 | temp = ((lf_carries) & (LF_MASK_AF)) | ((lf_carries) << 16); \ | |
71 | } else if ((size) == 8) { \ | |
72 | temp = ((lf_carries) & (LF_MASK_AF)) | ((lf_carries) << 24); \ | |
73 | } else { \ | |
74 | VM_PANIC("unimplemented"); \ | |
75 | } \ | |
ff2de166 | 76 | env->hvf_emul->lflags.auxbits = (target_ulong)(uint32_t)temp; \ |
e8a63257 PB |
77 | } |
78 | ||
79 | /* carries, result */ | |
80 | #define SET_FLAGS_OSZAPC_8(carries, result) \ | |
81 | SET_FLAGS_OSZAPC_SIZE(8, carries, result) | |
82 | #define SET_FLAGS_OSZAPC_16(carries, result) \ | |
83 | SET_FLAGS_OSZAPC_SIZE(16, carries, result) | |
84 | #define SET_FLAGS_OSZAPC_32(carries, result) \ | |
85 | SET_FLAGS_OSZAPC_SIZE(32, carries, result) | |
86 | ||
87 | /* ******************* */ | |
88 | /* OSZAP */ | |
89 | /* ******************* */ | |
90 | /* size, carries, result */ | |
91 | #define SET_FLAGS_OSZAP_SIZE(size, lf_carries, lf_result) { \ | |
ff2de166 | 92 | target_ulong temp = ((lf_carries) & (LF_MASK_AF)) | \ |
e8a63257 PB |
93 | (((lf_carries) >> (size - 2)) << LF_BIT_PO); \ |
94 | if ((size) == 32) { \ | |
95 | temp = ((lf_carries) & ~(LF_MASK_PDB | LF_MASK_SD)); \ | |
96 | } else if ((size) == 16) { \ | |
97 | temp = ((lf_carries) & (LF_MASK_AF)) | ((lf_carries) << 16); \ | |
98 | } else if ((size) == 8) { \ | |
99 | temp = ((lf_carries) & (LF_MASK_AF)) | ((lf_carries) << 24); \ | |
100 | } else { \ | |
101 | VM_PANIC("unimplemented"); \ | |
102 | } \ | |
ff2de166 PB |
103 | env->hvf_emul->lflags.result = (target_ulong)(int##size##_t)(lf_result); \ |
104 | target_ulong delta_c = (env->hvf_emul->lflags.auxbits ^ temp) & LF_MASK_CF; \ | |
e8a63257 | 105 | delta_c ^= (delta_c >> 1); \ |
ff2de166 | 106 | env->hvf_emul->lflags.auxbits = (target_ulong)(uint32_t)(temp ^ delta_c); \ |
e8a63257 PB |
107 | } |
108 | ||
109 | /* carries, result */ | |
110 | #define SET_FLAGS_OSZAP_8(carries, result) \ | |
111 | SET_FLAGS_OSZAP_SIZE(8, carries, result) | |
112 | #define SET_FLAGS_OSZAP_16(carries, result) \ | |
113 | SET_FLAGS_OSZAP_SIZE(16, carries, result) | |
114 | #define SET_FLAGS_OSZAP_32(carries, result) \ | |
115 | SET_FLAGS_OSZAP_SIZE(32, carries, result) | |
116 | ||
c97d6d2c SAGDR |
117 | void SET_FLAGS_OxxxxC(CPUX86State *env, uint32_t new_of, uint32_t new_cf) |
118 | { | |
119 | uint32_t temp_po = new_of ^ new_cf; | |
120 | env->hvf_emul->lflags.auxbits &= ~(LF_MASK_PO | LF_MASK_CF); | |
121 | env->hvf_emul->lflags.auxbits |= (temp_po << LF_BIT_PO) | | |
122 | (new_cf << LF_BIT_CF); | |
123 | } | |
124 | ||
125 | void SET_FLAGS_OSZAPC_SUB32(CPUX86State *env, uint32_t v1, uint32_t v2, | |
126 | uint32_t diff) | |
127 | { | |
e8a63257 | 128 | SET_FLAGS_OSZAPC_32(SUB_COUT_VEC(v1, v2, diff), diff); |
c97d6d2c SAGDR |
129 | } |
130 | ||
131 | void SET_FLAGS_OSZAPC_SUB16(CPUX86State *env, uint16_t v1, uint16_t v2, | |
132 | uint16_t diff) | |
133 | { | |
e8a63257 | 134 | SET_FLAGS_OSZAPC_16(SUB_COUT_VEC(v1, v2, diff), diff); |
c97d6d2c SAGDR |
135 | } |
136 | ||
137 | void SET_FLAGS_OSZAPC_SUB8(CPUX86State *env, uint8_t v1, uint8_t v2, | |
138 | uint8_t diff) | |
139 | { | |
e8a63257 | 140 | SET_FLAGS_OSZAPC_8(SUB_COUT_VEC(v1, v2, diff), diff); |
c97d6d2c SAGDR |
141 | } |
142 | ||
143 | void SET_FLAGS_OSZAPC_ADD32(CPUX86State *env, uint32_t v1, uint32_t v2, | |
144 | uint32_t diff) | |
145 | { | |
e8a63257 | 146 | SET_FLAGS_OSZAPC_32(ADD_COUT_VEC(v1, v2, diff), diff); |
c97d6d2c SAGDR |
147 | } |
148 | ||
149 | void SET_FLAGS_OSZAPC_ADD16(CPUX86State *env, uint16_t v1, uint16_t v2, | |
150 | uint16_t diff) | |
151 | { | |
e8a63257 | 152 | SET_FLAGS_OSZAPC_16(ADD_COUT_VEC(v1, v2, diff), diff); |
c97d6d2c SAGDR |
153 | } |
154 | ||
155 | void SET_FLAGS_OSZAPC_ADD8(CPUX86State *env, uint8_t v1, uint8_t v2, | |
156 | uint8_t diff) | |
157 | { | |
e8a63257 | 158 | SET_FLAGS_OSZAPC_8(ADD_COUT_VEC(v1, v2, diff), diff); |
c97d6d2c SAGDR |
159 | } |
160 | ||
161 | void SET_FLAGS_OSZAP_SUB32(CPUX86State *env, uint32_t v1, uint32_t v2, | |
162 | uint32_t diff) | |
163 | { | |
e8a63257 | 164 | SET_FLAGS_OSZAP_32(SUB_COUT_VEC(v1, v2, diff), diff); |
c97d6d2c SAGDR |
165 | } |
166 | ||
167 | void SET_FLAGS_OSZAP_SUB16(CPUX86State *env, uint16_t v1, uint16_t v2, | |
168 | uint16_t diff) | |
169 | { | |
e8a63257 | 170 | SET_FLAGS_OSZAP_16(SUB_COUT_VEC(v1, v2, diff), diff); |
c97d6d2c SAGDR |
171 | } |
172 | ||
173 | void SET_FLAGS_OSZAP_SUB8(CPUX86State *env, uint8_t v1, uint8_t v2, | |
174 | uint8_t diff) | |
175 | { | |
e8a63257 | 176 | SET_FLAGS_OSZAP_8(SUB_COUT_VEC(v1, v2, diff), diff); |
c97d6d2c SAGDR |
177 | } |
178 | ||
179 | void SET_FLAGS_OSZAP_ADD32(CPUX86State *env, uint32_t v1, uint32_t v2, | |
180 | uint32_t diff) | |
181 | { | |
e8a63257 | 182 | SET_FLAGS_OSZAP_32(ADD_COUT_VEC(v1, v2, diff), diff); |
c97d6d2c SAGDR |
183 | } |
184 | ||
185 | void SET_FLAGS_OSZAP_ADD16(CPUX86State *env, uint16_t v1, uint16_t v2, | |
186 | uint16_t diff) | |
187 | { | |
e8a63257 | 188 | SET_FLAGS_OSZAP_16(ADD_COUT_VEC(v1, v2, diff), diff); |
c97d6d2c SAGDR |
189 | } |
190 | ||
191 | void SET_FLAGS_OSZAP_ADD8(CPUX86State *env, uint8_t v1, uint8_t v2, | |
192 | uint8_t diff) | |
193 | { | |
e8a63257 | 194 | SET_FLAGS_OSZAP_8(ADD_COUT_VEC(v1, v2, diff), diff); |
c97d6d2c SAGDR |
195 | } |
196 | ||
c97d6d2c | 197 | |
e8a63257 PB |
198 | void SET_FLAGS_OSZAPC_LOGIC32(CPUX86State *env, uint32_t v1, uint32_t v2, |
199 | uint32_t diff) | |
c97d6d2c | 200 | { |
e8a63257 | 201 | SET_FLAGS_OSZAPC_32(0, diff); |
c97d6d2c SAGDR |
202 | } |
203 | ||
e8a63257 PB |
204 | void SET_FLAGS_OSZAPC_LOGIC16(CPUX86State *env, uint16_t v1, uint16_t v2, |
205 | uint16_t diff) | |
c97d6d2c | 206 | { |
e8a63257 | 207 | SET_FLAGS_OSZAPC_16(0, diff); |
c97d6d2c SAGDR |
208 | } |
209 | ||
e8a63257 PB |
210 | void SET_FLAGS_OSZAPC_LOGIC8(CPUX86State *env, uint8_t v1, uint8_t v2, |
211 | uint8_t diff) | |
c97d6d2c | 212 | { |
e8a63257 | 213 | SET_FLAGS_OSZAPC_8(0, diff); |
c97d6d2c SAGDR |
214 | } |
215 | ||
216 | bool get_PF(CPUX86State *env) | |
217 | { | |
218 | uint32_t temp = (255 & env->hvf_emul->lflags.result); | |
219 | temp = temp ^ (255 & (env->hvf_emul->lflags.auxbits >> LF_BIT_PDB)); | |
220 | temp = (temp ^ (temp >> 4)) & 0x0F; | |
221 | return (0x9669U >> temp) & 1; | |
222 | } | |
223 | ||
224 | void set_PF(CPUX86State *env, bool val) | |
225 | { | |
226 | uint32_t temp = (255 & env->hvf_emul->lflags.result) ^ (!val); | |
227 | env->hvf_emul->lflags.auxbits &= ~(LF_MASK_PDB); | |
228 | env->hvf_emul->lflags.auxbits |= (temp << LF_BIT_PDB); | |
229 | } | |
230 | ||
c97d6d2c SAGDR |
231 | bool get_OF(CPUX86State *env) |
232 | { | |
e8a63257 | 233 | return ((env->hvf_emul->lflags.auxbits + (1U << LF_BIT_PO)) >> LF_BIT_CF) & 1; |
c97d6d2c SAGDR |
234 | } |
235 | ||
236 | bool get_CF(CPUX86State *env) | |
237 | { | |
e8a63257 | 238 | return (env->hvf_emul->lflags.auxbits >> LF_BIT_CF) & 1; |
c97d6d2c SAGDR |
239 | } |
240 | ||
241 | void set_OF(CPUX86State *env, bool val) | |
242 | { | |
e8a63257 PB |
243 | bool old_cf = get_CF(env); |
244 | SET_FLAGS_OxxxxC(env, val, old_cf); | |
c97d6d2c SAGDR |
245 | } |
246 | ||
247 | void set_CF(CPUX86State *env, bool val) | |
248 | { | |
e8a63257 PB |
249 | bool old_of = get_OF(env); |
250 | SET_FLAGS_OxxxxC(env, old_of, val); | |
c97d6d2c SAGDR |
251 | } |
252 | ||
253 | bool get_AF(CPUX86State *env) | |
254 | { | |
255 | return (env->hvf_emul->lflags.auxbits >> LF_BIT_AF) & 1; | |
256 | } | |
257 | ||
258 | void set_AF(CPUX86State *env, bool val) | |
259 | { | |
260 | env->hvf_emul->lflags.auxbits &= ~(LF_MASK_AF); | |
e8a63257 | 261 | env->hvf_emul->lflags.auxbits |= val << LF_BIT_AF; |
c97d6d2c SAGDR |
262 | } |
263 | ||
264 | bool get_ZF(CPUX86State *env) | |
265 | { | |
266 | return !env->hvf_emul->lflags.result; | |
267 | } | |
268 | ||
269 | void set_ZF(CPUX86State *env, bool val) | |
270 | { | |
271 | if (val) { | |
272 | env->hvf_emul->lflags.auxbits ^= | |
273 | (((env->hvf_emul->lflags.result >> LF_SIGN_BIT) & 1) << LF_BIT_SD); | |
274 | /* merge the parity bits into the Parity Delta Byte */ | |
275 | uint32_t temp_pdb = (255 & env->hvf_emul->lflags.result); | |
276 | env->hvf_emul->lflags.auxbits ^= (temp_pdb << LF_BIT_PDB); | |
277 | /* now zero the .result value */ | |
278 | env->hvf_emul->lflags.result = 0; | |
279 | } else { | |
280 | env->hvf_emul->lflags.result |= (1 << 8); | |
281 | } | |
282 | } | |
283 | ||
284 | bool get_SF(CPUX86State *env) | |
285 | { | |
286 | return ((env->hvf_emul->lflags.result >> LF_SIGN_BIT) ^ | |
287 | (env->hvf_emul->lflags.auxbits >> LF_BIT_SD)) & 1; | |
288 | } | |
289 | ||
290 | void set_SF(CPUX86State *env, bool val) | |
291 | { | |
292 | bool temp_sf = get_SF(env); | |
293 | env->hvf_emul->lflags.auxbits ^= (temp_sf ^ val) << LF_BIT_SD; | |
294 | } | |
295 | ||
c97d6d2c SAGDR |
296 | void lflags_to_rflags(CPUX86State *env) |
297 | { | |
967f4da2 RB |
298 | env->eflags |= get_CF(env) ? CC_C : 0; |
299 | env->eflags |= get_PF(env) ? CC_P : 0; | |
300 | env->eflags |= get_AF(env) ? CC_A : 0; | |
301 | env->eflags |= get_ZF(env) ? CC_Z : 0; | |
302 | env->eflags |= get_SF(env) ? CC_S : 0; | |
303 | env->eflags |= get_OF(env) ? CC_O : 0; | |
c97d6d2c SAGDR |
304 | } |
305 | ||
306 | void rflags_to_lflags(CPUX86State *env) | |
307 | { | |
308 | env->hvf_emul->lflags.auxbits = env->hvf_emul->lflags.result = 0; | |
967f4da2 RB |
309 | set_OF(env, env->eflags & CC_O); |
310 | set_SF(env, env->eflags & CC_S); | |
311 | set_ZF(env, env->eflags & CC_Z); | |
312 | set_AF(env, env->eflags & CC_A); | |
313 | set_PF(env, env->eflags & CC_P); | |
314 | set_CF(env, env->eflags & CC_C); | |
c97d6d2c | 315 | } |