; include file
#dontinclude



function	sega_dma	sega_dma_dst,sega_dma_len,sega_dma_src
rem	sega_dma( destination tile.w , howmany.w , source.l )
rem	does a dma transfer 
rem	replaces command sega_loadtile
rem	

def	sega_dma_len	word
def	sega_dma_src	long
def	sega_dma_dst	word


stack	a6/d6



sega_dma

	move.l	sega_dma_src,d6

	move.w	#$8f02,$c00004	; set vdp autoincrement to words

; length of transfer
	move.l	#0,d7
	move.w	sega_dma_len,d7
	mulu.w	#16,d7
	move.w	d7,sega_dma_len
; calc per tile basis

	move.w	#$9300,d7
	move.w	sega_dma_len,d6
	move.b	d6,d7
	move.w	d7,$C00004	; low byte

	move.w	#$9400,d7
	move.w	sega_dma_len,d6
	lsr.w	#8,d6
	move.b	d6,d7
	move.w	d7,$C00004	; high byte

; source of transfer

	move.w	#$9500,d7
	move.l	sega_dma_src,d6
	lsr.w	#1,d6

	add.b	d6,d7
	move.w	d7,$C00004	; low byte

	move.w	#$9600,d7
	move.l	sega_dma_src,d6
	lsr.l	#8,d6
	lsr.w	#1,d6
	add.b	d6,d7
	move.w	d7,$C00004	; mid byte

	move.w	#$9700,d7
	move.l	sega_dma_src,d6
	swap	d6
	lsr.b	#1,d6
	and.b	#$7f,d6
	add.b	d6,d7

	move.w	d7,$C00004	; high byte rom copy

; destination + write it
	move.l	#0,d7

	move.w	sega_dma_dst,d7
	mulu.w	#32,d7	;per tile

	move.w	d7,d6	;copy

	lsr.w	#6,d6
	lsr.w	#8,d6
	and.w	#3,d6

	and.w	#$3fff,d7
	add.w	#$4000,d7
	swap	d7
	move.w	d6,d7	;

; top is set
	add.b	#128,d7

	move.l	d7,($c00004)


	rts
endfunction


