Advertisement
Guest User

CHR-RAM try

a guest
Oct 2nd, 2010
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. .segment "HEADER"
  2.    
  3.     .byte   "NES", $1A  ; iNES header identifier
  4.     .byte   1       ; 1x 16KB PRG code
  5.     .byte   0       ; 1x  8KB CHR data
  6.     .byte   $01, $00    ; mapper 0, vertical mirroring
  7.  
  8. ;;;;;;;;;;;;;;;
  9.  
  10. ;;; "nes" linker config requires a STARTUP section, even if it's empty
  11. .segment "STARTUP"
  12.  
  13. .segment "CODE"
  14.  
  15. reset:
  16.     sei         ; disable IRQs
  17.     cld         ; disable decimal mode
  18.     ldx #$40
  19.     stx $4017       ; dsiable APU frame IRQ
  20.     ldx #$ff        ; Set up stack
  21.     txs         ;  .
  22.     inx         ; now X = 0
  23.     stx $2000       ; disable NMI
  24.     stx $2001       ; disable rendering
  25.     stx $4010       ; disable DMC IRQs
  26.  
  27.     ;; first wait for vblank to make sure PPU is ready
  28. vblankwait1:
  29.     bit $2002
  30.     bpl vblankwait1
  31.  
  32. clear_memory:
  33.     lda #$00
  34.     sta $0000, x
  35.     sta $0100, x
  36.     sta $0300, x
  37.     sta $0400, x
  38.     sta $0500, x
  39.     sta $0600, x
  40.     sta $0700, x
  41.     lda #$fe
  42.     sta $0200, x    ; move all sprites off screen
  43.     inx
  44.     bne clear_memory
  45.  
  46.     ;; second wait for vblank, PPU is ready after this
  47. vblankwait2:
  48.     bit $2002
  49.     bpl vblankwait2
  50.  
  51. ; for ca65
  52. PPUADDR = $2006
  53. PPUDATA = $2007
  54. .proc copy_mytiles_chr
  55. src = 0
  56.   lda #<mytiles_chr  ; load the source address into a pointer in zero page
  57.   sta src
  58.   lda #>mytiles_chr
  59.   sta src+1
  60.  
  61.   ldy #0       ; starting index into the first page
  62.   sty PPUADDR  ; load the destination address into the PPU
  63.   sty PPUADDR
  64.   ldx #32      ; number of 256-byte pages to copy
  65. loop:
  66.   lda (src),y  ; copy one byte
  67.   sta PPUDATA
  68.   iny
  69.   bne loop  ; repeat until we finish the page
  70.   inc src+1  ; go to the next page
  71.   dex
  72.   bne loop  ; repeat until we've copied enough pages
  73.   rts
  74. .endproc
  75.  
  76. load_palettes:
  77.     lda $2002       ; read PPU status to reset the high/low latch
  78.     lda #$3f
  79.     sta $2006
  80.     lda #$00
  81.     sta $2006
  82.     ldx #$00
  83. @loop:
  84.     lda palette,; load palette byte
  85.     sta $2007       ; write to PPU
  86.     inx         ; set index to next byte
  87.     cpx #$20
  88.     bne @loop       ; if x = $20, 32 bytes copied, all done
  89.  
  90.     lda #$80
  91.     sta $0200       ; put sprite 0 in center ($80) of screen vert
  92.     sta $0203       ; put sprite 0 in center ($80) of screen horiz
  93.     lda #$00
  94.     sta $0201       ; tile number = 0
  95.     sta $0202       ; color = 0, no flipping
  96.  
  97.     lda #%10000000  ; enable NMI, sprites from Pattern Table 0
  98.     sta $2000
  99.  
  100.     lda #%00010000  ; enable sprites
  101.     sta $2001
  102.    
  103. forever:
  104.     jmp forever
  105.  
  106. nmi:
  107.     lda #$00        ; set the low byte (00) of the RAM address
  108.     sta $2003
  109.     lda #$02        ; set the high byte (02) of the RAM address
  110.     sta $4014       ; start the transfer
  111.    
  112.     rti         ; return from interrupt
  113.  
  114. palette:
  115.     ;; Background palette
  116.     .byte   $0F,$31,$32,$33
  117.     .byte   $0F,$35,$36,$37
  118.     .byte   $0F,$39,$3A,$3B
  119.     .byte   $0F,$3D,$3E,$0F
  120.     ;; Sprite palette
  121.     .byte   $0F,$16,$27,$18
  122.     .byte   $0F,$02,$38,$3C
  123.     .byte   $0F,$1C,$15,$14
  124.     .byte   $0F,$02,$38,$3C
  125.  
  126. ;;;;;;;;;;;;;;  
  127.  
  128. .segment "VECTORS"
  129.  
  130.     .word   0, 0, 0     ; Unused, but needed to advance PC to $fffa.
  131.     ;; When an NMI happens (once per frame if enabled) the label nmi:
  132.     .word   nmi
  133.     ;; When the processor first turns on or is reset, it will jump to the
  134.     ;; label reset:
  135.     .word   reset
  136.     ;; External interrupt IRQ is not used in this tutorial
  137.     .word   0
  138.  
  139. ;;;;;;;;;;;;;;  
  140.  
  141. .segment "RODATA"
  142.    
  143.     mytiles_chr: .incbin "mario.chr"
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement