1 /////////////////////////////////////////////////////////////////////////
2 // $Id: bit64.cc,v 1.19 2008/08/11 18:53:23 sshwarts Exp $
3 /////////////////////////////////////////////////////////////////////////
5 // Copyright (C) 2001 MandrakeSoft S.A.
9 // 75002 Paris - France
10 // http://www.linux-mandrake.com/
11 // http://www.mandrakesoft.com/
13 // This library is free software; you can redistribute it and/or
14 // modify it under the terms of the GNU Lesser General Public
15 // License as published by the Free Software Foundation; either
16 // version 2 of the License, or (at your option) any later version.
18 // This library is distributed in the hope that it will be useful,
19 // but WITHOUT ANY WARRANTY; without even the implied warranty of
20 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
21 // Lesser General Public License for more details.
23 // You should have received a copy of the GNU Lesser General Public
24 // License along with this library; if not, write to the Free Software
25 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
26 /////////////////////////////////////////////////////////////////////////
28 #define NEED_CPU_REG_SHORTCUTS 1
31 #define LOG_THIS BX_CPU_THIS_PTR
35 void BX_CPP_AttrRegparmN(1) BX_CPU_C::BSF_GqEqR(bxInstruction_c
*i
)
37 Bit64u op2_64
= BX_READ_64BIT_REG(i
->rm());
40 assert_ZF(); /* op1_64 undefined */
44 while ((op2_64
& 0x01) == 0) {
49 SET_FLAGS_OSZAPC_LOGIC_64(op1_64
);
52 /* now write result back to destination */
53 BX_WRITE_64BIT_REG(i
->nnn(), op1_64
);
57 void BX_CPP_AttrRegparmN(1) BX_CPU_C::BSR_GqEqR(bxInstruction_c
*i
)
59 Bit64u op2_64
= BX_READ_64BIT_REG(i
->rm());
62 assert_ZF(); /* op1_64 undefined */
66 while ((op2_64
& BX_CONST64(0x8000000000000000)) == 0) {
71 SET_FLAGS_OSZAPC_LOGIC_64(op1_64
);
74 /* now write result back to destination */
75 BX_WRITE_64BIT_REG(i
->nnn(), op1_64
);
79 void BX_CPP_AttrRegparmN(1) BX_CPU_C::BT_EqGqM(bxInstruction_c
*i
)
82 Bit64u op1_64
, op2_64
;
83 Bit64s displacement64
;
86 bx_address eaddr
= BX_CPU_CALL_METHODR(i
->ResolveModrm
, (i
));
88 op2_64
= BX_READ_64BIT_REG(i
->nnn());
89 index
= op2_64
& 0x3f;
90 displacement64
= ((Bit64s
) (op2_64
& BX_CONST64(0xffffffffffffffc0))) / 64;
91 op1_addr
= eaddr
+ 8 * displacement64
;
93 op1_addr
= (Bit32u
) op1_addr
;
95 /* pointer, segment address pair */
96 op1_64
= read_virtual_qword_64(i
->seg(), op1_addr
);
98 set_CF((op1_64
>> index
) & 0x01);
101 void BX_CPP_AttrRegparmN(1) BX_CPU_C::BT_EqGqR(bxInstruction_c
*i
)
103 Bit64u op1_64
, op2_64
;
105 op1_64
= BX_READ_64BIT_REG(i
->rm());
106 op2_64
= BX_READ_64BIT_REG(i
->nnn());
108 set_CF((op1_64
>> op2_64
) & 0x01);
111 void BX_CPP_AttrRegparmN(1) BX_CPU_C::BTS_EqGqM(bxInstruction_c
*i
)
114 Bit64u op1_64
, op2_64
, index
;
115 Bit64s displacement64
;
118 bx_address eaddr
= BX_CPU_CALL_METHODR(i
->ResolveModrm
, (i
));
120 op2_64
= BX_READ_64BIT_REG(i
->nnn());
121 index
= op2_64
& 0x3f;
122 displacement64
= ((Bit64s
) (op2_64
& BX_CONST64(0xffffffffffffffc0))) / 64;
123 op1_addr
= eaddr
+ 8 * displacement64
;
125 op1_addr
= (Bit32u
) op1_addr
;
127 /* pointer, segment address pair */
128 op1_64
= read_RMW_virtual_qword_64(i
->seg(), op1_addr
);
129 bit_i
= (op1_64
>> index
) & 0x01;
130 op1_64
|= (((Bit64u
) 1) << index
);
131 write_RMW_virtual_qword(op1_64
);
136 void BX_CPP_AttrRegparmN(1) BX_CPU_C::BTS_EqGqR(bxInstruction_c
*i
)
138 Bit64u op1_64
, op2_64
;
140 op1_64
= BX_READ_64BIT_REG(i
->rm());
141 op2_64
= BX_READ_64BIT_REG(i
->nnn());
143 set_CF((op1_64
>> op2_64
) & 0x01);
144 op1_64
|= (((Bit64u
) 1) << op2_64
);
146 /* now write result back to the destination */
147 BX_WRITE_64BIT_REG(i
->rm(), op1_64
);
150 void BX_CPP_AttrRegparmN(1) BX_CPU_C::BTR_EqGqM(bxInstruction_c
*i
)
153 Bit64u op1_64
, op2_64
, index
;
154 Bit64s displacement64
;
156 bx_address eaddr
= BX_CPU_CALL_METHODR(i
->ResolveModrm
, (i
));
158 op2_64
= BX_READ_64BIT_REG(i
->nnn());
159 index
= op2_64
& 0x3f;
160 displacement64
= ((Bit64s
) (op2_64
& BX_CONST64(0xffffffffffffffc0))) / 64;
161 op1_addr
= eaddr
+ 8 * displacement64
;
163 op1_addr
= (Bit32u
) op1_addr
;
165 /* pointer, segment address pair */
166 op1_64
= read_RMW_virtual_qword_64(i
->seg(), op1_addr
);
167 bx_bool temp_cf
= (op1_64
>> index
) & 0x01;
168 op1_64
&= ~(((Bit64u
) 1) << index
);
169 /* now write back to destination */
170 write_RMW_virtual_qword(op1_64
);
175 void BX_CPP_AttrRegparmN(1) BX_CPU_C::BTR_EqGqR(bxInstruction_c
*i
)
177 Bit64u op1_64
, op2_64
;
179 op1_64
= BX_READ_64BIT_REG(i
->rm());
180 op2_64
= BX_READ_64BIT_REG(i
->nnn());
182 set_CF((op1_64
>> op2_64
) & 0x01);
183 op1_64
&= ~(((Bit64u
) 1) << op2_64
);
185 /* now write result back to the destination */
186 BX_WRITE_64BIT_REG(i
->rm(), op1_64
);
189 void BX_CPP_AttrRegparmN(1) BX_CPU_C::BTC_EqGqM(bxInstruction_c
*i
)
192 Bit64u op1_64
, op2_64
;
193 Bit64s displacement64
;
196 bx_address eaddr
= BX_CPU_CALL_METHODR(i
->ResolveModrm
, (i
));
198 op2_64
= BX_READ_64BIT_REG(i
->nnn());
199 index
= op2_64
& 0x3f;
200 displacement64
= ((Bit64s
) (op2_64
& BX_CONST64(0xffffffffffffffc0))) / 64;
201 op1_addr
= eaddr
+ 8 * displacement64
;
203 op1_addr
= (Bit32u
) op1_addr
;
205 op1_64
= read_RMW_virtual_qword_64(i
->seg(), op1_addr
);
206 bx_bool temp_CF
= (op1_64
>> index
) & 0x01;
207 op1_64
^= (((Bit64u
) 1) << index
); /* toggle bit */
210 write_RMW_virtual_qword(op1_64
);
213 void BX_CPP_AttrRegparmN(1) BX_CPU_C::BTC_EqGqR(bxInstruction_c
*i
)
215 Bit64u op1_64
, op2_64
;
217 op1_64
= BX_READ_64BIT_REG(i
->rm());
218 op2_64
= BX_READ_64BIT_REG(i
->nnn());
221 bx_bool temp_CF
= (op1_64
>> op2_64
) & 0x01;
222 op1_64
^= (((Bit64u
) 1) << op2_64
); /* toggle bit */
225 BX_WRITE_64BIT_REG(i
->rm(), op1_64
);
228 void BX_CPP_AttrRegparmN(1) BX_CPU_C::BT_EqIbM(bxInstruction_c
*i
)
230 bx_address eaddr
= BX_CPU_CALL_METHODR(i
->ResolveModrm
, (i
));
232 Bit64u op1_64
= read_virtual_qword_64(i
->seg(), eaddr
);
233 Bit8u op2_8
= i
->Ib() & 0x3f;
235 set_CF((op1_64
>> op2_8
) & 0x01);
238 void BX_CPP_AttrRegparmN(1) BX_CPU_C::BT_EqIbR(bxInstruction_c
*i
)
240 Bit64u op1_64
= BX_READ_64BIT_REG(i
->rm());
241 Bit8u op2_8
= i
->Ib() & 0x3f;
243 set_CF((op1_64
>> op2_8
) & 0x01);
246 void BX_CPP_AttrRegparmN(1) BX_CPU_C::BTS_EqIbM(bxInstruction_c
*i
)
248 Bit8u op2_8
= i
->Ib() & 0x3f;
250 bx_address eaddr
= BX_CPU_CALL_METHODR(i
->ResolveModrm
, (i
));
252 Bit64u op1_64
= read_RMW_virtual_qword_64(i
->seg(), eaddr
);
253 bx_bool temp_CF
= (op1_64
>> op2_8
) & 0x01;
254 op1_64
|= (((Bit64u
) 1) << op2_8
);
255 write_RMW_virtual_qword(op1_64
);
260 void BX_CPP_AttrRegparmN(1) BX_CPU_C::BTS_EqIbR(bxInstruction_c
*i
)
262 Bit8u op2_8
= i
->Ib() & 0x3f;
264 Bit64u op1_64
= BX_READ_64BIT_REG(i
->rm());
265 bx_bool temp_CF
= (op1_64
>> op2_8
) & 0x01;
266 op1_64
|= (((Bit64u
) 1) << op2_8
);
267 BX_WRITE_64BIT_REG(i
->rm(), op1_64
);
272 void BX_CPP_AttrRegparmN(1) BX_CPU_C::BTC_EqIbM(bxInstruction_c
*i
)
274 Bit8u op2_8
= i
->Ib() & 0x3f;
276 bx_address eaddr
= BX_CPU_CALL_METHODR(i
->ResolveModrm
, (i
));
278 Bit64u op1_64
= read_RMW_virtual_qword_64(i
->seg(), eaddr
);
279 bx_bool temp_CF
= (op1_64
>> op2_8
) & 0x01;
280 op1_64
^= (((Bit64u
) 1) << op2_8
); /* toggle bit */
281 write_RMW_virtual_qword(op1_64
);
286 void BX_CPP_AttrRegparmN(1) BX_CPU_C::BTC_EqIbR(bxInstruction_c
*i
)
288 Bit8u op2_8
= i
->Ib() & 0x3f;
290 Bit64u op1_64
= BX_READ_64BIT_REG(i
->rm());
291 bx_bool temp_CF
= (op1_64
>> op2_8
) & 0x01;
292 op1_64
^= (((Bit64u
) 1) << op2_8
); /* toggle bit */
293 BX_WRITE_64BIT_REG(i
->rm(), op1_64
);
298 void BX_CPP_AttrRegparmN(1) BX_CPU_C::BTR_EqIbM(bxInstruction_c
*i
)
300 Bit8u op2_8
= i
->Ib() & 0x3f;
302 bx_address eaddr
= BX_CPU_CALL_METHODR(i
->ResolveModrm
, (i
));
304 Bit64u op1_64
= read_RMW_virtual_qword_64(i
->seg(), eaddr
);
305 bx_bool temp_CF
= (op1_64
>> op2_8
) & 0x01;
306 op1_64
&= ~(((Bit64u
) 1) << op2_8
);
307 write_RMW_virtual_qword(op1_64
);
312 void BX_CPP_AttrRegparmN(1) BX_CPU_C::BTR_EqIbR(bxInstruction_c
*i
)
314 Bit8u op2_8
= i
->Ib() & 0x3f;
316 Bit64u op1_64
= BX_READ_64BIT_REG(i
->rm());
317 bx_bool temp_CF
= (op1_64
>> op2_8
) & 0x01;
318 op1_64
&= ~(((Bit64u
) 1) << op2_8
);
319 BX_WRITE_64BIT_REG(i
->rm(), op1_64
);
325 void BX_CPP_AttrRegparmN(1) BX_CPU_C::POPCNT_GqEqR(bxInstruction_c
*i
)
327 #if BX_SUPPORT_POPCNT || (BX_SUPPORT_SSE > 4) || (BX_SUPPORT_SSE >= 4 && BX_SUPPORT_SSE_EXTENSION > 0)
328 Bit64u op2_64
= BX_READ_64BIT_REG(i
->rm());
331 while (op2_64
!= 0) {
332 if (op2_64
& 1) op1_64
++;
336 Bit32u flags
= op1_64
? 0 : EFlagsZFMask
;
337 setEFlagsOSZAPC(flags
);
339 /* now write result back to destination */
340 BX_WRITE_64BIT_REG(i
->nnn(), op1_64
);
342 BX_INFO(("POPCNT_GqEq: required POPCNT support, use --enable-popcnt option"));
343 exception(BX_UD_EXCEPTION
, 0, 0);
347 #endif // BX_SUPPORT_X86_64