;	*-------+---------------------------------------*
;	|Name	| Processor 1-pxl/line linedraw		|
;	|Version| 0.1	Comment:			|
;	+-------+---------------------------------------+
;	|						|
;	|						|
;	+-----------------------------------------------+
;	|  Copyright (C) 1992 Henrik /MAX/ Erlandsson!	|
;	*-----------------------------------------------*
**************** EXT FILES ****************
**************** SYMBOLS ****************
Wid=320
Hgt=256
Bpls=5
Bpl=Wid/8				;INTERLEAVED!
Bwid=Bpls*Bpl
Buff1=$7f800-Bwid*Hgt			;leave space for SSP
Buff0=Buff1-Bwid*Hgt
**************** MACROS ****************
WaitBlit2:MACRO				;a6=$dff002!
	btst #14,(a6)
	bne.s *-4
	ENDM

SpecWBlit:MACRO				;a6=$dff002,DReg \1=#14!
	btst \1,(a6)
	bne.s *-2
	ENDM

WaitSync:MACRO				;wait until raster beam=\1
	cmp.b \1,6-2(a6)
	bne.s *-6
	btst #0,5-2(a6)
	bne.s *-14
	ENDM

WaitSync1:MACRO				;wait until raster beam=$100+\1
	cmp.b \1,6-2(a6)
	bne.s *-6
	btst #0,5-2(a6)
	beq.s *-14
	ENDM
**************** PROGRAM ****************
JUMPPTR B
ORG  $20000
LOAD $20000
B:
	MOVEM.L D1-A6,-(SP)
	LEA $DFF002,A6
	WAITBLIT2
	move.w $1c-2(a6),-(SP)
	move.w (a6),-(SP)
	move.l $6c.w,-(SP)
	move.w #$7fff,$9c-2(a6)
	move.w #$7fff,$9a-2(a6)
	move.w #$7fff,$96-2(a6)

	move.l #Vblank,$6c.w
	move.w #$c020,$9a-2(a6)		;start Vbl
	WAITSYNC #1			;wait until custom copper started
	move.w #$87f0,$96-2(a6)
	bset #1,$bfe001			;filter off

INIT:	lea Buff0,a0
	move.l #2*Bwid*Hgt,d0
	moveq #0,d1
	bsr FillMem			;Clear Screen buffers
;;
.Loop:	lea LineDef(PC),a0
	bsr LineDraw
	lea LineDef(PC),a0
	cmp.w #319,4(a0)
	beq.s .nope
	addq.w #1,4(a0)
	bra.s .Loop
.nope:

EXIT:	btst #6,$bfe001
	bne.s EXIT
	WAITBLIT2
	bclr #1,$bfe001
	move.w #$7fff,$9c-2(a6)
	move.w #$7fff,$9a-2(a6)
	move.w #$7fff,$96-2(a6)
	move.l (SP)+,$6c.w

	move.l a6,a5
	move.l 4.w,a6
	lea GfxName(PC),a1
	jsr -408(a6)
	move.l d0,a1
	move.l $26(a1),$80-2(a5)
	move.w #0,$88-2(a5)		;restore cop
	jsr -414(a6)

	move.w (SP)+,d0
	bset #15,d0
	move.w d0,$96-2(a5)
	move.w (SP)+,d0
	bset #15,d0
	move.w d0,$9a-2(a5)
	MOVEM.L (SP)+,D1-A6
	MOVEQ #0,D0
	RTS
**************** PROGRAM ROUTINES ****************
;;
LineDraw:		;draw line.a0=coord-def
.LLoop:	lea Buff1,a1
	moveq #0,d6				;const
	movem.w (a0),d0-d3			;x,y,x2,y2
	move.w d3,d5
	sub.w d1,d5
	move.w d2,d4
	sub.w d0,d4
	cmp.w d5,d4				;if DX>DY, then horiz-scan!
	bgt.s .Horiz
	tst.w d5
	bpl.s .Down				;make sure line is drawn down!
	exg d0,d2
	exg d1,d3
.Down:	sub.w d0,d2
	sub.w d1,d3
	ext.l d2
	divs d3,d2
	move.w d2,d4
	clr.w d2
	move.w d3,d5
	divu d3,d2
	swap d2
	move.w d4,d2
	swap d0
	clr.w d0
	mulu #BWid,d1
	add.l d1,a1
	subq.w #1,d5
.VLoop:	move.w d0,d1
	move.w d1,d3
	asr.w #3,d1
	not.w d3
	bset d3,(a1,d1.w)
	add.l d2,d0
	addx.w d6,d0
	lea BWid(a1),a1
	dbra d5,.VLoop
	rts

.Horiz:	tst.w d4
	bpl.s .Rgt			;make sure line is drawn L to R!
	exg d0,d2
	exg d1,d3
.Rgt:	sub.w d0,d2
	move.w #BWid,d5
	sub.w d1,d3
	bpl.s .Pos3
	neg.w d3
	neg.w d5
.Pos3:	swap d3
	clr.w d3
	divu d2,d3			;DY/DX=vertical stepspeed.w
	ext.l d2
	mulu #BWid,d1
	move.w d0,d4
	lsr.w #3,d4
	ext.l d4
	add.l d4,d1
	add.l d1,a1			;origin
	not.w d0
	and.w #7,d0			;start bit#!
	subq.w #1,d2			;DX-1=ctr
.HLoop:	bset d0,(a1)
	dbra d0,.NoWr
	moveq #7,d0
	addq.w #1,a1
.NoWr:	add.w d3,d6
	bcc.s .NoWr2
	add.w d5,a1
.NoWr2:	dbra d2,.HLoop
	RTS
LineDef:
dc.w 0,0,0,100
**************** SYSTEM ROUTINES ****************
VBlank:					;set copper, etc.
	MOVE.L A6,-(SP)
	lea $dff09c,a6
	move.l CopPtr(PC),$80-$9c(a6)
	move.w #0,$88-$9c(a6)
	move.w #$0020,(a6)
	MOVE.L (SP)+,A6
	RTE

DblBuf:					;switch coppers and set Buff-Addr.
	moveq #0,d0
	lea Frame(PC),a0
	lea Buff1,a1
	lea Cop0(PC),a2
	btst d0,1(a0)
	beq.s .Even
	lea Buff0,a1
	lea Cop1(PC),a2
.Even:	addq.w #1,(a0)+
	movem.l a1-a2,(a0)
	RTS

ShadeRGB: ;shade from a0 to a1-->a2.d0=step0-32*1024+512,d1=dstmod,d2=nrcols-1
	MOVEM.L D0-D1/A0,-(SP)
	move.w d1,a3			;NO NEG. MODULOS!!!!
	move.w #$f0,d1			;AND-const
	cmp.w #32768,d0			;full intensity?
	blo.s .Loop
.Copy:	move.w (a1)+,(a2)+		;get src2-col->d4
	add.w a3,a2
	dbf d2,.Copy
	bra.s .End
.Loop:	move.w (a0)+,d3			;get src1-col->d3
	move.w (a1)+,d4			;get src2-col->d4
	move.w d3,d5			;R
	clr.b d5
	move.w d4,d7
	clr.b d7
	sub.w d5,d7
	add.w d7,d7
	muls d0,d7
	swap d7
	add.w d5,d7
	move.w d3,d5			;G
	and.w d1,d5
	move.w d4,d6
	and.w d1,d6
	sub.w d5,d6
	add.w d6,d6
	muls d0,d6
	swap d6
	add.w d5,d6
	and.w d1,d6
	move.b d6,d7
	moveq #$f,d6
	and.w d6,d3
	and.w d6,d4
	sub.w d3,d4			;B
	add.w d4,d4
	muls d0,d4
	swap d4
	add.w d3,d4
	or.w d4,d7
	move.w d7,(a2)+
	add.w a3,a2
	dbf d2,.Loop
.End:	MOVEM.L (SP)+,D0-D1/A0
	RTS

FillMem:	;Fill ANY Memory.a0=addr,d0=Nr of bytes,d1=Fill-LWORD!.
	MOVEM.L D0/A0/A4-A7,HEAP	;uses a7.
	move.l a0,d2
	btst #0,d2			;odd start-address?
	beq.s .EvenA
	move.b d1,(a0)+
	subq.l #1,d0
	ble.w .End
.EvenA:	btst #0,d0			;odd byte-length now?
	beq.s .EvenL
	move.b d1,-1(a0,d0.L)		;then fill that byte!
	subq.l #1,d0
.EvenL:	move.l d0,d2
.BankLp:moveq #4,d0
	swap d0				;=$40000=256K
	sub.l d0,d2			;<=256K left?
	bgt.s .Nope
	add.l d2,d0
	ble.w .End
.Nope:	movem.l d2/a0,Heap+24
	moveq #0,d2
	cmp.l #$200000,a0		;ChipMem address?
	bge.s .NoBlt			;if not, then DON'T BLIT(!)
	move.l d0,d2
	lsr.l #8,d2			;>>1>>7 (half by 128 bytes=64 words)
	beq.s .NoBlt
	lsl.l #6,d2
	lea $dff002,a6			;because all regs destr:d below!
	WAITBLIT2
	move.l a0,$54-2(a6)
	move.w d1,$74-2(a6)		;Fill with d1-value
	clr.w $66-2(a6)
	move.l #$01f00000,$40-2(a6)
	move.w d2,$58-2(a6)
	add.l d2,d2
.NoBlt:	add.l d0,a0
	sub.l d2,d0
	move.l d1,d2
	move.l d1,d3
	move.l d1,d4
	move.l d1,d5
	move.l d1,d6
	move.l d1,d7
	move.l d1,a1
	move.l d1,a2
	move.l d1,a3
	move.l d1,a4
	move.l d1,a5
	move.l d1,a6
	move.l d1,a7
	divu #14*4,d0
	move.w d0,Heap+32
	lsr.w #4,d0
	subq.w #1,d0
	bmi.s .No16
.Lp16:	REPT 16
	movem.l d1-d7/a1-a7,-(a0)
	ENDR
	dbf d0,.Lp16
.No16:	move.w Heap+32(PC),d0
	and.w #15,d0
	subq.w #1,d0
	bmi.s .No1
.Lp1:	movem.l d1-d7/a1-a7,-(a0)
	dbf d0,.Lp1
.No1:	swap d0
	lsr.w #1,d0
	subq.w #1,d0
	bmi.s .NoWord
.WordLp:move.w d1,-(a0)			;faster than clr!
	dbf d0,.WordLp
.NoWord:movem.l Heap+24(PC),d2/a0
	add.l #$40000,a0		;add 256K. If last, a0 not important!
	bra.w .BankLp
.End:	MOVEM.L HEAP(PC),D0/A0/A4-A7	;D0/D1/A0 intact. Uses 34b Heap
	RTS
**************** COPPER 0 ****************
EVEN
Cop0:
dc.w $180,$0
dc.w $08e,$2881
dc.w $090,$28c1
dc.w $092,$38
dc.w $094,$d0
dc.w $102,0
dc.w $104,0
dc.w $108,(Bpls-1)*Bpl
dc.w $10a,(Bpls-1)*Bpl
Spr0:
dc.w $120,0,$122,0,$124,0,$126,0,$128,0,$12a,0,$12c,0,$12e,0
dc.w $130,0,$132,0,$134,0,$136,0,$138,0,$13a,0,$13c,0,$13e,0
Pal0:
dc.w $180,$000,$182,$f00,$184,$0f0,$186,$00f
dc.w $188,$ff0,$18a,$000,$18c,$000,$18e,$000
dc.w $190,$000,$192,$000,$194,$000,$196,$000
dc.w $198,$000,$19a,$000,$19c,$000,$19e,$000
dc.w $1a0,$000,$1a2,$000,$1a4,$000,$1a6,$000
dc.w $1a8,$000,$1aa,$000,$1ac,$000,$1ae,$000
dc.w $1b0,$000,$1b2,$000,$1b4,$000,$1b6,$000
dc.w $1b8,$000,$1ba,$000,$1bc,$000,$1be,$000
Bpl0:
dc.w $0e0,Buff0/65536
dc.w $0e2,Buff0&65535
dc.w $0e4,(Buff0+Bpl)/65536
dc.w $0e6,(Buff0+Bpl)&65535
dc.w $0e8,(Buff0+2*Bpl)/65536
dc.w $0ea,(Buff0+2*Bpl)&65535
dc.w $0ec,(Buff0+3*Bpl)/65536
dc.w $0ee,(Buff0+3*Bpl)&65535
dc.w $0f0,(Buff0+4*Bpl)/65536
dc.w $0f2,(Buff0+4*Bpl)&65535
Ena0:
dc.w $100,bpls*$1000+$200
dc.l -2
**************** COPPER 1 ****************
Cop1:
dc.w $180,$0
dc.w $08e,$2881
dc.w $090,$28c1
dc.w $092,$38
dc.w $094,$d0
dc.w $102,0
dc.w $104,0
dc.w $108,(Bpls-1)*Bpl
dc.w $10a,(Bpls-1)*Bpl
Spr1:
dc.w $120,0,$122,0,$124,0,$126,0,$128,0,$12a,0,$12c,0,$12e,0
dc.w $130,0,$132,0,$134,0,$136,0,$138,0,$13a,0,$13c,0,$13e,0
Pal1:
dc.w $180,$000,$182,$f00,$184,$0f0,$186,$00f
dc.w $188,$ff0,$18a,$000,$18c,$000,$18e,$000
dc.w $190,$000,$192,$000,$194,$000,$196,$000
dc.w $198,$000,$19a,$000,$19c,$000,$19e,$000
dc.w $1a0,$000,$1a2,$000,$1a4,$000,$1a6,$000
dc.w $1a8,$000,$1aa,$000,$1ac,$000,$1ae,$000
dc.w $1b0,$000,$1b2,$000,$1b4,$000,$1b6,$000
dc.w $1b8,$000,$1ba,$000,$1bc,$000,$1be,$000
Bpl1:
dc.w $0e0,Buff1/65536
dc.w $0e2,Buff1&65535
dc.w $0e4,(Buff1+Bpl)/65536
dc.w $0e6,(Buff1+Bpl)&65535
dc.w $0e8,(Buff1+2*Bpl)/65536
dc.w $0ea,(Buff1+2*Bpl)&65535
dc.w $0ec,(Buff1+3*Bpl)/65536
dc.w $0ee,(Buff1+3*Bpl)&65535
dc.w $0f0,(Buff1+4*Bpl)/65536
dc.w $0f2,(Buff1+4*Bpl)&65535
Ena1:
dc.w $100,bpls*$1000+$200
dc.l -2
**************** DC-DATA ****************
EVEN
;;
**************** VARS ****************
EVEN
;;
Frame:	dc.w 0			;if even, then use Cop0, otherwise Cop1
Buff:	dc.l Buff0		;buffer to draw into(not shown).
CopPtr:	dc.l Cop1		;Shown-Buff Copper.DON'T SEPARATE^^

Black:	blk.w 32,0
White:	blk.w 32,$fff
Heap:	blk.w 17,0
Temp:	blk.l 8,0
DBug:	blk.l 8,0
GfxName:dc.b "graphics.library",0
EVEN
E:
