system.pp 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  1. {
  2. This file is part of the Free Pascal run time library.
  3. Copyright (c) 2006 by Francesco Lombardi.
  4. System unit for Nintendo DS
  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 HAS_CMDLINE}
  15. {$define FPC_HAS_FEATURE_CONSOLEIO}
  16. {$define FPC_HAS_FEATURE_COMMANDARGS}
  17. {$define FPC_HAS_FEATURE_TEXTIO}
  18. {$define FPC_HAS_FEATURE_FILEIO}
  19. {$define FPC_HAS_FEATURE_THREADING}
  20. {$define CPUARM_HAS_UMULL}
  21. {$define CPUARM_HAS_CLZ}
  22. {$i systemh.inc}
  23. {$i ndsbiosh.inc}
  24. {$i ndsh.inc}
  25. {$i libch.inc}
  26. {$define fpc_softfpu_interface}
  27. {$i softfpu.pp}
  28. {$undef fpc_softfpu_interface}
  29. const
  30. LineEnding = #10;
  31. LFNSupport = true;
  32. DirectorySeparator = '/';
  33. DriveSeparator = ':';
  34. ExtensionSeparator = '.';
  35. PathSeparator = ':';
  36. AllowDirectorySeparators : set of char = ['\','/'];
  37. AllowDriveSeparators : set of char = [':'];
  38. maxExitCode = 255;
  39. MaxPathLen = 1024; // BSDs since 1993, Solaris 10, Darwin
  40. AllFilesMask = '*';
  41. UnusedHandle = -1;
  42. StdInputHandle = 0;
  43. StdOutputHandle = 1;
  44. StdErrorHandle = 2;
  45. FileNameCaseSensitive : boolean = true;
  46. FileNameCasePreserving: boolean = true;
  47. CtrlZMarksEOF: boolean = true; (* #26 not considered as end of file *)
  48. sLineBreak = LineEnding;
  49. DefaultTextLineBreakStyle : TTextLineBreakStyle = tlbsLF;
  50. var
  51. argc: LongInt = 0;
  52. argv: PPChar;
  53. envp: PPChar;
  54. // errno: integer;
  55. fake_heap_end: ^byte; cvar; external;
  56. irq_vector: integer; external name '__irq_vector';
  57. function get_cmdline:Pchar;
  58. property cmdline:Pchar read get_cmdline;
  59. implementation
  60. const
  61. calculated_cmdline: Pchar = nil;
  62. { System limits, POSIX value in parentheses, used for buffer and stack allocation }
  63. ARG_MAX = 65536; {4096} { Maximum number of argument size }
  64. PATH_MAX = 1024; {255} { Maximum number of bytes in pathname }
  65. {$define fpc_softfpu_implementation}
  66. {$i softfpu.pp}
  67. {$undef fpc_softfpu_implementation}
  68. { we get these functions and types from the softfpu code }
  69. {$define FPC_SYSTEM_HAS_float64}
  70. {$define FPC_SYSTEM_HAS_float32}
  71. {$define FPC_SYSTEM_HAS_flag}
  72. {$define FPC_SYSTEM_HAS_extractFloat64Frac0}
  73. {$define FPC_SYSTEM_HAS_extractFloat64Frac1}
  74. {$define FPC_SYSTEM_HAS_extractFloat64Exp}
  75. {$define FPC_SYSTEM_HAS_extractFloat64Sign}
  76. {$define FPC_SYSTEM_HAS_ExtractFloat32Frac}
  77. {$define FPC_SYSTEM_HAS_extractFloat32Exp}
  78. {$define FPC_SYSTEM_HAS_extractFloat32Sign}
  79. {$i system.inc}
  80. {$i ndsbios.inc}
  81. {$i nds.inc}
  82. {$i libc.inc}
  83. {$ifdef FPC_HAS_FEATURE_PROCESSES}
  84. function GetProcessID: SizeUInt;
  85. begin
  86. GetProcessID := 0;
  87. end;
  88. {$endif}
  89. {*****************************************************************************
  90. Misc. System Dependent Functions
  91. *****************************************************************************}
  92. procedure System_exit;
  93. begin
  94. // Boo!
  95. end;
  96. {*****************************************************************************
  97. ParamStr/Randomize
  98. *****************************************************************************}
  99. { set randseed to a new pseudo random value }
  100. procedure randomize;
  101. var
  102. IPC_Timer: array [0..2] of byte absolute $27FF01B;
  103. begin
  104. RandSeed := (IPC_Timer[0] * 3600) + (IPC_Timer[1] * 60) + IPC_Timer[2];
  105. end;
  106. Function ParamCount: Longint;
  107. Begin
  108. Paramcount:=argc-1
  109. End;
  110. { variable where full path and filename and executable is stored }
  111. { is setup by the startup of the system unit. }
  112. var
  113. execpathstr : shortstring;
  114. function paramstr(l: longint) : string;
  115. begin
  116. { stricly conforming POSIX applications }
  117. { have the executing filename as argv[0] }
  118. if l=0 then
  119. begin
  120. paramstr := execpathstr;
  121. end
  122. else
  123. paramstr:=strpas(argv[l]);
  124. end;
  125. {*****************************************************************************
  126. cmdline
  127. *****************************************************************************}
  128. procedure SetupCmdLine;
  129. var
  130. bufsize,
  131. len,j,
  132. size,i : longint;
  133. found : boolean;
  134. buf : pchar;
  135. procedure AddBuf;
  136. begin
  137. reallocmem(calculated_cmdline,size+bufsize);
  138. move(buf^,calculated_cmdline[size],bufsize);
  139. inc(size,bufsize);
  140. bufsize:=0;
  141. end;
  142. begin
  143. if argc<=0 then
  144. exit;
  145. GetMem(buf,ARG_MAX);
  146. size:=0;
  147. bufsize:=0;
  148. i:=0;
  149. while (i<argc) do
  150. begin
  151. len:=strlen(argv[i]);
  152. if len>ARG_MAX-2 then
  153. len:=ARG_MAX-2;
  154. found:=false;
  155. for j:=1 to len do
  156. if argv[i][j]=' ' then
  157. begin
  158. found:=true;
  159. break;
  160. end;
  161. if bufsize+len>=ARG_MAX-2 then
  162. AddBuf;
  163. if found then
  164. begin
  165. buf[bufsize]:='"';
  166. inc(bufsize);
  167. end;
  168. move(argv[i]^,buf[bufsize],len);
  169. inc(bufsize,len);
  170. if found then
  171. begin
  172. buf[bufsize]:='"';
  173. inc(bufsize);
  174. end;
  175. if i<argc-1 then
  176. buf[bufsize]:=' '
  177. else
  178. buf[bufsize]:=#0;
  179. inc(bufsize);
  180. inc(i);
  181. end;
  182. AddBuf;
  183. FreeMem(buf,ARG_MAX);
  184. end;
  185. function get_cmdline:Pchar;
  186. begin
  187. if calculated_cmdline=nil then
  188. setupcmdline;
  189. get_cmdline:=calculated_cmdline;
  190. end;
  191. procedure SysInitStdIO;
  192. begin
  193. OpenStdIO(Input,fmInput,StdInputHandle);
  194. OpenStdIO(Output,fmOutput,StdOutputHandle);
  195. OpenStdIO(ErrOutput,fmOutput,StdErrorHandle);
  196. OpenStdIO(StdOut,fmOutput,StdOutputHandle);
  197. OpenStdIO(StdErr,fmOutput,StdErrorHandle);
  198. end;
  199. function CheckInitialStkLen(stklen : SizeUInt) : SizeUInt;
  200. begin
  201. result := stklen;
  202. end;
  203. begin
  204. StackLength := CheckInitialStkLen(InitialStkLen);
  205. StackBottom := Sptr - StackLength;
  206. { OS specific startup }
  207. { Set up signals handlers }
  208. fpc_cpucodeinit;
  209. { Setup heap }
  210. InitHeap;
  211. SysInitExceptions;
  212. SetupCmdLine;
  213. {$ifdef FPC_HAS_FEATURE_CONSOLEIO}
  214. { Setup stdin, stdout and stderr }
  215. SysInitStdIO;
  216. { Reset IO Error }
  217. InOutRes:=0;
  218. {$endif FPC_HAS_FEATURE_CONSOLEIO}
  219. { Arguments }
  220. {$ifdef FPC_HAS_FEATURE_THREADING}
  221. { threading }
  222. InitSystemThreads;
  223. {$endif FPC_HAS_FEATURE_THREADING}
  224. end.