ScreenInit:
	lda #>SnesScreenBuffer
	sta z_H
	lda #<SnesScreenBuffer
	sta z_L
	
	lda #$08
	sta z_B
	lda #$00
	sta z_C
	
	jsr cldir

		 ;aaaabbbb -aaa=base addr for BG2 bbb=base addr for BG1
	lda #%00010001
	sta $210B 		;BG1 & BG2 VRAM location register [BG12NBA]                    
	
	;     xxxxxxss 	- xxx=address… ss=SC size  00=32x32 01=64x32 10=32x64 11=64x64
	lda #%00000000
	sta $2107		;BG1SC - BG1 Tilemap VRAM location
	
	;	  x000bbbb - x=screen disable (1=disable) bbbb=brightness (15=max)
	lda #%10001111	;INIDISP - Screen display register
	sta $2100

	lda #0			
	sta $2121		;CGADD - Colour # (or pallete) selection  
	lda #%00000000	;?bbbbbgg 
	sta $2122		;CGDATA - Colour data register
	lda #%00111100	;gggrrrrr 
	sta $2122

	lda #1
	sta $2121		;CGADD - Colour # (or pallete) selection  
	lda #%11100000	;?bbbbbgg 
	sta $2122		;CGDATA - Colour data register
	lda #%11111111	;gggrrrrr 
	sta $2122
	
	lda #2
	sta $2121		;CGADD - Colour # (or pallete) selection  
	lda #%00011111	;?bbbbbgg
	sta $2122		;CGDATA - Colour data register
	lda #%00000000	;gggrrrrr 
	sta $2122
	
	lda #3
	sta $2121		;CGADD - Colour # (or pallete) selection  
	lda #%11111111	;?bbbbbgg 
	sta $2122		;CGDATA - Colour data register
	lda #%00000111	;gggrrrrr 
	sta $2122
	
	;	  i000abcd - I 1=inc on $2118 or $2139 0=$2119 or $213A… abcd=move size
	lda #%00000000
	sta $2115 		;VMAIN - Video port control (Inc on write to $2118)
	
		; abcdefff - abcd=tile sizes e=pri fff=mode def
	lda #%00001001
	sta $2105		;BGMODE - Screen mode register

	jsr load_font
	
	
	lda #%00000010				;Set Sprite pos to $4000
	sta $2101	;OAM settings
	
	;Clear sprite OAM
	lda #0
	sta $2102	;OAM Address L
	sta $2103	;OAM Address H
	
	ldx #0
	ldy #4
ZeroSprites	
	sta $2104  ;SpriteData
	dex 
	bne ZeroSprites
	dey
	bne ZeroSprites
	
	;Clear Sprite 0
	lda #<$4000			
	sta z_E					
	lda #>$4000		
	sta z_D
	jsr prepareVram

	ldx #16
ClearSpritePattern:
	stz $2119
	stz $2118
	dex
	bne ClearSpritePattern
	
	
	;        S4321
	lda #%00010001	;Turn on BG1 / Sprites
	sta $212C 		;TM - Main screen designation                             
	
	;	  x000bbbb - x=screen disable (1=disable) bbbb=brightness (15=max)
	lda #%00001111	;INIDISP - Screen display register
	sta $2100
	
	;Set Scroll position
	lda #0
	sta $210D  		;BG1HOFS BG1 horizontal scroll   
	lda #0
	sta $210D  		;BG1HOFS
	lda #-1
	sta $210E  		;BG1VOFS BG1 vertical scroll 
	lda #0
	sta $210E  		;BG1VOFS
	
	rts

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
WaitVblankSafe:
	pha
	jsr WaitVblank
	pla
	rts
WaitVblank:
	lda $4212 			;HVBJOY - Status 
	
		; xy00000a		- x=vblank state y=hblank state a=joypad ready
	and #%10000000
	beq WaitVblank		;Wait until we get nonzero - this means we're in VBLANK
	rts	
	
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;	

CustomNmihandler:
	php
	PushAll
		lda #0
		sta $2116		;MemL -Video port address [VMADDL/VMADDH]                            
		sta $2117		;MemH
		
		lda #128
		sta $2115			;Inc address on write to $2119
			 
		lda #%00000001		;Write mode 001=two bytes alternate
		sta $4300
		
		lda #$18
		sta $4301			;Destination $21--
		
		lda #<SnesScreenBuffer
		sta $4302			;Source (24 bit - Little endian)
		lda #>SnesScreenBuffer
		sta $4303
		lda #0				;bits 16-23
		sta $4304
		
		lda #<(32*32*2)
		sta $4305			;No of bytes (24 bit - Little endian
		lda #>(32*32*2)			;(only 1st 16 bits used?)
		sta $4306
		lda #0
		sta $4307
		
		lda #0
		sta $420C			;Disable H-DMA transfer 
		lda #%00000001		
		sta $420B			;enable DMA 0 (bit0=1)
		
		lda #0
		sta $2115			;Inc address on write to $2118
	PullAll
	plp
	rti
	

GetVDPScreenPos:	; BC=XYpos	
		lda z_C
		ifdef ScrWid256	;256x192
			clc
			adc #2
		endif
		sta z_H			;32 tiles per Y line
		lda #0
		clc
		ror z_H
		ror 
		ror z_H
		ror 
		
		adc z_B 	
		adc z_B 	
		sta z_L
		
		lda z_H
		adc #0
		sta z_H
		
		lda z_L			;Calculate address in Buffer
		adc #<SnesScreenBuffer
		sta z_L
		                    
		lda z_H
		adc #>SnesScreenBuffer
		sta z_H
	rts

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;	
	
	
	;lda #3	;SX
	;sta z_B
	;lda #3	;SY
	;sta z_C
	
	;ldx #6	;WID
	;ldy #6	;HEI
	
	;lda #0	;TileStart
	
FillAreaWithTiles:
	sta z_D
FillAreaWithTiles_Yagain:
	jsr GetVDPScreenPos
	PushAll
		ldy #0
FillAreaWithTiles_Xagain:
		lda z_D			;ttttttttt
		sta (z_HL),Y 	;sta $2118	;l
		iny
		lda #$00		;vhoppptt
		sta (z_HL),Y 	;$2119	;h -Video port data [VMDATAL/VMDATAH]                             
		iny
		
		inc z_D
		dex 
		bne FillAreaWithTiles_Xagain
		inc z_C
	PullAll
	dey
	bne FillAreaWithTiles_Yagain
	rts
	
	

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;	
	
	
		
	;BC=Bytes
	;DE=Destination Ram
	;HL=Source Bytes
DefineTiles:
	jsr prepareVram
	
	ldy #0
DefineTilesAgain	
	jsr WaitVblank
	lda (z_HL),Y	
	sta $2119
	jsr DecBC
	jsr IncHL
		
	jsr WaitVblank	
	lda (z_HL),Y	
	sta $2118
	jsr DecBC
	jsr IncHL
		
	lda z_B
	ora z_C
	bne DefineTilesAgain
	
DefineTileDone:
	lda #%10000000		;Turn on interrupts - FOR BUFFER
	sta $4200
	rts
	
prepareVram:	
	lda #%00000000		;Turn off interrupts - FOR BUFFER
	sta $4200
	
		 	
	jsr WaitVblank
	lda z_E
	sta $2116		;MemL
	lda z_D
	sta $2117		;MemH
	rts
	

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;	
	
		
	
	
SetHardwareSprite:
	jsr WaitVblankSafe
		tay
		asl
		sta $2102	;Address L
		lda #0
		sta $2103	;Address H
		lda z_ixl
		sta $2104  ;X
		lda z_iyl
		sta $2104  ;Y
		lda z_H
		sta $2104  ;Tile
		lda z_L
		sta $2104  ;Attribs
		tya
		and %11111100
		lsr
		lsr
		pha
			jsr WaitVblankSafe
			sta $2102		;Address L
			lda #1
			sta $2103		;Address H
			
		 
			lda $2138
			sta z_As		;Get current attr2
		pla
		sta $2102			;Address L
		lda #1
		sta $2103			;Address H
		 
		lda #%00000011		;Prep the mask
		sta z_B
		 
		tya
		and #%00000011		;4 sprites per byte of attr2
		tax
		 
		lda z_ixh
		and #%00000011
		 
		cpx #0				;Shift bits ------sx into correct position
		beq SpriteSkipShift
SpriteShiftAgain:
		asl
		asl					;Shift new val
		asl z_B
		asl z_B				;Shift mask
		dex
		; jsr monitor
		bne SpriteShiftAgain
SpriteSkipShift: 
		pha
			lda z_As		;Get back current value
			and z_B			;Apply mask
			sta z_As		;Get back current value
			
			
			
		pla
		ora z_As
		sta $2104  
		
		
	rts