helper.inc 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. (*
  2. $Id: helper.inc 25 2007-12-10 21:06:46Z p4p3r0 $
  3. ------------------------------------------------------------------------------
  4. Copyright (C) 2007 Francesco Lombardi
  5. This software is provided 'as-is', without any express or implied
  6. warranty. In no event will the authors be held liable for any
  7. damages arising from the use of this software.
  8. Permission is granted to anyone to use this software for any
  9. purpose, including commercial applications, and to alter it and
  10. redistribute it freely, subject to the following restrictions:
  11. 1. The origin of this software must not be misrepresented; you
  12. must not claim that you wrote the original software. If you use
  13. this software in a product, an acknowledgment in the product
  14. documentation would be appreciated but is not required.
  15. 2. Altered source versions must be plainly marked as such, and
  16. must not be misrepresented as being the original software.
  17. 3. This notice may not be removed or altered from any source
  18. distribution.
  19. ------------------------------------------------------------------------------
  20. Conversion by Legolas (http://itaprogaming.free.fr) for freepascal compiler
  21. (http://www.freepascal.org)
  22. Check http://sourceforge.net/projects/libndsfpc for updates
  23. ------------------------------------------------------------------------------
  24. $Log$
  25. *)
  26. (* +------------------------------------------------------------------------+ *)
  27. (* Begin Random function *)
  28. (* +------------------------------------------------------------------------+ *)
  29. {$ifdef NDS_INTERFACE}
  30. const
  31. QRAN_SHIFT = 15;
  32. QRAN_MASK = ((1 shl QRAN_SHIFT) - 1);
  33. QRAN_MAX = QRAN_MASK;
  34. QRAN_A = 1664525;
  35. QRAN_C = 1013904223;
  36. var
  37. RandSeed: dword = 42;
  38. {$endif NDS_INTERFACE}
  39. {$ifdef NDS_IMPLEMENTATION}
  40. function Randomize(seed: cint): cint;
  41. var
  42. old: cint;
  43. begin
  44. old := RandSeed;
  45. RandSeed := seed;
  46. Randomize := old;
  47. end;
  48. function Rand(): cint;
  49. begin
  50. RandSeed := QRAN_A * RandSeed + QRAN_C;
  51. Rand := (RandSeed shr 16) and QRAN_MAX;
  52. end;
  53. function Rand(value: cint): cint;
  54. var
  55. a: cint;
  56. begin
  57. RandSeed := QRAN_A * RandSeed + QRAN_C;
  58. a := (RandSeed shr 16) and QRAN_MAX;
  59. Rand := (a * value) shr 15;
  60. end;
  61. (* +------------------------------------------------------------------------+ *)
  62. (* End Random function *)
  63. (* +------------------------------------------------------------------------+ *)
  64. {$endif NDS_IMPLEMENTATION}
  65. {$ifdef NDS_INTERFACE}
  66. (* Some libc functions *)
  67. function printf(format: Pchar; args: array of const): longint; cdecl; external;
  68. function printf(format: Pchar): longint; cdecl; varargs; external;
  69. function sprintf(s: Pchar; format: Pchar; args: array of const): longint; cdecl; external;
  70. function sprintf(s: Pchar; format: Pchar): longint; varargs; cdecl; external;
  71. function iprintf(format: Pchar; args: array of const): longint; cdecl; external;
  72. function iprintf(format: Pchar): longint; varargs; cdecl; external;
  73. function scanf(format: Pchar; args: array of const): longint; cdecl; external;
  74. function scanf(format: Pchar): longint; cdecl; varargs; external;
  75. function sscanf(s: Pchar; format: Pchar; args: array of const): longint; cdecl; external;
  76. function sscanf(s: Pchar; format: Pchar): longint; cdecl; varargs; external;
  77. (* libc file handling types and routines *)
  78. type
  79. _FILE = record
  80. firstCluster: cuint;
  81. length: cuint;
  82. curPos: cuint;
  83. curClus: cuint; // Current cluster to read from
  84. curSect: integer; // Current sector within cluster
  85. curByte: integer; // Current byte within sector
  86. readBuffer: array [0..511] of byte; // Buffer used for unaligned reads
  87. appClus: cuint; // Cluster to append to
  88. appSect: integer; // Sector within cluster for appending
  89. appByte: integer; // Byte within sector for appending
  90. read: boolean; // Can read from file
  91. write: boolean; // Can write to file
  92. append: boolean;// Can append to file
  93. inUse: boolean; // This file is open
  94. dirEntSector: cuint; // The sector where the directory entry is stored
  95. dirEntOffset: integer; // The offset within the directory sector
  96. end;
  97. P_FILE = ^_FILE;
  98. const
  99. SEEK_SET = 0;
  100. SEEK_CUR = 1;
  101. SEEK_END = 2;
  102. function fopen(filename: Pchar; modes: Pchar): P_FILE; cdecl; external;
  103. function fread(ptr: pointer; size: longint; n: longint; stream: P_FILE): longint; cdecl; external;
  104. function fwrite(ptr: pointer; size: longint; n: longint; s: P_FILE): longint; cdecl; external;
  105. function ftell(stream: P_FILE): longint; cdecl; external;
  106. function fseek(stream: P_FILE; off: longint; whence: longint): longint; cdecl; external;
  107. function fclose(stream: P_FILE): longint; cdecl; external;
  108. {$endif NDS_INTERFACE}
  109. {$ifdef NDS_INTERFACE}
  110. function Randomize(seed: cint): cint;
  111. function Rand(): cint;
  112. function Rand(value: cint): cint;
  113. {$endif NDS_INTERFACE}