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