system.pp 7.8 KB

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