$title (Plotter driver V1.0 BRESENHAM rutina) 
$pagewidth  (80)
$pagelength (70)
$registerbank (1)

BRESDSEG		SEGMENT		DATA
		RSEG		BRESDSEG

cw1:		ds	1	;gibanje z nacinom 1
cw2:		ds	1	;gibanje z nacinom 2
accscnt:	ds	1	;stevec korakov za pospeske
delayl:		ds	1	;tekoca zakasnitev
delayh:		ds	1	


CODESEG		SEGMENT		CODE
		RSEG		CODESEG

		PUBLIC		BRESENHAM

		EXTRN		DATA (x1)
		EXTRN		DATA (x2)
		EXTRN		DATA (y1)
		EXTRN		DATA (y2)
		EXTRN		DATA (pmask)
		EXTRN		DATA (maxdly)
		EXTRN		DATA (mindly)
		EXTRN		DATA (ddly)
		EXTRN		DATA (accsteps)	

	

bresenham:

	delaynum    	equ	33000

	accsign		BIT	ACC.7
	brake		BIT	F0
	stepsl		equ	R0
	stepsh		equ	R1
	dxl		equ	R2
	dxh		equ	R3
	dyl		equ	R4
	dyh		equ	R5
	errorl		equ	R6
	errorh		equ	R7
	timerl		equ	TL0
	timerh		equ	TH0
	timerrun	BIT	TR0
	timer_ov	BIT	TF0


	clr	brake		;najprej pospesujemo zato brisi bremzo
	mov	delayl,maxdly  	;delay=maxdelay
	mov	delayh,maxdly+1

	mov	a,pmask		;Beri masko za P1 krmilni port
	anl	a,#10010011b	;Nastavi incx in incy
	mov	cw1,a		;in jih shrani v cw1
	mov 	cw2,a		;ter cw2.
	clr	c		;dx=x2-x1
	mov	a,x2
	subb	a,x1
	mov	dxl,a
	mov	a,x2+1
	subb	a,x1+1
	mov	dxh,a
	jnb	accsign,brsubdy	;skoci, ce je dx>=0
	clr	c		;sicer dx=-dx
	clr	a
	subb	a,dxl
	mov	dxl,a
	clr	a
	subb	a,dxh
	mov	dxh,a
	orl	cw1,#00100000b	;cw1=x01x00xx ;za oktante 3,4,5,6 decx
	orl	cw2,#00100000b	;cw2=x01x00xx
brsubdy:
	clr	c		;dy=y2-y1
	mov	a,y2
	subb	a,y1
	mov	dyl,a
	mov	a,y2+1
	subb	a,y1+1
	mov	dyh,a
	jnb	accsign,brcmpdxdy ;skoci ce je dy>=0
	clr	c		;sicer dy=-dy
	clr	a
	subb	a,dyl
	mov	dyl,a
	clr	a
	subb	a,dyh
	mov	dyh,a
	orl	cw1,#00000100b	;cw1=x0?x01xx ;za oktante 5,6,7,8 decy
	orl	cw2,#00000100b	;cw2=x0?x01xx
brcmpdxdy:
	clr	c		;c <- dx-dy
	mov	a,dxl	
	subb	a,dyl
	mov	a,dxh
	subb	a,dyh
	jnb	accsign,brdxgedy ;skoci ce je dx>=dy
	mov	a,dxl		;sicer swap dx,dy
	xch	a,dyl
	mov	dxl,a
	mov	a,dxh
	xch	a,dyh
	mov	dxh,a
	orl	cw1,#01000000b	;eliminiraj x gibanje za 2, 3, 6, 7
	jmp	brcalc
brdxgedy:
	orl	cw1,#00001000b	;cw1=x0?x1?xx eliminiraj y gibanje za
				; oktante 1, 4, 5, 8
brcalc:
	mov	a,dxl		;steps=dx+dy
	add	a,dyl
	mov 	stepsl,a
	mov	a,dxh
	addc	a,dyh
	mov	stepsh,a	;OV pomeni napako pri racuanju!
	
       	mov	accscnt,accsteps ;predvidi normalno pospesevanje

				;racunam steps-2*accsteps
	mov	a,accsteps	;shl accsteps
	clr	c
	rlc	a
	mov	errorl,a	;error je le zacasni pomnilnik
	clr	a
	rlc	a
	mov	errorh,a
	mov	a,stepsl	;Kontrola ali bo dosezena meja
	clr	c		;maksimalne hitrosti
	subb	a,errorl
	mov	a,stepsh
	subb	a,errorh
	jnb	accsign,bracc3	;pot je dovolj dolga za pospesevanje zato skoci
	mov	a,stepsh	;accscnt=steps div 2
	clr	c
	rrc	a
	mov	a,stepsl
	rrc	a
	mov	accscnt,a

bracc3:
	clr	c		;error=-dx
	clr	a
	subb	a,dxl
	mov	errorl,a
	clr	a
	subb	a,dxh
	mov	errorh,a	;accsign mora biti 1, ce je vse prav
	mov	a,dxl		;shl dx
	clr	c
	rlc	a
	mov	dxl,a
	mov	a,dxh
	rlc	a
	mov	dxh,a		;CY pomeni napako pri mnozenju z 2
	mov	a,dyl		;shl dy
	clr	c
	rlc	a
	mov	dyl,a
	mov	a,dyh
	rlc	a
	mov	dyh,a		;CY pomeni napako pri mnozenju z 2
brstzero:
	mov	a,stepsl	;step == 0
	orl	a,stepsh
	jz	bresenhamend	;ce je koncaj
	mov	a,errorl	;error=error+dy
	add	a,dyl
	mov	errorl,a
	mov	a,errorh
	addc	a,dyh
	mov	errorh,a
	jnb	accsign,brmovcw2 ;ce je error<0 se gibaj z 2 nacinom
	mov	P1,cw1		;gibaj se s cw1
	nop
	orl	P1,#01001000b
	mov	a,stepsl	;dec steps
	clr	c		;brisi CY in da bo odstel le en korak
	jmp	brdelay
bresenhamend:
	ret
brmovcw2:
	mov	P1,cw2		;gibaj se s cw2
	nop
	orl	P1,#01001000b	
	setb	c		;prizgi CY da bo odstel dva koraka
	mov	a,errorl	;error=error-dx
	clr	c		
	subb	a,dxl
	mov	errorl,a
	mov	a,errorh
	subb	a,dxh
	mov	errorh,a	;OV pomeni napako
brdelay:
	jnc	brdlync		;ce ni CY ni porebno preracunavati delay
	mov	a,accscnt	
	jz	brdlync		;ce je accscnt == 0 skoci

	dec	accscnt		;sicer zmanjsaj stevec korakov pospesevanja
	jb	brake,breaking0	;ce je zavora vklopljena potem jo uporabi
	mov	a,delayl	;delay=delay-ddelay
	clr	c
	subb	a,ddly
	mov	delayl,a
	mov	a,delayh
	subb	a,ddly+1
	mov	delayh,a
	setb	c
	jmp	brdlync
breaking0:
	mov	a,delayl
	add	a,ddly
	mov	delayl,a
	mov	a,delayh
	addc	a,ddly+1
	mov	delayh,a
	setb	c
brdlync:
	mov	a,stepsl	;steps=steps-1-CY
	subb	a,#1
	mov	stepsl,a
	mov	a,stepsh
	subb	a,#0
	mov	stepsh,a
 
	mov	a,accscnt
	jnz	brdlydec	;ce ni stevec pospeskov 0 skoci
	mov	a,stepsl
	clr	c		;accsign <- steps-accsteps
	subb	a,accsteps	
	mov	a,stepsh
	subb	a,#0
	jnb	accsign,brwait  ;skoci ce ni potrebno zavirati ne pospesevati
	mov	accscnt,#0FFh	;ostali del poti je zaviranje
	setb	brake
	jmp 	breaking

brdlydec:
	dec	accscnt		;zmanjsaj stevec korakov pospesevanja
	jb	brake,breaking	;ce je zavora vklopljena potem jo uporabi
	mov	a,delayl	;delay=delay-ddelay
	clr	c
	subb	a,ddly
	mov	delayl,a
	mov	a,delayh
	subb	a,ddly+1
	mov	delayh,a
	jmp	brwait
breaking:
	mov	a,delayl
	add	a,ddly
	mov	delayl,a
	mov	a,delayh
	addc	a,ddly+1
	mov	delayh,a

brwait:	
	clr	c		;timer=-delay
	clr	a
	subb	a,delayl
	mov	timerl,a
	clr	a
	subb	a,delayh
	mov	timerh,a


	clr	timer_ov	;brisi zastavico
	setb	timerrun	;pozeni timer za zakasnitev
	jnb	timer_ov,$	;cakaj da timer presteje do konca
	clr	timerrun	;pa ga spet ustavi

	jmp	brstzero
	



outa:	cpl	a
	mov	P1,a
	jmp	$

flasherr:
	mov	P1,#0FFh
	mov	a,#0ffh
fel:	dec	a
    	jnz	fel
	mov 	p1,#0
	mov	a,#0ffh
fel1:	dec	a
    	jnz	fel1
	jmp	flasherr

END