atm959

InitSNES.asm

Sep 30th, 2016
309
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.25 KB | None | 0 0
  1. ;------------------------------------------------------------------------
  2. ;- Written by: Neviksti
  3. ;- If you use my code, please share your creations with me
  4. ;- as I am always curious :)
  5. ;------------------------------------------------------------------------
  6.  
  7.  
  8. ;----------------------------------------------------------------------------
  9. ; InitializeSNES -- my "standard" initialization of SNES memory and registers
  10. ;----------------------------------------------------------------------------
  11. .MACRO InitializeSNES
  12. sei ;disable interrupts
  13. clc ;switch to native mode
  14. xce
  15.  
  16. REP #$38 ; mem/A = 16 bit, X/Y = 16 bit
  17. ;decimal mode off
  18.  
  19. LDX #$1FFF ;Setup the stack
  20. TXS ;Transfer Index X to Stack Pointer Register
  21.  
  22. ;do the rest of the initialization in a routine
  23. JSL $008000
  24.  
  25. SEP #$20 ; mem/A = 8 bit
  26. .ENDM
  27.  
  28. ;----------------------------------------------------------------------------
  29.  
  30. .BANK 0 SLOT 0
  31. .ORG HEADER_OFF
  32. .SECTION "InitSNESCode" FORCE
  33.  
  34. InitSNES:
  35. PHK ;set Data Bank = Program Bank
  36. PLB
  37.  
  38. LDA #$0000 ;set Direct Page = $0000
  39. TCD ;Transfer Accumulator to Direct Register
  40.  
  41. LDX $1FFD ;we clear all the mem at one point ...
  42. STX $4372 ;so save the return address in a place that won't get overwritten
  43. LDX $1FFF
  44. STX $4374
  45.  
  46. SEP #$20 ; mem/A = 8 bit
  47.  
  48. LDA #$8F
  49. STA $2100 ;turn screen off for now, set brightness to normal
  50.  
  51. LDX #$2101
  52. _Loop00: ;regs $2101-$210C
  53. STZ $00,X ;set Sprite,Character,Tile sizes to lowest, and set addresses to $0000
  54. INX
  55. CPX #$210D
  56. BNE _Loop00
  57.  
  58. _Loop01: ;regs $210D-$2114
  59. STZ $00,X ;Set all BG scroll values to $0000
  60. STZ $00,X
  61. INX
  62. CPX #$2115
  63. BNE _Loop01
  64.  
  65. LDA #$80 ;reg $2115
  66. STA $2115 ; Initialize VRAM transfer mode to word-access, increment by 1
  67.  
  68. STZ $2116 ;regs $2117-$2117
  69. STZ $2117 ;VRAM address = $0000
  70.  
  71. ;reg $2118-$2119
  72. ;VRAM write register... don't need to initialize
  73.  
  74. STZ $211A ;clear Mode7 setting
  75.  
  76. LDX #$211B
  77. _Loop02: ;regs $211B-$2120
  78. STZ $00,X ;clear out the Mode7 matrix values
  79. STZ $00,X
  80. INX
  81. CPX #$2121
  82. BNE _Loop02
  83.  
  84. ;reg $2121 - Color address, doesn't need initilaizing
  85. ;reg $2122 - Color data, is initialized later
  86.  
  87. LDX #$2123
  88. _Loop03: ;regs $2123-$2133
  89. STZ $00,X ;turn off windows, main screens, sub screens, color addition,
  90. INX ;fixed color = $00, no super-impose (external synchronization),
  91. CPX #$2134 ;no interlaced mode, normal resolution
  92. BNE _Loop03
  93.  
  94. ;regs $2134-$2136 - multiplication result, no initialization needed
  95. ;reg $2137 - software H/V latch, no initialization needed
  96. ;reg $2138 - Sprite data read, no initialization needed
  97. ;regs $2139-$213A - VRAM data read, no initialization needed
  98. ;reg $213B - Color RAM data read, no initialization needed
  99. ;regs $213C-$213D - H/V latched data read, no initialization needed
  100.  
  101. STZ $213E ;reg $213E - might not be necesary, but selects PPU master/slave mode
  102. ;reg $213F - PPU status flag, no initialization needed
  103.  
  104. ;reg $2140-$2143 - APU communication regs, no initialization required
  105.  
  106. ;reg $2180 - read/write WRAM register, no initialization required
  107. ;reg $2181-$2183 - WRAM address, no initialization required
  108.  
  109. ;reg $4016-$4017 - serial JoyPad read registers, no need to initialize
  110.  
  111.  
  112. STZ $4200 ;reg $4200 - disable timers, NMI,and auto-joyread
  113.  
  114. LDA #$FF
  115. STA $4201 ;reg $4201 - programmable I/O write port, initalize to allow reading at in-port
  116.  
  117. ;regs $4202-$4203 - multiplication registers, no initialization required
  118. ;regs $4204-$4206 - division registers, no initialization required
  119.  
  120. ;regs $4207-$4208 - Horizontal-IRQ timer setting, since we disabled this, it is OK to not init
  121. ;regs $4209-$420A - Vertical-IRQ timer setting, since we disabled this, it is OK to not init
  122.  
  123. STZ $420B ;reg $420B - turn off all general DMA channels
  124. STZ $420C ;reg $420C - turn off all H-MA channels
  125.  
  126. STZ $420D ;reg $420D - ROM access time to slow (2.68Mhz)
  127.  
  128. LDA $4210 ;reg $4210 - NMI status, reading resets
  129.  
  130. ;reg $4211 - IRQ status, no need to initialize
  131. ;reg $4212 - H/V blank and JoyRead status, no need to initialize
  132. ;reg $4213 - programmable I/O inport, no need to initialize
  133.  
  134. ;reg $4214-$4215 - divide results, no need to initialize
  135. ;reg $4216-$4217 - multiplication or remainder results, no need to initialize
  136.  
  137. ;regs $4218-$421f - JoyPad read registers, no need to initialize
  138.  
  139. ;regs $4300-$437F
  140. ;no need to intialize because DMA was disabled above
  141. ;also, we're not sure what all of the registers do, so it is better to leave them at
  142. ;their reset state value
  143.  
  144. JSR ClearVRAM ;Reset VRAM
  145. JSR ClearPalette ;Reset colors
  146.  
  147. ;**** clear Sprite tables ********
  148.  
  149. STZ $2102 ;sprites initialized to be off the screen, palette 0, character 0
  150. STZ $2103
  151. LDX #$0080
  152. LDA #$F0
  153. _Loop08:
  154. STA $2104 ;set X = 240
  155. STA $2104 ;set Y = 240
  156. STZ $2104 ;set character = $00
  157. STZ $2104 ;set priority=0, no flips
  158. DEX
  159. BNE _Loop08
  160.  
  161. LDX #$0020
  162. _Loop09:
  163. STZ $2104 ;set size bit=0, x MSB = 0
  164. DEX
  165. BNE _Loop09
  166.  
  167. ;**** clear WRAM ********
  168.  
  169. STZ $2181 ;set WRAM address to $000000
  170. STZ $2182
  171. STZ $2183
  172.  
  173. LDX #$8008
  174. STX $4300 ;Set DMA mode to fixed source, BYTE to $2180
  175. LDX #wram_fill_byte
  176. STX $4302 ;Set source offset
  177. LDA #:wram_fill_byte
  178. STA $4304 ;Set source bank
  179. LDX #$0000
  180. STX $4305 ;Set transfer size to 64k bytes
  181. LDA #$01
  182. STA $420B ;Initiate transfer
  183.  
  184. LDA #$01 ;now set the next 64k bytes
  185. STA $420B ;Initiate transfer
  186.  
  187. PHK ;make sure Data Bank = Program Bank
  188. PLB
  189.  
  190. CLI ;enable interrupts again
  191.  
  192. LDX $4372 ;get our return address...
  193. STX $1FFD
  194. LDA $4374
  195. STA $1FFF
  196. RTL
  197.  
  198. wram_fill_byte:
  199. .db $00
  200.  
  201. ;----------------------------------------------------------------------------
  202. ; ClearVRAM -- Sets every byte of VRAM to zero
  203. ; In: None
  204. ; Out: None
  205. ; Modifies: flags
  206. ;----------------------------------------------------------------------------
  207. ClearVRAM:
  208. pha
  209. phx
  210. php
  211.  
  212. REP #$30 ; mem/A = 8 bit, X/Y = 16 bit
  213. SEP #$20
  214.  
  215. LDA #$80
  216. STA $2115 ;Set VRAM port to word access
  217. LDX #$1809
  218. STX $4300 ;Set DMA mode to fixed source, WORD to $2118/9
  219. LDX #$0000
  220. STX $2116 ;Set VRAM port address to $0000
  221. STX $0000 ;Set $00:0000 to $0000 (assumes scratchpad ram)
  222. STX $4302 ;Set source address to $xx:0000
  223. LDA #$00
  224. STA $4304 ;Set source bank to $00
  225. LDX #$FFFF
  226. STX $4305 ;Set transfer size to 64k-1 bytes
  227. LDA #$01
  228. STA $420B ;Initiate transfer
  229.  
  230. STZ $2119 ;clear the last byte of the VRAM
  231.  
  232. plp
  233. plx
  234. pla
  235. RTS
  236.  
  237. ;----------------------------------------------------------------------------
  238. ; ClearPalette -- Reset all palette colors to zero
  239. ; In: None
  240. ; Out: None
  241. ; Modifies: flags
  242. ;----------------------------------------------------------------------------
  243. ClearPalette:
  244. PHX
  245. PHP
  246. REP #$30 ; mem/A = 8 bit, X/Y = 16 bit
  247. SEP #$20
  248.  
  249. STZ $2121
  250. LDX #$0100
  251. ClearPaletteLoop:
  252. STZ $2122
  253. DEX
  254. BNE ClearPaletteLoop
  255.  
  256. PLP
  257. PLX
  258. RTS
  259.  
  260. .ENDS
Advertisement
Add Comment
Please, Sign In to add comment