





;------------------------------------------------------------------------
;-  Written by: Neviksti
;-     If you use my code, please share your creations with me
;-     as I am always curious :)
;------------------------------------------------------------------------
      
.BANK 0 SLOT 0
.ORG HEADER_OFF
.SECTION "QuickCodeSection" SEMIFREE


VBlank:
  pha          ;
  phx          ;
  phy          ;     make sure to store all registers
  phb          ;     before you run your IRQ routine
  php          ;     and pull them back after you're done
  phk          ;     so when it returns it will not have screwed
  plb          ;     up anything!
        
        

	sep #$20		; mem/A = 8 bit, X/Y = 16 bit
  rep #$10


  jump jDoScrolling



;;change sprite graphics here
	lda cursorX		
	sta SpriteBuf1
	lda cursorY
  sec
  sbc #$10
;  clc
;  adc #$08
	sta SpriteBuf1+1


	;*********transfer sprite data

	stz $2102		; set OAM address to 0
	stz $2103

	LDY #$0400
	STY $4300		; CPU -> PPU, auto increment, write 1 reg, $2104 (OAM data write)
	LDY #SpriteBuf1
	STY $4302		; source offset
	LDY #$0220
	STY $4305		; number of bytes to transfer
	LDA #$00
	STA $4304		; bank address = $7F  (work RAM)
	LDA #$01
	STA $420B		;start DMA transfer channel 2



	;*********transfer BG3 data
	LDA #$00
	STA $2115		;set up VRAM write to write only the lower byte

	LDX #$0400
	STX $2116		;set VRAM address to BG3 tile map

	LDY #$1800
	STY $4300		; CPU -> PPU, auto increment, write 1 reg, $2118 (Lowbyte of VRAM write)
	LDY #TextBuffer
	STY $4302		; source offset
	LDY #$0400
	STY $4305		; number of bytes to transfer
	LDA #$00
	STA $4304		; bank address = $7F  (work RAM)
	LDA #$01
	STA $420B		;start DMA transfer channel 2



	;update the joypad data
	JSR GetInput


  lda scrollY
	sta $2112
  ;lda scrollX+1  ;Scroll BG3 vertical
	stz $2112         


	lda $4210		;clear NMI Flag

	REP #$30		;A/Mem=16bits, X/Y=16bits
	
	inc FrameNum

  plp     ;    pull original registers back!
  plb     ;
  ply     ;
  plx     ;
  pla     ;
  RTI
  
;============================================================================

QuickSetup:
	php

	rep #$10		;A/mem = 8bit, X/Y=16bit
	sep #$20

	;Load palette to make our pictures look correct
	LoadPalette	BG_Palette

	;Load Tile and Character data to VRAM
	LoadBlockToVRAM	ASCIITiles, $0000, $0800	;128 tiles * (2bit color = 2 planes) --> 2048 bytes
	LoadBlockToVRAM	SpriteTiles, $6000, $2000	;16 32x32 tiles * (4bit color = 4 planes) --> 8192 bytes

	;Set the priority bit of all the BG3 tiles
	LDA #$80
	STA $2115		;set up the VRAM so we can write just to the high byte
	LDX #$0400
	STX $2116
	LDX #$0400		;32x32 tiles = 1024
	LDA #$20
__next_tile:
	STA $2119
	DEX
	BNE __next_tile

  ;lda #$A3		;Sprites 32x32 or 64x64, character data at $6000 (word address)
  lda #%01100011  ;16x16 sprites
  sta $2101         


  lda #$01		;Set video mode 1, 8x8 tiles (16 color BG1 + BG2, 4 color BG3)
  sta $2105         

  lda #$08		;Set BG1's Tile Map VRAM offset to $0800 (word address)
  sta $2107		;   and the Tile Map size to 32 tiles x 32 tiles

  lda #$0C		;Set BG2's Tile Map VRAM offset to $0C00 (word address)
  sta $2108		;   and the Tile Map size to 32 tiles x 32 tiles

  lda #$04		;Set BG3's Tile Map VRAM offset to $0400 (word address)
  sta $2109		;   and the Tile Map size to 32 tiles x 32 tiles

  lda #$41		;Set BG1's Character VRAM offset to $1000 (word address)
  sta $210B		;Set BG2's Character VRAM offset to $4000 (word address)

  lda #$00		;Set BG3's Character VRAM offset to $0000 (word address)
  sta $210C		;


  lda #%00010100		;Turn on BG1 and BG2 and BG3 and sprites
  sta $212C
  sta $212E

  lda #$FF		;Scroll BG3 down 1 pixel
  sta $2112
  ; lda scrollX+1
  stz $2112
  
  stz $2111  ;bg3 horiz scroll
  stz $2111


  lda #$0F		;Turn on screen, full brightness
  sta $2100		

	plp
	rts






DoScrolling:

CheckCursor:
  ;check cursor counter, no scroll if counter=0
  lda cursorYCounter
  cmp #$00
  beq CursorDone
  dec cursorYCounter  
  ;scroll = scroll + scrollUp - scrollDown
CursorScroll:
  lda cursorY
  sec
  sbc cursorYUp
  clc
  adc cursorYDown
  sta cursorY
CursorDone:
  
CheckYScroll:
  ;check scroll counter, no scroll if counter=0
  lda scrollYCounter
  beq DoYScrollDone
  dec scrollYCounter  
  ;scroll = scroll - scrollUp + scrollDown
DoYScroll:
  lda scrollY
  sec
  sbc scrollYUp
  clc
  adc scrollYDown
  sta scrollY
;	cmp #$F0 ; 240
;	bcc	DoYScrollDone  ; scrollY < 240, no underflow/overflow
;DoYScrollOverflow:
;	cmp	#$F8	; 248
;	bcs	DoYScrollUnderflow  ; scrollY > 248, no overflow
;	sec
;	sbc	#$F0	; scroll = scroll - 240
;	sta	scrollY
;	jmp	DoYScrollDone
;DoYScrollUnderflow:
;	lda scrollY
;	sec
;	sbc	#$10
;	sta	scrollY
DoYScrollDone:

  rts
  
  
  
  
  
  
  
ScrollUp:
  sec
  lda selectedEntry
  sbc #$01
  sta selectedEntry
  lda selectedEntry+1              ;go to prev entry
  sbc #$00
  sta selectedEntry+1

  lda selectedEntry
  cmp #$FF
  bne ScrollUpCheckTop
  lda selectedEntry+1
  cmp #$FF
  bne ScrollUpCheckTop             ;check if selectedEntry = FFFF (underflow)
  
  lda filesInDir
  sta selectedEntry
  lda filesInDir+1
  sta selectedEntry+1              ;underflow, reset selectedEntry = filesInDir-1
  sec
  lda selectedEntry
  sbc #$01
  sta selectedEntry
  lda selectedEntry+1              
  sbc #$00
  sta selectedEntry+1  

  lda filesInDir+1
  cmp #$00
  bne ScrollUpCheckTop
  lda filesInDir
  cmp #$1d
  bcs ScrollUpCheckTop              ; check if filesInDir > 28
  
  lda filesInDir
  asl a
  asl a
  asl a                         ;multiply by 8 for sprite height
  clc
  adc #$08
  sta cursorY                       ; put cursor at bottom of list
  jmp ScrollUpCheckMiddle

ScrollUpCheckTop:
  lda cursorY
  cmp #$08
  bne ScrollUpCheckMiddle           ;check if cursor at top
  
  lda speedCounter
  sta scrollYCounter                ;cursor at top, scroll background, leave cursor
  lda speedScroll
  sta scrollYUp
  lda #$00
  sta scrollYDown
  

  lda insertTop
  sta temp
  jump jPrintClearLine
  lda temp
  asl a
  asl a
  asl a
  asl a  
  asl a
  sta Cursor   
  lda temp
  lsr a
  lsr a
  lsr a
  sta Cursor+1
  jump jDirPrintEntry

  lda insertBottom
  sec
  sbc #$01
  and #%00011111
  sta insertBottom
  lda insertTop
  sec
  sbc #$01
  and #%00011111
  sta insertTop
  
  jmp ScrollUpDone
    
ScrollUpCheckMiddle:    
  lda speedCounter
  sta cursorYCounter
  lda speedScroll
  sta cursorYUp
  lda #$00
  sta cursorYDown

ScrollUpDone:
  rts
  
  



ScrollDown:
  clc
  lda selectedEntry
  adc #$01
  sta selectedEntry
  lda selectedEntry+1              ;go to next entry
  adc #$00
  sta selectedEntry+1
  
  lda selectedEntry
  cmp filesInDir
  bne ScrollDownCheckBottom
  lda selectedEntry+1
  cmp filesInDir+1
  bne ScrollDownCheckBottom        ;check if selectedEntry = filesInDir
  
  lda #$00
  sta selectedEntry
  sta selectedEntry+1              ;overflow, reset to 0
  
  lda filesInDir+1
  cmp #$00
  bne ScrollDownCheckBottom
  lda filesInDir
  cmp #$1d
  bcs ScrollDownCheckBottom         ; check if filesInDir > 28
  
  lda #$00
  sta cursorY                       ; put cursor at top of screen
  jmp ScrollDownCheckMiddle
  
ScrollDownCheckBottom:
  lda cursorY
  cmp #$E0   ;= pixel 224
  bne ScrollDownCheckMiddle         ; check if cursor at bottom
  
  lda speedCounter                  ;cursor at bottom, move background, leave cursor
  sta scrollYCounter                ;set scrollYCounter (8 or 4)
  lda speedScroll
  sta scrollYDown                   ;set scrollYDown to speed (1 or 2)
  lda #$00
  sta scrollYUp
  
ScrollDownCheckBottomDone:
  lda insertBottom
  sta temp
  jump jPrintClearLine
  lda temp
  asl a
  asl a
  asl a
  asl a  
  asl a
  sta Cursor   
  lda temp
  lsr a
  lsr a
  lsr a
  sta Cursor+1      
  
  jump jDirPrintEntry

  lda insertBottom
  clc
  adc #$01
  and #%00011111
  sta insertBottom
  lda insertTop
  clc
  adc #$01
  and #%00011111
  sta insertTop
  
  jmp ScrollDownDone
    
ScrollDownCheckMiddle:
  lda speedCounter
  sta cursorYCounter
  lda speedScroll
  sta cursorYDown
  lda #$00
  sta cursorYUp
    
ScrollDownDone:
  rts 
  
  
  
  
  
  
  
  
  
  
.ENDS

;==========================================================================================

.BANK 1 SLOT 0
.ORG 0
.SECTION "CharacterData"

BG_Palette:
	;.INCBIN ".\\Pictures\\ascii.clr";
	;.REPT 128
	;.DW 0
	;.ENDR
	.INCBIN ".\\Pictures\\mymap2.clr"
	.INCBIN ".\\Pictures\\dwarf2.clr"

ASCIITiles:
	.INCBIN ".\\Pictures\\ascii.smc"

SpriteTiles:
	.INCBIN ".\\Pictures\\dwarf.smc"

.ENDS

;==========================================================================================
