system.pp 5.7 KB

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