$title (Plotter driver V1.0 bresenham rutine) 

	clkx	BIT	P1.6
	clky	BIT	P1.3
	delh	equ	30 

	errsign	BIT F0


CODESEG		SEGMENT		CODE
		RSEG		CODESEG

		PUBLIC		bres_1
		PUBLIC		BRESENHAM
	
		EXTRN		DATA (error)
		EXTRN		DATA (derror1)
		EXTRN		DATA (derror2)
		EXTRN		DATA (steps)
		EXTRN		DATA (dx)
		EXTRN		DATA (dy)
		EXTRN		DATA (pmask)
		EXTRN		DATA (cw1)
		EXTRN		DATA (cw2)


decstep:
	clr	c
	mov	a,steps
	subb	a,#1
	mov 	steps,a
	mov	a,steps+1
	subb	a,#0
	mov	steps+1,a
	ret

adderror:
	mov	a,error
	add	a,r4
	mov	error,a
	mov	a,error+1
	addc	a,r5
	mov	error+1,a
	jnc	adde1
	  clr	errsign
adde1:	ret

substrerror:
	mov 	a,error
	clr	c
	subb	a,r4
	mov	error,a
	mov	a,error+1
	subb	a,r5
	mov	error+1,a
	jnc	subst1
          setb	errsign
subst1:	ret
	
bres_1: ;bresenham hitra rutina za prvi oktant
	mov 	error,derror1	  	;error <- derror1
	mov	error+1,derror1+1 
	mov	r4,dx			;error <- error-dx
	mov	r5,dx+1			
	call	substrerror		

br_1r:	mov	a,steps			;if step==0
	orl	a,steps+1
	jz	br_1end			;koncaj
	  call decstep			;steps <- steps-1
	  jnb	errsign,br_1neg		;if errror <= 0 skoci na br_1neg
	    clr 	clkx		;step_xi
	    nop
	    setb	clkx
	    mov		r4,derror1	;error <- error+derror1
	    mov		r5,derror1+1
	    call	adderror
	    jmp		br_1d
br_1neg: 
	    clr 	clkx		;step_ix_iy
	    clr		clky
	    setb	clkx
	    setb	clky
	    mov		r4,derror2   	;error <- error-derror2
	    mov		r5,derror2+1
	    call	substrerror
	    call	decstep		;steps <- steps-1
br_1d:    mov 	r5,#delh
br_1d1:	  mov 	r4,#50
	  djnz 	r4,$
	  djnz 	r5,br_1d1
	  jmp 	br_1r
br_1end:
	ret
	


bresenham:
	stepsl	equ	R0
	stepsh	equ	R1
	dxl	equ	R2
	dxh	equ	R3
	dyl	equ	R4
	dyh	equ	R5
	errorl	equ	R6
	errorh	equ	R7

	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=x1-x2
	mov	a,x1
	subb	a,x2
	mov	dxl,a
	mov	a,x1+1
	subb	a,x2+1
	mov	dxh,a
	jnc	brsubdy		;skoci, ce je x1>x2
	clr	c		;sicer dx=x2-x1
	mov	a,x2			
	subb	a,x1
	mov	dxl,a
	mov	a,x2+1
	subb	a,x1+1
	mov	dxh,a
	orl	cw1,#00100000b	;cw1=x01x00xx ;za oktante 3,4,5,6 decx
	orl	cw2,#00100000b	;cw2=x01x00xx
brsubdy:
	clr	c		;dy=x1-x2
	mov	a,y1
	subb	a,y2
	mov	dyl,a
	mov	a,y1+1
	subb	a,y2+1
	mov	dyh,a
	jnc	brcmpdxdy	;skoci ce je y1>y2
	clr	c		;sicer dy=y2-y1
	mov	a,y2
	subb	a,y1
	mov	dyl,a
	mov	a,y2+1
	subb	a,y1+1
	mov	dyh,a
	orl	cw1,#00000100b	;cw1=x0?x01xx ;za oktante 5,6,7,8 decy
	orl	cw2,#00000100b	;cw2=x0?x01xx
brcmpdxdy:
	clrc			;c <- dx-dy
	mov	a,dxl	
	subb	a,dyl
	mov	a,dxh
	subb	a,dyh
	jnc	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	;CY pomeni napako pri racuanju!
	setb	errorsign	;Prizgi predznak za error ker bo
				;error negativen.
	clr	c		;error=-dx
	clr	a
	subb	a,dxl
	mov	errorl,a
	clr	a
	subb	a,dxh
	mov	errorh,a
	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,stepl		;step == 0
	orl	a,steph
	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
	jnc	brmovcw2	;ce je error<0 se gibaj z 2 nacinom
	clr	errorsign	;brisi predznak, ker je error>=0
	mov	P1,cw1		;gibaj se s cw1
	nop
	orl	P1,#01001000b
	mov	a,stepsl	;dec steps
	clr	c
	subb	a,#1
	mov	stepsl,a
	mov	a,stepsh
	subb	a,#0
	jmp	brdelay
brmovcw2:
	mov	P1,cw2		;gibaj se s cw2
	nop
	orl	P1,#01001000b	
	mov	a,stepsl	;steps=steps-2
	clr	c
	subb	a,#2
	mov	stepsl,a
	mov	a,stepsh
	subb	a,#0
	mov	a,errorl	;error=error-dx
	clr	c
	subb	a,dxl
	mov	errorl,a
	mov	a,errorh
	subb	a,dxh
	mov	errorh,a
	jnc	brdelay		;ce ni CY ni negeativen error
	setb	errorsign	;sicer prizgi znak za minus
brdelay:
	
	


	


	
	

	
	
	
	
	
	
	
	
brdxpos:



END