Guest User

Untitled

a guest
Jul 17th, 2018
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 11.01 KB | None | 0 0
  1. // Define an alias for unsigned shorts
  2. typedef unsigned char u8;
  3. typedef unsigned short u16;
  4. typedef unsigned int u32;
  5.  
  6. // Video defines
  7. #define VIDBUFADR (u16 *)0x6000000
  8. #define REG_DISPCTL *(unsigned short *)0x4000000
  9. #define SCANLINECOUNTER (*(volatile u16*)0x4000006)
  10.  
  11. #define REG_DISPCNTL (*(u16*)0x4000000)
  12. #define PAL_MEM ((u16*)0x5000000)
  13. #define RGB(r, g, b) ((r) | ((g)<<5) | ((b)<<10))
  14.  
  15. // Mode 4 ----------------------------------------------------------------------------------
  16.  
  17. #define BUFFER0 ((u16 *)0x6000000)
  18. #define BUFFER1 ((u16 *)0x600A000)
  19. #define BUFFERFLAG 0x10 // Buffer Select
  20. #define PALETTE ((unsigned short *)0x5000000)
  21.  
  22. // Modes
  23. #define MODE0 0
  24. #define MODE3 3
  25. #define MODE4 4
  26.  
  27. // Backgrounds
  28. #define BG0_ENABLE (1<<8)
  29. #define BG1_ENABLE (1<<9)
  30. #define BG2_ENABLE (1<<10)
  31. #define BG3_ENABLE (1<<11)
  32.  
  33. //background control registers
  34. #define REG_BG0CNT *(volatile unsigned short*)0x4000008
  35. #define REG_BG1CNT *(volatile unsigned short*)0x400000A
  36. #define REG_BG2CNT *(volatile unsigned short*)0x400000C
  37. #define REG_BG3CNT *(volatile unsigned short*)0x400000E
  38.  
  39.  
  40. // Useful macros
  41. #define OFFSET(r,c,rowlen) ((r)*(rowlen) + (c))
  42. #define COLOR(r,g,b) ((r) | (g)<<5 | (b)<<10)
  43.  
  44. // Some colors
  45. #define WHITE COLOR(31,31,31)
  46. #define RED COLOR(31,0,0)
  47. #define GREEN COLOR(0,31,0)
  48. #define BLUE COLOR(0,0,31)
  49. #define CYAN COLOR(0,31,31)
  50. #define MAGENTA COLOR(31,0,31)
  51. #define YELLOW COLOR(31,31,0)
  52. #define BLACK 0
  53.  
  54. enum {BLACKINDEX, REDINDEX, GREENINDEX, BLUEINDEX, YELLOWINDEX,
  55. CYANINDEX, MAGENTAINDEX};
  56.  
  57. #define SQUARE(x) ((x)*(x))
  58.  
  59. // Buttons
  60.  
  61. #define BUTTONS (*(volatile unsigned int *)0x04000130)
  62.  
  63. #define BUTTON_A (1<<0)
  64. #define BUTTON_B (1<<1)
  65. #define BUTTON_SELECT (1<<2)
  66. #define BUTTON_START (1<<3)
  67. #define BUTTON_RIGHT (1<<4)
  68. #define BUTTON_LEFT (1<<5)
  69. #define BUTTON_UP (1<<6)
  70. #define BUTTON_DOWN (1<<7)
  71. #define BUTTON_R (1<<8)
  72. #define BUTTON_L (1<<9)
  73.  
  74. //#define KEY_DOWN_NOW(key) (~(BUTTONS) & key)
  75. #define BUTTON_HELD(mask) ((mask & buttons) == mask)
  76. #define BUTTON_PRESSED(mask) (((lastButtons & (mask)) != (mask)) && BUTTON_HELD(mask))
  77.  
  78.  
  79. // This allows any file that includes this header file
  80. // to have access to the static (global) variable that
  81. // will hold the address of the video buffer. The word
  82. // extern lets us and C know that this is not the
  83. // definition but just the declaration
  84. extern u16 *videoBuffer;
  85.  
  86. volatile u32* rawButtons;
  87.  
  88. // Prototypes
  89. void setPixel(int row, int col, u16 color);
  90. void drawRect(int row, int col, int height, int width, volatile u16 color);
  91. void drawRect4(int row, int col, int height, int width, volatile u8 index);
  92. void delay(int clicks);
  93. void waitForVblank();
  94. void fillPicture(const u16 *picture);
  95. void FlipPage();
  96. void setPixel4(int row, int col, unsigned char index);
  97. void fillScreen4(volatile unsigned char index);
  98. void fillScreen(volatile u16 color);
  99. void dmaTransfer(void *destination, const void *source, u32 chunks, u32 mode);
  100. /* DMA */
  101.  
  102. typedef struct
  103. { // ***********************************************************
  104. const volatile void *src; // We mark this as const which means we can assign it const
  105. volatile void *dst; // things without the compiler yelling but we can also pass it
  106. volatile u32 cnt; // things that are not const!
  107. // ***********************************************************
  108. } DMAREC;
  109.  
  110. #define DMA ((volatile DMAREC *)0x040000B0)
  111.  
  112. #define DMA_CHANNEL_0 0
  113. #define DMA_CHANNEL_1 1
  114. #define DMA_CHANNEL_2 2
  115. #define DMA_CHANNEL_3 3
  116.  
  117. #define DMA_DESTINATION_INCREMENT (0 << 21)
  118. #define DMA_DESTINATION_DECREMENT (1 << 21)
  119. #define DMA_DESTINATION_FIXED (2 << 21)
  120. #define DMA_DESTINATION_RESET (3 << 21)
  121.  
  122. #define DMA_SOURCE_INCREMENT (0 << 23)
  123. #define DMA_SOURCE_DECREMENT (1 << 23)
  124. #define DMA_SOURCE_FIXED (2 << 23)
  125.  
  126. #define DMA_REPEAT (1 << 25)
  127.  
  128. #define DMA_16 (0 << 26)
  129. #define DMA_32 (1 << 26)
  130.  
  131. #define DMA_NOW (0 << 28)
  132. #define DMA_AT_VBLANK (1 << 28)
  133. #define DMA_AT_HBLANK (2 << 28)
  134. #define DMA_AT_REFRESH (3 << 28)
  135.  
  136. #define DMA_IRQ (1 << 30)
  137. #define DMA_ON (1 << 31)
  138.  
  139. #define DMA_MEMORY ((volatile DMA_CONTROLLER *) 0x040000B0)
  140.  
  141. #define START_ON_FIFO_EMPTY 0x30000000
  142.  
  143. // *** Timers ========================================================
  144.  
  145. // Timers
  146.  
  147. #define REG_TM0CNT *(volatile u16*)0x4000102
  148. #define REG_TM1CNT *(volatile u16*)0x4000106
  149. #define REG_TM2CNT *(volatile u16*)0x400010A
  150. #define REG_TM3CNT *(volatile u16*)0x400010E
  151.  
  152. #define REG_TM0D *(volatile u16*)0x4000100
  153. #define REG_TM1D *(volatile u16*)0x4000104
  154. #define REG_TM2D *(volatile u16*)0x4000108
  155. #define REG_TM3D *(volatile u16*)0x400010C
  156.  
  157. // Timer flags
  158. #define TIMER_ON (1<<7)
  159. #define TM_IRQ (1<<6)
  160. #define TM_CASCADE (1<<2)
  161. #define TM_FREQ_1 0
  162. #define TM_FREQ_64 1
  163. #define TM_FREQ_256 2
  164. #define TM_FREQ_1024 3
  165.  
  166. // Time factors to multiply clock ticks to convert to microsec (usec)
  167. // The next line (uncommented) should be in myLib.c
  168. // double timefactors[] = {0.059604, 3.811, 15.259, 59.382};
  169. extern double timefactors[];
  170.  
  171.  
  172. /* Interrupts -------------------------------------------------------------------- */
  173.  
  174.  
  175. #define REG_IME *(u16*)0x4000208
  176. #define REG_IE *(u16*)0x4000200
  177. #define REG_IF *(volatile u16*)0x4000202
  178. #define REG_INTERRUPT *(u32*)0x3007FFC
  179.  
  180. /* Display Status Control Register ------------------------------------------------ */
  181. #define REG_DISPSTAT *(u16*)0x4000004
  182.  
  183. //interrupt constants for turning them on
  184. #define INT_VBLANK_ENABLE (1<<3)
  185. #define INT_HBLANK_ENABLE (1<<4)
  186. #define INT_VCOUNT_ENABLE (1<<5)
  187.  
  188. // Interrupt constants for "enablng receipt of" and
  189. // "checking which type of" interrupt happened
  190.  
  191. #define INT_VB (1<< 0) // VB – vertical blank interrupt
  192. #define INT_HB (1<< 1) // HB – horizontal blank interrupt
  193. #define INT_VC (1<< 2) // VC – vertical scanline count interrupt
  194. #define INT_T0 (1<< 3) // T0 – timer 0 interrupt
  195. #define INT_T1 (1<< 4) // T1 – timer 1 interrupt
  196. #define INT_T2 (1<< 5) // T2 – timer 2 interrupt
  197. #define INT_T3 (1<< 6) // T3 – timer 3 interrupt
  198. #define INT_COM (1<< 7) // COM – serial communication interrupt
  199. #define INT_DMA0 (1<< 8) // DMA0 – DMA0 finished interrupt
  200. #define INT_DMA1 (1<< 9) // DMA1 – DMA1 finished interrupt
  201. #define INT_DMA2 (1<<10) // DMA2 – DMA2 finished interrupt
  202. #define INT_DMA3 (1<<11) // DMA3 – DMA3 finished interrupt
  203. #define INT_BUTTON (1<<12) // BUTTON – button interrupt
  204. #define INT_CART (1<<13) // CART – game cartridge interrupt
  205.  
  206. // *** Sound =========================================================
  207.  
  208. #define REG_SOUNDCNT_X (*(u32*)0x04000084)
  209. #define SND_ENABLED 0x00000080
  210.  
  211. // register definitions
  212. #define REG_SOUNDCNT_L (*(u16*)0x04000080)
  213. #define REG_SOUNDCNT_H (*(u16*)0x04000082)
  214.  
  215. // flags
  216. #define SND_ENABLED 0x00000080
  217. #define SND_OUTPUT_RATIO_25 0x0000
  218. #define SND_OUTPUT_RATIO_50 0x0001
  219. #define SND_OUTPUT_RATIO_100 0x0002
  220. #define DSA_OUTPUT_RATIO_50 0x0000
  221. #define DSA_OUTPUT_RATIO_100 0x0004
  222. #define DSA_OUTPUT_TO_RIGHT 0x0100
  223. #define DSA_OUTPUT_TO_LEFT 0x0200
  224. #define DSA_OUTPUT_TO_BOTH 0x0300
  225. #define DSA_TIMER0 0x0000
  226. #define DSA_TIMER1 0x0400
  227. #define DSA_FIFO_RESET 0x0800
  228. #define DSB_OUTPUT_RATIO_50 0x0000
  229. #define DSB_OUTPUT_RATIO_100 0x0008
  230. #define DSB_OUTPUT_TO_RIGHT 0x1000
  231. #define DSB_OUTPUT_TO_LEFT 0x2000
  232. #define DSB_OUTPUT_TO_BOTH 0x3000
  233. #define DSB_TIMER0 0x0000
  234. #define DSB_TIMER1 0x4000
  235. #define DSB_FIFO_RESET 0x8000
  236.  
  237. // FIFO address defines
  238. #define REG_FIFO_A ((void *)0x040000A0)
  239. #define REG_FIFO_B ((void *)0x040000A4)
  240.  
  241. // Vblank frequency
  242. #define VBLANKFREQ 59.727
  243.  
  244. // *** Tiles =========================================================
  245.  
  246. typedef struct { u16 tileimg[8192]; } charblock;
  247. typedef struct { u16 tilemap[1024]; } screenblock;
  248.  
  249. #define CHARBLOCKBASE ((charblock *)0x6000000)
  250. #define SCREENBLOCKBASE ((screenblock *)0x6000000)
  251.  
  252.  
  253. //background offset registers
  254. #define REG_BG0HOFS *(volatile unsigned short *)0x04000010
  255. #define REG_BG1HOFS *(volatile unsigned short *)0x04000014
  256. #define REG_BG2HOFS *(volatile unsigned short *)0x04000018
  257. #define REG_BG3HOFS *(volatile unsigned short *)0x0400001C
  258.  
  259. #define REG_BG0VOFS *(volatile unsigned short *)0x04000012
  260. #define REG_BG1VOFS *(volatile unsigned short *)0x04000016
  261. #define REG_BG2VOFS *(volatile unsigned short *)0x0400001A
  262. #define REG_BG3VOFS *(volatile unsigned short *)0x0400001E
  263.  
  264. #define BG_4BPP (0 << 7)
  265. #define BG_8BPP (1 << 7)
  266. #define BG_REG_32x32 (0 << 14)
  267. #define BG_REG_64x32 (1 << 14)
  268. #define BG_REG_32x64 (2 << 14)
  269. #define BG_REG_64x64 (3 << 14)
  270.  
  271. #define CBB(i) ((i) << 2) //place this in the BG CNT register to define what Character block the data is in
  272. #define SBB(i) ((i) << 8) //place this in the BG CNT register to define what Screen block the Map is in
  273.  
  274. #define SPRITE_MODE_1D (1 << 6) //put this into REG_DISPCNTL to enable 1D mapping mode
  275. #define SPRITE_ENABLE (1 << 12) //put this into REG_DISPCNTL to enable sprites
  276.  
  277. #define ATTR0_REGULAR (0 << 8) //Normal Sprites
  278. #define ATTR0_AFFINE (1 << 8) //Affine Sprites
  279. #define ATTR0_HIDE (2 << 8) // Hide Sprites
  280. #define ATTR0_DOUBLE_AFFINE (3 << 8) //Double Affine Sprites (Never used it before so don't ask me -Peter)
  281. #define ATTR0_NORMAL (0 << 10) //Normal Rendering
  282. #define ATTR0_BLEND (1 << 10) //Enables Alpha Blending. Don't worry about it. Unless you want too =D
  283. #define ATTR0_WIN (2 << 10) //Object Window mode. Again, no idea.
  284. #define ATTR0_MOSAIC (1 << 12) //Enables the mosaic effect for this sprite. It's a cool visual effect. We can talk about it on thursday perhaps
  285. #define ATTR0_4BPP (0 << 13) //16 color Sprites
  286. #define ATTR0_8BPP (1 << 13) //256 color Sprites
  287. #define ATTR0_SQUARE (0 << 14) //Square Shape
  288. #define ATTR0_WIDE (1 << 14) // Wide Shape
  289. #define ATTR0_TALL (2 << 14) //Tall Shape
  290.  
  291. #define ATTR1_HFLIP (1 << 12)
  292. #define ATTR1_VFLIP (1 << 13)
  293. #define ATTR1_SIZE8 (0 << 14)
  294. #define ATTR1_SIZE16 (1 << 14)
  295. #define ATTR1_SIZE32 (2 << 14)
  296. #define ATTR1_SIZE64 (3 << 14)
  297.  
  298. #define OAM ((OBJ_ATTR*)(0x7000000))
  299. #define SPRITE_PALETTE ((unsigned short*)(0x5000200))
  300.  
  301. #define SPRITEOFFSET16(r,c) (r)*32+(c)
  302.  
  303. typedef struct
  304. {
  305. const void *source;
  306. void *destination;
  307. u32 control;
  308. } DMA_CONTROLLER;
  309.  
  310. typedef struct {
  311. unsigned int data[512];
  312. } SBLOCK;
  313.  
  314. typedef struct {
  315. unsigned int data[4096];
  316. } CBLOCK;
  317.  
  318. typedef struct{
  319. unsigned short attr0;
  320. unsigned short attr1;
  321. unsigned short attr2;
  322. unsigned short fill;
  323. }OBJ_ATTR;
  324.  
  325. typedef struct {
  326. int row;
  327. int col;
  328. int start;
  329. int shape;
  330. int size;
  331. int height;
  332. int width;
  333. int rdel;
  334. int cdel;
  335. } Sprite;
  336.  
  337. //macros and bit constants for setting the background control register specifics
  338. #define COLOR256 1 << 7
  339. #define BG_SIZE0 0<<14 // 32 x 32 tiles
  340. #define BG_SIZE1 1<<14 // 64 x 32
  341. #define BG_SIZE2 2<<14 // 32 x 64
  342. #define BG_SIZE3 3<<14 // 64 x 64
Add Comment
Please, Sign In to add comment