*******************************************************************************
*LINE from d0,d1 (x1,y1) to d2,d3 (x2,y2) without blitter. (Raped Bresenham :-)
*Uses only 68000 instructions (almost no gain by using higher CPU intructions)
*Can be converted to work in chunky-mode because it 'walks' through adresses.
*I've included some psuedo-C comments to clearify the sourcecode.
*Made by Patrick van Logchem (v912152@si.hhs.nl) on 7 June 1993.
*******************************************************************************

	cnop	0,8
	
CPUline:
	lea	screen,a1	; offset='start adress of bitplane'

	move.w	#Width>>3,d4
	muls	d1,d4
	add.w	d4,a1		; 'add Y offset to screen-pointer'

	move.w	d0,d4
	lsr.w	#3,d4
	add.w	d4,a1		; 'add X offset to screen-pointer'

	moveq	#1,d4
	move.w	d2,d6
	sub.w	d0,d6		; ax = abs(x2 - x1)
	bge.s	noABSx
	neg.w	d6
	neg.w	d4		; sx = sign(x2 - x1)		/* X-step */
noABSx:
	move.w	#Width>>3,d5		
	move.w	d3,d7
	sub.w	d1,d7		; ay = abs(y2 - y1)
	bge.s	noABSy
	neg.w	d7
	neg.w	d5		; sy = (Width/8)*sign(y2 - y1)	/* Y-step */
noABSy:
	cmp.w	d6,d7
	bgt.w	ydomi		; if(ax > ay)

;-----------------------------------------
; X dominant part:

xdomi:
	move.w	d6,d1		; d1 = counter	/* length of line = ax */
	move.w	d6,d2
	asr.w	#1,d2		; D = ax>>1	/* 'D' is digital error */

	not.b	d0		; X = bitnr(X)
	and.w	#7,d0		; X &= 7
	bchg.b	d0,(a1)		; 'change bit on [offset]'
	dbf	d1,xloop
	rts

	nop	
	nop
	nop ; use nop's to make sure xloop is on a 8 byte boundary (for speed)
xloop:
	sub.w	d7,d2		; D -= ay
	bgt.s	x_no_e		; if(D < 0) {
	add.w	d6,d2		;  D += ax
	add.w	d5,a1		;  offset += sy }
x_no_e:
	sub.w	d4,d0		; X -= sx	/* next bitnr */
	btst	#3,d0		; /* bit 3 clear = X between 0 and 7 ? */
	beq.s	xjump		; if(X & 8) {
	and.w	#7,d0		;  X &= 7
	add.w	d4,a1		;  offset += sx }
xjump:
	bchg.b	d0,(a1)		; 'change bit on [offset]'
	dbf	d1,xloop
	rts

;-----------------------------------------
; Y dominant part:

ydomi:
	move.w	d7,d1		; d1 = counter	/* length of line = ay */

	move.w	d7,d2
	asr.w	#1,d2		; D = ay>>1	/* 'D' is digital error */

	not.b	d0		; X = bitnr(X)
	and.w	#7,d0		; X &= 7
	bchg.b	d0,(a1)		; 'change bit on [offset]'
	dbf	d1,yloop
	rts

	nop
	nop
	nop
yloop:
	sub.w	d6,d2		; D -= ax
	bgt.s	y_no_e		; if(D < 0) {
	add.w	d7,d2		;  D += ay
	sub.w	d4,d0		;  X -= sx	/* next bitnr */
	btst	#3,d0		;  /* bit 3 clear = X between 0 and 7 ? */
	beq.s	y_no_e		;  if(X & 8) {
	and.w	#7,d0		;   X &= 7
	add.w	d4,a1		;   offset += sx }
				; }
y_no_e:
	bchg.b	d0,(a1)		; 'change bit on [offset]'
	dbf	d1,yloop
	rts
