atm959

main.asm

Mar 25th, 2016
260
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.56 KB | None | 0 0
  1. ;------------------------------------------------------------------------
  2. ;- Written by Bazz
  3. ;- This code demonstrates displaying a simple tile on the screen.
  4. ;- This can be expanded on easily, and will be used as a base for
  5. ;- later examples.
  6. ;-
  7. ;- All I want is feedback, so please tell me if my tutorials suck,
  8. ;- are decent, or whatever. I'd also like to know what needs
  9. ;- better explanations and so on. I must improve the tutorials!
  10. ;------------------------------------------------------------------------
  11.  
  12.  
  13. ;============================================================================
  14. ; Includes
  15. ;============================================================================
  16.  
  17. ;== Include MemoryMap, Vector Table, and HeaderInfo ==
  18. .INCLUDE "header.inc"
  19.  
  20. ;== Include SNES Initialization routines ==
  21. .INCLUDE "InitSNES.asm"
  22.  
  23. ;============================================================================
  24. ; Macros
  25. ;============================================================================
  26. ;============================================================================
  27. ;LoadPalette - Macro that loads palette information into CGRAM
  28. ;----------------------------------------------------------------------------
  29. ; In: SRC_ADDR -- 24 bit address of source data,
  30. ; START -- Color # to start on,
  31. ; SIZE -- # of COLORS to copy
  32. ;----------------------------------------------------------------------------
  33. ; Out: None
  34. ;----------------------------------------------------------------------------
  35. ; Modifies: A,X,Y
  36. ; Requires: mem/A = 8 bit, X/Y = 16 bit
  37. ;----------------------------------------------------------------------------
  38. .MACRO LoadPalette
  39. lda #\2
  40. sta $2121 ; Start at START color
  41. lda #:\1 ; Using : before the parameter gets its bank.
  42. ldx #\1 ; Not using : gets the offset address.
  43. ldy #(\3 * 2) ; 2 bytes for every color
  44. jsr DMAPalette
  45. .ENDM
  46.  
  47. ;============================================================================
  48. ; LoadBlockToVRAM -- Macro that simplifies calling LoadVRAM to copy data to VRAM
  49. ;----------------------------------------------------------------------------
  50. ; In: SRC_ADDR -- 24 bit address of source data
  51. ; DEST -- VRAM address to write to (WORD address!!)
  52. ; SIZE -- number of BYTEs to copy
  53. ;----------------------------------------------------------------------------
  54. ; Out: None
  55. ;----------------------------------------------------------------------------
  56. ; Modifies: A, X, Y
  57. ;----------------------------------------------------------------------------
  58.  
  59. ;LoadBlockToVRAM SRC_ADDRESS, DEST, SIZE
  60. ; requires: mem/A = 8 bit, X/Y = 16 bit
  61. .MACRO LoadBlockToVRAM
  62. lda #$80
  63. sta $2115
  64. ldx #\2 ; DEST
  65. stx $2116 ; $2116: Word address for accessing VRAM.
  66. lda #:\1 ; SRCBANK
  67. ldx #\1 ; SRCOFFSET
  68. ldy #\3 ; SIZE
  69. jsr LoadVRAM
  70. .ENDM
  71.  
  72.  
  73.  
  74. ;============================================================================
  75. ; Main Code
  76. ;============================================================================
  77.  
  78. .BANK 0 SLOT 0
  79. .ORG 0
  80. .SECTION "MainCode"
  81.  
  82. VBlank:
  83. RTI
  84.  
  85. Main:
  86. InitializeSNES ; Clear registers, etc.
  87.  
  88. ; Load Palette for our tiles
  89. LoadPalette BG_Palette, 0, 4
  90.  
  91. ; Load Tile data to VRAM
  92. LoadBlockToVRAM Tiles, $0000, $0040
  93.  
  94. ; Now, load up some data into our tile map
  95. ; (If you had an full map, you could use LoadBlockToVRAM)
  96. ; Remember that in the default map, all entries point to tile #0
  97. lda #$80
  98. sta $2115
  99. ldx #$0400
  100. stx $2116
  101. lda #$02
  102. sta $2118
  103.  
  104. lda #$80
  105. sta $2115
  106. ldx #$0401
  107. stx $2116
  108. lda #$02
  109. sta $2118
  110.  
  111. lda #$80
  112. sta $2115
  113. ldx #$0420
  114. stx $2116
  115. lda #$02
  116. sta $2118
  117.  
  118. lda #$80
  119. sta $2115
  120. ldx #$0421
  121. stx $2116
  122. lda #$02
  123. sta $2118
  124.  
  125. lda #$80
  126. sta $2115
  127. ldx #$0440
  128. stx $2116
  129. lda #$02
  130. sta $2118
  131.  
  132. lda #$80
  133. sta $2115
  134. ldx #$0441
  135. stx $2116
  136. lda #$02
  137. sta $2118
  138.  
  139. lda #$80
  140. sta $2115
  141. ldx #$0460
  142. stx $2116
  143. lda #$02
  144. sta $2118
  145.  
  146. lda #$80
  147. sta $2115
  148. ldx #$0461
  149. stx $2116
  150. lda #$02
  151. sta $2118
  152.  
  153. lda #$80
  154. sta $2115
  155. ldx #$0480
  156. stx $2116
  157. lda #$02
  158. sta $2118
  159.  
  160. lda #$80
  161. sta $2115
  162. ldx #$0481
  163. stx $2116
  164. lda #$02
  165. sta $2118
  166.  
  167. lda #$80
  168. sta $2115
  169. ldx #$0480 + 32
  170. stx $2116
  171. lda #$02
  172. sta $2118
  173.  
  174. lda #$80
  175. sta $2115
  176. ldx #$0481 + 32
  177. stx $2116
  178. lda #$02
  179. sta $2118
  180.  
  181. lda #$80
  182. sta $2115
  183. ldx #$0480 + 64
  184. stx $2116
  185. lda #$02
  186. sta $2118
  187.  
  188. lda #$80
  189. sta $2115
  190. ldx #$0481 + 64
  191. stx $2116
  192. lda #$03
  193. sta $2118
  194.  
  195. ; ================
  196. ; END OF TILE DATA
  197. ;=================
  198.  
  199. ; Setup Video modes and other stuff, then turn on the screen
  200. jsr SetupVideo
  201.  
  202. Infinity:
  203. jmp Infinity ; bwa hahahahaha
  204.  
  205.  
  206. ;============================================================================
  207. ; SetupVideo -- Sets up the video mode and tile-related registers
  208. ;----------------------------------------------------------------------------
  209. ; In: None
  210. ;----------------------------------------------------------------------------
  211. ; Out: None
  212. ;----------------------------------------------------------------------------
  213. SetupVideo:
  214. php
  215.  
  216. sep #$20 ; 8bit A, 16bit X/Y
  217.  
  218. ; DMA sprite data
  219. stz $2102
  220. stz $2103 ; Set OAM address to 0
  221.  
  222. ldy #$0400 ; Writes #$00 to $4300, #$04 to $4301
  223. sty $4300 ; CPU -> PPU, auto inc, $2104 (OAM write)
  224. stz $4302
  225. stz $4303
  226. lda #$7E
  227. sta $4304 ; CPU address 7E:0000 - Work RAM
  228. ldy #$0220
  229. sty $4305 ; #$220 bytes to transfer
  230. lda #$01
  231. sta $420B
  232.  
  233. lda #%10100000 ; 32x32 and 64x64 size sprites (we are using a 32x32)
  234. sta $2101
  235.  
  236. lda #%00010000 ; Enable Sprites
  237. sta $212C
  238.  
  239. lda #$00
  240. sta $2105 ; Set Video mode 0, 8x8 tiles, 4 color BG1/BG2/BG3/BG4
  241.  
  242. lda #$04 ; Set BG1's Tile Map offset to $0400 (Word address)
  243. sta $2107 ; And the Tile Map size to 32x32
  244.  
  245. stz $210B ; Set BG1's Character VRAM offset to $0000 (word address)
  246.  
  247. lda #$01 ; Enable BG1
  248. sta $212C
  249.  
  250. lda #$FF
  251. sta $210E
  252. sta $210E
  253.  
  254. lda #$0F
  255. sta $2100 ; Turn on screen, full Brightness
  256.  
  257. plp
  258. rts
  259. ;============================================================================
  260.  
  261. ;============================================================================
  262. ; LoadVRAM -- Load data into VRAM
  263. ;----------------------------------------------------------------------------
  264. ; In: A:X -- points to the data
  265. ; Y -- Number of bytes to copy (0 to 65535) (assumes 16-bit index)
  266. ;----------------------------------------------------------------------------
  267. ; Out: None
  268. ;----------------------------------------------------------------------------
  269. ; Modifies: none
  270. ;----------------------------------------------------------------------------
  271. ; Notes: Assumes VRAM address has been previously set!!
  272. ;----------------------------------------------------------------------------
  273. LoadVRAM:
  274. php ; Preserve Registers
  275.  
  276. stx $4302 ; Store Data offset into DMA source offset
  277. sta $4304 ; Store data Bank into DMA source bank
  278. sty $4305 ; Store size of data block
  279.  
  280. lda #$01
  281. sta $4300 ; Set DMA mode (word, normal increment)
  282. lda #$18 ; Set the destination register (VRAM write register)
  283. sta $4301
  284. lda #$01 ; Initiate DMA transfer (channel 1)
  285. sta $420B
  286.  
  287. plp ; restore registers
  288. rts ; return
  289. ;============================================================================
  290.  
  291. ;============================================================================
  292. ; DMAPalette -- Load entire palette using DMA
  293. ;----------------------------------------------------------------------------
  294. ; In: A:X -- points to the data
  295. ; Y -- Size of data
  296. ;----------------------------------------------------------------------------
  297. ; Out: None
  298. ;----------------------------------------------------------------------------
  299. ; Modifies: none
  300. ;----------------------------------------------------------------------------
  301. DMAPalette:
  302. php ; Preserve Registers
  303.  
  304. stx $4302 ; Store data offset into DMA source offset
  305. sta $4304 ; Store data bank into DMA source bank
  306. sty $4305 ; Store size of data block
  307.  
  308. stz $4300 ; Set DMA Mode (byte, normal increment)
  309. lda #$22 ; Set destination register ($2122 - CGRAM Write)
  310. sta $4301
  311. lda #$01 ; Initiate DMA transfer
  312. sta $420B
  313.  
  314. plp
  315. rts ; return from subroutine
  316.  
  317.  
  318. .ENDS
  319.  
  320. ;============================================================================
  321. ; Character Data
  322. ;============================================================================
  323. .BANK 1 SLOT 0
  324. .ORG 0
  325. .SECTION "CharacterData"
  326.  
  327. .INCLUDE "tiles.inc"
  328.  
  329. .ENDS
Advertisement
Add Comment
Please, Sign In to add comment