system.pp 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  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. human68k_vernum: word;
  54. {$if defined(FPUSOFT)}
  55. {$define fpc_softfpu_interface}
  56. {$i softfpu.pp}
  57. {$undef fpc_softfpu_interface}
  58. {$endif defined(FPUSOFT)}
  59. implementation
  60. {$if defined(FPUSOFT)}
  61. {$define fpc_softfpu_implementation}
  62. {$define softfpu_compiler_mul32to64}
  63. {$define softfpu_inline}
  64. {$i softfpu.pp}
  65. {$undef fpc_softfpu_implementation}
  66. { we get these functions and types from the softfpu code }
  67. {$define FPC_SYSTEM_HAS_float64}
  68. {$define FPC_SYSTEM_HAS_float32}
  69. {$define FPC_SYSTEM_HAS_flag}
  70. {$define FPC_SYSTEM_HAS_extractFloat64Frac0}
  71. {$define FPC_SYSTEM_HAS_extractFloat64Frac1}
  72. {$define FPC_SYSTEM_HAS_extractFloat64Exp}
  73. {$define FPC_SYSTEM_HAS_extractFloat64Sign}
  74. {$define FPC_SYSTEM_HAS_ExtractFloat32Frac}
  75. {$define FPC_SYSTEM_HAS_extractFloat32Exp}
  76. {$define FPC_SYSTEM_HAS_extractFloat32Sign}
  77. {$endif defined(FPUSOFT)}
  78. {$i system.inc}
  79. {$ifdef FPC_HUMAN68K_USE_TINYHEAP}
  80. {$i tinyheap.inc}
  81. {$endif FPC_HUMAN68K_USE_TINYHEAP}
  82. function GetProcessID:SizeUInt;
  83. begin
  84. GetProcessID := 1;
  85. end;
  86. var
  87. h68k_startup: Th68kdos_startup; external name '_h68k_startup';
  88. h68k_psp: Ph68kdos_psp; external name '_h68k_psp';
  89. {$ifdef FPC_HUMAN68K_USE_TINYHEAP}
  90. initial_heap_start: pointer; public name '__initial_heap_start';
  91. initial_heap_end: pointer; public name '__initial_heap_end';
  92. {$endif FPC_HUMAN68K_USE_TINYHEAP}
  93. {*****************************************************************************
  94. ParamStr
  95. *****************************************************************************}
  96. { number of args }
  97. function ParamCount: LongInt;
  98. begin
  99. ParamCount:=argc;
  100. end;
  101. { argument number l }
  102. function ParamStr(l: LongInt): shortstring;
  103. begin
  104. if assigned(argv) and (l >= 0) and (l <= argc) then
  105. ParamStr:=argv[l]
  106. else
  107. ParamStr:='';
  108. end;
  109. procedure GenerateArgs;
  110. var
  111. argcc: longint;
  112. argl,pathlen,namelen: longint;
  113. p: pchar;
  114. argsp: pchar;
  115. inquotes: boolean;
  116. inarg: boolean;
  117. i: longint;
  118. begin
  119. inquotes:=false;
  120. inarg:=false;
  121. if not assigned(h68k_startup.comm) then
  122. exit;
  123. p:=@h68k_startup.comm^.buffer;
  124. argl:=h68k_startup.comm^.len;
  125. args:=GetMem(argl+1);
  126. if not assigned(args) then
  127. exit;
  128. fillchar(args^,argl+1,#0);
  129. argsp:=args;
  130. for i:=0 to argl-1 do
  131. begin
  132. case p[i] of
  133. ' ':
  134. begin
  135. if not inquotes then
  136. begin
  137. if inarg then
  138. begin
  139. inc(argc);
  140. inarg:=false;
  141. end;
  142. argsp^:=#0;
  143. end
  144. else
  145. argsp^:=p[i];
  146. inc(argsp);
  147. end;
  148. '"':
  149. begin
  150. inquotes:=not inquotes;
  151. end;
  152. else
  153. begin
  154. inarg:=true;
  155. argsp^:=p[i];
  156. inc(argsp);
  157. end;
  158. end;
  159. end;
  160. if inarg then
  161. inc(argc);
  162. argv:=GetMem(argc+1);
  163. if not assigned(argv) then
  164. exit;
  165. argsp:=args;
  166. argcc:=0;
  167. inarg:=false;
  168. while (argsp < (args + argl)) and (argcc < argc) do
  169. begin
  170. if argsp^ = #0 then
  171. inarg:=false
  172. else
  173. if not inarg then
  174. begin
  175. inarg:=true;
  176. argv[argcc+1]:=argsp;
  177. inc(argcc);
  178. end;
  179. inc(argsp);
  180. end;
  181. pathlen:=strlen(h68k_psp^.exe_path);
  182. namelen:=strlen(h68k_psp^.exe_name);
  183. argl:=pathlen+namelen;
  184. argv[0]:=GetMem(argl+1);
  185. if not assigned(argv[0]) then
  186. exit;
  187. Move(h68k_psp^.exe_path[0],argv[0][0],pathlen);
  188. Move(h68k_psp^.exe_name[0],argv[0][pathlen],namelen);
  189. argv[0][argl]:=#0;
  190. end;
  191. procedure SysInitParamsAndEnv;
  192. begin
  193. GenerateArgs;
  194. end;
  195. procedure randomize;
  196. begin
  197. {$WARNING: randseed is uninitialized}
  198. randseed:=0;
  199. end;
  200. {*****************************************************************************
  201. System Dependent Exit code
  202. *****************************************************************************}
  203. procedure haltproc(e:longint); noreturn; external name '_haltproc';
  204. Procedure system_exit; noreturn;
  205. begin
  206. haltproc(ExitCode);
  207. end;
  208. {*****************************************************************************
  209. System Unit Initialization
  210. *****************************************************************************}
  211. {$ifdef FPC_HUMAN68K_USE_TINYHEAP}
  212. procedure InitHeap;
  213. var
  214. aligned_heap_start: pointer;
  215. begin
  216. aligned_heap_start:=align(initial_heap_start,sizeof(ttinyheapblock));
  217. RegisterTinyHeapBlock_Simple_Prealigned(aligned_heap_start, ptruint(initial_heap_end - aligned_heap_start));
  218. end;
  219. {$endif FPC_HUMAN68K_USE_TINYHEAP}
  220. procedure SysInitStdIO;
  221. begin
  222. OpenStdIO(Input,fmInput,StdInputHandle);
  223. OpenStdIO(Output,fmOutput,StdOutputHandle);
  224. OpenStdIO(ErrOutput,fmOutput,StdErrorHandle);
  225. {$ifndef FPC_STDOUT_TRUE_ALIAS}
  226. OpenStdIO(StdOut,fmOutput,StdOutputHandle);
  227. OpenStdIO(StdErr,fmOutput,StdErrorHandle);
  228. {$endif FPC_STDOUT_TRUE_ALIAS}
  229. end;
  230. function CheckInitialStkLen (StkLen: SizeUInt): SizeUInt;
  231. begin
  232. CheckInitialStkLen := StkLen;
  233. end;
  234. begin
  235. human68k_vernum:=word(h68kdos_vernum);
  236. StackLength := CheckInitialStkLen (InitialStkLen);
  237. { Initialize ExitProc }
  238. ExitProc:=Nil;
  239. { Setup heap }
  240. InitHeap;
  241. SysInitExceptions;
  242. {$ifdef FPC_HAS_FEATURE_UNICODESTRINGS}
  243. InitUnicodeStringManager;
  244. {$endif FPC_HAS_FEATURE_UNICODESTRINGS}
  245. { Setup stdin, stdout and stderr }
  246. SysInitStdIO;
  247. { Reset IO Error }
  248. InOutRes:=0;
  249. { Setup command line arguments }
  250. SysInitParamsAndEnv;
  251. {$ifdef FPC_HAS_FEATURE_THREADING}
  252. InitSystemThreads;
  253. {$endif FPC_HAS_FEATURE_THREADING}
  254. end.