]> git.proxmox.com Git - mirror_qemu.git/blob - target/i386/hvf/x86_flags.c
i386: hvf: move all hvf files in the same directory
[mirror_qemu.git] / target / i386 / hvf / x86_flags.c
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"
25 #include "qemu-common.h"
26
27 #include "cpu.h"
28 #include "x86_flags.h"
29 #include "x86.h"
30
31 void SET_FLAGS_OxxxxC(CPUX86State *env, uint32_t new_of, uint32_t new_cf)
32 {
33 uint32_t temp_po = new_of ^ new_cf;
34 env->hvf_emul->lflags.auxbits &= ~(LF_MASK_PO | LF_MASK_CF);
35 env->hvf_emul->lflags.auxbits |= (temp_po << LF_BIT_PO) |
36 (new_cf << LF_BIT_CF);
37 }
38
39 void SET_FLAGS_OSZAPC_SUB32(CPUX86State *env, uint32_t v1, uint32_t v2,
40 uint32_t diff)
41 {
42 SET_FLAGS_OSZAPC_SUB_32(v1, v2, diff);
43 }
44
45 void SET_FLAGS_OSZAPC_SUB16(CPUX86State *env, uint16_t v1, uint16_t v2,
46 uint16_t diff)
47 {
48 SET_FLAGS_OSZAPC_SUB_16(v1, v2, diff);
49 }
50
51 void SET_FLAGS_OSZAPC_SUB8(CPUX86State *env, uint8_t v1, uint8_t v2,
52 uint8_t diff)
53 {
54 SET_FLAGS_OSZAPC_SUB_8(v1, v2, diff);
55 }
56
57 void SET_FLAGS_OSZAPC_ADD32(CPUX86State *env, uint32_t v1, uint32_t v2,
58 uint32_t diff)
59 {
60 SET_FLAGS_OSZAPC_ADD_32(v1, v2, diff);
61 }
62
63 void SET_FLAGS_OSZAPC_ADD16(CPUX86State *env, uint16_t v1, uint16_t v2,
64 uint16_t diff)
65 {
66 SET_FLAGS_OSZAPC_ADD_16(v1, v2, diff);
67 }
68
69 void SET_FLAGS_OSZAPC_ADD8(CPUX86State *env, uint8_t v1, uint8_t v2,
70 uint8_t diff)
71 {
72 SET_FLAGS_OSZAPC_ADD_8(v1, v2, diff);
73 }
74
75 void SET_FLAGS_OSZAP_SUB32(CPUX86State *env, uint32_t v1, uint32_t v2,
76 uint32_t diff)
77 {
78 SET_FLAGS_OSZAP_SUB_32(v1, v2, diff);
79 }
80
81 void SET_FLAGS_OSZAP_SUB16(CPUX86State *env, uint16_t v1, uint16_t v2,
82 uint16_t diff)
83 {
84 SET_FLAGS_OSZAP_SUB_16(v1, v2, diff);
85 }
86
87 void SET_FLAGS_OSZAP_SUB8(CPUX86State *env, uint8_t v1, uint8_t v2,
88 uint8_t diff)
89 {
90 SET_FLAGS_OSZAP_SUB_8(v1, v2, diff);
91 }
92
93 void SET_FLAGS_OSZAP_ADD32(CPUX86State *env, uint32_t v1, uint32_t v2,
94 uint32_t diff)
95 {
96 SET_FLAGS_OSZAP_ADD_32(v1, v2, diff);
97 }
98
99 void SET_FLAGS_OSZAP_ADD16(CPUX86State *env, uint16_t v1, uint16_t v2,
100 uint16_t diff)
101 {
102 SET_FLAGS_OSZAP_ADD_16(v1, v2, diff);
103 }
104
105 void SET_FLAGS_OSZAP_ADD8(CPUX86State *env, uint8_t v1, uint8_t v2,
106 uint8_t diff)
107 {
108 SET_FLAGS_OSZAP_ADD_8(v1, v2, diff);
109 }
110
111
112 void SET_FLAGS_OSZAPC_LOGIC32(CPUX86State *env, uint32_t diff)
113 {
114 SET_FLAGS_OSZAPC_LOGIC_32(diff);
115 }
116
117 void SET_FLAGS_OSZAPC_LOGIC16(CPUX86State *env, uint16_t diff)
118 {
119 SET_FLAGS_OSZAPC_LOGIC_16(diff);
120 }
121
122 void SET_FLAGS_OSZAPC_LOGIC8(CPUX86State *env, uint8_t diff)
123 {
124 SET_FLAGS_OSZAPC_LOGIC_8(diff);
125 }
126
127 void SET_FLAGS_SHR32(CPUX86State *env, uint32_t v, int count, uint32_t res)
128 {
129 int cf = (v >> (count - 1)) & 0x1;
130 int of = (((res << 1) ^ res) >> 31);
131
132 SET_FLAGS_OSZAPC_LOGIC_32(res);
133 SET_FLAGS_OxxxxC(env, of, cf);
134 }
135
136 void SET_FLAGS_SHR16(CPUX86State *env, uint16_t v, int count, uint16_t res)
137 {
138 int cf = (v >> (count - 1)) & 0x1;
139 int of = (((res << 1) ^ res) >> 15);
140
141 SET_FLAGS_OSZAPC_LOGIC_16(res);
142 SET_FLAGS_OxxxxC(env, of, cf);
143 }
144
145 void SET_FLAGS_SHR8(CPUX86State *env, uint8_t v, int count, uint8_t res)
146 {
147 int cf = (v >> (count - 1)) & 0x1;
148 int of = (((res << 1) ^ res) >> 7);
149
150 SET_FLAGS_OSZAPC_LOGIC_8(res);
151 SET_FLAGS_OxxxxC(env, of, cf);
152 }
153
154 void SET_FLAGS_SAR32(CPUX86State *env, int32_t v, int count, uint32_t res)
155 {
156 int cf = (v >> (count - 1)) & 0x1;
157
158 SET_FLAGS_OSZAPC_LOGIC_32(res);
159 SET_FLAGS_OxxxxC(env, 0, cf);
160 }
161
162 void SET_FLAGS_SAR16(CPUX86State *env, int16_t v, int count, uint16_t res)
163 {
164 int cf = (v >> (count - 1)) & 0x1;
165
166 SET_FLAGS_OSZAPC_LOGIC_16(res);
167 SET_FLAGS_OxxxxC(env, 0, cf);
168 }
169
170 void SET_FLAGS_SAR8(CPUX86State *env, int8_t v, int count, uint8_t res)
171 {
172 int cf = (v >> (count - 1)) & 0x1;
173
174 SET_FLAGS_OSZAPC_LOGIC_8(res);
175 SET_FLAGS_OxxxxC(env, 0, cf);
176 }
177
178
179 void SET_FLAGS_SHL32(CPUX86State *env, uint32_t v, int count, uint32_t res)
180 {
181 int of, cf;
182
183 cf = (v >> (32 - count)) & 0x1;
184 of = cf ^ (res >> 31);
185
186 SET_FLAGS_OSZAPC_LOGIC_32(res);
187 SET_FLAGS_OxxxxC(env, of, cf);
188 }
189
190 void SET_FLAGS_SHL16(CPUX86State *env, uint16_t v, int count, uint16_t res)
191 {
192 int of = 0, cf = 0;
193
194 if (count <= 16) {
195 cf = (v >> (16 - count)) & 0x1;
196 of = cf ^ (res >> 15);
197 }
198
199 SET_FLAGS_OSZAPC_LOGIC_16(res);
200 SET_FLAGS_OxxxxC(env, of, cf);
201 }
202
203 void SET_FLAGS_SHL8(CPUX86State *env, uint8_t v, int count, uint8_t res)
204 {
205 int of = 0, cf = 0;
206
207 if (count <= 8) {
208 cf = (v >> (8 - count)) & 0x1;
209 of = cf ^ (res >> 7);
210 }
211
212 SET_FLAGS_OSZAPC_LOGIC_8(res);
213 SET_FLAGS_OxxxxC(env, of, cf);
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
231 bool _get_OF(CPUX86State *env)
232 {
233 return ((env->hvf_emul->lflags.auxbits + (1U << LF_BIT_PO)) >> LF_BIT_CF) & 1;
234 }
235
236 bool get_OF(CPUX86State *env)
237 {
238 return _get_OF(env);
239 }
240
241 bool _get_CF(CPUX86State *env)
242 {
243 return (env->hvf_emul->lflags.auxbits >> LF_BIT_CF) & 1;
244 }
245
246 bool get_CF(CPUX86State *env)
247 {
248 return _get_CF(env);
249 }
250
251 void set_OF(CPUX86State *env, bool val)
252 {
253 SET_FLAGS_OxxxxC(env, val, _get_CF(env));
254 }
255
256 void set_CF(CPUX86State *env, bool val)
257 {
258 SET_FLAGS_OxxxxC(env, _get_OF(env), (val));
259 }
260
261 bool get_AF(CPUX86State *env)
262 {
263 return (env->hvf_emul->lflags.auxbits >> LF_BIT_AF) & 1;
264 }
265
266 void set_AF(CPUX86State *env, bool val)
267 {
268 env->hvf_emul->lflags.auxbits &= ~(LF_MASK_AF);
269 env->hvf_emul->lflags.auxbits |= (val) << LF_BIT_AF;
270 }
271
272 bool get_ZF(CPUX86State *env)
273 {
274 return !env->hvf_emul->lflags.result;
275 }
276
277 void set_ZF(CPUX86State *env, bool val)
278 {
279 if (val) {
280 env->hvf_emul->lflags.auxbits ^=
281 (((env->hvf_emul->lflags.result >> LF_SIGN_BIT) & 1) << LF_BIT_SD);
282 /* merge the parity bits into the Parity Delta Byte */
283 uint32_t temp_pdb = (255 & env->hvf_emul->lflags.result);
284 env->hvf_emul->lflags.auxbits ^= (temp_pdb << LF_BIT_PDB);
285 /* now zero the .result value */
286 env->hvf_emul->lflags.result = 0;
287 } else {
288 env->hvf_emul->lflags.result |= (1 << 8);
289 }
290 }
291
292 bool get_SF(CPUX86State *env)
293 {
294 return ((env->hvf_emul->lflags.result >> LF_SIGN_BIT) ^
295 (env->hvf_emul->lflags.auxbits >> LF_BIT_SD)) & 1;
296 }
297
298 void set_SF(CPUX86State *env, bool val)
299 {
300 bool temp_sf = get_SF(env);
301 env->hvf_emul->lflags.auxbits ^= (temp_sf ^ val) << LF_BIT_SD;
302 }
303
304 void set_OSZAPC(CPUX86State *env, uint32_t flags32)
305 {
306 set_OF(env, env->hvf_emul->rflags.of);
307 set_SF(env, env->hvf_emul->rflags.sf);
308 set_ZF(env, env->hvf_emul->rflags.zf);
309 set_AF(env, env->hvf_emul->rflags.af);
310 set_PF(env, env->hvf_emul->rflags.pf);
311 set_CF(env, env->hvf_emul->rflags.cf);
312 }
313
314 void lflags_to_rflags(CPUX86State *env)
315 {
316 env->hvf_emul->rflags.cf = get_CF(env);
317 env->hvf_emul->rflags.pf = get_PF(env);
318 env->hvf_emul->rflags.af = get_AF(env);
319 env->hvf_emul->rflags.zf = get_ZF(env);
320 env->hvf_emul->rflags.sf = get_SF(env);
321 env->hvf_emul->rflags.of = get_OF(env);
322 }
323
324 void rflags_to_lflags(CPUX86State *env)
325 {
326 env->hvf_emul->lflags.auxbits = env->hvf_emul->lflags.result = 0;
327 set_OF(env, env->hvf_emul->rflags.of);
328 set_SF(env, env->hvf_emul->rflags.sf);
329 set_ZF(env, env->hvf_emul->rflags.zf);
330 set_AF(env, env->hvf_emul->rflags.af);
331 set_PF(env, env->hvf_emul->rflags.pf);
332 set_CF(env, env->hvf_emul->rflags.cf);
333 }