fpu_aux.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. /*---------------------------------------------------------------------------+
  2. | fpu_aux.c |
  3. | |
  4. | Code to implement some of the FPU auxiliary instructions. |
  5. | |
  6. | Copyright (C) 1992,1993,1994,1997 |
  7. | W. Metzenthen, 22 Parker St, Ormond, Vic 3163, Australia |
  8. | E-mail billm@suburbia.net |
  9. | |
  10. | |
  11. +---------------------------------------------------------------------------*/
  12. #include "fpu_system.h"
  13. #include "exception.h"
  14. #include "fpu_emu.h"
  15. #include "status_w.h"
  16. #include "control_w.h"
  17. static void fnop(void)
  18. {
  19. }
  20. static void fclex(void)
  21. {
  22. partial_status &=
  23. ~(SW_Backward | SW_Summary | SW_Stack_Fault | SW_Precision |
  24. SW_Underflow | SW_Overflow | SW_Zero_Div | SW_Denorm_Op |
  25. SW_Invalid);
  26. no_ip_update = 1;
  27. }
  28. /* Needs to be externally visible */
  29. void finit(void)
  30. {
  31. control_word = 0x037f;
  32. partial_status = 0;
  33. top = 0; /* We don't keep top in the status word internally. */
  34. fpu_tag_word = 0xffff;
  35. /* The behaviour is different from that detailed in
  36. Section 15.1.6 of the Intel manual */
  37. operand_address.offset = 0;
  38. operand_address.selector = 0;
  39. instruction_address.offset = 0;
  40. instruction_address.selector = 0;
  41. instruction_address.opcode = 0;
  42. no_ip_update = 1;
  43. }
  44. /*
  45. * These are nops on the i387..
  46. */
  47. #define feni fnop
  48. #define fdisi fnop
  49. #define fsetpm fnop
  50. static FUNC const finit_table[] = {
  51. feni, fdisi, fclex, finit,
  52. fsetpm, FPU_illegal, FPU_illegal, FPU_illegal
  53. };
  54. void finit_(void)
  55. {
  56. (finit_table[FPU_rm]) ();
  57. }
  58. static void fstsw_ax(void)
  59. {
  60. *(short *)&FPU_EAX = status_word();
  61. no_ip_update = 1;
  62. }
  63. static FUNC const fstsw_table[] = {
  64. fstsw_ax, FPU_illegal, FPU_illegal, FPU_illegal,
  65. FPU_illegal, FPU_illegal, FPU_illegal, FPU_illegal
  66. };
  67. void fstsw_(void)
  68. {
  69. (fstsw_table[FPU_rm]) ();
  70. }
  71. static FUNC const fp_nop_table[] = {
  72. fnop, FPU_illegal, FPU_illegal, FPU_illegal,
  73. FPU_illegal, FPU_illegal, FPU_illegal, FPU_illegal
  74. };
  75. void fp_nop(void)
  76. {
  77. (fp_nop_table[FPU_rm]) ();
  78. }
  79. void fld_i_(void)
  80. {
  81. FPU_REG *st_new_ptr;
  82. int i;
  83. u_char tag;
  84. if (STACK_OVERFLOW) {
  85. FPU_stack_overflow();
  86. return;
  87. }
  88. /* fld st(i) */
  89. i = FPU_rm;
  90. if (NOT_EMPTY(i)) {
  91. reg_copy(&st(i), st_new_ptr);
  92. tag = FPU_gettagi(i);
  93. push();
  94. FPU_settag0(tag);
  95. } else {
  96. if (control_word & CW_Invalid) {
  97. /* The masked response */
  98. FPU_stack_underflow();
  99. } else
  100. EXCEPTION(EX_StackUnder);
  101. }
  102. }
  103. void fxch_i(void)
  104. {
  105. /* fxch st(i) */
  106. FPU_REG t;
  107. int i = FPU_rm;
  108. FPU_REG *st0_ptr = &st(0), *sti_ptr = &st(i);
  109. long tag_word = fpu_tag_word;
  110. int regnr = top & 7, regnri = ((regnr + i) & 7);
  111. u_char st0_tag = (tag_word >> (regnr * 2)) & 3;
  112. u_char sti_tag = (tag_word >> (regnri * 2)) & 3;
  113. if (st0_tag == TAG_Empty) {
  114. if (sti_tag == TAG_Empty) {
  115. FPU_stack_underflow();
  116. FPU_stack_underflow_i(i);
  117. return;
  118. }
  119. if (control_word & CW_Invalid) {
  120. /* Masked response */
  121. FPU_copy_to_reg0(sti_ptr, sti_tag);
  122. }
  123. FPU_stack_underflow_i(i);
  124. return;
  125. }
  126. if (sti_tag == TAG_Empty) {
  127. if (control_word & CW_Invalid) {
  128. /* Masked response */
  129. FPU_copy_to_regi(st0_ptr, st0_tag, i);
  130. }
  131. FPU_stack_underflow();
  132. return;
  133. }
  134. clear_C1();
  135. reg_copy(st0_ptr, &t);
  136. reg_copy(sti_ptr, st0_ptr);
  137. reg_copy(&t, sti_ptr);
  138. tag_word &= ~(3 << (regnr * 2)) & ~(3 << (regnri * 2));
  139. tag_word |= (sti_tag << (regnr * 2)) | (st0_tag << (regnri * 2));
  140. fpu_tag_word = tag_word;
  141. }
  142. void ffree_(void)
  143. {
  144. /* ffree st(i) */
  145. FPU_settagi(FPU_rm, TAG_Empty);
  146. }
  147. void ffreep(void)
  148. {
  149. /* ffree st(i) + pop - unofficial code */
  150. FPU_settagi(FPU_rm, TAG_Empty);
  151. FPU_pop();
  152. }
  153. void fst_i_(void)
  154. {
  155. /* fst st(i) */
  156. FPU_copy_to_regi(&st(0), FPU_gettag0(), FPU_rm);
  157. }
  158. void fstp_i(void)
  159. {
  160. /* fstp st(i) */
  161. FPU_copy_to_regi(&st(0), FPU_gettag0(), FPU_rm);
  162. FPU_pop();
  163. }