system.pp 6.6 KB

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