system.pp 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  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. var
  68. dos_version:Word;public name 'dos_version';
  69. {$I registers.inc}
  70. procedure Intr(IntNo: Byte; var Regs: Registers); external name 'FPC_INTR';
  71. procedure MsDos(var Regs: Registers); external name 'FPC_MSDOS';
  72. { invokes int 21h with the carry flag set on entry; used for the LFN functions
  73. to ensure that the carry flag is set on exit on older DOS versions which don't
  74. support them }
  75. procedure MsDos_Carry(var Regs: Registers); external name 'FPC_MSDOS_CARRY';
  76. {$I system.inc}
  77. {$I tinyheap.inc}
  78. procedure DebugWrite(const S: string);
  79. begin
  80. asm
  81. mov si, S
  82. lodsb
  83. mov cl, al
  84. xor ch, ch
  85. mov ah, 2
  86. @@1:
  87. lodsb
  88. mov dl, al
  89. int 21h
  90. loop @@1
  91. end ['ax','bx','cx','dx','si','di'];
  92. end;
  93. procedure DebugWriteLn(const S: string);
  94. begin
  95. DebugWrite(S);
  96. DebugWrite(#13#10);
  97. end;
  98. {*****************************************************************************
  99. ParamStr/Randomize
  100. *****************************************************************************}
  101. function GetProgramName: string;
  102. type
  103. PFarByte = ^Byte;far;
  104. PFarWord = ^Word;far;
  105. var
  106. dos_env_seg: Word;
  107. ofs: Word;
  108. Ch, Ch2: Char;
  109. begin
  110. dos_env_seg := PFarWord(Ptr(dos_psp, $2C))^;
  111. ofs := 1;
  112. repeat
  113. Ch := Chr(PFarByte(Ptr(dos_env_seg,ofs - 1))^);
  114. Ch2 := Chr(PFarByte(Ptr(dos_env_seg,ofs))^);
  115. if (Ch = #0) and (Ch2 = #0) then
  116. begin
  117. Inc(ofs, 3);
  118. GetProgramName := '';
  119. repeat
  120. Ch := Chr(PFarByte(Ptr(dos_env_seg,ofs))^);
  121. if Ch <> #0 then
  122. GetProgramName := GetProgramName + Ch;
  123. Inc(ofs);
  124. if ofs = 0 then
  125. begin
  126. GetProgramName := '';
  127. exit;
  128. end;
  129. until Ch = #0;
  130. exit;
  131. end;
  132. Inc(ofs);
  133. if ofs = 0 then
  134. begin
  135. GetProgramName := '';
  136. exit;
  137. end;
  138. until false;
  139. end;
  140. function paramcount : longint;
  141. begin
  142. paramcount := 0;
  143. end;
  144. function paramstr(l : longint) : string;
  145. begin
  146. if l = 0 then
  147. paramstr := GetProgramName
  148. else
  149. paramstr := '';
  150. end;
  151. procedure randomize;
  152. var
  153. hl : longint;
  154. regs : Registers;
  155. begin
  156. regs.AH:=$2C;
  157. MsDos(regs);
  158. hl:=regs.DX;
  159. randseed:=hl*$10000+ regs.CX;
  160. end;
  161. {*****************************************************************************
  162. System Dependent Exit code
  163. *****************************************************************************}
  164. procedure system_exit;
  165. var
  166. h : byte;
  167. begin
  168. for h:=0 to max_files-1 do
  169. if openfiles[h] then
  170. begin
  171. {$ifdef SYSTEMDEBUG}
  172. writeln(stderr,'file ',opennames[h],' not closed at exit');
  173. {$endif SYSTEMDEBUG}
  174. if h>=5 then
  175. do_close(h);
  176. end;
  177. asm
  178. mov al, byte [exitcode]
  179. mov ah, 4Ch
  180. int 21h
  181. end;
  182. end;
  183. {*****************************************************************************
  184. SystemUnit Initialization
  185. *****************************************************************************}
  186. procedure InitNearHeap;
  187. begin
  188. SetMemoryManager(TinyHeapMemoryManager);
  189. RegisterTinyHeapBlock(__nearheap_start, ptruint(__nearheap_end) - ptruint(__nearheap_start));
  190. end;
  191. function CheckLFN:boolean;
  192. var
  193. regs : Registers;
  194. RootName : pchar;
  195. buf : array [0..31] of char;
  196. begin
  197. { Check LFN API on drive c:\ }
  198. RootName:='C:\';
  199. { Call 'Get Volume Information' ($71A0) }
  200. regs.AX:=$71a0;
  201. regs.ES:=Seg(buf);
  202. regs.DI:=Ofs(buf);
  203. regs.CX:=32;
  204. regs.DS:=Seg(RootName^);
  205. regs.DX:=Ofs(RootName^);
  206. MsDos_Carry(regs);
  207. { If carryflag=0 and LFN API bit in ebx is set then use Long file names }
  208. CheckLFN:=(regs.Flags and fCarry=0) and (regs.BX and $4000=$4000);
  209. end;
  210. procedure SysInitStdIO;
  211. begin
  212. OpenStdIO(Input,fmInput,StdInputHandle);
  213. OpenStdIO(Output,fmOutput,StdOutputHandle);
  214. OpenStdIO(ErrOutput,fmOutput,StdErrorHandle);
  215. OpenStdIO(StdOut,fmOutput,StdOutputHandle);
  216. OpenStdIO(StdErr,fmOutput,StdErrorHandle);
  217. end;
  218. function GetProcessID: SizeUInt;
  219. begin
  220. GetProcessID := dos_psp;
  221. end;
  222. function CheckInitialStkLen(stklen : SizeUInt) : SizeUInt;
  223. begin
  224. result := stklen;
  225. end;
  226. begin
  227. StackLength := CheckInitialStkLen(InitialStkLen);
  228. StackBottom := __stkbottom;
  229. if DetectFPU then
  230. SysInitFPU;
  231. { To be set if this is a GUI or console application }
  232. IsConsole := TRUE;
  233. { To be set if this is a library and not a program }
  234. IsLibrary := FALSE;
  235. { Setup heap }
  236. InitNearHeap;
  237. SysInitExceptions;
  238. initunicodestringmanager;
  239. { Setup stdin, stdout and stderr }
  240. SysInitStdIO;
  241. { Use LFNSupport LFN }
  242. LFNSupport:=CheckLFN;
  243. if LFNSupport then
  244. begin
  245. FileNameCasePreserving:=true;
  246. AllFilesMask := '*';
  247. end
  248. else
  249. AllFilesMask := '*.*';
  250. { Reset IO Error }
  251. InOutRes:=0;
  252. initvariantmanager;
  253. end.