system.pp 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  1. {
  2. This file is part of the Free Pascal run time library.
  3. Copyright (c) 2006 by Florian Klaempfl
  4. member of the Free Pascal development team.
  5. System unit for embedded systems
  6. See the file COPYING.FPC, included in this distribution,
  7. for details about the copyright.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  11. **********************************************************************}
  12. Unit System;
  13. {*****************************************************************************}
  14. interface
  15. {*****************************************************************************}
  16. {$define FPC_IS_SYSTEM}
  17. {$define HAS_CMDLINE}
  18. { currently, the avr compiler cannot compile complex procedures especially dealing with int64
  19. which are probaly anyways rarely used on avr }
  20. {$ifdef CPUAVR}
  21. {$define EXCLUDE_COMPLEX_PROCS}
  22. {$endif CPUAVR}
  23. { $define USE_NOTHREADMANAGER}
  24. {$define DISABLE_NO_THREAD_MANAGER}
  25. { Do not use standard memory manager }
  26. {$define HAS_MEMORYMANAGER}
  27. {$define FPC_ANSI_TEXTFILEREC}
  28. {$I check.inc}
  29. {$I systemh.inc}
  30. const
  31. {$ifdef FPC_HAS_FEATURE_TEXTIO}
  32. LineEnding = #10;
  33. {$endif FPC_HAS_FEATURE_TEXTIO}
  34. {$ifdef FPC_HAS_FEATURE_FILEIO}
  35. LFNSupport = true;
  36. DirectorySeparator = '/';
  37. DriveSeparator = ':';
  38. ExtensionSeparator = '.';
  39. PathSeparator = ':';
  40. AllowDirectorySeparators : set of char = ['\','/'];
  41. AllowDriveSeparators : set of char = [':'];
  42. {$endif FPC_HAS_FEATURE_FILEIO}
  43. { FileNameCaseSensitive and FileNameCasePreserving are defined below! }
  44. {$ifdef FPC_HAS_FEATURE_EXITCODE}
  45. maxExitCode = 255;
  46. {$endif FPC_HAS_FEATURE_EXITCODE}
  47. {$ifdef FPC_HAS_FEATURE_FILEIO}
  48. MaxPathLen = 1024; // BSDs since 1993, Solaris 10, Darwin
  49. AllFilesMask = '*';
  50. UnusedHandle = -1;
  51. StdInputHandle = 0;
  52. StdOutputHandle = 1;
  53. StdErrorHandle = 2;
  54. FileNameCaseSensitive : boolean = true;
  55. FileNameCasePreserving: boolean = true;
  56. {$endif FPC_HAS_FEATURE_FILEIO}
  57. {$ifdef FPC_HAS_FEATURE_TEXTIO}
  58. CtrlZMarksEOF: boolean = false; (* #26 not considered as end of file *)
  59. sLineBreak = LineEnding;
  60. DefaultTextLineBreakStyle : TTextLineBreakStyle = tlbsCrLF;
  61. {$endif FPC_HAS_FEATURE_TEXTIO}
  62. type
  63. trtl_do_close = procedure (handle : longint);
  64. trtl_do_erase = procedure (p : pchar);
  65. trtl_do_rename = procedure (p1,p2 : pchar);
  66. trtl_do_write = function (h: longint; addr: pointer; len: longint) : longint;
  67. trtl_do_read = function (h: longint; addr: pointer; len: longint) : longint;
  68. trtl_do_filepos = function (handle: longint) : longint;
  69. trtl_do_seek = procedure (handle, pos: longint);
  70. trtl_do_seekend = function (handle: longint):longint;
  71. trtl_do_filesize = function (handle : longint) : longint;
  72. trtl_do_truncate = procedure (handle, pos: longint);
  73. trtl_do_open = procedure (var f;p:pchar;flags:longint);
  74. trtl_do_isdevice = function (handle: longint): boolean;
  75. var
  76. rtl_do_close : trtl_do_close = nil;
  77. rtl_do_erase : trtl_do_erase = nil;
  78. rtl_do_rename : trtl_do_rename = nil;
  79. rtl_do_write : trtl_do_write = nil;
  80. rtl_do_read : trtl_do_read = nil;
  81. rtl_do_filepos : trtl_do_filepos = nil;
  82. rtl_do_seek : trtl_do_seek = nil;
  83. rtl_do_seekend : trtl_do_seekend = nil;
  84. rtl_do_filesize : trtl_do_filesize = nil;
  85. rtl_do_truncate : trtl_do_truncate = nil;
  86. rtl_do_open : trtl_do_open = nil;
  87. rtl_do_isdevice : trtl_do_isdevice = nil;
  88. {$ifdef FPC_HAS_FEATURE_COMMANDARGS}
  89. var
  90. argc: LongInt = 0;
  91. argv: PPChar = nil;
  92. envp: PPChar = nil;
  93. cmdline: PChar = nil;
  94. {$endif FPC_HAS_FEATURE_COMMANDARGS}
  95. {$ifndef FPUNONE}
  96. {$ifdef FPC_HAS_FEATURE_SOFTFPU}
  97. {$define fpc_softfpu_interface}
  98. {$i softfpu.pp}
  99. {$undef fpc_softfpu_interface}
  100. {$endif FPC_HAS_FEATURE_SOFTFPU}
  101. {$endif FPUNONE}
  102. {*****************************************************************************}
  103. implementation
  104. {*****************************************************************************}
  105. { Include ELF resources }
  106. const calculated_cmdline:Pchar=nil;
  107. {$ifndef FPUNONE}
  108. {$ifdef FPC_HAS_FEATURE_SOFTFPU}
  109. {$define fpc_softfpu_implementation}
  110. {$i softfpu.pp}
  111. {$undef fpc_softfpu_implementation}
  112. {$endif FPUNONE}
  113. { we get these functions and types from the softfpu code }
  114. {$define FPC_SYSTEM_HAS_float64}
  115. {$define FPC_SYSTEM_HAS_float32}
  116. {$define FPC_SYSTEM_HAS_flag}
  117. {$define FPC_SYSTEM_HAS_extractFloat64Frac0}
  118. {$define FPC_SYSTEM_HAS_extractFloat64Frac1}
  119. {$define FPC_SYSTEM_HAS_extractFloat64Exp}
  120. {$define FPC_SYSTEM_HAS_extractFloat64Frac}
  121. {$define FPC_SYSTEM_HAS_extractFloat64Sign}
  122. {$define FPC_SYSTEM_HAS_ExtractFloat32Frac}
  123. {$define FPC_SYSTEM_HAS_extractFloat32Exp}
  124. {$define FPC_SYSTEM_HAS_extractFloat32Sign}
  125. {$endif FPC_HAS_FEATURE_SOFTFPU}
  126. {$I system.inc}
  127. {*****************************************************************************
  128. Misc. System Dependent Functions
  129. *****************************************************************************}
  130. procedure haltproc(e:longint);cdecl;external name '_haltproc';
  131. procedure System_exit;noreturn;
  132. begin
  133. {$ifdef FPC_HAS_FEATURE_EXITCODE}
  134. haltproc(ExitCode);
  135. {$else FPC_HAS_FEATURE_EXITCODE}
  136. haltproc(0);
  137. {$endif FPC_HAS_FEATURE_EXITCODE}
  138. End;
  139. {$ifdef FPC_HAS_FEATURE_PROCESSES}
  140. function GetProcessID: SizeUInt;
  141. begin
  142. GetProcessID := 0;
  143. end;
  144. {$endif}
  145. {$ifdef FPC_HAS_FEATURE_COMMANDARGS}
  146. Function ParamCount: Longint;
  147. Begin
  148. Paramcount:=argc-1
  149. End;
  150. function paramstr(l: longint) : string;
  151. begin
  152. paramstr := '';
  153. end;
  154. {$endif FPC_HAS_FEATURE_COMMANDARGS}
  155. const
  156. QRAN_SHIFT = 15;
  157. QRAN_MASK = ((1 shl QRAN_SHIFT) - 1);
  158. QRAN_MAX = QRAN_MASK;
  159. QRAN_A = 1664525;
  160. QRAN_C = 1013904223;
  161. {$ifdef FPC_HAS_FEATURE_RANDOM}
  162. procedure randomize();
  163. begin
  164. RandSeed := 63458;
  165. end;
  166. procedure randomize(value: integer);
  167. begin
  168. RandSeed := value;
  169. end;
  170. function random(): integer;
  171. begin
  172. RandSeed := QRAN_A * RandSeed + QRAN_C;
  173. random := (RandSeed shr 16) and QRAN_MAX;
  174. end;
  175. function random(value: integer): integer;
  176. var
  177. a: integer;
  178. begin
  179. RandSeed := QRAN_A * RandSeed + QRAN_C;
  180. a := (RandSeed shr 16) and QRAN_MAX;
  181. random := (a * value) shr 15;
  182. end;
  183. {$endif FPC_HAS_FEATURE_RANDOM}
  184. {*****************************************************************************
  185. SystemUnit Initialization
  186. *****************************************************************************}
  187. {$ifdef FPC_HAS_FEATURE_STACKCHECK}
  188. function CheckInitialStkLen(stklen : SizeUInt) : SizeUInt;inline;
  189. begin
  190. result := stklen;
  191. end;
  192. var
  193. initialstkptr : Pointer; // external name '__stkptr';
  194. {$endif FPC_HAS_FEATURE_STACKCHECK}
  195. begin
  196. { FPU (hard or soft) is initialized from fpc_cpuinit, which is included
  197. per-cpu unconditionally.
  198. SysResetFPU;
  199. if not(IsLibrary) then
  200. SysInitFPU;
  201. }
  202. {$ifdef FPC_HAS_FEATURE_CONSOLEIO}
  203. IsConsole := TRUE;
  204. {$endif FPC_HAS_FEATURE_CONSOLEIO}
  205. {$ifdef FPC_HAS_FEATURE_STACKCHECK}
  206. StackLength := CheckInitialStkLen(initialStkLen);
  207. StackBottom := initialstkptr - StackLength;
  208. {$endif FPC_HAS_FEATURE_STACKCHECK}
  209. {$ifdef FPC_HAS_FEATURE_EXCEPTIONS}
  210. { SysInitExceptions initializes only ExceptObjectstack and ExceptAddrStack
  211. with nil since both are located in the bss section, they are zeroed at startup
  212. anyways so not calling SysInitExceptions saves some bytes for simple programs. Even for threaded
  213. programs this does not matter because in the main thread, the variables are located
  214. in bss
  215. SysInitExceptions;
  216. }
  217. {$endif FPC_HAS_FEATURE_EXCEPTIONS}
  218. {$ifdef FPC_HAS_FEATURE_CONSOLEIO}
  219. { Reset IO Error }
  220. InOutRes:=0;
  221. {$endif FPC_HAS_FEATURE_CONSOLEIO}
  222. {$ifdef FPC_HAS_FEATURE_THREADING}
  223. { threading }
  224. //InitSystemThreads; // Empty call for embedded anyway
  225. {$endif FPC_HAS_FEATURE_THREADING}
  226. {$ifdef FPC_HAS_FEATURE_WIDESTRINGS}
  227. // initunicodestringmanager;
  228. {$endif FPC_HAS_FEATURE_WIDESTRINGS}
  229. end.