system.pp 6.3 KB

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