1 /////////////////////////////////////////////////////////////////////////
2 // $Id: arith8.cc,v 1.39 2006/03/26 18:58:00 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
28 #define NEED_CPU_REG_SHORTCUTS 1
31 #define LOG_THIS BX_CPU_THIS_PTR
34 void BX_CPU_C::ADD_EbGb(bxInstruction_c
*i
)
38 op2
= BX_READ_8BIT_REGx(i
->nnn(),i
->extend8bitL());
41 op1
= BX_READ_8BIT_REGx(i
->rm(),i
->extend8bitL());
43 BX_WRITE_8BIT_REGx(i
->rm(), i
->extend8bitL(), sum
);
46 read_RMW_virtual_byte(i
->seg(), RMAddr(i
), &op1
);
48 write_RMW_virtual_byte(sum
);
51 SET_FLAGS_OSZAPC_8(op1
, op2
, sum
, BX_INSTR_ADD8
);
54 void BX_CPU_C::ADD_GbEb(bxInstruction_c
*i
)
58 op1
= BX_READ_8BIT_REGx(i
->nnn(),i
->extend8bitL());
61 op2
= BX_READ_8BIT_REGx(i
->rm(),i
->extend8bitL());
65 read_virtual_byte(i
->seg(), RMAddr(i
), &op2
);
69 BX_WRITE_8BIT_REGx(i
->nnn(), i
->extend8bitL(), sum
);
71 SET_FLAGS_OSZAPC_8(op1
, op2
, sum
, BX_INSTR_ADD8
);
74 void BX_CPU_C::ADD_ALIb(bxInstruction_c
*i
)
83 SET_FLAGS_OSZAPC_8(op1
, op2
, sum
, BX_INSTR_ADD8
);
86 void BX_CPU_C::ADC_EbGb(bxInstruction_c
*i
)
89 bx_bool temp_CF
= getB_CF();
91 op2
= BX_READ_8BIT_REGx(i
->nnn(),i
->extend8bitL());
94 op1
= BX_READ_8BIT_REGx(i
->rm(),i
->extend8bitL());
95 sum
= op1
+ op2
+ temp_CF
;
96 BX_WRITE_8BIT_REGx(i
->rm(), i
->extend8bitL(), sum
);
99 read_RMW_virtual_byte(i
->seg(), RMAddr(i
), &op1
);
100 sum
= op1
+ op2
+ temp_CF
;
101 write_RMW_virtual_byte(sum
);
104 SET_FLAGS_OSZAPC_8(op1
, op2
, sum
, BX_INSTR_ADD_ADC8(temp_CF
));
107 void BX_CPU_C::ADC_GbEb(bxInstruction_c
*i
)
110 bx_bool temp_CF
= getB_CF();
112 op1
= BX_READ_8BIT_REGx(i
->nnn(),i
->extend8bitL());
115 op2
= BX_READ_8BIT_REGx(i
->rm(),i
->extend8bitL());
118 read_virtual_byte(i
->seg(), RMAddr(i
), &op2
);
121 sum
= op1
+ op2
+ temp_CF
;
123 SET_FLAGS_OSZAPC_8(op1
, op2
, sum
, BX_INSTR_ADD_ADC8(temp_CF
));
125 BX_WRITE_8BIT_REGx(i
->nnn(), i
->extend8bitL(), sum
);
128 void BX_CPU_C::ADC_ALIb(bxInstruction_c
*i
)
131 bx_bool temp_CF
= getB_CF();
135 sum
= op1
+ op2
+ temp_CF
;
138 SET_FLAGS_OSZAPC_8(op1
, op2
, sum
, BX_INSTR_ADD_ADC8(temp_CF
));
141 void BX_CPU_C::SBB_EbGb(bxInstruction_c
*i
)
143 Bit8u op2_8
, op1_8
, diff_8
;
144 bx_bool temp_CF
= getB_CF();
146 op2_8
= BX_READ_8BIT_REGx(i
->nnn(),i
->extend8bitL());
149 op1_8
= BX_READ_8BIT_REGx(i
->rm(),i
->extend8bitL());
150 diff_8
= op1_8
- (op2_8
+ temp_CF
);
151 BX_WRITE_8BIT_REGx(i
->rm(), i
->extend8bitL(), diff_8
);
154 read_RMW_virtual_byte(i
->seg(), RMAddr(i
), &op1_8
);
155 diff_8
= op1_8
- (op2_8
+ temp_CF
);
156 write_RMW_virtual_byte(diff_8
);
159 SET_FLAGS_OSZAPC_8(op1_8
, op2_8
, diff_8
, BX_INSTR_SUB_SBB8(temp_CF
));
162 void BX_CPU_C::SBB_GbEb(bxInstruction_c
*i
)
164 Bit8u op1_8
, op2_8
, diff_8
;
165 bx_bool temp_CF
= getB_CF();
167 op1_8
= BX_READ_8BIT_REGx(i
->nnn(),i
->extend8bitL());
170 op2_8
= BX_READ_8BIT_REGx(i
->rm(),i
->extend8bitL());
173 read_virtual_byte(i
->seg(), RMAddr(i
), &op2_8
);
176 diff_8
= op1_8
- (op2_8
+ temp_CF
);
178 BX_WRITE_8BIT_REGx(i
->nnn(), i
->extend8bitL(), diff_8
);
180 SET_FLAGS_OSZAPC_8(op1_8
, op2_8
, diff_8
, BX_INSTR_SUB_SBB8(temp_CF
));
183 void BX_CPU_C::SBB_ALIb(bxInstruction_c
*i
)
185 Bit8u op1_8
, op2_8
, diff_8
;
186 bx_bool temp_CF
= getB_CF();
190 diff_8
= op1_8
- (op2_8
+ temp_CF
);
193 SET_FLAGS_OSZAPC_8(op1_8
, op2_8
, diff_8
, BX_INSTR_SUB_SBB8(temp_CF
));
196 void BX_CPU_C::SBB_EbIb(bxInstruction_c
*i
)
198 Bit8u op2_8
, op1_8
, diff_8
;
199 bx_bool temp_CF
= getB_CF();
204 op1_8
= BX_READ_8BIT_REGx(i
->rm(),i
->extend8bitL());
205 diff_8
= op1_8
- (op2_8
+ temp_CF
);
206 BX_WRITE_8BIT_REGx(i
->rm(), i
->extend8bitL(), diff_8
);
209 read_RMW_virtual_byte(i
->seg(), RMAddr(i
), &op1_8
);
210 diff_8
= op1_8
- (op2_8
+ temp_CF
);
211 write_RMW_virtual_byte(diff_8
);
214 SET_FLAGS_OSZAPC_8(op1_8
, op2_8
, diff_8
, BX_INSTR_SUB_SBB8(temp_CF
));
217 void BX_CPU_C::SUB_EbGb(bxInstruction_c
*i
)
219 Bit8u op2_8
, op1_8
, diff_8
;
221 op2_8
= BX_READ_8BIT_REGx(i
->nnn(),i
->extend8bitL());
224 op1_8
= BX_READ_8BIT_REGx(i
->rm(),i
->extend8bitL());
225 diff_8
= op1_8
- op2_8
;
226 BX_WRITE_8BIT_REGx(i
->rm(), i
->extend8bitL(), diff_8
);
229 read_RMW_virtual_byte(i
->seg(), RMAddr(i
), &op1_8
);
230 diff_8
= op1_8
- op2_8
;
231 write_RMW_virtual_byte(diff_8
);
234 SET_FLAGS_OSZAPC_8(op1_8
, op2_8
, diff_8
, BX_INSTR_SUB8
);
237 void BX_CPU_C::SUB_GbEb(bxInstruction_c
*i
)
239 Bit8u op1_8
, op2_8
, diff_8
;
241 op1_8
= BX_READ_8BIT_REGx(i
->nnn(),i
->extend8bitL());
244 op2_8
= BX_READ_8BIT_REGx(i
->rm(),i
->extend8bitL());
247 read_virtual_byte(i
->seg(), RMAddr(i
), &op2_8
);
250 diff_8
= op1_8
- op2_8
;
252 BX_WRITE_8BIT_REGx(i
->nnn(), i
->extend8bitL(), diff_8
);
254 SET_FLAGS_OSZAPC_8(op1_8
, op2_8
, diff_8
, BX_INSTR_SUB8
);
257 void BX_CPU_C::SUB_ALIb(bxInstruction_c
*i
)
259 Bit8u op1_8
, op2_8
, diff_8
;
263 diff_8
= op1_8
- op2_8
;
266 SET_FLAGS_OSZAPC_8(op1_8
, op2_8
, diff_8
, BX_INSTR_SUB8
);
269 void BX_CPU_C::CMP_EbGb(bxInstruction_c
*i
)
273 op2_8
= BX_READ_8BIT_REGx(i
->nnn(),i
->extend8bitL());
276 op1_8
= BX_READ_8BIT_REGx(i
->rm(),i
->extend8bitL());
279 read_virtual_byte(i
->seg(), RMAddr(i
), &op1_8
);
282 #if defined(BX_HostAsm_Cmp8)
284 asmCmp8(op1_8
, op2_8
, flags32
);
285 setEFlagsOSZAPC(flags32
);
287 Bit8u diff_8
= op1_8
- op2_8
;
288 SET_FLAGS_OSZAPC_8(op1_8
, op2_8
, diff_8
, BX_INSTR_COMPARE8
);
292 void BX_CPU_C::CMP_GbEb(bxInstruction_c
*i
)
296 op1_8
= BX_READ_8BIT_REGx(i
->nnn(),i
->extend8bitL());
299 op2_8
= BX_READ_8BIT_REGx(i
->rm(),i
->extend8bitL());
302 read_virtual_byte(i
->seg(), RMAddr(i
), &op2_8
);
305 #if defined(BX_HostAsm_Cmp8)
307 asmCmp8(op1_8
, op2_8
, flags32
);
308 setEFlagsOSZAPC(flags32
);
310 Bit8u diff_8
= op1_8
- op2_8
;
311 SET_FLAGS_OSZAPC_8(op1_8
, op2_8
, diff_8
, BX_INSTR_COMPARE8
);
315 void BX_CPU_C::CMP_ALIb(bxInstruction_c
*i
)
318 Bit8u op2_8
= i
->Ib();
320 #if defined(BX_HostAsm_Cmp8)
322 asmCmp8(op1_8
, op2_8
, flags32
);
323 setEFlagsOSZAPC(flags32
);
325 Bit8u diff_8
= op1_8
- op2_8
;
326 SET_FLAGS_OSZAPC_8(op1_8
, op2_8
, diff_8
, BX_INSTR_COMPARE8
);
330 void BX_CPU_C::XADD_EbGb(bxInstruction_c
*i
)
332 #if (BX_CPU_LEVEL >= 4) || (BX_CPU_LEVEL_HACKED >= 4)
335 /* XADD dst(r/m8), src(r8)
336 * temp <-- src + dst | sum = op2 + op1
337 * src <-- dst | op2 = op1
338 * dst <-- tmp | op1 = sum
341 op2
= BX_READ_8BIT_REGx(i
->nnn(),i
->extend8bitL());
344 op1
= BX_READ_8BIT_REGx(i
->rm(),i
->extend8bitL());
346 // and write destination into source
347 // Note: if both op1 & op2 are registers, the last one written
348 // should be the sum, as op1 & op2 may be the same register.
349 // For example: XADD AL, AL
350 BX_WRITE_8BIT_REGx(i
->nnn(), i
->extend8bitL(), op1
);
351 BX_WRITE_8BIT_REGx(i
->rm(), i
->extend8bitL(), sum
);
354 read_RMW_virtual_byte(i
->seg(), RMAddr(i
), &op1
);
356 write_RMW_virtual_byte(sum
);
357 /* and write destination into source */
358 BX_WRITE_8BIT_REGx(i
->nnn(), i
->extend8bitL(), op1
);
361 SET_FLAGS_OSZAPC_8(op1
, op2
, sum
, BX_INSTR_ADD8
);
363 BX_INFO(("XADD_EbGb: not supported on < 80486"));
368 void BX_CPU_C::ADD_EbIb(bxInstruction_c
*i
)
375 op1
= BX_READ_8BIT_REGx(i
->rm(),i
->extend8bitL());
377 BX_WRITE_8BIT_REGx(i
->rm(), i
->extend8bitL(), sum
);
380 read_RMW_virtual_byte(i
->seg(), RMAddr(i
), &op1
);
382 write_RMW_virtual_byte(sum
);
385 SET_FLAGS_OSZAPC_8(op1
, op2
, sum
, BX_INSTR_ADD8
);
388 void BX_CPU_C::ADC_EbIb(bxInstruction_c
*i
)
391 bx_bool temp_CF
= getB_CF();
396 op1
= BX_READ_8BIT_REGx(i
->rm(),i
->extend8bitL());
397 sum
= op1
+ op2
+ temp_CF
;
398 BX_WRITE_8BIT_REGx(i
->rm(), i
->extend8bitL(), sum
);
401 read_RMW_virtual_byte(i
->seg(), RMAddr(i
), &op1
);
402 sum
= op1
+ op2
+ temp_CF
;
403 write_RMW_virtual_byte(sum
);
406 SET_FLAGS_OSZAPC_8(op1
, op2
, sum
, BX_INSTR_ADD_ADC8(temp_CF
));
409 void BX_CPU_C::SUB_EbIb(bxInstruction_c
*i
)
411 Bit8u op2_8
, op1_8
, diff_8
;
416 op1_8
= BX_READ_8BIT_REGx(i
->rm(),i
->extend8bitL());
417 diff_8
= op1_8
- op2_8
;
418 BX_WRITE_8BIT_REGx(i
->rm(), i
->extend8bitL(), diff_8
);
421 read_RMW_virtual_byte(i
->seg(), RMAddr(i
), &op1_8
);
422 diff_8
= op1_8
- op2_8
;
423 write_RMW_virtual_byte(diff_8
);
426 SET_FLAGS_OSZAPC_8(op1_8
, op2_8
, diff_8
, BX_INSTR_SUB8
);
429 void BX_CPU_C::CMP_EbIb(bxInstruction_c
*i
)
436 op1_8
= BX_READ_8BIT_REGx(i
->rm(),i
->extend8bitL());
439 read_virtual_byte(i
->seg(), RMAddr(i
), &op1_8
);
442 #if defined(BX_HostAsm_Cmp8)
444 asmCmp8(op1_8
, op2_8
, flags32
);
445 setEFlagsOSZAPC(flags32
);
447 Bit8u diff_8
= op1_8
- op2_8
;
448 SET_FLAGS_OSZAPC_8(op1_8
, op2_8
, diff_8
, BX_INSTR_COMPARE8
);
452 void BX_CPU_C::NEG_Eb(bxInstruction_c
*i
)
457 op1_8
= BX_READ_8BIT_REGx(i
->rm(),i
->extend8bitL());
459 BX_WRITE_8BIT_REGx(i
->rm(), i
->extend8bitL(), diff_8
);
462 read_RMW_virtual_byte(i
->seg(), RMAddr(i
), &op1_8
);
464 write_RMW_virtual_byte(diff_8
);
467 SET_FLAGS_OSZAPC_RESULT_8(diff_8
, BX_INSTR_NEG8
);
470 void BX_CPU_C::INC_Eb(bxInstruction_c
*i
)
475 op1
= BX_READ_8BIT_REGx(i
->rm(),i
->extend8bitL());
477 BX_WRITE_8BIT_REGx(i
->rm(), i
->extend8bitL(), op1
);
480 read_RMW_virtual_byte(i
->seg(), RMAddr(i
), &op1
);
482 write_RMW_virtual_byte(op1
);
485 SET_FLAGS_OSZAP_RESULT_8(op1
, BX_INSTR_INC8
);
488 void BX_CPU_C::DEC_Eb(bxInstruction_c
*i
)
493 op1_8
= BX_READ_8BIT_REGx(i
->rm(),i
->extend8bitL());
495 BX_WRITE_8BIT_REGx(i
->rm(), i
->extend8bitL(), op1_8
);
498 read_RMW_virtual_byte(i
->seg(), RMAddr(i
), &op1_8
);
500 write_RMW_virtual_byte(op1_8
);
503 SET_FLAGS_OSZAP_RESULT_8(op1_8
, BX_INSTR_DEC8
);
506 void BX_CPU_C::CMPXCHG_EbGb(bxInstruction_c
*i
)
508 #if (BX_CPU_LEVEL >= 4) || (BX_CPU_LEVEL_HACKED >= 4)
509 Bit8u op2_8
, op1_8
, diff_8
;
512 op1_8
= BX_READ_8BIT_REGx(i
->rm(),i
->extend8bitL());
515 read_RMW_virtual_byte(i
->seg(), RMAddr(i
), &op1_8
);
520 SET_FLAGS_OSZAPC_8(AL
, op1_8
, diff_8
, BX_INSTR_COMPARE8
);
522 if (diff_8
== 0) { // if accumulator == dest
524 op2_8
= BX_READ_8BIT_REGx(i
->nnn(),i
->extend8bitL());
527 BX_WRITE_8BIT_REGx(i
->rm(), i
->extend8bitL(), op2_8
);
530 write_RMW_virtual_byte(op2_8
);
534 // accumulator <-- dest
539 BX_INFO(("CMPXCHG_EbGb: not supported for cpulevel <= 3"));