system.pp 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  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. Platform Specific Helpers
  95. *****************************************************************************}
  96. function h68kdos_exec0(const fil: pchar; p1: pointer; p2: pointer): longint; assembler; public name '_fpc_h68kdos_exec0';
  97. asm
  98. movem.l d2-d7/a2-a6,-(sp)
  99. move.l p2,-(sp)
  100. move.l p1,-(sp)
  101. move.l fil,-(sp)
  102. move.w #0,-(sp)
  103. dc.w $ff4b
  104. lea.l 14(sp),sp
  105. movem.l (sp)+,d2-d7/a2-a6
  106. end;
  107. {*****************************************************************************
  108. ParamStr
  109. *****************************************************************************}
  110. { number of args }
  111. function ParamCount: LongInt;
  112. begin
  113. ParamCount:=argc;
  114. end;
  115. { argument number l }
  116. function ParamStr(l: LongInt): shortstring;
  117. begin
  118. if assigned(argv) and (l >= 0) and (l <= argc) then
  119. ParamStr:=argv[l]
  120. else
  121. ParamStr:='';
  122. end;
  123. procedure GenerateArgs;
  124. var
  125. argcc: longint;
  126. argl,pathlen,namelen: longint;
  127. p: pchar;
  128. argsp: pchar;
  129. inquotes: boolean;
  130. inarg: boolean;
  131. i: longint;
  132. begin
  133. inquotes:=false;
  134. inarg:=false;
  135. if not assigned(h68k_startup.comm) then
  136. exit;
  137. p:=@h68k_startup.comm^.buffer;
  138. argl:=h68k_startup.comm^.len;
  139. args:=GetMem(argl+1);
  140. if not assigned(args) then
  141. exit;
  142. fillchar(args^,argl+1,#0);
  143. argsp:=args;
  144. for i:=0 to argl-1 do
  145. begin
  146. case p[i] of
  147. ' ':
  148. begin
  149. if not inquotes then
  150. begin
  151. if inarg then
  152. begin
  153. inc(argc);
  154. inarg:=false;
  155. end;
  156. argsp^:=#0;
  157. end
  158. else
  159. argsp^:=p[i];
  160. inc(argsp);
  161. end;
  162. '"':
  163. begin
  164. inquotes:=not inquotes;
  165. end;
  166. else
  167. begin
  168. inarg:=true;
  169. argsp^:=p[i];
  170. inc(argsp);
  171. end;
  172. end;
  173. end;
  174. if inarg then
  175. inc(argc);
  176. argv:=GetMem(argc+1);
  177. if not assigned(argv) then
  178. exit;
  179. argsp:=args;
  180. argcc:=0;
  181. inarg:=false;
  182. while (argsp < (args + argl)) and (argcc < argc) do
  183. begin
  184. if argsp^ = #0 then
  185. inarg:=false
  186. else
  187. if not inarg then
  188. begin
  189. inarg:=true;
  190. argv[argcc+1]:=argsp;
  191. inc(argcc);
  192. end;
  193. inc(argsp);
  194. end;
  195. pathlen:=strlen(h68k_psp^.exe_path);
  196. namelen:=strlen(h68k_psp^.exe_name);
  197. argl:=pathlen+namelen;
  198. argv[0]:=GetMem(argl+1);
  199. if not assigned(argv[0]) then
  200. exit;
  201. Move(h68k_psp^.exe_path[0],argv[0][0],pathlen);
  202. Move(h68k_psp^.exe_name[0],argv[0][pathlen],namelen);
  203. argv[0][argl]:=#0;
  204. end;
  205. procedure SysInitParamsAndEnv;
  206. begin
  207. GenerateArgs;
  208. end;
  209. procedure randomize;
  210. begin
  211. {$WARNING: randseed is uninitialized}
  212. randseed:=0;
  213. end;
  214. {*****************************************************************************
  215. System Dependent Exit code
  216. *****************************************************************************}
  217. procedure haltproc(e:longint); noreturn; external name '_haltproc';
  218. Procedure system_exit; noreturn;
  219. begin
  220. haltproc(ExitCode);
  221. end;
  222. {*****************************************************************************
  223. System Unit Initialization
  224. *****************************************************************************}
  225. {$ifdef FPC_HUMAN68K_USE_TINYHEAP}
  226. procedure InitHeap;
  227. var
  228. aligned_heap_start: pointer;
  229. begin
  230. aligned_heap_start:=align(initial_heap_start,sizeof(ttinyheapblock));
  231. RegisterTinyHeapBlock_Simple_Prealigned(aligned_heap_start, ptruint(initial_heap_end - aligned_heap_start));
  232. end;
  233. {$endif FPC_HUMAN68K_USE_TINYHEAP}
  234. procedure SysInitStdIO;
  235. begin
  236. OpenStdIO(Input,fmInput,StdInputHandle);
  237. OpenStdIO(Output,fmOutput,StdOutputHandle);
  238. OpenStdIO(ErrOutput,fmOutput,StdErrorHandle);
  239. {$ifndef FPC_STDOUT_TRUE_ALIAS}
  240. OpenStdIO(StdOut,fmOutput,StdOutputHandle);
  241. OpenStdIO(StdErr,fmOutput,StdErrorHandle);
  242. {$endif FPC_STDOUT_TRUE_ALIAS}
  243. end;
  244. function CheckInitialStkLen (StkLen: SizeUInt): SizeUInt;
  245. begin
  246. CheckInitialStkLen := StkLen;
  247. end;
  248. begin
  249. human68k_vernum:=word(h68kdos_vernum);
  250. StackLength := CheckInitialStkLen (InitialStkLen);
  251. { Initialize ExitProc }
  252. ExitProc:=Nil;
  253. { Setup heap }
  254. InitHeap;
  255. SysInitExceptions;
  256. {$ifdef FPC_HAS_FEATURE_UNICODESTRINGS}
  257. InitUnicodeStringManager;
  258. {$endif FPC_HAS_FEATURE_UNICODESTRINGS}
  259. { Setup stdin, stdout and stderr }
  260. SysInitStdIO;
  261. { Reset IO Error }
  262. InOutRes:=0;
  263. { Setup command line arguments }
  264. SysInitParamsAndEnv;
  265. {$ifdef FPC_HAS_FEATURE_THREADING}
  266. InitSystemThreads;
  267. {$endif FPC_HAS_FEATURE_THREADING}
  268. end.