Advertisement
Guest User

Untitled

a guest
Jul 29th, 2017
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. ;=============================
  2. ;DEFINES
  3. ;=============================
  4.  
  5. PPUCTRL         = $2000
  6. PPUMASK         = $2001
  7. PPUSTATUS       = $2002
  8. OAMADDR         = $2003
  9. OAMDATA         = $2004
  10. PPUSCROLL       = $2005
  11. PPUADDR         = $2006
  12. PPUDATA         = $2007
  13. OAMDMA          = $4014
  14.  
  15.  
  16. ;=============================
  17. ;ZP RESERVATIONS
  18. ;=============================
  19. .segment "ZEROPAGE"
  20.     ptr:    .res 2      ;general purp pointer
  21.     cbank:  .res 1      ;keeps current bank in memory so NMI can restore it.
  22.  
  23. ;=============================
  24. ;ROM START
  25. ;=============================
  26. .segment "HEADER"
  27.     .byte "NES", $1A    ;magic number
  28.     .byte 8             ;8 banks, range 0-7
  29.     .byte 0             ;CHR RAM instead of ROM
  30.     .byte $20, $08      ;Mapper 2 - horz mirroring
  31.     .byte $00           ;
  32.     .byte $00           ;
  33.     .byte $00           ;PRG RAM: no.
  34.     .byte $07           ;8kB CHR RAM
  35.     .byte $00           ;NTSC
  36.     .byte $00           ;
  37.  
  38. ;=============================
  39. ;ROM ADDR $8000
  40. ;============================= 
  41. .segment "BANK0_TITLE"
  42.    
  43.    
  44. ;=============================
  45. ;ROM ADDR $C000
  46. ;=============================
  47.  
  48.  
  49. .segment "BANKTABLE"
  50. banktable:   ;used for bus conflict avoidance. Bank $07 is fixed.          
  51.   .byte $00, $01, $02, $03, $04, $05, $06  
  52.  
  53.  
  54. .segment "CODE_MAIN"    ;all program code in fixed bank goes here.
  55.  
  56. reset:
  57. ;makeshift without proper initialization
  58. ldy #0    
  59.     sty PPUCTRL  
  60.     sty PPUMASK    
  61.  
  62. jsr bankswitch ;expects a 0 at y for bank 0.      
  63. jsr w_vblank
  64.  
  65. ;set up sprite pattern
  66. lda #>title_spr
  67. ldy #<title_spr
  68. sta ptr+1
  69. sty ptr+0          
  70.  
  71. ldy #0
  72. lda #$00   ;0 is table 0, $10 is table 1
  73. jsr set_ppuaddr_ay  
  74.  
  75. ldx #16      ; + 16 pages' worth for spr chr.  
  76. jsr st_ppudata_y_x_ptr
  77.  
  78. ;set up background
  79. lda #>title_bg
  80. ldy #<title_bg
  81. sta ptr+1
  82. sty ptr+0          
  83.  
  84. ldy #0
  85. lda #$10   ;0 is table 0, $10 is table 1
  86. jsr set_ppuaddr_ay  
  87.  
  88. ldx #20      ; + 20 pages' worth - 16 for bg chr and 4 for nt
  89. jsr st_ppudata_y_x_ptr  
  90.  
  91.    
  92. Load_palette:
  93. ;should be made a subroutine
  94. .scope
  95.     lda PPUSTATUS
  96.     lda #$3F
  97.     sta PPUADDR
  98.     lda #0
  99.     sta PPUADDR
  100.     ldx #0
  101.     lda title_pal ;QUESTION: why do i have to load title_pal here to make colours load correctly?
  102. loop:
  103.     lda title_pal, x
  104.     sta PPUDATA
  105.     inx
  106.     cpx #32
  107.     bne loop ; iterate
  108. .endscope
  109.  
  110. ldx #0
  111.        
  112. curtain_up:
  113. ;enable viewing. NMI is currently disabled.
  114.     stx PPUSCROLL
  115.     stx PPUSCROLL
  116.  
  117.     lda #%00010000
  118.     sta PPUCTRL
  119.    
  120.     lda #%00011110
  121.     sta PPUMASK
  122.    
  123.     clc ;set a known condition
  124. loop_forever:
  125.     bcc loop_forever
  126.    
  127. ;=============================
  128. ;SUBROUTINES
  129. ;=============================
  130.  
  131. bankswitch:
  132.   sty cbank       ; save the current bank in RAM so the NMI handler can restore it
  133.   lda banktable, y      ; |
  134.   sta banktable, y      ; |<-- read and write to switch bank.
  135.   rts
  136.  
  137. w_vblank:
  138. .scope  
  139.     bit PPUSTATUS  ;spinlock starts here        
  140. spinlock:  
  141.     bit PPUSTATUS    
  142.     bpl spinlock
  143.     rts
  144. .endscope
  145.  
  146. set_ppuaddr_ay:
  147.     sta PPUADDR    
  148.     sty PPUADDR
  149.     rts
  150.    
  151. st_ppudata_y_x_ptr:
  152. .scope
  153. loop:  
  154.    lda (ptr), y
  155.    sta PPUDATA
  156.    iny  
  157.    bne loop    ; done a page yet?  (y reg)
  158.    inc ptr+1    
  159.    dex      
  160.    bne loop    ; done all pages yet? (x reg)
  161. rts
  162. .endscope  
  163.  
  164.  
  165. ;=============================
  166. ;INTERRUPTS
  167. ;=============================  
  168.  
  169. nmi:
  170.    pha
  171.    txa
  172.    pha
  173.    tya
  174.    pha
  175.  
  176. ;Place new stuff here and reenable NMI
  177.  
  178.    pla
  179.    tay
  180.    pla
  181.    tax
  182.    pla
  183. rti
  184.  
  185.  
  186. .segment "RODATA"
  187. ;place data here that should always be available regardless bank.
  188.  
  189. ;QUESTION: Why does nametable etc break when i move this to the bank0 segment at line 41?
  190. title_spr:  .incbin "dd_sprite.chr" ;4k
  191. title_bg:   .incbin "dd.chr" ;4k
  192. title_nam:  .incbin "dd.nam" ;1k
  193. title_pal:
  194.     .incbin "dd.pal"
  195.     .incbin "dd.pal"   
  196.    
  197. .segment "VECTORS"
  198.     .word nmi
  199.     .word reset
  200.     .word reset
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement