system.pp 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  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_SYSTEM_HAS_STACKTOP}
  17. {$define FPC_IS_SYSTEM}
  18. {$define HAS_CMDLINE}
  19. { currently, the avr compiler cannot compile complex procedures especially dealing with int64
  20. which are probably anyways rarely used on avr
  21. meanwhile, the avr compiler can deal with them (FK) }
  22. {$ifdef CPUAVR}
  23. { $define EXCLUDE_COMPLEX_PROCS}
  24. {$endif CPUAVR}
  25. { $define USE_NOTHREADMANAGER}
  26. {$define DISABLE_NO_THREAD_MANAGER}
  27. { Do not use standard memory manager }
  28. {$define HAS_MEMORYMANAGER}
  29. {$define FPC_NO_DEFAULT_HEAP}
  30. {$define FPC_ANSI_TEXTFILEREC}
  31. { make output and stdout as well as input and stdin equal to save memory }
  32. {$define FPC_STDOUT_TRUE_ALIAS}
  33. {$ifdef CPUI8086}
  34. {$DEFINE FPC_INCLUDE_SOFTWARE_MUL}
  35. {$DEFINE FPC_INCLUDE_SOFTWARE_MOD_DIV}
  36. {$endif CPUI8086}
  37. {$I check.inc}
  38. {$I systemh.inc}
  39. const
  40. {$ifdef FPC_HAS_FEATURE_TEXTIO}
  41. LineEnding = #10;
  42. {$endif FPC_HAS_FEATURE_TEXTIO}
  43. {$ifdef FPC_HAS_FEATURE_FILEIO}
  44. LFNSupport = true;
  45. DirectorySeparator = '/';
  46. DriveSeparator = ':';
  47. ExtensionSeparator = '.';
  48. PathSeparator = ':';
  49. AllowDirectorySeparators : set of char = ['\','/'];
  50. AllowDriveSeparators : set of char = [':'];
  51. {$endif FPC_HAS_FEATURE_FILEIO}
  52. { FileNameCaseSensitive and FileNameCasePreserving are defined below! }
  53. {$ifdef FPC_HAS_FEATURE_EXITCODE}
  54. maxExitCode = 255;
  55. {$endif FPC_HAS_FEATURE_EXITCODE}
  56. {$ifdef FPC_HAS_FEATURE_FILEIO}
  57. MaxPathLen = 1024; // BSDs since 1993, Solaris 10, Darwin
  58. AllFilesMask = '*';
  59. UnusedHandle = -1;
  60. StdInputHandle = 0;
  61. StdOutputHandle = 1;
  62. StdErrorHandle = 2;
  63. FileNameCaseSensitive : boolean = true;
  64. FileNameCasePreserving: boolean = true;
  65. {$endif FPC_HAS_FEATURE_FILEIO}
  66. {$ifdef FPC_HAS_FEATURE_TEXTIO}
  67. CtrlZMarksEOF: boolean = false; (* #26 not considered as end of file *)
  68. sLineBreak = LineEnding;
  69. DefaultTextLineBreakStyle : TTextLineBreakStyle = tlbsCrLF;
  70. {$endif FPC_HAS_FEATURE_TEXTIO}
  71. {$ifdef CPUI8086}
  72. { The value that needs to be added to the segment to move the pointer by
  73. 64K bytes (BP7 compatibility) }
  74. SelectorInc: Word = $1000;
  75. {$endif CPUI8086}
  76. type
  77. trtl_do_close = procedure (handle : longint);
  78. trtl_do_erase = procedure (p : pchar);
  79. trtl_do_rename = procedure (p1,p2 : pchar);
  80. trtl_do_write = function (h: longint; addr: pointer; len: longint) : longint;
  81. trtl_do_read = function (h: longint; addr: pointer; len: longint) : longint;
  82. trtl_do_filepos = function (handle: longint) : longint;
  83. trtl_do_seek = procedure (handle, pos: longint);
  84. trtl_do_seekend = function (handle: longint):longint;
  85. trtl_do_filesize = function (handle : longint) : longint;
  86. trtl_do_truncate = procedure (handle, pos: longint);
  87. trtl_do_open = procedure (var f;p:pchar;flags:longint);
  88. trtl_do_isdevice = function (handle: longint): boolean;
  89. var
  90. rtl_do_close : trtl_do_close = nil;
  91. rtl_do_erase : trtl_do_erase = nil;
  92. rtl_do_rename : trtl_do_rename = nil;
  93. rtl_do_write : trtl_do_write = nil;
  94. rtl_do_read : trtl_do_read = nil;
  95. rtl_do_filepos : trtl_do_filepos = nil;
  96. rtl_do_seek : trtl_do_seek = nil;
  97. rtl_do_seekend : trtl_do_seekend = nil;
  98. rtl_do_filesize : trtl_do_filesize = nil;
  99. rtl_do_truncate : trtl_do_truncate = nil;
  100. rtl_do_open : trtl_do_open = nil;
  101. rtl_do_isdevice : trtl_do_isdevice = nil;
  102. {$ifdef FPC_HAS_FEATURE_COMMANDARGS}
  103. var
  104. argc: LongInt = 0;
  105. argv: PPChar = nil;
  106. envp: PPChar = nil;
  107. cmdline: PChar = nil;
  108. {$endif FPC_HAS_FEATURE_COMMANDARGS}
  109. {$ifndef FPUNONE}
  110. {$ifdef FPC_HAS_FEATURE_SOFTFPU}
  111. {$define fpc_softfpu_interface}
  112. {$i softfpu.pp}
  113. {$undef fpc_softfpu_interface}
  114. {$endif FPC_HAS_FEATURE_SOFTFPU}
  115. {$endif FPUNONE}
  116. {$ifdef CPUI8086}
  117. var
  118. { Mem[] support }
  119. mem : array[0..$7fff-1] of byte absolute $0:$0;
  120. memw : array[0..($7fff div sizeof(word))-1] of word absolute $0:$0;
  121. meml : array[0..($7fff div sizeof(longint))-1] of longint absolute $0:$0;
  122. __stkbottom : pointer;public name '__stkbottom';
  123. {$endif CPUI8086}
  124. {*****************************************************************************}
  125. implementation
  126. {*****************************************************************************}
  127. {$ifdef CPUI8086}
  128. { used for an offset fixup for accessing the proc parameters in asm routines
  129. that use nostackframe. We can't use the parameter name directly, because
  130. i8086 doesn't support sp relative addressing. }
  131. const
  132. {$ifdef FPC_X86_CODE_FAR}
  133. extra_param_offset = 2;
  134. {$else FPC_X86_CODE_FAR}
  135. extra_param_offset = 0;
  136. {$endif FPC_X86_CODE_FAR}
  137. {$if defined(FPC_X86_DATA_FAR) or defined(FPC_X86_DATA_HUGE)}
  138. extra_data_offset = 2;
  139. {$else}
  140. extra_data_offset = 0;
  141. {$endif}
  142. {$endif CPUI8086}
  143. { Include ELF resources }
  144. const calculated_cmdline:Pchar=nil;
  145. {$ifndef FPUNONE}
  146. {$ifdef FPC_HAS_FEATURE_SOFTFPU}
  147. {$define fpc_softfpu_implementation}
  148. {$i softfpu.pp}
  149. {$undef fpc_softfpu_implementation}
  150. { we get these functions and types from the softfpu code }
  151. {$define FPC_SYSTEM_HAS_float64}
  152. {$define FPC_SYSTEM_HAS_float32}
  153. {$define FPC_SYSTEM_HAS_flag}
  154. {$define FPC_SYSTEM_HAS_extractFloat64Frac0}
  155. {$define FPC_SYSTEM_HAS_extractFloat64Frac1}
  156. {$define FPC_SYSTEM_HAS_extractFloat64Exp}
  157. {$define FPC_SYSTEM_HAS_extractFloat64Frac}
  158. {$define FPC_SYSTEM_HAS_extractFloat64Sign}
  159. {$define FPC_SYSTEM_HAS_ExtractFloat32Frac}
  160. {$define FPC_SYSTEM_HAS_extractFloat32Exp}
  161. {$define FPC_SYSTEM_HAS_extractFloat32Sign}
  162. {$endif FPC_HAS_FEATURE_SOFTFPU}
  163. {$endif FPUNONE}
  164. {$define FPC_SYSTEM_EXIT_NO_RETURN}
  165. {$I system.inc}
  166. {*****************************************************************************
  167. Misc. System Dependent Functions
  168. *****************************************************************************}
  169. var
  170. _stack_top: record end; external name '_stack_top';
  171. function StackTop: pointer;
  172. begin
  173. StackTop:=@_stack_top;
  174. end;
  175. procedure haltproc;cdecl;external name '_haltproc';
  176. procedure System_exit;noreturn;external name '_haltproc';
  177. {$ifdef FPC_HAS_FEATURE_PROCESSES}
  178. function GetProcessID: SizeUInt;
  179. begin
  180. GetProcessID := 0;
  181. end;
  182. {$endif}
  183. {$ifdef FPC_HAS_FEATURE_COMMANDARGS}
  184. Function ParamCount: Longint;
  185. Begin
  186. Paramcount:=argc-1
  187. End;
  188. function paramstr(l: longint) : string;
  189. begin
  190. paramstr := '';
  191. end;
  192. {$endif FPC_HAS_FEATURE_COMMANDARGS}
  193. {$ifdef FPC_HAS_FEATURE_RANDOM}
  194. procedure randomize();
  195. begin
  196. RandSeed := 63458;
  197. end;
  198. {$endif FPC_HAS_FEATURE_RANDOM}
  199. {*****************************************************************************
  200. SystemUnit Initialization
  201. *****************************************************************************}
  202. {$ifdef FPC_HAS_FEATURE_STACKCHECK}
  203. function CheckInitialStkLen(stklen : SizeUInt) : SizeUInt;inline;
  204. begin
  205. result := stklen;
  206. end;
  207. var
  208. initialstkptr : record end; external name '_stack_top';
  209. {$endif FPC_HAS_FEATURE_STACKCHECK}
  210. begin
  211. { FPU (hard or soft) is initialized from fpc_cpuinit, which is included
  212. per-cpu unconditionally.
  213. SysResetFPU;
  214. if not(IsLibrary) then
  215. SysInitFPU;
  216. }
  217. {$ifdef FPC_HAS_FEATURE_CONSOLEIO}
  218. IsConsole := TRUE;
  219. {$endif FPC_HAS_FEATURE_CONSOLEIO}
  220. {$ifdef FPC_HAS_FEATURE_STACKCHECK}
  221. StackLength := CheckInitialStkLen(initialStkLen);
  222. StackBottom := @initialstkptr - StackLength;
  223. {$endif FPC_HAS_FEATURE_STACKCHECK}
  224. {$ifdef FPC_HAS_FEATURE_EXCEPTIONS}
  225. { SysInitExceptions initializes only ExceptObjectstack and ExceptAddrStack
  226. with nil since both are located in the bss section, they are zeroed at startup
  227. anyways so not calling SysInitExceptions saves some bytes for simple programs. Even for threaded
  228. programs this does not matter because in the main thread, the variables are located
  229. in bss
  230. SysInitExceptions;
  231. }
  232. {$endif FPC_HAS_FEATURE_EXCEPTIONS}
  233. {$ifdef FPC_HAS_FEATURE_CONSOLEIO}
  234. { Reset IO Error }
  235. InOutRes:=0;
  236. {$endif FPC_HAS_FEATURE_CONSOLEIO}
  237. {$ifdef FPC_HAS_FEATURE_THREADING}
  238. { threading }
  239. InitSystemThreads;
  240. {$endif FPC_HAS_FEATURE_THREADING}
  241. {$ifdef FPC_HAS_FEATURE_WIDESTRINGS}
  242. // initunicodestringmanager;
  243. {$endif FPC_HAS_FEATURE_WIDESTRINGS}
  244. end.