system.pp 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  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
  125. paramstr:=strpas(argv[l]);
  126. end;
  127. {*****************************************************************************
  128. cmdline
  129. *****************************************************************************}
  130. procedure SetupCmdLine;
  131. var
  132. bufsize,
  133. len,j,
  134. size,i : longint;
  135. found : boolean;
  136. buf : pchar;
  137. procedure AddBuf;
  138. begin
  139. reallocmem(calculated_cmdline,size+bufsize);
  140. move(buf^,calculated_cmdline[size],bufsize);
  141. inc(size,bufsize);
  142. bufsize:=0;
  143. end;
  144. begin
  145. if argc<=0 then
  146. exit;
  147. GetMem(buf,ARG_MAX);
  148. size:=0;
  149. bufsize:=0;
  150. i:=0;
  151. while (i<argc) do
  152. begin
  153. len:=strlen(argv[i]);
  154. if len>ARG_MAX-2 then
  155. len:=ARG_MAX-2;
  156. found:=false;
  157. for j:=1 to len do
  158. if argv[i][j]=' ' then
  159. begin
  160. found:=true;
  161. break;
  162. end;
  163. if bufsize+len>=ARG_MAX-2 then
  164. AddBuf;
  165. if found then
  166. begin
  167. buf[bufsize]:='"';
  168. inc(bufsize);
  169. end;
  170. move(argv[i]^,buf[bufsize],len);
  171. inc(bufsize,len);
  172. if found then
  173. begin
  174. buf[bufsize]:='"';
  175. inc(bufsize);
  176. end;
  177. if i<argc-1 then
  178. buf[bufsize]:=' '
  179. else
  180. buf[bufsize]:=#0;
  181. inc(bufsize);
  182. inc(i);
  183. end;
  184. AddBuf;
  185. FreeMem(buf,ARG_MAX);
  186. end;
  187. function get_cmdline:Pchar;
  188. begin
  189. if calculated_cmdline=nil then
  190. setupcmdline;
  191. get_cmdline:=calculated_cmdline;
  192. end;
  193. procedure SysInitStdIO;
  194. begin
  195. OpenStdIO(Input,fmInput,StdInputHandle);
  196. OpenStdIO(Output,fmOutput,StdOutputHandle);
  197. OpenStdIO(ErrOutput,fmOutput,StdErrorHandle);
  198. OpenStdIO(StdOut,fmOutput,StdOutputHandle);
  199. OpenStdIO(StdErr,fmOutput,StdErrorHandle);
  200. end;
  201. function CheckInitialStkLen(stklen : SizeUInt) : SizeUInt;
  202. begin
  203. result := stklen;
  204. end;
  205. begin
  206. StackLength := CheckInitialStkLen(InitialStkLen);
  207. StackBottom := Sptr - StackLength;
  208. { OS specific startup }
  209. { Set up signals handlers }
  210. fpc_cpucodeinit;
  211. { Setup heap }
  212. InitHeap;
  213. SysInitExceptions;
  214. SetupCmdLine;
  215. {$ifdef FPC_HAS_FEATURE_CONSOLEIO}
  216. { Setup stdin, stdout and stderr }
  217. SysInitStdIO;
  218. { Reset IO Error }
  219. InOutRes:=0;
  220. {$endif FPC_HAS_FEATURE_CONSOLEIO}
  221. { Arguments }
  222. {$ifdef FPC_HAS_FEATURE_THREADING}
  223. { threading }
  224. InitSystemThreads;
  225. {$endif FPC_HAS_FEATURE_THREADING}
  226. end.