system.pp 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. unit system;
  2. {$ASMMODE intel}
  3. interface
  4. {$DEFINE FPC_INCLUDE_SOFTWARE_MUL}
  5. {$DEFINE FPC_INCLUDE_SOFTWARE_MOD_DIV}
  6. {$DEFINE FPC_USE_SMALL_DEFAULTSTACKSIZE}
  7. { To avoid warnings in thread.inc code,
  8. but value must be really given after
  9. systemh.inc is included otherwise the
  10. $mode switch is not effective }
  11. {$I systemh.inc}
  12. const
  13. LineEnding = #13#10;
  14. { LFNSupport is a variable here, defined below!!! }
  15. DirectorySeparator = '\';
  16. DriveSeparator = ':';
  17. ExtensionSeparator = '.';
  18. PathSeparator = ';';
  19. AllowDirectorySeparators : set of char = ['\','/'];
  20. AllowDriveSeparators : set of char = [':'];
  21. { FileNameCaseSensitive and FileNameCasePreserving are defined separately below!!! }
  22. maxExitCode = 255;
  23. MaxPathLen = 256;
  24. const
  25. { Default filehandles }
  26. UnusedHandle = $ffff;{ instead of -1, as it is a word value}
  27. StdInputHandle = 0;
  28. StdOutputHandle = 1;
  29. StdErrorHandle = 2;
  30. FileNameCaseSensitive : boolean = false;
  31. FileNameCasePreserving: boolean = false;
  32. CtrlZMarksEOF: boolean = true; (* #26 is considered as end of file *)
  33. sLineBreak = LineEnding;
  34. DefaultTextLineBreakStyle : TTextLineBreakStyle = tlbsCRLF;
  35. { Default memory segments (Tp7 compatibility) }
  36. seg0040 = $0040;
  37. segA000 = $A000;
  38. segB000 = $B000;
  39. segB800 = $B800;
  40. var
  41. { Mem[] support }
  42. mem : array[0..$7fff-1] of byte absolute $0:$0;
  43. memw : array[0..($7fff div sizeof(word))-1] of word absolute $0:$0;
  44. meml : array[0..($7fff div sizeof(longint))-1] of longint absolute $0:$0;
  45. { C-compatible arguments and environment }
  46. argc:longint; //!! public name 'operatingsystem_parameter_argc';
  47. argv:PPchar; //!! public name 'operatingsystem_parameter_argv';
  48. envp:PPchar; //!! public name 'operatingsystem_parameter_envp';
  49. dos_argv0 : pchar; //!! public name 'dos_argv0';
  50. dos_psp:Word;public name 'dos_psp';
  51. __stkbottom : pointer;public name '__stkbottom';
  52. __nearheap_start: pointer;public name '__nearheap_start';
  53. __nearheap_end: pointer;public name '__nearheap_end';
  54. AllFilesMask: string [3];
  55. {$ifndef RTLLITE}
  56. { System info }
  57. LFNSupport : boolean;
  58. {$ELSE RTLLITE}
  59. const
  60. LFNSupport = false;
  61. {$endif RTLLITE}
  62. procedure DebugWrite(const S: string);
  63. procedure DebugWriteLn(const S: string);
  64. implementation
  65. const
  66. fCarry = 1;
  67. {$I registers.inc}
  68. procedure Intr(IntNo: Byte; var Regs: Registers); external name 'FPC_INTR';
  69. procedure MsDos(var Regs: Registers); external name 'FPC_MSDOS';
  70. { invokes int 21h with the carry flag set on entry; used for the LFN functions
  71. to ensure that the carry flag is set on exit on older DOS versions which don't
  72. support them }
  73. procedure MsDos_Carry(var Regs: Registers); external name 'FPC_MSDOS_CARRY';
  74. {$I system.inc}
  75. {$I tinyheap.inc}
  76. procedure DebugWrite(const S: string);
  77. begin
  78. asm
  79. mov si, S
  80. lodsb
  81. mov cl, al
  82. xor ch, ch
  83. mov ah, 2
  84. @@1:
  85. lodsb
  86. mov dl, al
  87. int 21h
  88. loop @@1
  89. end ['ax','bx','cx','dx','si','di'];
  90. end;
  91. procedure DebugWriteLn(const S: string);
  92. begin
  93. DebugWrite(S);
  94. DebugWrite(#13#10);
  95. end;
  96. {*****************************************************************************
  97. ParamStr/Randomize
  98. *****************************************************************************}
  99. function paramcount : longint;
  100. begin
  101. paramcount := 0;
  102. end;
  103. function paramstr(l : longint) : string;
  104. begin
  105. paramstr := '';
  106. end;
  107. procedure randomize;
  108. var
  109. hl : longint;
  110. regs : Registers;
  111. begin
  112. regs.AH:=$2C;
  113. MsDos(regs);
  114. hl:=regs.DX;
  115. randseed:=hl*$10000+ regs.CX;
  116. end;
  117. {*****************************************************************************
  118. System Dependent Exit code
  119. *****************************************************************************}
  120. procedure system_exit;
  121. var
  122. h : byte;
  123. begin
  124. for h:=0 to max_files-1 do
  125. if openfiles[h] then
  126. begin
  127. {$ifdef SYSTEMDEBUG}
  128. writeln(stderr,'file ',opennames[h],' not closed at exit');
  129. {$endif SYSTEMDEBUG}
  130. if h>=5 then
  131. do_close(h);
  132. end;
  133. asm
  134. mov al, byte [exitcode]
  135. mov ah, 4Ch
  136. int 21h
  137. end;
  138. end;
  139. {*****************************************************************************
  140. SystemUnit Initialization
  141. *****************************************************************************}
  142. procedure InitNearHeap;
  143. begin
  144. SetMemoryManager(TinyHeapMemoryManager);
  145. RegisterTinyHeapBlock(__nearheap_start, ptruint(__nearheap_end) - ptruint(__nearheap_start));
  146. end;
  147. function CheckLFN:boolean;
  148. var
  149. regs : Registers;
  150. RootName : pchar;
  151. buf : array [0..31] of char;
  152. begin
  153. { Check LFN API on drive c:\ }
  154. RootName:='C:\';
  155. { Call 'Get Volume Information' ($71A0) }
  156. regs.AX:=$71a0;
  157. regs.ES:=Seg(buf);
  158. regs.DI:=Ofs(buf);
  159. regs.CX:=32;
  160. regs.DS:=Seg(RootName^);
  161. regs.DX:=Ofs(RootName^);
  162. MsDos_Carry(regs);
  163. { If carryflag=0 and LFN API bit in ebx is set then use Long file names }
  164. CheckLFN:=(regs.Flags and fCarry=0) and (regs.BX and $4000=$4000);
  165. end;
  166. procedure SysInitStdIO;
  167. begin
  168. OpenStdIO(Input,fmInput,StdInputHandle);
  169. OpenStdIO(Output,fmOutput,StdOutputHandle);
  170. OpenStdIO(ErrOutput,fmOutput,StdErrorHandle);
  171. OpenStdIO(StdOut,fmOutput,StdOutputHandle);
  172. OpenStdIO(StdErr,fmOutput,StdErrorHandle);
  173. end;
  174. function GetProcessID: SizeUInt;
  175. begin
  176. GetProcessID := dos_psp;
  177. end;
  178. function CheckInitialStkLen(stklen : SizeUInt) : SizeUInt;
  179. begin
  180. result := stklen;
  181. end;
  182. begin
  183. StackLength := CheckInitialStkLen(InitialStkLen);
  184. StackBottom := __stkbottom;
  185. if DetectFPU then
  186. SysInitFPU;
  187. { To be set if this is a GUI or console application }
  188. IsConsole := TRUE;
  189. { To be set if this is a library and not a program }
  190. IsLibrary := FALSE;
  191. { Setup heap }
  192. InitNearHeap;
  193. SysInitExceptions;
  194. initunicodestringmanager;
  195. { Setup stdin, stdout and stderr }
  196. SysInitStdIO;
  197. { Use LFNSupport LFN }
  198. LFNSupport:=CheckLFN;
  199. if LFNSupport then
  200. begin
  201. FileNameCasePreserving:=true;
  202. AllFilesMask := '*';
  203. end
  204. else
  205. AllFilesMask := '*.*';
  206. { Reset IO Error }
  207. InOutRes:=0;
  208. initvariantmanager;
  209. end.