1 /////////////////////////////////////////////////////////////////////////
2 // $Id: lazy_flags.h 14086 2021-01-30 08:35:35Z sshwarts $
3 /////////////////////////////////////////////////////////////////////////
4 //
5 // Copyright (C) 2001-2017 The Bochs Project
6 //
7 // This library is free software; you can redistribute it and/or
8 // modify it under the terms of the GNU Lesser General Public
9 // License as published by the Free Software Foundation; either
10 // version 2 of the License, or (at your option) any later version.
11 //
12 // This library is distributed in the hope that it will be useful,
13 // but WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 // Lesser General Public License for more details.
16 //
17 // You should have received a copy of the GNU Lesser General Public
18 // License along with this library; if not, write to the Free Software
19 // Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20 //
21 /////////////////////////////////////////////////////////////////////////
22
23 #ifndef BX_LAZY_FLAGS_DEF
24 #define BX_LAZY_FLAGS_DEF
25
26 #if BX_SUPPORT_X86_64
27 #define BX_LF_SIGN_BIT 63
28 #else
29 #define BX_LF_SIGN_BIT 31
30 #endif
31
32 // These are the lazy flags bits in oszapc.auxbits which hold lazy state
33 // of zero flag, adjust flag, carry flag, and overflow flag.
34
35 enum {
36 LF_BIT_SD = 0, /* lazy Sign Flag Delta */
37 LF_BIT_AF = 3, /* lazy Adjust flag */
38 LF_BIT_PDB = 8, /* lazy Parity Delta Byte (8 bits) */
39 LF_BIT_CF = 31, /* lazy Carry Flag */
40 LF_BIT_PO = 30 /* lazy Partial Overflow = CF ^ OF */
41 };
42
43 const Bit32u LF_MASK_SD = (0x01 << LF_BIT_SD);
44 const Bit32u LF_MASK_AF = (0x01 << LF_BIT_AF);
45 const Bit32u LF_MASK_PDB = (0xFF << LF_BIT_PDB);
46 const Bit32u LF_MASK_CF = (0x01 << LF_BIT_CF);
47 const Bit32u 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) { \
64 bx_address temp = ((lf_carries) & (LF_MASK_AF)) | \
65 (((lf_carries) >> (size - 2)) << LF_BIT_PO); \
66 BX_CPU_THIS_PTR oszapc.result = (bx_address)(Bit##size##s)(lf_result); \
67 if ((size) == 32) temp = ((lf_carries) & ~(LF_MASK_PDB | LF_MASK_SD)); \
68 if ((size) == 16) temp = ((lf_carries) & (LF_MASK_AF)) | ((lf_carries) << 16); \
69 if ((size) == 8) temp = ((lf_carries) & (LF_MASK_AF)) | ((lf_carries) << 24); \
70 BX_CPU_THIS_PTR oszapc.auxbits = (bx_address)(Bit32u)temp; \
71 }
72
73 /* carries, result */
74 #define SET_FLAGS_OSZAPC_8(carries, result) \
75 SET_FLAGS_OSZAPC_SIZE(8, carries, result)
76 #define SET_FLAGS_OSZAPC_16(carries, result) \
77 SET_FLAGS_OSZAPC_SIZE(16, carries, result)
78 #define SET_FLAGS_OSZAPC_32(carries, result) \
79 SET_FLAGS_OSZAPC_SIZE(32, carries, result)
80 #if BX_SUPPORT_X86_64
81 #define SET_FLAGS_OSZAPC_64(carries, result) \
82 SET_FLAGS_OSZAPC_SIZE(64, carries, result)
83 #endif
84
85 /* result */
86 #define SET_FLAGS_OSZAPC_LOGIC_8(result_8) \
87 SET_FLAGS_OSZAPC_8(0, (result_8))
88 #define SET_FLAGS_OSZAPC_LOGIC_16(result_16) \
89 SET_FLAGS_OSZAPC_16(0, (result_16))
90 #define SET_FLAGS_OSZAPC_LOGIC_32(result_32) \
91 SET_FLAGS_OSZAPC_32(0, (result_32))
92 #if BX_SUPPORT_X86_64
93 #define SET_FLAGS_OSZAPC_LOGIC_64(result_64) \
94 SET_FLAGS_OSZAPC_64(BX_CONST64(0), (result_64))
95 #endif
96
97 /* op1, op2, result */
98 #define SET_FLAGS_OSZAPC_ADD_8(op1_8, op2_8, sum_8) \
99 SET_FLAGS_OSZAPC_8(ADD_COUT_VEC((op1_8), (op2_8), (sum_8)), (sum_8))
100 #define SET_FLAGS_OSZAPC_ADD_16(op1_16, op2_16, sum_16) \
101 SET_FLAGS_OSZAPC_16(ADD_COUT_VEC((op1_16), (op2_16), (sum_16)), (sum_16))
102 #define SET_FLAGS_OSZAPC_ADD_32(op1_32, op2_32, sum_32) \
103 SET_FLAGS_OSZAPC_32(ADD_COUT_VEC((op1_32), (op2_32), (sum_32)), (sum_32))
104 #if BX_SUPPORT_X86_64
105 #define SET_FLAGS_OSZAPC_ADD_64(op1_64, op2_64, sum_64) \
106 SET_FLAGS_OSZAPC_64(ADD_COUT_VEC((op1_64), (op2_64), (sum_64)), (sum_64))
107 #endif
108
109 /* op1, op2, result */
110 #define SET_FLAGS_OSZAPC_SUB_8(op1_8, op2_8, diff_8) \
111 SET_FLAGS_OSZAPC_8(SUB_COUT_VEC((op1_8), (op2_8), (diff_8)), (diff_8))
112 #define SET_FLAGS_OSZAPC_SUB_16(op1_16, op2_16, diff_16) \
113 SET_FLAGS_OSZAPC_16(SUB_COUT_VEC((op1_16), (op2_16), (diff_16)), (diff_16))
114 #define SET_FLAGS_OSZAPC_SUB_32(op1_32, op2_32, diff_32) \
115 SET_FLAGS_OSZAPC_32(SUB_COUT_VEC((op1_32), (op2_32), (diff_32)), (diff_32))
116 #if BX_SUPPORT_X86_64
117 #define SET_FLAGS_OSZAPC_SUB_64(op1_64, op2_64, diff_64) \
118 SET_FLAGS_OSZAPC_64(SUB_COUT_VEC((op1_64), (op2_64), (diff_64)), (diff_64))
119 #endif
120
121 // *******************
122 // OSZAP
123 // *******************
124
125 /* size, carries, result */
126 #define SET_FLAGS_OSZAP_SIZE(size, lf_carries, lf_result) { \
127 bx_address temp = ((lf_carries) & (LF_MASK_AF)) | \
128 (((lf_carries) >> (size - 2)) << LF_BIT_PO); \
129 if ((size) == 32) temp = ((lf_carries) & ~(LF_MASK_PDB | LF_MASK_SD)); \
130 if ((size) == 16) temp = ((lf_carries) & (LF_MASK_AF)) | ((lf_carries) << 16); \
131 if ((size) == 8) temp = ((lf_carries) & (LF_MASK_AF)) | ((lf_carries) << 24); \
132 BX_CPU_THIS_PTR oszapc.result = (bx_address)(Bit##size##s)(lf_result); \
133 bx_address delta_c = (BX_CPU_THIS_PTR oszapc.auxbits ^ temp) & LF_MASK_CF; \
134 delta_c ^= (delta_c >> 1); \
135 BX_CPU_THIS_PTR oszapc.auxbits = (bx_address)(Bit32u)(temp ^ delta_c); \
136 }
137
138 /* carries, result */
139 #define SET_FLAGS_OSZAP_8(carries, result) \
140 SET_FLAGS_OSZAP_SIZE(8, carries, result)
141 #define SET_FLAGS_OSZAP_16(carries, result) \
142 SET_FLAGS_OSZAP_SIZE(16, carries, result)
143 #define SET_FLAGS_OSZAP_32(carries, result) \
144 SET_FLAGS_OSZAP_SIZE(32, carries, result)
145 #if BX_SUPPORT_X86_64
146 #define SET_FLAGS_OSZAP_64(carries, result) \
147 SET_FLAGS_OSZAP_SIZE(64, carries, result)
148 #endif
149
150 /* op1, op2, result */
151 #define SET_FLAGS_OSZAP_ADD_8(op1_8, op2_8, sum_8) \
152 SET_FLAGS_OSZAP_8(ADD_COUT_VEC((op1_8), (op2_8), (sum_8)), (sum_8))
153 #define SET_FLAGS_OSZAP_ADD_16(op1_16, op2_16, sum_16) \
154 SET_FLAGS_OSZAP_16(ADD_COUT_VEC((op1_16), (op2_16), (sum_16)), (sum_16))
155 #define SET_FLAGS_OSZAP_ADD_32(op1_32, op2_32, sum_32) \
156 SET_FLAGS_OSZAP_32(ADD_COUT_VEC((op1_32), (op2_32), (sum_32)), (sum_32))
157 #if BX_SUPPORT_X86_64
158 #define SET_FLAGS_OSZAP_ADD_64(op1_64, op2_64, sum_64) \
159 SET_FLAGS_OSZAP_64(ADD_COUT_VEC((op1_64), (op2_64), (sum_64)), (sum_64))
160 #endif
161
162 /* op1, op2, result */
163 #define SET_FLAGS_OSZAP_SUB_8(op1_8, op2_8, diff_8) \
164 SET_FLAGS_OSZAP_8(SUB_COUT_VEC((op1_8), (op2_8), (diff_8)), (diff_8))
165 #define SET_FLAGS_OSZAP_SUB_16(op1_16, op2_16, diff_16) \
166 SET_FLAGS_OSZAP_16(SUB_COUT_VEC((op1_16), (op2_16), (diff_16)), (diff_16))
167 #define SET_FLAGS_OSZAP_SUB_32(op1_32, op2_32, diff_32) \
168 SET_FLAGS_OSZAP_32(SUB_COUT_VEC((op1_32), (op2_32), (diff_32)), (diff_32))
169 #if BX_SUPPORT_X86_64
170 #define SET_FLAGS_OSZAP_SUB_64(op1_64, op2_64, diff_64) \
171 SET_FLAGS_OSZAP_64(SUB_COUT_VEC((op1_64), (op2_64), (diff_64)), (diff_64))
172 #endif
173
174 // *******************
175 // OSZAxC
176 // *******************
177
178 /* size, carries, result */
179 #define SET_FLAGS_OSZAxC_LOGIC_SIZE(size, lf_result) { \
180 bool saved_PF = getB_PF(); \
181 SET_FLAGS_OSZAPC_SIZE(size, (Bit##size##u)(0), lf_result); \
182 set_PF(saved_PF); \
183 }
184
185 /* result */
186 #define SET_FLAGS_OSZAxC_LOGIC_32(result_32) \
187 SET_FLAGS_OSZAxC_LOGIC_SIZE(32, (result_32))
188 #if BX_SUPPORT_X86_64
189 #define SET_FLAGS_OSZAxC_LOGIC_64(result_64) \
190 SET_FLAGS_OSZAxC_LOGIC_SIZE(64, (result_64))
191 #endif
192
193 struct bx_lazyflags_entry {
194 bx_address result;
195 bx_address auxbits;
196
197 BX_CPP_INLINE unsigned getB_OF(void) const;
198 BX_CPP_INLINE unsigned get_OF(void) const;
199 BX_CPP_INLINE void set_OF(bool val);
200 BX_CPP_INLINE void clear_OF(void);
201 BX_CPP_INLINE void assert_OF(void);
202
203 BX_CPP_INLINE unsigned getB_SF(void) const;
204 BX_CPP_INLINE unsigned get_SF(void) const;
205 BX_CPP_INLINE void set_SF(bool val);
206 BX_CPP_INLINE void clear_SF(void);
207 BX_CPP_INLINE void assert_SF(void);
208
209 BX_CPP_INLINE unsigned getB_ZF(void) const;
210 BX_CPP_INLINE unsigned get_ZF(void) const;
211 BX_CPP_INLINE void set_ZF(bool val);
212 BX_CPP_INLINE void clear_ZF(void);
213 BX_CPP_INLINE void assert_ZF(void);
214
215 BX_CPP_INLINE unsigned getB_AF(void) const;
216 BX_CPP_INLINE unsigned get_AF(void) const;
217 BX_CPP_INLINE void set_AF(bool val);
218 BX_CPP_INLINE void clear_AF(void);
219 BX_CPP_INLINE void assert_AF(void);
220
221 BX_CPP_INLINE unsigned getB_PF(void) const;
222 BX_CPP_INLINE unsigned get_PF(void) const;
223 BX_CPP_INLINE void set_PF(bool val);
224 BX_CPP_INLINE void clear_PF(void);
225 BX_CPP_INLINE void assert_PF(void);
226
227 BX_CPP_INLINE unsigned getB_CF(void) const;
228 BX_CPP_INLINE unsigned get_CF(void) const;
229 BX_CPP_INLINE void set_CF(bool val);
230 BX_CPP_INLINE void clear_CF(void);
231 BX_CPP_INLINE void assert_CF(void);
232
set_flags_OxxxxCbx_lazyflags_entry233 BX_CPP_INLINE void set_flags_OxxxxC(Bit32u new_of, Bit32u new_cf)
234 {
235 Bit32u temp_po = new_of ^ new_cf;
236 auxbits &= ~(LF_MASK_PO | LF_MASK_CF);
237 auxbits |= (temp_po << LF_BIT_PO) | (new_cf << LF_BIT_CF);
238 }
239
assert_flags_OxxxxCbx_lazyflags_entry240 BX_CPP_INLINE void assert_flags_OxxxxC() { set_flags_OxxxxC(1,1); }
241 };
242
243 /// OF ////////////////////////////////////////
getB_OF(void)244 BX_CPP_INLINE unsigned bx_lazyflags_entry::getB_OF(void) const
245 {
246 return ((auxbits + (1U << LF_BIT_PO)) >> LF_BIT_CF) & 1;
247 }
248
get_OF(void)249 BX_CPP_INLINE unsigned bx_lazyflags_entry::get_OF(void) const
250 {
251 return (auxbits + (1U << LF_BIT_PO)) & (1U << LF_BIT_CF);
252 }
253
set_OF(bool val)254 BX_CPP_INLINE void bx_lazyflags_entry::set_OF(bool val)
255 {
256 bool temp_cf = getB_CF();
257 set_flags_OxxxxC(val, temp_cf);
258 }
259
clear_OF(void)260 BX_CPP_INLINE void bx_lazyflags_entry::clear_OF(void)
261 {
262 bool temp_cf = getB_CF();
263 set_flags_OxxxxC(0, temp_cf);
264 }
265
assert_OF(void)266 BX_CPP_INLINE void bx_lazyflags_entry::assert_OF(void)
267 {
268 unsigned temp_cf = getB_CF();
269 set_flags_OxxxxC(1, temp_cf);
270 }
271
272 /// SF ////////////////////////////////////////
getB_SF(void)273 BX_CPP_INLINE unsigned bx_lazyflags_entry::getB_SF(void) const
274 {
275 return ((result >> BX_LF_SIGN_BIT) ^ (auxbits >> LF_BIT_SD)) & 1;
276 }
277
get_SF(void)278 BX_CPP_INLINE unsigned bx_lazyflags_entry::get_SF(void) const { return getB_SF(); }
279
set_SF(bool val)280 BX_CPP_INLINE void bx_lazyflags_entry::set_SF(bool val)
281 {
282 bool temp_sf = getB_SF();
283 auxbits ^= (temp_sf ^ val) << LF_BIT_SD;
284 }
285
clear_SF(void)286 BX_CPP_INLINE void bx_lazyflags_entry::clear_SF (void) { set_SF(0); }
assert_SF(void)287 BX_CPP_INLINE void bx_lazyflags_entry::assert_SF (void) { set_SF(1); }
288
289 /// ZF ////////////////////////////////////////
getB_ZF(void)290 BX_CPP_INLINE unsigned bx_lazyflags_entry::getB_ZF(void) const
291 {
292 return (0 == result);
293 }
294
get_ZF(void)295 BX_CPP_INLINE unsigned bx_lazyflags_entry::get_ZF(void) const { return getB_ZF(); }
296
set_ZF(bool val)297 BX_CPP_INLINE void bx_lazyflags_entry::set_ZF(bool val)
298 {
299 if (val) assert_ZF();
300 else clear_ZF();
301 }
302
clear_ZF(void)303 BX_CPP_INLINE void bx_lazyflags_entry::clear_ZF(void)
304 {
305 result |= (1 << 8);
306 }
307
assert_ZF(void)308 BX_CPP_INLINE void bx_lazyflags_entry::assert_ZF(void)
309 {
310 // merge the sign bit into the Sign Delta
311 auxbits ^= (((result >> BX_LF_SIGN_BIT) & 1) << LF_BIT_SD);
312
313 // merge the parity bits into the Parity Delta Byte
314 Bit32u temp_pdb = (255 & result);
315 auxbits ^= (temp_pdb << LF_BIT_PDB);
316
317 // now zero the .result value
318 result = 0;
319 }
320
321 /// AF ////////////////////////////////////////
322
323 // AF - bit 4 in EFLAGS, represented by bit LF_BIT_AF of oszapc.auxbits
getB_AF(void)324 BX_CPP_INLINE unsigned bx_lazyflags_entry::getB_AF(void) const
325 {
326 return (auxbits >> LF_BIT_AF) & 1;
327 }
328
get_AF(void)329 BX_CPP_INLINE unsigned bx_lazyflags_entry::get_AF(void) const
330 {
331 return (auxbits & LF_MASK_AF);
332 }
333
set_AF(bool val)334 BX_CPP_INLINE void bx_lazyflags_entry::set_AF(bool val)
335 {
336 auxbits &= ~(LF_MASK_AF);
337 auxbits |= (val) << LF_BIT_AF;
338 }
339
clear_AF(void)340 BX_CPP_INLINE void bx_lazyflags_entry::clear_AF(void)
341 {
342 auxbits &= ~(LF_MASK_AF);
343 }
344
assert_AF(void)345 BX_CPP_INLINE void bx_lazyflags_entry::assert_AF(void)
346 {
347 auxbits |= (LF_MASK_AF);
348 }
349
350 /// PF ////////////////////////////////////////
351
352 // PF - bit 2 in EFLAGS, represented by lower 8 bits of oszapc.result
getB_PF(void)353 BX_CPP_INLINE unsigned bx_lazyflags_entry::getB_PF(void) const
354 {
355 Bit32u temp = (255 & result);
356 temp = temp ^ (255 & (auxbits >> LF_BIT_PDB));
357 temp = (temp ^ (temp >> 4)) & 0x0F;
358 return (0x9669U >> temp) & 1;
359 }
360
get_PF(void)361 BX_CPP_INLINE unsigned bx_lazyflags_entry::get_PF(void) const { return getB_PF(); }
362
set_PF(bool val)363 BX_CPP_INLINE void bx_lazyflags_entry::set_PF(bool val)
364 {
365 Bit32u temp_pdb = (255 & result) ^ (!val);
366 auxbits &= ~(LF_MASK_PDB);
367 auxbits |= (temp_pdb << LF_BIT_PDB);
368 }
369
clear_PF(void)370 BX_CPP_INLINE void bx_lazyflags_entry::clear_PF (void) { set_PF(0); }
assert_PF(void)371 BX_CPP_INLINE void bx_lazyflags_entry::assert_PF (void) { set_PF(1); }
372
373 /// CF ////////////////////////////////////////
374
getB_CF(void)375 BX_CPP_INLINE unsigned bx_lazyflags_entry::getB_CF(void) const
376 {
377 return (auxbits >> LF_BIT_CF) & 1;
378 }
379
get_CF(void)380 BX_CPP_INLINE unsigned bx_lazyflags_entry::get_CF(void) const
381 {
382 return (auxbits & LF_MASK_CF);
383 }
384
set_CF(bool val)385 BX_CPP_INLINE void bx_lazyflags_entry::set_CF(bool val)
386 {
387 bool temp_of = getB_OF();
388 set_flags_OxxxxC(temp_of, val);
389 }
390
clear_CF(void)391 BX_CPP_INLINE void bx_lazyflags_entry::clear_CF(void)
392 {
393 bool temp_of = getB_OF();
394 set_flags_OxxxxC(temp_of, 0);
395 }
396
assert_CF(void)397 BX_CPP_INLINE void bx_lazyflags_entry::assert_CF(void)
398 {
399 bool temp_of = getB_OF();
400 set_flags_OxxxxC(temp_of, 1);
401 }
402
403 #endif // BX_LAZY_FLAGS_DEF
404