	
	; MAP data struct

	.def	mapWidth		0
	.def	mapHeight		1
	.def	mapPositionX		2
	.def	mapPositionY		3
	.def	mapScreenWidth		4
	.def	mapScreenHeight		5
	.def	mapTileAdrHigh		6
	.def	mapTileAdrLow		7
	.def	mapTileLutAdrHigh	8
	.def	mapTileLutAdrLow	9
	.def	mapAdrHigh		10
	.def	mapAdrLow		11
	.def	mapAdrCtrlHigh		12
	.def	mapAdrCtrlLow	        13
	.def	mapBobStructAdr		14
	.def	mapAnimAdrHigh		15
	.def	mapAnimAdrLow		16
	.def	mapEnemysAdrHigh	17
	.def	mapEnemysAdrLow		18
	
;******************************
;*
;* drawMap
;* 
;* r0 = mapStruct
;*	
	
drawMap

	;prolog

	push	r1
	push	r2
	push	r3
	push	r4
	push	r5
	push	r6
	push	r7

	;
	
	rqldi	r0,mapPositionX
	rqldi	r0,mapPositionY
	movei	r7,16
	ld	r1	;x
	ld	r2	;y
	
	;nop
	;dump
	
		
	movei	r3,0
	sub	r4,r1,r7	;X = x-16	(screen x offset)
	sub	r5,r3,r2	;Y = -Y
	sub	r4,r3,r4	;X = -X
	
	movei	r3,$f
	asrqi	r1,r4,4		;r1 = tx start 		(X/16)
	asrqi	r2,r5,4		;r2 = ty start 		(Y/16)
	and	r3,r5,r3	;r3 = ty start mod	(Y%16)
	
	;
	
	rqldi	r0,mapScreenWidth
	rqldi	r0,mapScreenHeight
	nop
	ld	r4		;screen width
	ld	r5		;screen height
	
	subi	r4,16		;width - 16 
	asrqi	r5,r5,4		;height / 16
	asrqi	r4,r4,4		;width / 16
	add	r5,r5,r2	;r5 = ty end = height / 16 + ty start
	add	r4,r4,r1	;r4 = tx end = (width - 16) / 16 + tx start
	
	;
	
	cmpeqi	r3,0
	tnt			;its dynamit
	addtqi	r5,r5,0		;ty start mode != 0 then r5++
	nop

	;now
	;
	;r1 = tx start 
	;r2 = ty start 
	;r4 = tx end
	;r5 = ty end
	
	;y loop
	
mapDrawYLoop

	cmples	r2,r5	;y (ty start) <= ty end
	brtc	mapDrawYLoopEnd
	nop		;delay slot
	nop		;delay slot
	nop		;delay slot
	nop		;delay slot
	
	push	r1	;r1 is stored
	push	r5	;r5 is stored
	
	;x loop
	
mapDrawXLoop

	cmples	r1,r4	;x (tx start) <= tx end
	brtc	mapDrawXLoopEnd
	nop		;delay slot
	nop		;delay slot
	nop		;delay slot
	nop		;delay slot
	
	push	r4	;r4 is stored
	push	r1	;r1 is stored 
	push	r2	;r2 is stored 
	
	; in y,x loop	-> r1 = x, r2 = y
	
	;calculate destination position in screen
	
	rqldi	r0,mapPositionX
	rqldi	r0,mapPositionY
	rqldi	r0,mapWidth
	ld	r3	;position x
	ld	r4	;position y
	ld	r7	;map width
		
	or	r5,r1,r1
	or	r6,r2,r2
	muli	r5,16		;px = x * 16
	muli	r6,16		;py = y * 16
	rqldi	r0,mapHeight
	add	r3,r3,r5	;px = screen x + (x * 16) 	
	add	r4,r4,r6	;px = screen y + (y * 16) 
	ld	r6		;map height
	
	;check against map borders
	
	movei	r5,0
	cmplo	r2,r6		;y<map height
	movets	r2,r2,r5
	
	cmplo	r1,r7		;x<map width
	movets	r1,r1,r5
	
	;
	
	mul	r2,r2,r7	;y * map width
	nop
	add	r2,r2,r1	;tileNumber in map = y *  map width + x	
	
	;now
	;
	;r2 = tileNumber in map
	;r3 = screen x
	;r4 = screen y
	
	rqldi	r0,mapAdrLow
	rqldi	r0,mapAdrHigh
	nop
	ld	r5	;low
	ld	r6	;high
		
	add	r5,r5,r2
	addtqi	r6,r6,0
	
	nop
	edrqldi	r6,r5,0	;request value
	
	;
	
	rqldi	r0,mapTileLutAdrLow
	rqldi	r0,mapTileLutAdrHigh
	nop
	ld	r5	;lut low
	ld	r6	;lut high
	
	dexti	$3f
	movei	r7,$ff
	
	eld	r1	;get value 
		
	nop
	or	r2,r1,r1
	and	r1,r1,r7	;remove xy bits
	lsri	r2,14		;xy bits
	muli	r1,2		;* 2 because double word
	or	r7,r2,r2	;r7 = xy bits
	
	add	r5,r5,r1
	addtqi	r6,r6,0
		
	nop	
	edrqldi	r6,r5,1	;request low offset
	edrqldi	r6,r5,0	;request high offset
	
	;
	
	rqldi	r0,mapTileAdrLow
	rqldi	r0,mapTileAdrHigh
	nop
	ld	r5	;main low
	ld	r6	;main high
	nop		;??
		
	eld	r1	;low offset
	eld	r2	;high offset
			
	;add offset to tile main address
	
	add	r5,r5,r1
	addt	r6,r6,r2
	
	;now
	;
	;r3 = screen x
	;r4 = screen y
	;r6:r5 = gfx source
	;r7 = xy bits
	
	push	r0
	
	;------------------ its time to draw the bob
	
	rqldi	r0,mapBobStructAdr
	nop
	nop
	ld	r0
		
	;set xy

	ssto	csFlipXY
	stwo	r0,r7
		
	;set destination x , y
	
	ssto	csDestX
	stwo	r0,r3
	ssto	csDestY
	stwo	r0,r4
	
	ssto	csSourceAdrHigh	
	stwo	r0,r6
	ssto	csSourceAdrLow	
	stwo	r0,r5
	
	;set destination address (draw frame)	
	
	dexti	>drawFrame
	movei	r7,drawFrame
	nop
	rqldi	r7,0
	rqldi	r7,1
	nop
	ld	r2	;high
	ld	r1	;low
	
	ssto	csDestAdrLow		
	stwo	r0,r1
	ssto	csDestAdrHigh
	stwo	r0,r2
		
	;draw it

	dexti	>drawBob
	movei	r1,drawBob
	gpci	r7,2		;next 4 + X
	jmpi	r1,0		;br	
	nop			;delay slot
	nop			;delay slot	
	nop			;delay slot
	nop			;delay slot	
	
	;-------------------
	
	
	rqpop
	nop
	nop
	pop	r0
	
	  
	;
	
	rqpop
	rqpop
	rqpop
	pop	r2
	pop	r1
	pop	r4
	
	br	mapDrawXLoop
	nop		;	delay slot
	nop		;	delay slot
	addi	r1,1	;x++	delay slot
	nop		;	delay slot
	
	;
	
mapDrawXLoopEnd

	
	rqpop
	rqpop
	br	mapDrawYLoop
	pop	r5	;	delay slot
	pop	r1	;	delay slot
	addi	r2,1	;y++	delay slot
	nop		;	delay slot
	
	
	
mapDrawYLoopEnd	
	 
	;epilog
	
	rqpop
	rqpop
	rqpop
	poparqp	r7
	poparqp	r6
	poparqp	r5
	poparqp	r4
	pop	r3
	pop	r2
	pop	r1
	
	jmpi	r7,0
	nop		;delay slot
	nop		;delay slot	
	nop		;delay slot
	nop		;delay slot	
		

;******************************
;*
;* animMap
;* 
;* r0 = map struct
;*

animMap

	;prolog

	push	r1
	push	r2
	push	r3
	push	r4
	push	r5
	push	r6
	push	r7
	
	;
	
	rqldi	r0,mapAnimAdrHigh
	rqldi	r0,mapAnimAdrLow	
	nop
	ld	r6
	ld	r5	;r6:r5 is anim data
	nop
		
animLoop

	edrqldi	r6,r5,0	;counts
	edrqldi	r6,r5,1	;map position
	
	addi	r5,2		;skip 2 words code
	addtqi	r6,r6,0
		
	eld	r7
	eld	r1
	
	;check end
	
	nop
	
	cmpeqi	r7,0	;anim loop end
	brts	animLoopEnd
	nop		;delay slot
	nop		;delay slot
	nop		;delay slot
	nop		;delay slot
	
	;select correct counter
	
	dexti	>counters
	movei	r2,counters	;2,3,4,5,6,7
	nop
	subi	r2,2		;0,1 counter dont exists
	nop
	rqld	r2,r7		;get counter value
	nop
	nop
	ld	r2		;counter value
	
	esadr	r6,r5
	erqld	r2		;get map value
	nop
	eld	r2		;map value (store in map at r1)
	
	rqldi	r0,mapAdrHigh
	rqldi	r0,mapAdrLow	
	nop
	ld	r4
	ld	r3	;r4:r3 is map data
	nop
	add	r3,r3,r1
	addtqi	r4,r4,0
	nop
	esadr	r4,r3
	est	r2,0
	
	
	;
	add	r5,r5,r7	;next entry
	addtqi	r6,r6,0
	
	
	br	animLoop
	nop		;delay slot
	nop		;delay slot
	nop		;delay slot
	nop		;delay slot
	
animLoopEnd	
	
	;epilog
	
	rqpop
	rqpop
	rqpop
	poparqp	r7
	poparqp	r6
	poparqp	r5
	poparqp	r4
	pop	r3
	pop	r2
	pop	r1
	
	jmpi	r7,0
	nop		;delay slot
	nop		;delay slot	
	nop		;delay slot
	nop		;delay slot	
	