memory.inc 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  1. (*
  2. $Id: memory.inc 25 2007-12-10 21:06:46Z p4p3r0 $
  3. ------------------------------------------------------------------------------
  4. Copyright (C) 2005
  5. Jason Rogers (dovoto)
  6. Dave Murphy (WinterMute)
  7. This software is provided 'as-is', without any express or implied
  8. warranty. In no event will the authors be held liable for any
  9. damages arising from the use of this software.
  10. Permission is granted to anyone to use this software for any
  11. purpose, including commercial applications, and to alter it and
  12. redistribute it freely, subject to the following restrictions:
  13. 1. The origin of this software must not be misrepresented; you
  14. must not claim that you wrote the original software. If you use
  15. this software in a product, an acknowledgment in the product
  16. documentation would be appreciated but is not required.
  17. 2. Altered source versions must be plainly marked as such, and
  18. must not be misrepresented as being the original software.
  19. 3. This notice may not be removed or altered from any source
  20. distribution.
  21. ------------------------------------------------------------------------------
  22. Conversion by Legolas (http://itaprogaming.free.fr) for freepascal compiler
  23. (http://www.freepascal.org)
  24. Copyright (C) 2006 Francesco Lombardi
  25. Check http://sourceforge.net/projects/libndsfpc for updates
  26. ------------------------------------------------------------------------------
  27. $Log$
  28. *)
  29. {$ifdef NDS_INTERFACE}
  30. // WAIT_CR: Wait State Control Register
  31. const
  32. {$ifdef ARM9}
  33. REG_EXMEMCNT : pcuint16 = pointer($04000204);
  34. {$else}
  35. REG_EXMEMSTAT: pcuint16 = pointer($04000204);
  36. {$endif}
  37. ARM7_MAIN_RAM_PRIORITY = (1 shl 15);
  38. ARM7_OWNS_CARD = (1 shl 11);
  39. ARM7_OWNS_ROM = (1 shl 7);
  40. // Protection register (write-once sadly)
  41. {$ifdef ARM7}
  42. PROTECTION : pcuint32 = pointer($04000308);
  43. {$endif ARM7}
  44. ALLRAM : pcuint8 = pointer($00000000);
  45. MAINRAM8 : pcuint8 = pointer($02000000);
  46. MAINRAM16 : pcuint16 = pointer($02000000);
  47. MAINRAM32 : pcuint32 = pointer($02000000);
  48. // fixme: shared RAM
  49. // GBA_BUS is volatile, while GBAROM is not
  50. GBA_BUS : pcuint16 = pointer($08000000);
  51. GBAROM : pcuint16 = pointer($08000000);
  52. SRAM : pcuint8 = pointer($0A000000);
  53. {$ifdef ARM9}
  54. PALETTE : pcuint16 = pointer($05000000);
  55. PALETTE_SUB : pcuint16 = pointer($05000400);
  56. BG_PALETTE : pcuint16 = pointer($05000000);
  57. BG_PALETTE_SUB : pcuint16 = pointer($05000400);
  58. SPRITE_PALETTE : pcuint16 = pointer($05000200);
  59. SPRITE_PALETTE_SUB : pcuint16 = pointer($05000600);
  60. BG_GFX : pcuint16 = pointer($06000000);
  61. BG_GFX_SUB : pcuint16 = pointer($06200000);
  62. SPRITE_GFX : pcuint16 = pointer($06400000);
  63. SPRITE_GFX_SUB : pcuint16 = pointer($06600000);
  64. VRAM_0 : pcuint16 = pointer($06000000);
  65. VRAM : pcuint16 = pointer($06800000);
  66. VRAM_A : pcuint16 = pointer($06800000);
  67. VRAM_B : pcuint16 = pointer($06820000);
  68. VRAM_C : pcuint16 = pointer($06840000);
  69. VRAM_D : pcuint16 = pointer($06860000);
  70. VRAM_E : pcuint16 = pointer($06880000);
  71. VRAM_F : pcuint16 = pointer($06890000);
  72. VRAM_G : pcuint16 = pointer($06894000);
  73. VRAM_H : pcuint16 = pointer($06898000);
  74. VRAM_I : pcuint16 = pointer($068A0000);
  75. OAM : pcuint16 = pointer($07000000);
  76. OAM_SUB : pcuint16 = pointer($07000400);
  77. {$endif ARM9}
  78. {$ifdef ARM7}
  79. VRAM : pcuint16 = pointer($06000000);
  80. {$endif ARM7}
  81. type
  82. sGBAHeader = packed record
  83. entryPoint: cuint32;
  84. logo: array [0..155] of cuint8;
  85. title: array [0..11] of cchar;
  86. gamecode: array [0..3] of cchar;
  87. makercode: cuint16;
  88. is96h: cuint8;
  89. unitcode: cuint8;
  90. devicecode: cuint8;
  91. unused: array [0..6] of cuint8;
  92. version: cuint8;
  93. complement: cuint8;
  94. checksum: cuint16;
  95. end;
  96. TGBAHeader = sGBAHeader;
  97. PGBAHeader = ^sGBAHeader;
  98. const
  99. GBA_HEADER : pGBAHeader = pointer($08000000);
  100. type
  101. sNDSHeader = packed record
  102. gameTitle: array [0..11] of cchar;
  103. gameCode: array [0..3] of cchar;
  104. makercode: array [0..1] of cchar;
  105. unitCode: cuint8;
  106. deviceType: cuint8; // type of device in the game card
  107. deviceSize: cuint8; // device capacity (1<<n Mbit)
  108. reserved1: array [0..8] of cuint8;
  109. romversion: cuint8;
  110. flags: cuint8; // auto-boot flag
  111. arm9romSource: cuint32;
  112. arm9executeAddress: cuint32;
  113. arm9destination: cuint32;
  114. arm9binarySize: cuint32;
  115. arm7romSource: cuint32;
  116. arm7executeAddress: cuint32;
  117. arm7destination: cuint32;
  118. arm7binarySize: cuint32;
  119. filenameSource: cuint32;
  120. filenameSize: cuint32;
  121. fatSource: cuint32;
  122. fatSize: cuint32;
  123. arm9overlaySource: cuint32;
  124. arm9overlaySize: cuint32;
  125. arm7overlaySource: cuint32;
  126. arm7overlaySize: cuint32;
  127. cardControl13: cuint32; // used in modes 1 and 3
  128. cardControlBF: cuint32; // used in mode 2
  129. bannerOffset: cuint32;
  130. secureCRC16: cuint16;
  131. readTimeout: cuint16;
  132. unknownRAM1: cuint32;
  133. unknownRAM2: cuint32;
  134. bfPrime1: cuint32;
  135. bfPrime2: cuint32;
  136. romSize: cuint32;
  137. headerSize: cuint32;
  138. zeros88: array [0..13] of cuint32;
  139. gbaLogo: array [0..155] of cuint8;
  140. logoCRC16: cuint16;
  141. headerCRC16: cuint16;
  142. debugRomSource: cuint32;
  143. debugRomSize: cuint32;
  144. debugRomDestination: cuint32;
  145. offset_0x16C: cuint32;
  146. zero: array [0..143] of cuint8;
  147. end;
  148. tNDSHeader = sNDSHeader;
  149. pNDSHeader = ^tNDSHeader;
  150. const
  151. NDSHeader : pNDSHeader = pointer($027FFE00);
  152. type
  153. sNDSBanner = packed record
  154. version: cuint16;
  155. crc: cuint16;
  156. reserved: array [0..27] of cuint8;
  157. icon: array [0..511] of cuint8;
  158. palette: array [0..15] of cuint16;
  159. titles: array [0..5, 0..127] of cuint16;
  160. end;
  161. tNDSBanner = sNDSBanner;
  162. pNDSBanner = ^tNDSBanner;
  163. {$endif NDS_INTERFACE}
  164. {$ifdef ARM9}
  165. {$ifdef NDS_INTERFACE}
  166. const
  167. BUS_OWNER_ARM9 = true;
  168. BUS_OWNER_ARM7 = false;
  169. {$endif NDS_INTERFACE}
  170. {$ifdef NDS_IMPLEMENTATION}
  171. // Changes only the gba rom bus ownership
  172. procedure sysSetCartOwner(arm9: cbool); inline;
  173. var
  174. i: cint;
  175. begin
  176. if arm9 then
  177. i := 0
  178. else
  179. i := ARM7_OWNS_ROM;
  180. REG_EXMEMCNT^ := (REG_EXMEMCNT^ and not ARM7_OWNS_ROM) or (i);
  181. end;
  182. // Changes only the nds card bus ownership
  183. procedure sysSetCardOwner(arm9: cbool); inline;
  184. var
  185. i: cint;
  186. begin
  187. if arm9 then
  188. i := 0
  189. else
  190. i := ARM7_OWNS_ROM;
  191. REG_EXMEMCNT^ := (REG_EXMEMCNT^ and not ARM7_OWNS_CARD) or (i);
  192. end;
  193. // Changes all bus ownerships
  194. procedure sysSetBusOwners(arm9rom, arm9card: cbool); inline;
  195. var
  196. pattern: cuint16;
  197. a9r, a9c: cint;
  198. begin
  199. pattern := REG_EXMEMCNT^ and not (ARM7_OWNS_CARD or ARM7_OWNS_ROM);
  200. if arm9card then
  201. a9c := 0
  202. else
  203. a9c := ARM7_OWNS_CARD;
  204. if arm9rom then
  205. a9r := 0
  206. else
  207. a9r := ARM7_OWNS_ROM;
  208. pattern := pattern or (a9c) or (a9r);
  209. REG_EXMEMCNT^ := pattern;
  210. end;
  211. {$endif NDS_IMPLEMENTATION}
  212. {$endif ARM9}
  213. {$ifdef NDS_INTERFACE}
  214. {$ifdef ARM9}
  215. procedure sysSetCartOwner(arm9: cbool); inline;
  216. procedure sysSetCardOwner(arm9: cbool); inline;
  217. procedure sysSetBusOwners(arm9rom, arm9card: cbool); inline;
  218. {$endif ARM9}
  219. {$endif NDS_INTERFACE}