  .inesprg 1   ; 1x 16KB PRG code
  .ineschr 1   ; 1x  8KB CHR data
  .inesmap 0   ; mapper 0 = NROM, no bank swapping
  .inesmir 1   ; background mirroring
  

;;;;;;;;;;;;;;;

    
  .bank 0
  .org $C000 
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

  LDA #$80
  STA $0000
  STA $0001
  LDA #00
  STA $0002



vblankwait2:      ; Second wait for vblank, PPU is ready after this
  BIT $2002
  BPL vblankwait2



; ************** NEW CODE ****************
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 $00
  STA $0200        ; set sprite 0's starting x coordinate
  LDA $01
  STA $0203        ; set sprite 0's starting y coordinate
  LDA #$00
  STA $0201        ; tile number = 0
  LDA #$00
  STA $0202        ; color = 0, no flipping

  LDA #%10000000   ; enable NMI, sprites from Pattern Table 0
  STA $2000

  LDA #%00010000   ; enable sprites
  STA $2001




;=================================================================================
; MAIN LOOP OF DOOM!
;=================================================================================


Forever:

controller_check:
  LDA #$01		; get ready to read controllers
  STA $4016
  LDA #$00
  STA $4016
  LDA $4016		; read A button on controller 1
  LDA $4016		; read B button on controller 1
  LDA $4016		; read Select button on controller 1
  LDA $4016		; read Start button on controller 1

check_up:
  LDA $4016		; read Up button on controller 1
  AND #1
  BEQ check_down        ; if not pressed, move on

  LDA $00               ; if y >= 7 y--
  CMP #08
  BCC check_down
  SBC #1
  STA $00

check_down:
  LDA $4016		; read Down button on controller 1

check_left:
  LDA $4016		; read Left button on controller 1
  AND #1
  BEQ check_right       ; if not pressed, move on

  LDA $01               ; if x >= 8 x--
  CMP #09
  BCC check_right
  SBC #1
  STA $01

check_right:
  LDA $4016		; read Right button on controller 1

check_end:
  LDA $00
  STA $0200             ; set sprite 0's x coordinate
  LDA $01
  STA $0203             ; set sprite 0's y coordinate



  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
  
  RTI        ; return from interrupt
 
;;;;;;;;;;;;;;  
  
  
  
  .bank 1
  .org $E000
palette:
  .db $0F,$31,$32,$33,$0F,$35,$36,$37,$0F,$39,$3A,$3B,$0F,$3D,$3E,$0F  ;background palette data
  .db $0F,$16,$2A,$07,$35,$02,$38,$3C,$0F,$1C,$15,$14,$0F,$02,$38,$3C  ;sprite palette data


  .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