( draw routines )

:dev/r fff8 ( std read port )
:dev/w fff9 ( std write port )

;x_ 2 ;y_ 2 ;color 1
;x0 2 ;y0 2 ;x1 2 ;y1 2 ;dx 2 ;dy 2 ;err1 2 ;err2 2

|0100 @RESET

	( set dev/write to screen ) 
	#01 =dev/w ( set dev/write to sprite ) 

	( positive )
	#01 ,color STR
	+0030 ,x0 STR2 +0040 ,y0 STR2
	+0100 ,x1 STR2 +0060 ,y1 STR2
	,line JSR

	#02 ,color STR
	+0020 ,x0 STR2 +0010 ,y0 STR2
	+0090 ,x1 STR2 +0070 ,y1 STR2
	,line JSR

	#03 ,color STR
	+0010 ,x0 STR2 +0040 ,y0 STR2
	+0070 ,x1 STR2 +0060 ,y1 STR2
	,line JSR

	( draw control points )
	#02 ,color STR
	,x0 LDR2 ,y0 LDR2 ,putpixel JSR
	,x1 LDR2 ,y1 LDR2 ,putpixel JSR

	,redraw JSR

BRK

@line
	,x0 LDR2 ,x_ STR2 ,y0 LDR2 ,y_ STR2                   ( start at x0,y0 )
	,x1 LDR2 ,x0 LDR2 ,diff16sub JSR ,dx STR2             ( int dx = abs[x1 - x0] )
	,y1 LDR2 ,y0 LDR2 ,diff16sub JSR -0001 MULS2 ,dy STR2 ( int dy = -abs[y1 - y0] )
	,dx LDR2 ,dy LDR2 ADDS2 ,err1 STR2                    ( int err1 = dx + dy, e2; )
	@lineloop
	,x_ LDR2 ,y_ LDR2 ,putpixel JSR                       ( draw )
	@line-x
	,err1 LDR2 +0002 MULS2 ,err2 STR2                     ( e2 = 2 * err; )
	,err2 LDR2 ,dy LDR2 LTHS2 ,line-y ROT JMP? POP2 	  ( e2 >= dy )
		,err1 LDR2 ,dy LDR2 ADDS2 ,err1 STR2 			  ( err1 += dy; )
		,x_ LDR2 +0001 ADDS2 ,x_ STR2                     ( y0 += y0 < y1 ? 1 : -1; )
	@line-y
	,err2 LDR2 ,dx LDR2 GTHS2 ,line-end ROT JMP? POP2     ( e2 <= dx )
		,err1 LDR2 ,dx LDR2 ADDS2 ,err1 STR2              ( err1 += dx; )
		,y_ LDR2 +0001 ADDS2 ,y_ STR2                     ( y0 += y0 < y1 ? 1 : -1; )
	@line-end
	,x_ LDR2 ,x1 LDR2 NEQS2 ,lineloop ROT JMP? POP2       ( loop )
	RTS

@redraw
	#0000 IOW2
	#0000 IOW2
	#00 IOW
	#01 IOW
	RTS

@putpixel 
	IOW2 ( y short )
	IOW2 ( x short )
	,color LDR IOW  ( color byte )
	#00 IOW  ( redraw byte )
	RTS

@diff16
	OVR2 OVR2 GTH2 ,diff16sub ROT JMP? POP2
	SWP2 @diff16sub SUB2
RTS

|c000 @FRAME BRK 
|d000 @ERROR BRK 
|FFFA .RESET .FRAME .ERROR