gba_video.pas 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  1. (*
  2. gba_video.pas 18/06/2006 4.38.37
  3. ------------------------------------------------------------------------------
  4. This lib is a raw porting of libgba library for gba (you can find it at
  5. http://www.devkitpro.org).
  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_video;
  28. {$i def.inc}
  29. interface
  30. uses
  31. gba_types, gba_regs;
  32. type
  33. LCDC_IRQ = (
  34. LCDC_VBL_FLAG = (1 shl 0),
  35. LCDC_HBL_FLAG = (1 shl 1),
  36. LCDC_VCNT_FLAG = (1 shl 2),
  37. LCDC_VBL = (1 shl 3),
  38. LCDC_HBL = (1 shl 4),
  39. LCDC_VCNT = (1 shl 5)
  40. );
  41. TLCDCIRQ = LCDC_IRQ;
  42. BG_CTRL_BITS = (
  43. BG_MOSAIC = BIT6,
  44. BG_16_COLOR = (0 shl 7),
  45. BG_256_COLOR = BIT7,
  46. BG_WRAP = BIT13,
  47. BG_SIZE_0 = (0 shl 14),
  48. BG_SIZE_1 = (1 shl 14),
  49. BG_SIZE_2 = (2 shl 14),
  50. BG_SIZE_3 = (3 shl 14)
  51. );
  52. TBgCTRLBits = BG_CTRL_BITS;
  53. NAMETABLE = array [0..31, 0..31] of word;
  54. TNameTable = NAMETABLE;
  55. PNameTable = ^TNameTable;
  56. MODE3_LINE = array [0..239] of word;
  57. TMODE3Line = MODE3_LINE;
  58. MODE5_LINE = array [0..159] of word;
  59. TMODE5Line = MODE5_LINE;
  60. const
  61. MODE_0 = 0;
  62. MODE_1 = 1;
  63. MODE_2 = 2;
  64. MODE_3 = 3;
  65. MODE_4 = 4;
  66. MODE_5 = 5;
  67. BACKBUFFER = BIT4;
  68. OBJ_1D_MAP = BIT6;
  69. LCDC_OFF = BIT7;
  70. BG0_ON = BIT8;
  71. BG1_ON = BIT9;
  72. BG2_ON = BIT10;
  73. BG3_ON = BIT11;
  74. OBJ_ON = BIT12;
  75. WIN0_ON = BIT13;
  76. WIN1_ON = BIT14;
  77. OBJ_WIN_ON = BIT15;
  78. BG0_ENABLE = BG0_ON;
  79. BG1_ENABLE = BG1_ON;
  80. BG2_ENABLE = BG2_ON;
  81. BG3_ENABLE = BG3_ON;
  82. OBJ_ENABLE = OBJ_ON;
  83. WIN0_ENABLE = WIN0_ON;
  84. WIN1_ENABLE = WIN1_ON;
  85. OBJ_WIN_ENABLE = BG0_ON;
  86. BG_ALL_ON = BG0_ON or BG1_ON or BG2_ON or BG3_ON;
  87. BG_ALL_ENABLE = BG0_ON or BG1_ON or BG2_ON or BG3_ON;
  88. const
  89. MAP : PNameTable = pointer($06000000);
  90. BG_WID_32 = BG_SIZE_0;
  91. BG_WID_64 = BG_SIZE_1;
  92. BG_HT_32 = BG_SIZE_0;
  93. BG_HT_64 = BG_SIZE_2;
  94. //---------------------------------------------------------------------------------
  95. // Symbolic names for the rot/scale map sizes
  96. //---------------------------------------------------------------------------------
  97. ROTBG_SIZE_16 = BG_SIZE_0;
  98. ROTBG_SIZE_32 = BG_SIZE_1;
  99. ROTBG_SIZE_64 = BG_SIZE_2;
  100. ROTBG_SIZE_128 = BG_SIZE_3;
  101. MODE3_FB: ^TMODE3Line = pointer($06000000);
  102. MODE5_FB: ^TMODE5Line = pointer($06000000);
  103. MODE5_BB: ^TMODE5Line = pointer($0600A000);
  104. function VCount(m: integer): dword; inline;
  105. function BgSize(m: integer): integer; inline;
  106. function CharBase(m: integer): integer; inline;
  107. function CharBaseAdr(m: integer): integer; inline;
  108. function MapBaseAdr(m: integer): integer; inline;
  109. function ScreenBase(m: integer): integer; inline;
  110. function TileBase(m: integer): integer; inline;
  111. function TileBaseAdr(m: integer): integer; inline;
  112. function BgPriority(m: integer): integer; inline;
  113. function BgPalette(m: integer): integer; inline;
  114. function PatRAM4(x, tn: integer): dword; inline;
  115. function PatRAM8(x, tn: integer): dword; inline;
  116. function SprVRAM(tn: integer): dword; inline;
  117. procedure SetMode(mode: dword); inline;
  118. procedure Wait(count: dword);
  119. procedure WaitForVBlank(); inline;
  120. procedure WaitForVDraw(); inline;
  121. procedure VSync(); inline;
  122. function Flip(): pword;
  123. function RGB(const r, g, b: word): word;inline;
  124. function RGB5(const r, g, b: word): word;inline;
  125. function RGB8(const r, g, b: word): word;inline;
  126. implementation
  127. function VCount(m: integer): dword; inline;
  128. begin
  129. VCount := m shl 8;
  130. end;
  131. function BgSize(m: integer): integer; inline;
  132. begin
  133. BgSize := ((m shl 14));
  134. end;
  135. function CharBase(m: integer): integer; inline;
  136. begin
  137. CharBase := ((m) shl 2);
  138. end;
  139. function CharBaseAdr(m: integer): integer; inline;
  140. begin
  141. CharBaseAdr := ($6000000 + ((m) shl 14));
  142. end;
  143. function MapBaseAdr(m: integer): integer; inline;
  144. begin
  145. MapBaseAdr := ($6000000 + ((m) shl 11));
  146. end;
  147. function ScreenBase(m: integer): integer; inline;
  148. begin
  149. ScreenBase := ((m) shl 8);
  150. end;
  151. //alternate names for char and screen base
  152. function TileBase(m: integer): integer; inline;
  153. begin
  154. TileBase := ((m) shl 2);
  155. end;
  156. function TileBaseAdr(m: integer): integer; inline;
  157. begin
  158. TileBaseAdr := ($6000000 + ((m) shl 14));
  159. end;
  160. function BgPriority(m: integer): integer; inline;
  161. begin
  162. BgPriority := (m);
  163. end;
  164. function BgPalette(m: integer): integer; inline;
  165. begin
  166. BgPalette := ((m) shl 12);
  167. end;
  168. (*---------------------------------------------------------------------------------
  169. CHAR_BASE_ADR() is the direct equivalent to old PATRAM(),
  170. giving the base address of a chr bank.
  171. These macros pinpoint the base address of a single tile.
  172. ---------------------------------------------------------------------------------*)
  173. function PatRAM4(x, tn: integer): dword; inline;
  174. begin
  175. PatRAM4 := (dword($6000000 or (((x) shl 14) + ((tn) shl 5)) ));
  176. end;
  177. function PatRAM8(x, tn: integer): dword; inline;
  178. begin
  179. PatRAM8 := (dword($6000000 or (((x) shl 14) + ((tn) shl 6)) ));
  180. end;
  181. function SprVRAM(tn: integer): dword; inline;
  182. begin
  183. SprVRAM := (dword($6000000 or $10000 or ((tn) shl 5)));
  184. end;
  185. procedure SetMode(mode: dword); inline;
  186. begin
  187. REG_DISPCNT^ := (mode);
  188. end;
  189. procedure Wait(count: dword);
  190. var
  191. vline: word;
  192. begin
  193. vline := REG_VCOUNT^;
  194. dec(count);
  195. while (count > 0) do
  196. begin
  197. while(vline = REG_VCOUNT^) do;
  198. while(vline <> REG_VCOUNT^) do;
  199. dec(count);
  200. end;
  201. end;
  202. procedure WaitForVBlank(); inline;
  203. begin
  204. while (REG_VCOUNT^ < 160) do
  205. end;
  206. procedure WaitForVDraw(); inline;
  207. begin
  208. while (REG_VCOUNT^ >= 160) do
  209. end;
  210. // wait till the _next_ vblank
  211. // Note that for real work, using a VBlank interrupt is preferred
  212. procedure VSync(); inline;
  213. begin
  214. WaitForVDraw();
  215. WaitForVBlank();
  216. end;
  217. function Flip(): pword;
  218. begin
  219. VideoBuffer := pword(dword(VideoBuffer) or $0000A000);
  220. REG_DISPCNT^ := REG_DISPCNT^ or $0010;
  221. Flip := VideoBuffer;
  222. end;
  223. function RGB(const r, g, b: word): word;inline;
  224. begin
  225. RGB := ((r) + (g shl 5) + (b shl 10));
  226. end;
  227. function RGB5(const r, g, b: word): word;inline;
  228. begin
  229. RGB5 := ((r) or ((g) shl 5) or ((b) shl 10));
  230. end;
  231. function RGB8(const r, g, b: word): word;inline;
  232. begin
  233. RGB8 := ( (((b) shr 3) shl 10) or (((g) shr 3) shl 5) or ((r) shr 3) );
  234. end;
  235. end.