1*4882a593Smuzhiyun /****************************************************************************
2*4882a593Smuzhiyun *
3*4882a593Smuzhiyun * Realmode X86 Emulator Library
4*4882a593Smuzhiyun *
5*4882a593Smuzhiyun * Copyright (C) 1991-2004 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 OF343364 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 <common.h>
101*4882a593Smuzhiyun
102*4882a593Smuzhiyun #define PRIM_OPS_NO_REDEFINE_ASM
103*4882a593Smuzhiyun #include "x86emu/x86emui.h"
104*4882a593Smuzhiyun
105*4882a593Smuzhiyun /*------------------------- Global Variables ------------------------------*/
106*4882a593Smuzhiyun
107*4882a593Smuzhiyun static u32 x86emu_parity_tab[8] =
108*4882a593Smuzhiyun {
109*4882a593Smuzhiyun 0x96696996,
110*4882a593Smuzhiyun 0x69969669,
111*4882a593Smuzhiyun 0x69969669,
112*4882a593Smuzhiyun 0x96696996,
113*4882a593Smuzhiyun 0x69969669,
114*4882a593Smuzhiyun 0x96696996,
115*4882a593Smuzhiyun 0x96696996,
116*4882a593Smuzhiyun 0x69969669,
117*4882a593Smuzhiyun };
118*4882a593Smuzhiyun
119*4882a593Smuzhiyun #define PARITY(x) (((x86emu_parity_tab[(x) / 32] >> ((x) % 32)) & 1) == 0)
120*4882a593Smuzhiyun #define XOR2(x) (((x) ^ ((x)>>1)) & 0x1)
121*4882a593Smuzhiyun
122*4882a593Smuzhiyun /*----------------------------- Implementation ----------------------------*/
123*4882a593Smuzhiyun
124*4882a593Smuzhiyun
125*4882a593Smuzhiyun /*--------- Side effects helper functions -------*/
126*4882a593Smuzhiyun
127*4882a593Smuzhiyun /****************************************************************************
128*4882a593Smuzhiyun REMARKS:
129*4882a593Smuzhiyun implements side efects for byte operations that don't overflow
130*4882a593Smuzhiyun ****************************************************************************/
131*4882a593Smuzhiyun
set_parity_flag(u32 res)132*4882a593Smuzhiyun static void set_parity_flag(u32 res)
133*4882a593Smuzhiyun {
134*4882a593Smuzhiyun CONDITIONAL_SET_FLAG(PARITY(res & 0xFF), F_PF);
135*4882a593Smuzhiyun }
136*4882a593Smuzhiyun
set_szp_flags_8(u8 res)137*4882a593Smuzhiyun static void set_szp_flags_8(u8 res)
138*4882a593Smuzhiyun {
139*4882a593Smuzhiyun CONDITIONAL_SET_FLAG(res & 0x80, F_SF);
140*4882a593Smuzhiyun CONDITIONAL_SET_FLAG(res == 0, F_ZF);
141*4882a593Smuzhiyun set_parity_flag(res);
142*4882a593Smuzhiyun }
143*4882a593Smuzhiyun
set_szp_flags_16(u16 res)144*4882a593Smuzhiyun static void set_szp_flags_16(u16 res)
145*4882a593Smuzhiyun {
146*4882a593Smuzhiyun CONDITIONAL_SET_FLAG(res & 0x8000, F_SF);
147*4882a593Smuzhiyun CONDITIONAL_SET_FLAG(res == 0, F_ZF);
148*4882a593Smuzhiyun set_parity_flag(res);
149*4882a593Smuzhiyun }
150*4882a593Smuzhiyun
set_szp_flags_32(u32 res)151*4882a593Smuzhiyun static void set_szp_flags_32(u32 res)
152*4882a593Smuzhiyun {
153*4882a593Smuzhiyun CONDITIONAL_SET_FLAG(res & 0x80000000, F_SF);
154*4882a593Smuzhiyun CONDITIONAL_SET_FLAG(res == 0, F_ZF);
155*4882a593Smuzhiyun set_parity_flag(res);
156*4882a593Smuzhiyun }
157*4882a593Smuzhiyun
no_carry_byte_side_eff(u8 res)158*4882a593Smuzhiyun static void no_carry_byte_side_eff(u8 res)
159*4882a593Smuzhiyun {
160*4882a593Smuzhiyun CLEAR_FLAG(F_OF);
161*4882a593Smuzhiyun CLEAR_FLAG(F_CF);
162*4882a593Smuzhiyun CLEAR_FLAG(F_AF);
163*4882a593Smuzhiyun set_szp_flags_8(res);
164*4882a593Smuzhiyun }
165*4882a593Smuzhiyun
no_carry_word_side_eff(u16 res)166*4882a593Smuzhiyun static void no_carry_word_side_eff(u16 res)
167*4882a593Smuzhiyun {
168*4882a593Smuzhiyun CLEAR_FLAG(F_OF);
169*4882a593Smuzhiyun CLEAR_FLAG(F_CF);
170*4882a593Smuzhiyun CLEAR_FLAG(F_AF);
171*4882a593Smuzhiyun set_szp_flags_16(res);
172*4882a593Smuzhiyun }
173*4882a593Smuzhiyun
no_carry_long_side_eff(u32 res)174*4882a593Smuzhiyun static void no_carry_long_side_eff(u32 res)
175*4882a593Smuzhiyun {
176*4882a593Smuzhiyun CLEAR_FLAG(F_OF);
177*4882a593Smuzhiyun CLEAR_FLAG(F_CF);
178*4882a593Smuzhiyun CLEAR_FLAG(F_AF);
179*4882a593Smuzhiyun set_szp_flags_32(res);
180*4882a593Smuzhiyun }
181*4882a593Smuzhiyun
calc_carry_chain(int bits,u32 d,u32 s,u32 res,int set_carry)182*4882a593Smuzhiyun static void calc_carry_chain(int bits, u32 d, u32 s, u32 res, int set_carry)
183*4882a593Smuzhiyun {
184*4882a593Smuzhiyun u32 cc;
185*4882a593Smuzhiyun
186*4882a593Smuzhiyun cc = (s & d) | ((~res) & (s | d));
187*4882a593Smuzhiyun CONDITIONAL_SET_FLAG(XOR2(cc >> (bits - 2)), F_OF);
188*4882a593Smuzhiyun CONDITIONAL_SET_FLAG(cc & 0x8, F_AF);
189*4882a593Smuzhiyun if (set_carry) {
190*4882a593Smuzhiyun CONDITIONAL_SET_FLAG(res & (1 << bits), F_CF);
191*4882a593Smuzhiyun }
192*4882a593Smuzhiyun }
193*4882a593Smuzhiyun
calc_borrow_chain(int bits,u32 d,u32 s,u32 res,int set_carry)194*4882a593Smuzhiyun static void calc_borrow_chain(int bits, u32 d, u32 s, u32 res, int set_carry)
195*4882a593Smuzhiyun {
196*4882a593Smuzhiyun u32 bc;
197*4882a593Smuzhiyun
198*4882a593Smuzhiyun bc = (res & (~d | s)) | (~d & s);
199*4882a593Smuzhiyun CONDITIONAL_SET_FLAG(XOR2(bc >> (bits - 2)), F_OF);
200*4882a593Smuzhiyun CONDITIONAL_SET_FLAG(bc & 0x8, F_AF);
201*4882a593Smuzhiyun if (set_carry) {
202*4882a593Smuzhiyun CONDITIONAL_SET_FLAG(bc & (1 << (bits - 1)), F_CF);
203*4882a593Smuzhiyun }
204*4882a593Smuzhiyun }
205*4882a593Smuzhiyun
206*4882a593Smuzhiyun /****************************************************************************
207*4882a593Smuzhiyun REMARKS:
208*4882a593Smuzhiyun Implements the AAA instruction and side effects.
209*4882a593Smuzhiyun ****************************************************************************/
aaa_word(u16 d)210*4882a593Smuzhiyun u16 aaa_word(u16 d)
211*4882a593Smuzhiyun {
212*4882a593Smuzhiyun u16 res;
213*4882a593Smuzhiyun if ((d & 0xf) > 0x9 || ACCESS_FLAG(F_AF)) {
214*4882a593Smuzhiyun d += 0x6;
215*4882a593Smuzhiyun d += 0x100;
216*4882a593Smuzhiyun SET_FLAG(F_AF);
217*4882a593Smuzhiyun SET_FLAG(F_CF);
218*4882a593Smuzhiyun } else {
219*4882a593Smuzhiyun CLEAR_FLAG(F_CF);
220*4882a593Smuzhiyun CLEAR_FLAG(F_AF);
221*4882a593Smuzhiyun }
222*4882a593Smuzhiyun res = (u16)(d & 0xFF0F);
223*4882a593Smuzhiyun set_szp_flags_16(res);
224*4882a593Smuzhiyun return res;
225*4882a593Smuzhiyun }
226*4882a593Smuzhiyun
227*4882a593Smuzhiyun /****************************************************************************
228*4882a593Smuzhiyun REMARKS:
229*4882a593Smuzhiyun Implements the AAA instruction and side effects.
230*4882a593Smuzhiyun ****************************************************************************/
aas_word(u16 d)231*4882a593Smuzhiyun u16 aas_word(u16 d)
232*4882a593Smuzhiyun {
233*4882a593Smuzhiyun u16 res;
234*4882a593Smuzhiyun if ((d & 0xf) > 0x9 || ACCESS_FLAG(F_AF)) {
235*4882a593Smuzhiyun d -= 0x6;
236*4882a593Smuzhiyun d -= 0x100;
237*4882a593Smuzhiyun SET_FLAG(F_AF);
238*4882a593Smuzhiyun SET_FLAG(F_CF);
239*4882a593Smuzhiyun } else {
240*4882a593Smuzhiyun CLEAR_FLAG(F_CF);
241*4882a593Smuzhiyun CLEAR_FLAG(F_AF);
242*4882a593Smuzhiyun }
243*4882a593Smuzhiyun res = (u16)(d & 0xFF0F);
244*4882a593Smuzhiyun set_szp_flags_16(res);
245*4882a593Smuzhiyun return res;
246*4882a593Smuzhiyun }
247*4882a593Smuzhiyun
248*4882a593Smuzhiyun /****************************************************************************
249*4882a593Smuzhiyun REMARKS:
250*4882a593Smuzhiyun Implements the AAD instruction and side effects.
251*4882a593Smuzhiyun ****************************************************************************/
aad_word(u16 d)252*4882a593Smuzhiyun u16 aad_word(u16 d)
253*4882a593Smuzhiyun {
254*4882a593Smuzhiyun u16 l;
255*4882a593Smuzhiyun u8 hb, lb;
256*4882a593Smuzhiyun
257*4882a593Smuzhiyun hb = (u8)((d >> 8) & 0xff);
258*4882a593Smuzhiyun lb = (u8)((d & 0xff));
259*4882a593Smuzhiyun l = (u16)((lb + 10 * hb) & 0xFF);
260*4882a593Smuzhiyun
261*4882a593Smuzhiyun no_carry_byte_side_eff(l & 0xFF);
262*4882a593Smuzhiyun return l;
263*4882a593Smuzhiyun }
264*4882a593Smuzhiyun
265*4882a593Smuzhiyun /****************************************************************************
266*4882a593Smuzhiyun REMARKS:
267*4882a593Smuzhiyun Implements the AAM instruction and side effects.
268*4882a593Smuzhiyun ****************************************************************************/
aam_word(u8 d)269*4882a593Smuzhiyun u16 aam_word(u8 d)
270*4882a593Smuzhiyun {
271*4882a593Smuzhiyun u16 h, l;
272*4882a593Smuzhiyun
273*4882a593Smuzhiyun h = (u16)(d / 10);
274*4882a593Smuzhiyun l = (u16)(d % 10);
275*4882a593Smuzhiyun l |= (u16)(h << 8);
276*4882a593Smuzhiyun
277*4882a593Smuzhiyun no_carry_byte_side_eff(l & 0xFF);
278*4882a593Smuzhiyun return l;
279*4882a593Smuzhiyun }
280*4882a593Smuzhiyun
281*4882a593Smuzhiyun /****************************************************************************
282*4882a593Smuzhiyun REMARKS:
283*4882a593Smuzhiyun Implements the ADC instruction and side effects.
284*4882a593Smuzhiyun ****************************************************************************/
adc_byte(u8 d,u8 s)285*4882a593Smuzhiyun u8 adc_byte(u8 d, u8 s)
286*4882a593Smuzhiyun {
287*4882a593Smuzhiyun u32 res; /* all operands in native machine order */
288*4882a593Smuzhiyun
289*4882a593Smuzhiyun res = d + s;
290*4882a593Smuzhiyun if (ACCESS_FLAG(F_CF)) res++;
291*4882a593Smuzhiyun
292*4882a593Smuzhiyun set_szp_flags_8(res);
293*4882a593Smuzhiyun calc_carry_chain(8,s,d,res,1);
294*4882a593Smuzhiyun
295*4882a593Smuzhiyun return (u8)res;
296*4882a593Smuzhiyun }
297*4882a593Smuzhiyun
298*4882a593Smuzhiyun /****************************************************************************
299*4882a593Smuzhiyun REMARKS:
300*4882a593Smuzhiyun Implements the ADC instruction and side effects.
301*4882a593Smuzhiyun ****************************************************************************/
adc_word(u16 d,u16 s)302*4882a593Smuzhiyun u16 adc_word(u16 d, u16 s)
303*4882a593Smuzhiyun {
304*4882a593Smuzhiyun u32 res; /* all operands in native machine order */
305*4882a593Smuzhiyun
306*4882a593Smuzhiyun res = d + s;
307*4882a593Smuzhiyun if (ACCESS_FLAG(F_CF))
308*4882a593Smuzhiyun res++;
309*4882a593Smuzhiyun
310*4882a593Smuzhiyun set_szp_flags_16((u16)res);
311*4882a593Smuzhiyun calc_carry_chain(16,s,d,res,1);
312*4882a593Smuzhiyun
313*4882a593Smuzhiyun return (u16)res;
314*4882a593Smuzhiyun }
315*4882a593Smuzhiyun
316*4882a593Smuzhiyun /****************************************************************************
317*4882a593Smuzhiyun REMARKS:
318*4882a593Smuzhiyun Implements the ADC instruction and side effects.
319*4882a593Smuzhiyun ****************************************************************************/
adc_long(u32 d,u32 s)320*4882a593Smuzhiyun u32 adc_long(u32 d, u32 s)
321*4882a593Smuzhiyun {
322*4882a593Smuzhiyun u32 lo; /* all operands in native machine order */
323*4882a593Smuzhiyun u32 hi;
324*4882a593Smuzhiyun u32 res;
325*4882a593Smuzhiyun
326*4882a593Smuzhiyun lo = (d & 0xFFFF) + (s & 0xFFFF);
327*4882a593Smuzhiyun res = d + s;
328*4882a593Smuzhiyun
329*4882a593Smuzhiyun if (ACCESS_FLAG(F_CF)) {
330*4882a593Smuzhiyun lo++;
331*4882a593Smuzhiyun res++;
332*4882a593Smuzhiyun }
333*4882a593Smuzhiyun
334*4882a593Smuzhiyun hi = (lo >> 16) + (d >> 16) + (s >> 16);
335*4882a593Smuzhiyun
336*4882a593Smuzhiyun set_szp_flags_32(res);
337*4882a593Smuzhiyun calc_carry_chain(32,s,d,res,0);
338*4882a593Smuzhiyun
339*4882a593Smuzhiyun CONDITIONAL_SET_FLAG(hi & 0x10000, F_CF);
340*4882a593Smuzhiyun
341*4882a593Smuzhiyun return res;
342*4882a593Smuzhiyun }
343*4882a593Smuzhiyun
344*4882a593Smuzhiyun /****************************************************************************
345*4882a593Smuzhiyun REMARKS:
346*4882a593Smuzhiyun Implements the ADD instruction and side effects.
347*4882a593Smuzhiyun ****************************************************************************/
add_byte(u8 d,u8 s)348*4882a593Smuzhiyun u8 add_byte(u8 d, u8 s)
349*4882a593Smuzhiyun {
350*4882a593Smuzhiyun u32 res; /* all operands in native machine order */
351*4882a593Smuzhiyun
352*4882a593Smuzhiyun res = d + s;
353*4882a593Smuzhiyun set_szp_flags_8((u8)res);
354*4882a593Smuzhiyun calc_carry_chain(8,s,d,res,1);
355*4882a593Smuzhiyun
356*4882a593Smuzhiyun return (u8)res;
357*4882a593Smuzhiyun }
358*4882a593Smuzhiyun
359*4882a593Smuzhiyun /****************************************************************************
360*4882a593Smuzhiyun REMARKS:
361*4882a593Smuzhiyun Implements the ADD instruction and side effects.
362*4882a593Smuzhiyun ****************************************************************************/
add_word(u16 d,u16 s)363*4882a593Smuzhiyun u16 add_word(u16 d, u16 s)
364*4882a593Smuzhiyun {
365*4882a593Smuzhiyun u32 res; /* all operands in native machine order */
366*4882a593Smuzhiyun
367*4882a593Smuzhiyun res = d + s;
368*4882a593Smuzhiyun set_szp_flags_16((u16)res);
369*4882a593Smuzhiyun calc_carry_chain(16,s,d,res,1);
370*4882a593Smuzhiyun
371*4882a593Smuzhiyun return (u16)res;
372*4882a593Smuzhiyun }
373*4882a593Smuzhiyun
374*4882a593Smuzhiyun /****************************************************************************
375*4882a593Smuzhiyun REMARKS:
376*4882a593Smuzhiyun Implements the ADD instruction and side effects.
377*4882a593Smuzhiyun ****************************************************************************/
add_long(u32 d,u32 s)378*4882a593Smuzhiyun u32 add_long(u32 d, u32 s)
379*4882a593Smuzhiyun {
380*4882a593Smuzhiyun u32 res;
381*4882a593Smuzhiyun
382*4882a593Smuzhiyun res = d + s;
383*4882a593Smuzhiyun set_szp_flags_32(res);
384*4882a593Smuzhiyun calc_carry_chain(32,s,d,res,0);
385*4882a593Smuzhiyun
386*4882a593Smuzhiyun CONDITIONAL_SET_FLAG(res < d || res < s, F_CF);
387*4882a593Smuzhiyun
388*4882a593Smuzhiyun return res;
389*4882a593Smuzhiyun }
390*4882a593Smuzhiyun
391*4882a593Smuzhiyun /****************************************************************************
392*4882a593Smuzhiyun REMARKS:
393*4882a593Smuzhiyun Implements the AND instruction and side effects.
394*4882a593Smuzhiyun ****************************************************************************/
and_byte(u8 d,u8 s)395*4882a593Smuzhiyun u8 and_byte(u8 d, u8 s)
396*4882a593Smuzhiyun {
397*4882a593Smuzhiyun u8 res; /* all operands in native machine order */
398*4882a593Smuzhiyun
399*4882a593Smuzhiyun res = d & s;
400*4882a593Smuzhiyun
401*4882a593Smuzhiyun no_carry_byte_side_eff(res);
402*4882a593Smuzhiyun return res;
403*4882a593Smuzhiyun }
404*4882a593Smuzhiyun
405*4882a593Smuzhiyun /****************************************************************************
406*4882a593Smuzhiyun REMARKS:
407*4882a593Smuzhiyun Implements the AND instruction and side effects.
408*4882a593Smuzhiyun ****************************************************************************/
and_word(u16 d,u16 s)409*4882a593Smuzhiyun u16 and_word(u16 d, u16 s)
410*4882a593Smuzhiyun {
411*4882a593Smuzhiyun u16 res; /* all operands in native machine order */
412*4882a593Smuzhiyun
413*4882a593Smuzhiyun res = d & s;
414*4882a593Smuzhiyun
415*4882a593Smuzhiyun no_carry_word_side_eff(res);
416*4882a593Smuzhiyun return res;
417*4882a593Smuzhiyun }
418*4882a593Smuzhiyun
419*4882a593Smuzhiyun /****************************************************************************
420*4882a593Smuzhiyun REMARKS:
421*4882a593Smuzhiyun Implements the AND instruction and side effects.
422*4882a593Smuzhiyun ****************************************************************************/
and_long(u32 d,u32 s)423*4882a593Smuzhiyun u32 and_long(u32 d, u32 s)
424*4882a593Smuzhiyun {
425*4882a593Smuzhiyun u32 res; /* all operands in native machine order */
426*4882a593Smuzhiyun
427*4882a593Smuzhiyun res = d & s;
428*4882a593Smuzhiyun no_carry_long_side_eff(res);
429*4882a593Smuzhiyun return res;
430*4882a593Smuzhiyun }
431*4882a593Smuzhiyun
432*4882a593Smuzhiyun /****************************************************************************
433*4882a593Smuzhiyun REMARKS:
434*4882a593Smuzhiyun Implements the CMP instruction and side effects.
435*4882a593Smuzhiyun ****************************************************************************/
cmp_byte(u8 d,u8 s)436*4882a593Smuzhiyun u8 cmp_byte(u8 d, u8 s)
437*4882a593Smuzhiyun {
438*4882a593Smuzhiyun u32 res; /* all operands in native machine order */
439*4882a593Smuzhiyun
440*4882a593Smuzhiyun res = d - s;
441*4882a593Smuzhiyun set_szp_flags_8((u8)res);
442*4882a593Smuzhiyun calc_borrow_chain(8, d, s, res, 1);
443*4882a593Smuzhiyun
444*4882a593Smuzhiyun return d;
445*4882a593Smuzhiyun }
446*4882a593Smuzhiyun
447*4882a593Smuzhiyun /****************************************************************************
448*4882a593Smuzhiyun REMARKS:
449*4882a593Smuzhiyun Implements the CMP instruction and side effects.
450*4882a593Smuzhiyun ****************************************************************************/
cmp_word(u16 d,u16 s)451*4882a593Smuzhiyun u16 cmp_word(u16 d, u16 s)
452*4882a593Smuzhiyun {
453*4882a593Smuzhiyun u32 res; /* all operands in native machine order */
454*4882a593Smuzhiyun
455*4882a593Smuzhiyun res = d - s;
456*4882a593Smuzhiyun set_szp_flags_16((u16)res);
457*4882a593Smuzhiyun calc_borrow_chain(16, d, s, res, 1);
458*4882a593Smuzhiyun
459*4882a593Smuzhiyun return d;
460*4882a593Smuzhiyun }
461*4882a593Smuzhiyun
462*4882a593Smuzhiyun /****************************************************************************
463*4882a593Smuzhiyun REMARKS:
464*4882a593Smuzhiyun Implements the CMP instruction and side effects.
465*4882a593Smuzhiyun ****************************************************************************/
cmp_long(u32 d,u32 s)466*4882a593Smuzhiyun u32 cmp_long(u32 d, u32 s)
467*4882a593Smuzhiyun {
468*4882a593Smuzhiyun u32 res; /* all operands in native machine order */
469*4882a593Smuzhiyun
470*4882a593Smuzhiyun res = d - s;
471*4882a593Smuzhiyun set_szp_flags_32(res);
472*4882a593Smuzhiyun calc_borrow_chain(32, d, s, res, 1);
473*4882a593Smuzhiyun
474*4882a593Smuzhiyun return d;
475*4882a593Smuzhiyun }
476*4882a593Smuzhiyun
477*4882a593Smuzhiyun /****************************************************************************
478*4882a593Smuzhiyun REMARKS:
479*4882a593Smuzhiyun Implements the DAA instruction and side effects.
480*4882a593Smuzhiyun ****************************************************************************/
daa_byte(u8 d)481*4882a593Smuzhiyun u8 daa_byte(u8 d)
482*4882a593Smuzhiyun {
483*4882a593Smuzhiyun u32 res = d;
484*4882a593Smuzhiyun if ((d & 0xf) > 9 || ACCESS_FLAG(F_AF)) {
485*4882a593Smuzhiyun res += 6;
486*4882a593Smuzhiyun SET_FLAG(F_AF);
487*4882a593Smuzhiyun }
488*4882a593Smuzhiyun if (res > 0x9F || ACCESS_FLAG(F_CF)) {
489*4882a593Smuzhiyun res += 0x60;
490*4882a593Smuzhiyun SET_FLAG(F_CF);
491*4882a593Smuzhiyun }
492*4882a593Smuzhiyun set_szp_flags_8((u8)res);
493*4882a593Smuzhiyun return (u8)res;
494*4882a593Smuzhiyun }
495*4882a593Smuzhiyun
496*4882a593Smuzhiyun /****************************************************************************
497*4882a593Smuzhiyun REMARKS:
498*4882a593Smuzhiyun Implements the DAS instruction and side effects.
499*4882a593Smuzhiyun ****************************************************************************/
das_byte(u8 d)500*4882a593Smuzhiyun u8 das_byte(u8 d)
501*4882a593Smuzhiyun {
502*4882a593Smuzhiyun if ((d & 0xf) > 9 || ACCESS_FLAG(F_AF)) {
503*4882a593Smuzhiyun d -= 6;
504*4882a593Smuzhiyun SET_FLAG(F_AF);
505*4882a593Smuzhiyun }
506*4882a593Smuzhiyun if (d > 0x9F || ACCESS_FLAG(F_CF)) {
507*4882a593Smuzhiyun d -= 0x60;
508*4882a593Smuzhiyun SET_FLAG(F_CF);
509*4882a593Smuzhiyun }
510*4882a593Smuzhiyun set_szp_flags_8(d);
511*4882a593Smuzhiyun return d;
512*4882a593Smuzhiyun }
513*4882a593Smuzhiyun
514*4882a593Smuzhiyun /****************************************************************************
515*4882a593Smuzhiyun REMARKS:
516*4882a593Smuzhiyun Implements the DEC instruction and side effects.
517*4882a593Smuzhiyun ****************************************************************************/
dec_byte(u8 d)518*4882a593Smuzhiyun u8 dec_byte(u8 d)
519*4882a593Smuzhiyun {
520*4882a593Smuzhiyun u32 res; /* all operands in native machine order */
521*4882a593Smuzhiyun
522*4882a593Smuzhiyun res = d - 1;
523*4882a593Smuzhiyun set_szp_flags_8((u8)res);
524*4882a593Smuzhiyun calc_borrow_chain(8, d, 1, res, 0);
525*4882a593Smuzhiyun
526*4882a593Smuzhiyun return (u8)res;
527*4882a593Smuzhiyun }
528*4882a593Smuzhiyun
529*4882a593Smuzhiyun /****************************************************************************
530*4882a593Smuzhiyun REMARKS:
531*4882a593Smuzhiyun Implements the DEC instruction and side effects.
532*4882a593Smuzhiyun ****************************************************************************/
dec_word(u16 d)533*4882a593Smuzhiyun u16 dec_word(u16 d)
534*4882a593Smuzhiyun {
535*4882a593Smuzhiyun u32 res; /* all operands in native machine order */
536*4882a593Smuzhiyun
537*4882a593Smuzhiyun res = d - 1;
538*4882a593Smuzhiyun set_szp_flags_16((u16)res);
539*4882a593Smuzhiyun calc_borrow_chain(16, d, 1, res, 0);
540*4882a593Smuzhiyun
541*4882a593Smuzhiyun return (u16)res;
542*4882a593Smuzhiyun }
543*4882a593Smuzhiyun
544*4882a593Smuzhiyun /****************************************************************************
545*4882a593Smuzhiyun REMARKS:
546*4882a593Smuzhiyun Implements the DEC instruction and side effects.
547*4882a593Smuzhiyun ****************************************************************************/
dec_long(u32 d)548*4882a593Smuzhiyun u32 dec_long(u32 d)
549*4882a593Smuzhiyun {
550*4882a593Smuzhiyun u32 res; /* all operands in native machine order */
551*4882a593Smuzhiyun
552*4882a593Smuzhiyun res = d - 1;
553*4882a593Smuzhiyun
554*4882a593Smuzhiyun set_szp_flags_32(res);
555*4882a593Smuzhiyun calc_borrow_chain(32, d, 1, res, 0);
556*4882a593Smuzhiyun
557*4882a593Smuzhiyun return res;
558*4882a593Smuzhiyun }
559*4882a593Smuzhiyun
560*4882a593Smuzhiyun /****************************************************************************
561*4882a593Smuzhiyun REMARKS:
562*4882a593Smuzhiyun Implements the INC instruction and side effects.
563*4882a593Smuzhiyun ****************************************************************************/
inc_byte(u8 d)564*4882a593Smuzhiyun u8 inc_byte(u8 d)
565*4882a593Smuzhiyun {
566*4882a593Smuzhiyun u32 res; /* all operands in native machine order */
567*4882a593Smuzhiyun
568*4882a593Smuzhiyun res = d + 1;
569*4882a593Smuzhiyun set_szp_flags_8((u8)res);
570*4882a593Smuzhiyun calc_carry_chain(8, d, 1, res, 0);
571*4882a593Smuzhiyun
572*4882a593Smuzhiyun return (u8)res;
573*4882a593Smuzhiyun }
574*4882a593Smuzhiyun
575*4882a593Smuzhiyun /****************************************************************************
576*4882a593Smuzhiyun REMARKS:
577*4882a593Smuzhiyun Implements the INC instruction and side effects.
578*4882a593Smuzhiyun ****************************************************************************/
inc_word(u16 d)579*4882a593Smuzhiyun u16 inc_word(u16 d)
580*4882a593Smuzhiyun {
581*4882a593Smuzhiyun u32 res; /* all operands in native machine order */
582*4882a593Smuzhiyun
583*4882a593Smuzhiyun res = d + 1;
584*4882a593Smuzhiyun set_szp_flags_16((u16)res);
585*4882a593Smuzhiyun calc_carry_chain(16, d, 1, res, 0);
586*4882a593Smuzhiyun
587*4882a593Smuzhiyun return (u16)res;
588*4882a593Smuzhiyun }
589*4882a593Smuzhiyun
590*4882a593Smuzhiyun /****************************************************************************
591*4882a593Smuzhiyun REMARKS:
592*4882a593Smuzhiyun Implements the INC instruction and side effects.
593*4882a593Smuzhiyun ****************************************************************************/
inc_long(u32 d)594*4882a593Smuzhiyun u32 inc_long(u32 d)
595*4882a593Smuzhiyun {
596*4882a593Smuzhiyun u32 res; /* all operands in native machine order */
597*4882a593Smuzhiyun
598*4882a593Smuzhiyun res = d + 1;
599*4882a593Smuzhiyun set_szp_flags_32(res);
600*4882a593Smuzhiyun calc_carry_chain(32, d, 1, res, 0);
601*4882a593Smuzhiyun
602*4882a593Smuzhiyun return res;
603*4882a593Smuzhiyun }
604*4882a593Smuzhiyun
605*4882a593Smuzhiyun /****************************************************************************
606*4882a593Smuzhiyun REMARKS:
607*4882a593Smuzhiyun Implements the OR instruction and side effects.
608*4882a593Smuzhiyun ****************************************************************************/
or_byte(u8 d,u8 s)609*4882a593Smuzhiyun u8 or_byte(u8 d, u8 s)
610*4882a593Smuzhiyun {
611*4882a593Smuzhiyun u8 res; /* all operands in native machine order */
612*4882a593Smuzhiyun
613*4882a593Smuzhiyun res = d | s;
614*4882a593Smuzhiyun no_carry_byte_side_eff(res);
615*4882a593Smuzhiyun
616*4882a593Smuzhiyun return res;
617*4882a593Smuzhiyun }
618*4882a593Smuzhiyun
619*4882a593Smuzhiyun /****************************************************************************
620*4882a593Smuzhiyun REMARKS:
621*4882a593Smuzhiyun Implements the OR instruction and side effects.
622*4882a593Smuzhiyun ****************************************************************************/
or_word(u16 d,u16 s)623*4882a593Smuzhiyun u16 or_word(u16 d, u16 s)
624*4882a593Smuzhiyun {
625*4882a593Smuzhiyun u16 res; /* all operands in native machine order */
626*4882a593Smuzhiyun
627*4882a593Smuzhiyun res = d | s;
628*4882a593Smuzhiyun no_carry_word_side_eff(res);
629*4882a593Smuzhiyun return res;
630*4882a593Smuzhiyun }
631*4882a593Smuzhiyun
632*4882a593Smuzhiyun /****************************************************************************
633*4882a593Smuzhiyun REMARKS:
634*4882a593Smuzhiyun Implements the OR instruction and side effects.
635*4882a593Smuzhiyun ****************************************************************************/
or_long(u32 d,u32 s)636*4882a593Smuzhiyun u32 or_long(u32 d, u32 s)
637*4882a593Smuzhiyun {
638*4882a593Smuzhiyun u32 res; /* all operands in native machine order */
639*4882a593Smuzhiyun
640*4882a593Smuzhiyun res = d | s;
641*4882a593Smuzhiyun no_carry_long_side_eff(res);
642*4882a593Smuzhiyun return res;
643*4882a593Smuzhiyun }
644*4882a593Smuzhiyun
645*4882a593Smuzhiyun /****************************************************************************
646*4882a593Smuzhiyun REMARKS:
647*4882a593Smuzhiyun Implements the OR instruction and side effects.
648*4882a593Smuzhiyun ****************************************************************************/
neg_byte(u8 s)649*4882a593Smuzhiyun u8 neg_byte(u8 s)
650*4882a593Smuzhiyun {
651*4882a593Smuzhiyun u8 res;
652*4882a593Smuzhiyun
653*4882a593Smuzhiyun CONDITIONAL_SET_FLAG(s != 0, F_CF);
654*4882a593Smuzhiyun res = (u8)-s;
655*4882a593Smuzhiyun set_szp_flags_8(res);
656*4882a593Smuzhiyun calc_borrow_chain(8, 0, s, res, 0);
657*4882a593Smuzhiyun
658*4882a593Smuzhiyun return res;
659*4882a593Smuzhiyun }
660*4882a593Smuzhiyun
661*4882a593Smuzhiyun /****************************************************************************
662*4882a593Smuzhiyun REMARKS:
663*4882a593Smuzhiyun Implements the OR instruction and side effects.
664*4882a593Smuzhiyun ****************************************************************************/
neg_word(u16 s)665*4882a593Smuzhiyun u16 neg_word(u16 s)
666*4882a593Smuzhiyun {
667*4882a593Smuzhiyun u16 res;
668*4882a593Smuzhiyun
669*4882a593Smuzhiyun CONDITIONAL_SET_FLAG(s != 0, F_CF);
670*4882a593Smuzhiyun res = (u16)-s;
671*4882a593Smuzhiyun set_szp_flags_16((u16)res);
672*4882a593Smuzhiyun calc_borrow_chain(16, 0, s, res, 0);
673*4882a593Smuzhiyun
674*4882a593Smuzhiyun return res;
675*4882a593Smuzhiyun }
676*4882a593Smuzhiyun
677*4882a593Smuzhiyun /****************************************************************************
678*4882a593Smuzhiyun REMARKS:
679*4882a593Smuzhiyun Implements the OR instruction and side effects.
680*4882a593Smuzhiyun ****************************************************************************/
neg_long(u32 s)681*4882a593Smuzhiyun u32 neg_long(u32 s)
682*4882a593Smuzhiyun {
683*4882a593Smuzhiyun u32 res;
684*4882a593Smuzhiyun
685*4882a593Smuzhiyun CONDITIONAL_SET_FLAG(s != 0, F_CF);
686*4882a593Smuzhiyun res = (u32)-s;
687*4882a593Smuzhiyun set_szp_flags_32(res);
688*4882a593Smuzhiyun calc_borrow_chain(32, 0, s, res, 0);
689*4882a593Smuzhiyun
690*4882a593Smuzhiyun return res;
691*4882a593Smuzhiyun }
692*4882a593Smuzhiyun
693*4882a593Smuzhiyun /****************************************************************************
694*4882a593Smuzhiyun REMARKS:
695*4882a593Smuzhiyun Implements the NOT instruction and side effects.
696*4882a593Smuzhiyun ****************************************************************************/
not_byte(u8 s)697*4882a593Smuzhiyun u8 not_byte(u8 s)
698*4882a593Smuzhiyun {
699*4882a593Smuzhiyun return ~s;
700*4882a593Smuzhiyun }
701*4882a593Smuzhiyun
702*4882a593Smuzhiyun /****************************************************************************
703*4882a593Smuzhiyun REMARKS:
704*4882a593Smuzhiyun Implements the NOT instruction and side effects.
705*4882a593Smuzhiyun ****************************************************************************/
not_word(u16 s)706*4882a593Smuzhiyun u16 not_word(u16 s)
707*4882a593Smuzhiyun {
708*4882a593Smuzhiyun return ~s;
709*4882a593Smuzhiyun }
710*4882a593Smuzhiyun
711*4882a593Smuzhiyun /****************************************************************************
712*4882a593Smuzhiyun REMARKS:
713*4882a593Smuzhiyun Implements the NOT instruction and side effects.
714*4882a593Smuzhiyun ****************************************************************************/
not_long(u32 s)715*4882a593Smuzhiyun u32 not_long(u32 s)
716*4882a593Smuzhiyun {
717*4882a593Smuzhiyun return ~s;
718*4882a593Smuzhiyun }
719*4882a593Smuzhiyun
720*4882a593Smuzhiyun /****************************************************************************
721*4882a593Smuzhiyun REMARKS:
722*4882a593Smuzhiyun Implements the RCL instruction and side effects.
723*4882a593Smuzhiyun ****************************************************************************/
rcl_byte(u8 d,u8 s)724*4882a593Smuzhiyun u8 rcl_byte(u8 d, u8 s)
725*4882a593Smuzhiyun {
726*4882a593Smuzhiyun unsigned int res, cnt, mask, cf;
727*4882a593Smuzhiyun
728*4882a593Smuzhiyun /* s is the rotate distance. It varies from 0 - 8. */
729*4882a593Smuzhiyun /* have
730*4882a593Smuzhiyun
731*4882a593Smuzhiyun CF B_7 B_6 B_5 B_4 B_3 B_2 B_1 B_0
732*4882a593Smuzhiyun
733*4882a593Smuzhiyun want to rotate through the carry by "s" bits. We could
734*4882a593Smuzhiyun loop, but that's inefficient. So the width is 9,
735*4882a593Smuzhiyun and we split into three parts:
736*4882a593Smuzhiyun
737*4882a593Smuzhiyun The new carry flag (was B_n)
738*4882a593Smuzhiyun the stuff in B_n-1 .. B_0
739*4882a593Smuzhiyun the stuff in B_7 .. B_n+1
740*4882a593Smuzhiyun
741*4882a593Smuzhiyun The new rotate is done mod 9, and given this,
742*4882a593Smuzhiyun for a rotation of n bits (mod 9) the new carry flag is
743*4882a593Smuzhiyun then located n bits from the MSB. The low part is
744*4882a593Smuzhiyun then shifted up cnt bits, and the high part is or'd
745*4882a593Smuzhiyun in. Using CAPS for new values, and lowercase for the
746*4882a593Smuzhiyun original values, this can be expressed as:
747*4882a593Smuzhiyun
748*4882a593Smuzhiyun IF n > 0
749*4882a593Smuzhiyun 1) CF <- b_(8-n)
750*4882a593Smuzhiyun 2) B_(7) .. B_(n) <- b_(8-(n+1)) .. b_0
751*4882a593Smuzhiyun 3) B_(n-1) <- cf
752*4882a593Smuzhiyun 4) B_(n-2) .. B_0 <- b_7 .. b_(8-(n-1))
753*4882a593Smuzhiyun */
754*4882a593Smuzhiyun res = d;
755*4882a593Smuzhiyun if ((cnt = s % 9) != 0) {
756*4882a593Smuzhiyun /* extract the new CARRY FLAG. */
757*4882a593Smuzhiyun /* CF <- b_(8-n) */
758*4882a593Smuzhiyun cf = (d >> (8 - cnt)) & 0x1;
759*4882a593Smuzhiyun
760*4882a593Smuzhiyun /* get the low stuff which rotated
761*4882a593Smuzhiyun into the range B_7 .. B_cnt */
762*4882a593Smuzhiyun /* B_(7) .. B_(n) <- b_(8-(n+1)) .. b_0 */
763*4882a593Smuzhiyun /* note that the right hand side done by the mask */
764*4882a593Smuzhiyun res = (d << cnt) & 0xff;
765*4882a593Smuzhiyun
766*4882a593Smuzhiyun /* now the high stuff which rotated around
767*4882a593Smuzhiyun into the positions B_cnt-2 .. B_0 */
768*4882a593Smuzhiyun /* B_(n-2) .. B_0 <- b_7 .. b_(8-(n-1)) */
769*4882a593Smuzhiyun /* shift it downward, 7-(n-2) = 9-n positions.
770*4882a593Smuzhiyun and mask off the result before or'ing in.
771*4882a593Smuzhiyun */
772*4882a593Smuzhiyun mask = (1 << (cnt - 1)) - 1;
773*4882a593Smuzhiyun res |= (d >> (9 - cnt)) & mask;
774*4882a593Smuzhiyun
775*4882a593Smuzhiyun /* if the carry flag was set, or it in. */
776*4882a593Smuzhiyun if (ACCESS_FLAG(F_CF)) { /* carry flag is set */
777*4882a593Smuzhiyun /* B_(n-1) <- cf */
778*4882a593Smuzhiyun res |= 1 << (cnt - 1);
779*4882a593Smuzhiyun }
780*4882a593Smuzhiyun /* set the new carry flag, based on the variable "cf" */
781*4882a593Smuzhiyun CONDITIONAL_SET_FLAG(cf, F_CF);
782*4882a593Smuzhiyun /* OVERFLOW is set *IFF* cnt==1, then it is the
783*4882a593Smuzhiyun xor of CF and the most significant bit. Blecck. */
784*4882a593Smuzhiyun /* parenthesized this expression since it appears to
785*4882a593Smuzhiyun be causing OF to be misset */
786*4882a593Smuzhiyun CONDITIONAL_SET_FLAG(cnt == 1 && XOR2(cf + ((res >> 6) & 0x2)),
787*4882a593Smuzhiyun F_OF);
788*4882a593Smuzhiyun
789*4882a593Smuzhiyun }
790*4882a593Smuzhiyun return (u8)res;
791*4882a593Smuzhiyun }
792*4882a593Smuzhiyun
793*4882a593Smuzhiyun /****************************************************************************
794*4882a593Smuzhiyun REMARKS:
795*4882a593Smuzhiyun Implements the RCL instruction and side effects.
796*4882a593Smuzhiyun ****************************************************************************/
rcl_word(u16 d,u8 s)797*4882a593Smuzhiyun u16 rcl_word(u16 d, u8 s)
798*4882a593Smuzhiyun {
799*4882a593Smuzhiyun unsigned int res, cnt, mask, cf;
800*4882a593Smuzhiyun
801*4882a593Smuzhiyun res = d;
802*4882a593Smuzhiyun if ((cnt = s % 17) != 0) {
803*4882a593Smuzhiyun cf = (d >> (16 - cnt)) & 0x1;
804*4882a593Smuzhiyun res = (d << cnt) & 0xffff;
805*4882a593Smuzhiyun mask = (1 << (cnt - 1)) - 1;
806*4882a593Smuzhiyun res |= (d >> (17 - cnt)) & mask;
807*4882a593Smuzhiyun if (ACCESS_FLAG(F_CF)) {
808*4882a593Smuzhiyun res |= 1 << (cnt - 1);
809*4882a593Smuzhiyun }
810*4882a593Smuzhiyun CONDITIONAL_SET_FLAG(cf, F_CF);
811*4882a593Smuzhiyun CONDITIONAL_SET_FLAG(cnt == 1 && XOR2(cf + ((res >> 14) & 0x2)),
812*4882a593Smuzhiyun F_OF);
813*4882a593Smuzhiyun }
814*4882a593Smuzhiyun return (u16)res;
815*4882a593Smuzhiyun }
816*4882a593Smuzhiyun
817*4882a593Smuzhiyun /****************************************************************************
818*4882a593Smuzhiyun REMARKS:
819*4882a593Smuzhiyun Implements the RCL instruction and side effects.
820*4882a593Smuzhiyun ****************************************************************************/
rcl_long(u32 d,u8 s)821*4882a593Smuzhiyun u32 rcl_long(u32 d, u8 s)
822*4882a593Smuzhiyun {
823*4882a593Smuzhiyun u32 res, cnt, mask, cf;
824*4882a593Smuzhiyun
825*4882a593Smuzhiyun res = d;
826*4882a593Smuzhiyun if ((cnt = s % 33) != 0) {
827*4882a593Smuzhiyun cf = (d >> (32 - cnt)) & 0x1;
828*4882a593Smuzhiyun res = (d << cnt) & 0xffffffff;
829*4882a593Smuzhiyun mask = (1 << (cnt - 1)) - 1;
830*4882a593Smuzhiyun res |= (d >> (33 - cnt)) & mask;
831*4882a593Smuzhiyun if (ACCESS_FLAG(F_CF)) { /* carry flag is set */
832*4882a593Smuzhiyun res |= 1 << (cnt - 1);
833*4882a593Smuzhiyun }
834*4882a593Smuzhiyun CONDITIONAL_SET_FLAG(cf, F_CF);
835*4882a593Smuzhiyun CONDITIONAL_SET_FLAG(cnt == 1 && XOR2(cf + ((res >> 30) & 0x2)),
836*4882a593Smuzhiyun F_OF);
837*4882a593Smuzhiyun }
838*4882a593Smuzhiyun return res;
839*4882a593Smuzhiyun }
840*4882a593Smuzhiyun
841*4882a593Smuzhiyun /****************************************************************************
842*4882a593Smuzhiyun REMARKS:
843*4882a593Smuzhiyun Implements the RCR instruction and side effects.
844*4882a593Smuzhiyun ****************************************************************************/
rcr_byte(u8 d,u8 s)845*4882a593Smuzhiyun u8 rcr_byte(u8 d, u8 s)
846*4882a593Smuzhiyun {
847*4882a593Smuzhiyun u32 res, cnt;
848*4882a593Smuzhiyun u32 mask, cf, ocf = 0;
849*4882a593Smuzhiyun
850*4882a593Smuzhiyun /* rotate right through carry */
851*4882a593Smuzhiyun /*
852*4882a593Smuzhiyun s is the rotate distance. It varies from 0 - 8.
853*4882a593Smuzhiyun d is the byte object rotated.
854*4882a593Smuzhiyun
855*4882a593Smuzhiyun have
856*4882a593Smuzhiyun
857*4882a593Smuzhiyun CF B_7 B_6 B_5 B_4 B_3 B_2 B_1 B_0
858*4882a593Smuzhiyun
859*4882a593Smuzhiyun The new rotate is done mod 9, and given this,
860*4882a593Smuzhiyun for a rotation of n bits (mod 9) the new carry flag is
861*4882a593Smuzhiyun then located n bits from the LSB. The low part is
862*4882a593Smuzhiyun then shifted up cnt bits, and the high part is or'd
863*4882a593Smuzhiyun in. Using CAPS for new values, and lowercase for the
864*4882a593Smuzhiyun original values, this can be expressed as:
865*4882a593Smuzhiyun
866*4882a593Smuzhiyun IF n > 0
867*4882a593Smuzhiyun 1) CF <- b_(n-1)
868*4882a593Smuzhiyun 2) B_(8-(n+1)) .. B_(0) <- b_(7) .. b_(n)
869*4882a593Smuzhiyun 3) B_(8-n) <- cf
870*4882a593Smuzhiyun 4) B_(7) .. B_(8-(n-1)) <- b_(n-2) .. b_(0)
871*4882a593Smuzhiyun */
872*4882a593Smuzhiyun res = d;
873*4882a593Smuzhiyun if ((cnt = s % 9) != 0) {
874*4882a593Smuzhiyun /* extract the new CARRY FLAG. */
875*4882a593Smuzhiyun /* CF <- b_(n-1) */
876*4882a593Smuzhiyun if (cnt == 1) {
877*4882a593Smuzhiyun cf = d & 0x1;
878*4882a593Smuzhiyun /* note hackery here. Access_flag(..) evaluates to either
879*4882a593Smuzhiyun 0 if flag not set
880*4882a593Smuzhiyun non-zero if flag is set.
881*4882a593Smuzhiyun doing access_flag(..) != 0 casts that into either
882*4882a593Smuzhiyun 0..1 in any representation of the flags register
883*4882a593Smuzhiyun (i.e. packed bit array or unpacked.)
884*4882a593Smuzhiyun */
885*4882a593Smuzhiyun ocf = ACCESS_FLAG(F_CF) != 0;
886*4882a593Smuzhiyun } else
887*4882a593Smuzhiyun cf = (d >> (cnt - 1)) & 0x1;
888*4882a593Smuzhiyun
889*4882a593Smuzhiyun /* B_(8-(n+1)) .. B_(0) <- b_(7) .. b_n */
890*4882a593Smuzhiyun /* note that the right hand side done by the mask
891*4882a593Smuzhiyun This is effectively done by shifting the
892*4882a593Smuzhiyun object to the right. The result must be masked,
893*4882a593Smuzhiyun in case the object came in and was treated
894*4882a593Smuzhiyun as a negative number. Needed??? */
895*4882a593Smuzhiyun
896*4882a593Smuzhiyun mask = (1 << (8 - cnt)) - 1;
897*4882a593Smuzhiyun res = (d >> cnt) & mask;
898*4882a593Smuzhiyun
899*4882a593Smuzhiyun /* now the high stuff which rotated around
900*4882a593Smuzhiyun into the positions B_cnt-2 .. B_0 */
901*4882a593Smuzhiyun /* B_(7) .. B_(8-(n-1)) <- b_(n-2) .. b_(0) */
902*4882a593Smuzhiyun /* shift it downward, 7-(n-2) = 9-n positions.
903*4882a593Smuzhiyun and mask off the result before or'ing in.
904*4882a593Smuzhiyun */
905*4882a593Smuzhiyun res |= (d << (9 - cnt));
906*4882a593Smuzhiyun
907*4882a593Smuzhiyun /* if the carry flag was set, or it in. */
908*4882a593Smuzhiyun if (ACCESS_FLAG(F_CF)) { /* carry flag is set */
909*4882a593Smuzhiyun /* B_(8-n) <- cf */
910*4882a593Smuzhiyun res |= 1 << (8 - cnt);
911*4882a593Smuzhiyun }
912*4882a593Smuzhiyun /* set the new carry flag, based on the variable "cf" */
913*4882a593Smuzhiyun CONDITIONAL_SET_FLAG(cf, F_CF);
914*4882a593Smuzhiyun /* OVERFLOW is set *IFF* cnt==1, then it is the
915*4882a593Smuzhiyun xor of CF and the most significant bit. Blecck. */
916*4882a593Smuzhiyun /* parenthesized... */
917*4882a593Smuzhiyun if (cnt == 1) {
918*4882a593Smuzhiyun CONDITIONAL_SET_FLAG(XOR2(ocf + ((d >> 6) & 0x2)),
919*4882a593Smuzhiyun F_OF);
920*4882a593Smuzhiyun }
921*4882a593Smuzhiyun }
922*4882a593Smuzhiyun return (u8)res;
923*4882a593Smuzhiyun }
924*4882a593Smuzhiyun
925*4882a593Smuzhiyun /****************************************************************************
926*4882a593Smuzhiyun REMARKS:
927*4882a593Smuzhiyun Implements the RCR instruction and side effects.
928*4882a593Smuzhiyun ****************************************************************************/
rcr_word(u16 d,u8 s)929*4882a593Smuzhiyun u16 rcr_word(u16 d, u8 s)
930*4882a593Smuzhiyun {
931*4882a593Smuzhiyun u32 res, cnt;
932*4882a593Smuzhiyun u32 mask, cf, ocf = 0;
933*4882a593Smuzhiyun
934*4882a593Smuzhiyun /* rotate right through carry */
935*4882a593Smuzhiyun res = d;
936*4882a593Smuzhiyun if ((cnt = s % 17) != 0) {
937*4882a593Smuzhiyun if (cnt == 1) {
938*4882a593Smuzhiyun cf = d & 0x1;
939*4882a593Smuzhiyun ocf = ACCESS_FLAG(F_CF) != 0;
940*4882a593Smuzhiyun } else
941*4882a593Smuzhiyun cf = (d >> (cnt - 1)) & 0x1;
942*4882a593Smuzhiyun mask = (1 << (16 - cnt)) - 1;
943*4882a593Smuzhiyun res = (d >> cnt) & mask;
944*4882a593Smuzhiyun res |= (d << (17 - cnt));
945*4882a593Smuzhiyun if (ACCESS_FLAG(F_CF)) {
946*4882a593Smuzhiyun res |= 1 << (16 - cnt);
947*4882a593Smuzhiyun }
948*4882a593Smuzhiyun CONDITIONAL_SET_FLAG(cf, F_CF);
949*4882a593Smuzhiyun if (cnt == 1) {
950*4882a593Smuzhiyun CONDITIONAL_SET_FLAG(XOR2(ocf + ((d >> 14) & 0x2)),
951*4882a593Smuzhiyun F_OF);
952*4882a593Smuzhiyun }
953*4882a593Smuzhiyun }
954*4882a593Smuzhiyun return (u16)res;
955*4882a593Smuzhiyun }
956*4882a593Smuzhiyun
957*4882a593Smuzhiyun /****************************************************************************
958*4882a593Smuzhiyun REMARKS:
959*4882a593Smuzhiyun Implements the RCR instruction and side effects.
960*4882a593Smuzhiyun ****************************************************************************/
rcr_long(u32 d,u8 s)961*4882a593Smuzhiyun u32 rcr_long(u32 d, u8 s)
962*4882a593Smuzhiyun {
963*4882a593Smuzhiyun u32 res, cnt;
964*4882a593Smuzhiyun u32 mask, cf, ocf = 0;
965*4882a593Smuzhiyun
966*4882a593Smuzhiyun /* rotate right through carry */
967*4882a593Smuzhiyun res = d;
968*4882a593Smuzhiyun if ((cnt = s % 33) != 0) {
969*4882a593Smuzhiyun if (cnt == 1) {
970*4882a593Smuzhiyun cf = d & 0x1;
971*4882a593Smuzhiyun ocf = ACCESS_FLAG(F_CF) != 0;
972*4882a593Smuzhiyun } else
973*4882a593Smuzhiyun cf = (d >> (cnt - 1)) & 0x1;
974*4882a593Smuzhiyun mask = (1 << (32 - cnt)) - 1;
975*4882a593Smuzhiyun res = (d >> cnt) & mask;
976*4882a593Smuzhiyun if (cnt != 1)
977*4882a593Smuzhiyun res |= (d << (33 - cnt));
978*4882a593Smuzhiyun if (ACCESS_FLAG(F_CF)) { /* carry flag is set */
979*4882a593Smuzhiyun res |= 1 << (32 - cnt);
980*4882a593Smuzhiyun }
981*4882a593Smuzhiyun CONDITIONAL_SET_FLAG(cf, F_CF);
982*4882a593Smuzhiyun if (cnt == 1) {
983*4882a593Smuzhiyun CONDITIONAL_SET_FLAG(XOR2(ocf + ((d >> 30) & 0x2)),
984*4882a593Smuzhiyun F_OF);
985*4882a593Smuzhiyun }
986*4882a593Smuzhiyun }
987*4882a593Smuzhiyun return res;
988*4882a593Smuzhiyun }
989*4882a593Smuzhiyun
990*4882a593Smuzhiyun /****************************************************************************
991*4882a593Smuzhiyun REMARKS:
992*4882a593Smuzhiyun Implements the ROL instruction and side effects.
993*4882a593Smuzhiyun ****************************************************************************/
rol_byte(u8 d,u8 s)994*4882a593Smuzhiyun u8 rol_byte(u8 d, u8 s)
995*4882a593Smuzhiyun {
996*4882a593Smuzhiyun unsigned int res, cnt, mask;
997*4882a593Smuzhiyun
998*4882a593Smuzhiyun /* rotate left */
999*4882a593Smuzhiyun /*
1000*4882a593Smuzhiyun s is the rotate distance. It varies from 0 - 8.
1001*4882a593Smuzhiyun d is the byte object rotated.
1002*4882a593Smuzhiyun
1003*4882a593Smuzhiyun have
1004*4882a593Smuzhiyun
1005*4882a593Smuzhiyun CF B_7 ... B_0
1006*4882a593Smuzhiyun
1007*4882a593Smuzhiyun The new rotate is done mod 8.
1008*4882a593Smuzhiyun Much simpler than the "rcl" or "rcr" operations.
1009*4882a593Smuzhiyun
1010*4882a593Smuzhiyun IF n > 0
1011*4882a593Smuzhiyun 1) B_(7) .. B_(n) <- b_(8-(n+1)) .. b_(0)
1012*4882a593Smuzhiyun 2) B_(n-1) .. B_(0) <- b_(7) .. b_(8-n)
1013*4882a593Smuzhiyun */
1014*4882a593Smuzhiyun res = d;
1015*4882a593Smuzhiyun if ((cnt = s % 8) != 0) {
1016*4882a593Smuzhiyun /* B_(7) .. B_(n) <- b_(8-(n+1)) .. b_(0) */
1017*4882a593Smuzhiyun res = (d << cnt);
1018*4882a593Smuzhiyun
1019*4882a593Smuzhiyun /* B_(n-1) .. B_(0) <- b_(7) .. b_(8-n) */
1020*4882a593Smuzhiyun mask = (1 << cnt) - 1;
1021*4882a593Smuzhiyun res |= (d >> (8 - cnt)) & mask;
1022*4882a593Smuzhiyun
1023*4882a593Smuzhiyun /* set the new carry flag, Note that it is the low order
1024*4882a593Smuzhiyun bit of the result!!! */
1025*4882a593Smuzhiyun CONDITIONAL_SET_FLAG(res & 0x1, F_CF);
1026*4882a593Smuzhiyun /* OVERFLOW is set *IFF* s==1, then it is the
1027*4882a593Smuzhiyun xor of CF and the most significant bit. Blecck. */
1028*4882a593Smuzhiyun CONDITIONAL_SET_FLAG(s == 1 &&
1029*4882a593Smuzhiyun XOR2((res & 0x1) + ((res >> 6) & 0x2)),
1030*4882a593Smuzhiyun F_OF);
1031*4882a593Smuzhiyun } if (s != 0) {
1032*4882a593Smuzhiyun /* set the new carry flag, Note that it is the low order
1033*4882a593Smuzhiyun bit of the result!!! */
1034*4882a593Smuzhiyun CONDITIONAL_SET_FLAG(res & 0x1, F_CF);
1035*4882a593Smuzhiyun }
1036*4882a593Smuzhiyun return (u8)res;
1037*4882a593Smuzhiyun }
1038*4882a593Smuzhiyun
1039*4882a593Smuzhiyun /****************************************************************************
1040*4882a593Smuzhiyun REMARKS:
1041*4882a593Smuzhiyun Implements the ROL instruction and side effects.
1042*4882a593Smuzhiyun ****************************************************************************/
rol_word(u16 d,u8 s)1043*4882a593Smuzhiyun u16 rol_word(u16 d, u8 s)
1044*4882a593Smuzhiyun {
1045*4882a593Smuzhiyun unsigned int res, cnt, mask;
1046*4882a593Smuzhiyun
1047*4882a593Smuzhiyun res = d;
1048*4882a593Smuzhiyun if ((cnt = s % 16) != 0) {
1049*4882a593Smuzhiyun res = (d << cnt);
1050*4882a593Smuzhiyun mask = (1 << cnt) - 1;
1051*4882a593Smuzhiyun res |= (d >> (16 - cnt)) & mask;
1052*4882a593Smuzhiyun CONDITIONAL_SET_FLAG(res & 0x1, F_CF);
1053*4882a593Smuzhiyun CONDITIONAL_SET_FLAG(s == 1 &&
1054*4882a593Smuzhiyun XOR2((res & 0x1) + ((res >> 14) & 0x2)),
1055*4882a593Smuzhiyun F_OF);
1056*4882a593Smuzhiyun } if (s != 0) {
1057*4882a593Smuzhiyun /* set the new carry flag, Note that it is the low order
1058*4882a593Smuzhiyun bit of the result!!! */
1059*4882a593Smuzhiyun CONDITIONAL_SET_FLAG(res & 0x1, F_CF);
1060*4882a593Smuzhiyun }
1061*4882a593Smuzhiyun return (u16)res;
1062*4882a593Smuzhiyun }
1063*4882a593Smuzhiyun
1064*4882a593Smuzhiyun /****************************************************************************
1065*4882a593Smuzhiyun REMARKS:
1066*4882a593Smuzhiyun Implements the ROL instruction and side effects.
1067*4882a593Smuzhiyun ****************************************************************************/
rol_long(u32 d,u8 s)1068*4882a593Smuzhiyun u32 rol_long(u32 d, u8 s)
1069*4882a593Smuzhiyun {
1070*4882a593Smuzhiyun u32 res, cnt, mask;
1071*4882a593Smuzhiyun
1072*4882a593Smuzhiyun res = d;
1073*4882a593Smuzhiyun if ((cnt = s % 32) != 0) {
1074*4882a593Smuzhiyun res = (d << cnt);
1075*4882a593Smuzhiyun mask = (1 << cnt) - 1;
1076*4882a593Smuzhiyun res |= (d >> (32 - cnt)) & mask;
1077*4882a593Smuzhiyun CONDITIONAL_SET_FLAG(res & 0x1, F_CF);
1078*4882a593Smuzhiyun CONDITIONAL_SET_FLAG(s == 1 &&
1079*4882a593Smuzhiyun XOR2((res & 0x1) + ((res >> 30) & 0x2)),
1080*4882a593Smuzhiyun F_OF);
1081*4882a593Smuzhiyun } if (s != 0) {
1082*4882a593Smuzhiyun /* set the new carry flag, Note that it is the low order
1083*4882a593Smuzhiyun bit of the result!!! */
1084*4882a593Smuzhiyun CONDITIONAL_SET_FLAG(res & 0x1, F_CF);
1085*4882a593Smuzhiyun }
1086*4882a593Smuzhiyun return res;
1087*4882a593Smuzhiyun }
1088*4882a593Smuzhiyun
1089*4882a593Smuzhiyun /****************************************************************************
1090*4882a593Smuzhiyun REMARKS:
1091*4882a593Smuzhiyun Implements the ROR instruction and side effects.
1092*4882a593Smuzhiyun ****************************************************************************/
ror_byte(u8 d,u8 s)1093*4882a593Smuzhiyun u8 ror_byte(u8 d, u8 s)
1094*4882a593Smuzhiyun {
1095*4882a593Smuzhiyun unsigned int res, cnt, mask;
1096*4882a593Smuzhiyun
1097*4882a593Smuzhiyun /* rotate right */
1098*4882a593Smuzhiyun /*
1099*4882a593Smuzhiyun s is the rotate distance. It varies from 0 - 8.
1100*4882a593Smuzhiyun d is the byte object rotated.
1101*4882a593Smuzhiyun
1102*4882a593Smuzhiyun have
1103*4882a593Smuzhiyun
1104*4882a593Smuzhiyun B_7 ... B_0
1105*4882a593Smuzhiyun
1106*4882a593Smuzhiyun The rotate is done mod 8.
1107*4882a593Smuzhiyun
1108*4882a593Smuzhiyun IF n > 0
1109*4882a593Smuzhiyun 1) B_(8-(n+1)) .. B_(0) <- b_(7) .. b_(n)
1110*4882a593Smuzhiyun 2) B_(7) .. B_(8-n) <- b_(n-1) .. b_(0)
1111*4882a593Smuzhiyun */
1112*4882a593Smuzhiyun res = d;
1113*4882a593Smuzhiyun if ((cnt = s % 8) != 0) { /* not a typo, do nada if cnt==0 */
1114*4882a593Smuzhiyun /* B_(7) .. B_(8-n) <- b_(n-1) .. b_(0) */
1115*4882a593Smuzhiyun res = (d << (8 - cnt));
1116*4882a593Smuzhiyun
1117*4882a593Smuzhiyun /* B_(8-(n+1)) .. B_(0) <- b_(7) .. b_(n) */
1118*4882a593Smuzhiyun mask = (1 << (8 - cnt)) - 1;
1119*4882a593Smuzhiyun res |= (d >> (cnt)) & mask;
1120*4882a593Smuzhiyun
1121*4882a593Smuzhiyun /* set the new carry flag, Note that it is the low order
1122*4882a593Smuzhiyun bit of the result!!! */
1123*4882a593Smuzhiyun CONDITIONAL_SET_FLAG(res & 0x80, F_CF);
1124*4882a593Smuzhiyun /* OVERFLOW is set *IFF* s==1, then it is the
1125*4882a593Smuzhiyun xor of the two most significant bits. Blecck. */
1126*4882a593Smuzhiyun CONDITIONAL_SET_FLAG(s == 1 && XOR2(res >> 6), F_OF);
1127*4882a593Smuzhiyun } else if (s != 0) {
1128*4882a593Smuzhiyun /* set the new carry flag, Note that it is the low order
1129*4882a593Smuzhiyun bit of the result!!! */
1130*4882a593Smuzhiyun CONDITIONAL_SET_FLAG(res & 0x80, F_CF);
1131*4882a593Smuzhiyun }
1132*4882a593Smuzhiyun return (u8)res;
1133*4882a593Smuzhiyun }
1134*4882a593Smuzhiyun
1135*4882a593Smuzhiyun /****************************************************************************
1136*4882a593Smuzhiyun REMARKS:
1137*4882a593Smuzhiyun Implements the ROR instruction and side effects.
1138*4882a593Smuzhiyun ****************************************************************************/
ror_word(u16 d,u8 s)1139*4882a593Smuzhiyun u16 ror_word(u16 d, u8 s)
1140*4882a593Smuzhiyun {
1141*4882a593Smuzhiyun unsigned int res, cnt, mask;
1142*4882a593Smuzhiyun
1143*4882a593Smuzhiyun res = d;
1144*4882a593Smuzhiyun if ((cnt = s % 16) != 0) {
1145*4882a593Smuzhiyun res = (d << (16 - cnt));
1146*4882a593Smuzhiyun mask = (1 << (16 - cnt)) - 1;
1147*4882a593Smuzhiyun res |= (d >> (cnt)) & mask;
1148*4882a593Smuzhiyun CONDITIONAL_SET_FLAG(res & 0x8000, F_CF);
1149*4882a593Smuzhiyun CONDITIONAL_SET_FLAG(s == 1 && XOR2(res >> 14), F_OF);
1150*4882a593Smuzhiyun } else if (s != 0) {
1151*4882a593Smuzhiyun /* set the new carry flag, Note that it is the low order
1152*4882a593Smuzhiyun bit of the result!!! */
1153*4882a593Smuzhiyun CONDITIONAL_SET_FLAG(res & 0x8000, F_CF);
1154*4882a593Smuzhiyun }
1155*4882a593Smuzhiyun return (u16)res;
1156*4882a593Smuzhiyun }
1157*4882a593Smuzhiyun
1158*4882a593Smuzhiyun /****************************************************************************
1159*4882a593Smuzhiyun REMARKS:
1160*4882a593Smuzhiyun Implements the ROR instruction and side effects.
1161*4882a593Smuzhiyun ****************************************************************************/
ror_long(u32 d,u8 s)1162*4882a593Smuzhiyun u32 ror_long(u32 d, u8 s)
1163*4882a593Smuzhiyun {
1164*4882a593Smuzhiyun u32 res, cnt, mask;
1165*4882a593Smuzhiyun
1166*4882a593Smuzhiyun res = d;
1167*4882a593Smuzhiyun if ((cnt = s % 32) != 0) {
1168*4882a593Smuzhiyun res = (d << (32 - cnt));
1169*4882a593Smuzhiyun mask = (1 << (32 - cnt)) - 1;
1170*4882a593Smuzhiyun res |= (d >> (cnt)) & mask;
1171*4882a593Smuzhiyun CONDITIONAL_SET_FLAG(res & 0x80000000, F_CF);
1172*4882a593Smuzhiyun CONDITIONAL_SET_FLAG(s == 1 && XOR2(res >> 30), F_OF);
1173*4882a593Smuzhiyun } else if (s != 0) {
1174*4882a593Smuzhiyun /* set the new carry flag, Note that it is the low order
1175*4882a593Smuzhiyun bit of the result!!! */
1176*4882a593Smuzhiyun CONDITIONAL_SET_FLAG(res & 0x80000000, F_CF);
1177*4882a593Smuzhiyun }
1178*4882a593Smuzhiyun return res;
1179*4882a593Smuzhiyun }
1180*4882a593Smuzhiyun
1181*4882a593Smuzhiyun /****************************************************************************
1182*4882a593Smuzhiyun REMARKS:
1183*4882a593Smuzhiyun Implements the SHL instruction and side effects.
1184*4882a593Smuzhiyun ****************************************************************************/
shl_byte(u8 d,u8 s)1185*4882a593Smuzhiyun u8 shl_byte(u8 d, u8 s)
1186*4882a593Smuzhiyun {
1187*4882a593Smuzhiyun unsigned int cnt, res, cf;
1188*4882a593Smuzhiyun
1189*4882a593Smuzhiyun if (s < 8) {
1190*4882a593Smuzhiyun cnt = s % 8;
1191*4882a593Smuzhiyun
1192*4882a593Smuzhiyun /* last bit shifted out goes into carry flag */
1193*4882a593Smuzhiyun if (cnt > 0) {
1194*4882a593Smuzhiyun res = d << cnt;
1195*4882a593Smuzhiyun cf = d & (1 << (8 - cnt));
1196*4882a593Smuzhiyun CONDITIONAL_SET_FLAG(cf, F_CF);
1197*4882a593Smuzhiyun set_szp_flags_8((u8)res);
1198*4882a593Smuzhiyun } else {
1199*4882a593Smuzhiyun res = (u8) d;
1200*4882a593Smuzhiyun }
1201*4882a593Smuzhiyun
1202*4882a593Smuzhiyun if (cnt == 1) {
1203*4882a593Smuzhiyun /* Needs simplification. */
1204*4882a593Smuzhiyun CONDITIONAL_SET_FLAG(
1205*4882a593Smuzhiyun (((res & 0x80) == 0x80) ^
1206*4882a593Smuzhiyun (ACCESS_FLAG(F_CF) != 0)),
1207*4882a593Smuzhiyun /* was (M.x86.R_FLG&F_CF)==F_CF)), */
1208*4882a593Smuzhiyun F_OF);
1209*4882a593Smuzhiyun } else {
1210*4882a593Smuzhiyun CLEAR_FLAG(F_OF);
1211*4882a593Smuzhiyun }
1212*4882a593Smuzhiyun } else {
1213*4882a593Smuzhiyun res = 0;
1214*4882a593Smuzhiyun CONDITIONAL_SET_FLAG((d << (s-1)) & 0x80, F_CF);
1215*4882a593Smuzhiyun CLEAR_FLAG(F_OF);
1216*4882a593Smuzhiyun CLEAR_FLAG(F_SF);
1217*4882a593Smuzhiyun SET_FLAG(F_PF);
1218*4882a593Smuzhiyun SET_FLAG(F_ZF);
1219*4882a593Smuzhiyun }
1220*4882a593Smuzhiyun return (u8)res;
1221*4882a593Smuzhiyun }
1222*4882a593Smuzhiyun
1223*4882a593Smuzhiyun /****************************************************************************
1224*4882a593Smuzhiyun REMARKS:
1225*4882a593Smuzhiyun Implements the SHL instruction and side effects.
1226*4882a593Smuzhiyun ****************************************************************************/
shl_word(u16 d,u8 s)1227*4882a593Smuzhiyun u16 shl_word(u16 d, u8 s)
1228*4882a593Smuzhiyun {
1229*4882a593Smuzhiyun unsigned int cnt, res, cf;
1230*4882a593Smuzhiyun
1231*4882a593Smuzhiyun if (s < 16) {
1232*4882a593Smuzhiyun cnt = s % 16;
1233*4882a593Smuzhiyun if (cnt > 0) {
1234*4882a593Smuzhiyun res = d << cnt;
1235*4882a593Smuzhiyun cf = d & (1 << (16 - cnt));
1236*4882a593Smuzhiyun CONDITIONAL_SET_FLAG(cf, F_CF);
1237*4882a593Smuzhiyun set_szp_flags_16((u16)res);
1238*4882a593Smuzhiyun } else {
1239*4882a593Smuzhiyun res = (u16) d;
1240*4882a593Smuzhiyun }
1241*4882a593Smuzhiyun
1242*4882a593Smuzhiyun if (cnt == 1) {
1243*4882a593Smuzhiyun CONDITIONAL_SET_FLAG(
1244*4882a593Smuzhiyun (((res & 0x8000) == 0x8000) ^
1245*4882a593Smuzhiyun (ACCESS_FLAG(F_CF) != 0)),
1246*4882a593Smuzhiyun F_OF);
1247*4882a593Smuzhiyun } else {
1248*4882a593Smuzhiyun CLEAR_FLAG(F_OF);
1249*4882a593Smuzhiyun }
1250*4882a593Smuzhiyun } else {
1251*4882a593Smuzhiyun res = 0;
1252*4882a593Smuzhiyun CONDITIONAL_SET_FLAG((d << (s-1)) & 0x8000, F_CF);
1253*4882a593Smuzhiyun CLEAR_FLAG(F_OF);
1254*4882a593Smuzhiyun CLEAR_FLAG(F_SF);
1255*4882a593Smuzhiyun SET_FLAG(F_PF);
1256*4882a593Smuzhiyun SET_FLAG(F_ZF);
1257*4882a593Smuzhiyun }
1258*4882a593Smuzhiyun return (u16)res;
1259*4882a593Smuzhiyun }
1260*4882a593Smuzhiyun
1261*4882a593Smuzhiyun /****************************************************************************
1262*4882a593Smuzhiyun REMARKS:
1263*4882a593Smuzhiyun Implements the SHL instruction and side effects.
1264*4882a593Smuzhiyun ****************************************************************************/
shl_long(u32 d,u8 s)1265*4882a593Smuzhiyun u32 shl_long(u32 d, u8 s)
1266*4882a593Smuzhiyun {
1267*4882a593Smuzhiyun unsigned int cnt, res, cf;
1268*4882a593Smuzhiyun
1269*4882a593Smuzhiyun if (s < 32) {
1270*4882a593Smuzhiyun cnt = s % 32;
1271*4882a593Smuzhiyun if (cnt > 0) {
1272*4882a593Smuzhiyun res = d << cnt;
1273*4882a593Smuzhiyun cf = d & (1 << (32 - cnt));
1274*4882a593Smuzhiyun CONDITIONAL_SET_FLAG(cf, F_CF);
1275*4882a593Smuzhiyun set_szp_flags_32((u32)res);
1276*4882a593Smuzhiyun } else {
1277*4882a593Smuzhiyun res = d;
1278*4882a593Smuzhiyun }
1279*4882a593Smuzhiyun if (cnt == 1) {
1280*4882a593Smuzhiyun CONDITIONAL_SET_FLAG((((res & 0x80000000) == 0x80000000) ^
1281*4882a593Smuzhiyun (ACCESS_FLAG(F_CF) != 0)), F_OF);
1282*4882a593Smuzhiyun } else {
1283*4882a593Smuzhiyun CLEAR_FLAG(F_OF);
1284*4882a593Smuzhiyun }
1285*4882a593Smuzhiyun } else {
1286*4882a593Smuzhiyun res = 0;
1287*4882a593Smuzhiyun CONDITIONAL_SET_FLAG((d << (s-1)) & 0x80000000, F_CF);
1288*4882a593Smuzhiyun CLEAR_FLAG(F_OF);
1289*4882a593Smuzhiyun CLEAR_FLAG(F_SF);
1290*4882a593Smuzhiyun SET_FLAG(F_PF);
1291*4882a593Smuzhiyun SET_FLAG(F_ZF);
1292*4882a593Smuzhiyun }
1293*4882a593Smuzhiyun return res;
1294*4882a593Smuzhiyun }
1295*4882a593Smuzhiyun
1296*4882a593Smuzhiyun /****************************************************************************
1297*4882a593Smuzhiyun REMARKS:
1298*4882a593Smuzhiyun Implements the SHR instruction and side effects.
1299*4882a593Smuzhiyun ****************************************************************************/
shr_byte(u8 d,u8 s)1300*4882a593Smuzhiyun u8 shr_byte(u8 d, u8 s)
1301*4882a593Smuzhiyun {
1302*4882a593Smuzhiyun unsigned int cnt, res, cf;
1303*4882a593Smuzhiyun
1304*4882a593Smuzhiyun if (s < 8) {
1305*4882a593Smuzhiyun cnt = s % 8;
1306*4882a593Smuzhiyun if (cnt > 0) {
1307*4882a593Smuzhiyun cf = d & (1 << (cnt - 1));
1308*4882a593Smuzhiyun res = d >> cnt;
1309*4882a593Smuzhiyun CONDITIONAL_SET_FLAG(cf, F_CF);
1310*4882a593Smuzhiyun set_szp_flags_8((u8)res);
1311*4882a593Smuzhiyun } else {
1312*4882a593Smuzhiyun res = (u8) d;
1313*4882a593Smuzhiyun }
1314*4882a593Smuzhiyun
1315*4882a593Smuzhiyun if (cnt == 1) {
1316*4882a593Smuzhiyun CONDITIONAL_SET_FLAG(XOR2(res >> 6), F_OF);
1317*4882a593Smuzhiyun } else {
1318*4882a593Smuzhiyun CLEAR_FLAG(F_OF);
1319*4882a593Smuzhiyun }
1320*4882a593Smuzhiyun } else {
1321*4882a593Smuzhiyun res = 0;
1322*4882a593Smuzhiyun CONDITIONAL_SET_FLAG((d >> (s-1)) & 0x1, F_CF);
1323*4882a593Smuzhiyun CLEAR_FLAG(F_OF);
1324*4882a593Smuzhiyun CLEAR_FLAG(F_SF);
1325*4882a593Smuzhiyun SET_FLAG(F_PF);
1326*4882a593Smuzhiyun SET_FLAG(F_ZF);
1327*4882a593Smuzhiyun }
1328*4882a593Smuzhiyun return (u8)res;
1329*4882a593Smuzhiyun }
1330*4882a593Smuzhiyun
1331*4882a593Smuzhiyun /****************************************************************************
1332*4882a593Smuzhiyun REMARKS:
1333*4882a593Smuzhiyun Implements the SHR instruction and side effects.
1334*4882a593Smuzhiyun ****************************************************************************/
shr_word(u16 d,u8 s)1335*4882a593Smuzhiyun u16 shr_word(u16 d, u8 s)
1336*4882a593Smuzhiyun {
1337*4882a593Smuzhiyun unsigned int cnt, res, cf;
1338*4882a593Smuzhiyun
1339*4882a593Smuzhiyun if (s < 16) {
1340*4882a593Smuzhiyun cnt = s % 16;
1341*4882a593Smuzhiyun if (cnt > 0) {
1342*4882a593Smuzhiyun cf = d & (1 << (cnt - 1));
1343*4882a593Smuzhiyun res = d >> cnt;
1344*4882a593Smuzhiyun CONDITIONAL_SET_FLAG(cf, F_CF);
1345*4882a593Smuzhiyun set_szp_flags_16((u16)res);
1346*4882a593Smuzhiyun } else {
1347*4882a593Smuzhiyun res = d;
1348*4882a593Smuzhiyun }
1349*4882a593Smuzhiyun
1350*4882a593Smuzhiyun if (cnt == 1) {
1351*4882a593Smuzhiyun CONDITIONAL_SET_FLAG(XOR2(res >> 14), F_OF);
1352*4882a593Smuzhiyun } else {
1353*4882a593Smuzhiyun CLEAR_FLAG(F_OF);
1354*4882a593Smuzhiyun }
1355*4882a593Smuzhiyun } else {
1356*4882a593Smuzhiyun res = 0;
1357*4882a593Smuzhiyun CLEAR_FLAG(F_CF);
1358*4882a593Smuzhiyun CLEAR_FLAG(F_OF);
1359*4882a593Smuzhiyun SET_FLAG(F_ZF);
1360*4882a593Smuzhiyun CLEAR_FLAG(F_SF);
1361*4882a593Smuzhiyun CLEAR_FLAG(F_PF);
1362*4882a593Smuzhiyun }
1363*4882a593Smuzhiyun return (u16)res;
1364*4882a593Smuzhiyun }
1365*4882a593Smuzhiyun
1366*4882a593Smuzhiyun /****************************************************************************
1367*4882a593Smuzhiyun REMARKS:
1368*4882a593Smuzhiyun Implements the SHR instruction and side effects.
1369*4882a593Smuzhiyun ****************************************************************************/
shr_long(u32 d,u8 s)1370*4882a593Smuzhiyun u32 shr_long(u32 d, u8 s)
1371*4882a593Smuzhiyun {
1372*4882a593Smuzhiyun unsigned int cnt, res, cf;
1373*4882a593Smuzhiyun
1374*4882a593Smuzhiyun if (s < 32) {
1375*4882a593Smuzhiyun cnt = s % 32;
1376*4882a593Smuzhiyun if (cnt > 0) {
1377*4882a593Smuzhiyun cf = d & (1 << (cnt - 1));
1378*4882a593Smuzhiyun res = d >> cnt;
1379*4882a593Smuzhiyun CONDITIONAL_SET_FLAG(cf, F_CF);
1380*4882a593Smuzhiyun set_szp_flags_32((u32)res);
1381*4882a593Smuzhiyun } else {
1382*4882a593Smuzhiyun res = d;
1383*4882a593Smuzhiyun }
1384*4882a593Smuzhiyun if (cnt == 1) {
1385*4882a593Smuzhiyun CONDITIONAL_SET_FLAG(XOR2(res >> 30), F_OF);
1386*4882a593Smuzhiyun } else {
1387*4882a593Smuzhiyun CLEAR_FLAG(F_OF);
1388*4882a593Smuzhiyun }
1389*4882a593Smuzhiyun } else {
1390*4882a593Smuzhiyun res = 0;
1391*4882a593Smuzhiyun CLEAR_FLAG(F_CF);
1392*4882a593Smuzhiyun CLEAR_FLAG(F_OF);
1393*4882a593Smuzhiyun SET_FLAG(F_ZF);
1394*4882a593Smuzhiyun CLEAR_FLAG(F_SF);
1395*4882a593Smuzhiyun CLEAR_FLAG(F_PF);
1396*4882a593Smuzhiyun }
1397*4882a593Smuzhiyun return res;
1398*4882a593Smuzhiyun }
1399*4882a593Smuzhiyun
1400*4882a593Smuzhiyun /****************************************************************************
1401*4882a593Smuzhiyun REMARKS:
1402*4882a593Smuzhiyun Implements the SAR instruction and side effects.
1403*4882a593Smuzhiyun ****************************************************************************/
sar_byte(u8 d,u8 s)1404*4882a593Smuzhiyun u8 sar_byte(u8 d, u8 s)
1405*4882a593Smuzhiyun {
1406*4882a593Smuzhiyun unsigned int cnt, res, cf, mask, sf;
1407*4882a593Smuzhiyun
1408*4882a593Smuzhiyun res = d;
1409*4882a593Smuzhiyun sf = d & 0x80;
1410*4882a593Smuzhiyun cnt = s % 8;
1411*4882a593Smuzhiyun if (cnt > 0 && cnt < 8) {
1412*4882a593Smuzhiyun mask = (1 << (8 - cnt)) - 1;
1413*4882a593Smuzhiyun cf = d & (1 << (cnt - 1));
1414*4882a593Smuzhiyun res = (d >> cnt) & mask;
1415*4882a593Smuzhiyun CONDITIONAL_SET_FLAG(cf, F_CF);
1416*4882a593Smuzhiyun if (sf) {
1417*4882a593Smuzhiyun res |= ~mask;
1418*4882a593Smuzhiyun }
1419*4882a593Smuzhiyun set_szp_flags_8((u8)res);
1420*4882a593Smuzhiyun } else if (cnt >= 8) {
1421*4882a593Smuzhiyun if (sf) {
1422*4882a593Smuzhiyun res = 0xff;
1423*4882a593Smuzhiyun SET_FLAG(F_CF);
1424*4882a593Smuzhiyun CLEAR_FLAG(F_ZF);
1425*4882a593Smuzhiyun SET_FLAG(F_SF);
1426*4882a593Smuzhiyun SET_FLAG(F_PF);
1427*4882a593Smuzhiyun } else {
1428*4882a593Smuzhiyun res = 0;
1429*4882a593Smuzhiyun CLEAR_FLAG(F_CF);
1430*4882a593Smuzhiyun SET_FLAG(F_ZF);
1431*4882a593Smuzhiyun CLEAR_FLAG(F_SF);
1432*4882a593Smuzhiyun CLEAR_FLAG(F_PF);
1433*4882a593Smuzhiyun }
1434*4882a593Smuzhiyun }
1435*4882a593Smuzhiyun return (u8)res;
1436*4882a593Smuzhiyun }
1437*4882a593Smuzhiyun
1438*4882a593Smuzhiyun /****************************************************************************
1439*4882a593Smuzhiyun REMARKS:
1440*4882a593Smuzhiyun Implements the SAR instruction and side effects.
1441*4882a593Smuzhiyun ****************************************************************************/
sar_word(u16 d,u8 s)1442*4882a593Smuzhiyun u16 sar_word(u16 d, u8 s)
1443*4882a593Smuzhiyun {
1444*4882a593Smuzhiyun unsigned int cnt, res, cf, mask, sf;
1445*4882a593Smuzhiyun
1446*4882a593Smuzhiyun sf = d & 0x8000;
1447*4882a593Smuzhiyun cnt = s % 16;
1448*4882a593Smuzhiyun res = d;
1449*4882a593Smuzhiyun if (cnt > 0 && cnt < 16) {
1450*4882a593Smuzhiyun mask = (1 << (16 - cnt)) - 1;
1451*4882a593Smuzhiyun cf = d & (1 << (cnt - 1));
1452*4882a593Smuzhiyun res = (d >> cnt) & mask;
1453*4882a593Smuzhiyun CONDITIONAL_SET_FLAG(cf, F_CF);
1454*4882a593Smuzhiyun if (sf) {
1455*4882a593Smuzhiyun res |= ~mask;
1456*4882a593Smuzhiyun }
1457*4882a593Smuzhiyun set_szp_flags_16((u16)res);
1458*4882a593Smuzhiyun } else if (cnt >= 16) {
1459*4882a593Smuzhiyun if (sf) {
1460*4882a593Smuzhiyun res = 0xffff;
1461*4882a593Smuzhiyun SET_FLAG(F_CF);
1462*4882a593Smuzhiyun CLEAR_FLAG(F_ZF);
1463*4882a593Smuzhiyun SET_FLAG(F_SF);
1464*4882a593Smuzhiyun SET_FLAG(F_PF);
1465*4882a593Smuzhiyun } else {
1466*4882a593Smuzhiyun res = 0;
1467*4882a593Smuzhiyun CLEAR_FLAG(F_CF);
1468*4882a593Smuzhiyun SET_FLAG(F_ZF);
1469*4882a593Smuzhiyun CLEAR_FLAG(F_SF);
1470*4882a593Smuzhiyun CLEAR_FLAG(F_PF);
1471*4882a593Smuzhiyun }
1472*4882a593Smuzhiyun }
1473*4882a593Smuzhiyun return (u16)res;
1474*4882a593Smuzhiyun }
1475*4882a593Smuzhiyun
1476*4882a593Smuzhiyun /****************************************************************************
1477*4882a593Smuzhiyun REMARKS:
1478*4882a593Smuzhiyun Implements the SAR instruction and side effects.
1479*4882a593Smuzhiyun ****************************************************************************/
sar_long(u32 d,u8 s)1480*4882a593Smuzhiyun u32 sar_long(u32 d, u8 s)
1481*4882a593Smuzhiyun {
1482*4882a593Smuzhiyun u32 cnt, res, cf, mask, sf;
1483*4882a593Smuzhiyun
1484*4882a593Smuzhiyun sf = d & 0x80000000;
1485*4882a593Smuzhiyun cnt = s % 32;
1486*4882a593Smuzhiyun res = d;
1487*4882a593Smuzhiyun if (cnt > 0 && cnt < 32) {
1488*4882a593Smuzhiyun mask = (1 << (32 - cnt)) - 1;
1489*4882a593Smuzhiyun cf = d & (1 << (cnt - 1));
1490*4882a593Smuzhiyun res = (d >> cnt) & mask;
1491*4882a593Smuzhiyun CONDITIONAL_SET_FLAG(cf, F_CF);
1492*4882a593Smuzhiyun if (sf) {
1493*4882a593Smuzhiyun res |= ~mask;
1494*4882a593Smuzhiyun }
1495*4882a593Smuzhiyun set_szp_flags_32(res);
1496*4882a593Smuzhiyun } else if (cnt >= 32) {
1497*4882a593Smuzhiyun if (sf) {
1498*4882a593Smuzhiyun res = 0xffffffff;
1499*4882a593Smuzhiyun SET_FLAG(F_CF);
1500*4882a593Smuzhiyun CLEAR_FLAG(F_ZF);
1501*4882a593Smuzhiyun SET_FLAG(F_SF);
1502*4882a593Smuzhiyun SET_FLAG(F_PF);
1503*4882a593Smuzhiyun } else {
1504*4882a593Smuzhiyun res = 0;
1505*4882a593Smuzhiyun CLEAR_FLAG(F_CF);
1506*4882a593Smuzhiyun SET_FLAG(F_ZF);
1507*4882a593Smuzhiyun CLEAR_FLAG(F_SF);
1508*4882a593Smuzhiyun CLEAR_FLAG(F_PF);
1509*4882a593Smuzhiyun }
1510*4882a593Smuzhiyun }
1511*4882a593Smuzhiyun return res;
1512*4882a593Smuzhiyun }
1513*4882a593Smuzhiyun
1514*4882a593Smuzhiyun /****************************************************************************
1515*4882a593Smuzhiyun REMARKS:
1516*4882a593Smuzhiyun Implements the SHLD instruction and side effects.
1517*4882a593Smuzhiyun ****************************************************************************/
shld_word(u16 d,u16 fill,u8 s)1518*4882a593Smuzhiyun u16 shld_word (u16 d, u16 fill, u8 s)
1519*4882a593Smuzhiyun {
1520*4882a593Smuzhiyun unsigned int cnt, res, cf;
1521*4882a593Smuzhiyun
1522*4882a593Smuzhiyun if (s < 16) {
1523*4882a593Smuzhiyun cnt = s % 16;
1524*4882a593Smuzhiyun if (cnt > 0) {
1525*4882a593Smuzhiyun res = (d << cnt) | (fill >> (16-cnt));
1526*4882a593Smuzhiyun cf = d & (1 << (16 - cnt));
1527*4882a593Smuzhiyun CONDITIONAL_SET_FLAG(cf, F_CF);
1528*4882a593Smuzhiyun set_szp_flags_16((u16)res);
1529*4882a593Smuzhiyun } else {
1530*4882a593Smuzhiyun res = d;
1531*4882a593Smuzhiyun }
1532*4882a593Smuzhiyun if (cnt == 1) {
1533*4882a593Smuzhiyun CONDITIONAL_SET_FLAG((((res & 0x8000) == 0x8000) ^
1534*4882a593Smuzhiyun (ACCESS_FLAG(F_CF) != 0)), F_OF);
1535*4882a593Smuzhiyun } else {
1536*4882a593Smuzhiyun CLEAR_FLAG(F_OF);
1537*4882a593Smuzhiyun }
1538*4882a593Smuzhiyun } else {
1539*4882a593Smuzhiyun res = 0;
1540*4882a593Smuzhiyun CONDITIONAL_SET_FLAG((d << (s-1)) & 0x8000, F_CF);
1541*4882a593Smuzhiyun CLEAR_FLAG(F_OF);
1542*4882a593Smuzhiyun CLEAR_FLAG(F_SF);
1543*4882a593Smuzhiyun SET_FLAG(F_PF);
1544*4882a593Smuzhiyun SET_FLAG(F_ZF);
1545*4882a593Smuzhiyun }
1546*4882a593Smuzhiyun return (u16)res;
1547*4882a593Smuzhiyun }
1548*4882a593Smuzhiyun
1549*4882a593Smuzhiyun /****************************************************************************
1550*4882a593Smuzhiyun REMARKS:
1551*4882a593Smuzhiyun Implements the SHLD instruction and side effects.
1552*4882a593Smuzhiyun ****************************************************************************/
shld_long(u32 d,u32 fill,u8 s)1553*4882a593Smuzhiyun u32 shld_long (u32 d, u32 fill, u8 s)
1554*4882a593Smuzhiyun {
1555*4882a593Smuzhiyun unsigned int cnt, res, cf;
1556*4882a593Smuzhiyun
1557*4882a593Smuzhiyun if (s < 32) {
1558*4882a593Smuzhiyun cnt = s % 32;
1559*4882a593Smuzhiyun if (cnt > 0) {
1560*4882a593Smuzhiyun res = (d << cnt) | (fill >> (32-cnt));
1561*4882a593Smuzhiyun cf = d & (1 << (32 - cnt));
1562*4882a593Smuzhiyun CONDITIONAL_SET_FLAG(cf, F_CF);
1563*4882a593Smuzhiyun set_szp_flags_32((u32)res);
1564*4882a593Smuzhiyun } else {
1565*4882a593Smuzhiyun res = d;
1566*4882a593Smuzhiyun }
1567*4882a593Smuzhiyun if (cnt == 1) {
1568*4882a593Smuzhiyun CONDITIONAL_SET_FLAG((((res & 0x80000000) == 0x80000000) ^
1569*4882a593Smuzhiyun (ACCESS_FLAG(F_CF) != 0)), F_OF);
1570*4882a593Smuzhiyun } else {
1571*4882a593Smuzhiyun CLEAR_FLAG(F_OF);
1572*4882a593Smuzhiyun }
1573*4882a593Smuzhiyun } else {
1574*4882a593Smuzhiyun res = 0;
1575*4882a593Smuzhiyun CONDITIONAL_SET_FLAG((d << (s-1)) & 0x80000000, F_CF);
1576*4882a593Smuzhiyun CLEAR_FLAG(F_OF);
1577*4882a593Smuzhiyun CLEAR_FLAG(F_SF);
1578*4882a593Smuzhiyun SET_FLAG(F_PF);
1579*4882a593Smuzhiyun SET_FLAG(F_ZF);
1580*4882a593Smuzhiyun }
1581*4882a593Smuzhiyun return res;
1582*4882a593Smuzhiyun }
1583*4882a593Smuzhiyun
1584*4882a593Smuzhiyun /****************************************************************************
1585*4882a593Smuzhiyun REMARKS:
1586*4882a593Smuzhiyun Implements the SHRD instruction and side effects.
1587*4882a593Smuzhiyun ****************************************************************************/
shrd_word(u16 d,u16 fill,u8 s)1588*4882a593Smuzhiyun u16 shrd_word (u16 d, u16 fill, u8 s)
1589*4882a593Smuzhiyun {
1590*4882a593Smuzhiyun unsigned int cnt, res, cf;
1591*4882a593Smuzhiyun
1592*4882a593Smuzhiyun if (s < 16) {
1593*4882a593Smuzhiyun cnt = s % 16;
1594*4882a593Smuzhiyun if (cnt > 0) {
1595*4882a593Smuzhiyun cf = d & (1 << (cnt - 1));
1596*4882a593Smuzhiyun res = (d >> cnt) | (fill << (16 - cnt));
1597*4882a593Smuzhiyun CONDITIONAL_SET_FLAG(cf, F_CF);
1598*4882a593Smuzhiyun set_szp_flags_16((u16)res);
1599*4882a593Smuzhiyun } else {
1600*4882a593Smuzhiyun res = d;
1601*4882a593Smuzhiyun }
1602*4882a593Smuzhiyun
1603*4882a593Smuzhiyun if (cnt == 1) {
1604*4882a593Smuzhiyun CONDITIONAL_SET_FLAG(XOR2(res >> 14), F_OF);
1605*4882a593Smuzhiyun } else {
1606*4882a593Smuzhiyun CLEAR_FLAG(F_OF);
1607*4882a593Smuzhiyun }
1608*4882a593Smuzhiyun } else {
1609*4882a593Smuzhiyun res = 0;
1610*4882a593Smuzhiyun CLEAR_FLAG(F_CF);
1611*4882a593Smuzhiyun CLEAR_FLAG(F_OF);
1612*4882a593Smuzhiyun SET_FLAG(F_ZF);
1613*4882a593Smuzhiyun CLEAR_FLAG(F_SF);
1614*4882a593Smuzhiyun CLEAR_FLAG(F_PF);
1615*4882a593Smuzhiyun }
1616*4882a593Smuzhiyun return (u16)res;
1617*4882a593Smuzhiyun }
1618*4882a593Smuzhiyun
1619*4882a593Smuzhiyun /****************************************************************************
1620*4882a593Smuzhiyun REMARKS:
1621*4882a593Smuzhiyun Implements the SHRD instruction and side effects.
1622*4882a593Smuzhiyun ****************************************************************************/
shrd_long(u32 d,u32 fill,u8 s)1623*4882a593Smuzhiyun u32 shrd_long (u32 d, u32 fill, u8 s)
1624*4882a593Smuzhiyun {
1625*4882a593Smuzhiyun unsigned int cnt, res, cf;
1626*4882a593Smuzhiyun
1627*4882a593Smuzhiyun if (s < 32) {
1628*4882a593Smuzhiyun cnt = s % 32;
1629*4882a593Smuzhiyun if (cnt > 0) {
1630*4882a593Smuzhiyun cf = d & (1 << (cnt - 1));
1631*4882a593Smuzhiyun res = (d >> cnt) | (fill << (32 - cnt));
1632*4882a593Smuzhiyun CONDITIONAL_SET_FLAG(cf, F_CF);
1633*4882a593Smuzhiyun set_szp_flags_32((u32)res);
1634*4882a593Smuzhiyun } else {
1635*4882a593Smuzhiyun res = d;
1636*4882a593Smuzhiyun }
1637*4882a593Smuzhiyun if (cnt == 1) {
1638*4882a593Smuzhiyun CONDITIONAL_SET_FLAG(XOR2(res >> 30), F_OF);
1639*4882a593Smuzhiyun } else {
1640*4882a593Smuzhiyun CLEAR_FLAG(F_OF);
1641*4882a593Smuzhiyun }
1642*4882a593Smuzhiyun } else {
1643*4882a593Smuzhiyun res = 0;
1644*4882a593Smuzhiyun CLEAR_FLAG(F_CF);
1645*4882a593Smuzhiyun CLEAR_FLAG(F_OF);
1646*4882a593Smuzhiyun SET_FLAG(F_ZF);
1647*4882a593Smuzhiyun CLEAR_FLAG(F_SF);
1648*4882a593Smuzhiyun CLEAR_FLAG(F_PF);
1649*4882a593Smuzhiyun }
1650*4882a593Smuzhiyun return res;
1651*4882a593Smuzhiyun }
1652*4882a593Smuzhiyun
1653*4882a593Smuzhiyun /****************************************************************************
1654*4882a593Smuzhiyun REMARKS:
1655*4882a593Smuzhiyun Implements the SBB instruction and side effects.
1656*4882a593Smuzhiyun ****************************************************************************/
sbb_byte(u8 d,u8 s)1657*4882a593Smuzhiyun u8 sbb_byte(u8 d, u8 s)
1658*4882a593Smuzhiyun {
1659*4882a593Smuzhiyun u32 res; /* all operands in native machine order */
1660*4882a593Smuzhiyun u32 bc;
1661*4882a593Smuzhiyun
1662*4882a593Smuzhiyun if (ACCESS_FLAG(F_CF))
1663*4882a593Smuzhiyun res = d - s - 1;
1664*4882a593Smuzhiyun else
1665*4882a593Smuzhiyun res = d - s;
1666*4882a593Smuzhiyun set_szp_flags_8((u8)res);
1667*4882a593Smuzhiyun
1668*4882a593Smuzhiyun /* calculate the borrow chain. See note at top */
1669*4882a593Smuzhiyun bc = (res & (~d | s)) | (~d & s);
1670*4882a593Smuzhiyun CONDITIONAL_SET_FLAG(bc & 0x80, F_CF);
1671*4882a593Smuzhiyun CONDITIONAL_SET_FLAG(XOR2(bc >> 6), F_OF);
1672*4882a593Smuzhiyun CONDITIONAL_SET_FLAG(bc & 0x8, F_AF);
1673*4882a593Smuzhiyun return (u8)res;
1674*4882a593Smuzhiyun }
1675*4882a593Smuzhiyun
1676*4882a593Smuzhiyun /****************************************************************************
1677*4882a593Smuzhiyun REMARKS:
1678*4882a593Smuzhiyun Implements the SBB instruction and side effects.
1679*4882a593Smuzhiyun ****************************************************************************/
sbb_word(u16 d,u16 s)1680*4882a593Smuzhiyun u16 sbb_word(u16 d, u16 s)
1681*4882a593Smuzhiyun {
1682*4882a593Smuzhiyun u32 res; /* all operands in native machine order */
1683*4882a593Smuzhiyun u32 bc;
1684*4882a593Smuzhiyun
1685*4882a593Smuzhiyun if (ACCESS_FLAG(F_CF))
1686*4882a593Smuzhiyun res = d - s - 1;
1687*4882a593Smuzhiyun else
1688*4882a593Smuzhiyun res = d - s;
1689*4882a593Smuzhiyun set_szp_flags_16((u16)res);
1690*4882a593Smuzhiyun
1691*4882a593Smuzhiyun /* calculate the borrow chain. See note at top */
1692*4882a593Smuzhiyun bc = (res & (~d | s)) | (~d & s);
1693*4882a593Smuzhiyun CONDITIONAL_SET_FLAG(bc & 0x8000, F_CF);
1694*4882a593Smuzhiyun CONDITIONAL_SET_FLAG(XOR2(bc >> 14), F_OF);
1695*4882a593Smuzhiyun CONDITIONAL_SET_FLAG(bc & 0x8, F_AF);
1696*4882a593Smuzhiyun return (u16)res;
1697*4882a593Smuzhiyun }
1698*4882a593Smuzhiyun
1699*4882a593Smuzhiyun /****************************************************************************
1700*4882a593Smuzhiyun REMARKS:
1701*4882a593Smuzhiyun Implements the SBB instruction and side effects.
1702*4882a593Smuzhiyun ****************************************************************************/
sbb_long(u32 d,u32 s)1703*4882a593Smuzhiyun u32 sbb_long(u32 d, u32 s)
1704*4882a593Smuzhiyun {
1705*4882a593Smuzhiyun u32 res; /* all operands in native machine order */
1706*4882a593Smuzhiyun u32 bc;
1707*4882a593Smuzhiyun
1708*4882a593Smuzhiyun if (ACCESS_FLAG(F_CF))
1709*4882a593Smuzhiyun res = d - s - 1;
1710*4882a593Smuzhiyun else
1711*4882a593Smuzhiyun res = d - s;
1712*4882a593Smuzhiyun
1713*4882a593Smuzhiyun set_szp_flags_32(res);
1714*4882a593Smuzhiyun
1715*4882a593Smuzhiyun /* calculate the borrow chain. See note at top */
1716*4882a593Smuzhiyun bc = (res & (~d | s)) | (~d & s);
1717*4882a593Smuzhiyun CONDITIONAL_SET_FLAG(bc & 0x80000000, F_CF);
1718*4882a593Smuzhiyun CONDITIONAL_SET_FLAG(XOR2(bc >> 30), F_OF);
1719*4882a593Smuzhiyun CONDITIONAL_SET_FLAG(bc & 0x8, F_AF);
1720*4882a593Smuzhiyun return res;
1721*4882a593Smuzhiyun }
1722*4882a593Smuzhiyun
1723*4882a593Smuzhiyun /****************************************************************************
1724*4882a593Smuzhiyun REMARKS:
1725*4882a593Smuzhiyun Implements the SUB instruction and side effects.
1726*4882a593Smuzhiyun ****************************************************************************/
sub_byte(u8 d,u8 s)1727*4882a593Smuzhiyun u8 sub_byte(u8 d, u8 s)
1728*4882a593Smuzhiyun {
1729*4882a593Smuzhiyun u32 res; /* all operands in native machine order */
1730*4882a593Smuzhiyun u32 bc;
1731*4882a593Smuzhiyun
1732*4882a593Smuzhiyun res = d - s;
1733*4882a593Smuzhiyun set_szp_flags_8((u8)res);
1734*4882a593Smuzhiyun
1735*4882a593Smuzhiyun /* calculate the borrow chain. See note at top */
1736*4882a593Smuzhiyun bc = (res & (~d | s)) | (~d & s);
1737*4882a593Smuzhiyun CONDITIONAL_SET_FLAG(bc & 0x80, F_CF);
1738*4882a593Smuzhiyun CONDITIONAL_SET_FLAG(XOR2(bc >> 6), F_OF);
1739*4882a593Smuzhiyun CONDITIONAL_SET_FLAG(bc & 0x8, F_AF);
1740*4882a593Smuzhiyun return (u8)res;
1741*4882a593Smuzhiyun }
1742*4882a593Smuzhiyun
1743*4882a593Smuzhiyun /****************************************************************************
1744*4882a593Smuzhiyun REMARKS:
1745*4882a593Smuzhiyun Implements the SUB instruction and side effects.
1746*4882a593Smuzhiyun ****************************************************************************/
sub_word(u16 d,u16 s)1747*4882a593Smuzhiyun u16 sub_word(u16 d, u16 s)
1748*4882a593Smuzhiyun {
1749*4882a593Smuzhiyun u32 res; /* all operands in native machine order */
1750*4882a593Smuzhiyun u32 bc;
1751*4882a593Smuzhiyun
1752*4882a593Smuzhiyun res = d - s;
1753*4882a593Smuzhiyun set_szp_flags_16((u16)res);
1754*4882a593Smuzhiyun
1755*4882a593Smuzhiyun /* calculate the borrow chain. See note at top */
1756*4882a593Smuzhiyun bc = (res & (~d | s)) | (~d & s);
1757*4882a593Smuzhiyun CONDITIONAL_SET_FLAG(bc & 0x8000, F_CF);
1758*4882a593Smuzhiyun CONDITIONAL_SET_FLAG(XOR2(bc >> 14), F_OF);
1759*4882a593Smuzhiyun CONDITIONAL_SET_FLAG(bc & 0x8, F_AF);
1760*4882a593Smuzhiyun return (u16)res;
1761*4882a593Smuzhiyun }
1762*4882a593Smuzhiyun
1763*4882a593Smuzhiyun /****************************************************************************
1764*4882a593Smuzhiyun REMARKS:
1765*4882a593Smuzhiyun Implements the SUB instruction and side effects.
1766*4882a593Smuzhiyun ****************************************************************************/
sub_long(u32 d,u32 s)1767*4882a593Smuzhiyun u32 sub_long(u32 d, u32 s)
1768*4882a593Smuzhiyun {
1769*4882a593Smuzhiyun u32 res; /* all operands in native machine order */
1770*4882a593Smuzhiyun u32 bc;
1771*4882a593Smuzhiyun
1772*4882a593Smuzhiyun res = d - s;
1773*4882a593Smuzhiyun set_szp_flags_32(res);
1774*4882a593Smuzhiyun
1775*4882a593Smuzhiyun /* calculate the borrow chain. See note at top */
1776*4882a593Smuzhiyun bc = (res & (~d | s)) | (~d & s);
1777*4882a593Smuzhiyun CONDITIONAL_SET_FLAG(bc & 0x80000000, F_CF);
1778*4882a593Smuzhiyun CONDITIONAL_SET_FLAG(XOR2(bc >> 30), F_OF);
1779*4882a593Smuzhiyun CONDITIONAL_SET_FLAG(bc & 0x8, F_AF);
1780*4882a593Smuzhiyun return res;
1781*4882a593Smuzhiyun }
1782*4882a593Smuzhiyun
1783*4882a593Smuzhiyun /****************************************************************************
1784*4882a593Smuzhiyun REMARKS:
1785*4882a593Smuzhiyun Implements the TEST instruction and side effects.
1786*4882a593Smuzhiyun ****************************************************************************/
test_byte(u8 d,u8 s)1787*4882a593Smuzhiyun void test_byte(u8 d, u8 s)
1788*4882a593Smuzhiyun {
1789*4882a593Smuzhiyun u32 res; /* all operands in native machine order */
1790*4882a593Smuzhiyun
1791*4882a593Smuzhiyun res = d & s;
1792*4882a593Smuzhiyun
1793*4882a593Smuzhiyun CLEAR_FLAG(F_OF);
1794*4882a593Smuzhiyun set_szp_flags_8((u8)res);
1795*4882a593Smuzhiyun /* AF == dont care */
1796*4882a593Smuzhiyun CLEAR_FLAG(F_CF);
1797*4882a593Smuzhiyun }
1798*4882a593Smuzhiyun
1799*4882a593Smuzhiyun /****************************************************************************
1800*4882a593Smuzhiyun REMARKS:
1801*4882a593Smuzhiyun Implements the TEST instruction and side effects.
1802*4882a593Smuzhiyun ****************************************************************************/
test_word(u16 d,u16 s)1803*4882a593Smuzhiyun void test_word(u16 d, u16 s)
1804*4882a593Smuzhiyun {
1805*4882a593Smuzhiyun u32 res; /* all operands in native machine order */
1806*4882a593Smuzhiyun
1807*4882a593Smuzhiyun res = d & s;
1808*4882a593Smuzhiyun
1809*4882a593Smuzhiyun CLEAR_FLAG(F_OF);
1810*4882a593Smuzhiyun set_szp_flags_16((u16)res);
1811*4882a593Smuzhiyun /* AF == dont care */
1812*4882a593Smuzhiyun CLEAR_FLAG(F_CF);
1813*4882a593Smuzhiyun }
1814*4882a593Smuzhiyun
1815*4882a593Smuzhiyun /****************************************************************************
1816*4882a593Smuzhiyun REMARKS:
1817*4882a593Smuzhiyun Implements the TEST instruction and side effects.
1818*4882a593Smuzhiyun ****************************************************************************/
test_long(u32 d,u32 s)1819*4882a593Smuzhiyun void test_long(u32 d, u32 s)
1820*4882a593Smuzhiyun {
1821*4882a593Smuzhiyun u32 res; /* all operands in native machine order */
1822*4882a593Smuzhiyun
1823*4882a593Smuzhiyun res = d & s;
1824*4882a593Smuzhiyun
1825*4882a593Smuzhiyun CLEAR_FLAG(F_OF);
1826*4882a593Smuzhiyun set_szp_flags_32(res);
1827*4882a593Smuzhiyun /* AF == dont care */
1828*4882a593Smuzhiyun CLEAR_FLAG(F_CF);
1829*4882a593Smuzhiyun }
1830*4882a593Smuzhiyun
1831*4882a593Smuzhiyun /****************************************************************************
1832*4882a593Smuzhiyun REMARKS:
1833*4882a593Smuzhiyun Implements the XOR instruction and side effects.
1834*4882a593Smuzhiyun ****************************************************************************/
xor_byte(u8 d,u8 s)1835*4882a593Smuzhiyun u8 xor_byte(u8 d, u8 s)
1836*4882a593Smuzhiyun {
1837*4882a593Smuzhiyun u8 res; /* all operands in native machine order */
1838*4882a593Smuzhiyun
1839*4882a593Smuzhiyun res = d ^ s;
1840*4882a593Smuzhiyun no_carry_byte_side_eff(res);
1841*4882a593Smuzhiyun return res;
1842*4882a593Smuzhiyun }
1843*4882a593Smuzhiyun
1844*4882a593Smuzhiyun /****************************************************************************
1845*4882a593Smuzhiyun REMARKS:
1846*4882a593Smuzhiyun Implements the XOR instruction and side effects.
1847*4882a593Smuzhiyun ****************************************************************************/
xor_word(u16 d,u16 s)1848*4882a593Smuzhiyun u16 xor_word(u16 d, u16 s)
1849*4882a593Smuzhiyun {
1850*4882a593Smuzhiyun u16 res; /* all operands in native machine order */
1851*4882a593Smuzhiyun
1852*4882a593Smuzhiyun res = d ^ s;
1853*4882a593Smuzhiyun no_carry_word_side_eff(res);
1854*4882a593Smuzhiyun return res;
1855*4882a593Smuzhiyun }
1856*4882a593Smuzhiyun
1857*4882a593Smuzhiyun /****************************************************************************
1858*4882a593Smuzhiyun REMARKS:
1859*4882a593Smuzhiyun Implements the XOR instruction and side effects.
1860*4882a593Smuzhiyun ****************************************************************************/
xor_long(u32 d,u32 s)1861*4882a593Smuzhiyun u32 xor_long(u32 d, u32 s)
1862*4882a593Smuzhiyun {
1863*4882a593Smuzhiyun u32 res; /* all operands in native machine order */
1864*4882a593Smuzhiyun
1865*4882a593Smuzhiyun res = d ^ s;
1866*4882a593Smuzhiyun no_carry_long_side_eff(res);
1867*4882a593Smuzhiyun return res;
1868*4882a593Smuzhiyun }
1869*4882a593Smuzhiyun
1870*4882a593Smuzhiyun /****************************************************************************
1871*4882a593Smuzhiyun REMARKS:
1872*4882a593Smuzhiyun Implements the IMUL instruction and side effects.
1873*4882a593Smuzhiyun ****************************************************************************/
imul_byte(u8 s)1874*4882a593Smuzhiyun void imul_byte(u8 s)
1875*4882a593Smuzhiyun {
1876*4882a593Smuzhiyun s16 res = (s16)((s8)M.x86.R_AL * (s8)s);
1877*4882a593Smuzhiyun
1878*4882a593Smuzhiyun M.x86.R_AX = res;
1879*4882a593Smuzhiyun if (((M.x86.R_AL & 0x80) == 0 && M.x86.R_AH == 0x00) ||
1880*4882a593Smuzhiyun ((M.x86.R_AL & 0x80) != 0 && M.x86.R_AH == 0xFF)) {
1881*4882a593Smuzhiyun CLEAR_FLAG(F_CF);
1882*4882a593Smuzhiyun CLEAR_FLAG(F_OF);
1883*4882a593Smuzhiyun } else {
1884*4882a593Smuzhiyun SET_FLAG(F_CF);
1885*4882a593Smuzhiyun SET_FLAG(F_OF);
1886*4882a593Smuzhiyun }
1887*4882a593Smuzhiyun }
1888*4882a593Smuzhiyun
1889*4882a593Smuzhiyun /****************************************************************************
1890*4882a593Smuzhiyun REMARKS:
1891*4882a593Smuzhiyun Implements the IMUL instruction and side effects.
1892*4882a593Smuzhiyun ****************************************************************************/
imul_word(u16 s)1893*4882a593Smuzhiyun void imul_word(u16 s)
1894*4882a593Smuzhiyun {
1895*4882a593Smuzhiyun s32 res = (s16)M.x86.R_AX * (s16)s;
1896*4882a593Smuzhiyun
1897*4882a593Smuzhiyun M.x86.R_AX = (u16)res;
1898*4882a593Smuzhiyun M.x86.R_DX = (u16)(res >> 16);
1899*4882a593Smuzhiyun if (((M.x86.R_AX & 0x8000) == 0 && M.x86.R_DX == 0x0000) ||
1900*4882a593Smuzhiyun ((M.x86.R_AX & 0x8000) != 0 && M.x86.R_DX == 0xFFFF)) {
1901*4882a593Smuzhiyun CLEAR_FLAG(F_CF);
1902*4882a593Smuzhiyun CLEAR_FLAG(F_OF);
1903*4882a593Smuzhiyun } else {
1904*4882a593Smuzhiyun SET_FLAG(F_CF);
1905*4882a593Smuzhiyun SET_FLAG(F_OF);
1906*4882a593Smuzhiyun }
1907*4882a593Smuzhiyun }
1908*4882a593Smuzhiyun
1909*4882a593Smuzhiyun /****************************************************************************
1910*4882a593Smuzhiyun REMARKS:
1911*4882a593Smuzhiyun Implements the IMUL instruction and side effects.
1912*4882a593Smuzhiyun ****************************************************************************/
imul_long_direct(u32 * res_lo,u32 * res_hi,u32 d,u32 s)1913*4882a593Smuzhiyun void imul_long_direct(u32 *res_lo, u32* res_hi,u32 d, u32 s)
1914*4882a593Smuzhiyun {
1915*4882a593Smuzhiyun #ifdef __HAS_LONG_LONG__
1916*4882a593Smuzhiyun s64 res = (s32)d * (s32)s;
1917*4882a593Smuzhiyun
1918*4882a593Smuzhiyun *res_lo = (u32)res;
1919*4882a593Smuzhiyun *res_hi = (u32)(res >> 32);
1920*4882a593Smuzhiyun #else
1921*4882a593Smuzhiyun u32 d_lo,d_hi,d_sign;
1922*4882a593Smuzhiyun u32 s_lo,s_hi,s_sign;
1923*4882a593Smuzhiyun u32 rlo_lo,rlo_hi,rhi_lo;
1924*4882a593Smuzhiyun
1925*4882a593Smuzhiyun if ((d_sign = d & 0x80000000) != 0)
1926*4882a593Smuzhiyun d = -d;
1927*4882a593Smuzhiyun d_lo = d & 0xFFFF;
1928*4882a593Smuzhiyun d_hi = d >> 16;
1929*4882a593Smuzhiyun if ((s_sign = s & 0x80000000) != 0)
1930*4882a593Smuzhiyun s = -s;
1931*4882a593Smuzhiyun s_lo = s & 0xFFFF;
1932*4882a593Smuzhiyun s_hi = s >> 16;
1933*4882a593Smuzhiyun rlo_lo = d_lo * s_lo;
1934*4882a593Smuzhiyun rlo_hi = (d_hi * s_lo + d_lo * s_hi) + (rlo_lo >> 16);
1935*4882a593Smuzhiyun rhi_lo = d_hi * s_hi + (rlo_hi >> 16);
1936*4882a593Smuzhiyun *res_lo = (rlo_hi << 16) | (rlo_lo & 0xFFFF);
1937*4882a593Smuzhiyun *res_hi = rhi_lo;
1938*4882a593Smuzhiyun if (d_sign != s_sign) {
1939*4882a593Smuzhiyun d = ~*res_lo;
1940*4882a593Smuzhiyun s = (((d & 0xFFFF) + 1) >> 16) + (d >> 16);
1941*4882a593Smuzhiyun *res_lo = ~*res_lo+1;
1942*4882a593Smuzhiyun *res_hi = ~*res_hi+(s >> 16);
1943*4882a593Smuzhiyun }
1944*4882a593Smuzhiyun #endif
1945*4882a593Smuzhiyun }
1946*4882a593Smuzhiyun
1947*4882a593Smuzhiyun /****************************************************************************
1948*4882a593Smuzhiyun REMARKS:
1949*4882a593Smuzhiyun Implements the IMUL instruction and side effects.
1950*4882a593Smuzhiyun ****************************************************************************/
imul_long(u32 s)1951*4882a593Smuzhiyun void imul_long(u32 s)
1952*4882a593Smuzhiyun {
1953*4882a593Smuzhiyun imul_long_direct(&M.x86.R_EAX,&M.x86.R_EDX,M.x86.R_EAX,s);
1954*4882a593Smuzhiyun if (((M.x86.R_EAX & 0x80000000) == 0 && M.x86.R_EDX == 0x00000000) ||
1955*4882a593Smuzhiyun ((M.x86.R_EAX & 0x80000000) != 0 && M.x86.R_EDX == 0xFFFFFFFF)) {
1956*4882a593Smuzhiyun CLEAR_FLAG(F_CF);
1957*4882a593Smuzhiyun CLEAR_FLAG(F_OF);
1958*4882a593Smuzhiyun } else {
1959*4882a593Smuzhiyun SET_FLAG(F_CF);
1960*4882a593Smuzhiyun SET_FLAG(F_OF);
1961*4882a593Smuzhiyun }
1962*4882a593Smuzhiyun }
1963*4882a593Smuzhiyun
1964*4882a593Smuzhiyun /****************************************************************************
1965*4882a593Smuzhiyun REMARKS:
1966*4882a593Smuzhiyun Implements the MUL instruction and side effects.
1967*4882a593Smuzhiyun ****************************************************************************/
mul_byte(u8 s)1968*4882a593Smuzhiyun void mul_byte(u8 s)
1969*4882a593Smuzhiyun {
1970*4882a593Smuzhiyun u16 res = (u16)(M.x86.R_AL * s);
1971*4882a593Smuzhiyun
1972*4882a593Smuzhiyun M.x86.R_AX = res;
1973*4882a593Smuzhiyun if (M.x86.R_AH == 0) {
1974*4882a593Smuzhiyun CLEAR_FLAG(F_CF);
1975*4882a593Smuzhiyun CLEAR_FLAG(F_OF);
1976*4882a593Smuzhiyun } else {
1977*4882a593Smuzhiyun SET_FLAG(F_CF);
1978*4882a593Smuzhiyun SET_FLAG(F_OF);
1979*4882a593Smuzhiyun }
1980*4882a593Smuzhiyun }
1981*4882a593Smuzhiyun
1982*4882a593Smuzhiyun /****************************************************************************
1983*4882a593Smuzhiyun REMARKS:
1984*4882a593Smuzhiyun Implements the MUL instruction and side effects.
1985*4882a593Smuzhiyun ****************************************************************************/
mul_word(u16 s)1986*4882a593Smuzhiyun void mul_word(u16 s)
1987*4882a593Smuzhiyun {
1988*4882a593Smuzhiyun u32 res = M.x86.R_AX * s;
1989*4882a593Smuzhiyun
1990*4882a593Smuzhiyun M.x86.R_AX = (u16)res;
1991*4882a593Smuzhiyun M.x86.R_DX = (u16)(res >> 16);
1992*4882a593Smuzhiyun if (M.x86.R_DX == 0) {
1993*4882a593Smuzhiyun CLEAR_FLAG(F_CF);
1994*4882a593Smuzhiyun CLEAR_FLAG(F_OF);
1995*4882a593Smuzhiyun } else {
1996*4882a593Smuzhiyun SET_FLAG(F_CF);
1997*4882a593Smuzhiyun SET_FLAG(F_OF);
1998*4882a593Smuzhiyun }
1999*4882a593Smuzhiyun }
2000*4882a593Smuzhiyun
2001*4882a593Smuzhiyun /****************************************************************************
2002*4882a593Smuzhiyun REMARKS:
2003*4882a593Smuzhiyun Implements the MUL instruction and side effects.
2004*4882a593Smuzhiyun ****************************************************************************/
mul_long(u32 s)2005*4882a593Smuzhiyun void mul_long(u32 s)
2006*4882a593Smuzhiyun {
2007*4882a593Smuzhiyun #ifdef __HAS_LONG_LONG__
2008*4882a593Smuzhiyun u64 res = (u32)M.x86.R_EAX * (u32)s;
2009*4882a593Smuzhiyun
2010*4882a593Smuzhiyun M.x86.R_EAX = (u32)res;
2011*4882a593Smuzhiyun M.x86.R_EDX = (u32)(res >> 32);
2012*4882a593Smuzhiyun #else
2013*4882a593Smuzhiyun u32 a,a_lo,a_hi;
2014*4882a593Smuzhiyun u32 s_lo,s_hi;
2015*4882a593Smuzhiyun u32 rlo_lo,rlo_hi,rhi_lo;
2016*4882a593Smuzhiyun
2017*4882a593Smuzhiyun a = M.x86.R_EAX;
2018*4882a593Smuzhiyun a_lo = a & 0xFFFF;
2019*4882a593Smuzhiyun a_hi = a >> 16;
2020*4882a593Smuzhiyun s_lo = s & 0xFFFF;
2021*4882a593Smuzhiyun s_hi = s >> 16;
2022*4882a593Smuzhiyun rlo_lo = a_lo * s_lo;
2023*4882a593Smuzhiyun rlo_hi = (a_hi * s_lo + a_lo * s_hi) + (rlo_lo >> 16);
2024*4882a593Smuzhiyun rhi_lo = a_hi * s_hi + (rlo_hi >> 16);
2025*4882a593Smuzhiyun M.x86.R_EAX = (rlo_hi << 16) | (rlo_lo & 0xFFFF);
2026*4882a593Smuzhiyun M.x86.R_EDX = rhi_lo;
2027*4882a593Smuzhiyun #endif
2028*4882a593Smuzhiyun if (M.x86.R_EDX == 0) {
2029*4882a593Smuzhiyun CLEAR_FLAG(F_CF);
2030*4882a593Smuzhiyun CLEAR_FLAG(F_OF);
2031*4882a593Smuzhiyun } else {
2032*4882a593Smuzhiyun SET_FLAG(F_CF);
2033*4882a593Smuzhiyun SET_FLAG(F_OF);
2034*4882a593Smuzhiyun }
2035*4882a593Smuzhiyun }
2036*4882a593Smuzhiyun
2037*4882a593Smuzhiyun /****************************************************************************
2038*4882a593Smuzhiyun REMARKS:
2039*4882a593Smuzhiyun Implements the IDIV instruction and side effects.
2040*4882a593Smuzhiyun ****************************************************************************/
idiv_byte(u8 s)2041*4882a593Smuzhiyun void idiv_byte(u8 s)
2042*4882a593Smuzhiyun {
2043*4882a593Smuzhiyun s32 dvd, div, mod;
2044*4882a593Smuzhiyun
2045*4882a593Smuzhiyun dvd = (s16)M.x86.R_AX;
2046*4882a593Smuzhiyun if (s == 0) {
2047*4882a593Smuzhiyun x86emu_intr_raise(0);
2048*4882a593Smuzhiyun return;
2049*4882a593Smuzhiyun }
2050*4882a593Smuzhiyun div = dvd / (s8)s;
2051*4882a593Smuzhiyun mod = dvd % (s8)s;
2052*4882a593Smuzhiyun if (abs(div) > 0x7f) {
2053*4882a593Smuzhiyun x86emu_intr_raise(0);
2054*4882a593Smuzhiyun return;
2055*4882a593Smuzhiyun }
2056*4882a593Smuzhiyun M.x86.R_AL = (s8) div;
2057*4882a593Smuzhiyun M.x86.R_AH = (s8) mod;
2058*4882a593Smuzhiyun }
2059*4882a593Smuzhiyun
2060*4882a593Smuzhiyun /****************************************************************************
2061*4882a593Smuzhiyun REMARKS:
2062*4882a593Smuzhiyun Implements the IDIV instruction and side effects.
2063*4882a593Smuzhiyun ****************************************************************************/
idiv_word(u16 s)2064*4882a593Smuzhiyun void idiv_word(u16 s)
2065*4882a593Smuzhiyun {
2066*4882a593Smuzhiyun s32 dvd, div, mod;
2067*4882a593Smuzhiyun
2068*4882a593Smuzhiyun dvd = (((s32)M.x86.R_DX) << 16) | M.x86.R_AX;
2069*4882a593Smuzhiyun if (s == 0) {
2070*4882a593Smuzhiyun x86emu_intr_raise(0);
2071*4882a593Smuzhiyun return;
2072*4882a593Smuzhiyun }
2073*4882a593Smuzhiyun div = dvd / (s16)s;
2074*4882a593Smuzhiyun mod = dvd % (s16)s;
2075*4882a593Smuzhiyun if (abs(div) > 0x7fff) {
2076*4882a593Smuzhiyun x86emu_intr_raise(0);
2077*4882a593Smuzhiyun return;
2078*4882a593Smuzhiyun }
2079*4882a593Smuzhiyun CLEAR_FLAG(F_CF);
2080*4882a593Smuzhiyun CLEAR_FLAG(F_SF);
2081*4882a593Smuzhiyun CONDITIONAL_SET_FLAG(div == 0, F_ZF);
2082*4882a593Smuzhiyun set_parity_flag(mod);
2083*4882a593Smuzhiyun
2084*4882a593Smuzhiyun M.x86.R_AX = (u16)div;
2085*4882a593Smuzhiyun M.x86.R_DX = (u16)mod;
2086*4882a593Smuzhiyun }
2087*4882a593Smuzhiyun
2088*4882a593Smuzhiyun /****************************************************************************
2089*4882a593Smuzhiyun REMARKS:
2090*4882a593Smuzhiyun Implements the IDIV instruction and side effects.
2091*4882a593Smuzhiyun ****************************************************************************/
idiv_long(u32 s)2092*4882a593Smuzhiyun void idiv_long(u32 s)
2093*4882a593Smuzhiyun {
2094*4882a593Smuzhiyun #ifdef __HAS_LONG_LONG__
2095*4882a593Smuzhiyun s64 dvd, div, mod;
2096*4882a593Smuzhiyun
2097*4882a593Smuzhiyun dvd = (((s64)M.x86.R_EDX) << 32) | M.x86.R_EAX;
2098*4882a593Smuzhiyun if (s == 0) {
2099*4882a593Smuzhiyun x86emu_intr_raise(0);
2100*4882a593Smuzhiyun return;
2101*4882a593Smuzhiyun }
2102*4882a593Smuzhiyun div = dvd / (s32)s;
2103*4882a593Smuzhiyun mod = dvd % (s32)s;
2104*4882a593Smuzhiyun if (abs(div) > 0x7fffffff) {
2105*4882a593Smuzhiyun x86emu_intr_raise(0);
2106*4882a593Smuzhiyun return;
2107*4882a593Smuzhiyun }
2108*4882a593Smuzhiyun #else
2109*4882a593Smuzhiyun s32 div = 0, mod;
2110*4882a593Smuzhiyun s32 h_dvd = M.x86.R_EDX;
2111*4882a593Smuzhiyun u32 l_dvd = M.x86.R_EAX;
2112*4882a593Smuzhiyun u32 abs_s = s & 0x7FFFFFFF;
2113*4882a593Smuzhiyun u32 abs_h_dvd = h_dvd & 0x7FFFFFFF;
2114*4882a593Smuzhiyun u32 h_s = abs_s >> 1;
2115*4882a593Smuzhiyun u32 l_s = abs_s << 31;
2116*4882a593Smuzhiyun int counter = 31;
2117*4882a593Smuzhiyun int carry;
2118*4882a593Smuzhiyun
2119*4882a593Smuzhiyun if (s == 0) {
2120*4882a593Smuzhiyun x86emu_intr_raise(0);
2121*4882a593Smuzhiyun return;
2122*4882a593Smuzhiyun }
2123*4882a593Smuzhiyun do {
2124*4882a593Smuzhiyun div <<= 1;
2125*4882a593Smuzhiyun carry = (l_dvd >= l_s) ? 0 : 1;
2126*4882a593Smuzhiyun
2127*4882a593Smuzhiyun if (abs_h_dvd < (h_s + carry)) {
2128*4882a593Smuzhiyun h_s >>= 1;
2129*4882a593Smuzhiyun l_s = abs_s << (--counter);
2130*4882a593Smuzhiyun continue;
2131*4882a593Smuzhiyun } else {
2132*4882a593Smuzhiyun abs_h_dvd -= (h_s + carry);
2133*4882a593Smuzhiyun l_dvd = carry ? ((0xFFFFFFFF - l_s) + l_dvd + 1)
2134*4882a593Smuzhiyun : (l_dvd - l_s);
2135*4882a593Smuzhiyun h_s >>= 1;
2136*4882a593Smuzhiyun l_s = abs_s << (--counter);
2137*4882a593Smuzhiyun div |= 1;
2138*4882a593Smuzhiyun continue;
2139*4882a593Smuzhiyun }
2140*4882a593Smuzhiyun
2141*4882a593Smuzhiyun } while (counter > -1);
2142*4882a593Smuzhiyun /* overflow */
2143*4882a593Smuzhiyun if (abs_h_dvd || (l_dvd > abs_s)) {
2144*4882a593Smuzhiyun x86emu_intr_raise(0);
2145*4882a593Smuzhiyun return;
2146*4882a593Smuzhiyun }
2147*4882a593Smuzhiyun /* sign */
2148*4882a593Smuzhiyun div |= ((h_dvd & 0x10000000) ^ (s & 0x10000000));
2149*4882a593Smuzhiyun mod = l_dvd;
2150*4882a593Smuzhiyun
2151*4882a593Smuzhiyun #endif
2152*4882a593Smuzhiyun CLEAR_FLAG(F_CF);
2153*4882a593Smuzhiyun CLEAR_FLAG(F_AF);
2154*4882a593Smuzhiyun CLEAR_FLAG(F_SF);
2155*4882a593Smuzhiyun SET_FLAG(F_ZF);
2156*4882a593Smuzhiyun set_parity_flag(mod);
2157*4882a593Smuzhiyun
2158*4882a593Smuzhiyun M.x86.R_EAX = (u32)div;
2159*4882a593Smuzhiyun M.x86.R_EDX = (u32)mod;
2160*4882a593Smuzhiyun }
2161*4882a593Smuzhiyun
2162*4882a593Smuzhiyun /****************************************************************************
2163*4882a593Smuzhiyun REMARKS:
2164*4882a593Smuzhiyun Implements the DIV instruction and side effects.
2165*4882a593Smuzhiyun ****************************************************************************/
div_byte(u8 s)2166*4882a593Smuzhiyun void div_byte(u8 s)
2167*4882a593Smuzhiyun {
2168*4882a593Smuzhiyun u32 dvd, div, mod;
2169*4882a593Smuzhiyun
2170*4882a593Smuzhiyun dvd = M.x86.R_AX;
2171*4882a593Smuzhiyun if (s == 0) {
2172*4882a593Smuzhiyun x86emu_intr_raise(0);
2173*4882a593Smuzhiyun return;
2174*4882a593Smuzhiyun }
2175*4882a593Smuzhiyun div = dvd / (u8)s;
2176*4882a593Smuzhiyun mod = dvd % (u8)s;
2177*4882a593Smuzhiyun if (abs(div) > 0xff) {
2178*4882a593Smuzhiyun x86emu_intr_raise(0);
2179*4882a593Smuzhiyun return;
2180*4882a593Smuzhiyun }
2181*4882a593Smuzhiyun M.x86.R_AL = (u8)div;
2182*4882a593Smuzhiyun M.x86.R_AH = (u8)mod;
2183*4882a593Smuzhiyun }
2184*4882a593Smuzhiyun
2185*4882a593Smuzhiyun /****************************************************************************
2186*4882a593Smuzhiyun REMARKS:
2187*4882a593Smuzhiyun Implements the DIV instruction and side effects.
2188*4882a593Smuzhiyun ****************************************************************************/
div_word(u16 s)2189*4882a593Smuzhiyun void div_word(u16 s)
2190*4882a593Smuzhiyun {
2191*4882a593Smuzhiyun u32 dvd, div, mod;
2192*4882a593Smuzhiyun
2193*4882a593Smuzhiyun dvd = (((u32)M.x86.R_DX) << 16) | M.x86.R_AX;
2194*4882a593Smuzhiyun if (s == 0) {
2195*4882a593Smuzhiyun x86emu_intr_raise(0);
2196*4882a593Smuzhiyun return;
2197*4882a593Smuzhiyun }
2198*4882a593Smuzhiyun div = dvd / (u16)s;
2199*4882a593Smuzhiyun mod = dvd % (u16)s;
2200*4882a593Smuzhiyun if (abs(div) > 0xffff) {
2201*4882a593Smuzhiyun x86emu_intr_raise(0);
2202*4882a593Smuzhiyun return;
2203*4882a593Smuzhiyun }
2204*4882a593Smuzhiyun CLEAR_FLAG(F_CF);
2205*4882a593Smuzhiyun CLEAR_FLAG(F_SF);
2206*4882a593Smuzhiyun CONDITIONAL_SET_FLAG(div == 0, F_ZF);
2207*4882a593Smuzhiyun set_parity_flag(mod);
2208*4882a593Smuzhiyun
2209*4882a593Smuzhiyun M.x86.R_AX = (u16)div;
2210*4882a593Smuzhiyun M.x86.R_DX = (u16)mod;
2211*4882a593Smuzhiyun }
2212*4882a593Smuzhiyun
2213*4882a593Smuzhiyun /****************************************************************************
2214*4882a593Smuzhiyun REMARKS:
2215*4882a593Smuzhiyun Implements the DIV instruction and side effects.
2216*4882a593Smuzhiyun ****************************************************************************/
div_long(u32 s)2217*4882a593Smuzhiyun void div_long(u32 s)
2218*4882a593Smuzhiyun {
2219*4882a593Smuzhiyun #ifdef __HAS_LONG_LONG__
2220*4882a593Smuzhiyun u64 dvd, div, mod;
2221*4882a593Smuzhiyun
2222*4882a593Smuzhiyun dvd = (((u64)M.x86.R_EDX) << 32) | M.x86.R_EAX;
2223*4882a593Smuzhiyun if (s == 0) {
2224*4882a593Smuzhiyun x86emu_intr_raise(0);
2225*4882a593Smuzhiyun return;
2226*4882a593Smuzhiyun }
2227*4882a593Smuzhiyun div = dvd / (u32)s;
2228*4882a593Smuzhiyun mod = dvd % (u32)s;
2229*4882a593Smuzhiyun if (abs(div) > 0xffffffff) {
2230*4882a593Smuzhiyun x86emu_intr_raise(0);
2231*4882a593Smuzhiyun return;
2232*4882a593Smuzhiyun }
2233*4882a593Smuzhiyun #else
2234*4882a593Smuzhiyun s32 div = 0, mod;
2235*4882a593Smuzhiyun s32 h_dvd = M.x86.R_EDX;
2236*4882a593Smuzhiyun u32 l_dvd = M.x86.R_EAX;
2237*4882a593Smuzhiyun
2238*4882a593Smuzhiyun u32 h_s = s;
2239*4882a593Smuzhiyun u32 l_s = 0;
2240*4882a593Smuzhiyun int counter = 32;
2241*4882a593Smuzhiyun int carry;
2242*4882a593Smuzhiyun
2243*4882a593Smuzhiyun if (s == 0) {
2244*4882a593Smuzhiyun x86emu_intr_raise(0);
2245*4882a593Smuzhiyun return;
2246*4882a593Smuzhiyun }
2247*4882a593Smuzhiyun do {
2248*4882a593Smuzhiyun div <<= 1;
2249*4882a593Smuzhiyun carry = (l_dvd >= l_s) ? 0 : 1;
2250*4882a593Smuzhiyun
2251*4882a593Smuzhiyun if (h_dvd < (h_s + carry)) {
2252*4882a593Smuzhiyun h_s >>= 1;
2253*4882a593Smuzhiyun l_s = s << (--counter);
2254*4882a593Smuzhiyun continue;
2255*4882a593Smuzhiyun } else {
2256*4882a593Smuzhiyun h_dvd -= (h_s + carry);
2257*4882a593Smuzhiyun l_dvd = carry ? ((0xFFFFFFFF - l_s) + l_dvd + 1)
2258*4882a593Smuzhiyun : (l_dvd - l_s);
2259*4882a593Smuzhiyun h_s >>= 1;
2260*4882a593Smuzhiyun l_s = s << (--counter);
2261*4882a593Smuzhiyun div |= 1;
2262*4882a593Smuzhiyun continue;
2263*4882a593Smuzhiyun }
2264*4882a593Smuzhiyun
2265*4882a593Smuzhiyun } while (counter > -1);
2266*4882a593Smuzhiyun /* overflow */
2267*4882a593Smuzhiyun if (h_dvd || (l_dvd > s)) {
2268*4882a593Smuzhiyun x86emu_intr_raise(0);
2269*4882a593Smuzhiyun return;
2270*4882a593Smuzhiyun }
2271*4882a593Smuzhiyun mod = l_dvd;
2272*4882a593Smuzhiyun #endif
2273*4882a593Smuzhiyun CLEAR_FLAG(F_CF);
2274*4882a593Smuzhiyun CLEAR_FLAG(F_AF);
2275*4882a593Smuzhiyun CLEAR_FLAG(F_SF);
2276*4882a593Smuzhiyun SET_FLAG(F_ZF);
2277*4882a593Smuzhiyun set_parity_flag(mod);
2278*4882a593Smuzhiyun
2279*4882a593Smuzhiyun M.x86.R_EAX = (u32)div;
2280*4882a593Smuzhiyun M.x86.R_EDX = (u32)mod;
2281*4882a593Smuzhiyun }
2282*4882a593Smuzhiyun
2283*4882a593Smuzhiyun /****************************************************************************
2284*4882a593Smuzhiyun REMARKS:
2285*4882a593Smuzhiyun Implements the IN string instruction and side effects.
2286*4882a593Smuzhiyun ****************************************************************************/
2287*4882a593Smuzhiyun
single_in(int size)2288*4882a593Smuzhiyun static void single_in(int size)
2289*4882a593Smuzhiyun {
2290*4882a593Smuzhiyun if(size == 1)
2291*4882a593Smuzhiyun store_data_byte_abs(M.x86.R_ES, M.x86.R_DI,(*sys_inb)(M.x86.R_DX));
2292*4882a593Smuzhiyun else if (size == 2)
2293*4882a593Smuzhiyun store_data_word_abs(M.x86.R_ES, M.x86.R_DI,(*sys_inw)(M.x86.R_DX));
2294*4882a593Smuzhiyun else
2295*4882a593Smuzhiyun store_data_long_abs(M.x86.R_ES, M.x86.R_DI,(*sys_inl)(M.x86.R_DX));
2296*4882a593Smuzhiyun }
2297*4882a593Smuzhiyun
ins(int size)2298*4882a593Smuzhiyun void ins(int size)
2299*4882a593Smuzhiyun {
2300*4882a593Smuzhiyun int inc = size;
2301*4882a593Smuzhiyun
2302*4882a593Smuzhiyun if (ACCESS_FLAG(F_DF)) {
2303*4882a593Smuzhiyun inc = -size;
2304*4882a593Smuzhiyun }
2305*4882a593Smuzhiyun if (M.x86.mode & (SYSMODE_PREFIX_REPE | SYSMODE_PREFIX_REPNE)) {
2306*4882a593Smuzhiyun /* dont care whether REPE or REPNE */
2307*4882a593Smuzhiyun /* in until CX is ZERO. */
2308*4882a593Smuzhiyun u32 count = ((M.x86.mode & SYSMODE_PREFIX_DATA) ?
2309*4882a593Smuzhiyun M.x86.R_ECX : M.x86.R_CX);
2310*4882a593Smuzhiyun
2311*4882a593Smuzhiyun while (count--) {
2312*4882a593Smuzhiyun single_in(size);
2313*4882a593Smuzhiyun M.x86.R_DI += inc;
2314*4882a593Smuzhiyun }
2315*4882a593Smuzhiyun M.x86.R_CX = 0;
2316*4882a593Smuzhiyun if (M.x86.mode & SYSMODE_PREFIX_DATA) {
2317*4882a593Smuzhiyun M.x86.R_ECX = 0;
2318*4882a593Smuzhiyun }
2319*4882a593Smuzhiyun M.x86.mode &= ~(SYSMODE_PREFIX_REPE | SYSMODE_PREFIX_REPNE);
2320*4882a593Smuzhiyun } else {
2321*4882a593Smuzhiyun single_in(size);
2322*4882a593Smuzhiyun M.x86.R_DI += inc;
2323*4882a593Smuzhiyun }
2324*4882a593Smuzhiyun }
2325*4882a593Smuzhiyun
2326*4882a593Smuzhiyun /****************************************************************************
2327*4882a593Smuzhiyun REMARKS:
2328*4882a593Smuzhiyun Implements the OUT string instruction and side effects.
2329*4882a593Smuzhiyun ****************************************************************************/
2330*4882a593Smuzhiyun
single_out(int size)2331*4882a593Smuzhiyun static void single_out(int size)
2332*4882a593Smuzhiyun {
2333*4882a593Smuzhiyun if(size == 1)
2334*4882a593Smuzhiyun (*sys_outb)(M.x86.R_DX,fetch_data_byte_abs(M.x86.R_ES, M.x86.R_SI));
2335*4882a593Smuzhiyun else if (size == 2)
2336*4882a593Smuzhiyun (*sys_outw)(M.x86.R_DX,fetch_data_word_abs(M.x86.R_ES, M.x86.R_SI));
2337*4882a593Smuzhiyun else
2338*4882a593Smuzhiyun (*sys_outl)(M.x86.R_DX,fetch_data_long_abs(M.x86.R_ES, M.x86.R_SI));
2339*4882a593Smuzhiyun }
2340*4882a593Smuzhiyun
outs(int size)2341*4882a593Smuzhiyun void outs(int size)
2342*4882a593Smuzhiyun {
2343*4882a593Smuzhiyun int inc = size;
2344*4882a593Smuzhiyun
2345*4882a593Smuzhiyun if (ACCESS_FLAG(F_DF)) {
2346*4882a593Smuzhiyun inc = -size;
2347*4882a593Smuzhiyun }
2348*4882a593Smuzhiyun if (M.x86.mode & (SYSMODE_PREFIX_REPE | SYSMODE_PREFIX_REPNE)) {
2349*4882a593Smuzhiyun /* dont care whether REPE or REPNE */
2350*4882a593Smuzhiyun /* out until CX is ZERO. */
2351*4882a593Smuzhiyun u32 count = ((M.x86.mode & SYSMODE_PREFIX_DATA) ?
2352*4882a593Smuzhiyun M.x86.R_ECX : M.x86.R_CX);
2353*4882a593Smuzhiyun while (count--) {
2354*4882a593Smuzhiyun single_out(size);
2355*4882a593Smuzhiyun M.x86.R_SI += inc;
2356*4882a593Smuzhiyun }
2357*4882a593Smuzhiyun M.x86.R_CX = 0;
2358*4882a593Smuzhiyun if (M.x86.mode & SYSMODE_PREFIX_DATA) {
2359*4882a593Smuzhiyun M.x86.R_ECX = 0;
2360*4882a593Smuzhiyun }
2361*4882a593Smuzhiyun M.x86.mode &= ~(SYSMODE_PREFIX_REPE | SYSMODE_PREFIX_REPNE);
2362*4882a593Smuzhiyun } else {
2363*4882a593Smuzhiyun single_out(size);
2364*4882a593Smuzhiyun M.x86.R_SI += inc;
2365*4882a593Smuzhiyun }
2366*4882a593Smuzhiyun }
2367*4882a593Smuzhiyun
2368*4882a593Smuzhiyun /****************************************************************************
2369*4882a593Smuzhiyun PARAMETERS:
2370*4882a593Smuzhiyun addr - Address to fetch word from
2371*4882a593Smuzhiyun
2372*4882a593Smuzhiyun REMARKS:
2373*4882a593Smuzhiyun Fetches a word from emulator memory using an absolute address.
2374*4882a593Smuzhiyun ****************************************************************************/
mem_access_word(int addr)2375*4882a593Smuzhiyun u16 mem_access_word(int addr)
2376*4882a593Smuzhiyun {
2377*4882a593Smuzhiyun DB( if (CHECK_MEM_ACCESS())
2378*4882a593Smuzhiyun x86emu_check_mem_access(addr);)
2379*4882a593Smuzhiyun return (*sys_rdw)(addr);
2380*4882a593Smuzhiyun }
2381*4882a593Smuzhiyun
2382*4882a593Smuzhiyun /****************************************************************************
2383*4882a593Smuzhiyun REMARKS:
2384*4882a593Smuzhiyun Pushes a word onto the stack.
2385*4882a593Smuzhiyun
2386*4882a593Smuzhiyun NOTE: Do not inline this, as (*sys_wrX) is already inline!
2387*4882a593Smuzhiyun ****************************************************************************/
push_word(u16 w)2388*4882a593Smuzhiyun void push_word(u16 w)
2389*4882a593Smuzhiyun {
2390*4882a593Smuzhiyun DB( if (CHECK_SP_ACCESS())
2391*4882a593Smuzhiyun x86emu_check_sp_access();)
2392*4882a593Smuzhiyun M.x86.R_SP -= 2;
2393*4882a593Smuzhiyun (*sys_wrw)(((u32)M.x86.R_SS << 4) + M.x86.R_SP, w);
2394*4882a593Smuzhiyun }
2395*4882a593Smuzhiyun
2396*4882a593Smuzhiyun /****************************************************************************
2397*4882a593Smuzhiyun REMARKS:
2398*4882a593Smuzhiyun Pushes a long onto the stack.
2399*4882a593Smuzhiyun
2400*4882a593Smuzhiyun NOTE: Do not inline this, as (*sys_wrX) is already inline!
2401*4882a593Smuzhiyun ****************************************************************************/
push_long(u32 w)2402*4882a593Smuzhiyun void push_long(u32 w)
2403*4882a593Smuzhiyun {
2404*4882a593Smuzhiyun DB( if (CHECK_SP_ACCESS())
2405*4882a593Smuzhiyun x86emu_check_sp_access();)
2406*4882a593Smuzhiyun M.x86.R_SP -= 4;
2407*4882a593Smuzhiyun (*sys_wrl)(((u32)M.x86.R_SS << 4) + M.x86.R_SP, w);
2408*4882a593Smuzhiyun }
2409*4882a593Smuzhiyun
2410*4882a593Smuzhiyun /****************************************************************************
2411*4882a593Smuzhiyun REMARKS:
2412*4882a593Smuzhiyun Pops a word from the stack.
2413*4882a593Smuzhiyun
2414*4882a593Smuzhiyun NOTE: Do not inline this, as (*sys_rdX) is already inline!
2415*4882a593Smuzhiyun ****************************************************************************/
pop_word(void)2416*4882a593Smuzhiyun u16 pop_word(void)
2417*4882a593Smuzhiyun {
2418*4882a593Smuzhiyun u16 res;
2419*4882a593Smuzhiyun
2420*4882a593Smuzhiyun DB( if (CHECK_SP_ACCESS())
2421*4882a593Smuzhiyun x86emu_check_sp_access();)
2422*4882a593Smuzhiyun res = (*sys_rdw)(((u32)M.x86.R_SS << 4) + M.x86.R_SP);
2423*4882a593Smuzhiyun M.x86.R_SP += 2;
2424*4882a593Smuzhiyun return res;
2425*4882a593Smuzhiyun }
2426*4882a593Smuzhiyun
2427*4882a593Smuzhiyun /****************************************************************************
2428*4882a593Smuzhiyun REMARKS:
2429*4882a593Smuzhiyun Pops a long from the stack.
2430*4882a593Smuzhiyun
2431*4882a593Smuzhiyun NOTE: Do not inline this, as (*sys_rdX) is already inline!
2432*4882a593Smuzhiyun ****************************************************************************/
pop_long(void)2433*4882a593Smuzhiyun u32 pop_long(void)
2434*4882a593Smuzhiyun {
2435*4882a593Smuzhiyun u32 res;
2436*4882a593Smuzhiyun
2437*4882a593Smuzhiyun DB( if (CHECK_SP_ACCESS())
2438*4882a593Smuzhiyun x86emu_check_sp_access();)
2439*4882a593Smuzhiyun res = (*sys_rdl)(((u32)M.x86.R_SS << 4) + M.x86.R_SP);
2440*4882a593Smuzhiyun M.x86.R_SP += 4;
2441*4882a593Smuzhiyun return res;
2442*4882a593Smuzhiyun }
2443