From e58a6330d96426670b1e522256b4aee7b7854c22 Mon Sep 17 00:00:00 2001 From: Tyrel Souza Date: Mon, 28 Nov 2022 13:15:40 -0500 Subject: [PATCH] clearmem clears the mem and ppu stuff --- .gitignore | 1 + Makefile | 18 ++++++++ clearmem.asm | 104 +++++++++++++++++++++--------------------- helloppu/Makefile | 18 ++++++++ helloppu/helloppu.asm | 104 ++++++++++++++++++++++++++++++++++++++++++ helloppu/nes.cfg | 16 +++++++ nes.cfg | 1 + 7 files changed, 210 insertions(+), 52 deletions(-) create mode 100644 .gitignore create mode 100755 Makefile create mode 100755 helloppu/Makefile create mode 100755 helloppu/helloppu.asm create mode 100755 helloppu/nes.cfg diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..6e92f57 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +tags diff --git a/Makefile b/Makefile new file mode 100755 index 0000000..59acb56 --- /dev/null +++ b/Makefile @@ -0,0 +1,18 @@ +############################################################################### +# Rule to assemble and link all assembly files +############################################################################### +build: + ca65 clearmem.asm -o clearmem.o + ld65 -C nes.cfg clearmem.o -o clearmem.nes + +############################################################################### +# Rule to remove all object (.o) files and cartridge (.nes) files +############################################################################### +clean: + rm *.o *.nes + +############################################################################### +# Rule to run the final cartridge .nes file in the FCEUX emulator +############################################################################### +run: + fceux clearmem.nes diff --git a/clearmem.asm b/clearmem.asm index b5c5ccc..929e762 100644 --- a/clearmem.asm +++ b/clearmem.asm @@ -1,64 +1,64 @@ -;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; -;; The iNES Header contains a total of 16 bytes with -;; flags at $7FF0 -;; https://www.nesdev.org/wiki/INES -;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; +;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; +;; The iNES header (contains a total of 16 bytes with the header flags) +;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; .segment "HEADER" -.org $7FF0 -.byte $4E,$45,$53,$1A ; 4 bytes with chars NES\n -.byte $02 ; 1 byte how many 16kb in pgm-rom -.byte $01 ; 1 byte how many 8kb chr-rom -.byte %00000000 ; Horiz mirroring, nothing else -.byte %00000000 ; Flags 7 - nothing -.byte $00 ; Flags 8 - no PGM-RAM -.byte $00 ; NTSC -.byte $00 ; no PGM-RAM -.byte $00,$00,$00,$00,$00 ; unused padding to complete 16bytes - -;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; -;; PGM-ROM code located at $8000 -;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; +.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 +;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; +;; PRG-ROM code located at $8000 +;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; .segment "CODE" -.org $8000 ; start of program rom +;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; +;; Reset handler (called when the NES resets/powers-on) +;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; +Reset: + sei ; Disable all IRQ interrupts + cld ; Clear the decimal mode (unsupported by the NES) + ldx #$FF + txs ; Initialize the stack pointer at $01FF -;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; -;; -;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; -RESET: - sei ; set interrupt disable - disable all IRQ - cld ; clear decimal mode BCD flag (unused) - ldx #$FF ; - txs ; initialize stack pointer at $01FF + inx ; Increment X, causing a roll-off from $FF to $00 - ;;;;; LOOP ALL MEMORY POSITIONS FROM 00 to FF clearing + 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 ; X++ + bne ClearRAM ; Loops until X reaches 0 again (after roll-off) - lda #$0 ; A = $0 - inx ; increment X from $FF to $0 -MemLoop: - sta $0,x ; store the value of A (zero) into $0 + X - dex ; X-- - bne MemLoop ; loop back to MemLoop if X is not 0 +LoopForever: + jmp LoopForever ; Forces an infinite loop - -;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; -;; NMI Handler - executed every time NMI interrupt -;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; +;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; +;; NMI interrupt handler +;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; NMI: - rti ;return from interrupt + rti ; Return from interrupt -;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; -;; IRQ Handler - executed every time IRQ interrupt -;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; +;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; +;; IRQ interrupt handler +;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; IRQ: - rti ;return from interrupt + rti ; Return from interrupt - -;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; -;; SEGMENTS -;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; +;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; +;; Vectors with the addresses of the handlers that we always add at $FFFA +;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; .segment "VECTORS" -.org $FFFA ; 6502 will always start at $FFFA -.word NMI ; address of the NMI Handler label -.word RESET ; address of the RESET Handler label -.word IRQ ; address of the IRQ Handler label +.word NMI ; Address (2 bytes) of the NMI handler +.word Reset ; Address (2 bytes) of the Reset handler +.word IRQ ; Address (2 bytes) of the IRQ handler diff --git a/helloppu/Makefile b/helloppu/Makefile new file mode 100755 index 0000000..4eeb862 --- /dev/null +++ b/helloppu/Makefile @@ -0,0 +1,18 @@ +############################################################################### +# Rule to assemble and link all assembly files +############################################################################### +build: + ca65 helloppu.asm -o helloppu.o + ld65 -C nes.cfg helloppu.o -o helloppu.nes + +############################################################################### +# Rule to remove all object (.o) files and cartridge (.nes) files +############################################################################### +clean: + rm *.o *.nes + +############################################################################### +# Rule to run the final cartridge .nes file in the FCEUX emulator +############################################################################### +run: + fceux helloppu.nes diff --git a/helloppu/helloppu.asm b/helloppu/helloppu.asm new file mode 100755 index 0000000..ce079e7 --- /dev/null +++ b/helloppu/helloppu.asm @@ -0,0 +1,104 @@ +;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; +;; 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 + +;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; +;; PRG-ROM code located at $8000 +;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; +.segment "CODE" + +;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; +;; 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 + +Main: + ldx #$3F + stx PPU_ADDR + ldx #$00 + stx PPU_ADDR + + lda #$25 + sta PPU_DATA + + lda %00011110 + sta PPU_MASK + +LoopForever: + jmp LoopForever + +;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; +;; NMI interrupt handler +;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; +NMI: + rti ; Return from interrupt + +;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; +;; IRQ interrupt handler +;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; +IRQ: + rti ; Return from interrupt + +;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; +;; Vectors with the addresses of the handlers that we always add at $FFFA +;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; +.segment "VECTORS" +.word NMI ; Address (2 bytes) of the NMI handler +.word Reset ; Address (2 bytes) of the Reset handler +.word IRQ ; Address (2 bytes) of the IRQ handler diff --git a/helloppu/nes.cfg b/helloppu/nes.cfg new file mode 100755 index 0000000..7daa74b --- /dev/null +++ b/helloppu/nes.cfg @@ -0,0 +1,16 @@ +MEMORY { + ZP: start = $0000, size = $0100, type = rw, file = ""; + OAM: start = $0200, size = $0100, type = rw, file = ""; + RAM: start = $0300, size = $0500, type = rw, file = ""; + HDR: start = $7FF0, size = $0010, type = ro, file = %O, fill = yes, fillval = $00; + PRG: start = $8000, size = $8000, type = ro, file = %O, fill = yes, fillval = $00; + CHR: start = $0000, size = $2000, type = ro, file = %O, fill = yes, fillval = $00; +} + +SEGMENTS { + ZEROPAGE: load = ZP, type = zp; + HEADER: load = HDR, type = ro; + CODE: load = PRG, type = ro, start = $8000; + CHARS: load = CHR, type = ro, optional = yes; + VECTORS: load = PRG, type = ro, start = $FFFA; +} \ No newline at end of file diff --git a/nes.cfg b/nes.cfg index 013c1b6..72d6c78 100644 --- a/nes.cfg +++ b/nes.cfg @@ -7,6 +7,7 @@ MEMORY { CHR: start = $0000, size = $2000, type = ro, file = %O, fill = yes, fillval = $00; } + SEGMENTS { ZEROPAGE: load = ZP, type = zp; HEADER: load = HDR, type = ro;