Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- .inesprg 1 ; 1x 16KB PRG code
- .ineschr 1 ; 1x 8KB CHR data
- .inesmap 0 ; mapper 0 = NROM, no bank swapping
- .inesmir 1 ; background mirroring
- ;;;;;;;;;;;;;;;
- ;; DECLARE SOME VARIABLES HERE
- .zp;The .rsset and .rs commands are what you have used before.
- ;They work just as well, but I prefer to choose where each variable goes
- ;It is up to you if you want to use the other syntax
- vblank = $00;This variable is used to keep the frame rate fairly steady
- ballx = $01
- bally = $02
- paddle1x = $03
- paddle1y = $04
- paddle2x = $05
- paddle2y = $06
- ;Zero page RAM locations $07-$FD are free
- buttons1 = $FE;Stores the buttons for player1
- buttons2 = $FF;Stores the buttons for player2
- .code
- .bank 0
- .org $8000
- 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
- stx $4015 ;turn off sound
- 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
- INX
- BNE clrmem
- vblankwait2: ; Second wait for vblank, PPU is ready after this
- BIT $2002
- BPL vblankwait2
- LoadPalettes:
- LDA #$3F
- STA $2006 ; write the high byte of $3F00 address
- LDA #$00
- STA $2006 ; write the low byte of $3F00 address
- LDX #$00 ; start out at 0
- LoadPalettesLoop:
- LDA palette, x ; load data from address (palette + the value in x)
- ; 1st time through loop it will load palette+0
- ; 2nd time through loop it will load palette+1
- ; 3rd time through loop it will load palette+2
- ; etc
- STA $2007 ; write to PPU
- INX ; X = X + 1
- CPX #$20 ; Compare X to hex $10, decimal 16 - copying 16 bytes = 4 sprites
- BNE LoadPalettesLoop ; Branch to LoadPalettesLoop if compare was Not Equal to zero
- ; if compare was equal to 32, keep going down
- clearnametables:
- lda #$20
- sta $2006
- lda #$00
- sta $2006
- tay
- ldx #$0C
- clearnext:
- sta $2007
- iny
- bne clearnext
- dex
- bne clearnext
- 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
- ;;;Set some initial ball stats
- LDA #$50
- STA bally
- LDA #$80
- STA ballx
- main:
- lda vblank;This variable will be set to #$FF when the NMI occurs
- bpl main;But until it occurs, we will keep branching backwards.
- ;This ensures the game runs at a fixed frame rate.
- lda #$00
- sta vblank;This makes vblank 0 again, so when we end up back
- ;At the start of the main loop, it will pause until the NMI occurs
- JSR ReadController1 ;;get the current button data for player 1
- JSR ReadController2 ;;get the current button data for player 2
- JMP main ;jump back to main
- NMI:
- pha;This pushes a, x, and y to the stack.
- txa;Doing this saves them because the nmi will change them
- pha;And the NMI can potentially occur any time
- tya;while your program is being run
- pha
- lda $2002;read PPU status to reset the high/low latch
- 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
- lda #$00
- sta $2005;Writes the x scroll low byte
- sta $2005;Writes the y scroll low byte
- lda #$FF
- sta <vblank
- pla;This pulls a, x, and y from the stack.
- tay;This is so when the nmi returns to the code
- pla;it interrupts, the expected values
- tax;will be there
- pla
- RTI ; return from interrupt
- ReadController1:
- LDA #$01
- STA $4016
- LDA #$00
- STA $4016
- LDX #$08
- ReadController1Loop:
- LDA $4016
- LSR A ; bit0 -> Carry
- ROL buttons1 ; bit0 <- Carry
- DEX
- BNE ReadController1Loop
- RTS
- ReadController2:
- LDA #$01
- STA $4016
- LDA #$00
- STA $4016
- LDX #$08
- ReadController2Loop:
- LDA $4017
- LSR A ; bit0 -> Carry
- ROL buttons2 ; bit0 <- Carry
- DEX
- BNE ReadController2Loop
- RTS
- ;;;;;;;;;;;;;;
- .bank 1
- .org $E000
- palette:
- .db $22,$29,$1A,$0F, $22,$36,$17,$0F, $22,$30,$21,$0F, $22,$27,$17,$0F ;;background palette
- .db $22,$1C,$15,$14, $22,$02,$38,$3C, $22,$1C,$15,$14, $22,$02,$38,$3C ;;sprite palette
- .org $FFFA ;first of the three vectors starts here
- .dw NMI ;when an NMI happens (once per frame if enabled) the
- ;processor will jump to the label NMI:
- .dw RESET ;when the processor first turns on or is reset, it will jump
- ;to the label RESET:
- .dw 0 ;external interrupt IRQ is not used in this tutorial
- ;;;;;;;;;;;;;;
- .bank 2
- .org $0000
- .incbin "mario.chr" ;includes 8KB graphics file from SMB1
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement