2
0

emu387.pp 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. {
  2. This file is part of the Free Pascal run time library.
  3. Copyright (c) 1999-2000 by Pierre Muller
  4. FPU Emulator support
  5. See the file COPYING.FPC, included in this distribution,
  6. for details about the copyright.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  10. **********************************************************************}
  11. unit emu387;
  12. interface
  13. procedure npxsetup(prog_name : string);
  14. implementation
  15. {$asmmode ATT}
  16. uses
  17. dxeload,dpmiexcp,strings;
  18. type
  19. emu_entry_type = function(exc : pexception_state) : longint;
  20. var
  21. _emu_entry : emu_entry_type;
  22. procedure _control87(mask1,mask2 : longint);
  23. begin
  24. { Copyright (C) 1995 DJ Delorie, see COPYING.DJ for details }
  25. { from file cntrl87.s in src/libc/pc_hw/fpu }
  26. asm
  27. { make room on stack }
  28. pushl %eax
  29. fstcw (%esp)
  30. fwait
  31. popl %eax
  32. andl $0xffff, %eax
  33. { OK; we have the old value ready }
  34. movl mask2, %ecx
  35. notl %ecx
  36. andl %eax, %ecx { the bits we want to keep }
  37. movl mask2, %edx
  38. andl mask1, %edx { the bits we want to change }
  39. orl %ecx, %edx { the new value }
  40. pushl %edx
  41. fldcw (%esp)
  42. popl %edx
  43. end;
  44. end;
  45. { the problem with the stack that is not cleared }
  46. function emu_entry(exc : pexception_state) : longint;
  47. begin
  48. emu_entry:=_emu_entry(exc);
  49. end;
  50. function nofpsig( sig : longint) : longint;cdecl;
  51. const
  52. last_eip : longint = 0;
  53. var
  54. res : longint;
  55. begin
  56. {if last_eip=djgpp_exception_state^.__eip then
  57. begin
  58. writeln('emu call two times at same address');
  59. dpmi_set_coprocessor_emulation(1);
  60. _raise(SIGFPE);
  61. exit(0);
  62. end; }
  63. last_eip:=djgpp_exception_state^.__eip;
  64. res:=emu_entry(djgpp_exception_state);
  65. if res<>0 then
  66. begin
  67. writeln('emu call failed. res = ',res);
  68. dpmi_set_coprocessor_emulation(1);
  69. _raise(SIGFPE);
  70. exit(0);
  71. end;
  72. dpmi_longjmp(pdpmi_jmp_buf(djgpp_exception_state)^, djgpp_exception_state^.__eax);
  73. nofpsig:=0;
  74. end;
  75. var
  76. prev_exit : pointer;
  77. procedure restore_DPMI_fpu_state;
  78. begin
  79. exitproc:=prev_exit;
  80. { Enable Coprocessor, no exceptions }
  81. dpmi_set_coprocessor_emulation(1);
  82. {$ifdef SYSTEMDEBUG}
  83. writeln(stderr,'Coprocessor restored ');
  84. {$endif}
  85. end;
  86. { function _detect_80387 : boolean;
  87. not used because of the underscore problem }
  88. {$L fpu.o }
  89. function getenv(const envvar:string):string;
  90. { Copied here, preserves uses Dos (PFV) }
  91. var
  92. hp : ppchar;
  93. hs,
  94. _envvar : string;
  95. eqpos : longint;
  96. begin
  97. _envvar:=upcase(envvar);
  98. hp:=envp;
  99. getenv:='';
  100. while assigned(hp^) do
  101. begin
  102. hs:=strpas(hp^);
  103. eqpos:=pos('=',hs);
  104. if copy(hs,1,eqpos-1)=_envvar then
  105. begin
  106. getenv:=copy(hs,eqpos+1,255);
  107. exit;
  108. end;
  109. inc(hp);
  110. end;
  111. end;
  112. function __detect_80387:byte;external name '__detect_80387';
  113. procedure npxsetup(prog_name : string);
  114. var
  115. cp : string;
  116. i : byte;
  117. have_80387 : boolean;
  118. emu_p : pointer;
  119. const
  120. veryfirst : boolean = True;
  121. begin
  122. cp:=getenv('387');
  123. if (length(cp)>0) and (upcase(cp[1])='N') then
  124. have_80387:=False
  125. else
  126. begin
  127. dpmi_set_coprocessor_emulation(1);
  128. asm
  129. call __detect_80387
  130. movb %al,have_80387
  131. end;
  132. end;
  133. if (length(cp)>0) and (upcase(cp[1])='Q') then
  134. begin
  135. if not have_80387 then
  136. write(stderr,'No ');
  137. writeln(stderr,'80387 detected.');
  138. end;
  139. if have_80387 then
  140. begin
  141. { mask all exceptions, except invalid operation }
  142. { change to same value as in v2prt0.as (PM) }
  143. _control87($0332, $ffff)
  144. end
  145. else
  146. begin
  147. { Flags value 3 means coprocessor emulation, exceptions to us }
  148. if (dpmi_set_coprocessor_emulation(3)<>0) then
  149. begin
  150. writeln(stderr,'Warning: Coprocessor not present and DPMI setup failed!');
  151. writeln(stderr,' If application attempts floating operations system may hang!');
  152. end
  153. else
  154. begin
  155. cp:=getenv('EMU387');
  156. if length(cp)=0 then
  157. begin
  158. for i:=length(prog_name) downto 1 do
  159. if prog_name[i] in AllowDirectorySeparators then
  160. break;
  161. if i>1 then
  162. cp:=copy(prog_name,1,i);
  163. cp:=cp+'wmemu387.dxe';
  164. end;
  165. emu_p:=dxe_load(cp);
  166. _emu_entry:=emu_entry_type(emu_p);
  167. if (emu_p=nil) then
  168. begin
  169. writeln(cp+' load failed !');
  170. halt;
  171. end;
  172. if veryfirst then
  173. begin
  174. veryfirst:=false;
  175. prev_exit:=exitproc;
  176. exitproc:=@restore_DPMI_fpu_state;
  177. end;
  178. signal(SIGNOFP,@nofpsig);
  179. end;
  180. end;
  181. end;
  182. begin
  183. npxsetup(paramstr(0));
  184. end.