system.pp 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  1. {
  2. This file is part of the Free Pascal run time library.
  3. Copyright (c) 2023 by Karoly Balogh
  4. System unit for the Human 68k (Sharp X68000)
  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. interface
  13. {$define FPC_IS_SYSTEM}
  14. {$define FPC_STDOUT_TRUE_ALIAS}
  15. {$define FPC_ANSI_TEXTFILEREC}
  16. {$define FPC_SYSTEM_EXIT_NO_RETURN}
  17. {$.define FPC_HUMAN68K_USE_TINYHEAP}
  18. {$ifdef FPC_HUMAN68K_USE_TINYHEAP}
  19. {$define HAS_MEMORYMANAGER}
  20. {$endif FPC_HUMAN68K_USE_TINYHEAP}
  21. {$i systemh.inc}
  22. {$ifdef FPC_HUMAN68K_USE_TINYHEAP}
  23. {$i tnyheaph.inc}
  24. {$endif FPC_HUMAN68K_USE_TINYHEAP}
  25. {Platform specific information}
  26. const
  27. LineEnding = #13#10;
  28. LFNSupport = false;
  29. CtrlZMarksEOF: boolean = false; (* #26 not considered as end of file *)
  30. DirectorySeparator = '\';
  31. DriveSeparator = ':';
  32. ExtensionSeparator = '.';
  33. PathSeparator = ';';
  34. AllowDirectorySeparators : set of char = ['\','/'];
  35. AllowDriveSeparators : set of char = [':'];
  36. FileNameCaseSensitive = false;
  37. FileNameCasePreserving = false;
  38. maxExitCode = 255;
  39. MaxPathLen = 255;
  40. AllFilesMask = '*.*';
  41. sLineBreak = LineEnding;
  42. DefaultTextLineBreakStyle : TTextLineBreakStyle = tlbsCRLF;
  43. const
  44. UnusedHandle = -1;
  45. StdInputHandle: longint = 0;
  46. StdOutputHandle: longint = 1;
  47. StdErrorHandle: longint = 2;
  48. var
  49. args: PChar;
  50. argc: LongInt;
  51. argv: PPChar;
  52. envp: PPChar;
  53. {$if defined(FPUSOFT)}
  54. {$define fpc_softfpu_interface}
  55. {$i softfpu.pp}
  56. {$undef fpc_softfpu_interface}
  57. {$endif defined(FPUSOFT)}
  58. implementation
  59. {$if defined(FPUSOFT)}
  60. {$define fpc_softfpu_implementation}
  61. {$define softfpu_compiler_mul32to64}
  62. {$define softfpu_inline}
  63. {$i softfpu.pp}
  64. {$undef fpc_softfpu_implementation}
  65. { we get these functions and types from the softfpu code }
  66. {$define FPC_SYSTEM_HAS_float64}
  67. {$define FPC_SYSTEM_HAS_float32}
  68. {$define FPC_SYSTEM_HAS_flag}
  69. {$define FPC_SYSTEM_HAS_extractFloat64Frac0}
  70. {$define FPC_SYSTEM_HAS_extractFloat64Frac1}
  71. {$define FPC_SYSTEM_HAS_extractFloat64Exp}
  72. {$define FPC_SYSTEM_HAS_extractFloat64Sign}
  73. {$define FPC_SYSTEM_HAS_ExtractFloat32Frac}
  74. {$define FPC_SYSTEM_HAS_extractFloat32Exp}
  75. {$define FPC_SYSTEM_HAS_extractFloat32Sign}
  76. {$endif defined(FPUSOFT)}
  77. {$i system.inc}
  78. {$ifdef FPC_HUMAN68K_USE_TINYHEAP}
  79. {$i tinyheap.inc}
  80. {$endif FPC_HUMAN68K_USE_TINYHEAP}
  81. function GetProcessID:SizeUInt;
  82. begin
  83. GetProcessID := 1;
  84. end;
  85. var
  86. h68k_startup: Th68kdos_startup; external name '_h68k_startup';
  87. h68k_psp: Ph68kdos_psp; external name '_h68k_psp';
  88. {$ifdef FPC_HUMAN68K_USE_TINYHEAP}
  89. initial_heap_start: pointer; public name '__initial_heap_start';
  90. initial_heap_end: pointer; public name '__initial_heap_end';
  91. {$endif FPC_HUMAN68K_USE_TINYHEAP}
  92. {*****************************************************************************
  93. ParamStr
  94. *****************************************************************************}
  95. { number of args }
  96. function ParamCount: LongInt;
  97. begin
  98. ParamCount:=argc;
  99. end;
  100. { argument number l }
  101. function ParamStr(l: LongInt): shortstring;
  102. begin
  103. if assigned(argv) and (l >= 0) and (l <= argc) then
  104. ParamStr:=argv[l]
  105. else
  106. ParamStr:='';
  107. end;
  108. procedure GenerateArgs;
  109. var
  110. argcc: longint;
  111. argl: longint;
  112. p: pchar;
  113. argsp: pchar;
  114. inquotes: boolean;
  115. inarg: boolean;
  116. begin
  117. argc:=0;
  118. inquotes:=false;
  119. inarg:=false;
  120. p:=@h68k_startup.comm^.buffer;
  121. if not assigned(p) then
  122. exit;
  123. argl:=h68k_startup.comm^.len;
  124. if argl < 1 then
  125. argl:=1;
  126. args:=getmem(argl+1);
  127. fillchar(args^,argl,#0);
  128. argsp:=args;
  129. while p^ <> #0 do
  130. begin
  131. case p^ of
  132. ' ':
  133. begin
  134. if not inquotes then
  135. begin
  136. if inarg then
  137. begin
  138. inc(argc);
  139. inarg:=false;
  140. end;
  141. argsp^:=#0;
  142. end
  143. else
  144. argsp^:=p^;
  145. inc(argsp);
  146. end;
  147. '"':
  148. begin
  149. inquotes:=not inquotes;
  150. end;
  151. else
  152. begin
  153. inarg:=true;
  154. argsp^:=p^;
  155. inc(argsp);
  156. end;
  157. end;
  158. inc(p);
  159. end;
  160. if inarg then
  161. inc(argc);
  162. argv:=GetMem(argc+1);
  163. argsp:=args;
  164. argcc:=0;
  165. inarg:=false;
  166. while (argsp < (args + argl)) and (argcc < argc) do
  167. begin
  168. if argsp^ = #0 then
  169. inarg:=false
  170. else
  171. if not inarg then
  172. begin
  173. inarg:=true;
  174. argv[argcc+1]:=argsp;
  175. inc(argcc);
  176. end;
  177. inc(argsp);
  178. end;
  179. argl:=length(h68k_psp^.exe_path)+length(h68k_psp^.exe_name);
  180. argv[0]:=GetMem(argl+1);
  181. MoveChar0(h68k_psp^.exe_path[0],argv[0][0],argl);
  182. MoveChar0(h68k_psp^.exe_name[0],argv[0][length(argv[0])],argl);
  183. end;
  184. procedure SysInitParamsAndEnv;
  185. begin
  186. GenerateArgs;
  187. end;
  188. procedure randomize;
  189. begin
  190. {$WARNING: randseed is uninitialized}
  191. randseed:=0;
  192. end;
  193. {*****************************************************************************
  194. System Dependent Exit code
  195. *****************************************************************************}
  196. procedure haltproc(e:longint); noreturn; external name '_haltproc';
  197. Procedure system_exit; noreturn;
  198. begin
  199. haltproc(ExitCode);
  200. end;
  201. {*****************************************************************************
  202. System Unit Initialization
  203. *****************************************************************************}
  204. {$ifdef FPC_HUMAN68K_USE_TINYHEAP}
  205. procedure InitHeap;
  206. var
  207. aligned_heap_start: pointer;
  208. begin
  209. aligned_heap_start:=align(initial_heap_start,sizeof(ttinyheapblock));
  210. RegisterTinyHeapBlock_Simple_Prealigned(aligned_heap_start, ptruint(initial_heap_end - aligned_heap_start));
  211. end;
  212. {$endif FPC_HUMAN68K_USE_TINYHEAP}
  213. procedure SysInitStdIO;
  214. begin
  215. OpenStdIO(Input,fmInput,StdInputHandle);
  216. OpenStdIO(Output,fmOutput,StdOutputHandle);
  217. OpenStdIO(ErrOutput,fmOutput,StdErrorHandle);
  218. {$ifndef FPC_STDOUT_TRUE_ALIAS}
  219. OpenStdIO(StdOut,fmOutput,StdOutputHandle);
  220. OpenStdIO(StdErr,fmOutput,StdErrorHandle);
  221. {$endif FPC_STDOUT_TRUE_ALIAS}
  222. end;
  223. function CheckInitialStkLen (StkLen: SizeUInt): SizeUInt;
  224. begin
  225. CheckInitialStkLen := StkLen;
  226. end;
  227. begin
  228. StackLength := CheckInitialStkLen (InitialStkLen);
  229. { Initialize ExitProc }
  230. ExitProc:=Nil;
  231. { Setup heap }
  232. InitHeap;
  233. SysInitExceptions;
  234. {$ifdef FPC_HAS_FEATURE_UNICODESTRINGS}
  235. InitUnicodeStringManager;
  236. {$endif FPC_HAS_FEATURE_UNICODESTRINGS}
  237. { Setup stdin, stdout and stderr }
  238. SysInitStdIO;
  239. { Reset IO Error }
  240. InOutRes:=0;
  241. { Setup command line arguments }
  242. SysInitParamsAndEnv;
  243. {$ifdef FPC_HAS_FEATURE_THREADING}
  244. InitSystemThreads;
  245. {$endif FPC_HAS_FEATURE_THREADING}
  246. end.