2
0

system.pp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374
  1. {
  2. This file is part of the Free Pascal run time library.
  3. Copyright (c) 2004-2006 by Karoly Balogh
  4. System unit for AmigaOS 3.x/4.x
  5. Uses parts of the Free Pascal 1.0.x for Commodore Amiga/68k port
  6. by Carl Eric Codere and Nils Sjoholm
  7. See the file COPYING.FPC, included in this distribution,
  8. for details about the copyright.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  12. **********************************************************************}
  13. unit System;
  14. interface
  15. {$define FPC_IS_SYSTEM}
  16. {$define FPC_ANSI_TEXTFILEREC}
  17. {$if defined(AMIGA_V1_0_ONLY) or defined(AMIGA_V1_2_ONLY)}
  18. {$define AMIGA_LEGACY}
  19. {$endif}
  20. {$ifdef AMIGA_LEGACY}
  21. {$.define FPC_AMIGA_USE_TINYHEAP}
  22. {$endif}
  23. {$ifdef FPC_AMIGA_USE_TINYHEAP}
  24. {$define HAS_MEMORYMANAGER}
  25. {$endif FPC_AMIGA_USE_TINYHEAP}
  26. {$I systemh.inc}
  27. {$ifdef FPC_AMIGA_USE_TINYHEAP}
  28. {$i tnyheaph.inc}
  29. {$endif FPC_AMIGA_USE_TINYHEAP}
  30. {$I osdebugh.inc}
  31. {$if defined(cpum68k) and defined(fpusoft)}
  32. {$define fpc_softfpu_interface}
  33. {$i softfpu.pp}
  34. {$undef fpc_softfpu_interface}
  35. {$endif defined(cpum68k) and defined(fpusoft)}
  36. const
  37. {$if defined(AMIGA_V1_0_ONLY)}
  38. AMIGA_OS_MINVERSION = 0;
  39. {$else}
  40. {$if defined(AMIGA_V1_2_ONLY)}
  41. AMIGA_OS_MINVERSION = 33;
  42. {$else}
  43. {$if defined(AMIGA_V2_0_ONLY)}
  44. AMIGA_OS_MINVERSION = 37;
  45. {$else}
  46. {$ifndef cpupowerpc}
  47. AMIGA_OS_MINVERSION = 39;
  48. {$else}
  49. AMIGA_OS_MINVERSION = 50;
  50. {$endif}
  51. {$endif}
  52. {$endif}
  53. {$endif}
  54. const
  55. LineEnding = #10;
  56. LFNSupport = True;
  57. DirectorySeparator = '/';
  58. DriveSeparator = ':';
  59. ExtensionSeparator = '.';
  60. PathSeparator = ';';
  61. AllowDirectorySeparators : set of char = ['\','/'];
  62. AllowDriveSeparators : set of char = [':'];
  63. maxExitCode = 255;
  64. MaxPathLen = 256;
  65. AllFilesMask = '#?';
  66. const
  67. UnusedHandle : LongInt = -1;
  68. StdInputHandle : LongInt = 0;
  69. StdOutputHandle : LongInt = 0;
  70. StdErrorHandle : LongInt = 0;
  71. FileNameCaseSensitive : Boolean = False;
  72. FileNameCasePreserving: boolean = True;
  73. CtrlZMarksEOF: boolean = false; (* #26 not considered as end of file *)
  74. sLineBreak = LineEnding;
  75. DefaultTextLineBreakStyle : TTextLineBreakStyle = tlbsLF;
  76. BreakOn : Boolean = True;
  77. { FIX ME: remove the kludge required by amunits package, which needs a huge rework }
  78. var
  79. AOS_ExecBase : Pointer; external name '_ExecBase';
  80. _ExecBase: Pointer; external name '_ExecBase'; { amunits compatibility kludge }
  81. AOS_DOSBase : Pointer; public name '_DOSBase'; { the "public" part is amunits compatibility kludge }
  82. _DOSBase: Pointer; external name '_DOSBase'; { amunits compatibility kludge }
  83. AOS_UtilityBase: Pointer; public name '_UtilityBase'; { the "public" part is amunits compatibility kludge }
  84. _UtilityBase: Pointer; external name '_UtilityBase'; { amunits compatibility kludge }
  85. AOS_IntuitionBase: Pointer; public name '_IntuitionBase'; { amunits compatibility kludge }
  86. _IntuitionBase: Pointer; external name '_IntuitionBase'; { amunits compatibility kludge }
  87. {$IFDEF AMIGAOS4}
  88. {$WARNING iExec, iDOS and iUtility should be typed pointer later}
  89. var
  90. IExec : Pointer; external name '_IExec';
  91. IDOS : Pointer;
  92. IUtility : Pointer;
  93. {$ENDIF}
  94. ASYS_heapPool : Pointer; { pointer for the OS pool for growing the heap }
  95. ASYS_heapSemaphore: Pointer; { 68k OS from 3.x has no MEMF_SEM_PROTECTED for pools, have to do it ourselves }
  96. ASYS_fileSemaphore: Pointer; { mutex semaphore for filelist access arbitration }
  97. ASYS_origDir : LongInt; { original directory on startup }
  98. AOS_wbMsg : Pointer; public name '_WBenchMsg'; { the "public" part is amunits compatibility kludge }
  99. _WBenchMsg : Pointer; external name '_WBenchMsg'; { amunits compatibility kludge }
  100. AOS_ConName : PChar ='CON:10/30/620/100/FPC Console Output/AUTO/CLOSE/WAIT';
  101. AOS_ConHandle: LongInt;
  102. argc: LongInt;
  103. argv: PPChar;
  104. envp: PPChar;
  105. implementation
  106. {$if defined(cpum68k) and defined(fpusoft)}
  107. {$define fpc_softfpu_implementation}
  108. {$define softfpu_compiler_mul32to64}
  109. {$define softfpu_inline}
  110. {$i softfpu.pp}
  111. {$undef fpc_softfpu_implementation}
  112. { we get these functions and types from the softfpu code }
  113. {$define FPC_SYSTEM_HAS_float64}
  114. {$define FPC_SYSTEM_HAS_float32}
  115. {$define FPC_SYSTEM_HAS_flag}
  116. {$define FPC_SYSTEM_HAS_extractFloat64Frac0}
  117. {$define FPC_SYSTEM_HAS_extractFloat64Frac1}
  118. {$define FPC_SYSTEM_HAS_extractFloat64Exp}
  119. {$define FPC_SYSTEM_HAS_extractFloat64Sign}
  120. {$define FPC_SYSTEM_HAS_ExtractFloat32Frac}
  121. {$define FPC_SYSTEM_HAS_extractFloat32Exp}
  122. {$define FPC_SYSTEM_HAS_extractFloat32Sign}
  123. {$endif defined(cpum68k) and defined(fpusoft)}
  124. {$I system.inc}
  125. {$ifdef FPC_AMIGA_USE_TINYHEAP}
  126. {$i tinyheap.inc}
  127. {$endif FPC_AMIGA_USE_TINYHEAP}
  128. {$I osdebug.inc}
  129. {$IFDEF AMIGAOS4}
  130. // Required to allow opening of utility library interface...
  131. {$include utilf.inc}
  132. {$ENDIF}
  133. {$IFDEF ASYS_FPC_FILEDEBUG}
  134. {$WARNING Compiling with file debug enabled!}
  135. {$ENDIF}
  136. {$IFDEF ASYS_FPC_MEMDEBUG}
  137. {$WARNING Compiling with memory debug enabled!}
  138. {$ENDIF}
  139. type
  140. PWBArg = ^TWBArg;
  141. TWBArg = record
  142. wa_Lock : LongInt; { a lock descriptor }
  143. wa_Name : PChar; { a string relative to that lock }
  144. end;
  145. WBArgList = array[1..MaxInt] of TWBArg; { Only 1..smNumArgs are valid }
  146. PWBArgList = ^WBArgList;
  147. PWBStartup = ^TWBStartup;
  148. TWBStartup = record
  149. sm_Message : TMessage; { a standard message structure }
  150. sm_Process : Pointer; { the process descriptor for you }
  151. sm_Segment : Pointer; { a descriptor for your code }
  152. sm_NumArgs : Longint; { the number of elements in ArgList }
  153. sm_ToolWindow : Pointer; { description of window }
  154. sm_ArgList : PWBArgList; { the arguments themselves }
  155. end;
  156. {*****************************************************************************
  157. Misc. System Dependent Functions
  158. *****************************************************************************}
  159. procedure haltproc(e:longint);cdecl;external name '_haltproc';
  160. procedure System_exit;
  161. var
  162. oldDirLock: LongInt;
  163. begin
  164. { Dispose the thread init/exit chains }
  165. CleanupThreadProcChain(threadInitProcList);
  166. CleanupThreadProcChain(threadExitProcList);
  167. { We must remove the CTRL-C FLAG here because halt }
  168. { may call I/O routines, which in turn might call }
  169. { halt, so a recursive stack crash }
  170. if BreakOn then begin
  171. if (SetSignal(0,0) and SIGBREAKF_CTRL_C)<>0 then
  172. SetSignal(0,SIGBREAKF_CTRL_C);
  173. end;
  174. { Closing opened files }
  175. CloseList(ASYS_fileList);
  176. { Changing back to original directory if changed }
  177. if ASYS_origDir<>0 then begin
  178. oldDirLock:=CurrentDir(ASYS_origDir);
  179. { unlock our lock if its safe, so we won't leak the lock }
  180. if (oldDirLock<>0) and (oldDirLock<>ASYS_origDir) then
  181. Unlock(oldDirLock);
  182. end;
  183. {$IFDEF AMIGAOS4}
  184. if iDOS<>nil then DropInterface(iDOS);
  185. if iUtility<>nil then DropInterface(iUtility);
  186. {$ENDIF}
  187. if AOS_IntuitionBase<>nil then CloseLibrary(AOS_IntuitionBase); { amunits kludge }
  188. if AOS_UtilityBase<>nil then CloseLibrary(AOS_UtilityBase);
  189. if AOS_DOSBase<>nil then CloseLibrary(AOS_DOSBase);
  190. if ASYS_heapPool<>nil then DeletePool(ASYS_heapPool);
  191. { If in Workbench mode, replying WBMsg }
  192. if AOS_wbMsg<>nil then begin
  193. Forbid;
  194. ReplyMsg(AOS_wbMsg);
  195. end;
  196. haltproc(ExitCode);
  197. end;
  198. {*****************************************************************************
  199. Parameterhandling
  200. as include in amicommon
  201. *****************************************************************************}
  202. {$I paramhandling.inc}
  203. {*****************************************************************************
  204. Randomize
  205. *****************************************************************************}
  206. { set randseed to a new pseudo random value }
  207. procedure randomize;
  208. var tmpTime: TDateStamp;
  209. begin
  210. DateStamp(@tmpTime);
  211. randseed:=tmpTime.ds_tick;
  212. end;
  213. { AmigaOS specific startup }
  214. procedure SysInitAmigaOS;
  215. var self: PProcess;
  216. begin
  217. self:=PProcess(FindTask(nil));
  218. if self^.pr_CLI=0 then begin
  219. { if we're running from Ambient/Workbench, we catch its message }
  220. WaitPort(@self^.pr_MsgPort);
  221. AOS_wbMsg:=GetMsg(@self^.pr_MsgPort);
  222. end;
  223. AOS_DOSBase:=OpenLibrary('dos.library',AMIGA_OS_MINVERSION);
  224. if AOS_DOSBase=nil then Halt(1);
  225. {$ifndef AMIGA_LEGACY}
  226. AOS_UtilityBase:=OpenLibrary('utility.library',AMIGA_OS_MINVERSION);
  227. if AOS_UtilityBase=nil then Halt(1);
  228. {$endif}
  229. AOS_IntuitionBase:=OpenLibrary('intuition.library',AMIGA_OS_MINVERSION); { amunits support kludge }
  230. if AOS_IntuitionBase=nil then Halt(1);
  231. {$IFDEF AMIGAOS4}
  232. { Open main interfaces on OS4 }
  233. iDOS := GetInterface(AOS_DOSBase,'main',1,nil);
  234. iUtility := GetInterface(AOS_UtilityBase,'main',1,nil);
  235. {$ENDIF}
  236. { Creating the memory pool for growing heap }
  237. ASYS_heapPool:=CreatePool(MEMF_ANY,growheapsize2,growheapsize1);
  238. if ASYS_heapPool=nil then Halt(1);
  239. ASYS_heapSemaphore:=AllocPooled(ASYS_heapPool,sizeof(TSignalSemaphore));
  240. if ASYS_heapSemaphore = nil then Halt(1);
  241. InitSemaphore(ASYS_heapSemaphore);
  242. { Initialize semaphore for filelist access arbitration }
  243. ASYS_fileSemaphore:=AllocPooled(ASYS_heapPool,sizeof(TSignalSemaphore));
  244. if ASYS_fileSemaphore = nil then Halt(1);
  245. InitSemaphore(ASYS_fileSemaphore);
  246. if AOS_wbMsg=nil then begin
  247. StdInputHandle:=dosInput;
  248. StdOutputHandle:=dosOutput;
  249. StdErrorHandle:=StdOutputHandle;
  250. end else begin
  251. AOS_ConHandle:=Open(AOS_ConName,MODE_OLDFILE);
  252. if AOS_ConHandle<>0 then begin
  253. StdInputHandle:=AOS_ConHandle;
  254. StdOutputHandle:=AOS_ConHandle;
  255. StdErrorHandle:=AOS_ConHandle;
  256. end else
  257. Halt(1);
  258. end;
  259. end;
  260. procedure SysInitStdIO;
  261. begin
  262. OpenStdIO(Input,fmInput,StdInputHandle);
  263. OpenStdIO(Output,fmOutput,StdOutputHandle);
  264. OpenStdIO(ErrOutput,fmOutput,StdErrorHandle);
  265. {$ifndef FPC_STDOUT_TRUE_ALIAS}
  266. OpenStdIO(StdOut,fmOutput,StdOutputHandle);
  267. OpenStdIO(StdErr,fmOutput,StdErrorHandle);
  268. {$endif FPC_STDOUT_TRUE_ALIAS}
  269. end;
  270. function GetProcessID: SizeUInt;
  271. begin
  272. GetProcessID:=SizeUInt(FindTask(NIL));
  273. end;
  274. function CheckInitialStkLen(stklen : SizeUInt) : SizeUInt;
  275. begin
  276. result := stklen;
  277. end;
  278. begin
  279. IsConsole := TRUE;
  280. StackLength := CheckInitialStkLen(InitialStkLen);
  281. StackBottom := StackTop - StackLength;
  282. { OS specific startup }
  283. AOS_wbMsg:=nil;
  284. ASYS_origDir:=0;
  285. ASYS_fileList:=nil;
  286. envp:=nil;
  287. SysInitAmigaOS;
  288. { Set up signals handlers }
  289. // InstallSignals;
  290. {$ifndef FPC_AMIGA_USE_TINYHEAP}
  291. { Setup heap }
  292. InitHeap;
  293. {$endif FPC_AMIGA_USE_TINYHEAP}
  294. SysInitExceptions;
  295. {$ifdef cpum68k}
  296. fpc_cpucodeinit;
  297. {$endif}
  298. initunicodestringmanager;
  299. { Setup stdin, stdout and stderr }
  300. SysInitStdIO;
  301. { Reset IO Error }
  302. InOutRes:=0;
  303. { Arguments }
  304. GenerateArgs;
  305. InitSystemThreads;
  306. {$ifdef FPC_HAS_FEATURE_DYNLIBS}
  307. InitSystemDynLibs;
  308. {$endif}
  309. end.