From db9827a299fb6304b822db4cd07982066409fc36 Mon Sep 17 00:00:00 2001 From: Tyrel Souza Date: Mon, 28 Nov 2022 22:55:23 -0500 Subject: [PATCH] background colors --- helloppu/consts.inc | 11 +++++ helloppu/header.inc | 13 ++++++ helloppu/helloppu.asm | 103 ++++++++++++++---------------------------- helloppu/reset.inc | 38 ++++++++++++++++ 4 files changed, 95 insertions(+), 70 deletions(-) create mode 100755 helloppu/consts.inc create mode 100644 helloppu/header.inc create mode 100644 helloppu/reset.inc diff --git a/helloppu/consts.inc b/helloppu/consts.inc new file mode 100755 index 0000000..5c26a36 --- /dev/null +++ b/helloppu/consts.inc @@ -0,0 +1,11 @@ +;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; +;; Constants for PPU registers mapped from addresses $2000 to $2007 +;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; +PPU_CTRL = $2000 +PPU_MASK = $2001 +PPU_STATUS = $2002 +OAM_ADDR = $2003 +OAM_DATA = $2004 +PPU_SCROLL = $2005 +PPU_ADDR = $2006 +PPU_DATA = $2007 diff --git a/helloppu/header.inc b/helloppu/header.inc new file mode 100644 index 0000000..ff36fb2 --- /dev/null +++ b/helloppu/header.inc @@ -0,0 +1,13 @@ +;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; +;; The iNES header (contains a total of 16 bytes with the header flags) +;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; +.segment "HEADER" +.byte $4E,$45,$53,$1A ; 4 bytes with the characters 'N','E','S','\n' +.byte $02 ; How many 16KB of PRG-ROM we'll use (=32KB) +.byte $01 ; How many 8KB of CHR-ROM we'll use (=8KB) +.byte %00000000 ; Horz mirroring, no battery, mapper 0 +.byte %00000000 ; mapper 0, playchoice, NES 2.0 +.byte $00 ; No PRG-RAM +.byte $00 ; NTSC TV format +.byte $00 ; Extra flags for TV format and PRG-RAM +.byte $00,$00,$00,$00,$00 ; Unused padding to complete 16 bytes of header diff --git a/helloppu/helloppu.asm b/helloppu/helloppu.asm index ce079e7..bc2e836 100755 --- a/helloppu/helloppu.asm +++ b/helloppu/helloppu.asm @@ -1,99 +1,62 @@ -;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; -;; Constants for PPU registers mapped from addresses $2000 to $2007 -;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; -PPU_CTRL = $2000 -PPU_MASK = $2001 -PPU_STATUS = $2002 -OAM_ADDR = $2003 -OAM_DATA = $2004 -PPU_SCROLL = $2005 -PPU_ADDR = $2006 -PPU_DATA = $2007 - -;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; -;; The iNES header (contains a total of 16 bytes with the flags at $7F00) -;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; -.segment "HEADER" -.byte $4E,$45,$53,$1A ; 4 bytes with the characters 'N','E','S','\n' -.byte $02 ; How many 16KB of PRG-ROM we'll use (=32KB) -.byte $01 ; How many 8KB of CHR-ROM we'll use (=8KB) -.byte %00000000 ; Horz mirroring, no battery, mapper 0 -.byte %00000000 ; mapper 0, playchoice, NES 2.0 -.byte $00 ; No PRG-RAM -.byte $00 ; NTSC TV format -.byte $00 ; Extra flags for TV format and PRG-RAM -.byte $00,$00,$00,$00,$00 ; Unused padding to complete 16 bytes of header +.include "consts.inc" +.include "header.inc" +.include "reset.inc" ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;; PRG-ROM code located at $8000 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; .segment "CODE" +.proc LoadPalette + ldy #0 +LoopPalette: ;unnamed label + lda PaletteData,y ; lookup byte in ROM + sta PPU_DATA ; set the value to send to PPU_DATA, + ; will auto increment PPU_ADDR + + iny + cpy #32 + bne LoopPalette ; loop to previous label + + rts ;return from subroutine +.endproc + ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;; Reset handler (called when the NES resets or powers on) ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; Reset: - sei ; Disable all IRQ interrupts - cld ; Clear decimal mode (not supported by the NES) - ldx #$FF - txs ; Initialize the stack pointer at address $FF - - inx ; Increment X, causing a rolloff from $FF to $00 - stx PPU_CTRL ; disable NMI - stx PPU_MASK ; disable rendering - stx $4010 ; disable DMC IRQs - - lda #$40 - sta $4017 ; disable APU frame IRQ - -Wait1stVBlank: - bit PPU_STATUS - bpl Wait1stVBlank - - txa ; A = 0 - -ClearRAM: - sta $0000,x ; Zero RAM addresses from $0000 to $00FF - sta $0100,x ; Zero RAM addresses from $0100 to $01FF - sta $0200,x ; Zero RAM addresses from $0200 to $02FF - sta $0300,x ; Zero RAM addresses from $0300 to $03FF - sta $0400,x ; Zero RAM addresses from $0400 to $04FF - sta $0500,x ; Zero RAM addresses from $0500 to $05FF - sta $0600,x ; Zero RAM addresses from $0600 to $06FF - sta $0700,x ; Zero RAM addresses from $0700 to $07FF - inx - bne ClearRAM - -Wait2ndVBlank: - bit PPU_STATUS - bpl Wait2ndVBlank ; bit-7 is 1 + INIT_NES Main: - ldx #$3F - stx PPU_ADDR - ldx #$00 - stx PPU_ADDR + bit PPU_STATUS ; reset the latch + ldx #$3F + stx PPU_ADDR + ldx #$00 + stx PPU_ADDR - lda #$25 - sta PPU_DATA + jsr LoadPalette ;jump to subroutine - lda %00011110 - sta PPU_MASK + lda #%00011110 + sta PPU_MASK LoopForever: - jmp LoopForever + jmp LoopForever ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;; NMI interrupt handler ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; NMI: - rti ; Return from interrupt + rti ; Return from interrupt ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;; IRQ interrupt handler ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; IRQ: - rti ; Return from interrupt + rti ; Return from interrupt + +PaletteData: + .byte $0F,$2A,$0C,$3A, $0F,$2A,$0C,$3A, $0F,$2A,$0C,$3A, $0F,$2A,$0C,$3A ; background + .byte $0F,$10,$00,$26, $0F,$10,$00,$26, $0F,$10,$00,$26, $0F,$10,$00,$26 ; sprite ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;; Vectors with the addresses of the handlers that we always add at $FFFA diff --git a/helloppu/reset.inc b/helloppu/reset.inc new file mode 100644 index 0000000..b1d387a --- /dev/null +++ b/helloppu/reset.inc @@ -0,0 +1,38 @@ +.macro INIT_NES + sei ; Disable all IRQ interrupts + cld ; Clear decimal mode (not supported by the NES) + ldx #$FF + txs ; Initialize the stack pointer at address $FF + + inx ; Increment X, causing a rolloff from $FF to $00 + stx PPU_CTRL ; disable NMI + stx PPU_MASK ; disable rendering + stx $4010 ; disable DMC IRQs + + lda #$40 + sta $4017 ; disable APU frame IRQ + +Wait1stVBlank: + bit PPU_STATUS + bpl Wait1stVBlank + + txa ; A = 0 + +ClearRAM: + sta $0000,x ; Zero RAM addresses from $0000 to $00FF + sta $0100,x ; Zero RAM addresses from $0100 to $01FF + sta $0200,x ; Zero RAM addresses from $0200 to $02FF + sta $0300,x ; Zero RAM addresses from $0300 to $03FF + sta $0400,x ; Zero RAM addresses from $0400 to $04FF + sta $0500,x ; Zero RAM addresses from $0500 to $05FF + sta $0600,x ; Zero RAM addresses from $0600 to $06FF + sta $0700,x ; Zero RAM addresses from $0700 to $07FF + inx + bne ClearRAM + +Wait2ndVBlank: + bit PPU_STATUS + bpl Wait2ndVBlank ; bit-7 is 1 + + +.endmacro