system.pp 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362
  1. {
  2. This file is part of the Free Pascal run time library.
  3. Copyright (c) 1999-2000 by Carl Eric Codere
  4. member of the Free Pascal development team
  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 system;
  12. {--------------------------------------------------------------------}
  13. { LEFT TO DO: }
  14. {--------------------------------------------------------------------}
  15. { o SBrk }
  16. { o Implement truncate }
  17. { o Implement paramstr(0) }
  18. {--------------------------------------------------------------------}
  19. interface
  20. {$I systemh.inc}
  21. {Platform specific information}
  22. const
  23. LineEnding = #10;
  24. LFNSupport = true;
  25. CtrlZMarksEOF: boolean = false; (* #26 not considered as end of file *)
  26. DirectorySeparator = '/';
  27. DriveSeparator = ':';
  28. ExtensionSeparator = '.';
  29. PathSeparator = ';';
  30. AllowDirectorySeparators : set of char = ['\','/'];
  31. AllowDriveSeparators : set of char = [':'];
  32. FileNameCaseSensitive = false;
  33. FileNameCasePreserving = false;
  34. maxExitCode = 255;
  35. MaxPathLen = 255;
  36. AllFilesMask = '*';
  37. sLineBreak: string [1] = LineEnding;
  38. { used for single computations }
  39. const BIAS4 = $7f-1;
  40. const
  41. UnusedHandle = $ffff;
  42. StdInputHandle = 0;
  43. StdOutputHandle = 1;
  44. StdErrorHandle = $ffff;
  45. {$if defined(CPUARM) or defined(CPUM68K)}
  46. {$define fpc_softfpu_interface}
  47. {$i softfpu.pp}
  48. {$undef fpc_softfpu_interface}
  49. {$endif defined(CPUARM) or defined(CPUM68K)}
  50. implementation
  51. {$if defined(CPUARM) or defined(CPUM68K)}
  52. {$define fpc_softfpu_implementation}
  53. {$i softfpu.pp}
  54. {$undef fpc_softfpu_implementation}
  55. { we get these functions and types from the softfpu code }
  56. {$define FPC_SYSTEM_HAS_float64}
  57. {$define FPC_SYSTEM_HAS_float32}
  58. {$define FPC_SYSTEM_HAS_flag}
  59. {$define FPC_SYSTEM_HAS_extractFloat64Frac0}
  60. {$define FPC_SYSTEM_HAS_extractFloat64Frac1}
  61. {$define FPC_SYSTEM_HAS_extractFloat64Exp}
  62. {$define FPC_SYSTEM_HAS_extractFloat64Sign}
  63. {$define FPC_SYSTEM_HAS_ExtractFloat32Frac}
  64. {$define FPC_SYSTEM_HAS_extractFloat32Exp}
  65. {$define FPC_SYSTEM_HAS_extractFloat32Sign}
  66. {$endif defined(CPUARM) or defined(CPUM68K)}
  67. {$I system.inc}
  68. {$I lowmath.inc}
  69. function GetProcessID:SizeUInt;
  70. begin
  71. {$WARNING To be checked by platform maintainer}
  72. GetProcessID := 1;
  73. end;
  74. const
  75. argc : longint = 0;
  76. var
  77. errno : integer;
  78. {$S-}
  79. procedure Stack_Check; assembler;
  80. { Check for local variable allocation }
  81. { On Entry -> d0 : size of local stack we are trying to allocate }
  82. asm
  83. XDEF STACKCHECK
  84. move.l sp,d1 { get value of stack pointer }
  85. sub.l d0,d1 { sp - stack_size }
  86. sub.l #2048,d1
  87. cmp.l __BREAK,d1
  88. bgt @st1nosweat
  89. move.l #202,d0
  90. jsr HALT_ERROR
  91. @st1nosweat:
  92. end;
  93. Procedure Error2InOut;
  94. Begin
  95. if (errno <= -2) and (errno >= -11) then
  96. InOutRes:=150-errno { 150+errno }
  97. else
  98. Begin
  99. case errno of
  100. -32 : InOutRes:=1;
  101. -33 : InOutRes:=2;
  102. -34 : InOutRes:=3;
  103. -35 : InOutRes:=4;
  104. -36 : InOutRes:=5;
  105. -37 : InOutRes:=8;
  106. -39 : InOutRes:=8;
  107. -40 : InOutRes:=9;
  108. -46 : InOutRes:=15;
  109. -67..-64 : InOutRes:=153;
  110. -15 : InOutRes:=151;
  111. -13 : InOutRes:=150;
  112. else
  113. InOutres := word(errno);
  114. end;
  115. end;
  116. errno:=0;
  117. end;
  118. procedure halt(errnum : byte);
  119. begin
  120. do_exit;
  121. flush(stderr);
  122. asm
  123. clr.l d0
  124. move.b errnum,d0
  125. move.w d0,-(sp)
  126. move.w #$4c,-(sp)
  127. trap #1
  128. end;
  129. end;
  130. function args : pointer; assembler;
  131. asm
  132. move.l __ARGS,d0
  133. end;
  134. Function GetParamCount(const p: pchar): longint;
  135. var
  136. i: word;
  137. count: word;
  138. Begin
  139. i:=0;
  140. count:=0;
  141. while p[count] <> #0 do
  142. Begin
  143. if (p[count] <> ' ') and (p[count] <> #9) and (p[count] <> #0) then
  144. Begin
  145. i:=i+1;
  146. while (p[count] <> ' ') and (p[count] <> #9) and (p[count] <> #0) do
  147. count:=count+1;
  148. end;
  149. if p[count] = #0 then break;
  150. count:=count+1;
  151. end;
  152. GetParamCount:=longint(i);
  153. end;
  154. Function GetParam(index: word; const p : pchar): string;
  155. { On Entry: index = string index to correct parameter }
  156. { On exit: = correct character index into pchar array }
  157. { Returns correct index to command line argument }
  158. var
  159. count: word;
  160. localindex: word;
  161. l: byte;
  162. temp: string;
  163. Begin
  164. temp:='';
  165. count := 0;
  166. { first index is one }
  167. localindex := 1;
  168. l:=0;
  169. While p[count] <> #0 do
  170. Begin
  171. if (p[count] <> ' ') and (p[count] <> #9) then
  172. Begin
  173. if localindex = index then
  174. Begin
  175. while (p[count] <> #0) and (p[count] <> ' ') and (p[count] <> #9) and (l < 256) do
  176. Begin
  177. temp:=temp+p[count];
  178. l:=l+1;
  179. count:=count+1;
  180. end;
  181. temp[0]:=char(l);
  182. GetParam:=temp;
  183. exit;
  184. end;
  185. { Point to next argument in list }
  186. while (p[count] <> #0) and (p[count] <> ' ') and (p[count] <> #9) do
  187. Begin
  188. count:=count+1;
  189. end;
  190. localindex:=localindex+1;
  191. end;
  192. if p[count] = #0 then break;
  193. count:=count+1;
  194. end;
  195. GetParam:=temp;
  196. end;
  197. function paramstr(l : longint) : string;
  198. var
  199. p : pchar;
  200. s1 : string;
  201. begin
  202. if l = 0 then
  203. Begin
  204. s1 := '';
  205. end
  206. else
  207. if (l>0) and (l<=paramcount) then
  208. begin
  209. p:=args;
  210. paramstr:=GetParam(word(l),p);
  211. end
  212. else paramstr:='';
  213. end;
  214. function paramcount : longint;
  215. Begin
  216. paramcount := argc;
  217. end;
  218. procedure randomize;
  219. var
  220. hl : longint;
  221. begin
  222. asm
  223. movem.l d2/d3/a2/a3, -(sp) { save OS registers }
  224. move.w #17,-(sp)
  225. trap #14 { call xbios - random number }
  226. add.l #2,sp
  227. movem.l (sp)+,d2/d3/a2/a3
  228. move.l d0,hl { result in d0 }
  229. end;
  230. randseed:=hl;
  231. end;
  232. function getheapstart:pointer;assembler;
  233. asm
  234. lea.l HEAP,a0
  235. move.l a0,d0
  236. end;
  237. function getheapsize:longint;assembler;
  238. asm
  239. move.l HEAP_SIZE,d0
  240. end ['D0'];
  241. { This routine is used to grow the heap. }
  242. { But here we do a trick, we say that the }
  243. { heap cannot be regrown! }
  244. function sbrk( size: longint): pointer;
  245. { on exit nil = if fails. }
  246. Begin
  247. sbrk:=nil;
  248. end;
  249. {$I heap.inc}
  250. {*****************************************************************************
  251. UnTyped File Handling
  252. *****************************************************************************}
  253. {$i file.inc}
  254. {*****************************************************************************
  255. Typed File Handling
  256. *****************************************************************************}
  257. {$i typefile.inc}
  258. {*****************************************************************************
  259. Text File Handling
  260. *****************************************************************************}
  261. {$i text.inc}
  262. {*****************************************************************************
  263. System Dependent Exit code
  264. *****************************************************************************}
  265. Procedure system_exit;
  266. begin
  267. end;
  268. {*****************************************************************************
  269. SystemUnit Initialization
  270. *****************************************************************************}
  271. function CheckInitialStkLen (StkLen: SizeUInt): SizeUInt;
  272. begin
  273. CheckInitialStkLen := StkLen;
  274. end;
  275. begin
  276. StackLength := CheckInitialStkLen (InitialStkLen);
  277. { Initialize ExitProc }
  278. ExitProc:=Nil;
  279. { Setup heap }
  280. InitHeap;
  281. {$ifdef HASWIDESTRING}
  282. InitUnicodeStringManager;
  283. {$endif HASWIDESTRING}
  284. { Setup stdin, stdout and stderr }
  285. OpenStdIO(Input,fmInput,StdInputHandle);
  286. OpenStdIO(Output,fmOutput,StdOutputHandle);
  287. OpenStdIO(StdOut,fmOutput,StdOutputHandle);
  288. OpenStdIO(StdErr,fmOutput,StdErrorHandle);
  289. { Reset IO Error }
  290. InOutRes:=0;
  291. (* This should be changed to a real value during *)
  292. (* thread driver initialization if appropriate. *)
  293. ThreadID := 1;
  294. errno := 0;
  295. { Setup command line arguments }
  296. argc:=GetParamCount(args);
  297. InitVariantManager;
  298. end.