20 #define NO_SYS_HEADERS 21 #ifndef NO_SYS_HEADERS 25 #define PRIM_OPS_NO_REDEFINE_ASM 26 #include "x86emu/x86emui.h" 30 static u32 x86emu_parity_tab[8] = {
31 0x96696996, 0x69969669, 0x69969669, 0x96696996,
32 0x69969669, 0x96696996, 0x96696996, 0x69969669,
35 #define PARITY(x) (((x86emu_parity_tab[(x) / 32] >> ((x) % 32)) & 1) == 0) 36 #define XOR2(x) (((x) ^ ((x) >> 1)) & 0x1) 47 if ((d & 0xf) > 0x9 || ACCESS_FLAG(F_AF))
59 res = (u16)(d & 0xFF0F);
61 CONDITIONAL_SET_FLAG(res == 0, F_ZF);
62 CONDITIONAL_SET_FLAG(PARITY(res & 0xff), F_PF);
73 if ((d & 0xf) > 0x9 || ACCESS_FLAG(F_AF))
85 res = (u16)(d & 0xFF0F);
87 CONDITIONAL_SET_FLAG(res == 0, F_ZF);
88 CONDITIONAL_SET_FLAG(PARITY(res & 0xff), F_PF);
101 hb = (u8)((d >> 8) & 0xff);
102 lb = (u8)((d & 0xff));
103 l = (u16)((lb + 10 * hb) & 0xFF);
108 CONDITIONAL_SET_FLAG(l & 0x80, F_SF);
109 CONDITIONAL_SET_FLAG(l == 0, F_ZF);
110 CONDITIONAL_SET_FLAG(PARITY(l & 0xff), F_PF);
129 CONDITIONAL_SET_FLAG(l & 0x80, F_SF);
130 CONDITIONAL_SET_FLAG(l == 0, F_ZF);
131 CONDITIONAL_SET_FLAG(PARITY(l & 0xff), F_PF);
139 u8 adc_byte(u8 d, u8 s)
144 if (ACCESS_FLAG(F_CF))
149 CONDITIONAL_SET_FLAG(res & 0x100, F_CF);
150 CONDITIONAL_SET_FLAG((res & 0xff) == 0, F_ZF);
151 CONDITIONAL_SET_FLAG(res & 0x80, F_SF);
152 CONDITIONAL_SET_FLAG(PARITY(res & 0xff), F_PF);
155 cc = (s & d) | ((~res) & (s | d));
156 CONDITIONAL_SET_FLAG(XOR2(cc >> 6), F_OF);
157 CONDITIONAL_SET_FLAG(cc & 0x8, F_AF);
165 u16 adc_word(u16 d, u16 s)
170 if (ACCESS_FLAG(F_CF))
175 CONDITIONAL_SET_FLAG(res & 0x10000, F_CF);
176 CONDITIONAL_SET_FLAG((res & 0xffff) == 0, F_ZF);
177 CONDITIONAL_SET_FLAG(res & 0x8000, F_SF);
178 CONDITIONAL_SET_FLAG(PARITY(res & 0xff), F_PF);
181 cc = (s & d) | ((~res) & (s | d));
182 CONDITIONAL_SET_FLAG(XOR2(cc >> 14), F_OF);
183 CONDITIONAL_SET_FLAG(cc & 0x8, F_AF);
191 u32 adc_long(u32 d, u32 s)
198 if (ACCESS_FLAG(F_CF))
200 lo = 1 + (d & 0xFFFF) + (s & 0xFFFF);
205 lo = (d & 0xFFFF) + (s & 0xFFFF);
208 hi = (lo >> 16) + (d >> 16) + (s >> 16);
210 CONDITIONAL_SET_FLAG(hi & 0x10000, F_CF);
211 CONDITIONAL_SET_FLAG((res & 0xffffffff) == 0, F_ZF);
212 CONDITIONAL_SET_FLAG(res & 0x80000000, F_SF);
213 CONDITIONAL_SET_FLAG(PARITY(res & 0xff), F_PF);
216 cc = (s & d) | ((~res) & (s | d));
217 CONDITIONAL_SET_FLAG(XOR2(cc >> 30), F_OF);
218 CONDITIONAL_SET_FLAG(cc & 0x8, F_AF);
226 u8 add_byte(u8 d, u8 s)
232 CONDITIONAL_SET_FLAG(res & 0x100, F_CF);
233 CONDITIONAL_SET_FLAG((res & 0xff) == 0, F_ZF);
234 CONDITIONAL_SET_FLAG(res & 0x80, F_SF);
235 CONDITIONAL_SET_FLAG(PARITY(res & 0xff), F_PF);
238 cc = (s & d) | ((~res) & (s | d));
239 CONDITIONAL_SET_FLAG(XOR2(cc >> 6), F_OF);
240 CONDITIONAL_SET_FLAG(cc & 0x8, F_AF);
248 u16 add_word(u16 d, u16 s)
254 CONDITIONAL_SET_FLAG(res & 0x10000, F_CF);
255 CONDITIONAL_SET_FLAG((res & 0xffff) == 0, F_ZF);
256 CONDITIONAL_SET_FLAG(res & 0x8000, F_SF);
257 CONDITIONAL_SET_FLAG(PARITY(res & 0xff), F_PF);
260 cc = (s & d) | ((~res) & (s | d));
261 CONDITIONAL_SET_FLAG(XOR2(cc >> 14), F_OF);
262 CONDITIONAL_SET_FLAG(cc & 0x8, F_AF);
270 u32 add_long(u32 d, u32 s)
277 lo = (d & 0xFFFF) + (s & 0xFFFF);
279 hi = (lo >> 16) + (d >> 16) + (s >> 16);
281 CONDITIONAL_SET_FLAG(hi & 0x10000, F_CF);
282 CONDITIONAL_SET_FLAG((res & 0xffffffff) == 0, F_ZF);
283 CONDITIONAL_SET_FLAG(res & 0x80000000, F_SF);
284 CONDITIONAL_SET_FLAG(PARITY(res & 0xff), F_PF);
287 cc = (s & d) | ((~res) & (s | d));
288 CONDITIONAL_SET_FLAG(XOR2(cc >> 30), F_OF);
289 CONDITIONAL_SET_FLAG(cc & 0x8, F_AF);
298 u8 and_byte(u8 d, u8 s)
308 CONDITIONAL_SET_FLAG(res & 0x80, F_SF);
309 CONDITIONAL_SET_FLAG(res == 0, F_ZF);
310 CONDITIONAL_SET_FLAG(PARITY(res), F_PF);
318 u16 and_word(u16 d, u16 s)
328 CONDITIONAL_SET_FLAG(res & 0x8000, F_SF);
329 CONDITIONAL_SET_FLAG(res == 0, F_ZF);
330 CONDITIONAL_SET_FLAG(PARITY(res & 0xff), F_PF);
338 u32 and_long(u32 d, u32 s)
348 CONDITIONAL_SET_FLAG(res & 0x80000000, F_SF);
349 CONDITIONAL_SET_FLAG(res == 0, F_ZF);
350 CONDITIONAL_SET_FLAG(PARITY(res & 0xff), F_PF);
358 u8 cmp_byte(u8 d, u8 s)
365 CONDITIONAL_SET_FLAG(res & 0x80, F_SF);
366 CONDITIONAL_SET_FLAG((res & 0xff) == 0, F_ZF);
367 CONDITIONAL_SET_FLAG(PARITY(res & 0xff), F_PF);
370 bc = (res & (~d | s)) | (~d & s);
371 CONDITIONAL_SET_FLAG(bc & 0x80, F_CF);
372 CONDITIONAL_SET_FLAG(XOR2(bc >> 6), F_OF);
373 CONDITIONAL_SET_FLAG(bc & 0x8, F_AF);
381 u16 cmp_word(u16 d, u16 s)
387 CONDITIONAL_SET_FLAG(res & 0x8000, F_SF);
388 CONDITIONAL_SET_FLAG((res & 0xffff) == 0, F_ZF);
389 CONDITIONAL_SET_FLAG(PARITY(res & 0xff), F_PF);
392 bc = (res & (~d | s)) | (~d & s);
393 CONDITIONAL_SET_FLAG(bc & 0x8000, F_CF);
394 CONDITIONAL_SET_FLAG(XOR2(bc >> 14), F_OF);
395 CONDITIONAL_SET_FLAG(bc & 0x8, F_AF);
403 u32 cmp_long(u32 d, u32 s)
409 CONDITIONAL_SET_FLAG(res & 0x80000000, F_SF);
410 CONDITIONAL_SET_FLAG((res & 0xffffffff) == 0, F_ZF);
411 CONDITIONAL_SET_FLAG(PARITY(res & 0xff), F_PF);
414 bc = (res & (~d | s)) | (~d & s);
415 CONDITIONAL_SET_FLAG(bc & 0x80000000, F_CF);
416 CONDITIONAL_SET_FLAG(XOR2(bc >> 30), F_OF);
417 CONDITIONAL_SET_FLAG(bc & 0x8, F_AF);
428 if ((d & 0xf) > 9 || ACCESS_FLAG(F_AF))
433 if (res > 0x9F || ACCESS_FLAG(F_CF))
438 CONDITIONAL_SET_FLAG(res & 0x80, F_SF);
439 CONDITIONAL_SET_FLAG((res & 0xFF) == 0, F_ZF);
440 CONDITIONAL_SET_FLAG(PARITY(res & 0xff), F_PF);
450 if ((d & 0xf) > 9 || ACCESS_FLAG(F_AF))
455 if (d > 0x9F || ACCESS_FLAG(F_CF))
460 CONDITIONAL_SET_FLAG(d & 0x80, F_SF);
461 CONDITIONAL_SET_FLAG(d == 0, F_ZF);
462 CONDITIONAL_SET_FLAG(PARITY(d & 0xff), F_PF);
476 CONDITIONAL_SET_FLAG(res & 0x80, F_SF);
477 CONDITIONAL_SET_FLAG((res & 0xff) == 0, F_ZF);
478 CONDITIONAL_SET_FLAG(PARITY(res & 0xff), F_PF);
482 bc = (res & (~d | 1)) | (~d & 1);
484 CONDITIONAL_SET_FLAG(XOR2(bc >> 6), F_OF);
485 CONDITIONAL_SET_FLAG(bc & 0x8, F_AF);
499 CONDITIONAL_SET_FLAG(res & 0x8000, F_SF);
500 CONDITIONAL_SET_FLAG((res & 0xffff) == 0, F_ZF);
501 CONDITIONAL_SET_FLAG(PARITY(res & 0xff), F_PF);
505 bc = (res & (~d | 1)) | (~d & 1);
507 CONDITIONAL_SET_FLAG(XOR2(bc >> 14), F_OF);
508 CONDITIONAL_SET_FLAG(bc & 0x8, F_AF);
523 CONDITIONAL_SET_FLAG(res & 0x80000000, F_SF);
524 CONDITIONAL_SET_FLAG((res & 0xffffffff) == 0, F_ZF);
525 CONDITIONAL_SET_FLAG(PARITY(res & 0xff), F_PF);
528 bc = (res & (~d | 1)) | (~d & 1);
530 CONDITIONAL_SET_FLAG(XOR2(bc >> 30), F_OF);
531 CONDITIONAL_SET_FLAG(bc & 0x8, F_AF);
545 CONDITIONAL_SET_FLAG((res & 0xff) == 0, F_ZF);
546 CONDITIONAL_SET_FLAG(res & 0x80, F_SF);
547 CONDITIONAL_SET_FLAG(PARITY(res & 0xff), F_PF);
550 cc = ((1 & d) | (~res)) & (1 | d);
551 CONDITIONAL_SET_FLAG(XOR2(cc >> 6), F_OF);
552 CONDITIONAL_SET_FLAG(cc & 0x8, F_AF);
566 CONDITIONAL_SET_FLAG((res & 0xffff) == 0, F_ZF);
567 CONDITIONAL_SET_FLAG(res & 0x8000, F_SF);
568 CONDITIONAL_SET_FLAG(PARITY(res & 0xff), F_PF);
571 cc = (1 & d) | ((~res) & (1 | d));
572 CONDITIONAL_SET_FLAG(XOR2(cc >> 14), F_OF);
573 CONDITIONAL_SET_FLAG(cc & 0x8, F_AF);
587 CONDITIONAL_SET_FLAG((res & 0xffffffff) == 0, F_ZF);
588 CONDITIONAL_SET_FLAG(res & 0x80000000, F_SF);
589 CONDITIONAL_SET_FLAG(PARITY(res & 0xff), F_PF);
592 cc = (1 & d) | ((~res) & (1 | d));
593 CONDITIONAL_SET_FLAG(XOR2(cc >> 30), F_OF);
594 CONDITIONAL_SET_FLAG(cc & 0x8, F_AF);
602 u8 or_byte(u8 d, u8 s)
610 CONDITIONAL_SET_FLAG(res & 0x80, F_SF);
611 CONDITIONAL_SET_FLAG(res == 0, F_ZF);
612 CONDITIONAL_SET_FLAG(PARITY(res), F_PF);
620 u16 or_word(u16 d, u16 s)
629 CONDITIONAL_SET_FLAG(res & 0x8000, F_SF);
630 CONDITIONAL_SET_FLAG(res == 0, F_ZF);
631 CONDITIONAL_SET_FLAG(PARITY(res & 0xff), F_PF);
639 u32 or_long(u32 d, u32 s)
649 CONDITIONAL_SET_FLAG(res & 0x80000000, F_SF);
650 CONDITIONAL_SET_FLAG(res == 0, F_ZF);
651 CONDITIONAL_SET_FLAG(PARITY(res & 0xff), F_PF);
664 CONDITIONAL_SET_FLAG(s != 0, F_CF);
666 CONDITIONAL_SET_FLAG((res & 0xff) == 0, F_ZF);
667 CONDITIONAL_SET_FLAG(res & 0x80, F_SF);
668 CONDITIONAL_SET_FLAG(PARITY(res), F_PF);
675 CONDITIONAL_SET_FLAG(XOR2(bc >> 6), F_OF);
676 CONDITIONAL_SET_FLAG(bc & 0x8, F_AF);
689 CONDITIONAL_SET_FLAG(s != 0, F_CF);
691 CONDITIONAL_SET_FLAG((res & 0xffff) == 0, F_ZF);
692 CONDITIONAL_SET_FLAG(res & 0x8000, F_SF);
693 CONDITIONAL_SET_FLAG(PARITY(res & 0xff), F_PF);
701 CONDITIONAL_SET_FLAG(XOR2(bc >> 14), F_OF);
702 CONDITIONAL_SET_FLAG(bc & 0x8, F_AF);
715 CONDITIONAL_SET_FLAG(s != 0, F_CF);
717 CONDITIONAL_SET_FLAG((res & 0xffffffff) == 0, F_ZF);
718 CONDITIONAL_SET_FLAG(res & 0x80000000, F_SF);
719 CONDITIONAL_SET_FLAG(PARITY(res & 0xff), F_PF);
727 CONDITIONAL_SET_FLAG(XOR2(bc >> 30), F_OF);
728 CONDITIONAL_SET_FLAG(bc & 0x8, F_AF);
763 u8 rcl_byte(u8 d, u8 s)
765 register unsigned int res, cnt, mask, cf;
794 if ((cnt = s % 9) != 0)
798 cf = (d >> (8 - cnt)) & 0x1;
804 res = (d << cnt) & 0xff;
812 mask = (1 << (cnt - 1)) - 1;
813 res |= (d >> (9 - cnt)) & mask;
816 if (ACCESS_FLAG(F_CF))
819 res |= 1 << (cnt - 1);
822 CONDITIONAL_SET_FLAG(cf, F_CF);
827 CONDITIONAL_SET_FLAG(cnt == 1 && XOR2(cf + ((res >> 6) & 0x2)), F_OF);
836 u16 rcl_word(u16 d, u8 s)
838 register unsigned int res, cnt, mask, cf;
841 if ((cnt = s % 17) != 0)
843 cf = (d >> (16 - cnt)) & 0x1;
844 res = (d << cnt) & 0xffff;
845 mask = (1 << (cnt - 1)) - 1;
846 res |= (d >> (17 - cnt)) & mask;
847 if (ACCESS_FLAG(F_CF))
849 res |= 1 << (cnt - 1);
851 CONDITIONAL_SET_FLAG(cf, F_CF);
852 CONDITIONAL_SET_FLAG(cnt == 1 && XOR2(cf + ((res >> 14) & 0x2)), F_OF);
861 u32 rcl_long(u32 d, u8 s)
863 register u32 res, cnt, mask, cf;
866 if ((cnt = s % 33) != 0)
868 cf = (d >> (32 - cnt)) & 0x1;
869 res = (d << cnt) & 0xffffffff;
870 mask = (1 << (cnt - 1)) - 1;
871 res |= (d >> (33 - cnt)) & mask;
872 if (ACCESS_FLAG(F_CF))
874 res |= 1 << (cnt - 1);
876 CONDITIONAL_SET_FLAG(cf, F_CF);
877 CONDITIONAL_SET_FLAG(cnt == 1 && XOR2(cf + ((res >> 30) & 0x2)), F_OF);
886 u8 rcr_byte(u8 d, u8 s)
889 u32 mask, cf, ocf = 0;
914 if ((cnt = s % 9) != 0)
928 ocf = ACCESS_FLAG(F_CF) != 0;
931 cf = (d >> (cnt - 1)) & 0x1;
940 mask = (1 << (8 - cnt)) - 1;
941 res = (d >> cnt) & mask;
949 res |= (d << (9 - cnt));
952 if (ACCESS_FLAG(F_CF))
955 res |= 1 << (8 - cnt);
958 CONDITIONAL_SET_FLAG(cf, F_CF);
964 CONDITIONAL_SET_FLAG(XOR2(ocf + ((d >> 6) & 0x2)), F_OF);
974 u16 rcr_word(u16 d, u8 s)
977 u32 mask, cf, ocf = 0;
981 if ((cnt = s % 17) != 0)
986 ocf = ACCESS_FLAG(F_CF) != 0;
989 cf = (d >> (cnt - 1)) & 0x1;
990 mask = (1 << (16 - cnt)) - 1;
991 res = (d >> cnt) & mask;
992 res |= (d << (17 - cnt));
993 if (ACCESS_FLAG(F_CF))
995 res |= 1 << (16 - cnt);
997 CONDITIONAL_SET_FLAG(cf, F_CF);
1000 CONDITIONAL_SET_FLAG(XOR2(ocf + ((d >> 14) & 0x2)), F_OF);
1010 u32 rcr_long(u32 d, u8 s)
1013 u32 mask, cf, ocf = 0;
1017 if ((cnt = s % 33) != 0)
1022 ocf = ACCESS_FLAG(F_CF) != 0;
1025 cf = (d >> (cnt - 1)) & 0x1;
1026 mask = (1 << (32 - cnt)) - 1;
1027 res = (d >> cnt) & mask;
1029 res |= (d << (33 - cnt));
1030 if (ACCESS_FLAG(F_CF))
1032 res |= 1 << (32 - cnt);
1034 CONDITIONAL_SET_FLAG(cf, F_CF);
1037 CONDITIONAL_SET_FLAG(XOR2(ocf + ((d >> 30) & 0x2)), F_OF);
1047 u8 rol_byte(u8 d, u8 s)
1049 register unsigned int res, cnt, mask;
1068 if ((cnt = s % 8) != 0)
1074 mask = (1 << cnt) - 1;
1075 res |= (d >> (8 - cnt)) & mask;
1079 CONDITIONAL_SET_FLAG(res & 0x1, F_CF);
1082 CONDITIONAL_SET_FLAG(
1083 s == 1 && XOR2((res & 0x1) + ((res >> 6) & 0x2)), F_OF);
1089 CONDITIONAL_SET_FLAG(res & 0x1, F_CF);
1098 u16 rol_word(u16 d, u8 s)
1100 register unsigned int res, cnt, mask;
1103 if ((cnt = s % 16) != 0)
1106 mask = (1 << cnt) - 1;
1107 res |= (d >> (16 - cnt)) & mask;
1108 CONDITIONAL_SET_FLAG(res & 0x1, F_CF);
1109 CONDITIONAL_SET_FLAG(
1110 s == 1 && XOR2((res & 0x1) + ((res >> 14) & 0x2)), F_OF);
1116 CONDITIONAL_SET_FLAG(res & 0x1, F_CF);
1125 u32 rol_long(u32 d, u8 s)
1127 register u32 res, cnt, mask;
1130 if ((cnt = s % 32) != 0)
1133 mask = (1 << cnt) - 1;
1134 res |= (d >> (32 - cnt)) & mask;
1135 CONDITIONAL_SET_FLAG(res & 0x1, F_CF);
1136 CONDITIONAL_SET_FLAG(
1137 s == 1 && XOR2((res & 0x1) + ((res >> 30) & 0x2)), F_OF);
1143 CONDITIONAL_SET_FLAG(res & 0x1, F_CF);
1152 u8 ror_byte(u8 d, u8 s)
1154 register unsigned int res, cnt, mask;
1172 if ((cnt = s % 8) != 0)
1175 res = (d << (8 - cnt));
1178 mask = (1 << (8 - cnt)) - 1;
1179 res |= (d >> (cnt)) & mask;
1183 CONDITIONAL_SET_FLAG(res & 0x80, F_CF);
1186 CONDITIONAL_SET_FLAG(s == 1 && XOR2(res >> 6), F_OF);
1192 CONDITIONAL_SET_FLAG(res & 0x80, F_CF);
1201 u16 ror_word(u16 d, u8 s)
1203 register unsigned int res, cnt, mask;
1206 if ((cnt = s % 16) != 0)
1208 res = (d << (16 - cnt));
1209 mask = (1 << (16 - cnt)) - 1;
1210 res |= (d >> (cnt)) & mask;
1211 CONDITIONAL_SET_FLAG(res & 0x8000, F_CF);
1212 CONDITIONAL_SET_FLAG(s == 1 && XOR2(res >> 14), F_OF);
1218 CONDITIONAL_SET_FLAG(res & 0x8000, F_CF);
1227 u32 ror_long(u32 d, u8 s)
1229 register u32 res, cnt, mask;
1232 if ((cnt = s % 32) != 0)
1234 res = (d << (32 - cnt));
1235 mask = (1 << (32 - cnt)) - 1;
1236 res |= (d >> (cnt)) & mask;
1237 CONDITIONAL_SET_FLAG(res & 0x80000000, F_CF);
1238 CONDITIONAL_SET_FLAG(s == 1 && XOR2(res >> 30), F_OF);
1244 CONDITIONAL_SET_FLAG(res & 0x80000000, F_CF);
1253 u8 shl_byte(u8 d, u8 s)
1255 unsigned int cnt, res, cf;
1265 cf = d & (1 << (8 - cnt));
1266 CONDITIONAL_SET_FLAG(cf, F_CF);
1267 CONDITIONAL_SET_FLAG((res & 0xff) == 0, F_ZF);
1268 CONDITIONAL_SET_FLAG(res & 0x80, F_SF);
1269 CONDITIONAL_SET_FLAG(PARITY(res & 0xff), F_PF);
1279 CONDITIONAL_SET_FLAG(
1280 (((res & 0x80) == 0x80) ^ (ACCESS_FLAG(F_CF) != 0)),
1292 CONDITIONAL_SET_FLAG((d << (s - 1)) & 0x80, F_CF);
1305 u16 shl_word(u16 d, u8 s)
1307 unsigned int cnt, res, cf;
1315 cf = d & (1 << (16 - cnt));
1316 CONDITIONAL_SET_FLAG(cf, F_CF);
1317 CONDITIONAL_SET_FLAG((res & 0xffff) == 0, F_ZF);
1318 CONDITIONAL_SET_FLAG(res & 0x8000, F_SF);
1319 CONDITIONAL_SET_FLAG(PARITY(res & 0xff), F_PF);
1328 CONDITIONAL_SET_FLAG(
1329 (((res & 0x8000) == 0x8000) ^ (ACCESS_FLAG(F_CF) != 0)), F_OF);
1339 CONDITIONAL_SET_FLAG((d << (s - 1)) & 0x8000, F_CF);
1352 u32 shl_long(u32 d, u8 s)
1354 unsigned int cnt, res, cf;
1362 cf = d & (1 << (32 - cnt));
1363 CONDITIONAL_SET_FLAG(cf, F_CF);
1364 CONDITIONAL_SET_FLAG((res & 0xffffffff) == 0, F_ZF);
1365 CONDITIONAL_SET_FLAG(res & 0x80000000, F_SF);
1366 CONDITIONAL_SET_FLAG(PARITY(res & 0xff), F_PF);
1374 CONDITIONAL_SET_FLAG(
1375 (((res & 0x80000000) == 0x80000000) ^ (ACCESS_FLAG(F_CF) != 0)),
1386 CONDITIONAL_SET_FLAG((d << (s - 1)) & 0x80000000, F_CF);
1399 u8 shr_byte(u8 d, u8 s)
1401 unsigned int cnt, res, cf;
1408 cf = d & (1 << (cnt - 1));
1410 CONDITIONAL_SET_FLAG(cf, F_CF);
1411 CONDITIONAL_SET_FLAG((res & 0xff) == 0, F_ZF);
1412 CONDITIONAL_SET_FLAG(res & 0x80, F_SF);
1413 CONDITIONAL_SET_FLAG(PARITY(res & 0xff), F_PF);
1422 CONDITIONAL_SET_FLAG(XOR2(res >> 6), F_OF);
1432 CONDITIONAL_SET_FLAG((d >> (s - 1)) & 0x1, F_CF);
1445 u16 shr_word(u16 d, u8 s)
1447 unsigned int cnt, res, cf;
1454 cf = d & (1 << (cnt - 1));
1456 CONDITIONAL_SET_FLAG(cf, F_CF);
1457 CONDITIONAL_SET_FLAG((res & 0xffff) == 0, F_ZF);
1458 CONDITIONAL_SET_FLAG(res & 0x8000, F_SF);
1459 CONDITIONAL_SET_FLAG(PARITY(res & 0xff), F_PF);
1468 CONDITIONAL_SET_FLAG(XOR2(res >> 14), F_OF);
1491 u32 shr_long(u32 d, u8 s)
1493 unsigned int cnt, res, cf;
1500 cf = d & (1 << (cnt - 1));
1502 CONDITIONAL_SET_FLAG(cf, F_CF);
1503 CONDITIONAL_SET_FLAG((res & 0xffffffff) == 0, F_ZF);
1504 CONDITIONAL_SET_FLAG(res & 0x80000000, F_SF);
1505 CONDITIONAL_SET_FLAG(PARITY(res & 0xff), F_PF);
1513 CONDITIONAL_SET_FLAG(XOR2(res >> 30), F_OF);
1536 u8 sar_byte(u8 d, u8 s)
1538 unsigned int cnt, res, cf, mask, sf;
1543 if (cnt > 0 && cnt < 8)
1545 mask = (1 << (8 - cnt)) - 1;
1546 cf = d & (1 << (cnt - 1));
1547 res = (d >> cnt) & mask;
1548 CONDITIONAL_SET_FLAG(cf, F_CF);
1553 CONDITIONAL_SET_FLAG((res & 0xff) == 0, F_ZF);
1554 CONDITIONAL_SET_FLAG(PARITY(res & 0xff), F_PF);
1555 CONDITIONAL_SET_FLAG(res & 0x80, F_SF);
1583 u16 sar_word(u16 d, u8 s)
1585 unsigned int cnt, res, cf, mask, sf;
1590 if (cnt > 0 && cnt < 16)
1592 mask = (1 << (16 - cnt)) - 1;
1593 cf = d & (1 << (cnt - 1));
1594 res = (d >> cnt) & mask;
1595 CONDITIONAL_SET_FLAG(cf, F_CF);
1600 CONDITIONAL_SET_FLAG((res & 0xffff) == 0, F_ZF);
1601 CONDITIONAL_SET_FLAG(res & 0x8000, F_SF);
1602 CONDITIONAL_SET_FLAG(PARITY(res & 0xff), F_PF);
1630 u32 sar_long(u32 d, u8 s)
1632 u32 cnt, res, cf, mask, sf;
1634 sf = d & 0x80000000;
1637 if (cnt > 0 && cnt < 32)
1639 mask = (1 << (32 - cnt)) - 1;
1640 cf = d & (1 << (cnt - 1));
1641 res = (d >> cnt) & mask;
1642 CONDITIONAL_SET_FLAG(cf, F_CF);
1647 CONDITIONAL_SET_FLAG((res & 0xffffffff) == 0, F_ZF);
1648 CONDITIONAL_SET_FLAG(res & 0x80000000, F_SF);
1649 CONDITIONAL_SET_FLAG(PARITY(res & 0xff), F_PF);
1677 u16 shld_word(u16 d, u16 fill, u8 s)
1679 unsigned int cnt, res, cf;
1686 res = (d << cnt) | (fill >> (16 - cnt));
1687 cf = d & (1 << (16 - cnt));
1688 CONDITIONAL_SET_FLAG(cf, F_CF);
1689 CONDITIONAL_SET_FLAG((res & 0xffff) == 0, F_ZF);
1690 CONDITIONAL_SET_FLAG(res & 0x8000, F_SF);
1691 CONDITIONAL_SET_FLAG(PARITY(res & 0xff), F_PF);
1699 CONDITIONAL_SET_FLAG(
1700 (((res & 0x8000) == 0x8000) ^ (ACCESS_FLAG(F_CF) != 0)), F_OF);
1710 CONDITIONAL_SET_FLAG((d << (s - 1)) & 0x8000, F_CF);
1723 u32 shld_long(u32 d, u32 fill, u8 s)
1725 unsigned int cnt, res, cf;
1732 res = (d << cnt) | (fill >> (32 - cnt));
1733 cf = d & (1 << (32 - cnt));
1734 CONDITIONAL_SET_FLAG(cf, F_CF);
1735 CONDITIONAL_SET_FLAG((res & 0xffffffff) == 0, F_ZF);
1736 CONDITIONAL_SET_FLAG(res & 0x80000000, F_SF);
1737 CONDITIONAL_SET_FLAG(PARITY(res & 0xff), F_PF);
1745 CONDITIONAL_SET_FLAG(
1746 (((res & 0x80000000) == 0x80000000) ^ (ACCESS_FLAG(F_CF) != 0)),
1757 CONDITIONAL_SET_FLAG((d << (s - 1)) & 0x80000000, F_CF);
1770 u16 shrd_word(u16 d, u16 fill, u8 s)
1772 unsigned int cnt, res, cf;
1779 cf = d & (1 << (cnt - 1));
1780 res = (d >> cnt) | (fill << (16 - cnt));
1781 CONDITIONAL_SET_FLAG(cf, F_CF);
1782 CONDITIONAL_SET_FLAG((res & 0xffff) == 0, F_ZF);
1783 CONDITIONAL_SET_FLAG(res & 0x8000, F_SF);
1784 CONDITIONAL_SET_FLAG(PARITY(res & 0xff), F_PF);
1793 CONDITIONAL_SET_FLAG(XOR2(res >> 14), F_OF);
1816 u32 shrd_long(u32 d, u32 fill, u8 s)
1818 unsigned int cnt, res, cf;
1825 cf = d & (1 << (cnt - 1));
1826 res = (d >> cnt) | (fill << (32 - cnt));
1827 CONDITIONAL_SET_FLAG(cf, F_CF);
1828 CONDITIONAL_SET_FLAG((res & 0xffffffff) == 0, F_ZF);
1829 CONDITIONAL_SET_FLAG(res & 0x80000000, F_SF);
1830 CONDITIONAL_SET_FLAG(PARITY(res & 0xff), F_PF);
1838 CONDITIONAL_SET_FLAG(XOR2(res >> 30), F_OF);
1861 u8 sbb_byte(u8 d, u8 s)
1866 if (ACCESS_FLAG(F_CF))
1870 CONDITIONAL_SET_FLAG(res & 0x80, F_SF);
1871 CONDITIONAL_SET_FLAG((res & 0xff) == 0, F_ZF);
1872 CONDITIONAL_SET_FLAG(PARITY(res & 0xff), F_PF);
1875 bc = (res & (~d | s)) | (~d & s);
1876 CONDITIONAL_SET_FLAG(bc & 0x80, F_CF);
1877 CONDITIONAL_SET_FLAG(XOR2(bc >> 6), F_OF);
1878 CONDITIONAL_SET_FLAG(bc & 0x8, F_AF);
1886 u16 sbb_word(u16 d, u16 s)
1891 if (ACCESS_FLAG(F_CF))
1895 CONDITIONAL_SET_FLAG(res & 0x8000, F_SF);
1896 CONDITIONAL_SET_FLAG((res & 0xffff) == 0, F_ZF);
1897 CONDITIONAL_SET_FLAG(PARITY(res & 0xff), F_PF);
1900 bc = (res & (~d | s)) | (~d & s);
1901 CONDITIONAL_SET_FLAG(bc & 0x8000, F_CF);
1902 CONDITIONAL_SET_FLAG(XOR2(bc >> 14), F_OF);
1903 CONDITIONAL_SET_FLAG(bc & 0x8, F_AF);
1911 u32 sbb_long(u32 d, u32 s)
1916 if (ACCESS_FLAG(F_CF))
1920 CONDITIONAL_SET_FLAG(res & 0x80000000, F_SF);
1921 CONDITIONAL_SET_FLAG((res & 0xffffffff) == 0, F_ZF);
1922 CONDITIONAL_SET_FLAG(PARITY(res & 0xff), F_PF);
1925 bc = (res & (~d | s)) | (~d & s);
1926 CONDITIONAL_SET_FLAG(bc & 0x80000000, F_CF);
1927 CONDITIONAL_SET_FLAG(XOR2(bc >> 30), F_OF);
1928 CONDITIONAL_SET_FLAG(bc & 0x8, F_AF);
1936 u8 sub_byte(u8 d, u8 s)
1942 CONDITIONAL_SET_FLAG(res & 0x80, F_SF);
1943 CONDITIONAL_SET_FLAG((res & 0xff) == 0, F_ZF);
1944 CONDITIONAL_SET_FLAG(PARITY(res & 0xff), F_PF);
1947 bc = (res & (~d | s)) | (~d & s);
1948 CONDITIONAL_SET_FLAG(bc & 0x80, F_CF);
1949 CONDITIONAL_SET_FLAG(XOR2(bc >> 6), F_OF);
1950 CONDITIONAL_SET_FLAG(bc & 0x8, F_AF);
1958 u16 sub_word(u16 d, u16 s)
1964 CONDITIONAL_SET_FLAG(res & 0x8000, F_SF);
1965 CONDITIONAL_SET_FLAG((res & 0xffff) == 0, F_ZF);
1966 CONDITIONAL_SET_FLAG(PARITY(res & 0xff), F_PF);
1969 bc = (res & (~d | s)) | (~d & s);
1970 CONDITIONAL_SET_FLAG(bc & 0x8000, F_CF);
1971 CONDITIONAL_SET_FLAG(XOR2(bc >> 14), F_OF);
1972 CONDITIONAL_SET_FLAG(bc & 0x8, F_AF);
1980 u32 sub_long(u32 d, u32 s)
1986 CONDITIONAL_SET_FLAG(res & 0x80000000, F_SF);
1987 CONDITIONAL_SET_FLAG((res & 0xffffffff) == 0, F_ZF);
1988 CONDITIONAL_SET_FLAG(PARITY(res & 0xff), F_PF);
1991 bc = (res & (~d | s)) | (~d & s);
1992 CONDITIONAL_SET_FLAG(bc & 0x80000000, F_CF);
1993 CONDITIONAL_SET_FLAG(XOR2(bc >> 30), F_OF);
1994 CONDITIONAL_SET_FLAG(bc & 0x8, F_AF);
2002 void test_byte(u8 d, u8 s)
2009 CONDITIONAL_SET_FLAG(res & 0x80, F_SF);
2010 CONDITIONAL_SET_FLAG(res == 0, F_ZF);
2011 CONDITIONAL_SET_FLAG(PARITY(res & 0xff), F_PF);
2020 void test_word(u16 d, u16 s)
2027 CONDITIONAL_SET_FLAG(res & 0x8000, F_SF);
2028 CONDITIONAL_SET_FLAG(res == 0, F_ZF);
2029 CONDITIONAL_SET_FLAG(PARITY(res & 0xff), F_PF);
2038 void test_long(u32 d, u32 s)
2045 CONDITIONAL_SET_FLAG(res & 0x80000000, F_SF);
2046 CONDITIONAL_SET_FLAG(res == 0, F_ZF);
2047 CONDITIONAL_SET_FLAG(PARITY(res & 0xff), F_PF);
2056 u8 xor_byte(u8 d, u8 s)
2062 CONDITIONAL_SET_FLAG(res & 0x80, F_SF);
2063 CONDITIONAL_SET_FLAG(res == 0, F_ZF);
2064 CONDITIONAL_SET_FLAG(PARITY(res), F_PF);
2074 u16 xor_word(u16 d, u16 s)
2080 CONDITIONAL_SET_FLAG(res & 0x8000, F_SF);
2081 CONDITIONAL_SET_FLAG(res == 0, F_ZF);
2082 CONDITIONAL_SET_FLAG(PARITY(res & 0xff), F_PF);
2092 u32 xor_long(u32 d, u32 s)
2098 CONDITIONAL_SET_FLAG(res & 0x80000000, F_SF);
2099 CONDITIONAL_SET_FLAG(res == 0, F_ZF);
2100 CONDITIONAL_SET_FLAG(PARITY(res & 0xff), F_PF);
2110 void imul_byte(u8 s)
2112 s16 res = (s16)((s8) M.x86.R_AL * (s8) s);
2115 if (((M.x86.R_AL & 0x80) == 0 && M.x86.R_AH == 0x00) ||
2116 ((M.x86.R_AL & 0x80) != 0 && M.x86.R_AH == 0xFF))
2132 void imul_word(u16 s)
2134 s32 res = (s16) M.x86.R_AX * (s16) s;
2136 M.x86.R_AX = (u16) res;
2137 M.x86.R_DX = (u16)(res >> 16);
2138 if (((M.x86.R_AX & 0x8000) == 0 && M.x86.R_DX == 0x00) ||
2139 ((M.x86.R_AX & 0x8000) != 0 && M.x86.R_DX == 0xFF))
2155 void imul_long_direct(u32 *res_lo, u32 *res_hi, u32 d, u32 s)
2157 #ifdef __HAS_LONG_LONG__ 2158 s64 res = (s32) d * (s32) s;
2160 *res_lo = (u32) res;
2161 *res_hi = (u32)(res >> 32);
2163 u32 d_lo, d_hi, d_sign;
2164 u32 s_lo, s_hi, s_sign;
2165 u32 rlo_lo, rlo_hi, rhi_lo;
2167 if ((d_sign = d & 0x80000000) != 0)
2171 if ((s_sign = s & 0x80000000) != 0)
2175 rlo_lo = d_lo * s_lo;
2176 rlo_hi = (d_hi * s_lo + d_lo * s_hi) + (rlo_lo >> 16);
2177 rhi_lo = d_hi * s_hi + (rlo_hi >> 16);
2178 *res_lo = (rlo_hi << 16) | (rlo_lo & 0xFFFF);
2180 if (d_sign != s_sign)
2183 s = (((d & 0xFFFF) + 1) >> 16) + (d >> 16);
2184 *res_lo = ~*res_lo + 1;
2185 *res_hi = ~*res_hi + (s >> 16);
2194 void imul_long(u32 s)
2196 imul_long_direct(&M.x86.R_EAX, &M.x86.R_EDX, M.x86.R_EAX, s);
2197 if (((M.x86.R_EAX & 0x80000000) == 0 && M.x86.R_EDX == 0x00) ||
2198 ((M.x86.R_EAX & 0x80000000) != 0 && M.x86.R_EDX == 0xFF))
2216 u16 res = (u16)(M.x86.R_AL * s);
2219 if (M.x86.R_AH == 0)
2235 void mul_word(u16 s)
2237 u32 res = M.x86.R_AX * s;
2239 M.x86.R_AX = (u16) res;
2240 M.x86.R_DX = (u16)(res >> 16);
2241 if (M.x86.R_DX == 0)
2257 void mul_long(u32 s)
2259 #ifdef __HAS_LONG_LONG__ 2260 u64 res = (u32) M.x86.R_EAX * (u32) s;
2262 M.x86.R_EAX = (u32) res;
2263 M.x86.R_EDX = (u32)(res >> 32);
2267 u32 rlo_lo, rlo_hi, rhi_lo;
2274 rlo_lo = a_lo * s_lo;
2275 rlo_hi = (a_hi * s_lo + a_lo * s_hi) + (rlo_lo >> 16);
2276 rhi_lo = a_hi * s_hi + (rlo_hi >> 16);
2277 M.x86.R_EAX = (rlo_hi << 16) | (rlo_lo & 0xFFFF);
2278 M.x86.R_EDX = rhi_lo;
2281 if (M.x86.R_EDX == 0)
2297 void idiv_byte(u8 s)
2301 dvd = (s16) M.x86.R_AX;
2304 x86emu_intr_raise(0);
2310 if (__builtin_abs(div) > 0x7f)
2312 x86emu_intr_raise(0);
2315 M.x86.R_AL = (s8) div;
2316 M.x86.R_AH = (s8) mod;
2323 void idiv_word(u16 s)
2327 dvd = (((s32) M.x86.R_DX) << 16) | M.x86.R_AX;
2330 x86emu_intr_raise(0);
2333 div = dvd / (s16) s;
2334 mod = dvd % (s16) s;
2336 if (__builtin_abs(div) > 0x7fff)
2338 x86emu_intr_raise(0);
2343 CONDITIONAL_SET_FLAG(div == 0, F_ZF);
2344 CONDITIONAL_SET_FLAG(PARITY(mod & 0xff), F_PF);
2346 M.x86.R_AX = (u16) div;
2347 M.x86.R_DX = (u16) mod;
2354 void idiv_long(u32 s)
2356 #ifdef __HAS_LONG_LONG__ 2359 dvd = (((s64) M.x86.R_EDX) << 32) | M.x86.R_EAX;
2362 x86emu_intr_raise(0);
2365 div = dvd / (s32) s;
2366 mod = dvd % (s32) s;
2368 if (__builtin_abs(div) > 0x7fffffff)
2370 x86emu_intr_raise(0);
2375 s32 h_dvd = M.x86.R_EDX;
2376 u32 l_dvd = M.x86.R_EAX;
2377 u32 abs_s = s & 0x7FFFFFFF;
2378 u32 abs_h_dvd = h_dvd & 0x7FFFFFFF;
2379 u32 h_s = abs_s >> 1;
2380 u32 l_s = abs_s << 31;
2386 x86emu_intr_raise(0);
2392 carry = (l_dvd >= l_s) ? 0 : 1;
2394 if (abs_h_dvd < (h_s + carry))
2397 l_s = abs_s << (--counter);
2402 abs_h_dvd -= (h_s + carry);
2403 l_dvd = carry ? ((0xFFFFFFFF - l_s) + l_dvd + 1) : (l_dvd - l_s);
2405 l_s = abs_s << (--counter);
2410 }
while (counter > -1);
2412 if (abs_h_dvd || (l_dvd > abs_s))
2414 x86emu_intr_raise(0);
2418 div |= ((h_dvd & 0x10000000) ^ (s & 0x10000000));
2426 CONDITIONAL_SET_FLAG(PARITY(mod & 0xff), F_PF);
2428 M.x86.R_EAX = (u32) div;
2429 M.x86.R_EDX = (u32) mod;
2443 x86emu_intr_raise(0);
2449 if (__builtin_abs(div) > 0xff)
2451 x86emu_intr_raise(0);
2454 M.x86.R_AL = (u8) div;
2455 M.x86.R_AH = (u8) mod;
2462 void div_word(u16 s)
2466 dvd = (((u32) M.x86.R_DX) << 16) | M.x86.R_AX;
2469 x86emu_intr_raise(0);
2472 div = dvd / (u16) s;
2473 mod = dvd % (u16) s;
2475 if (__builtin_abs(div) > 0xffff)
2477 x86emu_intr_raise(0);
2482 CONDITIONAL_SET_FLAG(div == 0, F_ZF);
2483 CONDITIONAL_SET_FLAG(PARITY(mod & 0xff), F_PF);
2485 M.x86.R_AX = (u16) div;
2486 M.x86.R_DX = (u16) mod;
2493 void div_long(u32 s)
2495 #ifdef __HAS_LONG_LONG__ 2498 dvd = (((u64) M.x86.R_EDX) << 32) | M.x86.R_EAX;
2501 x86emu_intr_raise(0);
2504 div = dvd / (u32) s;
2505 mod = dvd % (u32) s;
2507 if ((
unsigned int) __builtin_abs(div) > 0xffffffff)
2509 x86emu_intr_raise(0);
2514 s32 h_dvd = M.x86.R_EDX;
2515 u32 l_dvd = M.x86.R_EAX;
2524 x86emu_intr_raise(0);
2530 carry = (l_dvd >= l_s) ? 0 : 1;
2532 if (h_dvd < (h_s + carry))
2535 l_s = s << (--counter);
2540 h_dvd -= (h_s + carry);
2541 l_dvd = carry ? ((0xFFFFFFFF - l_s) + l_dvd + 1) : (l_dvd - l_s);
2543 l_s = s << (--counter);
2548 }
while (counter > -1);
2550 if (h_dvd || (l_dvd > s))
2552 x86emu_intr_raise(0);
2561 CONDITIONAL_SET_FLAG(PARITY(mod & 0xff), F_PF);
2563 M.x86.R_EAX = (u32) div;
2564 M.x86.R_EDX = (u32) mod;
2575 if (ACCESS_FLAG(F_DF))
2579 if (M.x86.mode & (SYSMODE_PREFIX_REPE | SYSMODE_PREFIX_REPNE))
2584 ((M.x86.mode & SYSMODE_PREFIX_DATA) ? M.x86.R_ECX : M.x86.R_CX);
2590 store_data_byte_abs(
2591 M.x86.R_ES, M.x86.R_DI, (*sys_inb)(M.x86.R_DX));
2599 store_data_word_abs(
2600 M.x86.R_ES, M.x86.R_DI, (*sys_inw)(M.x86.R_DX));
2607 store_data_long_abs(
2608 M.x86.R_ES, M.x86.R_DI, (*sys_inl)(M.x86.R_DX));
2614 if (M.x86.mode & SYSMODE_PREFIX_DATA)
2618 M.x86.mode &= ~(SYSMODE_PREFIX_REPE | SYSMODE_PREFIX_REPNE);
2625 store_data_byte_abs(
2626 M.x86.R_ES, M.x86.R_DI, (*sys_inb)(M.x86.R_DX));
2629 store_data_word_abs(
2630 M.x86.R_ES, M.x86.R_DI, (*sys_inw)(M.x86.R_DX));
2633 store_data_long_abs(
2634 M.x86.R_ES, M.x86.R_DI, (*sys_inl)(M.x86.R_DX));
2649 if (ACCESS_FLAG(F_DF))
2653 if (M.x86.mode & (SYSMODE_PREFIX_REPE | SYSMODE_PREFIX_REPNE))
2658 ((M.x86.mode & SYSMODE_PREFIX_DATA) ? M.x86.R_ECX : M.x86.R_CX);
2666 fetch_data_byte_abs(M.x86.R_ES, M.x86.R_SI));
2676 fetch_data_word_abs(M.x86.R_ES, M.x86.R_SI));
2685 fetch_data_long_abs(M.x86.R_ES, M.x86.R_SI));
2691 if (M.x86.mode & SYSMODE_PREFIX_DATA)
2695 M.x86.mode &= ~(SYSMODE_PREFIX_REPE | SYSMODE_PREFIX_REPNE);
2703 M.x86.R_DX, fetch_data_byte_abs(M.x86.R_ES, M.x86.R_SI));
2707 M.x86.R_DX, fetch_data_word_abs(M.x86.R_ES, M.x86.R_SI));
2711 M.x86.R_DX, fetch_data_long_abs(M.x86.R_ES, M.x86.R_SI));
2725 u16 mem_access_word(
int addr)
2727 DB(
if (CHECK_MEM_ACCESS()) x86emu_check_mem_access(addr);)
2728 return (*sys_rdw)(addr);
2737 void push_word(u16 w)
2739 DB(
if (CHECK_SP_ACCESS()) x86emu_check_sp_access();)
2741 (*sys_wrw)(((u32) M.x86.R_SS << 4) + M.x86.R_SP, w);
2750 void push_long(u32 w)
2752 DB(
if (CHECK_SP_ACCESS()) x86emu_check_sp_access();)
2754 (*sys_wrl)(((u32) M.x86.R_SS << 4) + M.x86.R_SP, w);
2767 DB(
if (CHECK_SP_ACCESS()) x86emu_check_sp_access();)
2768 res = (*sys_rdw)(((u32) M.x86.R_SS << 4) + M.x86.R_SP);
2783 DB(
if (CHECK_SP_ACCESS()) x86emu_check_sp_access();)
2784 res = (*sys_rdl)(((u32) M.x86.R_SS << 4) + M.x86.R_SP);