atm959

main.asm (SNES Program)

Jun 20th, 2016
292
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.25 KB | None | 0 0
  1. ;============================================================================
  2. ; Includes
  3. ;============================================================================
  4.  
  5. ;== Include MemoryMap, Vector Table, and HeaderInfo ==
  6. .INCLUDE "header.inc"
  7.  
  8. ;== Include SNES Initialization routines ==
  9. .INCLUDE "InitSNES.asm"
  10.  
  11. ;============================================================================
  12. ; Macros
  13. ;============================================================================
  14. ;============================================================================
  15. ;LoadPalette - Macro that loads palette information into CGRAM
  16. ;----------------------------------------------------------------------------
  17. ; In: SRC_ADDR -- 24 bit address of source data,
  18. ; START -- Color # to start on,
  19. ; SIZE -- # of COLORS to copy
  20. ;----------------------------------------------------------------------------
  21. ; Out: None
  22. ;----------------------------------------------------------------------------
  23. ; Modifies: A,X,Y
  24. ; Requires: mem/A = 8 bit, X/Y = 16 bit
  25. ;----------------------------------------------------------------------------
  26. .MACRO LoadPalette
  27. lda #\2
  28. sta $2121 ; Start at START color
  29. lda #:\1 ; Using : before the parameter gets its bank.
  30. ldx #\1 ; Not using : gets the offset address.
  31. ldy #(\3 * 2) ; 2 bytes for every color
  32. jsr DMAPalette
  33. .ENDM
  34.  
  35. ;============================================================================
  36. ; LoadBlockToVRAM -- Macro that simplifies calling LoadVRAM to copy data to VRAM
  37. ;----------------------------------------------------------------------------
  38. ; In: SRC_ADDR -- 24 bit address of source data
  39. ; DEST -- VRAM address to write to (WORD address!!)
  40. ; SIZE -- number of BYTEs to copy
  41. ;----------------------------------------------------------------------------
  42. ; Out: None
  43. ;----------------------------------------------------------------------------
  44. ; Modifies: A, X, Y
  45. ;----------------------------------------------------------------------------
  46.  
  47. ;LoadBlockToVRAM SRC_ADDRESS, DEST, SIZE
  48. ; requires: mem/A = 8 bit, X/Y = 16 bit
  49. .MACRO LoadBlockToVRAM
  50. lda #$80
  51. sta $2115
  52. ldx #\2 ; DEST
  53. stx $2116 ; $2116: Word address for accessing VRAM.
  54. lda #:\1 ; SRCBANK
  55. ldx #\1 ; SRCOFFSET
  56. ldy #\3 ; SIZE
  57. jsr LoadVRAM
  58. .ENDM
  59.  
  60.  
  61.  
  62. ;============================================================================
  63. ; Main Code
  64. ;============================================================================
  65.  
  66. .BANK 0 SLOT 0
  67. .ORG 0
  68. .SECTION "MainCode"
  69.  
  70. VBlank:
  71. RTI
  72. RTS
  73.  
  74. Main:
  75. InitializeSNES ; Clear registers, etc.
  76.  
  77. ; Load Palette for our tiles
  78. LoadPalette BG_Palette, 0, 4
  79.  
  80. ; Load Tile data to VRAM
  81. LoadBlockToVRAM Tiles, $0000, (8*2*4) ; $0000
  82.  
  83. ; Setup Video modes and other stuff, then turn on the screen
  84. jsr SetupVideo
  85.  
  86. ;Start the game.
  87. lda #$80
  88. sta $2115
  89. ldx #$0400 ; Tile location
  90. stx $2116
  91. lda #$02
  92. sta $2118
  93.  
  94. Infinity:
  95. lda #$80
  96. sta $2115
  97. inx
  98. stx $2116
  99. lda #$02
  100. sta $2118
  101. wai
  102. jsr Infinity ; Loop.
  103.  
  104.  
  105. ;============================================================================
  106. ; SetupVideo -- Sets up the video mode and tile-related registers
  107. ;----------------------------------------------------------------------------
  108. ; In: None
  109. ;----------------------------------------------------------------------------
  110. ; Out: None
  111. ;----------------------------------------------------------------------------
  112. SetupVideo:
  113. php
  114.  
  115. sep #$20 ; 8bit A, 16bit X/Y
  116.  
  117. ; DMA sprite data
  118. stz $2102
  119. stz $2103 ; Set OAM address to 0
  120.  
  121. ldy #$0400 ; Writes #$00 to $4300, #$04 to $4301
  122. sty $4300 ; CPU -> PPU, auto inc, $2104 (OAM write)
  123. stz $4302
  124. stz $4303
  125. lda #$7E
  126. sta $4304 ; CPU address 7E:0000 - Work RAM
  127. ldy #$0220
  128. sty $4305 ; #$220 bytes to transfer
  129. lda #$01
  130. sta $420B
  131.  
  132. lda #%10100000 ; 32x32 and 64x64 size sprites (we are using a 32x32)
  133. sta $2101
  134.  
  135. lda #%00010000 ; Enable Sprites
  136. sta $212C
  137.  
  138. lda #$00
  139. sta $2105 ; Set Video mode 0, 8x8 tiles, 4 color BG1/BG2/BG3/BG4
  140.  
  141. lda #$04 ; Set BG1's Tile Map offset to $0400 (Word address)
  142. sta $2107 ; And the Tile Map size to 32x32
  143.  
  144. stz $210B ; Set BG1's Character VRAM offset to $0000 (word address)
  145.  
  146. lda #$01 ; Enable BG1
  147. sta $212C
  148.  
  149. lda #$FF
  150. sta $210E
  151. sta $210E
  152.  
  153. lda #$0F
  154. sta $2100 ; Turn on screen, full Brightness
  155.  
  156. plp
  157. rts
  158. ;============================================================================
  159.  
  160. ;============================================================================
  161. ; LoadVRAM -- Load data into VRAM
  162. ;----------------------------------------------------------------------------
  163. ; In: A:X -- points to the data
  164. ; Y -- Number of bytes to copy (0 to 65535) (assumes 16-bit index)
  165. ;----------------------------------------------------------------------------
  166. ; Out: None
  167. ;----------------------------------------------------------------------------
  168. ; Modifies: none
  169. ;----------------------------------------------------------------------------
  170. ; Notes: Assumes VRAM address has been previously set!!
  171. ;----------------------------------------------------------------------------
  172. LoadVRAM:
  173. php ; Preserve Registers
  174.  
  175. stx $4302 ; Store Data offset into DMA source offset
  176. sta $4304 ; Store data Bank into DMA source bank
  177. sty $4305 ; Store size of data block
  178.  
  179. lda #$01
  180. sta $4300 ; Set DMA mode (word, normal increment)
  181. lda #$18 ; Set the destination register (VRAM write register)
  182. sta $4301
  183. lda #$01 ; Initiate DMA transfer (channel 1)
  184. sta $420B
  185.  
  186. plp ; restore registers
  187. rts ; return
  188. ;============================================================================
  189.  
  190. ;============================================================================
  191. ; DMAPalette -- Load entire palette using DMA
  192. ;----------------------------------------------------------------------------
  193. ; In: A:X -- points to the data
  194. ; Y -- Size of data
  195. ;----------------------------------------------------------------------------
  196. ; Out: None
  197. ;----------------------------------------------------------------------------
  198. ; Modifies: none
  199. ;----------------------------------------------------------------------------
  200. DMAPalette:
  201. php ; Preserve Registers
  202.  
  203. stx $4302 ; Store data offset into DMA source offset
  204. sta $4304 ; Store data bank into DMA source bank
  205. sty $4305 ; Store size of data block
  206.  
  207. stz $4300 ; Set DMA Mode (byte, normal increment)
  208. lda #$22 ; Set destination register ($2122 - CGRAM Write)
  209. sta $4301
  210. lda #$01 ; Initiate DMA transfer
  211. sta $420B
  212.  
  213. plp
  214. rts ; return from subroutine
  215.  
  216.  
  217. .ENDS
  218.  
  219. ;============================================================================
  220. ; Character Data
  221. ;============================================================================
  222. .BANK 1 SLOT 0
  223. .ORG 0
  224. .SECTION "CharacterData"
  225.  
  226. .INCLUDE "tiles.inc"
  227.  
  228. .ENDS
Advertisement
Add Comment
Please, Sign In to add comment