system.pp 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  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. const
  100. QRAN_SHIFT = 15;
  101. QRAN_MASK = ((1 shl QRAN_SHIFT) - 1);
  102. QRAN_MAX = QRAN_MASK;
  103. QRAN_A = 1664525;
  104. QRAN_C = 1013904223;
  105. { set randseed to a new pseudo random value }
  106. procedure randomize;
  107. var
  108. IPC_Timer: array [0..2] of byte absolute $27FF01B;
  109. begin
  110. RandSeed := (IPC_Timer[0] * 3600) + (IPC_Timer[1] * 60) + IPC_Timer[2];
  111. end;
  112. function random(): integer;
  113. begin
  114. RandSeed := QRAN_A * RandSeed + QRAN_C;
  115. random := (RandSeed shr 16) and QRAN_MAX;
  116. end;
  117. function random(value: integer): integer;
  118. var
  119. a: integer;
  120. begin
  121. RandSeed := QRAN_A * RandSeed + QRAN_C;
  122. a := (RandSeed shr 16) and QRAN_MAX;
  123. random := (a * value) shr 15;
  124. end;
  125. Function ParamCount: Longint;
  126. Begin
  127. Paramcount:=argc-1
  128. End;
  129. { variable where full path and filename and executable is stored }
  130. { is setup by the startup of the system unit. }
  131. var
  132. execpathstr : shortstring;
  133. function paramstr(l: longint) : string;
  134. begin
  135. { stricly conforming POSIX applications }
  136. { have the executing filename as argv[0] }
  137. if l=0 then
  138. begin
  139. paramstr := execpathstr;
  140. end
  141. else
  142. paramstr:=strpas(argv[l]);
  143. end;
  144. {*****************************************************************************
  145. cmdline
  146. *****************************************************************************}
  147. procedure SetupCmdLine;
  148. var
  149. bufsize,
  150. len,j,
  151. size,i : longint;
  152. found : boolean;
  153. buf : pchar;
  154. procedure AddBuf;
  155. begin
  156. reallocmem(calculated_cmdline,size+bufsize);
  157. move(buf^,calculated_cmdline[size],bufsize);
  158. inc(size,bufsize);
  159. bufsize:=0;
  160. end;
  161. begin
  162. if argc<=0 then
  163. exit;
  164. GetMem(buf,ARG_MAX);
  165. size:=0;
  166. bufsize:=0;
  167. i:=0;
  168. while (i<argc) do
  169. begin
  170. len:=strlen(argv[i]);
  171. if len>ARG_MAX-2 then
  172. len:=ARG_MAX-2;
  173. found:=false;
  174. for j:=1 to len do
  175. if argv[i][j]=' ' then
  176. begin
  177. found:=true;
  178. break;
  179. end;
  180. if bufsize+len>=ARG_MAX-2 then
  181. AddBuf;
  182. if found then
  183. begin
  184. buf[bufsize]:='"';
  185. inc(bufsize);
  186. end;
  187. move(argv[i]^,buf[bufsize],len);
  188. inc(bufsize,len);
  189. if found then
  190. begin
  191. buf[bufsize]:='"';
  192. inc(bufsize);
  193. end;
  194. if i<argc-1 then
  195. buf[bufsize]:=' '
  196. else
  197. buf[bufsize]:=#0;
  198. inc(bufsize);
  199. inc(i);
  200. end;
  201. AddBuf;
  202. FreeMem(buf,ARG_MAX);
  203. end;
  204. function get_cmdline:Pchar;
  205. begin
  206. if calculated_cmdline=nil then
  207. setupcmdline;
  208. get_cmdline:=calculated_cmdline;
  209. end;
  210. procedure SysInitStdIO;
  211. begin
  212. OpenStdIO(Input,fmInput,StdInputHandle);
  213. OpenStdIO(Output,fmOutput,StdOutputHandle);
  214. OpenStdIO(ErrOutput,fmOutput,StdErrorHandle);
  215. OpenStdIO(StdOut,fmOutput,StdOutputHandle);
  216. OpenStdIO(StdErr,fmOutput,StdErrorHandle);
  217. end;
  218. function CheckInitialStkLen(stklen : SizeUInt) : SizeUInt;
  219. begin
  220. result := stklen;
  221. end;
  222. begin
  223. StackLength := CheckInitialStkLen(InitialStkLen);
  224. StackBottom := Sptr - StackLength;
  225. { OS specific startup }
  226. { Set up signals handlers }
  227. fpc_cpucodeinit;
  228. { Setup heap }
  229. InitHeap;
  230. SysInitExceptions;
  231. SetupCmdLine;
  232. {$ifdef FPC_HAS_FEATURE_CONSOLEIO}
  233. { Setup stdin, stdout and stderr }
  234. SysInitStdIO;
  235. { Reset IO Error }
  236. InOutRes:=0;
  237. {$endif FPC_HAS_FEATURE_CONSOLEIO}
  238. { Arguments }
  239. {$ifdef FPC_HAS_FEATURE_THREADING}
  240. { threading }
  241. InitSystemThreads;
  242. {$endif FPC_HAS_FEATURE_THREADING}
  243. end.