lol its in c
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

494 lines
14 KiB

  1. #ifndef GBSDK_HARDWARE_H
  2. #define GBSDK_HARDWARE_H
  3. // Register for reading joy pad info. (R/W)
  4. static volatile __sfr __at(0x00) rP1;
  5. #define P1_5 0b00100000 // P15 out port, set to 0 to get buttons
  6. #define P1_4 0b00010000 // P14 out port, set to 0 to get dpad
  7. #define P1_3 0b00001000 // P13 in port
  8. #define P1_2 0b00000100 // P12 in port
  9. #define P1_1 0b00000010 // P11 in port
  10. #define P1_0 0b00000001 // P10 in port
  11. #define P1_GET_DPAD P1_5
  12. #define P1_GET_BTN P1_4
  13. #define P1_GET_NONE (P1_4 | P1_5)
  14. // Serial Transfer Data (R/W)
  15. static volatile __sfr __at(0x01) rSB;
  16. // Serial I/O Control (R/W)
  17. static volatile __sfr __at(0x02) rSC;
  18. // Divider register (R/W)
  19. static volatile __sfr __at(0x04) rDIV;
  20. // Timer counter (R/W)
  21. static volatile __sfr __at(0x05) rTIMA;
  22. // Timer modulo (R/W)
  23. static volatile __sfr __at(0x06) rTMA;
  24. // Timer control (R/W)
  25. static volatile __sfr __at(0x07) rTAC;
  26. #define TAC_START 0b00000100
  27. #define TAC_STOP 0b00000000
  28. #define TAC_4KHZ 0b00000000
  29. #define TAC_16KHZ 0b00000011
  30. #define TAC_65KHZ 0b00000010
  31. #define TAC_262KHZ 0b00000001
  32. // Interrupt Flag (R/W)
  33. static volatile __sfr __at(0x0F) rIF;
  34. // AUD1SWEEP/NR10 ($FF10)
  35. // Sweep register (R/W)
  36. //
  37. // Bit 6-4 - Sweep Time
  38. // Bit 3 - Sweep Increase/Decrease
  39. // 0: Addition (frequency increases???)
  40. // 1: Subtraction (frequency increases???)
  41. // Bit 2-0 - Number of sweep shift (# 0-7)
  42. // Sweep Time: (n*7.8ms)
  43. static volatile __sfr __at(0x10) rNR10;
  44. #define rAUD1SWEEP rNR10
  45. #define AUD1SWEEP_UP 0b00000000
  46. #define AUD1SWEEP_DOWN 0b00001000
  47. // AUD1LEN/NR11 ($FF11)
  48. // Sound length/Wave pattern duty (R/W)
  49. //
  50. // Bit 7-6 - Wave Pattern Duty (00:12.5% 01:25% 10:50% 11:75%)
  51. // Bit 5-0 - Sound length data (# 0-63)
  52. static volatile __sfr __at(0x11) rNR11;
  53. #define rAUD1LEN rNR11
  54. // AUD1ENV/NR12 ($FF12)
  55. // Envelope (R/W)
  56. //
  57. // Bit 7-4 - Initial value of envelope
  58. // Bit 3 - Envelope UP/DOWN
  59. // 0: Decrease
  60. // 1: Range of increase
  61. // Bit 2-0 - Number of envelope sweep (# 0-7)
  62. static volatile __sfr __at(0x12) rNR12;
  63. #define rAUD1ENV rNR12
  64. // AUD1LOW/NR13 ($FF13)
  65. // Frequency low byte (W)
  66. static volatile __sfr __at(0x13) rNR13;
  67. #define rAUD1LOW rNR13
  68. // AUD1HIGH/NR14 ($FF14)
  69. // Frequency high byte (W)
  70. //
  71. // Bit 7 - Initial (when set, sound restarts)
  72. // Bit 6 - Counter/consecutive selection
  73. // Bit 2-0 - Frequency's higher 3 bits
  74. static volatile __sfr __at(0x14) rNR14;
  75. #define rAUD1HIGH rNR14
  76. // AUD2LEN/NR21 ($FF16)
  77. // Sound Length; Wave Pattern Duty (R/W)
  78. //
  79. // see AUD1LEN for info
  80. static volatile __sfr __at(0x16) rNR21;
  81. #define rAUD2LEN rNR21
  82. // AUD2ENV/NR22 ($FF17)
  83. // Envelope (R/W)
  84. //
  85. // see AUD1ENV for info
  86. static volatile __sfr __at(0x17) rNR22;
  87. #define rAUD2ENV rNR22
  88. // AUD2LOW/NR23 ($FF18)
  89. // Frequency low byte (W)
  90. static volatile __sfr __at(0x18) rNR23;
  91. #define rAUD2LOW rNR23
  92. // AUD2HIGH/NR24 ($FF19)
  93. // Frequency high byte (W)
  94. //
  95. // see AUD1HIGH for info
  96. static volatile __sfr __at(0x19) rNR24;
  97. #define rAUD2HIGH rNR24
  98. // AUD3ENA/NR30 ($FF1A)
  99. // Sound on/off (R/W)
  100. //
  101. // Bit 7 - Sound ON/OFF (1=ON,0=OFF)
  102. static volatile __sfr __at(0x1A) rNR30;
  103. #define rAUD3ENA rNR30
  104. // AUD3LEN/NR31 ($FF1B)
  105. // Sound length (R/W)
  106. //
  107. // Bit 7-0 - Sound length
  108. static volatile __sfr __at(0x1B) rNR31;
  109. #define rAUD3LEN rNR31
  110. // AUD3LEVEL/NR32 ($FF1C)
  111. // Select output level
  112. //
  113. // Bit 6-5 - Select output level
  114. // 00: 0/1 (mute)
  115. // 01: 1/1
  116. // 10: 1/2
  117. // 11: 1/4
  118. static volatile __sfr __at(0x1C) rNR32;
  119. #define rAUD3LEVEL rNR32
  120. // AUD3LOW/NR33 ($FF1D)
  121. // Frequency low byte (W)
  122. //
  123. // see AUD1LOW for info
  124. static volatile __sfr __at(0x1D) rNR33;
  125. #define rAUD3LOW rNR33
  126. // AUD3HIGH/NR34 ($FF1E)
  127. // Frequency high byte (W)
  128. //
  129. // see AUD1HIGH for info
  130. static volatile __sfr __at(0x1E) rNR34;
  131. #define rAUD3HIGH rNR34
  132. // AUD4LEN/NR41 ($FF20)
  133. // Sound length (R/W)
  134. //
  135. // Bit 5-0 - Sound length data (# 0-63)
  136. static volatile __sfr __at(0x20) rNR41;
  137. #define rAUD4LEN rNR41
  138. // AUD4ENV/NR42 ($FF21)
  139. // Envelope (R/W)
  140. //
  141. // see AUD1ENV for info
  142. static volatile __sfr __at(0x21) rNR42;
  143. #define rAUD4ENV rNR42
  144. // AUD4POLY/NR43 ($FF22)
  145. // Polynomial counter (R/W)
  146. //
  147. // Bit 7-4 - Selection of the shift clock frequency of the (scf)
  148. // polynomial counter (0000-1101)
  149. // freq=drf*1/2^scf (not sure)
  150. // Bit 3 - Selection of the polynomial counter's step
  151. // 0: 15 steps
  152. // 1: 7 steps
  153. // Bit 2-0 - Selection of the dividing ratio of frequencies (drf)
  154. // 000: f/4 001: f/8 010: f/16 011: f/24
  155. // 100: f/32 101: f/40 110: f/48 111: f/56 (f=4.194304 Mhz)
  156. static volatile __sfr __at(0x22) rNR43;
  157. #define rAUD4POLY rNR43
  158. // AUD4GO/NR44 ($FF23)
  159. //
  160. // Bit 7 - Inital
  161. // Bit 6 - Counter/consecutive selection
  162. static volatile __sfr __at(0x23) rNR44;
  163. #define rAUD4GO rNR44
  164. // AUDVOL/NR50 ($FF24)
  165. // Channel control / ON-OFF / Volume (R/W)
  166. //
  167. // Bit 7 - Vin->SO2 ON/OFF (Vin??)
  168. // Bit 6-4 - SO2 output level (volume) (# 0-7)
  169. // Bit 3 - Vin->SO1 ON/OFF (Vin??)
  170. // Bit 2-0 - SO1 output level (volume) (# 0-7)
  171. static volatile __sfr __at(0x24) rNR50;
  172. #define rAUDVOL rNR50
  173. #define AUDVOL_VIN_LEFT 0b10000000 // SO2
  174. #define AUDVOL_VIN_RIGHT 0b00001000 // SO1
  175. // AUDTERM/NR51 ($FF25)
  176. // Selection of Sound output terminal (R/W)
  177. //
  178. // Bit 7 - Output sound 4 to SO2 terminal
  179. // Bit 6 - Output sound 3 to SO2 terminal
  180. // Bit 5 - Output sound 2 to SO2 terminal
  181. // Bit 4 - Output sound 1 to SO2 terminal
  182. // Bit 3 - Output sound 4 to SO1 terminal
  183. // Bit 2 - Output sound 3 to SO1 terminal
  184. // Bit 1 - Output sound 2 to SO1 terminal
  185. // Bit 0 - Output sound 0 to SO1 terminal
  186. static volatile __sfr __at(0x25) rNR51;
  187. #define rAUDTERM rNR51
  188. // SO2
  189. #define AUDTERM_4_LEFT 0b10000000
  190. #define AUDTERM_3_LEFT 0b01000000
  191. #define AUDTERM_2_LEFT 0b00100000
  192. #define AUDTERM_1_LEFT 0b00010000
  193. // SO1
  194. #define AUDTERM_4_RIGHT 0b00001000
  195. #define AUDTERM_3_RIGHT 0b00000100
  196. #define AUDTERM_2_RIGHT 0b00000010
  197. #define AUDTERM_1_RIGHT 0b00000001
  198. // AUDENA/NR52 ($FF26)
  199. // Sound on/off (R/W)
  200. //
  201. // Bit 7 - All sound on/off (sets all audio regs to 0!)
  202. // Bit 3 - Sound 4 ON flag (read only)
  203. // Bit 2 - Sound 3 ON flag (read only)
  204. // Bit 1 - Sound 2 ON flag (read only)
  205. // Bit 0 - Sound 1 ON flag (read only)
  206. static volatile __sfr __at(0x26) rNR52;
  207. #define rAUDENA rNR52
  208. #define AUDENA_ON 0b10000000
  209. #define AUDENA_OFF 0b00000000 // sets all audio regs to 0!
  210. // LCDC ($FF40)
  211. // LCD Control (R/W)
  212. static volatile __sfr __at(0x40) rLCDC;
  213. #define LCDC_OFF 0b00000000 // LCD Control Operation
  214. #define LCDC_ON 0b10000000 // LCD Control Operation
  215. #define LCDC_WIN9800 0b00000000 // Window Tile Map Display Select
  216. #define LCDC_WIN9C00 0b01000000 // Window Tile Map Display Select
  217. #define LCDC_WINOFF 0b00000000 // Window Display
  218. #define LCDC_WINON 0b00100000 // Window Display
  219. #define LCDC_BG8800 0b00000000 // BG & Window Tile Data Select
  220. #define LCDC_BG8000 0b00010000 // BG & Window Tile Data Select
  221. #define LCDC_BG9800 0b00000000 // BG Tile Map Display Select
  222. #define LCDC_BG9C00 0b00001000 // BG Tile Map Display Select
  223. #define LCDC_OBJ8 0b00000000 // OBJ Construction
  224. #define LCDC_OBJ16 0b00000100 // OBJ Construction
  225. #define LCDC_OBJOFF 0b00000000 // OBJ Display
  226. #define LCDC_OBJON 0b00000010 // OBJ Display
  227. #define LCDC_BGOFF 0b00000000 // BG Display
  228. #define LCDC_BGON 0b00000001 // BG Display
  229. // STAT ($FF41)
  230. // LCDC Status (R/W)
  231. static volatile __sfr __at(0x41) rSTAT;
  232. #define STAT_LYC 0b01000000 // LYC=LY Coincidence (Selectable)
  233. #define STAT_MODE10 0b00100000 // Mode 10
  234. #define STAT_MODE01 0b00010000 // Mode 01 (V-Blank)
  235. #define STAT_MODE00 0b00001000 // Mode 00 (H-Blank)
  236. #define STAT_LYCF 0b00000100 // Coincidence Flag
  237. #define STAT_HBL 0b00000000 // H-Blank
  238. #define STAT_VBL 0b00000001 // V-Blank
  239. #define STAT_OAM 0b00000010 // OAM-RAM is used by system
  240. #define STAT_LCD 0b00000011 // Both OAM and VRAM used by system
  241. #define STAT_BUSY 0b00000010 // When set, VRAM access is unsafe
  242. // SCY ($FF42)
  243. // Scroll Y (R/W)
  244. static volatile __sfr __at(0x42) rSCY;
  245. // SCX ($FF43)
  246. // Scroll X (R/W)
  247. static volatile __sfr __at(0x43) rSCX;
  248. // LY ($FF44)
  249. // LCDC Y-Coordinate (R)
  250. //
  251. // Values range from 0->153. 144->153 is the VBlank period.
  252. static volatile __sfr __at(0x44) rLY;
  253. // LYC ($FF45)
  254. // LY Compare (R/W)
  255. //
  256. // When LY==LYC, STATF_LYCF will be set in STAT
  257. static volatile __sfr __at(0x45) rLYC;
  258. // DMA ($FF46)
  259. // DMA Transfer and Start Address (W)
  260. static volatile __sfr __at(0x46) rDMA;
  261. // BGP ($FF47)
  262. // BG Palette Data (W)
  263. //
  264. // Bit 7-6 - Intensity for %11
  265. // Bit 5-4 - Intensity for %10
  266. // Bit 3-2 - Intensity for %01
  267. // Bit 1-0 - Intensity for %00
  268. static volatile __sfr __at(0x47) rBGP;
  269. // OBP0 ($FF48)
  270. // Object Palette 0 Data (W)
  271. //
  272. // See BGP for info
  273. static volatile __sfr __at(0x48) rOBP0;
  274. // OBP1 ($FF49)
  275. // Object Palette 1 Data (W)
  276. //
  277. // See BGP for info
  278. static volatile __sfr __at(0x49) rOBP1;
  279. // WY ($FF4A)
  280. // Window Y Position (R/W)
  281. //
  282. // 0 <= WY <= 143
  283. // When WY = 0, the window is displayed from the top edge of the LCD screen.
  284. static volatile __sfr __at(0x4A) rWY;
  285. // WX ($FF4B)
  286. // Window X Position (R/W)
  287. //
  288. // 7 <= WX <= 166
  289. // When WX = 7, the window is displayed from the left edge of the LCD screen.
  290. // Values of 0-6 and 166 are unreliable due to hardware bugs.
  291. static volatile __sfr __at(0x4B) rWX;
  292. #if CGB
  293. // SPEED ($FF4D)
  294. // Select CPU Speed (R/W)
  295. static volatile __sfr __at(0x4D) rKEY1;
  296. #define rSPD rKEY1
  297. #define KEY1_DBLSPEED 0b10000000 // 0=Normal Speed, 1=Double Speed (R)
  298. #define KEY1_PREPARE 0b00000001 // 0=No, 1=Prepare (R/W)
  299. // VBK ($FF4F)
  300. // Select Video RAM Bank (R/W)
  301. //
  302. // Bit 0 - Bank Specification (0: Specify Bank 0; 1: Specify Bank 1)
  303. static volatile __sfr __at(0x4F) rVBK;
  304. // HDMA1 ($FF51)
  305. // High byte for Horizontal Blanking/General Purpose DMA source address (W)
  306. static volatile __sfr __at(0x51) rHDMA1;
  307. // HDMA2 ($FF52)
  308. // Low byte for Horizontal Blanking/General Purpose DMA source address (W)
  309. static volatile __sfr __at(0x52) rHDMA2;
  310. // HDMA3 ($FF53)
  311. // High byte for Horizontal Blanking/General Purpose DMA destination address (W)
  312. static volatile __sfr __at(0x53) rHDMA3;
  313. // HDMA4 ($FF54)
  314. // Low byte for Horizontal Blanking/General Purpose DMA destination address (W)
  315. static volatile __sfr __at(0x54) rHDMA4;
  316. // HDMA5 ($FF55)
  317. // Transfer length (in tiles minus 1)/mode/start for Horizontal Blanking, General Purpose DMA (R/W)
  318. static volatile __sfr __at(0x55) rHDMA5;
  319. #define HDMA5_MODE_GP 0b00000000 // General Purpose DMA (W)
  320. #define HDMA5_MODE_HBL 0b10000000 // HBlank DMA (W)
  321. // Once DMA has started, use HDMA5F_BUSY to check when the transfer is complete
  322. #define HDMA5_BUSY 0b10000000 // 0=Busy (DMA still in progress), 1=Transfer complete (R)
  323. // RP ($FF56)
  324. // Infrared Communications Port (R/W)
  325. static volatile __sfr __at(0x56) rRP;
  326. #define RP_ENREAD 0b11000000
  327. #define RP_DATAIN 0b00000010 // 0=Receiving IR Signal, 1=Normal
  328. #define RP_WRITE_HI 0b00000001
  329. #define RP_WRITE_LO 0b00000000
  330. // BCPS ($FF68)
  331. // Background Color Palette Specification (R/W)
  332. static volatile __sfr __at(0x68) rBCPS;
  333. #define BCPS_AUTOINC 0b10000000 // Auto Increment (0=Disabled, 1=Increment after Writing)
  334. // BCPD ($FF69)
  335. // Background Color Palette Data (R/W)
  336. static volatile __sfr __at(0x69) rBCPD;
  337. // OCPS ($FF6A)
  338. // Object Color Palette Specification (R/W)
  339. static volatile __sfr __at(0x6A) rOCPS;
  340. #define OCPS_AUTOINC 0b10000000 // Auto Increment (0=Disabled, 1=Increment after Writing)
  341. // OCPD ($FF6B)
  342. // Object Color Palette Data (R/W)
  343. static volatile __sfr __at(0x6B) rOCPD;
  344. // SMBK/SVBK ($FF70)
  345. // Select Main RAM Bank (R/W)
  346. //
  347. // Bit 2-0 - Bank Specification (0,1: Specify Bank 1; 2-7: Specify Banks 2-7)
  348. static volatile __sfr __at(0x70) rSVBK;
  349. #define rSMBK rSVBK
  350. // PCM12 ($FF76)
  351. // Sound channel 1&2 PCM amplitude (R)
  352. //
  353. // Bit 7-4 - Copy of sound channel 2's PCM amplitude
  354. // Bit 3-0 - Copy of sound channel 1's PCM amplitude
  355. static volatile __sfr __at(0x76) rPCM12;
  356. // PCM34 ($FF77)
  357. // Sound channel 3&4 PCM amplitude (R)
  358. //
  359. // Bit 7-4 - Copy of sound channel 4's PCM amplitude
  360. // Bit 3-0 - Copy of sound channel 3's PCM amplitude
  361. static volatile __sfr __at(0x77) rPCM34;
  362. #endif //CGB
  363. // IE ($FFFF)
  364. // Interrupt Enable (R/W)
  365. static volatile __sfr __at(0xFF) rIE;
  366. #define IE_HILO 0b00010000 // Transition from High to Low of Pin number P10-P13
  367. #define IE_SERIAL 0b00001000 // Serial I/O transfer end
  368. #define IE_TIMER 0b00000100 // Timer Overflow
  369. #define IE_LCDC 0b00000010 // LCDC (see STAT)
  370. #define IE_VBLANK 0b00000001 // V-Blank
  371. /***************************************************************************
  372. *
  373. * Flags common to multiple sound channels
  374. *
  375. ***************************************************************************/
  376. // Square wave duty cycle
  377. //
  378. // Can be used with AUD1LEN and AUD2LEN
  379. // See AUD1LEN for more info
  380. #define AUDLEN_DUTY_12_5 0b00000000 // 12.5%
  381. #define AUDLEN_DUTY_25 0b01000000 // 25%
  382. #define AUDLEN_DUTY_50 0b10000000 // 50%
  383. #define AUDLEN_DUTY_75 0b11000000 // 75%
  384. // Audio envelope flags
  385. //
  386. // Can be used with AUD1ENV, AUD2ENV, AUD4ENV
  387. // See AUD1ENV for more info
  388. #define AUDENV_UP 0b00001000
  389. #define AUDENV_DOWN 0b00000000
  390. // Audio trigger flags
  391. //
  392. // Can be used with AUD1HIGH, AUD2HIGH, AUD3HIGH
  393. // See AUD1HIGH for more info
  394. #define AUDHIGH_RESTART 0b10000000
  395. #define AUDHIGH_LENGTH_ON 0b01000000
  396. #define AUDHIGH_LENGTH_OFF 0b00000000
  397. // Shared bits between the OAM attributes and the CGB background attributes.
  398. #define ATTR_PRI 0b10000000
  399. #define ATTR_YFLIP 0b01000000
  400. #define ATTR_XFLIP 0b00100000
  401. #define ATTR_PAL0 0b00000000
  402. #define ATTR_PAL1 0b00010000
  403. #if CGB
  404. #define ATTR_BANK0 0b00000000
  405. #define ATTR_BANK1 0b00001000
  406. #define ATTR_PALMASK 0b00000111
  407. #endif
  408. // Defines for specific instructions
  409. #define HALT() __asm__("halt")
  410. #define DISABLE_INTERRUPTS() __asm__("di")
  411. #define ENABLE_INTERRUPTS() __asm__("ei")
  412. #endif