Skip to content

Instantly share code, notes, and snippets.

@bambam2174
Forked from wkjagt/show-sprite.asm
Created May 17, 2021 23:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bambam2174/5950b178c882467ba37baf1be07742af to your computer and use it in GitHub Desktop.
Save bambam2174/5950b178c882467ba37baf1be07742af to your computer and use it in GitHub Desktop.
.segment "HEADER"
.byte "NES", $1A ; Constant
.byte 2 ; 2 x 16KB PRG ROM
.byte 1 ; 1 x 8KB CHR ROM
.segment "CHARS"
;-------------------------------------------------------------
; CREATE SPRITES
;-------------------------------------------------------------
; The following creates one sprite. The following two bitmaps
; are overlayed to create one sprite, where each pair of bits
; in the same position in the two bitmaps are added to result
; in the following way:
; 00 : transparant
; 01 : color 1 from palette
; 10 : color 2 from palette
; 11 : color 3 from palette
.byte %00011000 ; 0240 1-48: **
.byte %00111110 ; 0241 1-48: *****
.byte %01100000 ; 0242 1-48: **
.byte %00111100 ; 0243 1-48: ****
.byte %00000110 ; 0244 1-48: **
.byte %01111100 ; 0245 1-48: *****
.byte %00011000 ; 0246 1-48: **
.byte %00000000 ; 0247 1-48:
.byte %00011000 ; 0248 1-49: **
.byte %00111110 ; 0249 1-49: *****
.byte %01100000 ; 024A 1-49: **
.byte %00111100 ; 024B 1-49: ****
.byte %00000110 ; 024C 1-49: **
.byte %01111100 ; 024D 1-49: *****
.byte %00011000 ; 024E 1-49: **
.byte %00000000 ; 024F 1-49:
.segment "VECTORS"
.word 0, 0, 0, nmi, reset, irq
.segment "STARTUP" ; avoids warning
.segment "CODE"
; First of two waits for vertical blank to make sure that the
; PPU has stabilized
vblankwait1:
BIT $2002
BPL vblankwait1
vblankwait2:
BIT $2002
BPL vblankwait2
; setup PPU
LDA $2002 ; read PPU status to reset the high/low latch to high
;-------------------------------------------------------------
; SET PPUCTRL
;-------------------------------------------------------------
; see: https://wiki.nesdev.com/w/index.php/PPU_registers#PPUCTRL
LDA #%10000000
; ||||||||
; ||||||++- Base nametable address
; |||||| (0 = $2000; 1 = $2400; 2 = $2800; 3 = $2C00)
; |||||+--- VRAM address increment per CPU read/write of PPUDATA
; ||||| (0: add 1, going across; 1: add 32, going down)
; ||||+---- Sprite pattern table address for 8x8 sprites
; |||| (0: $0000; 1: $1000; ignored in 8x16 mode)
; |||+----- Background pattern table address (0: $0000; 1: $1000)
; ||+------ Sprite size (0: 8x8; 1: 8x16)
; |+------- PPU master/slave select
; | (0: read backdrop from EXT pins; 1: output color on EXT pins)
; +-------- Generate an NMI at the start of the
; vertical blanking interval (0: off; 1: on)
STA $2000 ; PPUCTRL
; Set PPUMASK
LDA #%00010000
; ||||||||
; |||||||+- Greyscale (0: normal color, 1: produce a greyscale display)
; ||||||+-- 1: Show background in leftmost 8 pixels of screen, 0: Hide
; |||||+--- 1: Show sprites in leftmost 8 pixels of screen, 0: Hide
; ||||+---- 1: Show background
; |||+----- 1: Show sprites
; ||+------ Emphasize red*
; |+------- Emphasize green*
; +-------- Emphasize blue*
STA $2001
;-------------------------------------------------------------
; SET UP THE COLOUR PALETTE FOR SPRITES
;-------------------------------------------------------------
; Setting up the colour palette for sprites is done by writing
; the four colours a sprite can use to VRAM address $3f10-$3f13
; First let the PPU know what address in VRAM the CPU wants to
; write to by first setting the high byte of the staring address
; (3F) and the low byte (10) to address $2006 (PPUADDR).
LDA #$3F ; PPUADDR
STA $2006 ; write the high byte of $3F10 address
LDA #$10 ; PPUADDR
STA $2006 ; write the low byte of $3F10 address
; Then write the four colours that make up the palette to $2007
; (PPUDATA) one by one. Colour information:
; https://nesdoug.com/2015/11/19/5-a-little-color/
LDA #$02 ; dark blue
STA $2007 ; writes to PPU $3F10
LDA #$16 ; red
STA $2007 ; writes to PPU $3F11
LDA #$0B ; dark green
STA $2007 ; writes to PPU $3F12
LDA #$24 ; light purple
STA $2007 ; writes to PPU $3F13
; store sprite information in $0200 - $0202
LDA #$50 ; Y position of top of sprite
STA $0200
LDA %00000010 ; tile number. 0 means first tile from the CHARS segment
; ||||||||
; |||||||+- Bank ($0000 or $1000) of tiles
; +++++++-- Tile number of top of sprite (0 to 254; bottom half gets the next tile)
STA $0201
LDA #%00000000 ; attributes
; ||||||||
; ||||||++- Palette (4 to 7) of sprite
; |||+++--- Unimplemented
; ||+------ Priority (0: in front of background; 1: behind background)
; |+------- Flip sprite horizontally
; +-------- Flip sprite vertically
STA $0202
LDA #$50 ; X position of left side of sprite.
STA $0203
forever:
JMP forever
; INTERUPTS
nmi: ; triggered on vblank (1/60th of a second)
; Start DMA transfer. This will copy from $0200 to $02FF to the internal PPU OAM
; See: http://wiki.nesdev.com/w/index.php/PPU_registers#OAM_DMA_.28.244014.29_.3E_write
LDA #$00
STA $2003 ; set the low byte (00) of the RAM address in OAMADDR
LDA #$02
STA $4014 ; set the high byte (02) of the RAM address in 4014, start the transfer
RTI
irq:
RTI
reset:
SEI ; ignore IRQs
CLD ; disable decimal mode
LDX #$40
STX $4017 ; disable APU frame IRQ
LDX #$ff
TXS ; Set up stack
INX ; now X = 0
STX $2000 ; disable NMI
STX $2001 ; disable rendering
STX $4010 ; disable DMC IRQs
; Optional (omitted):
; Set up mapper and jmp to further init code here.
; If the user presses Reset during vblank, the PPU may reset
; with the vblank flag still true. This has about a 1 in 13
; chance of happening on NTSC or 2 in 9 on PAL. Clear the
; flag now so the @vblankwait1 loop sees an actual vblank.
BIT $2002
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment