1*4882a593Smuzhiyun /****************************************************************************
2*4882a593Smuzhiyun *
3*4882a593Smuzhiyun * Realmode X86 Emulator Library
4*4882a593Smuzhiyun *
5*4882a593Smuzhiyun * Copyright (C) 1996-1999 SciTech Software, Inc.
6*4882a593Smuzhiyun * Copyright (C) David Mosberger-Tang
7*4882a593Smuzhiyun * Copyright (C) 1999 Egbert Eich
8*4882a593Smuzhiyun *
9*4882a593Smuzhiyun * ========================================================================
10*4882a593Smuzhiyun *
11*4882a593Smuzhiyun * Permission to use, copy, modify, distribute, and sell this software and
12*4882a593Smuzhiyun * its documentation for any purpose is hereby granted without fee,
13*4882a593Smuzhiyun * provided that the above copyright notice appear in all copies and that
14*4882a593Smuzhiyun * both that copyright notice and this permission notice appear in
15*4882a593Smuzhiyun * supporting documentation, and that the name of the authors not be used
16*4882a593Smuzhiyun * in advertising or publicity pertaining to distribution of the software
17*4882a593Smuzhiyun * without specific, written prior permission. The authors makes no
18*4882a593Smuzhiyun * representations about the suitability of this software for any purpose.
19*4882a593Smuzhiyun * It is provided "as is" without express or implied warranty.
20*4882a593Smuzhiyun *
21*4882a593Smuzhiyun * THE AUTHORS DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
22*4882a593Smuzhiyun * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
23*4882a593Smuzhiyun * EVENT SHALL THE AUTHORS BE LIABLE FOR ANY SPECIAL, INDIRECT OR
24*4882a593Smuzhiyun * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF
25*4882a593Smuzhiyun * USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
26*4882a593Smuzhiyun * OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
27*4882a593Smuzhiyun * PERFORMANCE OF THIS SOFTWARE.
28*4882a593Smuzhiyun *
29*4882a593Smuzhiyun * ========================================================================
30*4882a593Smuzhiyun *
31*4882a593Smuzhiyun * Language: ANSI C
32*4882a593Smuzhiyun * Environment: Any
33*4882a593Smuzhiyun * Developer: Kendall Bennett
34*4882a593Smuzhiyun *
35*4882a593Smuzhiyun * Description: This file contains the code to implement the primitive
36*4882a593Smuzhiyun * machine operations used by the emulation code in ops.c
37*4882a593Smuzhiyun *
38*4882a593Smuzhiyun * Carry Chain Calculation
39*4882a593Smuzhiyun *
40*4882a593Smuzhiyun * This represents a somewhat expensive calculation which is
41*4882a593Smuzhiyun * apparently required to emulate the setting of the OF and AF flag.
42*4882a593Smuzhiyun * The latter is not so important, but the former is. The overflow
43*4882a593Smuzhiyun * flag is the XOR of the top two bits of the carry chain for an
44*4882a593Smuzhiyun * addition (similar for subtraction). Since we do not want to
45*4882a593Smuzhiyun * simulate the addition in a bitwise manner, we try to calculate the
46*4882a593Smuzhiyun * carry chain given the two operands and the result.
47*4882a593Smuzhiyun *
48*4882a593Smuzhiyun * So, given the following table, which represents the addition of two
49*4882a593Smuzhiyun * bits, we can derive a formula for the carry chain.
50*4882a593Smuzhiyun *
51*4882a593Smuzhiyun * a b cin r cout
52*4882a593Smuzhiyun * 0 0 0 0 0
53*4882a593Smuzhiyun * 0 0 1 1 0
54*4882a593Smuzhiyun * 0 1 0 1 0
55*4882a593Smuzhiyun * 0 1 1 0 1
56*4882a593Smuzhiyun * 1 0 0 1 0
57*4882a593Smuzhiyun * 1 0 1 0 1
58*4882a593Smuzhiyun * 1 1 0 0 1
59*4882a593Smuzhiyun * 1 1 1 1 1
60*4882a593Smuzhiyun *
61*4882a593Smuzhiyun * Construction of table for cout:
62*4882a593Smuzhiyun *
63*4882a593Smuzhiyun * ab
64*4882a593Smuzhiyun * r \ 00 01 11 10
65*4882a593Smuzhiyun * |------------------
66*4882a593Smuzhiyun * 0 | 0 1 1 1
67*4882a593Smuzhiyun * 1 | 0 0 1 0
68*4882a593Smuzhiyun *
69*4882a593Smuzhiyun * By inspection, one gets: cc = ab + r'(a + b)
70*4882a593Smuzhiyun *
71*4882a593Smuzhiyun * That represents alot of operations, but NO CHOICE....
72*4882a593Smuzhiyun *
73*4882a593Smuzhiyun * Borrow Chain Calculation.
74*4882a593Smuzhiyun *
75*4882a593Smuzhiyun * The following table represents the subtraction of two bits, from
76*4882a593Smuzhiyun * which we can derive a formula for the borrow chain.
77*4882a593Smuzhiyun *
78*4882a593Smuzhiyun * a b bin r bout
79*4882a593Smuzhiyun * 0 0 0 0 0
80*4882a593Smuzhiyun * 0 0 1 1 1
81*4882a593Smuzhiyun * 0 1 0 1 1
82*4882a593Smuzhiyun * 0 1 1 0 1
83*4882a593Smuzhiyun * 1 0 0 1 0
84*4882a593Smuzhiyun * 1 0 1 0 0
85*4882a593Smuzhiyun * 1 1 0 0 0
86*4882a593Smuzhiyun * 1 1 1 1 1
87*4882a593Smuzhiyun *
88*4882a593Smuzhiyun * Construction of table for cout:
89*4882a593Smuzhiyun *
90*4882a593Smuzhiyun * ab
91*4882a593Smuzhiyun * r \ 00 01 11 10
92*4882a593Smuzhiyun * |------------------
93*4882a593Smuzhiyun * 0 | 0 1 0 0
94*4882a593Smuzhiyun * 1 | 1 1 1 0
95*4882a593Smuzhiyun *
96*4882a593Smuzhiyun * By inspection, one gets: bc = a'b + r(a' + b)
97*4882a593Smuzhiyun *
98*4882a593Smuzhiyun ****************************************************************************/
99*4882a593Smuzhiyun
100*4882a593Smuzhiyun #include <stdlib.h>
101*4882a593Smuzhiyun
102*4882a593Smuzhiyun #define PRIM_OPS_NO_REDEFINE_ASM
103*4882a593Smuzhiyun #include "x86emu/x86emui.h"
104*4882a593Smuzhiyun
105*4882a593Smuzhiyun #if defined(__GNUC__)
106*4882a593Smuzhiyun #if defined (__i386__) || defined(__i386) || defined(__AMD64__) || defined(__amd64__)
107*4882a593Smuzhiyun #include "x86emu/prim_x86_gcc.h"
108*4882a593Smuzhiyun #endif
109*4882a593Smuzhiyun #endif
110*4882a593Smuzhiyun
111*4882a593Smuzhiyun /*------------------------- Global Variables ------------------------------*/
112*4882a593Smuzhiyun
113*4882a593Smuzhiyun static u32 x86emu_parity_tab[8] = {
114*4882a593Smuzhiyun 0x96696996,
115*4882a593Smuzhiyun 0x69969669,
116*4882a593Smuzhiyun 0x69969669,
117*4882a593Smuzhiyun 0x96696996,
118*4882a593Smuzhiyun 0x69969669,
119*4882a593Smuzhiyun 0x96696996,
120*4882a593Smuzhiyun 0x96696996,
121*4882a593Smuzhiyun 0x69969669,
122*4882a593Smuzhiyun };
123*4882a593Smuzhiyun
124*4882a593Smuzhiyun #define PARITY(x) (((x86emu_parity_tab[(x) / 32] >> ((x) % 32)) & 1) == 0)
125*4882a593Smuzhiyun #define XOR2(x) (((x) ^ ((x)>>1)) & 0x1)
126*4882a593Smuzhiyun
127*4882a593Smuzhiyun /*----------------------------- Implementation ----------------------------*/
128*4882a593Smuzhiyun
129*4882a593Smuzhiyun /****************************************************************************
130*4882a593Smuzhiyun REMARKS:
131*4882a593Smuzhiyun Implements the AAA instruction and side effects.
132*4882a593Smuzhiyun ****************************************************************************/
133*4882a593Smuzhiyun u16
aaa_word(u16 d)134*4882a593Smuzhiyun aaa_word(u16 d)
135*4882a593Smuzhiyun {
136*4882a593Smuzhiyun u16 res;
137*4882a593Smuzhiyun
138*4882a593Smuzhiyun if ((d & 0xf) > 0x9 || ACCESS_FLAG(F_AF)) {
139*4882a593Smuzhiyun d += 0x6;
140*4882a593Smuzhiyun d += 0x100;
141*4882a593Smuzhiyun SET_FLAG(F_AF);
142*4882a593Smuzhiyun SET_FLAG(F_CF);
143*4882a593Smuzhiyun }
144*4882a593Smuzhiyun else {
145*4882a593Smuzhiyun CLEAR_FLAG(F_CF);
146*4882a593Smuzhiyun CLEAR_FLAG(F_AF);
147*4882a593Smuzhiyun }
148*4882a593Smuzhiyun res = (u16) (d & 0xFF0F);
149*4882a593Smuzhiyun CLEAR_FLAG(F_SF);
150*4882a593Smuzhiyun CONDITIONAL_SET_FLAG(res == 0, F_ZF);
151*4882a593Smuzhiyun CONDITIONAL_SET_FLAG(PARITY(res & 0xff), F_PF);
152*4882a593Smuzhiyun return res;
153*4882a593Smuzhiyun }
154*4882a593Smuzhiyun
155*4882a593Smuzhiyun /****************************************************************************
156*4882a593Smuzhiyun REMARKS:
157*4882a593Smuzhiyun Implements the AAA instruction and side effects.
158*4882a593Smuzhiyun ****************************************************************************/
159*4882a593Smuzhiyun u16
aas_word(u16 d)160*4882a593Smuzhiyun aas_word(u16 d)
161*4882a593Smuzhiyun {
162*4882a593Smuzhiyun u16 res;
163*4882a593Smuzhiyun
164*4882a593Smuzhiyun if ((d & 0xf) > 0x9 || ACCESS_FLAG(F_AF)) {
165*4882a593Smuzhiyun d -= 0x6;
166*4882a593Smuzhiyun d -= 0x100;
167*4882a593Smuzhiyun SET_FLAG(F_AF);
168*4882a593Smuzhiyun SET_FLAG(F_CF);
169*4882a593Smuzhiyun }
170*4882a593Smuzhiyun else {
171*4882a593Smuzhiyun CLEAR_FLAG(F_CF);
172*4882a593Smuzhiyun CLEAR_FLAG(F_AF);
173*4882a593Smuzhiyun }
174*4882a593Smuzhiyun res = (u16) (d & 0xFF0F);
175*4882a593Smuzhiyun CLEAR_FLAG(F_SF);
176*4882a593Smuzhiyun CONDITIONAL_SET_FLAG(res == 0, F_ZF);
177*4882a593Smuzhiyun CONDITIONAL_SET_FLAG(PARITY(res & 0xff), F_PF);
178*4882a593Smuzhiyun return res;
179*4882a593Smuzhiyun }
180*4882a593Smuzhiyun
181*4882a593Smuzhiyun /****************************************************************************
182*4882a593Smuzhiyun REMARKS:
183*4882a593Smuzhiyun Implements the AAD instruction and side effects.
184*4882a593Smuzhiyun ****************************************************************************/
185*4882a593Smuzhiyun u16
aad_word(u16 d)186*4882a593Smuzhiyun aad_word(u16 d)
187*4882a593Smuzhiyun {
188*4882a593Smuzhiyun u16 l;
189*4882a593Smuzhiyun u8 hb, lb;
190*4882a593Smuzhiyun
191*4882a593Smuzhiyun hb = (u8) ((d >> 8) & 0xff);
192*4882a593Smuzhiyun lb = (u8) ((d & 0xff));
193*4882a593Smuzhiyun l = (u16) ((lb + 10 * hb) & 0xFF);
194*4882a593Smuzhiyun
195*4882a593Smuzhiyun CLEAR_FLAG(F_CF);
196*4882a593Smuzhiyun CLEAR_FLAG(F_AF);
197*4882a593Smuzhiyun CLEAR_FLAG(F_OF);
198*4882a593Smuzhiyun CONDITIONAL_SET_FLAG(l & 0x80, F_SF);
199*4882a593Smuzhiyun CONDITIONAL_SET_FLAG(l == 0, F_ZF);
200*4882a593Smuzhiyun CONDITIONAL_SET_FLAG(PARITY(l & 0xff), F_PF);
201*4882a593Smuzhiyun return l;
202*4882a593Smuzhiyun }
203*4882a593Smuzhiyun
204*4882a593Smuzhiyun /****************************************************************************
205*4882a593Smuzhiyun REMARKS:
206*4882a593Smuzhiyun Implements the AAM instruction and side effects.
207*4882a593Smuzhiyun ****************************************************************************/
208*4882a593Smuzhiyun u16
aam_word(u8 d)209*4882a593Smuzhiyun aam_word(u8 d)
210*4882a593Smuzhiyun {
211*4882a593Smuzhiyun u16 h, l;
212*4882a593Smuzhiyun
213*4882a593Smuzhiyun h = (u16) (d / 10);
214*4882a593Smuzhiyun l = (u16) (d % 10);
215*4882a593Smuzhiyun l |= (u16) (h << 8);
216*4882a593Smuzhiyun
217*4882a593Smuzhiyun CLEAR_FLAG(F_CF);
218*4882a593Smuzhiyun CLEAR_FLAG(F_AF);
219*4882a593Smuzhiyun CLEAR_FLAG(F_OF);
220*4882a593Smuzhiyun CONDITIONAL_SET_FLAG(l & 0x80, F_SF);
221*4882a593Smuzhiyun CONDITIONAL_SET_FLAG(l == 0, F_ZF);
222*4882a593Smuzhiyun CONDITIONAL_SET_FLAG(PARITY(l & 0xff), F_PF);
223*4882a593Smuzhiyun return l;
224*4882a593Smuzhiyun }
225*4882a593Smuzhiyun
226*4882a593Smuzhiyun /****************************************************************************
227*4882a593Smuzhiyun REMARKS:
228*4882a593Smuzhiyun Implements the ADC instruction and side effects.
229*4882a593Smuzhiyun ****************************************************************************/
230*4882a593Smuzhiyun u8
adc_byte(u8 d,u8 s)231*4882a593Smuzhiyun adc_byte(u8 d, u8 s)
232*4882a593Smuzhiyun {
233*4882a593Smuzhiyun register u32 res; /* all operands in native machine order */
234*4882a593Smuzhiyun register u32 cc;
235*4882a593Smuzhiyun
236*4882a593Smuzhiyun if (ACCESS_FLAG(F_CF))
237*4882a593Smuzhiyun res = 1 + d + s;
238*4882a593Smuzhiyun else
239*4882a593Smuzhiyun res = d + s;
240*4882a593Smuzhiyun
241*4882a593Smuzhiyun CONDITIONAL_SET_FLAG(res & 0x100, F_CF);
242*4882a593Smuzhiyun CONDITIONAL_SET_FLAG((res & 0xff) == 0, F_ZF);
243*4882a593Smuzhiyun CONDITIONAL_SET_FLAG(res & 0x80, F_SF);
244*4882a593Smuzhiyun CONDITIONAL_SET_FLAG(PARITY(res & 0xff), F_PF);
245*4882a593Smuzhiyun
246*4882a593Smuzhiyun /* calculate the carry chain SEE NOTE AT TOP. */
247*4882a593Smuzhiyun cc = (s & d) | ((~res) & (s | d));
248*4882a593Smuzhiyun CONDITIONAL_SET_FLAG(XOR2(cc >> 6), F_OF);
249*4882a593Smuzhiyun CONDITIONAL_SET_FLAG(cc & 0x8, F_AF);
250*4882a593Smuzhiyun return (u8) res;
251*4882a593Smuzhiyun }
252*4882a593Smuzhiyun
253*4882a593Smuzhiyun /****************************************************************************
254*4882a593Smuzhiyun REMARKS:
255*4882a593Smuzhiyun Implements the ADC instruction and side effects.
256*4882a593Smuzhiyun ****************************************************************************/
257*4882a593Smuzhiyun u16
adc_word(u16 d,u16 s)258*4882a593Smuzhiyun adc_word(u16 d, u16 s)
259*4882a593Smuzhiyun {
260*4882a593Smuzhiyun register u32 res; /* all operands in native machine order */
261*4882a593Smuzhiyun register u32 cc;
262*4882a593Smuzhiyun
263*4882a593Smuzhiyun if (ACCESS_FLAG(F_CF))
264*4882a593Smuzhiyun res = 1 + d + s;
265*4882a593Smuzhiyun else
266*4882a593Smuzhiyun res = d + s;
267*4882a593Smuzhiyun
268*4882a593Smuzhiyun CONDITIONAL_SET_FLAG(res & 0x10000, F_CF);
269*4882a593Smuzhiyun CONDITIONAL_SET_FLAG((res & 0xffff) == 0, F_ZF);
270*4882a593Smuzhiyun CONDITIONAL_SET_FLAG(res & 0x8000, F_SF);
271*4882a593Smuzhiyun CONDITIONAL_SET_FLAG(PARITY(res & 0xff), F_PF);
272*4882a593Smuzhiyun
273*4882a593Smuzhiyun /* calculate the carry chain SEE NOTE AT TOP. */
274*4882a593Smuzhiyun cc = (s & d) | ((~res) & (s | d));
275*4882a593Smuzhiyun CONDITIONAL_SET_FLAG(XOR2(cc >> 14), F_OF);
276*4882a593Smuzhiyun CONDITIONAL_SET_FLAG(cc & 0x8, F_AF);
277*4882a593Smuzhiyun return (u16) res;
278*4882a593Smuzhiyun }
279*4882a593Smuzhiyun
280*4882a593Smuzhiyun /****************************************************************************
281*4882a593Smuzhiyun REMARKS:
282*4882a593Smuzhiyun Implements the ADC instruction and side effects.
283*4882a593Smuzhiyun ****************************************************************************/
284*4882a593Smuzhiyun u32
adc_long(u32 d,u32 s)285*4882a593Smuzhiyun adc_long(u32 d, u32 s)
286*4882a593Smuzhiyun {
287*4882a593Smuzhiyun register u32 lo; /* all operands in native machine order */
288*4882a593Smuzhiyun register u32 hi;
289*4882a593Smuzhiyun register u32 res;
290*4882a593Smuzhiyun register u32 cc;
291*4882a593Smuzhiyun
292*4882a593Smuzhiyun if (ACCESS_FLAG(F_CF)) {
293*4882a593Smuzhiyun lo = 1 + (d & 0xFFFF) + (s & 0xFFFF);
294*4882a593Smuzhiyun res = 1 + d + s;
295*4882a593Smuzhiyun }
296*4882a593Smuzhiyun else {
297*4882a593Smuzhiyun lo = (d & 0xFFFF) + (s & 0xFFFF);
298*4882a593Smuzhiyun res = d + s;
299*4882a593Smuzhiyun }
300*4882a593Smuzhiyun hi = (lo >> 16) + (d >> 16) + (s >> 16);
301*4882a593Smuzhiyun
302*4882a593Smuzhiyun CONDITIONAL_SET_FLAG(hi & 0x10000, F_CF);
303*4882a593Smuzhiyun CONDITIONAL_SET_FLAG((res & 0xffffffff) == 0, F_ZF);
304*4882a593Smuzhiyun CONDITIONAL_SET_FLAG(res & 0x80000000, F_SF);
305*4882a593Smuzhiyun CONDITIONAL_SET_FLAG(PARITY(res & 0xff), F_PF);
306*4882a593Smuzhiyun
307*4882a593Smuzhiyun /* calculate the carry chain SEE NOTE AT TOP. */
308*4882a593Smuzhiyun cc = (s & d) | ((~res) & (s | d));
309*4882a593Smuzhiyun CONDITIONAL_SET_FLAG(XOR2(cc >> 30), F_OF);
310*4882a593Smuzhiyun CONDITIONAL_SET_FLAG(cc & 0x8, F_AF);
311*4882a593Smuzhiyun return res;
312*4882a593Smuzhiyun }
313*4882a593Smuzhiyun
314*4882a593Smuzhiyun /****************************************************************************
315*4882a593Smuzhiyun REMARKS:
316*4882a593Smuzhiyun Implements the ADD instruction and side effects.
317*4882a593Smuzhiyun ****************************************************************************/
318*4882a593Smuzhiyun u8
add_byte(u8 d,u8 s)319*4882a593Smuzhiyun add_byte(u8 d, u8 s)
320*4882a593Smuzhiyun {
321*4882a593Smuzhiyun register u32 res; /* all operands in native machine order */
322*4882a593Smuzhiyun register u32 cc;
323*4882a593Smuzhiyun
324*4882a593Smuzhiyun res = d + s;
325*4882a593Smuzhiyun CONDITIONAL_SET_FLAG(res & 0x100, F_CF);
326*4882a593Smuzhiyun CONDITIONAL_SET_FLAG((res & 0xff) == 0, F_ZF);
327*4882a593Smuzhiyun CONDITIONAL_SET_FLAG(res & 0x80, F_SF);
328*4882a593Smuzhiyun CONDITIONAL_SET_FLAG(PARITY(res & 0xff), F_PF);
329*4882a593Smuzhiyun
330*4882a593Smuzhiyun /* calculate the carry chain SEE NOTE AT TOP. */
331*4882a593Smuzhiyun cc = (s & d) | ((~res) & (s | d));
332*4882a593Smuzhiyun CONDITIONAL_SET_FLAG(XOR2(cc >> 6), F_OF);
333*4882a593Smuzhiyun CONDITIONAL_SET_FLAG(cc & 0x8, F_AF);
334*4882a593Smuzhiyun return (u8) res;
335*4882a593Smuzhiyun }
336*4882a593Smuzhiyun
337*4882a593Smuzhiyun /****************************************************************************
338*4882a593Smuzhiyun REMARKS:
339*4882a593Smuzhiyun Implements the ADD instruction and side effects.
340*4882a593Smuzhiyun ****************************************************************************/
341*4882a593Smuzhiyun u16
add_word(u16 d,u16 s)342*4882a593Smuzhiyun add_word(u16 d, u16 s)
343*4882a593Smuzhiyun {
344*4882a593Smuzhiyun register u32 res; /* all operands in native machine order */
345*4882a593Smuzhiyun register u32 cc;
346*4882a593Smuzhiyun
347*4882a593Smuzhiyun res = d + s;
348*4882a593Smuzhiyun CONDITIONAL_SET_FLAG(res & 0x10000, F_CF);
349*4882a593Smuzhiyun CONDITIONAL_SET_FLAG((res & 0xffff) == 0, F_ZF);
350*4882a593Smuzhiyun CONDITIONAL_SET_FLAG(res & 0x8000, F_SF);
351*4882a593Smuzhiyun CONDITIONAL_SET_FLAG(PARITY(res & 0xff), F_PF);
352*4882a593Smuzhiyun
353*4882a593Smuzhiyun /* calculate the carry chain SEE NOTE AT TOP. */
354*4882a593Smuzhiyun cc = (s & d) | ((~res) & (s | d));
355*4882a593Smuzhiyun CONDITIONAL_SET_FLAG(XOR2(cc >> 14), F_OF);
356*4882a593Smuzhiyun CONDITIONAL_SET_FLAG(cc & 0x8, F_AF);
357*4882a593Smuzhiyun return (u16) res;
358*4882a593Smuzhiyun }
359*4882a593Smuzhiyun
360*4882a593Smuzhiyun /****************************************************************************
361*4882a593Smuzhiyun REMARKS:
362*4882a593Smuzhiyun Implements the ADD instruction and side effects.
363*4882a593Smuzhiyun ****************************************************************************/
364*4882a593Smuzhiyun u32
add_long(u32 d,u32 s)365*4882a593Smuzhiyun add_long(u32 d, u32 s)
366*4882a593Smuzhiyun {
367*4882a593Smuzhiyun register u32 lo; /* all operands in native machine order */
368*4882a593Smuzhiyun register u32 hi;
369*4882a593Smuzhiyun register u32 res;
370*4882a593Smuzhiyun register u32 cc;
371*4882a593Smuzhiyun
372*4882a593Smuzhiyun lo = (d & 0xFFFF) + (s & 0xFFFF);
373*4882a593Smuzhiyun res = d + s;
374*4882a593Smuzhiyun hi = (lo >> 16) + (d >> 16) + (s >> 16);
375*4882a593Smuzhiyun
376*4882a593Smuzhiyun CONDITIONAL_SET_FLAG(hi & 0x10000, F_CF);
377*4882a593Smuzhiyun CONDITIONAL_SET_FLAG((res & 0xffffffff) == 0, F_ZF);
378*4882a593Smuzhiyun CONDITIONAL_SET_FLAG(res & 0x80000000, F_SF);
379*4882a593Smuzhiyun CONDITIONAL_SET_FLAG(PARITY(res & 0xff), F_PF);
380*4882a593Smuzhiyun
381*4882a593Smuzhiyun /* calculate the carry chain SEE NOTE AT TOP. */
382*4882a593Smuzhiyun cc = (s & d) | ((~res) & (s | d));
383*4882a593Smuzhiyun CONDITIONAL_SET_FLAG(XOR2(cc >> 30), F_OF);
384*4882a593Smuzhiyun CONDITIONAL_SET_FLAG(cc & 0x8, F_AF);
385*4882a593Smuzhiyun
386*4882a593Smuzhiyun return res;
387*4882a593Smuzhiyun }
388*4882a593Smuzhiyun
389*4882a593Smuzhiyun /****************************************************************************
390*4882a593Smuzhiyun REMARKS:
391*4882a593Smuzhiyun Implements the AND instruction and side effects.
392*4882a593Smuzhiyun ****************************************************************************/
393*4882a593Smuzhiyun u8
and_byte(u8 d,u8 s)394*4882a593Smuzhiyun and_byte(u8 d, u8 s)
395*4882a593Smuzhiyun {
396*4882a593Smuzhiyun register u8 res; /* all operands in native machine order */
397*4882a593Smuzhiyun
398*4882a593Smuzhiyun res = d & s;
399*4882a593Smuzhiyun
400*4882a593Smuzhiyun /* set the flags */
401*4882a593Smuzhiyun CLEAR_FLAG(F_OF);
402*4882a593Smuzhiyun CLEAR_FLAG(F_CF);
403*4882a593Smuzhiyun CLEAR_FLAG(F_AF);
404*4882a593Smuzhiyun CONDITIONAL_SET_FLAG(res & 0x80, F_SF);
405*4882a593Smuzhiyun CONDITIONAL_SET_FLAG(res == 0, F_ZF);
406*4882a593Smuzhiyun CONDITIONAL_SET_FLAG(PARITY(res), F_PF);
407*4882a593Smuzhiyun return res;
408*4882a593Smuzhiyun }
409*4882a593Smuzhiyun
410*4882a593Smuzhiyun /****************************************************************************
411*4882a593Smuzhiyun REMARKS:
412*4882a593Smuzhiyun Implements the AND instruction and side effects.
413*4882a593Smuzhiyun ****************************************************************************/
414*4882a593Smuzhiyun u16
and_word(u16 d,u16 s)415*4882a593Smuzhiyun and_word(u16 d, u16 s)
416*4882a593Smuzhiyun {
417*4882a593Smuzhiyun register u16 res; /* all operands in native machine order */
418*4882a593Smuzhiyun
419*4882a593Smuzhiyun res = d & s;
420*4882a593Smuzhiyun
421*4882a593Smuzhiyun /* set the flags */
422*4882a593Smuzhiyun CLEAR_FLAG(F_OF);
423*4882a593Smuzhiyun CLEAR_FLAG(F_CF);
424*4882a593Smuzhiyun CLEAR_FLAG(F_AF);
425*4882a593Smuzhiyun CONDITIONAL_SET_FLAG(res & 0x8000, F_SF);
426*4882a593Smuzhiyun CONDITIONAL_SET_FLAG(res == 0, F_ZF);
427*4882a593Smuzhiyun CONDITIONAL_SET_FLAG(PARITY(res & 0xff), F_PF);
428*4882a593Smuzhiyun return res;
429*4882a593Smuzhiyun }
430*4882a593Smuzhiyun
431*4882a593Smuzhiyun /****************************************************************************
432*4882a593Smuzhiyun REMARKS:
433*4882a593Smuzhiyun Implements the AND instruction and side effects.
434*4882a593Smuzhiyun ****************************************************************************/
435*4882a593Smuzhiyun u32
and_long(u32 d,u32 s)436*4882a593Smuzhiyun and_long(u32 d, u32 s)
437*4882a593Smuzhiyun {
438*4882a593Smuzhiyun register u32 res; /* all operands in native machine order */
439*4882a593Smuzhiyun
440*4882a593Smuzhiyun res = d & s;
441*4882a593Smuzhiyun
442*4882a593Smuzhiyun /* set the flags */
443*4882a593Smuzhiyun CLEAR_FLAG(F_OF);
444*4882a593Smuzhiyun CLEAR_FLAG(F_CF);
445*4882a593Smuzhiyun CLEAR_FLAG(F_AF);
446*4882a593Smuzhiyun CONDITIONAL_SET_FLAG(res & 0x80000000, F_SF);
447*4882a593Smuzhiyun CONDITIONAL_SET_FLAG(res == 0, F_ZF);
448*4882a593Smuzhiyun CONDITIONAL_SET_FLAG(PARITY(res & 0xff), F_PF);
449*4882a593Smuzhiyun return res;
450*4882a593Smuzhiyun }
451*4882a593Smuzhiyun
452*4882a593Smuzhiyun /****************************************************************************
453*4882a593Smuzhiyun REMARKS:
454*4882a593Smuzhiyun Implements the CMP instruction and side effects.
455*4882a593Smuzhiyun ****************************************************************************/
456*4882a593Smuzhiyun u8
cmp_byte(u8 d,u8 s)457*4882a593Smuzhiyun cmp_byte(u8 d, u8 s)
458*4882a593Smuzhiyun {
459*4882a593Smuzhiyun register u32 res; /* all operands in native machine order */
460*4882a593Smuzhiyun register u32 bc;
461*4882a593Smuzhiyun
462*4882a593Smuzhiyun res = d - s;
463*4882a593Smuzhiyun CLEAR_FLAG(F_CF);
464*4882a593Smuzhiyun CONDITIONAL_SET_FLAG(res & 0x80, F_SF);
465*4882a593Smuzhiyun CONDITIONAL_SET_FLAG((res & 0xff) == 0, F_ZF);
466*4882a593Smuzhiyun CONDITIONAL_SET_FLAG(PARITY(res & 0xff), F_PF);
467*4882a593Smuzhiyun
468*4882a593Smuzhiyun /* calculate the borrow chain. See note at top */
469*4882a593Smuzhiyun bc = (res & (~d | s)) | (~d & s);
470*4882a593Smuzhiyun CONDITIONAL_SET_FLAG(bc & 0x80, F_CF);
471*4882a593Smuzhiyun CONDITIONAL_SET_FLAG(XOR2(bc >> 6), F_OF);
472*4882a593Smuzhiyun CONDITIONAL_SET_FLAG(bc & 0x8, F_AF);
473*4882a593Smuzhiyun return d;
474*4882a593Smuzhiyun }
475*4882a593Smuzhiyun
476*4882a593Smuzhiyun /****************************************************************************
477*4882a593Smuzhiyun REMARKS:
478*4882a593Smuzhiyun Implements the CMP instruction and side effects.
479*4882a593Smuzhiyun ****************************************************************************/
480*4882a593Smuzhiyun u16
cmp_word(u16 d,u16 s)481*4882a593Smuzhiyun cmp_word(u16 d, u16 s)
482*4882a593Smuzhiyun {
483*4882a593Smuzhiyun register u32 res; /* all operands in native machine order */
484*4882a593Smuzhiyun register u32 bc;
485*4882a593Smuzhiyun
486*4882a593Smuzhiyun res = d - s;
487*4882a593Smuzhiyun CONDITIONAL_SET_FLAG(res & 0x8000, F_SF);
488*4882a593Smuzhiyun CONDITIONAL_SET_FLAG((res & 0xffff) == 0, F_ZF);
489*4882a593Smuzhiyun CONDITIONAL_SET_FLAG(PARITY(res & 0xff), F_PF);
490*4882a593Smuzhiyun
491*4882a593Smuzhiyun /* calculate the borrow chain. See note at top */
492*4882a593Smuzhiyun bc = (res & (~d | s)) | (~d & s);
493*4882a593Smuzhiyun CONDITIONAL_SET_FLAG(bc & 0x8000, F_CF);
494*4882a593Smuzhiyun CONDITIONAL_SET_FLAG(XOR2(bc >> 14), F_OF);
495*4882a593Smuzhiyun CONDITIONAL_SET_FLAG(bc & 0x8, F_AF);
496*4882a593Smuzhiyun return d;
497*4882a593Smuzhiyun }
498*4882a593Smuzhiyun
499*4882a593Smuzhiyun /****************************************************************************
500*4882a593Smuzhiyun REMARKS:
501*4882a593Smuzhiyun Implements the CMP instruction and side effects.
502*4882a593Smuzhiyun ****************************************************************************/
503*4882a593Smuzhiyun u32
cmp_long(u32 d,u32 s)504*4882a593Smuzhiyun cmp_long(u32 d, u32 s)
505*4882a593Smuzhiyun {
506*4882a593Smuzhiyun register u32 res; /* all operands in native machine order */
507*4882a593Smuzhiyun register u32 bc;
508*4882a593Smuzhiyun
509*4882a593Smuzhiyun res = d - s;
510*4882a593Smuzhiyun CONDITIONAL_SET_FLAG(res & 0x80000000, F_SF);
511*4882a593Smuzhiyun CONDITIONAL_SET_FLAG((res & 0xffffffff) == 0, F_ZF);
512*4882a593Smuzhiyun CONDITIONAL_SET_FLAG(PARITY(res & 0xff), F_PF);
513*4882a593Smuzhiyun
514*4882a593Smuzhiyun /* calculate the borrow chain. See note at top */
515*4882a593Smuzhiyun bc = (res & (~d | s)) | (~d & s);
516*4882a593Smuzhiyun CONDITIONAL_SET_FLAG(bc & 0x80000000, F_CF);
517*4882a593Smuzhiyun CONDITIONAL_SET_FLAG(XOR2(bc >> 30), F_OF);
518*4882a593Smuzhiyun CONDITIONAL_SET_FLAG(bc & 0x8, F_AF);
519*4882a593Smuzhiyun return d;
520*4882a593Smuzhiyun }
521*4882a593Smuzhiyun
522*4882a593Smuzhiyun /****************************************************************************
523*4882a593Smuzhiyun REMARKS:
524*4882a593Smuzhiyun Implements the DAA instruction and side effects.
525*4882a593Smuzhiyun ****************************************************************************/
526*4882a593Smuzhiyun u8
daa_byte(u8 d)527*4882a593Smuzhiyun daa_byte(u8 d)
528*4882a593Smuzhiyun {
529*4882a593Smuzhiyun u32 res = d;
530*4882a593Smuzhiyun
531*4882a593Smuzhiyun if ((d & 0xf) > 9 || ACCESS_FLAG(F_AF)) {
532*4882a593Smuzhiyun res += 6;
533*4882a593Smuzhiyun SET_FLAG(F_AF);
534*4882a593Smuzhiyun }
535*4882a593Smuzhiyun if (res > 0x9F || ACCESS_FLAG(F_CF)) {
536*4882a593Smuzhiyun res += 0x60;
537*4882a593Smuzhiyun SET_FLAG(F_CF);
538*4882a593Smuzhiyun }
539*4882a593Smuzhiyun CONDITIONAL_SET_FLAG(res & 0x80, F_SF);
540*4882a593Smuzhiyun CONDITIONAL_SET_FLAG((res & 0xFF) == 0, F_ZF);
541*4882a593Smuzhiyun CONDITIONAL_SET_FLAG(PARITY(res & 0xff), F_PF);
542*4882a593Smuzhiyun return (u8) res;
543*4882a593Smuzhiyun }
544*4882a593Smuzhiyun
545*4882a593Smuzhiyun /****************************************************************************
546*4882a593Smuzhiyun REMARKS:
547*4882a593Smuzhiyun Implements the DAS instruction and side effects.
548*4882a593Smuzhiyun ****************************************************************************/
549*4882a593Smuzhiyun u8
das_byte(u8 d)550*4882a593Smuzhiyun das_byte(u8 d)
551*4882a593Smuzhiyun {
552*4882a593Smuzhiyun if ((d & 0xf) > 9 || ACCESS_FLAG(F_AF)) {
553*4882a593Smuzhiyun d -= 6;
554*4882a593Smuzhiyun SET_FLAG(F_AF);
555*4882a593Smuzhiyun }
556*4882a593Smuzhiyun if (d > 0x9F || ACCESS_FLAG(F_CF)) {
557*4882a593Smuzhiyun d -= 0x60;
558*4882a593Smuzhiyun SET_FLAG(F_CF);
559*4882a593Smuzhiyun }
560*4882a593Smuzhiyun CONDITIONAL_SET_FLAG(d & 0x80, F_SF);
561*4882a593Smuzhiyun CONDITIONAL_SET_FLAG(d == 0, F_ZF);
562*4882a593Smuzhiyun CONDITIONAL_SET_FLAG(PARITY(d & 0xff), F_PF);
563*4882a593Smuzhiyun return d;
564*4882a593Smuzhiyun }
565*4882a593Smuzhiyun
566*4882a593Smuzhiyun /****************************************************************************
567*4882a593Smuzhiyun REMARKS:
568*4882a593Smuzhiyun Implements the DEC instruction and side effects.
569*4882a593Smuzhiyun ****************************************************************************/
570*4882a593Smuzhiyun u8
dec_byte(u8 d)571*4882a593Smuzhiyun dec_byte(u8 d)
572*4882a593Smuzhiyun {
573*4882a593Smuzhiyun register u32 res; /* all operands in native machine order */
574*4882a593Smuzhiyun register u32 bc;
575*4882a593Smuzhiyun
576*4882a593Smuzhiyun res = d - 1;
577*4882a593Smuzhiyun CONDITIONAL_SET_FLAG(res & 0x80, F_SF);
578*4882a593Smuzhiyun CONDITIONAL_SET_FLAG((res & 0xff) == 0, F_ZF);
579*4882a593Smuzhiyun CONDITIONAL_SET_FLAG(PARITY(res & 0xff), F_PF);
580*4882a593Smuzhiyun
581*4882a593Smuzhiyun /* calculate the borrow chain. See note at top */
582*4882a593Smuzhiyun /* based on sub_byte, uses s==1. */
583*4882a593Smuzhiyun bc = (res & (~d | 1)) | (~d & 1);
584*4882a593Smuzhiyun /* carry flag unchanged */
585*4882a593Smuzhiyun CONDITIONAL_SET_FLAG(XOR2(bc >> 6), F_OF);
586*4882a593Smuzhiyun CONDITIONAL_SET_FLAG(bc & 0x8, F_AF);
587*4882a593Smuzhiyun return (u8) res;
588*4882a593Smuzhiyun }
589*4882a593Smuzhiyun
590*4882a593Smuzhiyun /****************************************************************************
591*4882a593Smuzhiyun REMARKS:
592*4882a593Smuzhiyun Implements the DEC instruction and side effects.
593*4882a593Smuzhiyun ****************************************************************************/
594*4882a593Smuzhiyun u16
dec_word(u16 d)595*4882a593Smuzhiyun dec_word(u16 d)
596*4882a593Smuzhiyun {
597*4882a593Smuzhiyun register u32 res; /* all operands in native machine order */
598*4882a593Smuzhiyun register u32 bc;
599*4882a593Smuzhiyun
600*4882a593Smuzhiyun res = d - 1;
601*4882a593Smuzhiyun CONDITIONAL_SET_FLAG(res & 0x8000, F_SF);
602*4882a593Smuzhiyun CONDITIONAL_SET_FLAG((res & 0xffff) == 0, F_ZF);
603*4882a593Smuzhiyun CONDITIONAL_SET_FLAG(PARITY(res & 0xff), F_PF);
604*4882a593Smuzhiyun
605*4882a593Smuzhiyun /* calculate the borrow chain. See note at top */
606*4882a593Smuzhiyun /* based on the sub_byte routine, with s==1 */
607*4882a593Smuzhiyun bc = (res & (~d | 1)) | (~d & 1);
608*4882a593Smuzhiyun /* carry flag unchanged */
609*4882a593Smuzhiyun CONDITIONAL_SET_FLAG(XOR2(bc >> 14), F_OF);
610*4882a593Smuzhiyun CONDITIONAL_SET_FLAG(bc & 0x8, F_AF);
611*4882a593Smuzhiyun return (u16) res;
612*4882a593Smuzhiyun }
613*4882a593Smuzhiyun
614*4882a593Smuzhiyun /****************************************************************************
615*4882a593Smuzhiyun REMARKS:
616*4882a593Smuzhiyun Implements the DEC instruction and side effects.
617*4882a593Smuzhiyun ****************************************************************************/
618*4882a593Smuzhiyun u32
dec_long(u32 d)619*4882a593Smuzhiyun dec_long(u32 d)
620*4882a593Smuzhiyun {
621*4882a593Smuzhiyun register u32 res; /* all operands in native machine order */
622*4882a593Smuzhiyun register u32 bc;
623*4882a593Smuzhiyun
624*4882a593Smuzhiyun res = d - 1;
625*4882a593Smuzhiyun
626*4882a593Smuzhiyun CONDITIONAL_SET_FLAG(res & 0x80000000, F_SF);
627*4882a593Smuzhiyun CONDITIONAL_SET_FLAG((res & 0xffffffff) == 0, F_ZF);
628*4882a593Smuzhiyun CONDITIONAL_SET_FLAG(PARITY(res & 0xff), F_PF);
629*4882a593Smuzhiyun
630*4882a593Smuzhiyun /* calculate the borrow chain. See note at top */
631*4882a593Smuzhiyun bc = (res & (~d | 1)) | (~d & 1);
632*4882a593Smuzhiyun /* carry flag unchanged */
633*4882a593Smuzhiyun CONDITIONAL_SET_FLAG(XOR2(bc >> 30), F_OF);
634*4882a593Smuzhiyun CONDITIONAL_SET_FLAG(bc & 0x8, F_AF);
635*4882a593Smuzhiyun return res;
636*4882a593Smuzhiyun }
637*4882a593Smuzhiyun
638*4882a593Smuzhiyun /****************************************************************************
639*4882a593Smuzhiyun REMARKS:
640*4882a593Smuzhiyun Implements the INC instruction and side effects.
641*4882a593Smuzhiyun ****************************************************************************/
642*4882a593Smuzhiyun u8
inc_byte(u8 d)643*4882a593Smuzhiyun inc_byte(u8 d)
644*4882a593Smuzhiyun {
645*4882a593Smuzhiyun register u32 res; /* all operands in native machine order */
646*4882a593Smuzhiyun register u32 cc;
647*4882a593Smuzhiyun
648*4882a593Smuzhiyun res = d + 1;
649*4882a593Smuzhiyun CONDITIONAL_SET_FLAG((res & 0xff) == 0, F_ZF);
650*4882a593Smuzhiyun CONDITIONAL_SET_FLAG(res & 0x80, F_SF);
651*4882a593Smuzhiyun CONDITIONAL_SET_FLAG(PARITY(res & 0xff), F_PF);
652*4882a593Smuzhiyun
653*4882a593Smuzhiyun /* calculate the carry chain SEE NOTE AT TOP. */
654*4882a593Smuzhiyun cc = ((1 & d) | (~res)) & (1 | d);
655*4882a593Smuzhiyun CONDITIONAL_SET_FLAG(XOR2(cc >> 6), F_OF);
656*4882a593Smuzhiyun CONDITIONAL_SET_FLAG(cc & 0x8, F_AF);
657*4882a593Smuzhiyun return (u8) res;
658*4882a593Smuzhiyun }
659*4882a593Smuzhiyun
660*4882a593Smuzhiyun /****************************************************************************
661*4882a593Smuzhiyun REMARKS:
662*4882a593Smuzhiyun Implements the INC instruction and side effects.
663*4882a593Smuzhiyun ****************************************************************************/
664*4882a593Smuzhiyun u16
inc_word(u16 d)665*4882a593Smuzhiyun inc_word(u16 d)
666*4882a593Smuzhiyun {
667*4882a593Smuzhiyun register u32 res; /* all operands in native machine order */
668*4882a593Smuzhiyun register u32 cc;
669*4882a593Smuzhiyun
670*4882a593Smuzhiyun res = d + 1;
671*4882a593Smuzhiyun CONDITIONAL_SET_FLAG((res & 0xffff) == 0, F_ZF);
672*4882a593Smuzhiyun CONDITIONAL_SET_FLAG(res & 0x8000, F_SF);
673*4882a593Smuzhiyun CONDITIONAL_SET_FLAG(PARITY(res & 0xff), F_PF);
674*4882a593Smuzhiyun
675*4882a593Smuzhiyun /* calculate the carry chain SEE NOTE AT TOP. */
676*4882a593Smuzhiyun cc = (1 & d) | ((~res) & (1 | d));
677*4882a593Smuzhiyun CONDITIONAL_SET_FLAG(XOR2(cc >> 14), F_OF);
678*4882a593Smuzhiyun CONDITIONAL_SET_FLAG(cc & 0x8, F_AF);
679*4882a593Smuzhiyun return (u16) res;
680*4882a593Smuzhiyun }
681*4882a593Smuzhiyun
682*4882a593Smuzhiyun /****************************************************************************
683*4882a593Smuzhiyun REMARKS:
684*4882a593Smuzhiyun Implements the INC instruction and side effects.
685*4882a593Smuzhiyun ****************************************************************************/
686*4882a593Smuzhiyun u32
inc_long(u32 d)687*4882a593Smuzhiyun inc_long(u32 d)
688*4882a593Smuzhiyun {
689*4882a593Smuzhiyun register u32 res; /* all operands in native machine order */
690*4882a593Smuzhiyun register u32 cc;
691*4882a593Smuzhiyun
692*4882a593Smuzhiyun res = d + 1;
693*4882a593Smuzhiyun CONDITIONAL_SET_FLAG((res & 0xffffffff) == 0, F_ZF);
694*4882a593Smuzhiyun CONDITIONAL_SET_FLAG(res & 0x80000000, F_SF);
695*4882a593Smuzhiyun CONDITIONAL_SET_FLAG(PARITY(res & 0xff), F_PF);
696*4882a593Smuzhiyun
697*4882a593Smuzhiyun /* calculate the carry chain SEE NOTE AT TOP. */
698*4882a593Smuzhiyun cc = (1 & d) | ((~res) & (1 | d));
699*4882a593Smuzhiyun CONDITIONAL_SET_FLAG(XOR2(cc >> 30), F_OF);
700*4882a593Smuzhiyun CONDITIONAL_SET_FLAG(cc & 0x8, F_AF);
701*4882a593Smuzhiyun return res;
702*4882a593Smuzhiyun }
703*4882a593Smuzhiyun
704*4882a593Smuzhiyun /****************************************************************************
705*4882a593Smuzhiyun REMARKS:
706*4882a593Smuzhiyun Implements the OR instruction and side effects.
707*4882a593Smuzhiyun ****************************************************************************/
708*4882a593Smuzhiyun u8
or_byte(u8 d,u8 s)709*4882a593Smuzhiyun or_byte(u8 d, u8 s)
710*4882a593Smuzhiyun {
711*4882a593Smuzhiyun register u8 res; /* all operands in native machine order */
712*4882a593Smuzhiyun
713*4882a593Smuzhiyun res = d | s;
714*4882a593Smuzhiyun CLEAR_FLAG(F_OF);
715*4882a593Smuzhiyun CLEAR_FLAG(F_CF);
716*4882a593Smuzhiyun CLEAR_FLAG(F_AF);
717*4882a593Smuzhiyun CONDITIONAL_SET_FLAG(res & 0x80, F_SF);
718*4882a593Smuzhiyun CONDITIONAL_SET_FLAG(res == 0, F_ZF);
719*4882a593Smuzhiyun CONDITIONAL_SET_FLAG(PARITY(res), F_PF);
720*4882a593Smuzhiyun return res;
721*4882a593Smuzhiyun }
722*4882a593Smuzhiyun
723*4882a593Smuzhiyun /****************************************************************************
724*4882a593Smuzhiyun REMARKS:
725*4882a593Smuzhiyun Implements the OR instruction and side effects.
726*4882a593Smuzhiyun ****************************************************************************/
727*4882a593Smuzhiyun u16
or_word(u16 d,u16 s)728*4882a593Smuzhiyun or_word(u16 d, u16 s)
729*4882a593Smuzhiyun {
730*4882a593Smuzhiyun register u16 res; /* all operands in native machine order */
731*4882a593Smuzhiyun
732*4882a593Smuzhiyun res = d | s;
733*4882a593Smuzhiyun /* set the carry flag to be bit 8 */
734*4882a593Smuzhiyun CLEAR_FLAG(F_OF);
735*4882a593Smuzhiyun CLEAR_FLAG(F_CF);
736*4882a593Smuzhiyun CLEAR_FLAG(F_AF);
737*4882a593Smuzhiyun CONDITIONAL_SET_FLAG(res & 0x8000, F_SF);
738*4882a593Smuzhiyun CONDITIONAL_SET_FLAG(res == 0, F_ZF);
739*4882a593Smuzhiyun CONDITIONAL_SET_FLAG(PARITY(res & 0xff), F_PF);
740*4882a593Smuzhiyun return res;
741*4882a593Smuzhiyun }
742*4882a593Smuzhiyun
743*4882a593Smuzhiyun /****************************************************************************
744*4882a593Smuzhiyun REMARKS:
745*4882a593Smuzhiyun Implements the OR instruction and side effects.
746*4882a593Smuzhiyun ****************************************************************************/
747*4882a593Smuzhiyun u32
or_long(u32 d,u32 s)748*4882a593Smuzhiyun or_long(u32 d, u32 s)
749*4882a593Smuzhiyun {
750*4882a593Smuzhiyun register u32 res; /* all operands in native machine order */
751*4882a593Smuzhiyun
752*4882a593Smuzhiyun res = d | s;
753*4882a593Smuzhiyun
754*4882a593Smuzhiyun /* set the carry flag to be bit 8 */
755*4882a593Smuzhiyun CLEAR_FLAG(F_OF);
756*4882a593Smuzhiyun CLEAR_FLAG(F_CF);
757*4882a593Smuzhiyun CLEAR_FLAG(F_AF);
758*4882a593Smuzhiyun CONDITIONAL_SET_FLAG(res & 0x80000000, F_SF);
759*4882a593Smuzhiyun CONDITIONAL_SET_FLAG(res == 0, F_ZF);
760*4882a593Smuzhiyun CONDITIONAL_SET_FLAG(PARITY(res & 0xff), F_PF);
761*4882a593Smuzhiyun return res;
762*4882a593Smuzhiyun }
763*4882a593Smuzhiyun
764*4882a593Smuzhiyun /****************************************************************************
765*4882a593Smuzhiyun REMARKS:
766*4882a593Smuzhiyun Implements the OR instruction and side effects.
767*4882a593Smuzhiyun ****************************************************************************/
768*4882a593Smuzhiyun u8
neg_byte(u8 s)769*4882a593Smuzhiyun neg_byte(u8 s)
770*4882a593Smuzhiyun {
771*4882a593Smuzhiyun register u8 res;
772*4882a593Smuzhiyun register u8 bc;
773*4882a593Smuzhiyun
774*4882a593Smuzhiyun CONDITIONAL_SET_FLAG(s != 0, F_CF);
775*4882a593Smuzhiyun res = (u8) - s;
776*4882a593Smuzhiyun CONDITIONAL_SET_FLAG((res & 0xff) == 0, F_ZF);
777*4882a593Smuzhiyun CONDITIONAL_SET_FLAG(res & 0x80, F_SF);
778*4882a593Smuzhiyun CONDITIONAL_SET_FLAG(PARITY(res), F_PF);
779*4882a593Smuzhiyun /* calculate the borrow chain --- modified such that d=0.
780*4882a593Smuzhiyun substitutiing d=0 into bc= res&(~d|s)|(~d&s);
781*4882a593Smuzhiyun (the one used for sub) and simplifying, since ~d=0xff...,
782*4882a593Smuzhiyun ~d|s == 0xffff..., and res&0xfff... == res. Similarly
783*4882a593Smuzhiyun ~d&s == s. So the simplified result is: */
784*4882a593Smuzhiyun bc = res | s;
785*4882a593Smuzhiyun CONDITIONAL_SET_FLAG(XOR2(bc >> 6), F_OF);
786*4882a593Smuzhiyun CONDITIONAL_SET_FLAG(bc & 0x8, F_AF);
787*4882a593Smuzhiyun return res;
788*4882a593Smuzhiyun }
789*4882a593Smuzhiyun
790*4882a593Smuzhiyun /****************************************************************************
791*4882a593Smuzhiyun REMARKS:
792*4882a593Smuzhiyun Implements the OR instruction and side effects.
793*4882a593Smuzhiyun ****************************************************************************/
794*4882a593Smuzhiyun u16
neg_word(u16 s)795*4882a593Smuzhiyun neg_word(u16 s)
796*4882a593Smuzhiyun {
797*4882a593Smuzhiyun register u16 res;
798*4882a593Smuzhiyun register u16 bc;
799*4882a593Smuzhiyun
800*4882a593Smuzhiyun CONDITIONAL_SET_FLAG(s != 0, F_CF);
801*4882a593Smuzhiyun res = (u16) - s;
802*4882a593Smuzhiyun CONDITIONAL_SET_FLAG((res & 0xffff) == 0, F_ZF);
803*4882a593Smuzhiyun CONDITIONAL_SET_FLAG(res & 0x8000, F_SF);
804*4882a593Smuzhiyun CONDITIONAL_SET_FLAG(PARITY(res & 0xff), F_PF);
805*4882a593Smuzhiyun
806*4882a593Smuzhiyun /* calculate the borrow chain --- modified such that d=0.
807*4882a593Smuzhiyun substitutiing d=0 into bc= res&(~d|s)|(~d&s);
808*4882a593Smuzhiyun (the one used for sub) and simplifying, since ~d=0xff...,
809*4882a593Smuzhiyun ~d|s == 0xffff..., and res&0xfff... == res. Similarly
810*4882a593Smuzhiyun ~d&s == s. So the simplified result is: */
811*4882a593Smuzhiyun bc = res | s;
812*4882a593Smuzhiyun CONDITIONAL_SET_FLAG(XOR2(bc >> 14), F_OF);
813*4882a593Smuzhiyun CONDITIONAL_SET_FLAG(bc & 0x8, F_AF);
814*4882a593Smuzhiyun return res;
815*4882a593Smuzhiyun }
816*4882a593Smuzhiyun
817*4882a593Smuzhiyun /****************************************************************************
818*4882a593Smuzhiyun REMARKS:
819*4882a593Smuzhiyun Implements the OR instruction and side effects.
820*4882a593Smuzhiyun ****************************************************************************/
821*4882a593Smuzhiyun u32
neg_long(u32 s)822*4882a593Smuzhiyun neg_long(u32 s)
823*4882a593Smuzhiyun {
824*4882a593Smuzhiyun register u32 res;
825*4882a593Smuzhiyun register u32 bc;
826*4882a593Smuzhiyun
827*4882a593Smuzhiyun CONDITIONAL_SET_FLAG(s != 0, F_CF);
828*4882a593Smuzhiyun res = (u32) - s;
829*4882a593Smuzhiyun CONDITIONAL_SET_FLAG((res & 0xffffffff) == 0, F_ZF);
830*4882a593Smuzhiyun CONDITIONAL_SET_FLAG(res & 0x80000000, F_SF);
831*4882a593Smuzhiyun CONDITIONAL_SET_FLAG(PARITY(res & 0xff), F_PF);
832*4882a593Smuzhiyun
833*4882a593Smuzhiyun /* calculate the borrow chain --- modified such that d=0.
834*4882a593Smuzhiyun substitutiing d=0 into bc= res&(~d|s)|(~d&s);
835*4882a593Smuzhiyun (the one used for sub) and simplifying, since ~d=0xff...,
836*4882a593Smuzhiyun ~d|s == 0xffff..., and res&0xfff... == res. Similarly
837*4882a593Smuzhiyun ~d&s == s. So the simplified result is: */
838*4882a593Smuzhiyun bc = res | s;
839*4882a593Smuzhiyun CONDITIONAL_SET_FLAG(XOR2(bc >> 30), F_OF);
840*4882a593Smuzhiyun CONDITIONAL_SET_FLAG(bc & 0x8, F_AF);
841*4882a593Smuzhiyun return res;
842*4882a593Smuzhiyun }
843*4882a593Smuzhiyun
844*4882a593Smuzhiyun /****************************************************************************
845*4882a593Smuzhiyun REMARKS:
846*4882a593Smuzhiyun Implements the NOT instruction and side effects.
847*4882a593Smuzhiyun ****************************************************************************/
848*4882a593Smuzhiyun u8
not_byte(u8 s)849*4882a593Smuzhiyun not_byte(u8 s)
850*4882a593Smuzhiyun {
851*4882a593Smuzhiyun return ~s;
852*4882a593Smuzhiyun }
853*4882a593Smuzhiyun
854*4882a593Smuzhiyun /****************************************************************************
855*4882a593Smuzhiyun REMARKS:
856*4882a593Smuzhiyun Implements the NOT instruction and side effects.
857*4882a593Smuzhiyun ****************************************************************************/
858*4882a593Smuzhiyun u16
not_word(u16 s)859*4882a593Smuzhiyun not_word(u16 s)
860*4882a593Smuzhiyun {
861*4882a593Smuzhiyun return ~s;
862*4882a593Smuzhiyun }
863*4882a593Smuzhiyun
864*4882a593Smuzhiyun /****************************************************************************
865*4882a593Smuzhiyun REMARKS:
866*4882a593Smuzhiyun Implements the NOT instruction and side effects.
867*4882a593Smuzhiyun ****************************************************************************/
868*4882a593Smuzhiyun u32
not_long(u32 s)869*4882a593Smuzhiyun not_long(u32 s)
870*4882a593Smuzhiyun {
871*4882a593Smuzhiyun return ~s;
872*4882a593Smuzhiyun }
873*4882a593Smuzhiyun
874*4882a593Smuzhiyun /****************************************************************************
875*4882a593Smuzhiyun REMARKS:
876*4882a593Smuzhiyun Implements the RCL instruction and side effects.
877*4882a593Smuzhiyun ****************************************************************************/
878*4882a593Smuzhiyun u8
rcl_byte(u8 d,u8 s)879*4882a593Smuzhiyun rcl_byte(u8 d, u8 s)
880*4882a593Smuzhiyun {
881*4882a593Smuzhiyun register unsigned int res, cnt, mask, cf;
882*4882a593Smuzhiyun
883*4882a593Smuzhiyun /* s is the rotate distance. It varies from 0 - 8. */
884*4882a593Smuzhiyun /* have
885*4882a593Smuzhiyun
886*4882a593Smuzhiyun CF B_7 B_6 B_5 B_4 B_3 B_2 B_1 B_0
887*4882a593Smuzhiyun
888*4882a593Smuzhiyun want to rotate through the carry by "s" bits. We could
889*4882a593Smuzhiyun loop, but that's inefficient. So the width is 9,
890*4882a593Smuzhiyun and we split into three parts:
891*4882a593Smuzhiyun
892*4882a593Smuzhiyun The new carry flag (was B_n)
893*4882a593Smuzhiyun the stuff in B_n-1 .. B_0
894*4882a593Smuzhiyun the stuff in B_7 .. B_n+1
895*4882a593Smuzhiyun
896*4882a593Smuzhiyun The new rotate is done mod 9, and given this,
897*4882a593Smuzhiyun for a rotation of n bits (mod 9) the new carry flag is
898*4882a593Smuzhiyun then located n bits from the MSB. The low part is
899*4882a593Smuzhiyun then shifted up cnt bits, and the high part is or'd
900*4882a593Smuzhiyun in. Using CAPS for new values, and lowercase for the
901*4882a593Smuzhiyun original values, this can be expressed as:
902*4882a593Smuzhiyun
903*4882a593Smuzhiyun IF n > 0
904*4882a593Smuzhiyun 1) CF <- b_(8-n)
905*4882a593Smuzhiyun 2) B_(7) .. B_(n) <- b_(8-(n+1)) .. b_0
906*4882a593Smuzhiyun 3) B_(n-1) <- cf
907*4882a593Smuzhiyun 4) B_(n-2) .. B_0 <- b_7 .. b_(8-(n-1))
908*4882a593Smuzhiyun */
909*4882a593Smuzhiyun res = d;
910*4882a593Smuzhiyun if ((cnt = s % 9) != 0) {
911*4882a593Smuzhiyun /* extract the new CARRY FLAG. */
912*4882a593Smuzhiyun /* CF <- b_(8-n) */
913*4882a593Smuzhiyun cf = (d >> (8 - cnt)) & 0x1;
914*4882a593Smuzhiyun
915*4882a593Smuzhiyun /* get the low stuff which rotated
916*4882a593Smuzhiyun into the range B_7 .. B_cnt */
917*4882a593Smuzhiyun /* B_(7) .. B_(n) <- b_(8-(n+1)) .. b_0 */
918*4882a593Smuzhiyun /* note that the right hand side done by the mask */
919*4882a593Smuzhiyun res = (d << cnt) & 0xff;
920*4882a593Smuzhiyun
921*4882a593Smuzhiyun /* now the high stuff which rotated around
922*4882a593Smuzhiyun into the positions B_cnt-2 .. B_0 */
923*4882a593Smuzhiyun /* B_(n-2) .. B_0 <- b_7 .. b_(8-(n-1)) */
924*4882a593Smuzhiyun /* shift it downward, 7-(n-2) = 9-n positions.
925*4882a593Smuzhiyun and mask off the result before or'ing in.
926*4882a593Smuzhiyun */
927*4882a593Smuzhiyun mask = (1 << (cnt - 1)) - 1;
928*4882a593Smuzhiyun res |= (d >> (9 - cnt)) & mask;
929*4882a593Smuzhiyun
930*4882a593Smuzhiyun /* if the carry flag was set, or it in. */
931*4882a593Smuzhiyun if (ACCESS_FLAG(F_CF)) { /* carry flag is set */
932*4882a593Smuzhiyun /* B_(n-1) <- cf */
933*4882a593Smuzhiyun res |= 1 << (cnt - 1);
934*4882a593Smuzhiyun }
935*4882a593Smuzhiyun /* set the new carry flag, based on the variable "cf" */
936*4882a593Smuzhiyun CONDITIONAL_SET_FLAG(cf, F_CF);
937*4882a593Smuzhiyun /* OVERFLOW is set *IFF* cnt==1, then it is the
938*4882a593Smuzhiyun xor of CF and the most significant bit. Blecck. */
939*4882a593Smuzhiyun /* parenthesized this expression since it appears to
940*4882a593Smuzhiyun be causing OF to be misset */
941*4882a593Smuzhiyun CONDITIONAL_SET_FLAG(cnt == 1 && XOR2(cf + ((res >> 6) & 0x2)), F_OF);
942*4882a593Smuzhiyun
943*4882a593Smuzhiyun }
944*4882a593Smuzhiyun return (u8) res;
945*4882a593Smuzhiyun }
946*4882a593Smuzhiyun
947*4882a593Smuzhiyun /****************************************************************************
948*4882a593Smuzhiyun REMARKS:
949*4882a593Smuzhiyun Implements the RCL instruction and side effects.
950*4882a593Smuzhiyun ****************************************************************************/
951*4882a593Smuzhiyun u16
rcl_word(u16 d,u8 s)952*4882a593Smuzhiyun rcl_word(u16 d, u8 s)
953*4882a593Smuzhiyun {
954*4882a593Smuzhiyun register unsigned int res, cnt, mask, cf;
955*4882a593Smuzhiyun
956*4882a593Smuzhiyun res = d;
957*4882a593Smuzhiyun if ((cnt = s % 17) != 0) {
958*4882a593Smuzhiyun cf = (d >> (16 - cnt)) & 0x1;
959*4882a593Smuzhiyun res = (d << cnt) & 0xffff;
960*4882a593Smuzhiyun mask = (1 << (cnt - 1)) - 1;
961*4882a593Smuzhiyun res |= (d >> (17 - cnt)) & mask;
962*4882a593Smuzhiyun if (ACCESS_FLAG(F_CF)) {
963*4882a593Smuzhiyun res |= 1 << (cnt - 1);
964*4882a593Smuzhiyun }
965*4882a593Smuzhiyun CONDITIONAL_SET_FLAG(cf, F_CF);
966*4882a593Smuzhiyun CONDITIONAL_SET_FLAG(cnt == 1 && XOR2(cf + ((res >> 14) & 0x2)), F_OF);
967*4882a593Smuzhiyun }
968*4882a593Smuzhiyun return (u16) res;
969*4882a593Smuzhiyun }
970*4882a593Smuzhiyun
971*4882a593Smuzhiyun /****************************************************************************
972*4882a593Smuzhiyun REMARKS:
973*4882a593Smuzhiyun Implements the RCL instruction and side effects.
974*4882a593Smuzhiyun ****************************************************************************/
975*4882a593Smuzhiyun u32
rcl_long(u32 d,u8 s)976*4882a593Smuzhiyun rcl_long(u32 d, u8 s)
977*4882a593Smuzhiyun {
978*4882a593Smuzhiyun register u32 res, cnt, mask, cf;
979*4882a593Smuzhiyun
980*4882a593Smuzhiyun res = d;
981*4882a593Smuzhiyun if ((cnt = s % 33) != 0) {
982*4882a593Smuzhiyun cf = (d >> (32 - cnt)) & 0x1;
983*4882a593Smuzhiyun res = (d << cnt) & 0xffffffff;
984*4882a593Smuzhiyun mask = (1 << (cnt - 1)) - 1;
985*4882a593Smuzhiyun res |= (d >> (33 - cnt)) & mask;
986*4882a593Smuzhiyun if (ACCESS_FLAG(F_CF)) { /* carry flag is set */
987*4882a593Smuzhiyun res |= 1 << (cnt - 1);
988*4882a593Smuzhiyun }
989*4882a593Smuzhiyun CONDITIONAL_SET_FLAG(cf, F_CF);
990*4882a593Smuzhiyun CONDITIONAL_SET_FLAG(cnt == 1 && XOR2(cf + ((res >> 30) & 0x2)), F_OF);
991*4882a593Smuzhiyun }
992*4882a593Smuzhiyun return res;
993*4882a593Smuzhiyun }
994*4882a593Smuzhiyun
995*4882a593Smuzhiyun /****************************************************************************
996*4882a593Smuzhiyun REMARKS:
997*4882a593Smuzhiyun Implements the RCR instruction and side effects.
998*4882a593Smuzhiyun ****************************************************************************/
999*4882a593Smuzhiyun u8
rcr_byte(u8 d,u8 s)1000*4882a593Smuzhiyun rcr_byte(u8 d, u8 s)
1001*4882a593Smuzhiyun {
1002*4882a593Smuzhiyun u32 res, cnt;
1003*4882a593Smuzhiyun u32 mask, cf, ocf = 0;
1004*4882a593Smuzhiyun
1005*4882a593Smuzhiyun /* rotate right through carry */
1006*4882a593Smuzhiyun /*
1007*4882a593Smuzhiyun s is the rotate distance. It varies from 0 - 8.
1008*4882a593Smuzhiyun d is the byte object rotated.
1009*4882a593Smuzhiyun
1010*4882a593Smuzhiyun have
1011*4882a593Smuzhiyun
1012*4882a593Smuzhiyun CF B_7 B_6 B_5 B_4 B_3 B_2 B_1 B_0
1013*4882a593Smuzhiyun
1014*4882a593Smuzhiyun The new rotate is done mod 9, and given this,
1015*4882a593Smuzhiyun for a rotation of n bits (mod 9) the new carry flag is
1016*4882a593Smuzhiyun then located n bits from the LSB. The low part is
1017*4882a593Smuzhiyun then shifted up cnt bits, and the high part is or'd
1018*4882a593Smuzhiyun in. Using CAPS for new values, and lowercase for the
1019*4882a593Smuzhiyun original values, this can be expressed as:
1020*4882a593Smuzhiyun
1021*4882a593Smuzhiyun IF n > 0
1022*4882a593Smuzhiyun 1) CF <- b_(n-1)
1023*4882a593Smuzhiyun 2) B_(8-(n+1)) .. B_(0) <- b_(7) .. b_(n)
1024*4882a593Smuzhiyun 3) B_(8-n) <- cf
1025*4882a593Smuzhiyun 4) B_(7) .. B_(8-(n-1)) <- b_(n-2) .. b_(0)
1026*4882a593Smuzhiyun */
1027*4882a593Smuzhiyun res = d;
1028*4882a593Smuzhiyun if ((cnt = s % 9) != 0) {
1029*4882a593Smuzhiyun /* extract the new CARRY FLAG. */
1030*4882a593Smuzhiyun /* CF <- b_(n-1) */
1031*4882a593Smuzhiyun if (cnt == 1) {
1032*4882a593Smuzhiyun cf = d & 0x1;
1033*4882a593Smuzhiyun /* note hackery here. Access_flag(..) evaluates to either
1034*4882a593Smuzhiyun 0 if flag not set
1035*4882a593Smuzhiyun non-zero if flag is set.
1036*4882a593Smuzhiyun doing access_flag(..) != 0 casts that into either
1037*4882a593Smuzhiyun 0..1 in any representation of the flags register
1038*4882a593Smuzhiyun (i.e. packed bit array or unpacked.)
1039*4882a593Smuzhiyun */
1040*4882a593Smuzhiyun ocf = ACCESS_FLAG(F_CF) != 0;
1041*4882a593Smuzhiyun }
1042*4882a593Smuzhiyun else
1043*4882a593Smuzhiyun cf = (d >> (cnt - 1)) & 0x1;
1044*4882a593Smuzhiyun
1045*4882a593Smuzhiyun /* B_(8-(n+1)) .. B_(0) <- b_(7) .. b_n */
1046*4882a593Smuzhiyun /* note that the right hand side done by the mask
1047*4882a593Smuzhiyun This is effectively done by shifting the
1048*4882a593Smuzhiyun object to the right. The result must be masked,
1049*4882a593Smuzhiyun in case the object came in and was treated
1050*4882a593Smuzhiyun as a negative number. Needed??? */
1051*4882a593Smuzhiyun
1052*4882a593Smuzhiyun mask = (1 << (8 - cnt)) - 1;
1053*4882a593Smuzhiyun res = (d >> cnt) & mask;
1054*4882a593Smuzhiyun
1055*4882a593Smuzhiyun /* now the high stuff which rotated around
1056*4882a593Smuzhiyun into the positions B_cnt-2 .. B_0 */
1057*4882a593Smuzhiyun /* B_(7) .. B_(8-(n-1)) <- b_(n-2) .. b_(0) */
1058*4882a593Smuzhiyun /* shift it downward, 7-(n-2) = 9-n positions.
1059*4882a593Smuzhiyun and mask off the result before or'ing in.
1060*4882a593Smuzhiyun */
1061*4882a593Smuzhiyun res |= (d << (9 - cnt));
1062*4882a593Smuzhiyun
1063*4882a593Smuzhiyun /* if the carry flag was set, or it in. */
1064*4882a593Smuzhiyun if (ACCESS_FLAG(F_CF)) { /* carry flag is set */
1065*4882a593Smuzhiyun /* B_(8-n) <- cf */
1066*4882a593Smuzhiyun res |= 1 << (8 - cnt);
1067*4882a593Smuzhiyun }
1068*4882a593Smuzhiyun /* set the new carry flag, based on the variable "cf" */
1069*4882a593Smuzhiyun CONDITIONAL_SET_FLAG(cf, F_CF);
1070*4882a593Smuzhiyun /* OVERFLOW is set *IFF* cnt==1, then it is the
1071*4882a593Smuzhiyun xor of CF and the most significant bit. Blecck. */
1072*4882a593Smuzhiyun /* parenthesized... */
1073*4882a593Smuzhiyun if (cnt == 1) {
1074*4882a593Smuzhiyun CONDITIONAL_SET_FLAG(XOR2(ocf + ((d >> 6) & 0x2)), F_OF);
1075*4882a593Smuzhiyun }
1076*4882a593Smuzhiyun }
1077*4882a593Smuzhiyun return (u8) res;
1078*4882a593Smuzhiyun }
1079*4882a593Smuzhiyun
1080*4882a593Smuzhiyun /****************************************************************************
1081*4882a593Smuzhiyun REMARKS:
1082*4882a593Smuzhiyun Implements the RCR instruction and side effects.
1083*4882a593Smuzhiyun ****************************************************************************/
1084*4882a593Smuzhiyun u16
rcr_word(u16 d,u8 s)1085*4882a593Smuzhiyun rcr_word(u16 d, u8 s)
1086*4882a593Smuzhiyun {
1087*4882a593Smuzhiyun u32 res, cnt;
1088*4882a593Smuzhiyun u32 mask, cf, ocf = 0;
1089*4882a593Smuzhiyun
1090*4882a593Smuzhiyun /* rotate right through carry */
1091*4882a593Smuzhiyun res = d;
1092*4882a593Smuzhiyun if ((cnt = s % 17) != 0) {
1093*4882a593Smuzhiyun if (cnt == 1) {
1094*4882a593Smuzhiyun cf = d & 0x1;
1095*4882a593Smuzhiyun ocf = ACCESS_FLAG(F_CF) != 0;
1096*4882a593Smuzhiyun }
1097*4882a593Smuzhiyun else
1098*4882a593Smuzhiyun cf = (d >> (cnt - 1)) & 0x1;
1099*4882a593Smuzhiyun mask = (1 << (16 - cnt)) - 1;
1100*4882a593Smuzhiyun res = (d >> cnt) & mask;
1101*4882a593Smuzhiyun res |= (d << (17 - cnt));
1102*4882a593Smuzhiyun if (ACCESS_FLAG(F_CF)) {
1103*4882a593Smuzhiyun res |= 1 << (16 - cnt);
1104*4882a593Smuzhiyun }
1105*4882a593Smuzhiyun CONDITIONAL_SET_FLAG(cf, F_CF);
1106*4882a593Smuzhiyun if (cnt == 1) {
1107*4882a593Smuzhiyun CONDITIONAL_SET_FLAG(XOR2(ocf + ((d >> 14) & 0x2)), F_OF);
1108*4882a593Smuzhiyun }
1109*4882a593Smuzhiyun }
1110*4882a593Smuzhiyun return (u16) res;
1111*4882a593Smuzhiyun }
1112*4882a593Smuzhiyun
1113*4882a593Smuzhiyun /****************************************************************************
1114*4882a593Smuzhiyun REMARKS:
1115*4882a593Smuzhiyun Implements the RCR instruction and side effects.
1116*4882a593Smuzhiyun ****************************************************************************/
1117*4882a593Smuzhiyun u32
rcr_long(u32 d,u8 s)1118*4882a593Smuzhiyun rcr_long(u32 d, u8 s)
1119*4882a593Smuzhiyun {
1120*4882a593Smuzhiyun u32 res, cnt;
1121*4882a593Smuzhiyun u32 mask, cf, ocf = 0;
1122*4882a593Smuzhiyun
1123*4882a593Smuzhiyun /* rotate right through carry */
1124*4882a593Smuzhiyun res = d;
1125*4882a593Smuzhiyun if ((cnt = s % 33) != 0) {
1126*4882a593Smuzhiyun if (cnt == 1) {
1127*4882a593Smuzhiyun cf = d & 0x1;
1128*4882a593Smuzhiyun ocf = ACCESS_FLAG(F_CF) != 0;
1129*4882a593Smuzhiyun }
1130*4882a593Smuzhiyun else
1131*4882a593Smuzhiyun cf = (d >> (cnt - 1)) & 0x1;
1132*4882a593Smuzhiyun mask = (1 << (32 - cnt)) - 1;
1133*4882a593Smuzhiyun res = (d >> cnt) & mask;
1134*4882a593Smuzhiyun if (cnt != 1)
1135*4882a593Smuzhiyun res |= (d << (33 - cnt));
1136*4882a593Smuzhiyun if (ACCESS_FLAG(F_CF)) { /* carry flag is set */
1137*4882a593Smuzhiyun res |= 1 << (32 - cnt);
1138*4882a593Smuzhiyun }
1139*4882a593Smuzhiyun CONDITIONAL_SET_FLAG(cf, F_CF);
1140*4882a593Smuzhiyun if (cnt == 1) {
1141*4882a593Smuzhiyun CONDITIONAL_SET_FLAG(XOR2(ocf + ((d >> 30) & 0x2)), F_OF);
1142*4882a593Smuzhiyun }
1143*4882a593Smuzhiyun }
1144*4882a593Smuzhiyun return res;
1145*4882a593Smuzhiyun }
1146*4882a593Smuzhiyun
1147*4882a593Smuzhiyun /****************************************************************************
1148*4882a593Smuzhiyun REMARKS:
1149*4882a593Smuzhiyun Implements the ROL instruction and side effects.
1150*4882a593Smuzhiyun ****************************************************************************/
1151*4882a593Smuzhiyun u8
rol_byte(u8 d,u8 s)1152*4882a593Smuzhiyun rol_byte(u8 d, u8 s)
1153*4882a593Smuzhiyun {
1154*4882a593Smuzhiyun register unsigned int res, cnt, mask;
1155*4882a593Smuzhiyun
1156*4882a593Smuzhiyun /* rotate left */
1157*4882a593Smuzhiyun /*
1158*4882a593Smuzhiyun s is the rotate distance. It varies from 0 - 8.
1159*4882a593Smuzhiyun d is the byte object rotated.
1160*4882a593Smuzhiyun
1161*4882a593Smuzhiyun have
1162*4882a593Smuzhiyun
1163*4882a593Smuzhiyun CF B_7 ... B_0
1164*4882a593Smuzhiyun
1165*4882a593Smuzhiyun The new rotate is done mod 8.
1166*4882a593Smuzhiyun Much simpler than the "rcl" or "rcr" operations.
1167*4882a593Smuzhiyun
1168*4882a593Smuzhiyun IF n > 0
1169*4882a593Smuzhiyun 1) B_(7) .. B_(n) <- b_(8-(n+1)) .. b_(0)
1170*4882a593Smuzhiyun 2) B_(n-1) .. B_(0) <- b_(7) .. b_(8-n)
1171*4882a593Smuzhiyun */
1172*4882a593Smuzhiyun res = d;
1173*4882a593Smuzhiyun if ((cnt = s % 8) != 0) {
1174*4882a593Smuzhiyun /* B_(7) .. B_(n) <- b_(8-(n+1)) .. b_(0) */
1175*4882a593Smuzhiyun res = (d << cnt);
1176*4882a593Smuzhiyun
1177*4882a593Smuzhiyun /* B_(n-1) .. B_(0) <- b_(7) .. b_(8-n) */
1178*4882a593Smuzhiyun mask = (1 << cnt) - 1;
1179*4882a593Smuzhiyun res |= (d >> (8 - cnt)) & mask;
1180*4882a593Smuzhiyun
1181*4882a593Smuzhiyun /* set the new carry flag, Note that it is the low order
1182*4882a593Smuzhiyun bit of the result!!! */
1183*4882a593Smuzhiyun CONDITIONAL_SET_FLAG(res & 0x1, F_CF);
1184*4882a593Smuzhiyun /* OVERFLOW is set *IFF* s==1, then it is the
1185*4882a593Smuzhiyun xor of CF and the most significant bit. Blecck. */
1186*4882a593Smuzhiyun CONDITIONAL_SET_FLAG(s == 1 &&
1187*4882a593Smuzhiyun XOR2((res & 0x1) + ((res >> 6) & 0x2)), F_OF);
1188*4882a593Smuzhiyun }
1189*4882a593Smuzhiyun if (s != 0) {
1190*4882a593Smuzhiyun /* set the new carry flag, Note that it is the low order
1191*4882a593Smuzhiyun bit of the result!!! */
1192*4882a593Smuzhiyun CONDITIONAL_SET_FLAG(res & 0x1, F_CF);
1193*4882a593Smuzhiyun }
1194*4882a593Smuzhiyun return (u8) res;
1195*4882a593Smuzhiyun }
1196*4882a593Smuzhiyun
1197*4882a593Smuzhiyun /****************************************************************************
1198*4882a593Smuzhiyun REMARKS:
1199*4882a593Smuzhiyun Implements the ROL instruction and side effects.
1200*4882a593Smuzhiyun ****************************************************************************/
1201*4882a593Smuzhiyun u16
rol_word(u16 d,u8 s)1202*4882a593Smuzhiyun rol_word(u16 d, u8 s)
1203*4882a593Smuzhiyun {
1204*4882a593Smuzhiyun register unsigned int res, cnt, mask;
1205*4882a593Smuzhiyun
1206*4882a593Smuzhiyun res = d;
1207*4882a593Smuzhiyun if ((cnt = s % 16) != 0) {
1208*4882a593Smuzhiyun res = (d << cnt);
1209*4882a593Smuzhiyun mask = (1 << cnt) - 1;
1210*4882a593Smuzhiyun res |= (d >> (16 - cnt)) & mask;
1211*4882a593Smuzhiyun CONDITIONAL_SET_FLAG(res & 0x1, F_CF);
1212*4882a593Smuzhiyun CONDITIONAL_SET_FLAG(s == 1 &&
1213*4882a593Smuzhiyun XOR2((res & 0x1) + ((res >> 14) & 0x2)), F_OF);
1214*4882a593Smuzhiyun }
1215*4882a593Smuzhiyun if (s != 0) {
1216*4882a593Smuzhiyun /* set the new carry flag, Note that it is the low order
1217*4882a593Smuzhiyun bit of the result!!! */
1218*4882a593Smuzhiyun CONDITIONAL_SET_FLAG(res & 0x1, F_CF);
1219*4882a593Smuzhiyun }
1220*4882a593Smuzhiyun return (u16) res;
1221*4882a593Smuzhiyun }
1222*4882a593Smuzhiyun
1223*4882a593Smuzhiyun /****************************************************************************
1224*4882a593Smuzhiyun REMARKS:
1225*4882a593Smuzhiyun Implements the ROL instruction and side effects.
1226*4882a593Smuzhiyun ****************************************************************************/
1227*4882a593Smuzhiyun u32
rol_long(u32 d,u8 s)1228*4882a593Smuzhiyun rol_long(u32 d, u8 s)
1229*4882a593Smuzhiyun {
1230*4882a593Smuzhiyun register u32 res, cnt, mask;
1231*4882a593Smuzhiyun
1232*4882a593Smuzhiyun res = d;
1233*4882a593Smuzhiyun if ((cnt = s % 32) != 0) {
1234*4882a593Smuzhiyun res = (d << cnt);
1235*4882a593Smuzhiyun mask = (1 << cnt) - 1;
1236*4882a593Smuzhiyun res |= (d >> (32 - cnt)) & mask;
1237*4882a593Smuzhiyun CONDITIONAL_SET_FLAG(res & 0x1, F_CF);
1238*4882a593Smuzhiyun CONDITIONAL_SET_FLAG(s == 1 &&
1239*4882a593Smuzhiyun XOR2((res & 0x1) + ((res >> 30) & 0x2)), F_OF);
1240*4882a593Smuzhiyun }
1241*4882a593Smuzhiyun if (s != 0) {
1242*4882a593Smuzhiyun /* set the new carry flag, Note that it is the low order
1243*4882a593Smuzhiyun bit of the result!!! */
1244*4882a593Smuzhiyun CONDITIONAL_SET_FLAG(res & 0x1, F_CF);
1245*4882a593Smuzhiyun }
1246*4882a593Smuzhiyun return res;
1247*4882a593Smuzhiyun }
1248*4882a593Smuzhiyun
1249*4882a593Smuzhiyun /****************************************************************************
1250*4882a593Smuzhiyun REMARKS:
1251*4882a593Smuzhiyun Implements the ROR instruction and side effects.
1252*4882a593Smuzhiyun ****************************************************************************/
1253*4882a593Smuzhiyun u8
ror_byte(u8 d,u8 s)1254*4882a593Smuzhiyun ror_byte(u8 d, u8 s)
1255*4882a593Smuzhiyun {
1256*4882a593Smuzhiyun register unsigned int res, cnt, mask;
1257*4882a593Smuzhiyun
1258*4882a593Smuzhiyun /* rotate right */
1259*4882a593Smuzhiyun /*
1260*4882a593Smuzhiyun s is the rotate distance. It varies from 0 - 8.
1261*4882a593Smuzhiyun d is the byte object rotated.
1262*4882a593Smuzhiyun
1263*4882a593Smuzhiyun have
1264*4882a593Smuzhiyun
1265*4882a593Smuzhiyun B_7 ... B_0
1266*4882a593Smuzhiyun
1267*4882a593Smuzhiyun The rotate is done mod 8.
1268*4882a593Smuzhiyun
1269*4882a593Smuzhiyun IF n > 0
1270*4882a593Smuzhiyun 1) B_(8-(n+1)) .. B_(0) <- b_(7) .. b_(n)
1271*4882a593Smuzhiyun 2) B_(7) .. B_(8-n) <- b_(n-1) .. b_(0)
1272*4882a593Smuzhiyun */
1273*4882a593Smuzhiyun res = d;
1274*4882a593Smuzhiyun if ((cnt = s % 8) != 0) { /* not a typo, do nada if cnt==0 */
1275*4882a593Smuzhiyun /* B_(7) .. B_(8-n) <- b_(n-1) .. b_(0) */
1276*4882a593Smuzhiyun res = (d << (8 - cnt));
1277*4882a593Smuzhiyun
1278*4882a593Smuzhiyun /* B_(8-(n+1)) .. B_(0) <- b_(7) .. b_(n) */
1279*4882a593Smuzhiyun mask = (1 << (8 - cnt)) - 1;
1280*4882a593Smuzhiyun res |= (d >> (cnt)) & mask;
1281*4882a593Smuzhiyun
1282*4882a593Smuzhiyun /* set the new carry flag, Note that it is the low order
1283*4882a593Smuzhiyun bit of the result!!! */
1284*4882a593Smuzhiyun CONDITIONAL_SET_FLAG(res & 0x80, F_CF);
1285*4882a593Smuzhiyun /* OVERFLOW is set *IFF* s==1, then it is the
1286*4882a593Smuzhiyun xor of the two most significant bits. Blecck. */
1287*4882a593Smuzhiyun CONDITIONAL_SET_FLAG(s == 1 && XOR2(res >> 6), F_OF);
1288*4882a593Smuzhiyun }
1289*4882a593Smuzhiyun else if (s != 0) {
1290*4882a593Smuzhiyun /* set the new carry flag, Note that it is the low order
1291*4882a593Smuzhiyun bit of the result!!! */
1292*4882a593Smuzhiyun CONDITIONAL_SET_FLAG(res & 0x80, F_CF);
1293*4882a593Smuzhiyun }
1294*4882a593Smuzhiyun return (u8) res;
1295*4882a593Smuzhiyun }
1296*4882a593Smuzhiyun
1297*4882a593Smuzhiyun /****************************************************************************
1298*4882a593Smuzhiyun REMARKS:
1299*4882a593Smuzhiyun Implements the ROR instruction and side effects.
1300*4882a593Smuzhiyun ****************************************************************************/
1301*4882a593Smuzhiyun u16
ror_word(u16 d,u8 s)1302*4882a593Smuzhiyun ror_word(u16 d, u8 s)
1303*4882a593Smuzhiyun {
1304*4882a593Smuzhiyun register unsigned int res, cnt, mask;
1305*4882a593Smuzhiyun
1306*4882a593Smuzhiyun res = d;
1307*4882a593Smuzhiyun if ((cnt = s % 16) != 0) {
1308*4882a593Smuzhiyun res = (d << (16 - cnt));
1309*4882a593Smuzhiyun mask = (1 << (16 - cnt)) - 1;
1310*4882a593Smuzhiyun res |= (d >> (cnt)) & mask;
1311*4882a593Smuzhiyun CONDITIONAL_SET_FLAG(res & 0x8000, F_CF);
1312*4882a593Smuzhiyun CONDITIONAL_SET_FLAG(s == 1 && XOR2(res >> 14), F_OF);
1313*4882a593Smuzhiyun }
1314*4882a593Smuzhiyun else if (s != 0) {
1315*4882a593Smuzhiyun /* set the new carry flag, Note that it is the low order
1316*4882a593Smuzhiyun bit of the result!!! */
1317*4882a593Smuzhiyun CONDITIONAL_SET_FLAG(res & 0x8000, F_CF);
1318*4882a593Smuzhiyun }
1319*4882a593Smuzhiyun return (u16) res;
1320*4882a593Smuzhiyun }
1321*4882a593Smuzhiyun
1322*4882a593Smuzhiyun /****************************************************************************
1323*4882a593Smuzhiyun REMARKS:
1324*4882a593Smuzhiyun Implements the ROR instruction and side effects.
1325*4882a593Smuzhiyun ****************************************************************************/
1326*4882a593Smuzhiyun u32
ror_long(u32 d,u8 s)1327*4882a593Smuzhiyun ror_long(u32 d, u8 s)
1328*4882a593Smuzhiyun {
1329*4882a593Smuzhiyun register u32 res, cnt, mask;
1330*4882a593Smuzhiyun
1331*4882a593Smuzhiyun res = d;
1332*4882a593Smuzhiyun if ((cnt = s % 32) != 0) {
1333*4882a593Smuzhiyun res = (d << (32 - cnt));
1334*4882a593Smuzhiyun mask = (1 << (32 - cnt)) - 1;
1335*4882a593Smuzhiyun res |= (d >> (cnt)) & mask;
1336*4882a593Smuzhiyun CONDITIONAL_SET_FLAG(res & 0x80000000, F_CF);
1337*4882a593Smuzhiyun CONDITIONAL_SET_FLAG(s == 1 && XOR2(res >> 30), F_OF);
1338*4882a593Smuzhiyun }
1339*4882a593Smuzhiyun else if (s != 0) {
1340*4882a593Smuzhiyun /* set the new carry flag, Note that it is the low order
1341*4882a593Smuzhiyun bit of the result!!! */
1342*4882a593Smuzhiyun CONDITIONAL_SET_FLAG(res & 0x80000000, F_CF);
1343*4882a593Smuzhiyun }
1344*4882a593Smuzhiyun return res;
1345*4882a593Smuzhiyun }
1346*4882a593Smuzhiyun
1347*4882a593Smuzhiyun /****************************************************************************
1348*4882a593Smuzhiyun REMARKS:
1349*4882a593Smuzhiyun Implements the SHL instruction and side effects.
1350*4882a593Smuzhiyun ****************************************************************************/
1351*4882a593Smuzhiyun u8
shl_byte(u8 d,u8 s)1352*4882a593Smuzhiyun shl_byte(u8 d, u8 s)
1353*4882a593Smuzhiyun {
1354*4882a593Smuzhiyun unsigned int cnt, res, cf;
1355*4882a593Smuzhiyun
1356*4882a593Smuzhiyun if (s < 8) {
1357*4882a593Smuzhiyun cnt = s % 8;
1358*4882a593Smuzhiyun
1359*4882a593Smuzhiyun /* last bit shifted out goes into carry flag */
1360*4882a593Smuzhiyun if (cnt > 0) {
1361*4882a593Smuzhiyun res = d << cnt;
1362*4882a593Smuzhiyun cf = d & (1 << (8 - cnt));
1363*4882a593Smuzhiyun CONDITIONAL_SET_FLAG(cf, F_CF);
1364*4882a593Smuzhiyun CONDITIONAL_SET_FLAG((res & 0xff) == 0, F_ZF);
1365*4882a593Smuzhiyun CONDITIONAL_SET_FLAG(res & 0x80, F_SF);
1366*4882a593Smuzhiyun CONDITIONAL_SET_FLAG(PARITY(res & 0xff), F_PF);
1367*4882a593Smuzhiyun }
1368*4882a593Smuzhiyun else {
1369*4882a593Smuzhiyun res = (u8) d;
1370*4882a593Smuzhiyun }
1371*4882a593Smuzhiyun
1372*4882a593Smuzhiyun if (cnt == 1) {
1373*4882a593Smuzhiyun /* Needs simplification. */
1374*4882a593Smuzhiyun CONDITIONAL_SET_FLAG((((res & 0x80) == 0x80) ^
1375*4882a593Smuzhiyun (ACCESS_FLAG(F_CF) != 0)),
1376*4882a593Smuzhiyun /* was (M.x86.R_FLG&F_CF)==F_CF)), */
1377*4882a593Smuzhiyun F_OF);
1378*4882a593Smuzhiyun }
1379*4882a593Smuzhiyun else {
1380*4882a593Smuzhiyun CLEAR_FLAG(F_OF);
1381*4882a593Smuzhiyun }
1382*4882a593Smuzhiyun }
1383*4882a593Smuzhiyun else {
1384*4882a593Smuzhiyun res = 0;
1385*4882a593Smuzhiyun CONDITIONAL_SET_FLAG((d << (s - 1)) & 0x80, F_CF);
1386*4882a593Smuzhiyun CLEAR_FLAG(F_OF);
1387*4882a593Smuzhiyun CLEAR_FLAG(F_SF);
1388*4882a593Smuzhiyun SET_FLAG(F_PF);
1389*4882a593Smuzhiyun SET_FLAG(F_ZF);
1390*4882a593Smuzhiyun }
1391*4882a593Smuzhiyun return (u8) res;
1392*4882a593Smuzhiyun }
1393*4882a593Smuzhiyun
1394*4882a593Smuzhiyun /****************************************************************************
1395*4882a593Smuzhiyun REMARKS:
1396*4882a593Smuzhiyun Implements the SHL instruction and side effects.
1397*4882a593Smuzhiyun ****************************************************************************/
1398*4882a593Smuzhiyun u16
shl_word(u16 d,u8 s)1399*4882a593Smuzhiyun shl_word(u16 d, u8 s)
1400*4882a593Smuzhiyun {
1401*4882a593Smuzhiyun unsigned int cnt, res, cf;
1402*4882a593Smuzhiyun
1403*4882a593Smuzhiyun if (s < 16) {
1404*4882a593Smuzhiyun cnt = s % 16;
1405*4882a593Smuzhiyun if (cnt > 0) {
1406*4882a593Smuzhiyun res = d << cnt;
1407*4882a593Smuzhiyun cf = d & (1 << (16 - cnt));
1408*4882a593Smuzhiyun CONDITIONAL_SET_FLAG(cf, F_CF);
1409*4882a593Smuzhiyun CONDITIONAL_SET_FLAG((res & 0xffff) == 0, F_ZF);
1410*4882a593Smuzhiyun CONDITIONAL_SET_FLAG(res & 0x8000, F_SF);
1411*4882a593Smuzhiyun CONDITIONAL_SET_FLAG(PARITY(res & 0xff), F_PF);
1412*4882a593Smuzhiyun }
1413*4882a593Smuzhiyun else {
1414*4882a593Smuzhiyun res = (u16) d;
1415*4882a593Smuzhiyun }
1416*4882a593Smuzhiyun
1417*4882a593Smuzhiyun if (cnt == 1) {
1418*4882a593Smuzhiyun CONDITIONAL_SET_FLAG((((res & 0x8000) == 0x8000) ^
1419*4882a593Smuzhiyun (ACCESS_FLAG(F_CF) != 0)), F_OF);
1420*4882a593Smuzhiyun }
1421*4882a593Smuzhiyun else {
1422*4882a593Smuzhiyun CLEAR_FLAG(F_OF);
1423*4882a593Smuzhiyun }
1424*4882a593Smuzhiyun }
1425*4882a593Smuzhiyun else {
1426*4882a593Smuzhiyun res = 0;
1427*4882a593Smuzhiyun CONDITIONAL_SET_FLAG((d << (s - 1)) & 0x8000, F_CF);
1428*4882a593Smuzhiyun CLEAR_FLAG(F_OF);
1429*4882a593Smuzhiyun CLEAR_FLAG(F_SF);
1430*4882a593Smuzhiyun SET_FLAG(F_PF);
1431*4882a593Smuzhiyun SET_FLAG(F_ZF);
1432*4882a593Smuzhiyun }
1433*4882a593Smuzhiyun return (u16) res;
1434*4882a593Smuzhiyun }
1435*4882a593Smuzhiyun
1436*4882a593Smuzhiyun /****************************************************************************
1437*4882a593Smuzhiyun REMARKS:
1438*4882a593Smuzhiyun Implements the SHL instruction and side effects.
1439*4882a593Smuzhiyun ****************************************************************************/
1440*4882a593Smuzhiyun u32
shl_long(u32 d,u8 s)1441*4882a593Smuzhiyun shl_long(u32 d, u8 s)
1442*4882a593Smuzhiyun {
1443*4882a593Smuzhiyun unsigned int cnt, res, cf;
1444*4882a593Smuzhiyun
1445*4882a593Smuzhiyun if (s < 32) {
1446*4882a593Smuzhiyun cnt = s % 32;
1447*4882a593Smuzhiyun if (cnt > 0) {
1448*4882a593Smuzhiyun res = d << cnt;
1449*4882a593Smuzhiyun cf = d & (1 << (32 - cnt));
1450*4882a593Smuzhiyun CONDITIONAL_SET_FLAG(cf, F_CF);
1451*4882a593Smuzhiyun CONDITIONAL_SET_FLAG((res & 0xffffffff) == 0, F_ZF);
1452*4882a593Smuzhiyun CONDITIONAL_SET_FLAG(res & 0x80000000, F_SF);
1453*4882a593Smuzhiyun CONDITIONAL_SET_FLAG(PARITY(res & 0xff), F_PF);
1454*4882a593Smuzhiyun }
1455*4882a593Smuzhiyun else {
1456*4882a593Smuzhiyun res = d;
1457*4882a593Smuzhiyun }
1458*4882a593Smuzhiyun if (cnt == 1) {
1459*4882a593Smuzhiyun CONDITIONAL_SET_FLAG((((res & 0x80000000) == 0x80000000) ^
1460*4882a593Smuzhiyun (ACCESS_FLAG(F_CF) != 0)), F_OF);
1461*4882a593Smuzhiyun }
1462*4882a593Smuzhiyun else {
1463*4882a593Smuzhiyun CLEAR_FLAG(F_OF);
1464*4882a593Smuzhiyun }
1465*4882a593Smuzhiyun }
1466*4882a593Smuzhiyun else {
1467*4882a593Smuzhiyun res = 0;
1468*4882a593Smuzhiyun CONDITIONAL_SET_FLAG((d << (s - 1)) & 0x80000000, F_CF);
1469*4882a593Smuzhiyun CLEAR_FLAG(F_OF);
1470*4882a593Smuzhiyun CLEAR_FLAG(F_SF);
1471*4882a593Smuzhiyun SET_FLAG(F_PF);
1472*4882a593Smuzhiyun SET_FLAG(F_ZF);
1473*4882a593Smuzhiyun }
1474*4882a593Smuzhiyun return res;
1475*4882a593Smuzhiyun }
1476*4882a593Smuzhiyun
1477*4882a593Smuzhiyun /****************************************************************************
1478*4882a593Smuzhiyun REMARKS:
1479*4882a593Smuzhiyun Implements the SHR instruction and side effects.
1480*4882a593Smuzhiyun ****************************************************************************/
1481*4882a593Smuzhiyun u8
shr_byte(u8 d,u8 s)1482*4882a593Smuzhiyun shr_byte(u8 d, u8 s)
1483*4882a593Smuzhiyun {
1484*4882a593Smuzhiyun unsigned int cnt, res, cf;
1485*4882a593Smuzhiyun
1486*4882a593Smuzhiyun if (s < 8) {
1487*4882a593Smuzhiyun cnt = s % 8;
1488*4882a593Smuzhiyun if (cnt > 0) {
1489*4882a593Smuzhiyun cf = d & (1 << (cnt - 1));
1490*4882a593Smuzhiyun res = d >> cnt;
1491*4882a593Smuzhiyun CONDITIONAL_SET_FLAG(cf, F_CF);
1492*4882a593Smuzhiyun CONDITIONAL_SET_FLAG((res & 0xff) == 0, F_ZF);
1493*4882a593Smuzhiyun CONDITIONAL_SET_FLAG(res & 0x80, F_SF);
1494*4882a593Smuzhiyun CONDITIONAL_SET_FLAG(PARITY(res & 0xff), F_PF);
1495*4882a593Smuzhiyun }
1496*4882a593Smuzhiyun else {
1497*4882a593Smuzhiyun res = (u8) d;
1498*4882a593Smuzhiyun }
1499*4882a593Smuzhiyun
1500*4882a593Smuzhiyun if (cnt == 1) {
1501*4882a593Smuzhiyun CONDITIONAL_SET_FLAG(XOR2(res >> 6), F_OF);
1502*4882a593Smuzhiyun }
1503*4882a593Smuzhiyun else {
1504*4882a593Smuzhiyun CLEAR_FLAG(F_OF);
1505*4882a593Smuzhiyun }
1506*4882a593Smuzhiyun }
1507*4882a593Smuzhiyun else {
1508*4882a593Smuzhiyun res = 0;
1509*4882a593Smuzhiyun CONDITIONAL_SET_FLAG((d >> (s - 1)) & 0x1, F_CF);
1510*4882a593Smuzhiyun CLEAR_FLAG(F_OF);
1511*4882a593Smuzhiyun CLEAR_FLAG(F_SF);
1512*4882a593Smuzhiyun SET_FLAG(F_PF);
1513*4882a593Smuzhiyun SET_FLAG(F_ZF);
1514*4882a593Smuzhiyun }
1515*4882a593Smuzhiyun return (u8) res;
1516*4882a593Smuzhiyun }
1517*4882a593Smuzhiyun
1518*4882a593Smuzhiyun /****************************************************************************
1519*4882a593Smuzhiyun REMARKS:
1520*4882a593Smuzhiyun Implements the SHR instruction and side effects.
1521*4882a593Smuzhiyun ****************************************************************************/
1522*4882a593Smuzhiyun u16
shr_word(u16 d,u8 s)1523*4882a593Smuzhiyun shr_word(u16 d, u8 s)
1524*4882a593Smuzhiyun {
1525*4882a593Smuzhiyun unsigned int cnt, res, cf;
1526*4882a593Smuzhiyun
1527*4882a593Smuzhiyun if (s < 16) {
1528*4882a593Smuzhiyun cnt = s % 16;
1529*4882a593Smuzhiyun if (cnt > 0) {
1530*4882a593Smuzhiyun cf = d & (1 << (cnt - 1));
1531*4882a593Smuzhiyun res = d >> cnt;
1532*4882a593Smuzhiyun CONDITIONAL_SET_FLAG(cf, F_CF);
1533*4882a593Smuzhiyun CONDITIONAL_SET_FLAG((res & 0xffff) == 0, F_ZF);
1534*4882a593Smuzhiyun CONDITIONAL_SET_FLAG(res & 0x8000, F_SF);
1535*4882a593Smuzhiyun CONDITIONAL_SET_FLAG(PARITY(res & 0xff), F_PF);
1536*4882a593Smuzhiyun }
1537*4882a593Smuzhiyun else {
1538*4882a593Smuzhiyun res = d;
1539*4882a593Smuzhiyun }
1540*4882a593Smuzhiyun
1541*4882a593Smuzhiyun if (cnt == 1) {
1542*4882a593Smuzhiyun CONDITIONAL_SET_FLAG(XOR2(res >> 14), F_OF);
1543*4882a593Smuzhiyun }
1544*4882a593Smuzhiyun else {
1545*4882a593Smuzhiyun CLEAR_FLAG(F_OF);
1546*4882a593Smuzhiyun }
1547*4882a593Smuzhiyun }
1548*4882a593Smuzhiyun else {
1549*4882a593Smuzhiyun res = 0;
1550*4882a593Smuzhiyun CLEAR_FLAG(F_CF);
1551*4882a593Smuzhiyun CLEAR_FLAG(F_OF);
1552*4882a593Smuzhiyun SET_FLAG(F_ZF);
1553*4882a593Smuzhiyun CLEAR_FLAG(F_SF);
1554*4882a593Smuzhiyun CLEAR_FLAG(F_PF);
1555*4882a593Smuzhiyun }
1556*4882a593Smuzhiyun return (u16) res;
1557*4882a593Smuzhiyun }
1558*4882a593Smuzhiyun
1559*4882a593Smuzhiyun /****************************************************************************
1560*4882a593Smuzhiyun REMARKS:
1561*4882a593Smuzhiyun Implements the SHR instruction and side effects.
1562*4882a593Smuzhiyun ****************************************************************************/
1563*4882a593Smuzhiyun u32
shr_long(u32 d,u8 s)1564*4882a593Smuzhiyun shr_long(u32 d, u8 s)
1565*4882a593Smuzhiyun {
1566*4882a593Smuzhiyun unsigned int cnt, res, cf;
1567*4882a593Smuzhiyun
1568*4882a593Smuzhiyun if (s < 32) {
1569*4882a593Smuzhiyun cnt = s % 32;
1570*4882a593Smuzhiyun if (cnt > 0) {
1571*4882a593Smuzhiyun cf = d & (1 << (cnt - 1));
1572*4882a593Smuzhiyun res = d >> cnt;
1573*4882a593Smuzhiyun CONDITIONAL_SET_FLAG(cf, F_CF);
1574*4882a593Smuzhiyun CONDITIONAL_SET_FLAG((res & 0xffffffff) == 0, F_ZF);
1575*4882a593Smuzhiyun CONDITIONAL_SET_FLAG(res & 0x80000000, F_SF);
1576*4882a593Smuzhiyun CONDITIONAL_SET_FLAG(PARITY(res & 0xff), F_PF);
1577*4882a593Smuzhiyun }
1578*4882a593Smuzhiyun else {
1579*4882a593Smuzhiyun res = d;
1580*4882a593Smuzhiyun }
1581*4882a593Smuzhiyun if (cnt == 1) {
1582*4882a593Smuzhiyun CONDITIONAL_SET_FLAG(XOR2(res >> 30), F_OF);
1583*4882a593Smuzhiyun }
1584*4882a593Smuzhiyun else {
1585*4882a593Smuzhiyun CLEAR_FLAG(F_OF);
1586*4882a593Smuzhiyun }
1587*4882a593Smuzhiyun }
1588*4882a593Smuzhiyun else {
1589*4882a593Smuzhiyun res = 0;
1590*4882a593Smuzhiyun CLEAR_FLAG(F_CF);
1591*4882a593Smuzhiyun CLEAR_FLAG(F_OF);
1592*4882a593Smuzhiyun SET_FLAG(F_ZF);
1593*4882a593Smuzhiyun CLEAR_FLAG(F_SF);
1594*4882a593Smuzhiyun CLEAR_FLAG(F_PF);
1595*4882a593Smuzhiyun }
1596*4882a593Smuzhiyun return res;
1597*4882a593Smuzhiyun }
1598*4882a593Smuzhiyun
1599*4882a593Smuzhiyun /****************************************************************************
1600*4882a593Smuzhiyun REMARKS:
1601*4882a593Smuzhiyun Implements the SAR instruction and side effects.
1602*4882a593Smuzhiyun ****************************************************************************/
1603*4882a593Smuzhiyun u8
sar_byte(u8 d,u8 s)1604*4882a593Smuzhiyun sar_byte(u8 d, u8 s)
1605*4882a593Smuzhiyun {
1606*4882a593Smuzhiyun unsigned int cnt, res, cf, mask, sf;
1607*4882a593Smuzhiyun
1608*4882a593Smuzhiyun res = d;
1609*4882a593Smuzhiyun sf = d & 0x80;
1610*4882a593Smuzhiyun cnt = s % 8;
1611*4882a593Smuzhiyun if (cnt > 0 && cnt < 8) {
1612*4882a593Smuzhiyun mask = (1 << (8 - cnt)) - 1;
1613*4882a593Smuzhiyun cf = d & (1 << (cnt - 1));
1614*4882a593Smuzhiyun res = (d >> cnt) & mask;
1615*4882a593Smuzhiyun CONDITIONAL_SET_FLAG(cf, F_CF);
1616*4882a593Smuzhiyun if (sf) {
1617*4882a593Smuzhiyun res |= ~mask;
1618*4882a593Smuzhiyun }
1619*4882a593Smuzhiyun CONDITIONAL_SET_FLAG((res & 0xff) == 0, F_ZF);
1620*4882a593Smuzhiyun CONDITIONAL_SET_FLAG(PARITY(res & 0xff), F_PF);
1621*4882a593Smuzhiyun CONDITIONAL_SET_FLAG(res & 0x80, F_SF);
1622*4882a593Smuzhiyun }
1623*4882a593Smuzhiyun else if (cnt >= 8) {
1624*4882a593Smuzhiyun if (sf) {
1625*4882a593Smuzhiyun res = 0xff;
1626*4882a593Smuzhiyun SET_FLAG(F_CF);
1627*4882a593Smuzhiyun CLEAR_FLAG(F_ZF);
1628*4882a593Smuzhiyun SET_FLAG(F_SF);
1629*4882a593Smuzhiyun SET_FLAG(F_PF);
1630*4882a593Smuzhiyun }
1631*4882a593Smuzhiyun else {
1632*4882a593Smuzhiyun res = 0;
1633*4882a593Smuzhiyun CLEAR_FLAG(F_CF);
1634*4882a593Smuzhiyun SET_FLAG(F_ZF);
1635*4882a593Smuzhiyun CLEAR_FLAG(F_SF);
1636*4882a593Smuzhiyun CLEAR_FLAG(F_PF);
1637*4882a593Smuzhiyun }
1638*4882a593Smuzhiyun }
1639*4882a593Smuzhiyun return (u8) res;
1640*4882a593Smuzhiyun }
1641*4882a593Smuzhiyun
1642*4882a593Smuzhiyun /****************************************************************************
1643*4882a593Smuzhiyun REMARKS:
1644*4882a593Smuzhiyun Implements the SAR instruction and side effects.
1645*4882a593Smuzhiyun ****************************************************************************/
1646*4882a593Smuzhiyun u16
sar_word(u16 d,u8 s)1647*4882a593Smuzhiyun sar_word(u16 d, u8 s)
1648*4882a593Smuzhiyun {
1649*4882a593Smuzhiyun unsigned int cnt, res, cf, mask, sf;
1650*4882a593Smuzhiyun
1651*4882a593Smuzhiyun sf = d & 0x8000;
1652*4882a593Smuzhiyun cnt = s % 16;
1653*4882a593Smuzhiyun res = d;
1654*4882a593Smuzhiyun if (cnt > 0 && cnt < 16) {
1655*4882a593Smuzhiyun mask = (1 << (16 - cnt)) - 1;
1656*4882a593Smuzhiyun cf = d & (1 << (cnt - 1));
1657*4882a593Smuzhiyun res = (d >> cnt) & mask;
1658*4882a593Smuzhiyun CONDITIONAL_SET_FLAG(cf, F_CF);
1659*4882a593Smuzhiyun if (sf) {
1660*4882a593Smuzhiyun res |= ~mask;
1661*4882a593Smuzhiyun }
1662*4882a593Smuzhiyun CONDITIONAL_SET_FLAG((res & 0xffff) == 0, F_ZF);
1663*4882a593Smuzhiyun CONDITIONAL_SET_FLAG(res & 0x8000, F_SF);
1664*4882a593Smuzhiyun CONDITIONAL_SET_FLAG(PARITY(res & 0xff), F_PF);
1665*4882a593Smuzhiyun }
1666*4882a593Smuzhiyun else if (cnt >= 16) {
1667*4882a593Smuzhiyun if (sf) {
1668*4882a593Smuzhiyun res = 0xffff;
1669*4882a593Smuzhiyun SET_FLAG(F_CF);
1670*4882a593Smuzhiyun CLEAR_FLAG(F_ZF);
1671*4882a593Smuzhiyun SET_FLAG(F_SF);
1672*4882a593Smuzhiyun SET_FLAG(F_PF);
1673*4882a593Smuzhiyun }
1674*4882a593Smuzhiyun else {
1675*4882a593Smuzhiyun res = 0;
1676*4882a593Smuzhiyun CLEAR_FLAG(F_CF);
1677*4882a593Smuzhiyun SET_FLAG(F_ZF);
1678*4882a593Smuzhiyun CLEAR_FLAG(F_SF);
1679*4882a593Smuzhiyun CLEAR_FLAG(F_PF);
1680*4882a593Smuzhiyun }
1681*4882a593Smuzhiyun }
1682*4882a593Smuzhiyun return (u16) res;
1683*4882a593Smuzhiyun }
1684*4882a593Smuzhiyun
1685*4882a593Smuzhiyun /****************************************************************************
1686*4882a593Smuzhiyun REMARKS:
1687*4882a593Smuzhiyun Implements the SAR instruction and side effects.
1688*4882a593Smuzhiyun ****************************************************************************/
1689*4882a593Smuzhiyun u32
sar_long(u32 d,u8 s)1690*4882a593Smuzhiyun sar_long(u32 d, u8 s)
1691*4882a593Smuzhiyun {
1692*4882a593Smuzhiyun u32 cnt, res, cf, mask, sf;
1693*4882a593Smuzhiyun
1694*4882a593Smuzhiyun sf = d & 0x80000000;
1695*4882a593Smuzhiyun cnt = s % 32;
1696*4882a593Smuzhiyun res = d;
1697*4882a593Smuzhiyun if (cnt > 0 && cnt < 32) {
1698*4882a593Smuzhiyun mask = (1 << (32 - cnt)) - 1;
1699*4882a593Smuzhiyun cf = d & (1 << (cnt - 1));
1700*4882a593Smuzhiyun res = (d >> cnt) & mask;
1701*4882a593Smuzhiyun CONDITIONAL_SET_FLAG(cf, F_CF);
1702*4882a593Smuzhiyun if (sf) {
1703*4882a593Smuzhiyun res |= ~mask;
1704*4882a593Smuzhiyun }
1705*4882a593Smuzhiyun CONDITIONAL_SET_FLAG((res & 0xffffffff) == 0, F_ZF);
1706*4882a593Smuzhiyun CONDITIONAL_SET_FLAG(res & 0x80000000, F_SF);
1707*4882a593Smuzhiyun CONDITIONAL_SET_FLAG(PARITY(res & 0xff), F_PF);
1708*4882a593Smuzhiyun }
1709*4882a593Smuzhiyun else if (cnt >= 32) {
1710*4882a593Smuzhiyun if (sf) {
1711*4882a593Smuzhiyun res = 0xffffffff;
1712*4882a593Smuzhiyun SET_FLAG(F_CF);
1713*4882a593Smuzhiyun CLEAR_FLAG(F_ZF);
1714*4882a593Smuzhiyun SET_FLAG(F_SF);
1715*4882a593Smuzhiyun SET_FLAG(F_PF);
1716*4882a593Smuzhiyun }
1717*4882a593Smuzhiyun else {
1718*4882a593Smuzhiyun res = 0;
1719*4882a593Smuzhiyun CLEAR_FLAG(F_CF);
1720*4882a593Smuzhiyun SET_FLAG(F_ZF);
1721*4882a593Smuzhiyun CLEAR_FLAG(F_SF);
1722*4882a593Smuzhiyun CLEAR_FLAG(F_PF);
1723*4882a593Smuzhiyun }
1724*4882a593Smuzhiyun }
1725*4882a593Smuzhiyun return res;
1726*4882a593Smuzhiyun }
1727*4882a593Smuzhiyun
1728*4882a593Smuzhiyun /****************************************************************************
1729*4882a593Smuzhiyun REMARKS:
1730*4882a593Smuzhiyun Implements the SHLD instruction and side effects.
1731*4882a593Smuzhiyun ****************************************************************************/
1732*4882a593Smuzhiyun u16
shld_word(u16 d,u16 fill,u8 s)1733*4882a593Smuzhiyun shld_word(u16 d, u16 fill, u8 s)
1734*4882a593Smuzhiyun {
1735*4882a593Smuzhiyun unsigned int cnt, res, cf;
1736*4882a593Smuzhiyun
1737*4882a593Smuzhiyun if (s < 16) {
1738*4882a593Smuzhiyun cnt = s % 16;
1739*4882a593Smuzhiyun if (cnt > 0) {
1740*4882a593Smuzhiyun res = (d << cnt) | (fill >> (16 - cnt));
1741*4882a593Smuzhiyun cf = d & (1 << (16 - cnt));
1742*4882a593Smuzhiyun CONDITIONAL_SET_FLAG(cf, F_CF);
1743*4882a593Smuzhiyun CONDITIONAL_SET_FLAG((res & 0xffff) == 0, F_ZF);
1744*4882a593Smuzhiyun CONDITIONAL_SET_FLAG(res & 0x8000, F_SF);
1745*4882a593Smuzhiyun CONDITIONAL_SET_FLAG(PARITY(res & 0xff), F_PF);
1746*4882a593Smuzhiyun }
1747*4882a593Smuzhiyun else {
1748*4882a593Smuzhiyun res = d;
1749*4882a593Smuzhiyun }
1750*4882a593Smuzhiyun if (cnt == 1) {
1751*4882a593Smuzhiyun CONDITIONAL_SET_FLAG((((res & 0x8000) == 0x8000) ^
1752*4882a593Smuzhiyun (ACCESS_FLAG(F_CF) != 0)), F_OF);
1753*4882a593Smuzhiyun }
1754*4882a593Smuzhiyun else {
1755*4882a593Smuzhiyun CLEAR_FLAG(F_OF);
1756*4882a593Smuzhiyun }
1757*4882a593Smuzhiyun }
1758*4882a593Smuzhiyun else {
1759*4882a593Smuzhiyun res = 0;
1760*4882a593Smuzhiyun CONDITIONAL_SET_FLAG((d << (s - 1)) & 0x8000, F_CF);
1761*4882a593Smuzhiyun CLEAR_FLAG(F_OF);
1762*4882a593Smuzhiyun CLEAR_FLAG(F_SF);
1763*4882a593Smuzhiyun SET_FLAG(F_PF);
1764*4882a593Smuzhiyun SET_FLAG(F_ZF);
1765*4882a593Smuzhiyun }
1766*4882a593Smuzhiyun return (u16) res;
1767*4882a593Smuzhiyun }
1768*4882a593Smuzhiyun
1769*4882a593Smuzhiyun /****************************************************************************
1770*4882a593Smuzhiyun REMARKS:
1771*4882a593Smuzhiyun Implements the SHLD instruction and side effects.
1772*4882a593Smuzhiyun ****************************************************************************/
1773*4882a593Smuzhiyun u32
shld_long(u32 d,u32 fill,u8 s)1774*4882a593Smuzhiyun shld_long(u32 d, u32 fill, u8 s)
1775*4882a593Smuzhiyun {
1776*4882a593Smuzhiyun unsigned int cnt, res, cf;
1777*4882a593Smuzhiyun
1778*4882a593Smuzhiyun if (s < 32) {
1779*4882a593Smuzhiyun cnt = s % 32;
1780*4882a593Smuzhiyun if (cnt > 0) {
1781*4882a593Smuzhiyun res = (d << cnt) | (fill >> (32 - cnt));
1782*4882a593Smuzhiyun cf = d & (1 << (32 - cnt));
1783*4882a593Smuzhiyun CONDITIONAL_SET_FLAG(cf, F_CF);
1784*4882a593Smuzhiyun CONDITIONAL_SET_FLAG((res & 0xffffffff) == 0, F_ZF);
1785*4882a593Smuzhiyun CONDITIONAL_SET_FLAG(res & 0x80000000, F_SF);
1786*4882a593Smuzhiyun CONDITIONAL_SET_FLAG(PARITY(res & 0xff), F_PF);
1787*4882a593Smuzhiyun }
1788*4882a593Smuzhiyun else {
1789*4882a593Smuzhiyun res = d;
1790*4882a593Smuzhiyun }
1791*4882a593Smuzhiyun if (cnt == 1) {
1792*4882a593Smuzhiyun CONDITIONAL_SET_FLAG((((res & 0x80000000) == 0x80000000) ^
1793*4882a593Smuzhiyun (ACCESS_FLAG(F_CF) != 0)), F_OF);
1794*4882a593Smuzhiyun }
1795*4882a593Smuzhiyun else {
1796*4882a593Smuzhiyun CLEAR_FLAG(F_OF);
1797*4882a593Smuzhiyun }
1798*4882a593Smuzhiyun }
1799*4882a593Smuzhiyun else {
1800*4882a593Smuzhiyun res = 0;
1801*4882a593Smuzhiyun CONDITIONAL_SET_FLAG((d << (s - 1)) & 0x80000000, F_CF);
1802*4882a593Smuzhiyun CLEAR_FLAG(F_OF);
1803*4882a593Smuzhiyun CLEAR_FLAG(F_SF);
1804*4882a593Smuzhiyun SET_FLAG(F_PF);
1805*4882a593Smuzhiyun SET_FLAG(F_ZF);
1806*4882a593Smuzhiyun }
1807*4882a593Smuzhiyun return res;
1808*4882a593Smuzhiyun }
1809*4882a593Smuzhiyun
1810*4882a593Smuzhiyun /****************************************************************************
1811*4882a593Smuzhiyun REMARKS:
1812*4882a593Smuzhiyun Implements the SHRD instruction and side effects.
1813*4882a593Smuzhiyun ****************************************************************************/
1814*4882a593Smuzhiyun u16
shrd_word(u16 d,u16 fill,u8 s)1815*4882a593Smuzhiyun shrd_word(u16 d, u16 fill, u8 s)
1816*4882a593Smuzhiyun {
1817*4882a593Smuzhiyun unsigned int cnt, res, cf;
1818*4882a593Smuzhiyun
1819*4882a593Smuzhiyun if (s < 16) {
1820*4882a593Smuzhiyun cnt = s % 16;
1821*4882a593Smuzhiyun if (cnt > 0) {
1822*4882a593Smuzhiyun cf = d & (1 << (cnt - 1));
1823*4882a593Smuzhiyun res = (d >> cnt) | (fill << (16 - cnt));
1824*4882a593Smuzhiyun CONDITIONAL_SET_FLAG(cf, F_CF);
1825*4882a593Smuzhiyun CONDITIONAL_SET_FLAG((res & 0xffff) == 0, F_ZF);
1826*4882a593Smuzhiyun CONDITIONAL_SET_FLAG(res & 0x8000, F_SF);
1827*4882a593Smuzhiyun CONDITIONAL_SET_FLAG(PARITY(res & 0xff), F_PF);
1828*4882a593Smuzhiyun }
1829*4882a593Smuzhiyun else {
1830*4882a593Smuzhiyun res = d;
1831*4882a593Smuzhiyun }
1832*4882a593Smuzhiyun
1833*4882a593Smuzhiyun if (cnt == 1) {
1834*4882a593Smuzhiyun CONDITIONAL_SET_FLAG(XOR2(res >> 14), F_OF);
1835*4882a593Smuzhiyun }
1836*4882a593Smuzhiyun else {
1837*4882a593Smuzhiyun CLEAR_FLAG(F_OF);
1838*4882a593Smuzhiyun }
1839*4882a593Smuzhiyun }
1840*4882a593Smuzhiyun else {
1841*4882a593Smuzhiyun res = 0;
1842*4882a593Smuzhiyun CLEAR_FLAG(F_CF);
1843*4882a593Smuzhiyun CLEAR_FLAG(F_OF);
1844*4882a593Smuzhiyun SET_FLAG(F_ZF);
1845*4882a593Smuzhiyun CLEAR_FLAG(F_SF);
1846*4882a593Smuzhiyun CLEAR_FLAG(F_PF);
1847*4882a593Smuzhiyun }
1848*4882a593Smuzhiyun return (u16) res;
1849*4882a593Smuzhiyun }
1850*4882a593Smuzhiyun
1851*4882a593Smuzhiyun /****************************************************************************
1852*4882a593Smuzhiyun REMARKS:
1853*4882a593Smuzhiyun Implements the SHRD instruction and side effects.
1854*4882a593Smuzhiyun ****************************************************************************/
1855*4882a593Smuzhiyun u32
shrd_long(u32 d,u32 fill,u8 s)1856*4882a593Smuzhiyun shrd_long(u32 d, u32 fill, u8 s)
1857*4882a593Smuzhiyun {
1858*4882a593Smuzhiyun unsigned int cnt, res, cf;
1859*4882a593Smuzhiyun
1860*4882a593Smuzhiyun if (s < 32) {
1861*4882a593Smuzhiyun cnt = s % 32;
1862*4882a593Smuzhiyun if (cnt > 0) {
1863*4882a593Smuzhiyun cf = d & (1 << (cnt - 1));
1864*4882a593Smuzhiyun res = (d >> cnt) | (fill << (32 - cnt));
1865*4882a593Smuzhiyun CONDITIONAL_SET_FLAG(cf, F_CF);
1866*4882a593Smuzhiyun CONDITIONAL_SET_FLAG((res & 0xffffffff) == 0, F_ZF);
1867*4882a593Smuzhiyun CONDITIONAL_SET_FLAG(res & 0x80000000, F_SF);
1868*4882a593Smuzhiyun CONDITIONAL_SET_FLAG(PARITY(res & 0xff), F_PF);
1869*4882a593Smuzhiyun }
1870*4882a593Smuzhiyun else {
1871*4882a593Smuzhiyun res = d;
1872*4882a593Smuzhiyun }
1873*4882a593Smuzhiyun if (cnt == 1) {
1874*4882a593Smuzhiyun CONDITIONAL_SET_FLAG(XOR2(res >> 30), F_OF);
1875*4882a593Smuzhiyun }
1876*4882a593Smuzhiyun else {
1877*4882a593Smuzhiyun CLEAR_FLAG(F_OF);
1878*4882a593Smuzhiyun }
1879*4882a593Smuzhiyun }
1880*4882a593Smuzhiyun else {
1881*4882a593Smuzhiyun res = 0;
1882*4882a593Smuzhiyun CLEAR_FLAG(F_CF);
1883*4882a593Smuzhiyun CLEAR_FLAG(F_OF);
1884*4882a593Smuzhiyun SET_FLAG(F_ZF);
1885*4882a593Smuzhiyun CLEAR_FLAG(F_SF);
1886*4882a593Smuzhiyun CLEAR_FLAG(F_PF);
1887*4882a593Smuzhiyun }
1888*4882a593Smuzhiyun return res;
1889*4882a593Smuzhiyun }
1890*4882a593Smuzhiyun
1891*4882a593Smuzhiyun /****************************************************************************
1892*4882a593Smuzhiyun REMARKS:
1893*4882a593Smuzhiyun Implements the SBB instruction and side effects.
1894*4882a593Smuzhiyun ****************************************************************************/
1895*4882a593Smuzhiyun u8
sbb_byte(u8 d,u8 s)1896*4882a593Smuzhiyun sbb_byte(u8 d, u8 s)
1897*4882a593Smuzhiyun {
1898*4882a593Smuzhiyun register u32 res; /* all operands in native machine order */
1899*4882a593Smuzhiyun register u32 bc;
1900*4882a593Smuzhiyun
1901*4882a593Smuzhiyun if (ACCESS_FLAG(F_CF))
1902*4882a593Smuzhiyun res = d - s - 1;
1903*4882a593Smuzhiyun else
1904*4882a593Smuzhiyun res = d - s;
1905*4882a593Smuzhiyun CONDITIONAL_SET_FLAG(res & 0x80, F_SF);
1906*4882a593Smuzhiyun CONDITIONAL_SET_FLAG((res & 0xff) == 0, F_ZF);
1907*4882a593Smuzhiyun CONDITIONAL_SET_FLAG(PARITY(res & 0xff), F_PF);
1908*4882a593Smuzhiyun
1909*4882a593Smuzhiyun /* calculate the borrow chain. See note at top */
1910*4882a593Smuzhiyun bc = (res & (~d | s)) | (~d & s);
1911*4882a593Smuzhiyun CONDITIONAL_SET_FLAG(bc & 0x80, F_CF);
1912*4882a593Smuzhiyun CONDITIONAL_SET_FLAG(XOR2(bc >> 6), F_OF);
1913*4882a593Smuzhiyun CONDITIONAL_SET_FLAG(bc & 0x8, F_AF);
1914*4882a593Smuzhiyun return (u8) res;
1915*4882a593Smuzhiyun }
1916*4882a593Smuzhiyun
1917*4882a593Smuzhiyun /****************************************************************************
1918*4882a593Smuzhiyun REMARKS:
1919*4882a593Smuzhiyun Implements the SBB instruction and side effects.
1920*4882a593Smuzhiyun ****************************************************************************/
1921*4882a593Smuzhiyun u16
sbb_word(u16 d,u16 s)1922*4882a593Smuzhiyun sbb_word(u16 d, u16 s)
1923*4882a593Smuzhiyun {
1924*4882a593Smuzhiyun register u32 res; /* all operands in native machine order */
1925*4882a593Smuzhiyun register u32 bc;
1926*4882a593Smuzhiyun
1927*4882a593Smuzhiyun if (ACCESS_FLAG(F_CF))
1928*4882a593Smuzhiyun res = d - s - 1;
1929*4882a593Smuzhiyun else
1930*4882a593Smuzhiyun res = d - s;
1931*4882a593Smuzhiyun CONDITIONAL_SET_FLAG(res & 0x8000, F_SF);
1932*4882a593Smuzhiyun CONDITIONAL_SET_FLAG((res & 0xffff) == 0, F_ZF);
1933*4882a593Smuzhiyun CONDITIONAL_SET_FLAG(PARITY(res & 0xff), F_PF);
1934*4882a593Smuzhiyun
1935*4882a593Smuzhiyun /* calculate the borrow chain. See note at top */
1936*4882a593Smuzhiyun bc = (res & (~d | s)) | (~d & s);
1937*4882a593Smuzhiyun CONDITIONAL_SET_FLAG(bc & 0x8000, F_CF);
1938*4882a593Smuzhiyun CONDITIONAL_SET_FLAG(XOR2(bc >> 14), F_OF);
1939*4882a593Smuzhiyun CONDITIONAL_SET_FLAG(bc & 0x8, F_AF);
1940*4882a593Smuzhiyun return (u16) res;
1941*4882a593Smuzhiyun }
1942*4882a593Smuzhiyun
1943*4882a593Smuzhiyun /****************************************************************************
1944*4882a593Smuzhiyun REMARKS:
1945*4882a593Smuzhiyun Implements the SBB instruction and side effects.
1946*4882a593Smuzhiyun ****************************************************************************/
1947*4882a593Smuzhiyun u32
sbb_long(u32 d,u32 s)1948*4882a593Smuzhiyun sbb_long(u32 d, u32 s)
1949*4882a593Smuzhiyun {
1950*4882a593Smuzhiyun register u32 res; /* all operands in native machine order */
1951*4882a593Smuzhiyun register u32 bc;
1952*4882a593Smuzhiyun
1953*4882a593Smuzhiyun if (ACCESS_FLAG(F_CF))
1954*4882a593Smuzhiyun res = d - s - 1;
1955*4882a593Smuzhiyun else
1956*4882a593Smuzhiyun res = d - s;
1957*4882a593Smuzhiyun CONDITIONAL_SET_FLAG(res & 0x80000000, F_SF);
1958*4882a593Smuzhiyun CONDITIONAL_SET_FLAG((res & 0xffffffff) == 0, F_ZF);
1959*4882a593Smuzhiyun CONDITIONAL_SET_FLAG(PARITY(res & 0xff), F_PF);
1960*4882a593Smuzhiyun
1961*4882a593Smuzhiyun /* calculate the borrow chain. See note at top */
1962*4882a593Smuzhiyun bc = (res & (~d | s)) | (~d & s);
1963*4882a593Smuzhiyun CONDITIONAL_SET_FLAG(bc & 0x80000000, F_CF);
1964*4882a593Smuzhiyun CONDITIONAL_SET_FLAG(XOR2(bc >> 30), F_OF);
1965*4882a593Smuzhiyun CONDITIONAL_SET_FLAG(bc & 0x8, F_AF);
1966*4882a593Smuzhiyun return res;
1967*4882a593Smuzhiyun }
1968*4882a593Smuzhiyun
1969*4882a593Smuzhiyun /****************************************************************************
1970*4882a593Smuzhiyun REMARKS:
1971*4882a593Smuzhiyun Implements the SUB instruction and side effects.
1972*4882a593Smuzhiyun ****************************************************************************/
1973*4882a593Smuzhiyun u8
sub_byte(u8 d,u8 s)1974*4882a593Smuzhiyun sub_byte(u8 d, u8 s)
1975*4882a593Smuzhiyun {
1976*4882a593Smuzhiyun register u32 res; /* all operands in native machine order */
1977*4882a593Smuzhiyun register u32 bc;
1978*4882a593Smuzhiyun
1979*4882a593Smuzhiyun res = d - s;
1980*4882a593Smuzhiyun CONDITIONAL_SET_FLAG(res & 0x80, F_SF);
1981*4882a593Smuzhiyun CONDITIONAL_SET_FLAG((res & 0xff) == 0, F_ZF);
1982*4882a593Smuzhiyun CONDITIONAL_SET_FLAG(PARITY(res & 0xff), F_PF);
1983*4882a593Smuzhiyun
1984*4882a593Smuzhiyun /* calculate the borrow chain. See note at top */
1985*4882a593Smuzhiyun bc = (res & (~d | s)) | (~d & s);
1986*4882a593Smuzhiyun CONDITIONAL_SET_FLAG(bc & 0x80, F_CF);
1987*4882a593Smuzhiyun CONDITIONAL_SET_FLAG(XOR2(bc >> 6), F_OF);
1988*4882a593Smuzhiyun CONDITIONAL_SET_FLAG(bc & 0x8, F_AF);
1989*4882a593Smuzhiyun return (u8) res;
1990*4882a593Smuzhiyun }
1991*4882a593Smuzhiyun
1992*4882a593Smuzhiyun /****************************************************************************
1993*4882a593Smuzhiyun REMARKS:
1994*4882a593Smuzhiyun Implements the SUB instruction and side effects.
1995*4882a593Smuzhiyun ****************************************************************************/
1996*4882a593Smuzhiyun u16
sub_word(u16 d,u16 s)1997*4882a593Smuzhiyun sub_word(u16 d, u16 s)
1998*4882a593Smuzhiyun {
1999*4882a593Smuzhiyun register u32 res; /* all operands in native machine order */
2000*4882a593Smuzhiyun register u32 bc;
2001*4882a593Smuzhiyun
2002*4882a593Smuzhiyun res = d - s;
2003*4882a593Smuzhiyun CONDITIONAL_SET_FLAG(res & 0x8000, F_SF);
2004*4882a593Smuzhiyun CONDITIONAL_SET_FLAG((res & 0xffff) == 0, F_ZF);
2005*4882a593Smuzhiyun CONDITIONAL_SET_FLAG(PARITY(res & 0xff), F_PF);
2006*4882a593Smuzhiyun
2007*4882a593Smuzhiyun /* calculate the borrow chain. See note at top */
2008*4882a593Smuzhiyun bc = (res & (~d | s)) | (~d & s);
2009*4882a593Smuzhiyun CONDITIONAL_SET_FLAG(bc & 0x8000, F_CF);
2010*4882a593Smuzhiyun CONDITIONAL_SET_FLAG(XOR2(bc >> 14), F_OF);
2011*4882a593Smuzhiyun CONDITIONAL_SET_FLAG(bc & 0x8, F_AF);
2012*4882a593Smuzhiyun return (u16) res;
2013*4882a593Smuzhiyun }
2014*4882a593Smuzhiyun
2015*4882a593Smuzhiyun /****************************************************************************
2016*4882a593Smuzhiyun REMARKS:
2017*4882a593Smuzhiyun Implements the SUB instruction and side effects.
2018*4882a593Smuzhiyun ****************************************************************************/
2019*4882a593Smuzhiyun u32
sub_long(u32 d,u32 s)2020*4882a593Smuzhiyun sub_long(u32 d, u32 s)
2021*4882a593Smuzhiyun {
2022*4882a593Smuzhiyun register u32 res; /* all operands in native machine order */
2023*4882a593Smuzhiyun register u32 bc;
2024*4882a593Smuzhiyun
2025*4882a593Smuzhiyun res = d - s;
2026*4882a593Smuzhiyun CONDITIONAL_SET_FLAG(res & 0x80000000, F_SF);
2027*4882a593Smuzhiyun CONDITIONAL_SET_FLAG((res & 0xffffffff) == 0, F_ZF);
2028*4882a593Smuzhiyun CONDITIONAL_SET_FLAG(PARITY(res & 0xff), F_PF);
2029*4882a593Smuzhiyun
2030*4882a593Smuzhiyun /* calculate the borrow chain. See note at top */
2031*4882a593Smuzhiyun bc = (res & (~d | s)) | (~d & s);
2032*4882a593Smuzhiyun CONDITIONAL_SET_FLAG(bc & 0x80000000, F_CF);
2033*4882a593Smuzhiyun CONDITIONAL_SET_FLAG(XOR2(bc >> 30), F_OF);
2034*4882a593Smuzhiyun CONDITIONAL_SET_FLAG(bc & 0x8, F_AF);
2035*4882a593Smuzhiyun return res;
2036*4882a593Smuzhiyun }
2037*4882a593Smuzhiyun
2038*4882a593Smuzhiyun /****************************************************************************
2039*4882a593Smuzhiyun REMARKS:
2040*4882a593Smuzhiyun Implements the TEST instruction and side effects.
2041*4882a593Smuzhiyun ****************************************************************************/
2042*4882a593Smuzhiyun void
test_byte(u8 d,u8 s)2043*4882a593Smuzhiyun test_byte(u8 d, u8 s)
2044*4882a593Smuzhiyun {
2045*4882a593Smuzhiyun register u32 res; /* all operands in native machine order */
2046*4882a593Smuzhiyun
2047*4882a593Smuzhiyun res = d & s;
2048*4882a593Smuzhiyun
2049*4882a593Smuzhiyun CLEAR_FLAG(F_OF);
2050*4882a593Smuzhiyun CONDITIONAL_SET_FLAG(res & 0x80, F_SF);
2051*4882a593Smuzhiyun CONDITIONAL_SET_FLAG(res == 0, F_ZF);
2052*4882a593Smuzhiyun CONDITIONAL_SET_FLAG(PARITY(res & 0xff), F_PF);
2053*4882a593Smuzhiyun /* AF == dont care */
2054*4882a593Smuzhiyun CLEAR_FLAG(F_CF);
2055*4882a593Smuzhiyun }
2056*4882a593Smuzhiyun
2057*4882a593Smuzhiyun /****************************************************************************
2058*4882a593Smuzhiyun REMARKS:
2059*4882a593Smuzhiyun Implements the TEST instruction and side effects.
2060*4882a593Smuzhiyun ****************************************************************************/
2061*4882a593Smuzhiyun void
test_word(u16 d,u16 s)2062*4882a593Smuzhiyun test_word(u16 d, u16 s)
2063*4882a593Smuzhiyun {
2064*4882a593Smuzhiyun register u32 res; /* all operands in native machine order */
2065*4882a593Smuzhiyun
2066*4882a593Smuzhiyun res = d & s;
2067*4882a593Smuzhiyun
2068*4882a593Smuzhiyun CLEAR_FLAG(F_OF);
2069*4882a593Smuzhiyun CONDITIONAL_SET_FLAG(res & 0x8000, F_SF);
2070*4882a593Smuzhiyun CONDITIONAL_SET_FLAG(res == 0, F_ZF);
2071*4882a593Smuzhiyun CONDITIONAL_SET_FLAG(PARITY(res & 0xff), F_PF);
2072*4882a593Smuzhiyun /* AF == dont care */
2073*4882a593Smuzhiyun CLEAR_FLAG(F_CF);
2074*4882a593Smuzhiyun }
2075*4882a593Smuzhiyun
2076*4882a593Smuzhiyun /****************************************************************************
2077*4882a593Smuzhiyun REMARKS:
2078*4882a593Smuzhiyun Implements the TEST instruction and side effects.
2079*4882a593Smuzhiyun ****************************************************************************/
2080*4882a593Smuzhiyun void
test_long(u32 d,u32 s)2081*4882a593Smuzhiyun test_long(u32 d, u32 s)
2082*4882a593Smuzhiyun {
2083*4882a593Smuzhiyun register u32 res; /* all operands in native machine order */
2084*4882a593Smuzhiyun
2085*4882a593Smuzhiyun res = d & s;
2086*4882a593Smuzhiyun
2087*4882a593Smuzhiyun CLEAR_FLAG(F_OF);
2088*4882a593Smuzhiyun CONDITIONAL_SET_FLAG(res & 0x80000000, F_SF);
2089*4882a593Smuzhiyun CONDITIONAL_SET_FLAG(res == 0, F_ZF);
2090*4882a593Smuzhiyun CONDITIONAL_SET_FLAG(PARITY(res & 0xff), F_PF);
2091*4882a593Smuzhiyun /* AF == dont care */
2092*4882a593Smuzhiyun CLEAR_FLAG(F_CF);
2093*4882a593Smuzhiyun }
2094*4882a593Smuzhiyun
2095*4882a593Smuzhiyun /****************************************************************************
2096*4882a593Smuzhiyun REMARKS:
2097*4882a593Smuzhiyun Implements the XOR instruction and side effects.
2098*4882a593Smuzhiyun ****************************************************************************/
2099*4882a593Smuzhiyun u8
xor_byte(u8 d,u8 s)2100*4882a593Smuzhiyun xor_byte(u8 d, u8 s)
2101*4882a593Smuzhiyun {
2102*4882a593Smuzhiyun register u8 res; /* all operands in native machine order */
2103*4882a593Smuzhiyun
2104*4882a593Smuzhiyun res = d ^ s;
2105*4882a593Smuzhiyun CLEAR_FLAG(F_OF);
2106*4882a593Smuzhiyun CONDITIONAL_SET_FLAG(res & 0x80, F_SF);
2107*4882a593Smuzhiyun CONDITIONAL_SET_FLAG(res == 0, F_ZF);
2108*4882a593Smuzhiyun CONDITIONAL_SET_FLAG(PARITY(res), F_PF);
2109*4882a593Smuzhiyun CLEAR_FLAG(F_CF);
2110*4882a593Smuzhiyun CLEAR_FLAG(F_AF);
2111*4882a593Smuzhiyun return res;
2112*4882a593Smuzhiyun }
2113*4882a593Smuzhiyun
2114*4882a593Smuzhiyun /****************************************************************************
2115*4882a593Smuzhiyun REMARKS:
2116*4882a593Smuzhiyun Implements the XOR instruction and side effects.
2117*4882a593Smuzhiyun ****************************************************************************/
2118*4882a593Smuzhiyun u16
xor_word(u16 d,u16 s)2119*4882a593Smuzhiyun xor_word(u16 d, u16 s)
2120*4882a593Smuzhiyun {
2121*4882a593Smuzhiyun register u16 res; /* all operands in native machine order */
2122*4882a593Smuzhiyun
2123*4882a593Smuzhiyun res = d ^ s;
2124*4882a593Smuzhiyun CLEAR_FLAG(F_OF);
2125*4882a593Smuzhiyun CONDITIONAL_SET_FLAG(res & 0x8000, F_SF);
2126*4882a593Smuzhiyun CONDITIONAL_SET_FLAG(res == 0, F_ZF);
2127*4882a593Smuzhiyun CONDITIONAL_SET_FLAG(PARITY(res & 0xff), F_PF);
2128*4882a593Smuzhiyun CLEAR_FLAG(F_CF);
2129*4882a593Smuzhiyun CLEAR_FLAG(F_AF);
2130*4882a593Smuzhiyun return res;
2131*4882a593Smuzhiyun }
2132*4882a593Smuzhiyun
2133*4882a593Smuzhiyun /****************************************************************************
2134*4882a593Smuzhiyun REMARKS:
2135*4882a593Smuzhiyun Implements the XOR instruction and side effects.
2136*4882a593Smuzhiyun ****************************************************************************/
2137*4882a593Smuzhiyun u32
xor_long(u32 d,u32 s)2138*4882a593Smuzhiyun xor_long(u32 d, u32 s)
2139*4882a593Smuzhiyun {
2140*4882a593Smuzhiyun register u32 res; /* all operands in native machine order */
2141*4882a593Smuzhiyun
2142*4882a593Smuzhiyun res = d ^ s;
2143*4882a593Smuzhiyun CLEAR_FLAG(F_OF);
2144*4882a593Smuzhiyun CONDITIONAL_SET_FLAG(res & 0x80000000, F_SF);
2145*4882a593Smuzhiyun CONDITIONAL_SET_FLAG(res == 0, F_ZF);
2146*4882a593Smuzhiyun CONDITIONAL_SET_FLAG(PARITY(res & 0xff), F_PF);
2147*4882a593Smuzhiyun CLEAR_FLAG(F_CF);
2148*4882a593Smuzhiyun CLEAR_FLAG(F_AF);
2149*4882a593Smuzhiyun return res;
2150*4882a593Smuzhiyun }
2151*4882a593Smuzhiyun
2152*4882a593Smuzhiyun /****************************************************************************
2153*4882a593Smuzhiyun REMARKS:
2154*4882a593Smuzhiyun Implements the IMUL instruction and side effects.
2155*4882a593Smuzhiyun ****************************************************************************/
2156*4882a593Smuzhiyun void
imul_byte(u8 s)2157*4882a593Smuzhiyun imul_byte(u8 s)
2158*4882a593Smuzhiyun {
2159*4882a593Smuzhiyun s16 res = (s16) ((s8) M.x86.R_AL * (s8) s);
2160*4882a593Smuzhiyun
2161*4882a593Smuzhiyun M.x86.R_AX = res;
2162*4882a593Smuzhiyun if (((M.x86.R_AL & 0x80) == 0 && M.x86.R_AH == 0x00) ||
2163*4882a593Smuzhiyun ((M.x86.R_AL & 0x80) != 0 && M.x86.R_AH == 0xFF)) {
2164*4882a593Smuzhiyun CLEAR_FLAG(F_CF);
2165*4882a593Smuzhiyun CLEAR_FLAG(F_OF);
2166*4882a593Smuzhiyun }
2167*4882a593Smuzhiyun else {
2168*4882a593Smuzhiyun SET_FLAG(F_CF);
2169*4882a593Smuzhiyun SET_FLAG(F_OF);
2170*4882a593Smuzhiyun }
2171*4882a593Smuzhiyun }
2172*4882a593Smuzhiyun
2173*4882a593Smuzhiyun /****************************************************************************
2174*4882a593Smuzhiyun REMARKS:
2175*4882a593Smuzhiyun Implements the IMUL instruction and side effects.
2176*4882a593Smuzhiyun ****************************************************************************/
2177*4882a593Smuzhiyun void
imul_word(u16 s)2178*4882a593Smuzhiyun imul_word(u16 s)
2179*4882a593Smuzhiyun {
2180*4882a593Smuzhiyun s32 res = (s16) M.x86.R_AX * (s16) s;
2181*4882a593Smuzhiyun
2182*4882a593Smuzhiyun M.x86.R_AX = (u16) res;
2183*4882a593Smuzhiyun M.x86.R_DX = (u16) (res >> 16);
2184*4882a593Smuzhiyun if (((M.x86.R_AX & 0x8000) == 0 && M.x86.R_DX == 0x00) ||
2185*4882a593Smuzhiyun ((M.x86.R_AX & 0x8000) != 0 && M.x86.R_DX == 0xFF)) {
2186*4882a593Smuzhiyun CLEAR_FLAG(F_CF);
2187*4882a593Smuzhiyun CLEAR_FLAG(F_OF);
2188*4882a593Smuzhiyun }
2189*4882a593Smuzhiyun else {
2190*4882a593Smuzhiyun SET_FLAG(F_CF);
2191*4882a593Smuzhiyun SET_FLAG(F_OF);
2192*4882a593Smuzhiyun }
2193*4882a593Smuzhiyun }
2194*4882a593Smuzhiyun
2195*4882a593Smuzhiyun /****************************************************************************
2196*4882a593Smuzhiyun REMARKS:
2197*4882a593Smuzhiyun Implements the IMUL instruction and side effects.
2198*4882a593Smuzhiyun ****************************************************************************/
2199*4882a593Smuzhiyun void
imul_long_direct(u32 * res_lo,u32 * res_hi,u32 d,u32 s)2200*4882a593Smuzhiyun imul_long_direct(u32 * res_lo, u32 * res_hi, u32 d, u32 s)
2201*4882a593Smuzhiyun {
2202*4882a593Smuzhiyun #ifdef __HAS_LONG_LONG__
2203*4882a593Smuzhiyun s64 res = (s64) (s32) d * (s32) s;
2204*4882a593Smuzhiyun
2205*4882a593Smuzhiyun *res_lo = (u32) res;
2206*4882a593Smuzhiyun *res_hi = (u32) (res >> 32);
2207*4882a593Smuzhiyun #else
2208*4882a593Smuzhiyun u32 d_lo, d_hi, d_sign;
2209*4882a593Smuzhiyun u32 s_lo, s_hi, s_sign;
2210*4882a593Smuzhiyun u32 rlo_lo, rlo_hi, rhi_lo;
2211*4882a593Smuzhiyun
2212*4882a593Smuzhiyun if ((d_sign = d & 0x80000000) != 0)
2213*4882a593Smuzhiyun d = -d;
2214*4882a593Smuzhiyun d_lo = d & 0xFFFF;
2215*4882a593Smuzhiyun d_hi = d >> 16;
2216*4882a593Smuzhiyun if ((s_sign = s & 0x80000000) != 0)
2217*4882a593Smuzhiyun s = -s;
2218*4882a593Smuzhiyun s_lo = s & 0xFFFF;
2219*4882a593Smuzhiyun s_hi = s >> 16;
2220*4882a593Smuzhiyun rlo_lo = d_lo * s_lo;
2221*4882a593Smuzhiyun rlo_hi = (d_hi * s_lo + d_lo * s_hi) + (rlo_lo >> 16);
2222*4882a593Smuzhiyun rhi_lo = d_hi * s_hi + (rlo_hi >> 16);
2223*4882a593Smuzhiyun *res_lo = (rlo_hi << 16) | (rlo_lo & 0xFFFF);
2224*4882a593Smuzhiyun *res_hi = rhi_lo;
2225*4882a593Smuzhiyun if (d_sign != s_sign) {
2226*4882a593Smuzhiyun d = ~*res_lo;
2227*4882a593Smuzhiyun s = (((d & 0xFFFF) + 1) >> 16) + (d >> 16);
2228*4882a593Smuzhiyun *res_lo = ~*res_lo + 1;
2229*4882a593Smuzhiyun *res_hi = ~*res_hi + (s >> 16);
2230*4882a593Smuzhiyun }
2231*4882a593Smuzhiyun #endif
2232*4882a593Smuzhiyun }
2233*4882a593Smuzhiyun
2234*4882a593Smuzhiyun /****************************************************************************
2235*4882a593Smuzhiyun REMARKS:
2236*4882a593Smuzhiyun Implements the IMUL instruction and side effects.
2237*4882a593Smuzhiyun ****************************************************************************/
2238*4882a593Smuzhiyun void
imul_long(u32 s)2239*4882a593Smuzhiyun imul_long(u32 s)
2240*4882a593Smuzhiyun {
2241*4882a593Smuzhiyun imul_long_direct(&M.x86.R_EAX, &M.x86.R_EDX, M.x86.R_EAX, s);
2242*4882a593Smuzhiyun if (((M.x86.R_EAX & 0x80000000) == 0 && M.x86.R_EDX == 0x00) ||
2243*4882a593Smuzhiyun ((M.x86.R_EAX & 0x80000000) != 0 && M.x86.R_EDX == 0xFF)) {
2244*4882a593Smuzhiyun CLEAR_FLAG(F_CF);
2245*4882a593Smuzhiyun CLEAR_FLAG(F_OF);
2246*4882a593Smuzhiyun }
2247*4882a593Smuzhiyun else {
2248*4882a593Smuzhiyun SET_FLAG(F_CF);
2249*4882a593Smuzhiyun SET_FLAG(F_OF);
2250*4882a593Smuzhiyun }
2251*4882a593Smuzhiyun }
2252*4882a593Smuzhiyun
2253*4882a593Smuzhiyun /****************************************************************************
2254*4882a593Smuzhiyun REMARKS:
2255*4882a593Smuzhiyun Implements the MUL instruction and side effects.
2256*4882a593Smuzhiyun ****************************************************************************/
2257*4882a593Smuzhiyun void
mul_byte(u8 s)2258*4882a593Smuzhiyun mul_byte(u8 s)
2259*4882a593Smuzhiyun {
2260*4882a593Smuzhiyun u16 res = (u16) (M.x86.R_AL * s);
2261*4882a593Smuzhiyun
2262*4882a593Smuzhiyun M.x86.R_AX = res;
2263*4882a593Smuzhiyun if (M.x86.R_AH == 0) {
2264*4882a593Smuzhiyun CLEAR_FLAG(F_CF);
2265*4882a593Smuzhiyun CLEAR_FLAG(F_OF);
2266*4882a593Smuzhiyun }
2267*4882a593Smuzhiyun else {
2268*4882a593Smuzhiyun SET_FLAG(F_CF);
2269*4882a593Smuzhiyun SET_FLAG(F_OF);
2270*4882a593Smuzhiyun }
2271*4882a593Smuzhiyun }
2272*4882a593Smuzhiyun
2273*4882a593Smuzhiyun /****************************************************************************
2274*4882a593Smuzhiyun REMARKS:
2275*4882a593Smuzhiyun Implements the MUL instruction and side effects.
2276*4882a593Smuzhiyun ****************************************************************************/
2277*4882a593Smuzhiyun void
mul_word(u16 s)2278*4882a593Smuzhiyun mul_word(u16 s)
2279*4882a593Smuzhiyun {
2280*4882a593Smuzhiyun u32 res = M.x86.R_AX * s;
2281*4882a593Smuzhiyun
2282*4882a593Smuzhiyun M.x86.R_AX = (u16) res;
2283*4882a593Smuzhiyun M.x86.R_DX = (u16) (res >> 16);
2284*4882a593Smuzhiyun if (M.x86.R_DX == 0) {
2285*4882a593Smuzhiyun CLEAR_FLAG(F_CF);
2286*4882a593Smuzhiyun CLEAR_FLAG(F_OF);
2287*4882a593Smuzhiyun }
2288*4882a593Smuzhiyun else {
2289*4882a593Smuzhiyun SET_FLAG(F_CF);
2290*4882a593Smuzhiyun SET_FLAG(F_OF);
2291*4882a593Smuzhiyun }
2292*4882a593Smuzhiyun }
2293*4882a593Smuzhiyun
2294*4882a593Smuzhiyun /****************************************************************************
2295*4882a593Smuzhiyun REMARKS:
2296*4882a593Smuzhiyun Implements the MUL instruction and side effects.
2297*4882a593Smuzhiyun ****************************************************************************/
2298*4882a593Smuzhiyun void
mul_long(u32 s)2299*4882a593Smuzhiyun mul_long(u32 s)
2300*4882a593Smuzhiyun {
2301*4882a593Smuzhiyun #ifdef __HAS_LONG_LONG__
2302*4882a593Smuzhiyun u64 res = (u64) M.x86.R_EAX * s;
2303*4882a593Smuzhiyun
2304*4882a593Smuzhiyun M.x86.R_EAX = (u32) res;
2305*4882a593Smuzhiyun M.x86.R_EDX = (u32) (res >> 32);
2306*4882a593Smuzhiyun #else
2307*4882a593Smuzhiyun u32 a, a_lo, a_hi;
2308*4882a593Smuzhiyun u32 s_lo, s_hi;
2309*4882a593Smuzhiyun u32 rlo_lo, rlo_hi, rhi_lo;
2310*4882a593Smuzhiyun
2311*4882a593Smuzhiyun a = M.x86.R_EAX;
2312*4882a593Smuzhiyun a_lo = a & 0xFFFF;
2313*4882a593Smuzhiyun a_hi = a >> 16;
2314*4882a593Smuzhiyun s_lo = s & 0xFFFF;
2315*4882a593Smuzhiyun s_hi = s >> 16;
2316*4882a593Smuzhiyun rlo_lo = a_lo * s_lo;
2317*4882a593Smuzhiyun rlo_hi = (a_hi * s_lo + a_lo * s_hi) + (rlo_lo >> 16);
2318*4882a593Smuzhiyun rhi_lo = a_hi * s_hi + (rlo_hi >> 16);
2319*4882a593Smuzhiyun M.x86.R_EAX = (rlo_hi << 16) | (rlo_lo & 0xFFFF);
2320*4882a593Smuzhiyun M.x86.R_EDX = rhi_lo;
2321*4882a593Smuzhiyun #endif
2322*4882a593Smuzhiyun
2323*4882a593Smuzhiyun if (M.x86.R_EDX == 0) {
2324*4882a593Smuzhiyun CLEAR_FLAG(F_CF);
2325*4882a593Smuzhiyun CLEAR_FLAG(F_OF);
2326*4882a593Smuzhiyun }
2327*4882a593Smuzhiyun else {
2328*4882a593Smuzhiyun SET_FLAG(F_CF);
2329*4882a593Smuzhiyun SET_FLAG(F_OF);
2330*4882a593Smuzhiyun }
2331*4882a593Smuzhiyun }
2332*4882a593Smuzhiyun
2333*4882a593Smuzhiyun /****************************************************************************
2334*4882a593Smuzhiyun REMARKS:
2335*4882a593Smuzhiyun Implements the IDIV instruction and side effects.
2336*4882a593Smuzhiyun ****************************************************************************/
2337*4882a593Smuzhiyun void
idiv_byte(u8 s)2338*4882a593Smuzhiyun idiv_byte(u8 s)
2339*4882a593Smuzhiyun {
2340*4882a593Smuzhiyun s32 dvd, div, mod;
2341*4882a593Smuzhiyun
2342*4882a593Smuzhiyun dvd = (s16) M.x86.R_AX;
2343*4882a593Smuzhiyun if (s == 0) {
2344*4882a593Smuzhiyun x86emu_intr_raise(0);
2345*4882a593Smuzhiyun return;
2346*4882a593Smuzhiyun }
2347*4882a593Smuzhiyun div = dvd / (s8) s;
2348*4882a593Smuzhiyun mod = dvd % (s8) s;
2349*4882a593Smuzhiyun if (abs(div) > 0x7f) {
2350*4882a593Smuzhiyun x86emu_intr_raise(0);
2351*4882a593Smuzhiyun return;
2352*4882a593Smuzhiyun }
2353*4882a593Smuzhiyun M.x86.R_AL = (s8) div;
2354*4882a593Smuzhiyun M.x86.R_AH = (s8) mod;
2355*4882a593Smuzhiyun }
2356*4882a593Smuzhiyun
2357*4882a593Smuzhiyun /****************************************************************************
2358*4882a593Smuzhiyun REMARKS:
2359*4882a593Smuzhiyun Implements the IDIV instruction and side effects.
2360*4882a593Smuzhiyun ****************************************************************************/
2361*4882a593Smuzhiyun void
idiv_word(u16 s)2362*4882a593Smuzhiyun idiv_word(u16 s)
2363*4882a593Smuzhiyun {
2364*4882a593Smuzhiyun s32 dvd, div, mod;
2365*4882a593Smuzhiyun
2366*4882a593Smuzhiyun dvd = (((s32) M.x86.R_DX) << 16) | M.x86.R_AX;
2367*4882a593Smuzhiyun if (s == 0) {
2368*4882a593Smuzhiyun x86emu_intr_raise(0);
2369*4882a593Smuzhiyun return;
2370*4882a593Smuzhiyun }
2371*4882a593Smuzhiyun div = dvd / (s16) s;
2372*4882a593Smuzhiyun mod = dvd % (s16) s;
2373*4882a593Smuzhiyun if (abs(div) > 0x7fff) {
2374*4882a593Smuzhiyun x86emu_intr_raise(0);
2375*4882a593Smuzhiyun return;
2376*4882a593Smuzhiyun }
2377*4882a593Smuzhiyun CLEAR_FLAG(F_CF);
2378*4882a593Smuzhiyun CLEAR_FLAG(F_SF);
2379*4882a593Smuzhiyun CONDITIONAL_SET_FLAG(div == 0, F_ZF);
2380*4882a593Smuzhiyun CONDITIONAL_SET_FLAG(PARITY(mod & 0xff), F_PF);
2381*4882a593Smuzhiyun
2382*4882a593Smuzhiyun M.x86.R_AX = (u16) div;
2383*4882a593Smuzhiyun M.x86.R_DX = (u16) mod;
2384*4882a593Smuzhiyun }
2385*4882a593Smuzhiyun
2386*4882a593Smuzhiyun /****************************************************************************
2387*4882a593Smuzhiyun REMARKS:
2388*4882a593Smuzhiyun Implements the IDIV instruction and side effects.
2389*4882a593Smuzhiyun ****************************************************************************/
2390*4882a593Smuzhiyun void
idiv_long(u32 s)2391*4882a593Smuzhiyun idiv_long(u32 s)
2392*4882a593Smuzhiyun {
2393*4882a593Smuzhiyun #ifdef __HAS_LONG_LONG__
2394*4882a593Smuzhiyun s64 dvd, div, mod;
2395*4882a593Smuzhiyun
2396*4882a593Smuzhiyun dvd = (((s64) M.x86.R_EDX) << 32) | M.x86.R_EAX;
2397*4882a593Smuzhiyun if (s == 0) {
2398*4882a593Smuzhiyun x86emu_intr_raise(0);
2399*4882a593Smuzhiyun return;
2400*4882a593Smuzhiyun }
2401*4882a593Smuzhiyun div = dvd / (s32) s;
2402*4882a593Smuzhiyun mod = dvd % (s32) s;
2403*4882a593Smuzhiyun if (abs(div) > 0x7fffffff) {
2404*4882a593Smuzhiyun x86emu_intr_raise(0);
2405*4882a593Smuzhiyun return;
2406*4882a593Smuzhiyun }
2407*4882a593Smuzhiyun #else
2408*4882a593Smuzhiyun s32 div = 0, mod;
2409*4882a593Smuzhiyun s32 h_dvd = M.x86.R_EDX;
2410*4882a593Smuzhiyun u32 l_dvd = M.x86.R_EAX;
2411*4882a593Smuzhiyun u32 abs_s = s & 0x7FFFFFFF;
2412*4882a593Smuzhiyun u32 abs_h_dvd = h_dvd & 0x7FFFFFFF;
2413*4882a593Smuzhiyun u32 h_s = abs_s >> 1;
2414*4882a593Smuzhiyun u32 l_s = abs_s << 31;
2415*4882a593Smuzhiyun int counter = 31;
2416*4882a593Smuzhiyun int carry;
2417*4882a593Smuzhiyun
2418*4882a593Smuzhiyun if (s == 0) {
2419*4882a593Smuzhiyun x86emu_intr_raise(0);
2420*4882a593Smuzhiyun return;
2421*4882a593Smuzhiyun }
2422*4882a593Smuzhiyun do {
2423*4882a593Smuzhiyun div <<= 1;
2424*4882a593Smuzhiyun carry = (l_dvd >= l_s) ? 0 : 1;
2425*4882a593Smuzhiyun
2426*4882a593Smuzhiyun if (abs_h_dvd < (h_s + carry)) {
2427*4882a593Smuzhiyun h_s >>= 1;
2428*4882a593Smuzhiyun l_s = abs_s << (--counter);
2429*4882a593Smuzhiyun continue;
2430*4882a593Smuzhiyun }
2431*4882a593Smuzhiyun else {
2432*4882a593Smuzhiyun abs_h_dvd -= (h_s + carry);
2433*4882a593Smuzhiyun l_dvd = carry ? ((0xFFFFFFFF - l_s) + l_dvd + 1)
2434*4882a593Smuzhiyun : (l_dvd - l_s);
2435*4882a593Smuzhiyun h_s >>= 1;
2436*4882a593Smuzhiyun l_s = abs_s << (--counter);
2437*4882a593Smuzhiyun div |= 1;
2438*4882a593Smuzhiyun continue;
2439*4882a593Smuzhiyun }
2440*4882a593Smuzhiyun
2441*4882a593Smuzhiyun } while (counter > -1);
2442*4882a593Smuzhiyun /* overflow */
2443*4882a593Smuzhiyun if (abs_h_dvd || (l_dvd > abs_s)) {
2444*4882a593Smuzhiyun x86emu_intr_raise(0);
2445*4882a593Smuzhiyun return;
2446*4882a593Smuzhiyun }
2447*4882a593Smuzhiyun /* sign */
2448*4882a593Smuzhiyun div |= ((h_dvd & 0x10000000) ^ (s & 0x10000000));
2449*4882a593Smuzhiyun mod = l_dvd;
2450*4882a593Smuzhiyun
2451*4882a593Smuzhiyun #endif
2452*4882a593Smuzhiyun CLEAR_FLAG(F_CF);
2453*4882a593Smuzhiyun CLEAR_FLAG(F_AF);
2454*4882a593Smuzhiyun CLEAR_FLAG(F_SF);
2455*4882a593Smuzhiyun SET_FLAG(F_ZF);
2456*4882a593Smuzhiyun CONDITIONAL_SET_FLAG(PARITY(mod & 0xff), F_PF);
2457*4882a593Smuzhiyun
2458*4882a593Smuzhiyun M.x86.R_EAX = (u32) div;
2459*4882a593Smuzhiyun M.x86.R_EDX = (u32) mod;
2460*4882a593Smuzhiyun }
2461*4882a593Smuzhiyun
2462*4882a593Smuzhiyun /****************************************************************************
2463*4882a593Smuzhiyun REMARKS:
2464*4882a593Smuzhiyun Implements the DIV instruction and side effects.
2465*4882a593Smuzhiyun ****************************************************************************/
2466*4882a593Smuzhiyun void
div_byte(u8 s)2467*4882a593Smuzhiyun div_byte(u8 s)
2468*4882a593Smuzhiyun {
2469*4882a593Smuzhiyun u32 dvd, div, mod;
2470*4882a593Smuzhiyun
2471*4882a593Smuzhiyun dvd = M.x86.R_AX;
2472*4882a593Smuzhiyun if (s == 0) {
2473*4882a593Smuzhiyun x86emu_intr_raise(0);
2474*4882a593Smuzhiyun return;
2475*4882a593Smuzhiyun }
2476*4882a593Smuzhiyun div = dvd / (u8) s;
2477*4882a593Smuzhiyun mod = dvd % (u8) s;
2478*4882a593Smuzhiyun if (abs(div) > 0xff) {
2479*4882a593Smuzhiyun x86emu_intr_raise(0);
2480*4882a593Smuzhiyun return;
2481*4882a593Smuzhiyun }
2482*4882a593Smuzhiyun M.x86.R_AL = (u8) div;
2483*4882a593Smuzhiyun M.x86.R_AH = (u8) mod;
2484*4882a593Smuzhiyun }
2485*4882a593Smuzhiyun
2486*4882a593Smuzhiyun /****************************************************************************
2487*4882a593Smuzhiyun REMARKS:
2488*4882a593Smuzhiyun Implements the DIV instruction and side effects.
2489*4882a593Smuzhiyun ****************************************************************************/
2490*4882a593Smuzhiyun void
div_word(u16 s)2491*4882a593Smuzhiyun div_word(u16 s)
2492*4882a593Smuzhiyun {
2493*4882a593Smuzhiyun u32 dvd, div, mod;
2494*4882a593Smuzhiyun
2495*4882a593Smuzhiyun dvd = (((u32) M.x86.R_DX) << 16) | M.x86.R_AX;
2496*4882a593Smuzhiyun if (s == 0) {
2497*4882a593Smuzhiyun x86emu_intr_raise(0);
2498*4882a593Smuzhiyun return;
2499*4882a593Smuzhiyun }
2500*4882a593Smuzhiyun div = dvd / (u16) s;
2501*4882a593Smuzhiyun mod = dvd % (u16) s;
2502*4882a593Smuzhiyun if (abs(div) > 0xffff) {
2503*4882a593Smuzhiyun x86emu_intr_raise(0);
2504*4882a593Smuzhiyun return;
2505*4882a593Smuzhiyun }
2506*4882a593Smuzhiyun CLEAR_FLAG(F_CF);
2507*4882a593Smuzhiyun CLEAR_FLAG(F_SF);
2508*4882a593Smuzhiyun CONDITIONAL_SET_FLAG(div == 0, F_ZF);
2509*4882a593Smuzhiyun CONDITIONAL_SET_FLAG(PARITY(mod & 0xff), F_PF);
2510*4882a593Smuzhiyun
2511*4882a593Smuzhiyun M.x86.R_AX = (u16) div;
2512*4882a593Smuzhiyun M.x86.R_DX = (u16) mod;
2513*4882a593Smuzhiyun }
2514*4882a593Smuzhiyun
2515*4882a593Smuzhiyun /****************************************************************************
2516*4882a593Smuzhiyun REMARKS:
2517*4882a593Smuzhiyun Implements the DIV instruction and side effects.
2518*4882a593Smuzhiyun ****************************************************************************/
2519*4882a593Smuzhiyun void
div_long(u32 s)2520*4882a593Smuzhiyun div_long(u32 s)
2521*4882a593Smuzhiyun {
2522*4882a593Smuzhiyun #ifdef __HAS_LONG_LONG__
2523*4882a593Smuzhiyun u64 dvd, div, mod;
2524*4882a593Smuzhiyun
2525*4882a593Smuzhiyun dvd = (((u64) M.x86.R_EDX) << 32) | M.x86.R_EAX;
2526*4882a593Smuzhiyun if (s == 0) {
2527*4882a593Smuzhiyun x86emu_intr_raise(0);
2528*4882a593Smuzhiyun return;
2529*4882a593Smuzhiyun }
2530*4882a593Smuzhiyun div = dvd / (u32) s;
2531*4882a593Smuzhiyun mod = dvd % (u32) s;
2532*4882a593Smuzhiyun if (abs(div) > 0xffffffff) {
2533*4882a593Smuzhiyun x86emu_intr_raise(0);
2534*4882a593Smuzhiyun return;
2535*4882a593Smuzhiyun }
2536*4882a593Smuzhiyun #else
2537*4882a593Smuzhiyun s32 div = 0, mod;
2538*4882a593Smuzhiyun s32 h_dvd = M.x86.R_EDX;
2539*4882a593Smuzhiyun u32 l_dvd = M.x86.R_EAX;
2540*4882a593Smuzhiyun
2541*4882a593Smuzhiyun u32 h_s = s;
2542*4882a593Smuzhiyun u32 l_s = 0;
2543*4882a593Smuzhiyun int counter = 32;
2544*4882a593Smuzhiyun int carry;
2545*4882a593Smuzhiyun
2546*4882a593Smuzhiyun if (s == 0) {
2547*4882a593Smuzhiyun x86emu_intr_raise(0);
2548*4882a593Smuzhiyun return;
2549*4882a593Smuzhiyun }
2550*4882a593Smuzhiyun do {
2551*4882a593Smuzhiyun div <<= 1;
2552*4882a593Smuzhiyun carry = (l_dvd >= l_s) ? 0 : 1;
2553*4882a593Smuzhiyun
2554*4882a593Smuzhiyun if (h_dvd < (h_s + carry)) {
2555*4882a593Smuzhiyun h_s >>= 1;
2556*4882a593Smuzhiyun l_s = s << (--counter);
2557*4882a593Smuzhiyun continue;
2558*4882a593Smuzhiyun }
2559*4882a593Smuzhiyun else {
2560*4882a593Smuzhiyun h_dvd -= (h_s + carry);
2561*4882a593Smuzhiyun l_dvd = carry ? ((0xFFFFFFFF - l_s) + l_dvd + 1)
2562*4882a593Smuzhiyun : (l_dvd - l_s);
2563*4882a593Smuzhiyun h_s >>= 1;
2564*4882a593Smuzhiyun l_s = s << (--counter);
2565*4882a593Smuzhiyun div |= 1;
2566*4882a593Smuzhiyun continue;
2567*4882a593Smuzhiyun }
2568*4882a593Smuzhiyun
2569*4882a593Smuzhiyun } while (counter > -1);
2570*4882a593Smuzhiyun /* overflow */
2571*4882a593Smuzhiyun if (h_dvd || (l_dvd > s)) {
2572*4882a593Smuzhiyun x86emu_intr_raise(0);
2573*4882a593Smuzhiyun return;
2574*4882a593Smuzhiyun }
2575*4882a593Smuzhiyun mod = l_dvd;
2576*4882a593Smuzhiyun #endif
2577*4882a593Smuzhiyun CLEAR_FLAG(F_CF);
2578*4882a593Smuzhiyun CLEAR_FLAG(F_AF);
2579*4882a593Smuzhiyun CLEAR_FLAG(F_SF);
2580*4882a593Smuzhiyun SET_FLAG(F_ZF);
2581*4882a593Smuzhiyun CONDITIONAL_SET_FLAG(PARITY(mod & 0xff), F_PF);
2582*4882a593Smuzhiyun
2583*4882a593Smuzhiyun M.x86.R_EAX = (u32) div;
2584*4882a593Smuzhiyun M.x86.R_EDX = (u32) mod;
2585*4882a593Smuzhiyun }
2586*4882a593Smuzhiyun
2587*4882a593Smuzhiyun /****************************************************************************
2588*4882a593Smuzhiyun REMARKS:
2589*4882a593Smuzhiyun Implements the IN string instruction and side effects.
2590*4882a593Smuzhiyun ****************************************************************************/
2591*4882a593Smuzhiyun void
ins(int size)2592*4882a593Smuzhiyun ins(int size)
2593*4882a593Smuzhiyun {
2594*4882a593Smuzhiyun int inc = size;
2595*4882a593Smuzhiyun
2596*4882a593Smuzhiyun if (ACCESS_FLAG(F_DF)) {
2597*4882a593Smuzhiyun inc = -size;
2598*4882a593Smuzhiyun }
2599*4882a593Smuzhiyun if (M.x86.mode & (SYSMODE_PREFIX_REPE | SYSMODE_PREFIX_REPNE)) {
2600*4882a593Smuzhiyun /* dont care whether REPE or REPNE */
2601*4882a593Smuzhiyun /* in until CX is ZERO. */
2602*4882a593Smuzhiyun u32 count = ((M.x86.mode & SYSMODE_PREFIX_DATA) ?
2603*4882a593Smuzhiyun M.x86.R_ECX : M.x86.R_CX);
2604*4882a593Smuzhiyun switch (size) {
2605*4882a593Smuzhiyun case 1:
2606*4882a593Smuzhiyun while (count--) {
2607*4882a593Smuzhiyun store_data_byte_abs(M.x86.R_ES, M.x86.R_DI,
2608*4882a593Smuzhiyun (*sys_inb) (M.x86.R_DX));
2609*4882a593Smuzhiyun M.x86.R_DI += inc;
2610*4882a593Smuzhiyun }
2611*4882a593Smuzhiyun break;
2612*4882a593Smuzhiyun
2613*4882a593Smuzhiyun case 2:
2614*4882a593Smuzhiyun while (count--) {
2615*4882a593Smuzhiyun store_data_word_abs(M.x86.R_ES, M.x86.R_DI,
2616*4882a593Smuzhiyun (*sys_inw) (M.x86.R_DX));
2617*4882a593Smuzhiyun M.x86.R_DI += inc;
2618*4882a593Smuzhiyun }
2619*4882a593Smuzhiyun break;
2620*4882a593Smuzhiyun case 4:
2621*4882a593Smuzhiyun while (count--) {
2622*4882a593Smuzhiyun store_data_long_abs(M.x86.R_ES, M.x86.R_DI,
2623*4882a593Smuzhiyun (*sys_inl) (M.x86.R_DX));
2624*4882a593Smuzhiyun M.x86.R_DI += inc;
2625*4882a593Smuzhiyun break;
2626*4882a593Smuzhiyun }
2627*4882a593Smuzhiyun }
2628*4882a593Smuzhiyun M.x86.R_CX = 0;
2629*4882a593Smuzhiyun if (M.x86.mode & SYSMODE_PREFIX_DATA) {
2630*4882a593Smuzhiyun M.x86.R_ECX = 0;
2631*4882a593Smuzhiyun }
2632*4882a593Smuzhiyun M.x86.mode &= ~(SYSMODE_PREFIX_REPE | SYSMODE_PREFIX_REPNE);
2633*4882a593Smuzhiyun }
2634*4882a593Smuzhiyun else {
2635*4882a593Smuzhiyun switch (size) {
2636*4882a593Smuzhiyun case 1:
2637*4882a593Smuzhiyun store_data_byte_abs(M.x86.R_ES, M.x86.R_DI,
2638*4882a593Smuzhiyun (*sys_inb) (M.x86.R_DX));
2639*4882a593Smuzhiyun break;
2640*4882a593Smuzhiyun case 2:
2641*4882a593Smuzhiyun store_data_word_abs(M.x86.R_ES, M.x86.R_DI,
2642*4882a593Smuzhiyun (*sys_inw) (M.x86.R_DX));
2643*4882a593Smuzhiyun break;
2644*4882a593Smuzhiyun case 4:
2645*4882a593Smuzhiyun store_data_long_abs(M.x86.R_ES, M.x86.R_DI,
2646*4882a593Smuzhiyun (*sys_inl) (M.x86.R_DX));
2647*4882a593Smuzhiyun break;
2648*4882a593Smuzhiyun }
2649*4882a593Smuzhiyun M.x86.R_DI += inc;
2650*4882a593Smuzhiyun }
2651*4882a593Smuzhiyun }
2652*4882a593Smuzhiyun
2653*4882a593Smuzhiyun /****************************************************************************
2654*4882a593Smuzhiyun REMARKS:
2655*4882a593Smuzhiyun Implements the OUT string instruction and side effects.
2656*4882a593Smuzhiyun ****************************************************************************/
2657*4882a593Smuzhiyun void
outs(int size)2658*4882a593Smuzhiyun outs(int size)
2659*4882a593Smuzhiyun {
2660*4882a593Smuzhiyun int inc = size;
2661*4882a593Smuzhiyun
2662*4882a593Smuzhiyun if (ACCESS_FLAG(F_DF)) {
2663*4882a593Smuzhiyun inc = -size;
2664*4882a593Smuzhiyun }
2665*4882a593Smuzhiyun if (M.x86.mode & (SYSMODE_PREFIX_REPE | SYSMODE_PREFIX_REPNE)) {
2666*4882a593Smuzhiyun /* dont care whether REPE or REPNE */
2667*4882a593Smuzhiyun /* out until CX is ZERO. */
2668*4882a593Smuzhiyun u32 count = ((M.x86.mode & SYSMODE_PREFIX_DATA) ?
2669*4882a593Smuzhiyun M.x86.R_ECX : M.x86.R_CX);
2670*4882a593Smuzhiyun switch (size) {
2671*4882a593Smuzhiyun case 1:
2672*4882a593Smuzhiyun while (count--) {
2673*4882a593Smuzhiyun (*sys_outb) (M.x86.R_DX,
2674*4882a593Smuzhiyun fetch_data_byte_abs(M.x86.R_ES, M.x86.R_SI));
2675*4882a593Smuzhiyun M.x86.R_SI += inc;
2676*4882a593Smuzhiyun }
2677*4882a593Smuzhiyun break;
2678*4882a593Smuzhiyun
2679*4882a593Smuzhiyun case 2:
2680*4882a593Smuzhiyun while (count--) {
2681*4882a593Smuzhiyun (*sys_outw) (M.x86.R_DX,
2682*4882a593Smuzhiyun fetch_data_word_abs(M.x86.R_ES, M.x86.R_SI));
2683*4882a593Smuzhiyun M.x86.R_SI += inc;
2684*4882a593Smuzhiyun }
2685*4882a593Smuzhiyun break;
2686*4882a593Smuzhiyun case 4:
2687*4882a593Smuzhiyun while (count--) {
2688*4882a593Smuzhiyun (*sys_outl) (M.x86.R_DX,
2689*4882a593Smuzhiyun fetch_data_long_abs(M.x86.R_ES, M.x86.R_SI));
2690*4882a593Smuzhiyun M.x86.R_SI += inc;
2691*4882a593Smuzhiyun break;
2692*4882a593Smuzhiyun }
2693*4882a593Smuzhiyun }
2694*4882a593Smuzhiyun M.x86.R_CX = 0;
2695*4882a593Smuzhiyun if (M.x86.mode & SYSMODE_PREFIX_DATA) {
2696*4882a593Smuzhiyun M.x86.R_ECX = 0;
2697*4882a593Smuzhiyun }
2698*4882a593Smuzhiyun M.x86.mode &= ~(SYSMODE_PREFIX_REPE | SYSMODE_PREFIX_REPNE);
2699*4882a593Smuzhiyun }
2700*4882a593Smuzhiyun else {
2701*4882a593Smuzhiyun switch (size) {
2702*4882a593Smuzhiyun case 1:
2703*4882a593Smuzhiyun (*sys_outb) (M.x86.R_DX,
2704*4882a593Smuzhiyun fetch_data_byte_abs(M.x86.R_ES, M.x86.R_SI));
2705*4882a593Smuzhiyun break;
2706*4882a593Smuzhiyun case 2:
2707*4882a593Smuzhiyun (*sys_outw) (M.x86.R_DX,
2708*4882a593Smuzhiyun fetch_data_word_abs(M.x86.R_ES, M.x86.R_SI));
2709*4882a593Smuzhiyun break;
2710*4882a593Smuzhiyun case 4:
2711*4882a593Smuzhiyun (*sys_outl) (M.x86.R_DX,
2712*4882a593Smuzhiyun fetch_data_long_abs(M.x86.R_ES, M.x86.R_SI));
2713*4882a593Smuzhiyun break;
2714*4882a593Smuzhiyun }
2715*4882a593Smuzhiyun M.x86.R_SI += inc;
2716*4882a593Smuzhiyun }
2717*4882a593Smuzhiyun }
2718*4882a593Smuzhiyun
2719*4882a593Smuzhiyun /****************************************************************************
2720*4882a593Smuzhiyun PARAMETERS:
2721*4882a593Smuzhiyun addr - Address to fetch word from
2722*4882a593Smuzhiyun
2723*4882a593Smuzhiyun REMARKS:
2724*4882a593Smuzhiyun Fetches a word from emulator memory using an absolute address.
2725*4882a593Smuzhiyun ****************************************************************************/
2726*4882a593Smuzhiyun u16
mem_access_word(int addr)2727*4882a593Smuzhiyun mem_access_word(int addr)
2728*4882a593Smuzhiyun {
2729*4882a593Smuzhiyun DB(if (CHECK_MEM_ACCESS())
2730*4882a593Smuzhiyun x86emu_check_mem_access(addr);)
2731*4882a593Smuzhiyun return (*sys_rdw) (addr);
2732*4882a593Smuzhiyun }
2733*4882a593Smuzhiyun
2734*4882a593Smuzhiyun /****************************************************************************
2735*4882a593Smuzhiyun REMARKS:
2736*4882a593Smuzhiyun Pushes a word onto the stack.
2737*4882a593Smuzhiyun
2738*4882a593Smuzhiyun NOTE: Do not inline this, as (*sys_wrX) is already inline!
2739*4882a593Smuzhiyun ****************************************************************************/
2740*4882a593Smuzhiyun void
push_word(u16 w)2741*4882a593Smuzhiyun push_word(u16 w)
2742*4882a593Smuzhiyun {
2743*4882a593Smuzhiyun DB(if (CHECK_SP_ACCESS())
2744*4882a593Smuzhiyun x86emu_check_sp_access();)
2745*4882a593Smuzhiyun M.x86.R_SP -= 2;
2746*4882a593Smuzhiyun (*sys_wrw) (((u32) M.x86.R_SS << 4) + M.x86.R_SP, w);
2747*4882a593Smuzhiyun }
2748*4882a593Smuzhiyun
2749*4882a593Smuzhiyun /****************************************************************************
2750*4882a593Smuzhiyun REMARKS:
2751*4882a593Smuzhiyun Pushes a long onto the stack.
2752*4882a593Smuzhiyun
2753*4882a593Smuzhiyun NOTE: Do not inline this, as (*sys_wrX) is already inline!
2754*4882a593Smuzhiyun ****************************************************************************/
2755*4882a593Smuzhiyun void
push_long(u32 w)2756*4882a593Smuzhiyun push_long(u32 w)
2757*4882a593Smuzhiyun {
2758*4882a593Smuzhiyun DB(if (CHECK_SP_ACCESS())
2759*4882a593Smuzhiyun x86emu_check_sp_access();)
2760*4882a593Smuzhiyun M.x86.R_SP -= 4;
2761*4882a593Smuzhiyun (*sys_wrl) (((u32) M.x86.R_SS << 4) + M.x86.R_SP, w);
2762*4882a593Smuzhiyun }
2763*4882a593Smuzhiyun
2764*4882a593Smuzhiyun /****************************************************************************
2765*4882a593Smuzhiyun REMARKS:
2766*4882a593Smuzhiyun Pops a word from the stack.
2767*4882a593Smuzhiyun
2768*4882a593Smuzhiyun NOTE: Do not inline this, as (*sys_rdX) is already inline!
2769*4882a593Smuzhiyun ****************************************************************************/
2770*4882a593Smuzhiyun u16
pop_word(void)2771*4882a593Smuzhiyun pop_word(void)
2772*4882a593Smuzhiyun {
2773*4882a593Smuzhiyun register u16 res;
2774*4882a593Smuzhiyun
2775*4882a593Smuzhiyun DB(if (CHECK_SP_ACCESS())
2776*4882a593Smuzhiyun x86emu_check_sp_access();)
2777*4882a593Smuzhiyun res = (*sys_rdw) (((u32) M.x86.R_SS << 4) + M.x86.R_SP);
2778*4882a593Smuzhiyun M.x86.R_SP += 2;
2779*4882a593Smuzhiyun return res;
2780*4882a593Smuzhiyun }
2781*4882a593Smuzhiyun
2782*4882a593Smuzhiyun /****************************************************************************
2783*4882a593Smuzhiyun REMARKS:
2784*4882a593Smuzhiyun Pops a long from the stack.
2785*4882a593Smuzhiyun
2786*4882a593Smuzhiyun NOTE: Do not inline this, as (*sys_rdX) is already inline!
2787*4882a593Smuzhiyun ****************************************************************************/
2788*4882a593Smuzhiyun u32
pop_long(void)2789*4882a593Smuzhiyun pop_long(void)
2790*4882a593Smuzhiyun {
2791*4882a593Smuzhiyun register u32 res;
2792*4882a593Smuzhiyun
2793*4882a593Smuzhiyun DB(if (CHECK_SP_ACCESS())
2794*4882a593Smuzhiyun x86emu_check_sp_access();)
2795*4882a593Smuzhiyun res = (*sys_rdl) (((u32) M.x86.R_SS << 4) + M.x86.R_SP);
2796*4882a593Smuzhiyun M.x86.R_SP += 4;
2797*4882a593Smuzhiyun return res;
2798*4882a593Smuzhiyun }
2799*4882a593Smuzhiyun
2800*4882a593Smuzhiyun /****************************************************************************
2801*4882a593Smuzhiyun REMARKS:
2802*4882a593Smuzhiyun CPUID takes EAX/ECX as inputs, writes EAX/EBX/ECX/EDX as output
2803*4882a593Smuzhiyun ****************************************************************************/
2804*4882a593Smuzhiyun void
cpuid(void)2805*4882a593Smuzhiyun cpuid(void)
2806*4882a593Smuzhiyun {
2807*4882a593Smuzhiyun u32 feature = M.x86.R_EAX;
2808*4882a593Smuzhiyun
2809*4882a593Smuzhiyun #ifdef X86EMU_HAS_HW_CPUID
2810*4882a593Smuzhiyun /* If the platform allows it, we will base our values on the real
2811*4882a593Smuzhiyun * results from the CPUID instruction. We limit support to the
2812*4882a593Smuzhiyun * first two features, and the results of those are sanitized.
2813*4882a593Smuzhiyun */
2814*4882a593Smuzhiyun if (feature <= 1)
2815*4882a593Smuzhiyun hw_cpuid(&M.x86.R_EAX, &M.x86.R_EBX, &M.x86.R_ECX, &M.x86.R_EDX);
2816*4882a593Smuzhiyun #endif
2817*4882a593Smuzhiyun
2818*4882a593Smuzhiyun switch (feature) {
2819*4882a593Smuzhiyun case 0:
2820*4882a593Smuzhiyun /* Regardless if we have real data from the hardware, the emulator
2821*4882a593Smuzhiyun * will only support upto feature 1, which we set in register EAX.
2822*4882a593Smuzhiyun * Registers EBX:EDX:ECX contain a string identifying the CPU.
2823*4882a593Smuzhiyun */
2824*4882a593Smuzhiyun M.x86.R_EAX = 1;
2825*4882a593Smuzhiyun #ifndef X86EMU_HAS_HW_CPUID
2826*4882a593Smuzhiyun /* EBX:EDX:ECX = "GenuineIntel" */
2827*4882a593Smuzhiyun M.x86.R_EBX = 0x756e6547;
2828*4882a593Smuzhiyun M.x86.R_EDX = 0x49656e69;
2829*4882a593Smuzhiyun M.x86.R_ECX = 0x6c65746e;
2830*4882a593Smuzhiyun #endif
2831*4882a593Smuzhiyun break;
2832*4882a593Smuzhiyun case 1:
2833*4882a593Smuzhiyun #ifndef X86EMU_HAS_HW_CPUID
2834*4882a593Smuzhiyun /* If we don't have x86 compatible hardware, we return values from an
2835*4882a593Smuzhiyun * Intel 486dx4; which was one of the first processors to have CPUID.
2836*4882a593Smuzhiyun */
2837*4882a593Smuzhiyun M.x86.R_EAX = 0x00000480;
2838*4882a593Smuzhiyun M.x86.R_EBX = 0x00000000;
2839*4882a593Smuzhiyun M.x86.R_ECX = 0x00000000;
2840*4882a593Smuzhiyun M.x86.R_EDX = 0x00000002; /* VME */
2841*4882a593Smuzhiyun #else
2842*4882a593Smuzhiyun /* In the case that we have hardware CPUID instruction, we make sure
2843*4882a593Smuzhiyun * that the features reported are limited to TSC and VME.
2844*4882a593Smuzhiyun */
2845*4882a593Smuzhiyun M.x86.R_EDX &= 0x00000012;
2846*4882a593Smuzhiyun #endif
2847*4882a593Smuzhiyun break;
2848*4882a593Smuzhiyun default:
2849*4882a593Smuzhiyun /* Finally, we don't support any additional features. Most CPUs
2850*4882a593Smuzhiyun * return all zeros when queried for invalid or unsupported feature
2851*4882a593Smuzhiyun * numbers.
2852*4882a593Smuzhiyun */
2853*4882a593Smuzhiyun M.x86.R_EAX = 0;
2854*4882a593Smuzhiyun M.x86.R_EBX = 0;
2855*4882a593Smuzhiyun M.x86.R_ECX = 0;
2856*4882a593Smuzhiyun M.x86.R_EDX = 0;
2857*4882a593Smuzhiyun break;
2858*4882a593Smuzhiyun }
2859*4882a593Smuzhiyun }
2860