~rabbits/famicom-cookbook

fb0b856c3b1c583e07df034a12dbd0b157bc917a — Devine Lu Linvega 3 years ago 2b7096f
Cleaning up examples
15 files changed, 136 insertions(+), 369 deletions(-)

M examples/animation-sine.asm
M examples/audio-tone.asm
M examples/background-attributes.asm
R examples/{background-single/src/cart.asm => background-single.asm}
D examples/background-single/build.sh
D examples/background-single/src/sprite.chr
M examples/build.sh
M examples/cart.nes
D examples/math/build.sh
D examples/math/src/cart.asm
D examples/math/src/head.asm
D examples/math/src/main.asm
D examples/math/src/sprite.chr
D examples/math/src/tables.asm
D examples/math/src/vectors.asm
M examples/animation-sine.asm => examples/animation-sine.asm +3 -0
@@ 81,6 81,9 @@ DrawSprite2:
	STA $0206                          ; set tile.attribute
	LDA #$80
	STA $0207                          ; set tile.x pos

;; enable sprites

	LDA #%10000000                     ; enable NMI, sprites from Pattern Table 0
	STA $2000
	LDA #%00010000                     ; enable sprites

M examples/audio-tone.asm => examples/audio-tone.asm +1 -1
@@ 8,7 8,7 @@
	.dsb $09,$00 ; clear the remaining bytes
	.fillvalue $FF ; Sets all unused space in rom to value $FF

;; CONSTANTS
;; constants

	PPUCTRL                 .equ $2000 
	PPUMASK                 .equ $2001 

M examples/background-attributes.asm => examples/background-attributes.asm +1 -1
@@ 56,7 56,7 @@ Vblankwait2: ; Second wait for vblank, PPU is ready after this
	JSR LoadAttributes

EnableSprites: 
	LDA #%10010000                     ; enable NMI, sprites from Pattern Table 0, background from Pattern Table 1
	LDA #%10000000                     ; enable NMI, sprites from Pattern Table 0, background from Pattern Table 1
	STA $2000
	LDA #%00011110                     ; enable sprites, enable background, no clipping on left side
	STA $2001

R examples/background-single/src/cart.asm => examples/background-single.asm +130 -137
@@ 1,155 1,148 @@

;; header

  .db  "NES", $1a              ; identification of the iNES header
  .db  1                       ; number of 16KB PRG-ROM pages
  .db  $01                     ; number of 8KB CHR-ROM pages
  .db  $70|%0001               ; mapper 7
  .dsb $09, $00                ; clear the remaining bytes
  .fillvalue $FF               ; Sets all unused space in rom to value $FF
  .org $C000
	.db "NES", $1a ; identification of the iNES header
	.db 1 ; number of 16KB PRG-ROM pages
	.db $01 ; number of 8KB CHR-ROM pages
	.db $00 ; NROM
	.dsb $09,$00 ; clear the remaining bytes
	.fillvalue $FF ; Sets all unused space in rom to value $FF

;; constants

	.org $C000

;; Reset

RESET:                         ; 
  SEI                          ; disable 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
Vblankwait1:                   ; First wait for vblank to make sure PPU is ready
  BIT $2002
  BPL Vblankwait1
Clrmem:                        ; 
  LDA #$00
  STA $0000, x
  STA $0100, x
  STA $0300, x
  STA $0400, x
  STA $0500, x
  STA $0600, x
  STA $0700, x
  LDA #$FE
  STA $0200, x                 ; move all sprites off screen
  INX
  BNE Clrmem
Vblankwait2:                   ; Second wait for vblank, PPU is ready after this
  BIT $2002
  BPL Vblankwait2
RESET: ;
	SEI                                ; disable 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

Vblankwait1: ; First wait for vblank to make sure PPU is ready
	BIT $2002
	BPL Vblankwait1

Clrmem: ;
	LDA #$00
	STA $0000, x
	STA $0100, x
	STA $0300, x
	STA $0400, x
	STA $0500, x
	STA $0600, x
	STA $0700, x
	LDA #$FE
	STA $0200, x                       ; move all sprites off screen
	INX 
	BNE Clrmem

Vblankwait2: ; Second wait for vblank, PPU is ready after this
	BIT $2002
	BPL Vblankwait2

;; Main

  LDA #$01
  LDX #$04
  LDY #$21
  JSR DrawAt
  LDA #$01
  LDX #$08
  LDY #$21
  JSR DrawAt

;;

  JSR LoadPalettes
  JSR LoadAttributes

;;

EnableSprites:                 ; 
  LDA #%10010000               ; enable NMI, sprites from Pattern Table 0, background from Pattern Table 1
  STA $2000
  LDA #%00011110               ; enable sprites, enable background, no clipping on left side
  STA $2001
  LDA #$00                     ; No background scrolling
  STA $2006
  STA $2006
  STA $2005
  STA $2005

;;

Forever:                       ; 
  JMP Forever                  ; jump back to Forever, infinite loop

;;

DrawAt:                        ; (a:id,x,y)
  STY $2006
  STX $2006
  STA $2007
  RTS

;;

LoadPalettes:                  ; 
  LDA $2002
  LDA #$3F
  STA $2006
  LDA #$00
  STA $2006
  LDX #$00
@loop:                         ; 
  LDA palettes, x
  STA $2007
  INX
  CPX #$20
  BNE @loop
  RTS

;;

LoadAttributes:                ; 
  LDA $2002
  LDA #$23
  STA $2006
  LDA #$C0
  STA $2006
  LDX #$00
@loop:            ; 
  LDA attributes, x
  STA $2007
  INX
  CPX #$40
  BNE @loop
  RTS

;;

NMI:                           ; 
  LDA #$00
  STA $2003                    ; set the low byte (00) of the RAM address
  LDA #$02
  STA $4014                    ; set the high byte (02) of the RAM address, start the transfer
  RTI                          ; return from interrupt
	LDA #$01
	LDX #$04
	LDY #$21
	JSR DrawAt
	LDA #$01
	LDX #$08
	LDY #$21
	JSR DrawAt
	JSR LoadPalettes
	JSR LoadAttributes

EnableSprites: 
	LDA #%10000000                     ; enable NMI, sprites from Pattern Table 0, background from Pattern Table 1
	STA $2000
	LDA #%00011110                     ; enable sprites, enable background, no clipping on left side
	STA $2001
	LDA #$00                           ; No background scrolling
	STA $2006
	STA $2006
	STA $2005
	STA $2005

Forever: ;
	JMP Forever                        ; jump back to Forever, infinite loop

DrawAt: ; (a:id,x,y)
	STY $2006
	STX $2006
	STA $2007
	RTS 

LoadPalettes: ;
	LDA $2002
	LDA #$3F
	STA $2006
	LDA #$00
	STA $2006
	LDX #$00
@loop: ;
	LDA Palettes, x
	STA $2007
	INX 
	CPX #$20
	BNE @loop
	RTS 

LoadAttributes: ;
	LDA $2002
	LDA #$23
	STA $2006
	LDA #$C0
	STA $2006
	LDX #$00
@loop: ;
	LDA Attributes, x
	STA $2007
	INX 
	CPX #$40
	BNE @loop
	RTS 

NMI: ;
	LDA #$00
	STA $2003                          ; set the low byte (00) of the RAM address
	LDA #$02
	STA $4014                          ; set the high byte (02) of the RAM address, start the transfer
	RTI                                ; return from interrupt

;; Tables

 .org $E000
attributes:                    ; 
  .db %00000000, %00000000, %00000000, %00000000, %00000000, %00000000, %00000000, %00000000
  .db %00000000, %00000000, %00000000, %00000000, %00000000, %00000000, %00000000, %00000000
  .db %00000000, %00000000, %00000000, %00000000, %00000000, %00000000, %00000000, %00000000
  .db %00000000, %00000000, %00000000, %00000000, %00000000, %00000000, %00000000, %00000000
  .db %00000000, %00000000, %00000000, %00000000, %00000000, %00000000, %00000000, %00000000
  .db %00000000, %00000000, %00000000, %00000000, %00000000, %00000000, %00000000, %00000000
  .db %00000000, %00000000, %00000000, %00000000, %00000000, %00000000, %00000000, %00000000
  .db %00000000, %00000000, %00000000, %00000000, %00000000, %00000000, %00000000, %00000000
palettes:                      ; 
  .db $0F,$3B,$16,$2D, $3B,$3B,$0F,$0F, $0F,$3B,$0F,$0F, $0F,$3B,$0F,$0F
  .db $0F,$10,$17,$07, $0F,$3B,$0F,$0F, $0F,$0F,$0F,$0F, $0F,$0F,$0F,$0F
	.org $E000

Attributes: ;
	.db %00000000, %00000000, %00000000, %00000000, %00000000, %00000000, %00000000, %00000000
	.db %00000000, %00000000, %00000000, %00000000, %00000000, %00000000, %00000000, %00000000
	.db %00000000, %00000000, %00000000, %00000000, %00000000, %00000000, %00000000, %00000000
	.db %00000000, %00000000, %00000000, %00000000, %00000000, %00000000, %00000000, %00000000
	.db %00000000, %00000000, %00000000, %00000000, %00000000, %00000000, %00000000, %00000000
	.db %00000000, %00000000, %00000000, %00000000, %00000000, %00000000, %00000000, %00000000
	.db %00000000, %00000000, %00000000, %00000000, %00000000, %00000000, %00000000, %00000000
	.db %00000000, %00000000, %00000000, %00000000, %00000000, %00000000, %00000000, %00000000

Palettes: ;
	.db $0F,$3B,$16,$2D, $3B,$3B,$0F,$0F, $0F,$3B,$0F,$0F, $0F,$3B,$0F,$0F
	.db $0F,$10,$17,$07, $0F,$3B,$0F,$0F, $0F,$0F,$0F,$0F, $0F,$0F,$0F,$0F

;; Vectors

  .pad $FFFA
  .dw NMI
  .dw RESET
  .dw 0
	.pad $FFFA
	.dw NMI
	.dw RESET
	.dw 0

;; Sprites

  .incbin "src/sprite.chr"
\ No newline at end of file
	.incbin "sprite.chr"

D examples/background-single/build.sh => examples/background-single/build.sh +0 -13
@@ 1,13 0,0 @@
#!/bin/bash

# Remove old
rm cart.nes

# Lint source
node ../../tools/lint6502.js

# Build
../../assembler/asm6 src/cart.asm cart.nes

# Run
fceux cart.nes
\ No newline at end of file

D examples/background-single/src/sprite.chr => examples/background-single/src/sprite.chr +0 -0
M examples/build.sh => examples/build.sh +1 -1
@@ 1,6 1,6 @@
#!/bin/bash

target=audio-tone.asm
target=animation-sine.asm

# Remove old cart
if test -f "cart.nes"; then

M examples/cart.nes => examples/cart.nes +0 -0
D examples/math/build.sh => examples/math/build.sh +0 -12
@@ 1,12 0,0 @@
#!/bin/bash

# Remove old
rm cart.nes

# Build

../../assembler/asm6 src/cart.asm cart.nes

# Run

fceux cart.nes
\ No newline at end of file

D examples/math/src/cart.asm => examples/math/src/cart.asm +0 -9
@@ 1,9 0,0 @@
include "src/head.asm"
    
include "src/main.asm"

include "src/tables.asm"

include "src/vectors.asm"

    .incbin "src/sprite.chr"

D examples/math/src/head.asm => examples/math/src/head.asm +0 -74
@@ 1,74 0,0 @@
;;;;;;;;;;;;;;;;;;;;;;;
;;;   iNES HEADER   ;;;
;;;;;;;;;;;;;;;;;;;;;;;

    .db  "NES", $1a     ;identification of the iNES header
    .db  PRG_COUNT      ;number of 16KB PRG-ROM pages
    .db  $01            ;number of 8KB CHR-ROM pages
    .db  $70|MIRRORING  ;mapper 7
    .dsb $09, $00       ;clear the remaining bytes

    .fillvalue $FF      ; Sets all unused space in rom to value $FF

;;;;;;;;;;;;;;;;;;;;;
;;;   VARIABLES   ;;;
;;;;;;;;;;;;;;;;;;;;;

    .enum $0000 ; Zero Page variables

    .ende

;;;;;;;;;;;;;;;;;;;;;
;;;   CONSTANTS   ;;;
;;;;;;;;;;;;;;;;;;;;;

PRG_COUNT       = 1       ;1 = 16KB, 2 = 32KB
MIRRORING       = %0001

PPU_Control     .equ $2000
PPU_Mask        .equ $2001
PPU_Status      .equ $2002
PPU_Scroll      .equ $2005
PPU_Address     .equ $2006
PPU_Data        .equ $2007

spriteRAM       .equ $0200
    .org $C000
    
;;;;;;;;;;;;;;;;;
;;;   RESET   ;;;
;;;;;;;;;;;;;;;;;

RESET:
  SEI          ; disable 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

Vblankwait1:       ; First wait for vblank to make sure PPU is ready
  BIT $2002
  BPL Vblankwait1

Clrmem:
  LDA #$00
  STA $0000, x
  STA $0100, x
  STA $0300, x
  STA $0400, x
  STA $0500, x
  STA $0600, x
  STA $0700, x
  LDA #$FE
  STA $0200, x    ;move all sprites off screen
  INX
  BNE Clrmem
   
Vblankwait2:      ; Second wait for vblank, PPU is ready after this
  BIT $2002
  BPL Vblankwait2
\ No newline at end of file

D examples/math/src/main.asm => examples/math/src/main.asm +0 -103
@@ 1,103 0,0 @@
; No visuals for this one, use the tools/debug-zeropage.lua script
; Press A/B buttons to change the values in memory

LoadPalettes:
  LDA $2002    ; read PPU status to reset the high/low latch
  LDA #$3F
  STA $2006    ; write the high byte of $3F00 address
  LDA #$00
  STA $2006    ; write the low byte of $3F00 address
  LDX #$00
LoadPalettesLoop:
  LDA palette, x        ;load palette byte
  STA $2007             ;write to PPU
  INX                   ;set index to next byte
  CPX #$20            
  BNE LoadPalettesLoop  ;if x = $20, 32 bytes copied, all done

  LDA #%10000000   ; enable NMI, sprites from Pattern Table 1
  STA $2000
  LDA #%00010000   ; enable sprites
  STA $2001

  ; load some data up

  LDA #$09
  STA $00    ; memory addr A
  LDA #$05
  STA $01    ; memory addr B

  ; opposite value

  LDA #$00
  SBC #$10
  STA $02

  ;

  JSR Update

Forever:
  JMP Forever     ;jump back to Forever, infinite loop

NMI:
  LDA #$00
  STA $2003       ; set the low byte (00) of the RAM address
  LDA #$02
  STA $4014       ; set the high byte (02) of the RAM address, start the transfer

LatchController:
  LDA #$01
  STA $4016
  LDA #$00
  STA $4016       ; tell both the controllers to latch buttons

ReadA: 
  LDA $4016
  AND #%00000001  ; only look at bit 0
  BEQ ReadADone
  INC $00 ; memory addr A
  JSR Update
ReadADone:        ; handling this button is done
  
ReadB: 
  LDA $4016
  AND #%00000001  ; only look at bit 0
  BEQ ReadBDone
  INC $01 ; memory addr B
  JSR Update
ReadBDone:        ; handling this button is done

  RTI        ; return from interrupt

Update:
  JSR Mod
  STA $10

  JSR Div
  STA $11
  RTS

; modulus, returns in register A

Mod:
  LDA $00  ; memory addr A
  SEC
Modulus:  
  SBC $01  ; memory addr B
  BCS Modulus
  ADC $01
  RTS

; division, rounds up, returns in register A

Div:
  LDA $00 ; memory addr A
  LDX #0
  SEC
Divide:   
  INX
  SBC $01 ; memory addr B
  BCS Divide
  TXA
  RTS
\ No newline at end of file

D examples/math/src/sprite.chr => examples/math/src/sprite.chr +0 -0
D examples/math/src/tables.asm => examples/math/src/tables.asm +0 -9
@@ 1,9 0,0 @@
;;;;;;;;;;;;;;;;;;
;;;   TABLES   ;;;
;;;;;;;;;;;;;;;;;;

  .org $E000

palette:
  .db $0F,$2D,$16,$2D, $0F,$0F,$0F,$0F, $0F,$0F,$0F,$0F, $0F,$0F,$0F,$0F
  .db $0F,$10,$17,$07, $0F,$0F,$0F,$0F, $0F,$0F,$0F,$0F, $0F,$0F,$0F,$0F

D examples/math/src/vectors.asm => examples/math/src/vectors.asm +0 -9
@@ 1,9 0,0 @@
;;;;;;;;;;;;;;;;;;;
;;;   VECTORS   ;;;
;;;;;;;;;;;;;;;;;;;

    .pad $FFFA

    .dw NMI
    .dw RESET
    .dw 0
\ No newline at end of file