]> git.proxmox.com Git - qemu.git/blame - op-i386.c
more ring 0 operations
[qemu.git] / op-i386.c
CommitLineData
7d13299d
FB
1/*
2 * i386 micro operations
3 *
4 * Copyright (c) 2003 Fabrice Bellard
5 *
3ef693a0
FB
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
7d13299d 10 *
3ef693a0
FB
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
7d13299d 15 *
3ef693a0
FB
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
7d13299d
FB
19 */
20#include "exec-i386.h"
7bfdb6d1 21
367e86e8
FB
22/* n must be a constant to be efficient */
23static inline int lshift(int x, int n)
7bfdb6d1 24{
367e86e8
FB
25 if (n >= 0)
26 return x << n;
27 else
28 return x >> (-n);
7bfdb6d1 29}
7bfdb6d1
FB
30
31/* we define the various pieces of code used by the JIT */
32
33#define REG EAX
34#define REGNAME _EAX
35#include "opreg_template.h"
36#undef REG
37#undef REGNAME
38
39#define REG ECX
40#define REGNAME _ECX
41#include "opreg_template.h"
42#undef REG
43#undef REGNAME
44
45#define REG EDX
46#define REGNAME _EDX
47#include "opreg_template.h"
48#undef REG
49#undef REGNAME
50
51#define REG EBX
52#define REGNAME _EBX
53#include "opreg_template.h"
54#undef REG
55#undef REGNAME
56
57#define REG ESP
58#define REGNAME _ESP
59#include "opreg_template.h"
60#undef REG
61#undef REGNAME
62
63#define REG EBP
64#define REGNAME _EBP
65#include "opreg_template.h"
66#undef REG
67#undef REGNAME
68
69#define REG ESI
70#define REGNAME _ESI
71#include "opreg_template.h"
72#undef REG
73#undef REGNAME
74
75#define REG EDI
76#define REGNAME _EDI
77#include "opreg_template.h"
78#undef REG
79#undef REGNAME
80
dc99065b 81/* operations with flags */
7bfdb6d1
FB
82
83void OPPROTO op_addl_T0_T1_cc(void)
84{
85 CC_SRC = T0;
86 T0 += T1;
87 CC_DST = T0;
88}
89
90void OPPROTO op_orl_T0_T1_cc(void)
91{
92 T0 |= T1;
93 CC_DST = T0;
94}
95
7bfdb6d1
FB
96void OPPROTO op_andl_T0_T1_cc(void)
97{
98 T0 &= T1;
99 CC_DST = T0;
100}
101
102void OPPROTO op_subl_T0_T1_cc(void)
103{
104 CC_SRC = T0;
105 T0 -= T1;
106 CC_DST = T0;
107}
108
109void OPPROTO op_xorl_T0_T1_cc(void)
110{
111 T0 ^= T1;
112 CC_DST = T0;
113}
114
115void OPPROTO op_cmpl_T0_T1_cc(void)
116{
117 CC_SRC = T0;
118 CC_DST = T0 - T1;
119}
120
7bfdb6d1
FB
121void OPPROTO op_negl_T0_cc(void)
122{
123 CC_SRC = 0;
124 T0 = -T0;
125 CC_DST = T0;
126}
127
128void OPPROTO op_incl_T0_cc(void)
129{
4b74fe1f 130 CC_SRC = cc_table[CC_OP].compute_c();
7bfdb6d1
FB
131 T0++;
132 CC_DST = T0;
133}
134
135void OPPROTO op_decl_T0_cc(void)
136{
4b74fe1f 137 CC_SRC = cc_table[CC_OP].compute_c();
7bfdb6d1
FB
138 T0--;
139 CC_DST = T0;
140}
141
142void OPPROTO op_testl_T0_T1_cc(void)
143{
7bfdb6d1
FB
144 CC_DST = T0 & T1;
145}
146
dc99065b
FB
147/* operations without flags */
148
149void OPPROTO op_addl_T0_T1(void)
150{
151 T0 += T1;
152}
153
154void OPPROTO op_orl_T0_T1(void)
155{
156 T0 |= T1;
157}
158
159void OPPROTO op_andl_T0_T1(void)
160{
161 T0 &= T1;
162}
163
164void OPPROTO op_subl_T0_T1(void)
165{
166 T0 -= T1;
167}
168
169void OPPROTO op_xorl_T0_T1(void)
170{
171 T0 ^= T1;
172}
173
174void OPPROTO op_negl_T0(void)
175{
176 T0 = -T0;
177}
178
179void OPPROTO op_incl_T0(void)
180{
181 T0++;
182}
183
184void OPPROTO op_decl_T0(void)
185{
186 T0--;
187}
188
189void OPPROTO op_notl_T0(void)
190{
191 T0 = ~T0;
192}
193
4b74fe1f
FB
194void OPPROTO op_bswapl_T0(void)
195{
196 T0 = bswap32(T0);
197}
198
7bfdb6d1
FB
199/* multiply/divide */
200void OPPROTO op_mulb_AL_T0(void)
201{
202 unsigned int res;
203 res = (uint8_t)EAX * (uint8_t)T0;
204 EAX = (EAX & 0xffff0000) | res;
205 CC_SRC = (res & 0xff00);
206}
207
208void OPPROTO op_imulb_AL_T0(void)
209{
210 int res;
211 res = (int8_t)EAX * (int8_t)T0;
212 EAX = (EAX & 0xffff0000) | (res & 0xffff);
213 CC_SRC = (res != (int8_t)res);
214}
215
216void OPPROTO op_mulw_AX_T0(void)
217{
218 unsigned int res;
219 res = (uint16_t)EAX * (uint16_t)T0;
220 EAX = (EAX & 0xffff0000) | (res & 0xffff);
221 EDX = (EDX & 0xffff0000) | ((res >> 16) & 0xffff);
222 CC_SRC = res >> 16;
223}
224
225void OPPROTO op_imulw_AX_T0(void)
226{
227 int res;
228 res = (int16_t)EAX * (int16_t)T0;
229 EAX = (EAX & 0xffff0000) | (res & 0xffff);
230 EDX = (EDX & 0xffff0000) | ((res >> 16) & 0xffff);
231 CC_SRC = (res != (int16_t)res);
232}
233
234void OPPROTO op_mull_EAX_T0(void)
235{
236 uint64_t res;
237 res = (uint64_t)((uint32_t)EAX) * (uint64_t)((uint32_t)T0);
238 EAX = res;
239 EDX = res >> 32;
240 CC_SRC = res >> 32;
241}
242
243void OPPROTO op_imull_EAX_T0(void)
244{
245 int64_t res;
246 res = (int64_t)((int32_t)EAX) * (int64_t)((int32_t)T0);
247 EAX = res;
248 EDX = res >> 32;
249 CC_SRC = (res != (int32_t)res);
250}
251
252void OPPROTO op_imulw_T0_T1(void)
253{
254 int res;
255 res = (int16_t)T0 * (int16_t)T1;
256 T0 = res;
257 CC_SRC = (res != (int16_t)res);
258}
259
260void OPPROTO op_imull_T0_T1(void)
261{
262 int64_t res;
4b74fe1f 263 res = (int64_t)((int32_t)T0) * (int64_t)((int32_t)T1);
7bfdb6d1
FB
264 T0 = res;
265 CC_SRC = (res != (int32_t)res);
266}
267
268/* division, flags are undefined */
9de5e440 269/* XXX: add exceptions for overflow */
87f4827e 270
7bfdb6d1
FB
271void OPPROTO op_divb_AL_T0(void)
272{
273 unsigned int num, den, q, r;
274
275 num = (EAX & 0xffff);
276 den = (T0 & 0xff);
f4beb510
FB
277 if (den == 0) {
278 EIP = PARAM1;
9de5e440 279 raise_exception(EXCP00_DIVZ);
f4beb510 280 }
7bfdb6d1
FB
281 q = (num / den) & 0xff;
282 r = (num % den) & 0xff;
283 EAX = (EAX & 0xffff0000) | (r << 8) | q;
284}
285
286void OPPROTO op_idivb_AL_T0(void)
287{
288 int num, den, q, r;
289
290 num = (int16_t)EAX;
291 den = (int8_t)T0;
f4beb510
FB
292 if (den == 0) {
293 EIP = PARAM1;
9de5e440 294 raise_exception(EXCP00_DIVZ);
f4beb510 295 }
7bfdb6d1
FB
296 q = (num / den) & 0xff;
297 r = (num % den) & 0xff;
298 EAX = (EAX & 0xffff0000) | (r << 8) | q;
299}
300
301void OPPROTO op_divw_AX_T0(void)
302{
303 unsigned int num, den, q, r;
304
305 num = (EAX & 0xffff) | ((EDX & 0xffff) << 16);
306 den = (T0 & 0xffff);
f4beb510
FB
307 if (den == 0) {
308 EIP = PARAM1;
9de5e440 309 raise_exception(EXCP00_DIVZ);
f4beb510 310 }
7bfdb6d1
FB
311 q = (num / den) & 0xffff;
312 r = (num % den) & 0xffff;
313 EAX = (EAX & 0xffff0000) | q;
314 EDX = (EDX & 0xffff0000) | r;
315}
316
317void OPPROTO op_idivw_AX_T0(void)
318{
319 int num, den, q, r;
320
321 num = (EAX & 0xffff) | ((EDX & 0xffff) << 16);
322 den = (int16_t)T0;
f4beb510
FB
323 if (den == 0) {
324 EIP = PARAM1;
9de5e440 325 raise_exception(EXCP00_DIVZ);
f4beb510 326 }
7bfdb6d1
FB
327 q = (num / den) & 0xffff;
328 r = (num % den) & 0xffff;
329 EAX = (EAX & 0xffff0000) | q;
330 EDX = (EDX & 0xffff0000) | r;
331}
332
333void OPPROTO op_divl_EAX_T0(void)
334{
87f4827e 335 helper_divl_EAX_T0(PARAM1);
7bfdb6d1
FB
336}
337
338void OPPROTO op_idivl_EAX_T0(void)
339{
87f4827e 340 helper_idivl_EAX_T0(PARAM1);
7bfdb6d1
FB
341}
342
dab2ed99 343/* constant load & misc op */
7bfdb6d1 344
ba1c6e37 345void OPPROTO op_movl_T0_im(void)
7bfdb6d1
FB
346{
347 T0 = PARAM1;
348}
349
dab2ed99
FB
350void OPPROTO op_addl_T0_im(void)
351{
352 T0 += PARAM1;
353}
354
355void OPPROTO op_andl_T0_ffff(void)
356{
357 T0 = T0 & 0xffff;
358}
359
d8bc1fd0
FB
360void OPPROTO op_andl_T0_im(void)
361{
362 T0 = T0 & PARAM1;
363}
364
dab2ed99
FB
365void OPPROTO op_movl_T0_T1(void)
366{
367 T0 = T1;
368}
369
ba1c6e37 370void OPPROTO op_movl_T1_im(void)
7bfdb6d1
FB
371{
372 T1 = PARAM1;
373}
374
dab2ed99
FB
375void OPPROTO op_addl_T1_im(void)
376{
377 T1 += PARAM1;
378}
379
380void OPPROTO op_movl_T1_A0(void)
381{
382 T1 = A0;
383}
384
ba1c6e37 385void OPPROTO op_movl_A0_im(void)
7bfdb6d1
FB
386{
387 A0 = PARAM1;
388}
389
4b74fe1f
FB
390void OPPROTO op_addl_A0_im(void)
391{
392 A0 += PARAM1;
393}
394
31bb950b
FB
395void OPPROTO op_addl_A0_AL(void)
396{
397 A0 += (EAX & 0xff);
398}
399
4b74fe1f
FB
400void OPPROTO op_andl_A0_ffff(void)
401{
402 A0 = A0 & 0xffff;
403}
404
7bfdb6d1
FB
405/* memory access */
406
407void OPPROTO op_ldub_T0_A0(void)
408{
409 T0 = ldub((uint8_t *)A0);
410}
411
412void OPPROTO op_ldsb_T0_A0(void)
413{
414 T0 = ldsb((int8_t *)A0);
415}
416
417void OPPROTO op_lduw_T0_A0(void)
418{
419 T0 = lduw((uint8_t *)A0);
420}
421
422void OPPROTO op_ldsw_T0_A0(void)
423{
424 T0 = ldsw((int8_t *)A0);
425}
426
427void OPPROTO op_ldl_T0_A0(void)
428{
429 T0 = ldl((uint8_t *)A0);
430}
431
432void OPPROTO op_ldub_T1_A0(void)
433{
434 T1 = ldub((uint8_t *)A0);
435}
436
437void OPPROTO op_ldsb_T1_A0(void)
438{
439 T1 = ldsb((int8_t *)A0);
440}
441
442void OPPROTO op_lduw_T1_A0(void)
443{
444 T1 = lduw((uint8_t *)A0);
445}
446
447void OPPROTO op_ldsw_T1_A0(void)
448{
449 T1 = ldsw((int8_t *)A0);
450}
451
452void OPPROTO op_ldl_T1_A0(void)
453{
454 T1 = ldl((uint8_t *)A0);
455}
456
457void OPPROTO op_stb_T0_A0(void)
458{
459 stb((uint8_t *)A0, T0);
460}
461
462void OPPROTO op_stw_T0_A0(void)
463{
464 stw((uint8_t *)A0, T0);
465}
466
467void OPPROTO op_stl_T0_A0(void)
468{
469 stl((uint8_t *)A0, T0);
470}
471
4b74fe1f
FB
472/* used for bit operations */
473
474void OPPROTO op_add_bitw_A0_T1(void)
475{
476 A0 += ((int32_t)T1 >> 4) << 1;
477}
478
479void OPPROTO op_add_bitl_A0_T1(void)
480{
481 A0 += ((int32_t)T1 >> 5) << 2;
482}
7bfdb6d1
FB
483
484/* indirect jump */
0ecfa993 485
7bfdb6d1
FB
486void OPPROTO op_jmp_T0(void)
487{
dab2ed99 488 EIP = T0;
7bfdb6d1
FB
489}
490
491void OPPROTO op_jmp_im(void)
492{
dab2ed99 493 EIP = PARAM1;
7bfdb6d1
FB
494}
495
90a9fdae
FB
496void OPPROTO op_hlt(void)
497{
498 env->exception_index = EXCP_HLT;
499 cpu_loop_exit();
500}
501
f4beb510 502void OPPROTO op_raise_interrupt(void)
0ecfa993 503{
504e56eb 504 int intno;
f4beb510 505 unsigned int next_eip;
504e56eb 506 intno = PARAM1;
f4beb510
FB
507 next_eip = PARAM2;
508 raise_interrupt(intno, 1, 0, next_eip);
0ecfa993
FB
509}
510
564c8f99 511void OPPROTO op_raise_exception(void)
0ecfa993 512{
564c8f99
FB
513 int exception_index;
514 exception_index = PARAM1;
515 raise_exception(exception_index);
0ecfa993
FB
516}
517
518void OPPROTO op_into(void)
519{
520 int eflags;
521 eflags = cc_table[CC_OP].compute_all();
522 if (eflags & CC_O) {
f4beb510 523 raise_interrupt(EXCP04_INTO, 1, 0, PARAM1);
a4a0ffdb 524 }
504e56eb 525 FORCE_RET();
a4a0ffdb
FB
526}
527
504e56eb
FB
528void OPPROTO op_cli(void)
529{
530 env->eflags &= ~IF_MASK;
531}
532
f631ef9b
FB
533void OPPROTO op_sti(void)
534{
504e56eb 535 env->eflags |= IF_MASK;
f631ef9b
FB
536}
537
3acace13 538#if 0
f631ef9b 539/* vm86plus instructions */
f631ef9b
FB
540void OPPROTO op_cli_vm(void)
541{
542 env->eflags &= ~VIF_MASK;
543}
544
545void OPPROTO op_sti_vm(void)
546{
547 env->eflags |= VIF_MASK;
548 if (env->eflags & VIP_MASK) {
549 EIP = PARAM1;
550 raise_exception(EXCP0D_GPF);
551 }
552 FORCE_RET();
553}
3acace13 554#endif
f631ef9b 555
a4a0ffdb
FB
556void OPPROTO op_boundw(void)
557{
558 int low, high, v;
559 low = ldsw((uint8_t *)A0);
560 high = ldsw((uint8_t *)A0 + 2);
561 v = (int16_t)T0;
f4beb510
FB
562 if (v < low || v > high) {
563 EIP = PARAM1;
a4a0ffdb 564 raise_exception(EXCP05_BOUND);
f4beb510 565 }
a4a0ffdb
FB
566 FORCE_RET();
567}
568
569void OPPROTO op_boundl(void)
570{
571 int low, high, v;
572 low = ldl((uint8_t *)A0);
573 high = ldl((uint8_t *)A0 + 4);
574 v = T0;
f4beb510
FB
575 if (v < low || v > high) {
576 EIP = PARAM1;
a4a0ffdb 577 raise_exception(EXCP05_BOUND);
f4beb510 578 }
a4a0ffdb
FB
579 FORCE_RET();
580}
581
582void OPPROTO op_cmpxchg8b(void)
583{
87f4827e 584 helper_cmpxchg8b();
0ecfa993
FB
585}
586
d4e8164f
FB
587void OPPROTO op_jmp_tb_next(void)
588{
589 JUMP_TB(PARAM1, 0, PARAM2);
590}
591
592void OPPROTO op_movl_T0_0(void)
593{
594 T0 = 0;
595}
596
597/* multiple size ops */
7bfdb6d1
FB
598
599#define ldul ldl
600
7bfdb6d1 601#define SHIFT 0
367e86e8 602#include "ops_template.h"
7bfdb6d1
FB
603#undef SHIFT
604
7bfdb6d1 605#define SHIFT 1
367e86e8 606#include "ops_template.h"
7bfdb6d1
FB
607#undef SHIFT
608
7bfdb6d1 609#define SHIFT 2
367e86e8 610#include "ops_template.h"
7bfdb6d1
FB
611#undef SHIFT
612
613/* sign extend */
614
615void OPPROTO op_movsbl_T0_T0(void)
616{
617 T0 = (int8_t)T0;
618}
619
620void OPPROTO op_movzbl_T0_T0(void)
621{
622 T0 = (uint8_t)T0;
623}
624
625void OPPROTO op_movswl_T0_T0(void)
626{
627 T0 = (int16_t)T0;
628}
629
630void OPPROTO op_movzwl_T0_T0(void)
631{
632 T0 = (uint16_t)T0;
633}
634
635void OPPROTO op_movswl_EAX_AX(void)
636{
637 EAX = (int16_t)EAX;
638}
639
640void OPPROTO op_movsbw_AX_AL(void)
641{
642 EAX = (EAX & 0xffff0000) | ((int8_t)EAX & 0xffff);
643}
644
645void OPPROTO op_movslq_EDX_EAX(void)
646{
647 EDX = (int32_t)EAX >> 31;
648}
649
650void OPPROTO op_movswl_DX_AX(void)
651{
652 EDX = (EDX & 0xffff0000) | (((int16_t)EAX >> 15) & 0xffff);
653}
654
655/* push/pop */
7bfdb6d1
FB
656
657void op_pushl_T0(void)
658{
659 uint32_t offset;
660 offset = ESP - 4;
661 stl((void *)offset, T0);
662 /* modify ESP after to handle exceptions correctly */
663 ESP = offset;
664}
665
dab2ed99
FB
666void op_pushw_T0(void)
667{
668 uint32_t offset;
669 offset = ESP - 2;
670 stw((void *)offset, T0);
671 /* modify ESP after to handle exceptions correctly */
672 ESP = offset;
673}
674
675void op_pushl_ss32_T0(void)
7bfdb6d1
FB
676{
677 uint32_t offset;
678 offset = ESP - 4;
d8bc1fd0 679 stl(env->segs[R_SS].base + offset, T0);
dab2ed99
FB
680 /* modify ESP after to handle exceptions correctly */
681 ESP = offset;
682}
683
684void op_pushw_ss32_T0(void)
685{
686 uint32_t offset;
687 offset = ESP - 2;
d8bc1fd0 688 stw(env->segs[R_SS].base + offset, T0);
7bfdb6d1
FB
689 /* modify ESP after to handle exceptions correctly */
690 ESP = offset;
691}
692
dab2ed99
FB
693void op_pushl_ss16_T0(void)
694{
695 uint32_t offset;
696 offset = (ESP - 4) & 0xffff;
d8bc1fd0 697 stl(env->segs[R_SS].base + offset, T0);
dab2ed99
FB
698 /* modify ESP after to handle exceptions correctly */
699 ESP = (ESP & ~0xffff) | offset;
700}
701
702void op_pushw_ss16_T0(void)
703{
704 uint32_t offset;
705 offset = (ESP - 2) & 0xffff;
d8bc1fd0 706 stw(env->segs[R_SS].base + offset, T0);
dab2ed99
FB
707 /* modify ESP after to handle exceptions correctly */
708 ESP = (ESP & ~0xffff) | offset;
709}
710
711/* NOTE: ESP update is done after */
7bfdb6d1
FB
712void op_popl_T0(void)
713{
714 T0 = ldl((void *)ESP);
dab2ed99
FB
715}
716
717void op_popw_T0(void)
718{
719 T0 = lduw((void *)ESP);
720}
721
722void op_popl_ss32_T0(void)
723{
d8bc1fd0 724 T0 = ldl(env->segs[R_SS].base + ESP);
dab2ed99
FB
725}
726
727void op_popw_ss32_T0(void)
728{
d8bc1fd0 729 T0 = lduw(env->segs[R_SS].base + ESP);
dab2ed99
FB
730}
731
732void op_popl_ss16_T0(void)
733{
d8bc1fd0 734 T0 = ldl(env->segs[R_SS].base + (ESP & 0xffff));
dab2ed99
FB
735}
736
737void op_popw_ss16_T0(void)
738{
d8bc1fd0 739 T0 = lduw(env->segs[R_SS].base + (ESP & 0xffff));
dab2ed99
FB
740}
741
742void op_addl_ESP_4(void)
743{
7bfdb6d1
FB
744 ESP += 4;
745}
746
dab2ed99
FB
747void op_addl_ESP_2(void)
748{
749 ESP += 2;
750}
751
752void op_addw_ESP_4(void)
753{
754 ESP = (ESP & ~0xffff) | ((ESP + 4) & 0xffff);
755}
756
757void op_addw_ESP_2(void)
758{
759 ESP = (ESP & ~0xffff) | ((ESP + 2) & 0xffff);
760}
761
7bfdb6d1
FB
762void op_addl_ESP_im(void)
763{
764 ESP += PARAM1;
765}
367e86e8 766
dab2ed99
FB
767void op_addw_ESP_im(void)
768{
769 ESP = (ESP & ~0xffff) | ((ESP + PARAM1) & 0xffff);
27362c82
FB
770}
771
a4a0ffdb 772void OPPROTO op_rdtsc(void)
27362c82 773{
87f4827e 774 helper_rdtsc();
27362c82
FB
775}
776
a4a0ffdb
FB
777void OPPROTO op_cpuid(void)
778{
779 helper_cpuid();
780}
781
27362c82
FB
782/* bcd */
783
784/* XXX: exception */
785void OPPROTO op_aam(void)
786{
787 int base = PARAM1;
788 int al, ah;
789 al = EAX & 0xff;
790 ah = al / base;
791 al = al % base;
792 EAX = (EAX & ~0xffff) | al | (ah << 8);
793 CC_DST = al;
794}
795
796void OPPROTO op_aad(void)
797{
798 int base = PARAM1;
799 int al, ah;
800 al = EAX & 0xff;
801 ah = (EAX >> 8) & 0xff;
802 al = ((ah * base) + al) & 0xff;
803 EAX = (EAX & ~0xffff) | al;
804 CC_DST = al;
805}
806
807void OPPROTO op_aaa(void)
808{
809 int icarry;
810 int al, ah, af;
811 int eflags;
812
813 eflags = cc_table[CC_OP].compute_all();
814 af = eflags & CC_A;
815 al = EAX & 0xff;
816 ah = (EAX >> 8) & 0xff;
817
818 icarry = (al > 0xf9);
819 if (((al & 0x0f) > 9 ) || af) {
820 al = (al + 6) & 0x0f;
821 ah = (ah + 1 + icarry) & 0xff;
822 eflags |= CC_C | CC_A;
823 } else {
824 eflags &= ~(CC_C | CC_A);
825 al &= 0x0f;
826 }
827 EAX = (EAX & ~0xffff) | al | (ah << 8);
828 CC_SRC = eflags;
829}
830
831void OPPROTO op_aas(void)
832{
833 int icarry;
834 int al, ah, af;
835 int eflags;
836
837 eflags = cc_table[CC_OP].compute_all();
838 af = eflags & CC_A;
839 al = EAX & 0xff;
840 ah = (EAX >> 8) & 0xff;
841
842 icarry = (al < 6);
843 if (((al & 0x0f) > 9 ) || af) {
844 al = (al - 6) & 0x0f;
845 ah = (ah - 1 - icarry) & 0xff;
846 eflags |= CC_C | CC_A;
847 } else {
848 eflags &= ~(CC_C | CC_A);
849 al &= 0x0f;
850 }
851 EAX = (EAX & ~0xffff) | al | (ah << 8);
852 CC_SRC = eflags;
853}
854
855void OPPROTO op_daa(void)
856{
857 int al, af, cf;
858 int eflags;
859
860 eflags = cc_table[CC_OP].compute_all();
861 cf = eflags & CC_C;
862 af = eflags & CC_A;
863 al = EAX & 0xff;
864
865 eflags = 0;
866 if (((al & 0x0f) > 9 ) || af) {
867 al = (al + 6) & 0xff;
868 eflags |= CC_A;
869 }
870 if ((al > 0x9f) || cf) {
871 al = (al + 0x60) & 0xff;
872 eflags |= CC_C;
873 }
874 EAX = (EAX & ~0xff) | al;
875 /* well, speed is not an issue here, so we compute the flags by hand */
876 eflags |= (al == 0) << 6; /* zf */
877 eflags |= parity_table[al]; /* pf */
878 eflags |= (al & 0x80); /* sf */
879 CC_SRC = eflags;
880}
881
882void OPPROTO op_das(void)
883{
884 int al, al1, af, cf;
885 int eflags;
886
887 eflags = cc_table[CC_OP].compute_all();
888 cf = eflags & CC_C;
889 af = eflags & CC_A;
890 al = EAX & 0xff;
891
892 eflags = 0;
893 al1 = al;
894 if (((al & 0x0f) > 9 ) || af) {
895 eflags |= CC_A;
896 if (al < 6 || cf)
897 eflags |= CC_C;
898 al = (al - 6) & 0xff;
899 }
900 if ((al1 > 0x99) || cf) {
901 al = (al - 0x60) & 0xff;
902 eflags |= CC_C;
903 }
904 EAX = (EAX & ~0xff) | al;
905 /* well, speed is not an issue here, so we compute the flags by hand */
906 eflags |= (al == 0) << 6; /* zf */
907 eflags |= parity_table[al]; /* pf */
908 eflags |= (al & 0x80); /* sf */
909 CC_SRC = eflags;
910}
911
6dbad63e
FB
912/* segment handling */
913
6dbad63e
FB
914void OPPROTO op_movl_seg_T0(void)
915{
f4beb510
FB
916 load_seg(PARAM1, T0 & 0xffff, PARAM2);
917}
918
919/* faster VM86 version */
920void OPPROTO op_movl_seg_T0_vm(void)
921{
922 int selector;
d8bc1fd0 923 SegmentCache *sc;
f4beb510
FB
924
925 selector = T0 & 0xffff;
926 /* env->segs[] access */
d8bc1fd0
FB
927 sc = (SegmentCache *)((char *)env + PARAM1);
928 sc->selector = selector;
929 sc->base = (void *)(selector << 4);
6dbad63e
FB
930}
931
932void OPPROTO op_movl_T0_seg(void)
933{
d8bc1fd0 934 T0 = env->segs[PARAM1].selector;
6dbad63e
FB
935}
936
a4a0ffdb
FB
937void OPPROTO op_movl_A0_seg(void)
938{
939 A0 = *(unsigned long *)((char *)env + PARAM1);
940}
941
6dbad63e
FB
942void OPPROTO op_addl_A0_seg(void)
943{
944 A0 += *(unsigned long *)((char *)env + PARAM1);
945}
946
2792c4f2
FB
947void OPPROTO op_lsl(void)
948{
949 helper_lsl();
950}
951
2792c4f2
FB
952void OPPROTO op_lar(void)
953{
954 helper_lar();
955}
956
d8bc1fd0
FB
957/* T0: segment, T1:eip */
958void OPPROTO op_ljmp_T0_T1(void)
959{
960 jmp_seg(T0 & 0xffff, T1);
961}
962
90a9fdae
FB
963void OPPROTO op_iret_protected(void)
964{
965 helper_iret_protected(PARAM1);
966}
967
d8bc1fd0
FB
968void OPPROTO op_lldt_T0(void)
969{
970 helper_lldt_T0();
971}
972
973void OPPROTO op_ltr_T0(void)
974{
975 helper_ltr_T0();
976}
977
978/* CR registers access */
979void OPPROTO op_movl_crN_T0(void)
980{
981 helper_movl_crN_T0(PARAM1);
982}
983
984/* DR registers access */
985void OPPROTO op_movl_drN_T0(void)
986{
987 helper_movl_drN_T0(PARAM1);
988}
989
990void OPPROTO op_lmsw_T0(void)
991{
992 /* only 4 lower bits of CR0 are modified */
993 T0 = (env->cr[0] & ~0xf) | (T0 & 0xf);
994 helper_movl_crN_T0(0);
995}
996
90a9fdae
FB
997void OPPROTO op_invlpg_A0(void)
998{
999 helper_invlpg(A0);
1000}
1001
d8bc1fd0
FB
1002void OPPROTO op_movl_T0_env(void)
1003{
1004 T0 = *(uint32_t *)((char *)env + PARAM1);
1005}
1006
1007void OPPROTO op_movl_env_T0(void)
1008{
1009 *(uint32_t *)((char *)env + PARAM1) = T0;
1010}
1011
1012void OPPROTO op_movl_env_T1(void)
1013{
1014 *(uint32_t *)((char *)env + PARAM1) = T1;
1015}
1016
1017void OPPROTO op_clts(void)
1018{
1019 env->cr[0] &= ~CR0_TS_MASK;
1020}
1021
367e86e8
FB
1022/* flags handling */
1023
d4e8164f
FB
1024/* slow jumps cases : in order to avoid calling a function with a
1025 pointer (which can generate a stack frame on PowerPC), we use
1026 op_setcc to set T0 and then call op_jcc. */
1027void OPPROTO op_jcc(void)
367e86e8 1028{
d4e8164f
FB
1029 if (T0)
1030 JUMP_TB(PARAM1, 0, PARAM2);
367e86e8 1031 else
d4e8164f 1032 JUMP_TB(PARAM1, 1, PARAM3);
0ecfa993 1033 FORCE_RET();
367e86e8
FB
1034}
1035
1036/* slow set cases (compute x86 flags) */
1037void OPPROTO op_seto_T0_cc(void)
1038{
1039 int eflags;
1040 eflags = cc_table[CC_OP].compute_all();
1041 T0 = (eflags >> 11) & 1;
1042}
1043
1044void OPPROTO op_setb_T0_cc(void)
1045{
1046 T0 = cc_table[CC_OP].compute_c();
1047}
1048
1049void OPPROTO op_setz_T0_cc(void)
1050{
1051 int eflags;
1052 eflags = cc_table[CC_OP].compute_all();
1053 T0 = (eflags >> 6) & 1;
1054}
1055
1056void OPPROTO op_setbe_T0_cc(void)
1057{
1058 int eflags;
1059 eflags = cc_table[CC_OP].compute_all();
1060 T0 = (eflags & (CC_Z | CC_C)) != 0;
1061}
1062
1063void OPPROTO op_sets_T0_cc(void)
1064{
1065 int eflags;
1066 eflags = cc_table[CC_OP].compute_all();
1067 T0 = (eflags >> 7) & 1;
1068}
1069
1070void OPPROTO op_setp_T0_cc(void)
1071{
1072 int eflags;
1073 eflags = cc_table[CC_OP].compute_all();
1074 T0 = (eflags >> 2) & 1;
1075}
1076
1077void OPPROTO op_setl_T0_cc(void)
1078{
1079 int eflags;
1080 eflags = cc_table[CC_OP].compute_all();
1081 T0 = ((eflags ^ (eflags >> 4)) >> 7) & 1;
1082}
1083
1084void OPPROTO op_setle_T0_cc(void)
1085{
1086 int eflags;
1087 eflags = cc_table[CC_OP].compute_all();
1088 T0 = (((eflags ^ (eflags >> 4)) & 0x80) || (eflags & CC_Z)) != 0;
1089}
1090
1091void OPPROTO op_xor_T0_1(void)
1092{
1093 T0 ^= 1;
1094}
1095
1096void OPPROTO op_set_cc_op(void)
1097{
1098 CC_OP = PARAM1;
1099}
1100
90a9fdae 1101#define FL_UPDATE_MASK16 (FL_UPDATE_MASK32 & 0xffff)
a4a0ffdb 1102
367e86e8
FB
1103void OPPROTO op_movl_eflags_T0(void)
1104{
a4a0ffdb
FB
1105 int eflags;
1106 eflags = T0;
1107 CC_SRC = eflags & (CC_O | CC_S | CC_Z | CC_A | CC_P | CC_C);
1108 DF = 1 - (2 * ((eflags >> 10) & 1));
1109 /* we also update some system flags as in user mode */
90a9fdae
FB
1110 env->eflags = (env->eflags & ~FL_UPDATE_MASK32) |
1111 (eflags & FL_UPDATE_MASK32);
f631ef9b
FB
1112}
1113
1114void OPPROTO op_movw_eflags_T0(void)
1115{
1116 int eflags;
1117 eflags = T0;
1118 CC_SRC = eflags & (CC_O | CC_S | CC_Z | CC_A | CC_P | CC_C);
1119 DF = 1 - (2 * ((eflags >> 10) & 1));
1120 /* we also update some system flags as in user mode */
90a9fdae
FB
1121 env->eflags = (env->eflags & ~FL_UPDATE_MASK16) |
1122 (eflags & FL_UPDATE_MASK16);
1123}
1124
1125void OPPROTO op_movl_eflags_T0_cpl0(void)
1126{
1127 load_eflags(T0, FL_UPDATE_CPL0_MASK);
1128}
1129
1130void OPPROTO op_movw_eflags_T0_cpl0(void)
1131{
1132 load_eflags(T0, FL_UPDATE_CPL0_MASK & 0xffff);
f631ef9b
FB
1133}
1134
3acace13
FB
1135#if 0
1136/* vm86plus version */
f631ef9b
FB
1137void OPPROTO op_movw_eflags_T0_vm(void)
1138{
1139 int eflags;
1140 eflags = T0;
1141 CC_SRC = eflags & (CC_O | CC_S | CC_Z | CC_A | CC_P | CC_C);
1142 DF = 1 - (2 * ((eflags >> 10) & 1));
1143 /* we also update some system flags as in user mode */
1144 env->eflags = (env->eflags & ~(FL_UPDATE_MASK16 | VIF_MASK)) |
1145 (eflags & FL_UPDATE_MASK16);
1146 if (eflags & IF_MASK) {
1147 env->eflags |= VIF_MASK;
1148 if (env->eflags & VIP_MASK) {
1149 EIP = PARAM1;
1150 raise_exception(EXCP0D_GPF);
1151 }
1152 }
1153 FORCE_RET();
1154}
1155
1156void OPPROTO op_movl_eflags_T0_vm(void)
1157{
1158 int eflags;
1159 eflags = T0;
1160 CC_SRC = eflags & (CC_O | CC_S | CC_Z | CC_A | CC_P | CC_C);
1161 DF = 1 - (2 * ((eflags >> 10) & 1));
1162 /* we also update some system flags as in user mode */
1163 env->eflags = (env->eflags & ~(FL_UPDATE_MASK32 | VIF_MASK)) |
1164 (eflags & FL_UPDATE_MASK32);
1165 if (eflags & IF_MASK) {
1166 env->eflags |= VIF_MASK;
1167 if (env->eflags & VIP_MASK) {
1168 EIP = PARAM1;
1169 raise_exception(EXCP0D_GPF);
1170 }
1171 }
1172 FORCE_RET();
367e86e8 1173}
3acace13 1174#endif
367e86e8
FB
1175
1176/* XXX: compute only O flag */
1177void OPPROTO op_movb_eflags_T0(void)
1178{
1179 int of;
1180 of = cc_table[CC_OP].compute_all() & CC_O;
a4a0ffdb 1181 CC_SRC = (T0 & (CC_S | CC_Z | CC_A | CC_P | CC_C)) | of;
367e86e8
FB
1182}
1183
1184void OPPROTO op_movl_T0_eflags(void)
1185{
a4a0ffdb
FB
1186 int eflags;
1187 eflags = cc_table[CC_OP].compute_all();
1188 eflags |= (DF & DF_MASK);
1189 eflags |= env->eflags & ~(VM_MASK | RF_MASK);
1190 T0 = eflags;
367e86e8
FB
1191}
1192
3acace13
FB
1193/* vm86plus version */
1194#if 0
f631ef9b
FB
1195void OPPROTO op_movl_T0_eflags_vm(void)
1196{
1197 int eflags;
1198 eflags = cc_table[CC_OP].compute_all();
1199 eflags |= (DF & DF_MASK);
1200 eflags |= env->eflags & ~(VM_MASK | RF_MASK | IF_MASK);
1201 if (env->eflags & VIF_MASK)
1202 eflags |= IF_MASK;
1203 T0 = eflags;
1204}
3acace13 1205#endif
f631ef9b 1206
367e86e8
FB
1207void OPPROTO op_cld(void)
1208{
1209 DF = 1;
1210}
1211
1212void OPPROTO op_std(void)
1213{
1214 DF = -1;
1215}
1216
1217void OPPROTO op_clc(void)
1218{
1219 int eflags;
1220 eflags = cc_table[CC_OP].compute_all();
1221 eflags &= ~CC_C;
1222 CC_SRC = eflags;
1223}
1224
1225void OPPROTO op_stc(void)
1226{
1227 int eflags;
1228 eflags = cc_table[CC_OP].compute_all();
1229 eflags |= CC_C;
1230 CC_SRC = eflags;
1231}
1232
1233void OPPROTO op_cmc(void)
1234{
1235 int eflags;
1236 eflags = cc_table[CC_OP].compute_all();
1237 eflags ^= CC_C;
1238 CC_SRC = eflags;
1239}
1240
27362c82
FB
1241void OPPROTO op_salc(void)
1242{
1243 int cf;
1244 cf = cc_table[CC_OP].compute_c();
1245 EAX = (EAX & ~0xff) | ((-cf) & 0xff);
1246}
1247
367e86e8
FB
1248static int compute_all_eflags(void)
1249{
1250 return CC_SRC;
1251}
1252
1253static int compute_c_eflags(void)
1254{
1255 return CC_SRC & CC_C;
1256}
1257
1258static int compute_c_mul(void)
1259{
1260 int cf;
1261 cf = (CC_SRC != 0);
1262 return cf;
1263}
1264
1265static int compute_all_mul(void)
1266{
1267 int cf, pf, af, zf, sf, of;
1268 cf = (CC_SRC != 0);
1269 pf = 0; /* undefined */
1270 af = 0; /* undefined */
1271 zf = 0; /* undefined */
1272 sf = 0; /* undefined */
1273 of = cf << 11;
1274 return cf | pf | af | zf | sf | of;
1275}
1276
1277CCTable cc_table[CC_OP_NB] = {
1278 [CC_OP_DYNAMIC] = { /* should never happen */ },
1279
1280 [CC_OP_EFLAGS] = { compute_all_eflags, compute_c_eflags },
1281
1282 [CC_OP_MUL] = { compute_all_mul, compute_c_mul },
1283
1284 [CC_OP_ADDB] = { compute_all_addb, compute_c_addb },
1285 [CC_OP_ADDW] = { compute_all_addw, compute_c_addw },
1286 [CC_OP_ADDL] = { compute_all_addl, compute_c_addl },
1287
4b74fe1f
FB
1288 [CC_OP_ADCB] = { compute_all_adcb, compute_c_adcb },
1289 [CC_OP_ADCW] = { compute_all_adcw, compute_c_adcw },
1290 [CC_OP_ADCL] = { compute_all_adcl, compute_c_adcl },
1291
367e86e8
FB
1292 [CC_OP_SUBB] = { compute_all_subb, compute_c_subb },
1293 [CC_OP_SUBW] = { compute_all_subw, compute_c_subw },
1294 [CC_OP_SUBL] = { compute_all_subl, compute_c_subl },
1295
4b74fe1f
FB
1296 [CC_OP_SBBB] = { compute_all_sbbb, compute_c_sbbb },
1297 [CC_OP_SBBW] = { compute_all_sbbw, compute_c_sbbw },
1298 [CC_OP_SBBL] = { compute_all_sbbl, compute_c_sbbl },
1299
367e86e8
FB
1300 [CC_OP_LOGICB] = { compute_all_logicb, compute_c_logicb },
1301 [CC_OP_LOGICW] = { compute_all_logicw, compute_c_logicw },
1302 [CC_OP_LOGICL] = { compute_all_logicl, compute_c_logicl },
1303
4b74fe1f
FB
1304 [CC_OP_INCB] = { compute_all_incb, compute_c_incl },
1305 [CC_OP_INCW] = { compute_all_incw, compute_c_incl },
367e86e8
FB
1306 [CC_OP_INCL] = { compute_all_incl, compute_c_incl },
1307
4b74fe1f
FB
1308 [CC_OP_DECB] = { compute_all_decb, compute_c_incl },
1309 [CC_OP_DECW] = { compute_all_decw, compute_c_incl },
367e86e8
FB
1310 [CC_OP_DECL] = { compute_all_decl, compute_c_incl },
1311
2792c4f2
FB
1312 [CC_OP_SHLB] = { compute_all_shlb, compute_c_shlb },
1313 [CC_OP_SHLW] = { compute_all_shlw, compute_c_shlw },
367e86e8 1314 [CC_OP_SHLL] = { compute_all_shll, compute_c_shll },
4b74fe1f 1315
2792c4f2
FB
1316 [CC_OP_SARB] = { compute_all_sarb, compute_c_sarl },
1317 [CC_OP_SARW] = { compute_all_sarw, compute_c_sarl },
1318 [CC_OP_SARL] = { compute_all_sarl, compute_c_sarl },
367e86e8 1319};
927f621e 1320
f631ef9b
FB
1321/* floating point support. Some of the code for complicated x87
1322 functions comes from the LGPL'ed x86 emulator found in the Willows
1323 TWIN windows emulator. */
927f621e 1324
51fe6890
FB
1325#if defined(__powerpc__)
1326extern CPU86_LDouble copysign(CPU86_LDouble, CPU86_LDouble);
1327
1328/* correct (but slow) PowerPC rint() (glibc version is incorrect) */
1329double qemu_rint(double x)
1330{
1331 double y = 4503599627370496.0;
1332 if (fabs(x) >= y)
1333 return x;
1334 if (x < 0)
1335 y = -y;
1336 y = (x + y) - y;
1337 if (y == 0.0)
1338 y = copysign(y, x);
1339 return y;
1340}
1341
1342#define rint qemu_rint
1343#endif
1344
927f621e
FB
1345/* fp load FT0 */
1346
1347void OPPROTO op_flds_FT0_A0(void)
1348{
d014c98c
FB
1349#ifdef USE_FP_CONVERT
1350 FP_CONVERT.i32 = ldl((void *)A0);
1351 FT0 = FP_CONVERT.f;
1352#else
927f621e 1353 FT0 = ldfl((void *)A0);
d014c98c 1354#endif
927f621e
FB
1355}
1356
1357void OPPROTO op_fldl_FT0_A0(void)
1358{
d014c98c
FB
1359#ifdef USE_FP_CONVERT
1360 FP_CONVERT.i64 = ldq((void *)A0);
1361 FT0 = FP_CONVERT.d;
1362#else
927f621e 1363 FT0 = ldfq((void *)A0);
d014c98c 1364#endif
927f621e
FB
1365}
1366
04369ff2
FB
1367/* helpers are needed to avoid static constant reference. XXX: find a better way */
1368#ifdef USE_INT_TO_FLOAT_HELPERS
1369
1370void helper_fild_FT0_A0(void)
1371{
1372 FT0 = (CPU86_LDouble)ldsw((void *)A0);
1373}
1374
1375void helper_fildl_FT0_A0(void)
1376{
1377 FT0 = (CPU86_LDouble)((int32_t)ldl((void *)A0));
1378}
1379
1380void helper_fildll_FT0_A0(void)
1381{
1382 FT0 = (CPU86_LDouble)((int64_t)ldq((void *)A0));
1383}
1384
1385void OPPROTO op_fild_FT0_A0(void)
1386{
1387 helper_fild_FT0_A0();
1388}
1389
1390void OPPROTO op_fildl_FT0_A0(void)
1391{
1392 helper_fildl_FT0_A0();
1393}
1394
1395void OPPROTO op_fildll_FT0_A0(void)
1396{
1397 helper_fildll_FT0_A0();
1398}
1399
1400#else
1401
927f621e
FB
1402void OPPROTO op_fild_FT0_A0(void)
1403{
d014c98c
FB
1404#ifdef USE_FP_CONVERT
1405 FP_CONVERT.i32 = ldsw((void *)A0);
1406 FT0 = (CPU86_LDouble)FP_CONVERT.i32;
1407#else
927f621e 1408 FT0 = (CPU86_LDouble)ldsw((void *)A0);
d014c98c 1409#endif
927f621e
FB
1410}
1411
1412void OPPROTO op_fildl_FT0_A0(void)
1413{
d014c98c
FB
1414#ifdef USE_FP_CONVERT
1415 FP_CONVERT.i32 = (int32_t) ldl((void *)A0);
1416 FT0 = (CPU86_LDouble)FP_CONVERT.i32;
1417#else
927f621e 1418 FT0 = (CPU86_LDouble)((int32_t)ldl((void *)A0));
d014c98c 1419#endif
927f621e
FB
1420}
1421
1422void OPPROTO op_fildll_FT0_A0(void)
1423{
d014c98c
FB
1424#ifdef USE_FP_CONVERT
1425 FP_CONVERT.i64 = (int64_t) ldq((void *)A0);
1426 FT0 = (CPU86_LDouble)FP_CONVERT.i64;
1427#else
927f621e 1428 FT0 = (CPU86_LDouble)((int64_t)ldq((void *)A0));
d014c98c 1429#endif
927f621e 1430}
04369ff2 1431#endif
927f621e
FB
1432
1433/* fp load ST0 */
1434
1435void OPPROTO op_flds_ST0_A0(void)
1436{
d014c98c
FB
1437#ifdef USE_FP_CONVERT
1438 FP_CONVERT.i32 = ldl((void *)A0);
1439 ST0 = FP_CONVERT.f;
1440#else
927f621e 1441 ST0 = ldfl((void *)A0);
d014c98c 1442#endif
927f621e
FB
1443}
1444
1445void OPPROTO op_fldl_ST0_A0(void)
1446{
d014c98c
FB
1447#ifdef USE_FP_CONVERT
1448 FP_CONVERT.i64 = ldq((void *)A0);
1449 ST0 = FP_CONVERT.d;
1450#else
927f621e 1451 ST0 = ldfq((void *)A0);
d014c98c 1452#endif
927f621e
FB
1453}
1454
77f8dd5a
FB
1455#ifdef USE_X86LDOUBLE
1456void OPPROTO op_fldt_ST0_A0(void)
1457{
1458 ST0 = *(long double *)A0;
1459}
1460#else
77f8dd5a
FB
1461void OPPROTO op_fldt_ST0_A0(void)
1462{
1463 helper_fldt_ST0_A0();
1464}
1465#endif
1466
04369ff2
FB
1467/* helpers are needed to avoid static constant reference. XXX: find a better way */
1468#ifdef USE_INT_TO_FLOAT_HELPERS
1469
1470void helper_fild_ST0_A0(void)
1471{
1472 ST0 = (CPU86_LDouble)ldsw((void *)A0);
1473}
1474
1475void helper_fildl_ST0_A0(void)
1476{
1477 ST0 = (CPU86_LDouble)((int32_t)ldl((void *)A0));
1478}
1479
1480void helper_fildll_ST0_A0(void)
1481{
1482 ST0 = (CPU86_LDouble)((int64_t)ldq((void *)A0));
1483}
1484
1485void OPPROTO op_fild_ST0_A0(void)
1486{
1487 helper_fild_ST0_A0();
1488}
1489
1490void OPPROTO op_fildl_ST0_A0(void)
1491{
1492 helper_fildl_ST0_A0();
1493}
1494
1495void OPPROTO op_fildll_ST0_A0(void)
1496{
1497 helper_fildll_ST0_A0();
1498}
1499
1500#else
1501
927f621e
FB
1502void OPPROTO op_fild_ST0_A0(void)
1503{
d014c98c
FB
1504#ifdef USE_FP_CONVERT
1505 FP_CONVERT.i32 = ldsw((void *)A0);
1506 ST0 = (CPU86_LDouble)FP_CONVERT.i32;
1507#else
927f621e 1508 ST0 = (CPU86_LDouble)ldsw((void *)A0);
d014c98c 1509#endif
927f621e
FB
1510}
1511
1512void OPPROTO op_fildl_ST0_A0(void)
1513{
d014c98c
FB
1514#ifdef USE_FP_CONVERT
1515 FP_CONVERT.i32 = (int32_t) ldl((void *)A0);
1516 ST0 = (CPU86_LDouble)FP_CONVERT.i32;
1517#else
927f621e 1518 ST0 = (CPU86_LDouble)((int32_t)ldl((void *)A0));
d014c98c 1519#endif
927f621e
FB
1520}
1521
1522void OPPROTO op_fildll_ST0_A0(void)
1523{
d014c98c
FB
1524#ifdef USE_FP_CONVERT
1525 FP_CONVERT.i64 = (int64_t) ldq((void *)A0);
1526 ST0 = (CPU86_LDouble)FP_CONVERT.i64;
1527#else
927f621e 1528 ST0 = (CPU86_LDouble)((int64_t)ldq((void *)A0));
d014c98c 1529#endif
927f621e
FB
1530}
1531
04369ff2
FB
1532#endif
1533
927f621e
FB
1534/* fp store */
1535
1536void OPPROTO op_fsts_ST0_A0(void)
1537{
d014c98c 1538#ifdef USE_FP_CONVERT
87f4827e 1539 FP_CONVERT.f = (float)ST0;
d014c98c
FB
1540 stfl((void *)A0, FP_CONVERT.f);
1541#else
927f621e 1542 stfl((void *)A0, (float)ST0);
d014c98c 1543#endif
927f621e
FB
1544}
1545
1546void OPPROTO op_fstl_ST0_A0(void)
1547{
77f8dd5a 1548 stfq((void *)A0, (double)ST0);
927f621e
FB
1549}
1550
77f8dd5a
FB
1551#ifdef USE_X86LDOUBLE
1552void OPPROTO op_fstt_ST0_A0(void)
1553{
1554 *(long double *)A0 = ST0;
1555}
1556#else
77f8dd5a
FB
1557void OPPROTO op_fstt_ST0_A0(void)
1558{
1559 helper_fstt_ST0_A0();
1560}
1561#endif
1562
927f621e
FB
1563void OPPROTO op_fist_ST0_A0(void)
1564{
d014c98c
FB
1565#if defined(__sparc__) && !defined(__sparc_v9__)
1566 register CPU86_LDouble d asm("o0");
1567#else
1568 CPU86_LDouble d;
1569#endif
927f621e 1570 int val;
d014c98c
FB
1571
1572 d = ST0;
1573 val = lrint(d);
1e5ffbed
FB
1574 if (val != (int16_t)val)
1575 val = -32768;
927f621e
FB
1576 stw((void *)A0, val);
1577}
1578
1579void OPPROTO op_fistl_ST0_A0(void)
1580{
d014c98c
FB
1581#if defined(__sparc__) && !defined(__sparc_v9__)
1582 register CPU86_LDouble d asm("o0");
1583#else
1584 CPU86_LDouble d;
1585#endif
927f621e 1586 int val;
d014c98c
FB
1587
1588 d = ST0;
1589 val = lrint(d);
927f621e
FB
1590 stl((void *)A0, val);
1591}
1592
1593void OPPROTO op_fistll_ST0_A0(void)
1594{
d014c98c
FB
1595#if defined(__sparc__) && !defined(__sparc_v9__)
1596 register CPU86_LDouble d asm("o0");
1597#else
1598 CPU86_LDouble d;
1599#endif
927f621e 1600 int64_t val;
d014c98c
FB
1601
1602 d = ST0;
1603 val = llrint(d);
927f621e
FB
1604 stq((void *)A0, val);
1605}
1606
77f8dd5a
FB
1607void OPPROTO op_fbld_ST0_A0(void)
1608{
1609 helper_fbld_ST0_A0();
1610}
1611
77f8dd5a
FB
1612void OPPROTO op_fbst_ST0_A0(void)
1613{
1614 helper_fbst_ST0_A0();
1615}
1616
927f621e
FB
1617/* FPU move */
1618
927f621e
FB
1619void OPPROTO op_fpush(void)
1620{
1621 fpush();
1622}
1623
1624void OPPROTO op_fpop(void)
1625{
1626 fpop();
1627}
1628
1629void OPPROTO op_fdecstp(void)
1630{
1631 env->fpstt = (env->fpstt - 1) & 7;
1632 env->fpus &= (~0x4700);
1633}
1634
1635void OPPROTO op_fincstp(void)
1636{
1637 env->fpstt = (env->fpstt + 1) & 7;
1638 env->fpus &= (~0x4700);
1639}
1640
1641void OPPROTO op_fmov_ST0_FT0(void)
1642{
1643 ST0 = FT0;
1644}
1645
1646void OPPROTO op_fmov_FT0_STN(void)
1647{
1648 FT0 = ST(PARAM1);
1649}
1650
1651void OPPROTO op_fmov_ST0_STN(void)
1652{
1653 ST0 = ST(PARAM1);
1654}
1655
1656void OPPROTO op_fmov_STN_ST0(void)
1657{
1658 ST(PARAM1) = ST0;
1659}
1660
1661void OPPROTO op_fxchg_ST0_STN(void)
1662{
1663 CPU86_LDouble tmp;
1664 tmp = ST(PARAM1);
1665 ST(PARAM1) = ST0;
1666 ST0 = tmp;
1667}
1668
1669/* FPU operations */
1670
1671/* XXX: handle nans */
1672void OPPROTO op_fcom_ST0_FT0(void)
1673{
1674 env->fpus &= (~0x4500); /* (C3,C2,C0) <-- 000 */
1675 if (ST0 < FT0)
1676 env->fpus |= 0x100; /* (C3,C2,C0) <-- 001 */
1677 else if (ST0 == FT0)
1678 env->fpus |= 0x4000; /* (C3,C2,C0) <-- 100 */
1679 FORCE_RET();
1680}
1681
77f8dd5a
FB
1682/* XXX: handle nans */
1683void OPPROTO op_fucom_ST0_FT0(void)
1684{
1685 env->fpus &= (~0x4500); /* (C3,C2,C0) <-- 000 */
1686 if (ST0 < FT0)
1687 env->fpus |= 0x100; /* (C3,C2,C0) <-- 001 */
1688 else if (ST0 == FT0)
1689 env->fpus |= 0x4000; /* (C3,C2,C0) <-- 100 */
1690 FORCE_RET();
1691}
1692
d0a1ffc9
FB
1693/* XXX: handle nans */
1694void OPPROTO op_fcomi_ST0_FT0(void)
1695{
1696 int eflags;
1697 eflags = cc_table[CC_OP].compute_all();
1698 eflags &= ~(CC_Z | CC_P | CC_C);
1699 if (ST0 < FT0)
1700 eflags |= CC_C;
1701 else if (ST0 == FT0)
1702 eflags |= CC_Z;
1703 CC_SRC = eflags;
1704 FORCE_RET();
1705}
1706
1707/* XXX: handle nans */
1708void OPPROTO op_fucomi_ST0_FT0(void)
1709{
1710 int eflags;
1711 eflags = cc_table[CC_OP].compute_all();
1712 eflags &= ~(CC_Z | CC_P | CC_C);
1713 if (ST0 < FT0)
1714 eflags |= CC_C;
1715 else if (ST0 == FT0)
1716 eflags |= CC_Z;
1717 CC_SRC = eflags;
1718 FORCE_RET();
1719}
1720
927f621e
FB
1721void OPPROTO op_fadd_ST0_FT0(void)
1722{
1723 ST0 += FT0;
1724}
1725
1726void OPPROTO op_fmul_ST0_FT0(void)
1727{
1728 ST0 *= FT0;
1729}
1730
1731void OPPROTO op_fsub_ST0_FT0(void)
1732{
1733 ST0 -= FT0;
1734}
1735
1736void OPPROTO op_fsubr_ST0_FT0(void)
1737{
1738 ST0 = FT0 - ST0;
1739}
1740
1741void OPPROTO op_fdiv_ST0_FT0(void)
1742{
1743 ST0 /= FT0;
1744}
1745
1746void OPPROTO op_fdivr_ST0_FT0(void)
1747{
1748 ST0 = FT0 / ST0;
1749}
1750
1751/* fp operations between STN and ST0 */
1752
1753void OPPROTO op_fadd_STN_ST0(void)
1754{
1755 ST(PARAM1) += ST0;
1756}
1757
1758void OPPROTO op_fmul_STN_ST0(void)
1759{
1760 ST(PARAM1) *= ST0;
1761}
1762
1763void OPPROTO op_fsub_STN_ST0(void)
1764{
1765 ST(PARAM1) -= ST0;
1766}
1767
1768void OPPROTO op_fsubr_STN_ST0(void)
1769{
1770 CPU86_LDouble *p;
1771 p = &ST(PARAM1);
1772 *p = ST0 - *p;
1773}
1774
1775void OPPROTO op_fdiv_STN_ST0(void)
1776{
1777 ST(PARAM1) /= ST0;
1778}
1779
1780void OPPROTO op_fdivr_STN_ST0(void)
1781{
1782 CPU86_LDouble *p;
1783 p = &ST(PARAM1);
1784 *p = ST0 / *p;
1785}
1786
1787/* misc FPU operations */
1788void OPPROTO op_fchs_ST0(void)
1789{
1790 ST0 = -ST0;
1791}
1792
1793void OPPROTO op_fabs_ST0(void)
1794{
1795 ST0 = fabs(ST0);
1796}
1797
77f8dd5a
FB
1798void OPPROTO op_fxam_ST0(void)
1799{
1800 helper_fxam_ST0();
927f621e
FB
1801}
1802
1803void OPPROTO op_fld1_ST0(void)
1804{
87f4827e 1805 ST0 = f15rk[1];
927f621e
FB
1806}
1807
77f8dd5a 1808void OPPROTO op_fldl2t_ST0(void)
927f621e 1809{
87f4827e 1810 ST0 = f15rk[6];
927f621e
FB
1811}
1812
77f8dd5a 1813void OPPROTO op_fldl2e_ST0(void)
927f621e 1814{
87f4827e 1815 ST0 = f15rk[5];
927f621e
FB
1816}
1817
1818void OPPROTO op_fldpi_ST0(void)
1819{
87f4827e 1820 ST0 = f15rk[2];
927f621e
FB
1821}
1822
1823void OPPROTO op_fldlg2_ST0(void)
1824{
87f4827e 1825 ST0 = f15rk[3];
927f621e
FB
1826}
1827
1828void OPPROTO op_fldln2_ST0(void)
1829{
87f4827e 1830 ST0 = f15rk[4];
927f621e
FB
1831}
1832
1833void OPPROTO op_fldz_ST0(void)
1834{
87f4827e 1835 ST0 = f15rk[0];
927f621e
FB
1836}
1837
1838void OPPROTO op_fldz_FT0(void)
1839{
87f4827e 1840 ST0 = f15rk[0];
927f621e
FB
1841}
1842
927f621e
FB
1843/* associated heplers to reduce generated code length and to simplify
1844 relocation (FP constants are usually stored in .rodata section) */
1845
1846void OPPROTO op_f2xm1(void)
1847{
1848 helper_f2xm1();
1849}
1850
1851void OPPROTO op_fyl2x(void)
1852{
1853 helper_fyl2x();
1854}
1855
1856void OPPROTO op_fptan(void)
1857{
1858 helper_fptan();
1859}
1860
1861void OPPROTO op_fpatan(void)
1862{
1863 helper_fpatan();
1864}
1865
1866void OPPROTO op_fxtract(void)
1867{
1868 helper_fxtract();
1869}
1870
1871void OPPROTO op_fprem1(void)
1872{
1873 helper_fprem1();
1874}
1875
1876
1877void OPPROTO op_fprem(void)
1878{
1879 helper_fprem();
1880}
1881
1882void OPPROTO op_fyl2xp1(void)
1883{
1884 helper_fyl2xp1();
1885}
1886
1887void OPPROTO op_fsqrt(void)
1888{
1889 helper_fsqrt();
1890}
1891
1892void OPPROTO op_fsincos(void)
1893{
1894 helper_fsincos();
1895}
1896
1897void OPPROTO op_frndint(void)
1898{
1899 helper_frndint();
1900}
1901
1902void OPPROTO op_fscale(void)
1903{
1904 helper_fscale();
1905}
1906
1907void OPPROTO op_fsin(void)
1908{
1909 helper_fsin();
1910}
1911
1912void OPPROTO op_fcos(void)
1913{
1914 helper_fcos();
1915}
1916
4b74fe1f
FB
1917void OPPROTO op_fnstsw_A0(void)
1918{
1919 int fpus;
1920 fpus = (env->fpus & ~0x3800) | (env->fpstt & 0x7) << 11;
1921 stw((void *)A0, fpus);
1922}
1923
77f8dd5a
FB
1924void OPPROTO op_fnstsw_EAX(void)
1925{
1926 int fpus;
1927 fpus = (env->fpus & ~0x3800) | (env->fpstt & 0x7) << 11;
1928 EAX = (EAX & 0xffff0000) | fpus;
1929}
1930
4b74fe1f
FB
1931void OPPROTO op_fnstcw_A0(void)
1932{
1933 stw((void *)A0, env->fpuc);
1934}
1935
1936void OPPROTO op_fldcw_A0(void)
1937{
1938 int rnd_type;
1939 env->fpuc = lduw((void *)A0);
1940 /* set rounding mode */
1941 switch(env->fpuc & RC_MASK) {
1942 default:
1943 case RC_NEAR:
1944 rnd_type = FE_TONEAREST;
1945 break;
1946 case RC_DOWN:
1947 rnd_type = FE_DOWNWARD;
1948 break;
1949 case RC_UP:
1950 rnd_type = FE_UPWARD;
1951 break;
1952 case RC_CHOP:
1953 rnd_type = FE_TOWARDZERO;
1954 break;
1955 }
1956 fesetround(rnd_type);
1957}
1958
1a9353d2
FB
1959void OPPROTO op_fclex(void)
1960{
1961 env->fpus &= 0x7f00;
1962}
1963
1964void OPPROTO op_fninit(void)
1965{
1966 env->fpus = 0;
1967 env->fpstt = 0;
1968 env->fpuc = 0x37f;
1969 env->fptags[0] = 1;
1970 env->fptags[1] = 1;
1971 env->fptags[2] = 1;
1972 env->fptags[3] = 1;
1973 env->fptags[4] = 1;
1974 env->fptags[5] = 1;
1975 env->fptags[6] = 1;
1976 env->fptags[7] = 1;
1977}
1b6b029e 1978
d0a1ffc9
FB
1979void OPPROTO op_fnstenv_A0(void)
1980{
1981 helper_fstenv((uint8_t *)A0, PARAM1);
1982}
1983
1984void OPPROTO op_fldenv_A0(void)
1985{
1986 helper_fldenv((uint8_t *)A0, PARAM1);
1987}
1988
1989void OPPROTO op_fnsave_A0(void)
1990{
1991 helper_fsave((uint8_t *)A0, PARAM1);
1992}
1993
1994void OPPROTO op_frstor_A0(void)
1995{
1996 helper_frstor((uint8_t *)A0, PARAM1);
1997}
1998
1b6b029e
FB
1999/* threading support */
2000void OPPROTO op_lock(void)
2001{
2002 cpu_lock();
2003}
2004
2005void OPPROTO op_unlock(void)
2006{
2007 cpu_unlock();
2008}
3ec9c4fc 2009