2019-02-09 18:42:35 +00:00
|
|
|
|
|
|
|
include "nesdefs.asm"
|
|
|
|
|
|
|
|
;;;;; VARIABLES
|
|
|
|
|
|
|
|
seg.u RAM
|
|
|
|
org $0
|
|
|
|
|
2019-02-28 21:08:41 +00:00
|
|
|
ScrollPos word ; used during NMI
|
|
|
|
Rand byte ; random number
|
|
|
|
Temp1 byte ; temporary
|
2019-02-24 15:36:38 +00:00
|
|
|
|
2019-02-28 21:08:41 +00:00
|
|
|
; OAM sprite buffer
|
2019-02-24 15:36:38 +00:00
|
|
|
SpriteBuf equ $200
|
2019-02-09 18:42:35 +00:00
|
|
|
|
|
|
|
;;;;; NES CARTRIDGE HEADER
|
|
|
|
|
2019-02-28 21:08:41 +00:00
|
|
|
NES_HEADER 0,2,1,1 ; mapper 0, 2 PRGs, 1 CHR, horiz. mirror
|
2019-02-09 18:42:35 +00:00
|
|
|
|
|
|
|
;;;;; START OF CODE
|
|
|
|
|
|
|
|
Start:
|
|
|
|
NES_INIT ; set up stack pointer, turn off PPU
|
|
|
|
jsr WaitSync
|
|
|
|
jsr WaitSync
|
|
|
|
jsr ClearRAM
|
|
|
|
jsr WaitSync ;wait for VSYNC
|
|
|
|
jsr SetPalette ;set colors
|
|
|
|
jsr FillVRAM ;set PPU RAM
|
|
|
|
jsr WaitSync ;wait for VSYNC (and PPU warmup)
|
2019-02-24 15:36:38 +00:00
|
|
|
jsr InitSprites
|
2019-02-09 18:42:35 +00:00
|
|
|
lda #0
|
|
|
|
sta PPU_ADDR
|
|
|
|
sta PPU_ADDR ;PPU addr = 0
|
|
|
|
sta PPU_SCROLL
|
|
|
|
sta PPU_SCROLL ;scroll = 0
|
|
|
|
lda #CTRL_NMI
|
|
|
|
sta PPU_CTRL ; enable NMI
|
|
|
|
lda #MASK_BG|MASK_SPR
|
|
|
|
sta PPU_MASK ; enable rendering
|
|
|
|
.endless
|
|
|
|
jmp .endless ;endless loop
|
|
|
|
|
|
|
|
; fill video RAM
|
|
|
|
FillVRAM: subroutine
|
2019-03-02 23:15:03 +00:00
|
|
|
PPU_SETADDR $2000
|
2019-02-09 18:42:35 +00:00
|
|
|
ldy #$10
|
2019-03-04 14:35:33 +00:00
|
|
|
ldx #0
|
2019-02-09 18:42:35 +00:00
|
|
|
.loop:
|
2019-03-02 23:15:03 +00:00
|
|
|
stx PPU_DATA ; X -> PPU data port
|
|
|
|
inx ; X = X + 1
|
|
|
|
bne .loop ; repeat until 256 bytes
|
|
|
|
dey ; Y = Y - 1
|
|
|
|
bne .loop ; repeat until Y is 0
|
|
|
|
rts ; return to caller
|
|
|
|
|
|
|
|
; initialize OAM data
|
2019-02-24 15:36:38 +00:00
|
|
|
InitSprites: subroutine
|
2019-03-02 23:15:03 +00:00
|
|
|
lda #1 ; A = 1
|
|
|
|
ldx #0 ; X = 0
|
2019-02-24 15:36:38 +00:00
|
|
|
.loop
|
2019-03-02 23:15:03 +00:00
|
|
|
sta SpriteBuf,x ; store to OAM buffer
|
|
|
|
jsr NextRandom ; get next random number
|
|
|
|
inx ; X = X + 1
|
|
|
|
bne .loop ; loop until X wraps
|
|
|
|
rts ; return to caller
|
2019-02-24 15:36:38 +00:00
|
|
|
|
2019-03-02 23:15:03 +00:00
|
|
|
; move sprite data
|
2019-02-24 15:36:38 +00:00
|
|
|
MoveSprites: subroutine
|
2019-03-02 23:15:03 +00:00
|
|
|
lda #1 ; A = 1
|
|
|
|
ldx #0 ; X = 0
|
2019-02-24 15:36:38 +00:00
|
|
|
.loop
|
2019-03-02 23:15:03 +00:00
|
|
|
sta Temp1 ; save A
|
|
|
|
and #3 ; keep lower 2 bits
|
|
|
|
clc ; clear carry before add
|
|
|
|
adc SpriteBuf,x ; add to sprite buffer
|
|
|
|
sta SpriteBuf,x ; store in sprite buffer
|
|
|
|
lda Temp1 ; restore A
|
|
|
|
jsr NextRandom ; get next random number
|
|
|
|
inx ; X = X + 1
|
|
|
|
bne .loop ; loop until X wraps
|
|
|
|
rts ; return to caller
|
2019-02-24 15:36:38 +00:00
|
|
|
|
2019-02-09 18:42:35 +00:00
|
|
|
; set palette colors
|
|
|
|
SetPalette: subroutine
|
2019-03-02 23:15:03 +00:00
|
|
|
PPU_SETADDR $3f00
|
2019-03-04 14:35:33 +00:00
|
|
|
ldy #0
|
2019-02-09 18:42:35 +00:00
|
|
|
.loop:
|
2019-03-02 23:15:03 +00:00
|
|
|
lda Palette,y ; lookup byte in ROM
|
|
|
|
sta PPU_DATA ; store byte to PPU data
|
|
|
|
iny ; Y = Y + 1
|
|
|
|
cpy #32 ; is Y equal to 32?
|
|
|
|
bne .loop ; not yet, loop
|
|
|
|
rts ; return to caller
|
2019-02-09 18:42:35 +00:00
|
|
|
|
|
|
|
|
|
|
|
;;;;; COMMON SUBROUTINES
|
|
|
|
|
|
|
|
include "nesppu.asm"
|
|
|
|
|
|
|
|
;;;;; INTERRUPT HANDLERS
|
|
|
|
|
|
|
|
NMIHandler:
|
2019-02-28 21:08:41 +00:00
|
|
|
; save registers
|
2019-02-09 18:42:35 +00:00
|
|
|
SAVE_REGS
|
|
|
|
; update scroll position (must be done after VRAM updates)
|
2019-02-28 21:08:41 +00:00
|
|
|
inc ScrollPos ; increment low byte
|
|
|
|
bne .noinc ; Z flag set if wrapped to 0
|
|
|
|
inc ScrollPos+1 ; increment high byte
|
|
|
|
.noinc
|
|
|
|
; store X and Y scroll position
|
|
|
|
lda ScrollPos ; A -> low byte
|
|
|
|
sta PPU_SCROLL ; set horiz scroll
|
|
|
|
lda #0 ; A -> zero
|
|
|
|
sta PPU_SCROLL ; set vert scroll
|
|
|
|
; store 8th bit into name table selector
|
|
|
|
; name table A or B ($2000 or $2400)
|
|
|
|
lda ScrollPos+1 ; load high byte
|
|
|
|
and #1 ; select its low bit
|
|
|
|
ora #CTRL_NMI ; set rest of bits
|
|
|
|
sta PPU_CTRL
|
|
|
|
; load sprites
|
|
|
|
lda #$02 ; page 2 ($0200)
|
|
|
|
sta PPU_OAM_DMA ; start DMA transfer
|
2019-02-24 15:36:38 +00:00
|
|
|
; move sprites
|
2019-02-28 21:08:41 +00:00
|
|
|
jsr MoveSprites ; move sprites
|
|
|
|
; restore registers, return from interrupt
|
2019-02-09 18:42:35 +00:00
|
|
|
RESTORE_REGS
|
|
|
|
rti
|
|
|
|
|
|
|
|
;;;;; CONSTANT DATA
|
|
|
|
|
|
|
|
Palette:
|
2019-02-28 21:08:41 +00:00
|
|
|
hex 1f ;screen color
|
2019-04-09 14:24:23 +00:00
|
|
|
hex 01112100 ;background 0
|
|
|
|
hex 02122200 ;background 1
|
|
|
|
hex 02112100 ;background 2
|
|
|
|
hex 01122200 ;background 3
|
|
|
|
hex 19293900 ;sprite 0
|
|
|
|
hex 1a2a3a00 ;sprite 1
|
|
|
|
hex 1b2b3b00 ;sprite 2
|
|
|
|
hex 1c2c3c00 ;sprite 3
|
2019-02-09 18:42:35 +00:00
|
|
|
|
|
|
|
;;;;; CPU VECTORS
|
|
|
|
|
|
|
|
NES_VECTORS
|
|
|
|
|
|
|
|
;;;;; TILE SETS
|
|
|
|
|
|
|
|
org $10000
|
|
|
|
incbin "jroatch.chr"
|