
DrawTileMap:
	push af
		ld a,c
		and %00000011
		rlca
		ld (YShift),a
	pop af
	
	and %00000011
	rlca
	ld (XShift),a
	

	ld a,VTileMapXoff+HTileMapYoffL 	;X offset
	ld ixl,a

	ld iyh,VTilemapWidth ;Screen End byte
	
	
	ifdef vasm
		ld hl,(LTilemapHeight-VTilemapHeight)*2	;Set Spacer
	else
		ld hl,LTilemapHeight-VTilemapHeight*2	;Set Spacer
	endif
	ld (TileYUnused_Plus2-2),hl

	ld a,(ActiveBank)
	add HTileMapYoffH 
	ld ixh,a		;Top byte of screen address

	ld iyl,VTilemapHeight	;Height (24)


DrawTiles:
	ex de,hl	
	di
	exx
		;ld c,&80		;used to move down 1 screen line
		
	exx
	ld c,ixl	;Draw Start L
NextLine:

	ld b,iyl	;Rows
	ld a,c
	exx
		ld l,a		;L from C (Draw Start L)

		ld c,vdpControl
		
		ld a,ixh	;Draw Start
		or &40				;we set bit 6 to define that we want to Write data...
		ld h,a
		
		ld de,64			;64 bytes per X line
	exx
NextTile:
	
	ld e,(hl)	;Get Tile Addr
	inc l
	ld d,(hl)
	inc hl
	
	ld a,d
	and e
	inc a
	jr z,CustomTileTransp	;DE=&FFFF
DrawNormalTileSimple:		;For when Transparency isn't needed
	
	exx
	    out (c),l
	    out (c),h	;Select Dest Address
	exx
	
	;Tilemap takes two bytes,
	ld a,e
	out (vdpData),a		
	ld a,d				;---pcvhn p=Priority (1=Sprites behind) C=color palette
	out (vdpData),a		;(0=back 1=sprite), V=Vert Flip, H=Horiz Flip, N=Tilenum (0-511)
	exx
		
CustomTileDone:
		add hl,de
CustomTileDone2:
	exx
	djnz NextTile
		

	ld de,(TileYUnused_Plus2-2)
	add hl,de
	

	inc c
	inc c
	jr z,NextTileDone
	
	dec iyh
	jp nz,NextLine


NextTileDone:
	ret

CustomTileTransp:			;Special tile code (Empty tile)
	exx
	jp CustomTileDone
	