Advertisement
Guest User

Untitled

a guest
Apr 1st, 2018
246
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.20 KB | None | 0 0
  1.  
  2.  
  3. ;;;;;;;;;;;;;;;
  4.  
  5. ;; DECLARE SOME VARIABLES HERE
  6. .enum $0000 ;;start variables at ram location 0
  7.  
  8. scroll .dsb 1 ; horizontal scroll count
  9. nametable .dsb 1 ; which nametable to use, 0 or 1
  10. columnLow .dsb 1 ; low byte of new column address
  11. columnHigh .dsb 1 ; high byte of new column address
  12. sourceLow .dsb 1 ; source for column data
  13. sourceHigh .dsb 1
  14. columnNumber .dsb 1 ; which column of level data to draw
  15. .ende
  16.  
  17. ;;;;;;;;;;;;
  18.  
  19. .ORG $7ff0
  20. Header: ;16 byte .NES header (iNES)
  21. .db "NES", $1a ;NES followed by MS-DOS end-of-file
  22. .db $02 ;size of PRG ROM in 16kb units
  23. .db $01 ;size of CHR ROM in 8kb units
  24. .db #%00000001 ;flags 6, set to: mapper 4,
  25. .db #%00000000 ;flags 7, set to: mapper 4
  26. .db $00 ;size of PRG RAM in 8kb RAM
  27. .db $00 ;flags 9, set to 0
  28. .db $00 ;flags 10, set to 0
  29. .db $00 ;11 - the rest are zeroed out
  30. .db $00 ;12
  31. .db $00 ;13
  32. .db $00 ;14
  33. .db $00 ;15
  34.  
  35. .org $8000
  36. RESET:
  37. SEI ; disable IRQs
  38. CLD ; disable decimal mode
  39. LDX #$40
  40. STX $4017 ; disable APU frame IRQ
  41. LDX #$FF
  42. TXS ; Set up stack
  43. INX ; now X = 0
  44. STX $2000 ; disable NMI
  45. STX $2001 ; disable rendering
  46. STX $4010 ; disable DMC IRQs
  47.  
  48. vblankwait1: ; First wait for vblank to make sure PPU is ready
  49. BIT $2002
  50. BPL vblankwait1
  51.  
  52. clrmem:
  53. LDA #$00
  54. STA $0000, x
  55. STA $0100, x
  56. STA $0300, x
  57. STA $0400, x
  58. STA $0500, x
  59. STA $0600, x
  60. STA $0700, x
  61. LDA #$FE
  62. STA $0200, x
  63. INX
  64. BNE clrmem
  65.  
  66. vblankwait2: ; Second wait for vblank, PPU is ready after this
  67. BIT $2002
  68. BPL vblankwait2
  69.  
  70.  
  71. LoadPalettes:
  72. LDA $2002 ; read PPU status to reset the high/low latch
  73. LDA #$3F
  74. STA $2006 ; write the high byte of $3F00 address
  75. LDA #$00
  76. STA $2006 ; write the low byte of $3F00 address
  77. LDX #$00 ; start out at 0
  78. LoadPalettesLoop:
  79. LDA palette, x ; load data from address (palette + the value in x)
  80. STA $2007 ; write to PPU
  81. INX ; X = X + 1
  82. CPX #$20 ; Compare X to hex $10, decimal 16 - copying 16 bytes = 4 sprites
  83. BNE LoadPalettesLoop ; Branch to LoadPalettesLoop if compare was Not Equal to zero
  84. ; if compare was equal to 32, keep going down
  85.  
  86.  
  87.  
  88. LoadSprites:
  89. LDX #$00 ; start at 0
  90. LoadSpritesLoop:
  91. LDA sprites, x ; load data from address (sprites + x)
  92. STA $0200, x ; store into RAM address ($0200 + x)
  93. INX ; X = X + 1
  94. CPX #$10 ; Compare X to hex $20, decimal 16
  95. BNE LoadSpritesLoop ; Branch to LoadSpritesLoop if compare was Not Equal to zero
  96. ; if compare was equal to 16, keep going down
  97.  
  98.  
  99. InitializeNametables:
  100. LDA #$01
  101. STA nametable
  102. LDA #$00
  103. STA scroll
  104. STA columnNumber
  105. InitializeNametablesLoop:
  106. JSR DrawNewColumn ; draw bg column
  107. LDA scroll ; go to next column
  108. CLC
  109. ADC #$08
  110. STA scroll
  111. INC columnNumber
  112. LDA columnNumber ; repeat for first nametable
  113. CMP #$20
  114. BNE InitializeNametablesLoop
  115.  
  116. LDA #$00
  117. STA nametable
  118. LDA #$00
  119. STA scroll
  120. JSR DrawNewColumn ; draw first column of second nametable
  121. INC columnNumber
  122.  
  123. LDA #$00 ; set back to increment +1 mode
  124. STA $2000
  125. InitializeNametablesDone:
  126.  
  127.  
  128. InitializeAttributes:
  129. LDA #$01
  130. STA nametable
  131. LDA #$00
  132. STA scroll
  133. STA columnNumber
  134. InitializeAttributesLoop:
  135. JSR DrawNewAttributes ; draw attribs
  136. LDA scroll ; go to next column
  137. CLC
  138. ADC #$20
  139. STA scroll
  140.  
  141. LDA columnNumber ; repeat for first nametable
  142. CLC
  143. ADC #$04
  144. STA columnNumber
  145. CMP #$20
  146. BNE InitializeAttributesLoop
  147.  
  148. LDA #$00
  149. STA nametable
  150. LDA #$00
  151. STA scroll
  152. JSR DrawNewAttributes ; draw first column of second nametable
  153. InitializeAttributesDone:
  154.  
  155. LDA #$21
  156. STA columnNumber
  157.  
  158.  
  159. LDA #%10010000 ; enable NMI, sprites from Pattern Table 0, background from Pattern Table 1
  160. STA $2000
  161.  
  162. LDA #%00011110 ; enable sprites, enable background, no clipping on left side
  163. STA $2001
  164.  
  165. Forever:
  166. JMP Forever ;jump back to Forever, infinite loop
  167.  
  168.  
  169.  
  170. NMI:
  171. INC scroll ; add one to our scroll variable each frame
  172.  
  173.  
  174. NTSwapCheck:
  175. LDA scroll ; check if the scroll just wrapped from 255 to 0
  176. BNE NTSwapCheckDone
  177. NTSwap:
  178. LDA nametable ; load current nametable number (0 or 1)
  179. EOR #$01 ; exclusive OR of bit 0 will flip that bit
  180. STA nametable ; so if nametable was 0, now 1
  181. ; if nametable was 1, now 0
  182. NTSwapCheckDone:
  183.  
  184.  
  185. NewAttribCheck:
  186. LDA scroll
  187. AND #%00011111 ; check for multiple of 32
  188. BNE NewAttribCheckDone ; if low 5 bits = 0, time to write new attribute bytes
  189. jsr DrawNewAttributes
  190. NewAttribCheckDone:
  191.  
  192.  
  193. NewColumnCheck:
  194. LDA scroll
  195. AND #%00000111 ; throw away higher bits to check for multiple of 8
  196. BNE NewColumnCheckDone ; done if lower bits != 0
  197. JSR DrawNewColumn ; if lower bits = 0, time for new column
  198.  
  199. lda columnNumber
  200. clc
  201. adc #$01 ; go to next column
  202. and #%01111111 ; only 128 columns of data, throw away top bit to wrap
  203. sta columnNumber
  204. NewColumnCheckDone:
  205.  
  206.  
  207. LDA #$00
  208. STA $2003
  209. LDA #$02
  210. STA $4014 ; sprite DMA from $0200
  211.  
  212. ; run other game graphics updating code here
  213.  
  214. LDA #$00
  215. STA $2006 ; clean up PPU address registers
  216. STA $2006
  217.  
  218. LDA scroll
  219. STA $2005 ; write the horizontal scroll count register
  220.  
  221. LDA #$00 ; no vertical scrolling
  222. STA $2005
  223.  
  224. ;;This is the PPU clean up section, so rendering the next frame starts properly.
  225. LDA #%10010000 ; enable NMI, sprites from Pattern Table 0, background from Pattern Table 1
  226. ORA nametable ; select correct nametable for bit 0
  227. STA $2000
  228.  
  229. LDA #%00011110 ; enable sprites, enable background, no clipping on left side
  230. STA $2001
  231.  
  232. ; run normal game engine code here
  233. ; reading from controllers, etc
  234.  
  235. RTI ; return from interrupt
  236.  
  237.  
  238.  
  239.  
  240.  
  241. DrawNewColumn:
  242. LDA scroll ; calculate new column address using scroll register
  243. LSR A
  244. LSR A
  245. LSR A ; shift right 3 times = divide by 8
  246. STA columnLow ; $00 to $1F, screen is 32 tiles wide
  247.  
  248. LDA nametable ; calculate new column address using current nametable
  249. EOR #$01 ; invert low bit, A = $00 or $01
  250. ASL A ; shift up, A = $00 or $02
  251. ASL A ; $00 or $04
  252. CLC
  253. ADC #$20 ; add high byte of nametable base address ($2000)
  254. STA columnHigh ; now address = $20 or $24 for nametable 0 or 1
  255.  
  256. LDA columnNumber ; column number * 32 = column data offset
  257. ASL A
  258. ASL A
  259. ASL A
  260. ASL A
  261. ASL A
  262. STA sourceLow
  263. LDA columnNumber
  264. LSR A
  265. LSR A
  266. LSR A
  267. STA sourceHigh
  268.  
  269. LDA sourceLow ; column data start + offset = address to load column data from
  270. CLC
  271. ADC #<columnData
  272. STA sourceLow
  273. LDA sourceHigh
  274. ADC #>columnData
  275. STA sourceHigh
  276.  
  277. DrawColumn:
  278. LDA #%00000100 ; set to increment +32 mode
  279. STA $2000
  280.  
  281. LDA $2002 ; read PPU status to reset the high/low latch
  282. LDA columnHigh
  283. STA $2006 ; write the high byte of column address
  284. LDA columnLow
  285. STA $2006 ; write the low byte of column address
  286. LDX #$1E ; copy 30 bytes
  287. LDY #$00
  288. DrawColumnLoop:
  289. LDA sourceLow, y
  290. STA $2007
  291. INY
  292. DEX
  293. BNE DrawColumnLoop
  294.  
  295. RTS
  296.  
  297.  
  298.  
  299. DrawNewAttributes:
  300. LDA nametable
  301. EOR #$01 ; invert low bit, A = $00 or $01
  302. ASL A ; shift up, A = $00 or $02
  303. ASL A ; $00 or $04
  304. CLC
  305. ADC #$23 ; add high byte of attribute base address ($23C0)
  306. STA columnHigh ; now address = $23 or $27 for nametable 0 or 1
  307.  
  308. LDA scroll
  309. LSR A
  310. LSR A
  311. LSR A
  312. LSR A
  313. LSR A
  314. CLC
  315. ADC #$C0
  316. STA columnLow ; attribute base + scroll / 32
  317.  
  318. LDA columnNumber ; (column number / 4) * 8 = column data offset
  319. AND #%11111100
  320. ASL A
  321. STA sourceLow
  322. LDA columnNumber
  323. LSR A
  324. LSR A
  325. LSR A
  326. LSR A
  327. LSR A
  328. LSR A
  329. LSR A
  330. STA sourceHigh
  331.  
  332. LDA sourceLow ; column data start + offset = address to load column data from
  333. CLC
  334. ADC #<attribData
  335. STA sourceLow
  336. LDA sourceHigh
  337. ADC #>attribData
  338. STA sourceHigh
  339.  
  340. LDY #$00
  341. LDA $2002 ; read PPU status to reset the high/low latch
  342. DrawNewAttributesLoop
  343. LDA columnHigh
  344. STA $2006 ; write the high byte of column address
  345. LDA columnLow
  346. STA $2006 ; write the low byte of column address
  347. LDA sourceLow, y ; copy new attribute byte
  348. STA $2007
  349.  
  350. INY
  351. CPY #$08 ; copy 8 attribute bytes
  352. BEQ DrawNewAttributesLoopDone
  353.  
  354. LDA columnLow ; next attribute byte is at address + 8
  355. CLC
  356. ADC #$08
  357. STA columnLow
  358. JMP DrawNewAttributesLoop
  359. DrawNewAttributesLoopDone:
  360.  
  361. rts
  362. ;;;;;;;;;;;;;;
  363.  
  364. palette:
  365. .db $22,$29,$1A,$0F, $22,$36,$17,$0F, $22,$30,$21,$0F, $22,$27,$17,$0F ;;background palette
  366. .db $22,$16,$27,$18, $22,$1A,$30,$27, $22,$16,$30,$27, $22,$0F,$36,$17 ;;sprite palette
  367.  
  368. sprites:
  369. ;vert tile attr horiz
  370. .db $80, $32, $00, $80 ;sprite 0
  371. .db $80, $33, $00, $88 ;sprite 1
  372. .db $88, $34, $00, $80 ;sprite 2
  373. .db $88, $35, $00, $88 ;sprite 3
  374.  
  375.  
  376. columnData:
  377. .incbin "SMBlevel.bin"
  378.  
  379. attribData:
  380. .incbin "SMBattrib.bin"
  381.  
  382. .incbin "mario.chr" ;includes 8KB graphics file from SMB1
  383.  
  384. .org $FFFA ;first of the three vectors starts here
  385. .dw NMI ;when an NMI happens (once per frame if enabled) the
  386. ;processor will jump to the label NMI:
  387. .dw RESET ;when the processor first turns on or is reset, it will jump
  388. ;to the label RESET:
  389. .dw 0 ;external interrupt IRQ is not used in this tutorial
  390.  
  391.  
  392. ;;;;;;;;;;;;;;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement