gba_core.pas 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. (*
  2. gba_core.pas 01/09/2006 19.17.35
  3. ------------------------------------------------------------------------------
  4. This lib is a raw porting of tonclib library for gba (you can find it at
  5. http://user.chem.tue.nl/jakvijn/index.htm).
  6. As this is a direct port from c, I'm pretty sure that something could not work
  7. as you expect. I am even more sure that this code could be written better, so
  8. if you think that I have made some mistakes or you have some better
  9. implemented functions, let me know [francky74 (at) gmail (dot) com]
  10. Enjoy!
  11. Conversion by Legolas (http://itaprogaming.free.fr) for freepascal compiler
  12. (http://www.freepascal.org)
  13. Copyright (C) 2006 Francesco Lombardi
  14. This library is free software; you can redistribute it and/or
  15. modify it under the terms of the GNU Lesser General Public
  16. License as published by the Free Software Foundation; either
  17. version 2.1 of the License, or (at your option) any later version.
  18. This library is distributed in the hope that it will be useful,
  19. but WITHOUT ANY WARRANTY; without even the implied warranty of
  20. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  21. Lesser General Public License for more details.
  22. You should have received a copy of the GNU Lesser General Public
  23. License along with this library; if not, write to the Free Software
  24. Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  25. ------------------------------------------------------------------------------
  26. *)
  27. unit gba_core;
  28. {$i def.inc}
  29. interface
  30. uses
  31. gba_types;
  32. // random numbers -- courtesy of Cearn (TONClib)
  33. const
  34. QRAN_SHIFT = 15;
  35. QRAN_MASK = ((1 shl QRAN_SHIFT) - 1);
  36. QRAN_MAX = QRAN_MASK;
  37. QRAN_A = 1664525;
  38. QRAN_C = 1013904223;
  39. var
  40. gbaRandSeed: dword = 42;
  41. function gbaRandomize(seed: dword): dword;
  42. function gbaRand(): dword;
  43. function gbaRand(value: integer): dword;
  44. procedure memset16(dest: pointer; hw: word; hwcount: dword); cdecl; external;
  45. procedure memcpy16(dest: pointer; const src: pointer; hwcount: dword); cdecl; external;
  46. procedure memset32(dest: pointer; wd: dword; wcount: dword); cdecl; external;
  47. procedure memcpy32(dest: pointer; const src: pointer; wcount: dword); cdecl; external;
  48. procedure DebugPrint(s: string); assembler; inline;
  49. implementation
  50. function gbaRandomize(seed: dword): dword;
  51. var
  52. old: dword;
  53. begin
  54. old := gbaRandSeed;
  55. gbaRandSeed := seed;
  56. gbaRandomize := old;
  57. end;
  58. function gbaRand(): dword;
  59. begin
  60. gbaRandSeed := QRAN_A * gbaRandSeed + QRAN_C;
  61. gbaRand := (gbaRandSeed shr 16) and QRAN_MAX;
  62. end;
  63. function gbaRand(value: integer): dword;
  64. var
  65. a: dword;
  66. begin
  67. gbaRandSeed := QRAN_A * gbaRandSeed + QRAN_C;
  68. a := (gbaRandSeed shr 16) and QRAN_MAX;
  69. gbaRand := (a * value) shr 15;
  70. end;
  71. // memory handling routines
  72. // these are in ASM and optimized; use when possible
  73. {$l core_asm.o}
  74. {$OPTIMIZATION OFF}
  75. procedure DebugPrint(s: string); assembler; inline;
  76. asm
  77. mov r0,s
  78. swi #0xff0000
  79. end['r0'];
  80. {$OPTIMIZATION ON}
  81. end.