;********************************
;ATALAN nes sprite asm functions
;********************************
; created by Marcel Cevani 31.01.2011
; tnx to Atalan the snake & rudla :)

;_TEMPB1 equ 54  array offset
;_TEMPB2 equ 55  array offset

;  ATALAN code	 
;  PPU_DATA_ADR = $3F
;  PPU_DATA_ADR = $10
;  for i:0..15
;        PPU_DATA  = spritePal(i)
			    
sprLoadPal .proc
			sta _TEMPADR
			stx _TEMPADR+1
			ldx #$10
			ldy #$00
			lda #$3F
            sta $2006
			stx $2006
cpyloop_sr: lda (_TEMPADR),y
			sta $2007
			iny
			cpy #$10
			bne cpyloop_sr 
			rts
			.endp


;3440
setMultiSprite .proc
		    sta _TEMPADR     ; save spr array adr to zerro page
			stx _TEMPADR+1   ; 
		
			lda _SPR_NR
			asl
			tay

			lda  (_TEMPADR),y  
			sta _TEMPADRSPR
			iny
			
			lda  (_TEMPADR),y  
			sta _TEMPADRSPR+1
		
			lda _SPR_ANIM_NR
			asl
			tay

			lda  (_TEMPADRSPR),y  
			sta _TEMPADR
			iny
			
			lda  (_TEMPADRSPR),y  
			sta _TEMPADR+1
			

			ldy #$00		 ; set y pointer = 0
		
			; load xsize,ysize and attribute byte
			lda  (_TEMPADR),y  
			tax					; copy x size to X reg
			iny
			lda  (_TEMPADR),y  
			sta _SPR_YSIZE
			iny
			lda  (_TEMPADR),y  
			eor _SPR_ATTR
			sta _SPR_ATTR
			iny
			lda  (_TEMPADR),y  
			; set pointer to drawCol
			sta _TEMPADRSPR
			lda table+1
			sta _TEMPADRSPR+1

			
			lda _SPR_ATTR
			and #$64
			beq precalc_hnon

			ldx #$00
			; calc XPos table 
			lda	_SPR_XPOS
precalc_hflip:
			sta _SPR_PRECALC , x
			clc					  ; shift xpos + 8
			adc #$08
			inx
			cpx #$04
			bne precalc_hflip
			jmp precalc_end

precalc_hnon:
			; calc XPos table 
			lda	_SPR_XPOS
			;ldx #$00
			dex
precalc_hf: 
            sta _SPR_PRECALC , x
			beq precalc_end
			clc					  ; shift xpos + 8
			adc #$08
			dex
			jmp precalc_hf


precalc_end:
			lda _SPR_ATTR        ; check if we have vertical flip enabled
			and #128
			beq precalc_vnon
								; add y size 
			lda	_SPR_YPOS
			clc	
			adc #24
			sta _SPR_YPOS
			jmp copy_vflip
precalc_vnon:
			ldy #$03
			ldx _OAM_INDEX   ; load back oam index of last write
copy_row:	
			jsr jumpTable2
			dec	_SPR_YSIZE	       ; ySize = ySize -1
			beq copy_end
			lda	_SPR_YPOS
			clc	
			adc #$08
			sta _SPR_YPOS
			jmp copy_row
		
copy_vflip:	
			ldy #$03
			ldx _OAM_INDEX   ; load back oam index of last write
			
copy_vflip_row:	
			jsr jumpTable2
			dec	_SPR_YSIZE	       ; ySize = ySize -1
			beq copy_end
			lda	_SPR_YPOS
			clc
			sbc #07
			sta _SPR_YPOS
			jmp copy_vflip_row
copy_end:			
								  ; save x pointer to _OAM_INDEX
			txa
			sta	_OAM_INDEX	
			rts
			.endp



jumpTable2 .proc
	jmp (_TEMPADRSPR)
	.endp

resetUnusedSprites .proc
			ldx _OAM_INDEX
	fill:	lda #$FF 
			sta $0300 , x		  ; y pos
			inx 
			inx 
			inx 
			inx
			cpx _LAST_FRAME_OAM_INDEX
			bne fill 
			rts
			.endp

.align $B000,$00
drawSpriteColum .proc
s_size_8:
		 iny				  ; copy a tile to ram
	     lda  (_TEMPADR),y    ; get byte out of sprite array
	     beq notCopy8
							  ; copy data to OAM
		 sta $0301 , x		  ; store tile nr
		
		 lda _SPR_YPOS			  
	 	 sta $0300 , x		  ; y pos
		
		 lda _SPR_ATTR
		 sta $0302 , x		  ; store attribute byte 
		
		 lda  _SPR_PRECALC+7; 
		 sta $0303 , x		  ; store x pos
			
		 inx 
		 inx 
		 inx 
		 inx 
notCopy8:
s_size_7:
		 iny				  ; copy a tile to ram
	     lda  (_TEMPADR),y    ; get byte out of sprite array
	     beq notCopy7
							  ; copy data to OAM
		 sta $0301 , x		  ; store tile nr
		
		 lda _SPR_YPOS			  
	 	 sta $0300 , x		  ; y pos
		
		 lda _SPR_ATTR
		 sta $0302 , x		  ; store attribute byte 
		
		 lda  _SPR_PRECALC+6; 
		 sta $0303 , x		  ; store x pos
			
		 inx 
		 inx 
		 inx 
		 inx 
notCopy7:
s_size_6:
		 iny				  ; copy a tile to ram
	     lda  (_TEMPADR),y    ; get byte out of sprite array
	     beq notCopy6
							  ; copy data to OAM
		 sta $0301 , x		  ; store tile nr
		
		 lda _SPR_YPOS			  
	 	 sta $0300 , x		  ; y pos
		
		 lda _SPR_ATTR
		 sta $0302 , x		  ; store attribute byte 
		
		 lda  _SPR_PRECALC+5; 
		 sta $0303 , x		  ; store x pos
			
		 inx 
		 inx 
		 inx 
		 inx 
notCopy6
s_size_5:
		 iny				  ; copy a tile to ram
	     lda  (_TEMPADR),y    ; get byte out of sprite array
	     beq notCopy5
							  ; copy data to OAM
		 sta $0301 , x		  ; store tile nr
		
		 lda _SPR_YPOS			  
	 	 sta $0300 , x		  ; y pos
		
		 lda _SPR_ATTR
		 sta $0302 , x		  ; store attribute byte 
		
		 lda  _SPR_PRECALC+4; 
		 sta $0303 , x		  ; store x pos
			
		 inx 
		 inx 
		 inx 
		 inx 
notCopy5:
s_size_4:
		 iny				  ; copy a tile to ram
	     lda  (_TEMPADR),y    ; get byte out of sprite array
	     beq notCopy4
							  ; copy data to OAM
		 sta $0301 , x		  ; store tile nr
		
		 lda _SPR_YPOS			  
	 	 sta $0300 , x		  ; y pos
		
		 lda _SPR_ATTR
		 sta $0302 , x		  ; store attribute byte 
		
		 lda  _SPR_PRECALC+3; 
		 sta $0303 , x		  ; store x pos
			
		 inx 
		 inx 
		 inx 
		 inx 
notCopy4:
s_size_3:
		 iny				  ; copy a tile to ram
	     lda  (_TEMPADR),y    ; get byte out of sprite array
	     beq notCopy3
							  ; copy data to OAM
		 sta $0301 , x		  ; store tile nr
		
		 lda _SPR_YPOS			  
	 	 sta $0300 , x		  ; y pos
		
		 lda _SPR_ATTR
		 sta $0302 , x		  ; store attribute byte 
		
		 lda  _SPR_PRECALC+2; 
		 sta $0303 , x		  ; store x pos
			
		 inx 
		 inx 
		 inx 
		 inx 
notCopy3:
s_size_2:
		 iny				  ; copy a tile to ram
	     lda  (_TEMPADR),y    ; get byte out of sprite array
	     beq notCopy2
							  ; copy data to OAM
		 sta $0301 , x		  ; store tile nr
		
		 lda _SPR_YPOS			  
	 	 sta $0300 , x		  ; y pos
		
		 lda _SPR_ATTR
		 sta $0302 , x		  ; store attribute byte 
		
		 lda  _SPR_PRECALC+1; 
		 sta $0303 , x		  ; store x pos
			
		 inx 
		 inx 
		 inx 
		 inx 
notCopy2:
s_size_1:
		 iny				  ; copy a tile to ram
	     lda  (_TEMPADR),y    ; get byte out of sprite array
	     beq notCopy1
							  ; copy data to OAM
		 sta $0301 , x		  ; store tile nr
		
		 lda _SPR_YPOS			  
	 	 sta $0300 , x		  ; y pos
		
		 lda _SPR_ATTR
		 sta $0302 , x		  ; store attribute byte 
		
		 lda  _SPR_PRECALC; 
		 sta $0303 , x		  ; store x pos
			
		 inx 
		 inx 
		 inx 
		 inx 
notCopy1:
		rts
.endp


table:
 .word drawSpriteColum