*cxmin:	equ	16
*cxmax:	equ	303
*cymin:	equ	32
*cymax:	equ	96

*****************************************************************
*	initline						*
*	call before any line drawing to initialise routine	*
*****************************************************************
initline:
	move.w	#4,-(sp)
	trap	#14
	addq.l	#2,sp
	lea	params,a0
	asl.w	#2,d0
	move.l	(a0,d0.w),a0
	lea	lineset,a1
	move.l	(a0)+,(a1)+
	move.l	(a0)+,(a1)+
	move.l	(a0)+,(a1)+
	move.w	(a0)+,(a1)+
	rts

*****************************************************************
*	Line draw routine works in all ST and TT modes		*
*	set cxmin etc to clipping coords, line color in 	*
*	linecolor, and x1,y1,x2,y2, address of hiden in hiden	*
*	call INITLINE to setup routine				*
*****************************************************************
*/**//*/*//*//*/*//*//*/*/*//*/*/*/*//*/*/*/*//*//*//*///*/*/
bitcode:clr.l   d4
        cmpi.w  #cxmin,d0
        bge     b1
        or.b    #8,d4
b1:     cmpi.w  #cxmax,d0
        ble     b2
        or.b    #4,d4
b2:     cmpi.w  #cymin,d1
        bge     b3
        or.b    #1,d4
b3:     cmpi.w  #cymax,d1
        ble     b4
        or.b    #2,d4
b4:     move.w  d4,cbits
        rts
*****
bitc2:   clr.l   d5
        cmpi.w  #cxmin,d2
        bge     b5
        or.b    #8,d5
b5:     cmpi.w  #cxmax,d2
        ble     b6
        or.b    #4,d5
b6:     cmpi.w  #cymin,d3
        bge     b7
        or.b    #1,d5
b7:     cmpi.w  #cymax,d3
        ble     b8
        or.b    #2,d5
b8:     move.w  d5,cbits2
        rts
********************************************************
clipit:
	movem.l	d0-d7/a0,-(sp)
	move.w	x1,d0
	move.w	y1,d1
	move.w	x2,d2
	move.w	y2,d3

	bsr     bitcode
        bsr     bitc2
clipit2:or.b    d4,d5
        tst.b   d5
        beq     cliphome        ;finish if
        move.w  cbits,d4
        move.w  d2,xsav
        move.w  d3,ysav
        move.w  cbits2,d5
clip1:  and.b   d4,d5
        tst.b   d5
        bgt     clipb           ;home if line completely out
        move.w  cbits,d4
        move.w  cbits2,d5
        sub.w   d0,d2           ;d1 has dx
        move.w  d2,dx
        sub.w   d1,d3           ;d3 has dy
        move.w  d3,dy
        move.w  xsav,d2
        move.w  ysav,d3
        tst.w   d4
        beq     clip2   	;if x1<xmin
        cmpi.w  #cxmin,d0 	;y1=y1+dy*(xmin-x1)/dx
        bge     c1      
        move.w  #cxmin,d7         ;put xmin in d7
        sub.w   d0,d7
        muls    dy,d7           ;multiply by dy
        divs    dx,d7
        add.w   d1,d7
        move.w  d7,d1
        move.w  #cxmin,d0         ;x1=xmin
        bra     c4
c1:     cmpi.w  #cxmax,d0         ;if x1>xmax
        ble     c2              ;y1=y1+dy*(xmax-x1)/dx
        move.w  #cxmax,d7         ;put xmax in d7
        sub.w   d0,d7
        muls    dy,d7           ;multiply by dy
        divs    dx,d7
        add.w   d1,d7
        move.w  d7,d1
        move.w  #cxmax,d0         ;x1=xmax
        bra     c4
c2:     cmpi.w  #cymin,d1         ;if y1<ymin
        bge     c3
        move.w  #cymin,d7         ;put ymin in d7
        sub.w   d1,d7
        muls    dx,d7           ;multiply by dx
        divs    dy,d7
        add.w   d0,d7
        move.w  d7,d0
        move.w  #cymin,d1         ;y1=ymin
        bra     c4
c3:     cmpi.w  #cymax,d1         ;if y1>xmax
        ble     c4
        move.w  #cymax,d7         ;put ymax in d7
        sub.w   d1,d7
        muls    dx,d7           ;multiply by dy
        divs    dy,d7
        add.w   d0,d7
        move.w  d7,d0
        move.w  #cymax,d1         ;y1=ymax
c4:     bsr     bitcode
        bra     cliploop
******
clip2:  cmpi.w  #cxmin,d2 ;	y1=y1+dy*(xmin-x1)/dx
        bge     cc1     
        move.w  #cxmin,d7         ;put xmin in d7
        sub.w   d2,d7
        muls    dy,d7           ;multiply by dy
        divs    dx,d7
        add.w   d3,d7
        move.w  d7,d3
        move.w  #cxmin,d2         ;x2=xmin
        bra     cc4
cc1:    cmpi.w  #cxmax,d2         ;if x2>xmax
        ble     cc2
        move.w  #cxmax,d7         ;put xmax in d7
        sub.w   d2,d7
        muls    dy,d7           ;multiply by dy
        divs    dx,d7
        add.w   d3,d7
        move.w  d7,d3
        move.w  #cxmax,d2         ;x1=xmax
        bra     cc4
cc2:    cmpi.w  #cymin,d3         ;if y1<ymin
        bge     cc3
        move.w  #cymin,d7         ;put ymin in d7
        sub.w   d3,d7
        muls    dx,d7           ;multiply by dx
        divs    dy,d7
        add.w   d2,d7
        move.w  d7,d2
        move.w  #cymin,d3         ;y1=ymin
        bra     cc4
cc3:    cmpi.w  #cymax,d3         ;if y1>xmax
        ble     cc4
        move.w  #cymax,d7         ;put ymax in d7
        sub.w   d3,d7
        muls    dx,d7           ;multiply by dy
        divs    dy,d7
        add.w   d2,d7
        move.w  d7,d2
        move.w  #cymax,d3         ;y1=ymax
cc4:    bsr     bitc2
        bra     cliploop

cliploop:
        bra     clipit2
cliphome:
        move.w  d0,x1
        move.w  d1,y1
        move.w  d2,x2
        move.w  d3,y2
	bsr	myline
	movem.l	(sp)+,d0-d7/a0
	rts
clipb:
	movem.l	(sp)+,d0-d7/a0
	rts
*********************************************************
linecolor:
	dc.w	0
***********************************************
*	lets plot a fucking line
*	but lets do it fast
*	first some conventions
*	draws line on hiden hiden
*	between x1,y1,x2,y2 
***********************************************
myline:
*	movem.l	d4-d7/a0,-(sp)
calcdx:
	move.w	x2,d0
	sub.w	x1,d0	;x2-x1
	move.w	d0,dx	;d0 is dx
	bpl	calcdy
	neg	d0
calcdy:
	move.w	y2,d1
	sub.w	y1,d1	;y2-y1	
	move.w	d1,dy	;d6 is dy
	bpl	cmparedxdy
	neg	d1
cmparedxdy:
	cmp.w	d0,d1
	bgt	ymajor	;if dy is greater then ymajor
xmajor:
*	tst.w	dy
*	beq	horizontal	;special case
	tst.w	dx
	bpl	noswapx
	move.w	x2,d0
	move.w	x1,x2
	move.w	d0,x1
	move.w	y2,d0
	move.w	y1,y2
	move.w	d0,y1
	neg	dx
	neg	dy
noswapx:
	moveq	#0,d0
	move.w	dx,d0
	divs	dy,d0
	bmi	xpositive

xnegative:
	move.l	hiden,a0	;hiden ad
	move.w	dx,d0
	move.w	d0,d7	;counter is d7
	subq.w	#1,d7
	asr.w	#1,d0	;div by 2
	move.w	d0,d6	;dump is d6
	move.w	x1,d0	;will hold bitpattern
	move.w	d0,d2
	move.w	y1,d1
	and.l	#$0000ffff,d1
	mulu	bytesperline+2,d1
	adda.l	d1,a0		;a0 now has start hiden address
	and.l	#$0000fff0,d0	;mask off last 4 bits gives word across
	move.l	xshift,d1
	lsr.l	d1,d0
	add.l	d0,a0
	moveq	#0,d4
	tst.w	d7
	bmi	specxneg
	move.w	d2,d3
	and.w	#15,d3
	bne	xmnloop
	sub.l	bytestonext16,a0
xmnloop:
	move.w	d2,d3
	and.w	#15,d3
	bne	xmnla
	bsr	putlinein
	moveq	#0,d4
	add.l	bytestonext16,a0
xmnla:	sub.w	#15,d3
	neg	d3
	bset	d3,d4
	sub.w	dy,d6	;sub dy from dump
	bpl	xmn
	add.w	dx,d6
	bsr	putlinein
	adda.l	bytesperline,a0		;moveit to next line
	moveq	#0,d4
xmn:
	addq.w	#1,d2
	dbf	d7,xmnloop
specxneg:
	move.w	d2,d3
	and.w	#15,d3
	bne	spct1
	rts
spct1:	sub.w	#15,d3
	neg	d3
	bset	d3,d4
	bsr	putlinein
	rts

putlinein:
	move.w	d4,d5
	eor.w	#$ffff,d5
	move.w	planes,planes2
	move.l	a0,-(sp)
cloop:
	and.w	d5,(a0)+
	subq.w	#1,planes2
	bgt	cloop
	move.l	(sp)+,a0
	move.w	linecolor,d5
plane1:
	btst	#0,d5
	beq	plane2
	or.w	d4,(a0)
plane2:
	btst	#1,d5
	beq	plane3
	or.w	d4,2(a0)
plane3:
	btst	#2,d5
	beq	plane4
	or.w	d4,4(a0)
plane4:
	btst	#3,d5
	beq	plane5
	or.w	d4,6(a0)
plane5:
	btst	#4,d5
	beq	plane6
	or.w	d4,8(a0)
plane6:
	btst	#5,d5
	beq	plane7
	or.w	d4,10(a0)
plane7:
	btst	#6,d5
	beq	plane8
	or.w	d4,12(a0)
plane8:
	btst	#7,d5
	beq	plane9
	or.w	d4,14(a0)
plane9:
	rts

xpositive:
	move.l	hiden,a0	;hiden ad
	move.w	dx,d0
	move.w	d0,d7	;counter is d7
	subq.w	#1,d7
	asr.w	#1,d0	;div by 2
	move.w	d0,d6	;dump is d6
	move.w	x1,d0	;will hold bitpattern
	move.w	d0,d2
	move.w	y1,d1
	and.l	#$0000ffff,d1
	mulu	bytesperline+2,d1
	add.l	d1,a0	;a0 now has start hiden address
	and.l	#$0000fff0,d0	;mask off last 4 bits gives word across
	move.l	xshift,d1
	lsr.l	d1,d0
	add.l	d0,a0
	moveq	#0,d4
	move.w	d2,d3
	and.w	#15,d3
	bne	xmploop
	sub.l	bytestonext16,a0
xmploop:
	move.w	d2,d3
	and.w	#15,d3
	bne	xmpla
	bsr	putlinein
	moveq	#0,d4
	add.l	bytestonext16,a0
xmpla:	sub.w	#15,d3
	neg	d3
	bset	d3,d4
	add.w	dy,d6	;sub dy from dump
	bpl	xmp
	add.w	dx,d6
	bsr	putlinein
	moveq	#0,d4
	suba.l	bytesperline,a0		;moveit to next line
xmp:
	addq.w	#1,d2
	dbf	d7,xmploop
	move.w	d2,d3
	and.w	#15,d3
	bne	spct2
	rts
spct2:	sub.w	#15,d3
	neg	d3
	bset	d3,d4
	bsr	putlinein
	rts

ymajor:
	tst.w	dy
	bpl	noswapy
	move.w	x2,d0
	move.w	x1,x2
	move.w	d0,x1
	move.w	y2,d0
	move.w	y1,y2
	move.w	d0,y1
	neg	dx
	neg	dy
noswapy:
	moveq	#0,d0
	move.w	dy,d0
	divs	dx,d0
	bmi	ypositive
ynegative:
	tst.w	dx
	beq	astraightup1	;special case vertical line
	move.l	hiden,a0	;hiden ad
	move.w	dy,d0
	move.w	d0,d7	;counter is d7
*	subq.w	#1,d7
	asr.w	#1,d0	;div by 2
	move.w	d0,d6	;dump is d6
	move.w	x1,d0	;will hold bitpattern
	move.w	d0,d3
	move.w	y1,d1

*	move.w	#220,d1
	move.w	d1,d2
	and.l	#$0000ffff,d1
	mulu	bytesperline+2,d1
*	movem.l	d0-d7/a0-a6,-(sp)
*	moveq	#0,d0
*	move.l	d1,d0
*	moveq	#16,d1
*	moveq	#0,d2
*	moveq	#0,d3
*	bsr	prtnum
*	movem.l	(sp)+,d0-d7/a0-a6

	add.l	d1,a0		;a0 now has start hiden address
	and.l	#$0000fff0,d0	;mask off last 4 bits gives word across
	move.l	xshift,d1
	lsr.l	d1,d0
	add.l	d0,a0
	move.w	d3,d0	;x
	moveq	#0,d4
	move.w	d0,d3
	and.w	#15,d3
	move.w	#$8000,d4
	lsr.w	d3,d4
ymnloop:
	bsr	putlinein

	add.l	bytesperline,a0		;y+1
	sub.w	dx,d6		;sub dy from dump
	bpl	ymn
	add.w	dy,d6
	addq.w	#1,d0		;x+1
	move.w	d0,d3
	and.w	#15,d3
	bne	ymnla
	add.l	bytestonext16,a0
ymnla:	move.w	#$8000,d4
	lsr.w	d3,d4
ymn:
	dbf	d7,ymnloop
	rts

ypositive:
	tst.w	dx
	beq	straightup	;special case vertical line
	move.l	hiden,a0	;hiden ad
	move.w	dy,d0
	move.w	d0,d7	;counter is d7
	subq.w	#1,d7
	asr.w	#1,d0	;div by 2
	move.w	d0,d6	;dump is d6
	move.w	x1,d0	;will hold bitpattern
	move.w	d0,d3
	move.w	y1,d1
	move.w	d1,d2
	and.l	#$0000ffff,d1
	mulu	bytesperline+2,d1
	add.l	d1,a0	;a0 now has start hiden address
	and.l	#$0000fff0,d0	;mask off last 4 bits gives word across
	move.l	xshift,d1
	lsr.l	d1,d0
	add.l	d0,a0
	move.w	d3,d0	;x
	moveq	#0,d4
	and.w	#15,d3
	move.w	#$8000,d4
	lsr.w	d3,d4
ymploop:
	bsr	putlinein
	add.l	bytesperline,a0	;y+1
	add.w	dx,d6	;sub dy from dump
	bpl	ymp
	add.w	dy,d6
	subq.w	#1,d0	;x+1
	move.w	d0,d3
	and.w	#15,d3
	cmpi.w	#15,d3
	bne	ympla
	sub.l	bytestonext16,a0
ympla:	move.w	#$8000,d4
	lsr.w	d3,d4
ymp:
	dbf	d7,ymploop
	rts

straightup:
	move.l	hiden,a0	;hiden ad
	move.w	dy,d7
	move.w	x1,d0	;will hold bitpattern
	move.w	d0,d3
	move.w	y1,d1
	mulu	bytesperline+2,d1
	add.l	d1,a0	;a0 now has start hiden address
	and.l	#$0000fff0,d0	;mask off last 4 bits gives word across
	move.l	xshift,d1
	lsr.l	d1,d0
	add.l	d0,a0	;now a0 is ready
	moveq	#0,d4
	and.w	#15,d3
	move.w	#$8000,d4
	lsr.w	d3,d4	;d4 has proper bit pattern
	add.l	bytesperline,a0
	addq.w	#1,d7
stuploop:
	bsr	putlinein
	add.l	bytesperline,a0
	dbf	d7,stuploop
	rts	

astraightup1:
	move.l	hiden,a0	;hiden ad
	move.w	dy,d7
	move.w	x1,d0	;will hold bitpattern
	move.w	d0,d3
	move.w	y1,d1
	mulu	bytesperline+2,d1
	add.l	d1,a0	;a0 now has start hiden address
	and.l	#$0000fff0,d0	;mask off last 4 bits gives word across
	move.l	xshift,d1
	lsr.l	d1,d0
	add.l	d0,a0	;now a0 is ready
	moveq	#0,d4
	and.w	#15,d3
	move.w	#$8000,d4
	lsr.w	d3,d4	;d4 has proper bit pattern
	add.l	bytesperline,a0
	addq.w	#1,d7
astuploop1:
	bsr	putlinein
	sub.l	bytesperline,a0
	dbf	d7,stuploop
	rts	


cbits:		dc.w	0
cbits2:		dc.w	0
xsav:		dc.w	0
ysav:		dc.w	0
dump:		dc.w	0
gradient:	dc.w	0
x1:		dc.w	0
x2:		dc.w	0
y1:		dc.w	0
y2:		dc.w	0
dx:		dc.w	0
dy:		dc.w	0
hiden:		dc.l	0
lineset:
bytesperline:	dc.l	320
xshift:		dc.l	1
bytestonext16:	dc.l	8
planes:		dc.w	4
planes2:	dc.w	0

params:		dc.l	stlowres,stmedres,sthires
		dc.l	0,ttmedres,0,0,ttlowres

stlowres:	dc.l	160,1,8
		dc.w	4
stmedres:	dc.l	160,2,4
		dc.w	2
sthires:	dc.l	80,3,2
		dc.w	1
ttlowres:	dc.l	320,0,16
		dc.w	8
ttmedres:	dc.l	320,1,8
		dc.w	4
*tthires:	dc.l	


