JUMPPTR B
;	*-------+---------------------------------------*
;	|Name	| Generic Setup Routine			|
;	|Version| 1.3	Comment:040-comp & new FillMem	|
;	+-------+---------------------------------------+
;	|						|
;	|						|
;	+-----------------------------------------------+
;	|	    Copyright © 1993 macrøcosm		|
;	*-----------------------------------------------*
INCLUDE "CO:SYMBOLS.S"
LibBits	=%00000101
;Bits 0-7=Dos,Int,Gfx,Con,Icon,MathF,MathT,MathI
;Presume that all macros destroy D0-D1/A0-A1, and preserves all other regs.
;Macros that use supplied work-registers preserve ALL registers.
;When calling a macro, A5 MUST BE INIT-ADR+32768 AND A6 MUST BE $DFF002!
;Most Macros CONTAIN GLOBAL LABELS, so use local only in-between Macro calls.
;Labels to define if relevant macros are used:
;Params (#Max longs)
;CurrHdl (current filehandle storage long)
;Buf (blk.b 81,0) for conversion & input routines
INCLUDE "CO:Makaron.S"
**************** EXT FILES ****************
**************** SYMBOLS ****************
Buffs	=1
Wid	=320
Hgt	=256
Bpls	=4
Bpl	=Wid/8				;INTERLEAVED!
Bwid	=Bpls*Bpl

ExtData =$b0000
Buff0	=$ff000-Bwid*Hgt
IF Buffs=2
Buff1	=Buff0-Bwid*Hgt
ENDC
**************** PROGRAM MACROS ****************
**************** PROGRAM START !! ****************
;SECTION SETUP1,CODE_C
B:
	INIT
	OPENLIBS
D:
;;
	bsr InitHW
	IF Buffs=2
	lea Buff1,a0
	ELSE
	lea Buff0,a0
	ENDC
	move.l #Buffs*Bwid*Hgt,d0
	bsr ClrMem			;Clr Screen(s)
	move.l #Cop0,CopPtr-R(a5)
	move.w #$c020,$9a-2(a6)

MLOOP:	move.w #$128,d0
	bsr WaitSync

	move.w #$8,$180-2(a6)
.RMB:	btst #10,$16-2(a6)
	beq.s .RMB
	btst #6,$bfe001
	bne.s MLOOP

	bsr ExitHW
	CLOSELIBS
	EXIT
	moveq #0,d0
	RTS
**************** PROGRAM ROUTINES ****************
;;
**************** SYSTEM ROUTINES ****************
;;
****************
Lev7:
	RTE
****************
Lev6:					;set copper, etc.
	MOVE.L A6,-(SP)			;R(A5) IS INVALID IN HERE !!
	addq.w #1,Frame
	lea $dff09c,a6
	move.l CopPtr(PC),$80-$9c(a6)
	move.w d0,$88-$9c(a6)		;just strobe!
	move.w #$0020,(a6)
	MOVE.L (SP)+,A6
	RTE
****************
InitHW:
	lea HWDataE-R(a5),a0
	move.w $1c-2(a6),-(a0)
	move.w (a6),-(a0)
	move.l $6c.w,-(a0)
	move.l $7c.w,-(a0)
	WAITBLIT2
	move.w #$7fff,d0
	move.w d0,$9a-2(a6)
	move.w d0,$96-2(a6)
	move.w d0,$9c-2(a6)
	move.w #$87f0,$96-2(a6)		;Set DMA!
	move.l GfxBase-R(a5),a1
	move.l $26(a1),SysCop-R(a5)
	moveq #0,d0
	bsr WaitSync
	move.l CopPtr-R(a5),$80-2(a6)	;Set COPPER!
	move.w d0,$88-2(a6)		;strobe!

	move.l #Lev6,$6c.w
	move.l #Lev7,$7c.w
	bset #1,$bfe001			;filter off
	RTS
****************
ExitHW:
	lea HWData-R(a5),a0
	WAITBLIT2
	bclr #1,$bfe001			;filter on
	move.w #$7fff,d0
	move.w d0,$9a-2(a6)
	move.w d0,$96-2(a6)
	move.w d0,$9c-2(a6)
	move.l (a0)+,$7c.w
	move.l (a0)+,$6c.w

	move.l SysCop-R(a5),$80-2(a6)	;restore cop
	move.w d0,$88-2(a6)		;strobe!

	move.w (a0)+,d0
	moveq #15,d1
	bset d1,d0
	move.w d0,$96-2(a6)
	move.w (a0)+,d0
	bset d1,d0
	move.w d0,$9a-2(a6)
	RTS
****************
PutSpr:			;put AT sprite. a0=sprdef(X.w,Y.w,H.w,Ptr.L,CopPtr.L)
	movem.w (a0)+,d0-d2
	move.l (a0)+,a1
	add.w #$38*2+16,d0
	add.w #$28,d1
	moveq #$ffffff80,d3		;(only byte used)
	cmp.w #256,d1
	blt.s .NoE8
	addq.b #4,d3
.NoE8:	move.b d1,(a1)+
	lsr.w #1,d0
	bcc.s .NoH0
	addq.b #1,d3
.NoH0:	move.b d0,(a1)+
	add.w d2,d1
	cmp.w #256,d1
	blt.s .NoL8
	addq.b #2,d3
.NoL8:	move.b d1,(a1)+
	move.b d3,(a1)+
	add.w d2,d2
	add.w d2,d2			;hgt*4=spritesize
	lea 4(a1,d2.w),a2		;skip ctrl.L (auto) & end0.L
	move.l -(a1),(a2)		;set 2nd AT-sprite ctrlword
	move.l a1,d3
	move.l (a0)+,a1			;copptr
	move.w d3,4(a1)
	swap d3
	move.w d3,(a1)
	move.l a2,d3
	move.w d3,12(a1)
	swap d3
	move.w d3,8(a1)
	RTS
****************
ClrMem: 				;Clear D0 bytes from A0.
	MOVE.W D1,-(SP)
	clr.w d1
	bsr.s FillMem
	MOVE.W (SP)+,D1
	RTS
****************
FillMem:				;Fill D0 bytes from A0 with D1-WORD
	MOVEM.L D0-D7/A1-A6,-(SP)
	move.w d1,d2
	swap d1
	move.w d2,d1			;extend word to long
	move.l a0,d2
	btst #0,d2			;odd start addr? then pad it!
	beq.s .evenA
	move.b d1,(a0)+
	subq.l #1,d0
	beq.s .Done
.evenA:	move.l d0,d2
	lsr.l #1,d2			;words
	bcc.s .evenB
	move.b d1,-1(a0,d0.l)		;odd end addr now? then pad it!
	subq.l #1,d0
	beq.s .Done
.evenB:	lsr.l #1,d2			;D2=LONGS!
	bcc.s .evenW
	move.w d1,(a0)+			;not even # of longs? then pad it!
	subq.l #2,d0
	beq.s .Done
.evenW:
.Loop:	move.l #$0000ffff,d0
	cmp.l d0,d2
	bgt.s .dobig
	move.l d2,d0
.dobig:	MOVEM.L D0/D2/A0,-(SP)
	bsr.s FillMemL
	MOVEM.L (SP)+,D0/D2/A0
	move.l d0,d3
	lsl.l #2,d3
	add.l d3,a0
	sub.l d0,d2
	bgt.s .Loop			;downchunk until <=0 bytes left
.Done:	MOVEM.L (SP)+,D0-D7/A1-A6	;A0 NOW POINTS TO STOPADDR!
	RTS
****************
FillMemL:	;fills D0 LONGS from EVEN A0 with D1.WORD-LONG !! < 256K!
	moveq #0,d2
	move.w d0,d2
	move.l d2,d0			;clr upper word (AND with $0000ffff)
	add.l d0,d0			;d0=#words
	cmp.l #$200000,a0		;DON'T BLIT TO FASTMEM(!)
	bhs.s .NoBlt
	lea (a0,d0.l),a1
	add.l d0,a1			;point to end-of-data for CPU-clear
	and.w #-64,d2			;blit-fill 64-WORD chunks of HALF size
	beq.s .NoBlt
	WAITBLIT2
	move.l #$01f00000,$40-2(a6)	;MODE=copy (no src enabled,use DATreg)
	moveq #-1,d3
	move.l d3,$44-2(a6)		;AMASK=no masking at all
	move.w d1,$74-2(a6)		;ADAT=fill-word
	move.l a0,$54-2(a6)		;DEST=a0
	clr.w $66-2(a6)			;DESTMOD=0
	move.w d2,$58-2(a6)		;BLIT! (NO DIFF IF BNASTY/BNICE)
	sub.l d2,d0			;do the other #WORDS with CPU
.NoBlt:	
	move.l d1,d2			;fill other regs with d1
	move.l d1,d3			;(for MOVEM later!)
	move.l d1,d4
	move.l d1,d5
	move.l d1,d6
	move.l d1,d7
	lsr.l #1,d0			;#LONGS again! (placement for pipeline)
	move.l d1,a0
	move.l d1,a2
	divu #14,d0			;(placement for pipeline)
	move.l d1,a3
	move.l d1,a4
	MOVEM.L D0/A5-A7,TEMP		;(placement for pipeline & regs)
	move.l d1,a5
	move.l d1,a6
	move.l d1,a7
	lsr.w #5,d0
	beq.w .NoBig
	subq.w #1,d0
;	bra.s .BigLp
;	CNOP 0,8
.BigLp:	REPT 32
	MOVEM.L D1-A0/A2-A7,-(A1)	;fill 14x16 longs
	ENDR
	dbra d0,.BigLp			;^ is looped max 73 times
.NoBig:	move.w TEMP+2(PC),d0
	and.w #31,d0
	beq.s .NoSml
	subq.w #1,d0
;	bra.s .SmlLp
;	CNOP 0,8
.SmlLp:	MOVEM.L D1-A0/A2-A7,-(A1)	;fill 14 longs
	dbra d0,.SmlLp			;^ is looped max 31 times
.NoSml:	swap d0				;result from divu above
	add.w d0,d0
	neg.w d0
	JMP NoSng(PC,d0.w)		;jump into correct offset from end
.SngLp:	REPT 13
	move.l d1,-(a1)			;fill single longword
	ENDR
NoSng:	MOVEM.L TEMP+4(PC),A5-A7
	RTS
****************
ChkKey:		;read ext. ASCII key code-->d0.0=no key!auto-fixes shift etc.
	moveq #0,d0
	lea Key-R(a5),a1
	btst #3,$1f-2(a6)
	beq.s .End
	lea $bfed01,a0
	btst #3,(a0)
	beq.s .End			;key pressed down?
	clr.b (a0)			;nec?
	moveq #0,d1
	move.b -$100(a0),d1		;read key
	move.b #$50,$100(a0)		;output+force load (start handshake)
	lea 6-2(a6),a2			;<-for spd/Size only
	move.b (a2),d2			;wait for 75 microsecs=60 or so cycles
	not.b d1
	lsr.b #1,d1
	bcc.s .Pres
	moveq #-1,d0			;neg=released!
.Pres:	move.w Shift-R(a5),d3
	bpl.s .Shift
	move.w Caps-R(a5),d3
	bmi.s .NoSh
.Shift:	add.w #KeyTblS-KeyTbl,d1
.NoSh:	move.b KeyTbl-Key(a1,d1.w),d0	;fetch ASCII equivalent
	moveq #0,d1
	move.b d0,d1
	bpl.s .WLup1			;special key is neg!
	addq.b #5,d1			;less than -5?
	bmi.s .WLup1			;then no special key
	add.w d1,d1
	move.w d0,2(a1,d1.w)		;put state in corr. key-slot (-=OFF!)
.WLup1:	cmp.b (a2),d2			;QikFix:wait AT LEAST 1 scanline
	beq.s .WLup1
	move.b (a2),d2			;make this into an int later!
.WLup2:	cmp.b (a2),d2
	beq.s .WLup2
	move.b #$10,$100(a0)		;input+force load (handshake done)
	move.w #8,$9c-2(a6)		;clear lev2-intreq
.End:	move.w d0,(a1)
	RTS				;d0/[key]=Ascii key
****************
WaitSync:			;wait for a scanline 0-311 in d0.
	cmp.w #256,d0
	bge.s .Hi
.Lo:	cmp.b 6-2(a6),d0
	bne.s .Lo
	btst #0,5-2(a6)
	bne.s .Lo
	RTS
.Hi:	cmp.b 6-2(a6),d0
	bne.s .Hi
	btst #0,5-2(a6)
	beq.s .Hi
	RTS
****************
ShadeRGB: ;shade from a0 to a1-->a2.d0=step0-32*1024+512,d1=dstmod,d2=nrcols-1
	MOVEM.L D0-D1/A0,-(SP)		;NO NEG. MODULO !!
	cmp.w #32768,d0			;full intensity?
	blo.s .NFull
	moveq #3,d7
.Copy:	movem.w (a1)+,d0-d6/a0
	move.w d0,(a2)
	move.w d1,4(a2)
	move.w d2,8(a2)
	move.w d3,12(a2)
	move.w d4,16(a2)
	move.w d5,20(a2)
	move.w d6,24(a2)
	move.w a0,28(a2)
	lea 32(a2),a2
	dbra d7,.Copy
	bra.s .End
.NFull:	move.w d1,a3
	move.w #$f0,d1			;AND-const
	moveq #31,d2
.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
IF Buffs=2
****************
DblBuf:					;switch coppers and set Buff-Addr.
	moveq #0,d0
	lea Frame-R(a5),a0
	lea Buff1,a1
	lea Cop0,a2
	btst d0,1(a0)
	beq.s .Even
	lea Buff0,a1
	lea Cop1,a2
.Even:	addq.w #1,(a0)+
	movem.l a1-a2,(a0)
	RTS
ENDC
**************** DC-DATA ****************
EVEN
**************** VARS ****************
EVEN
;;
HWData:	blk.w 6,0
HWDataE:
SysCop:	dc.l 0
Frame:	dc.w 0			;if even, then use Cop0, otherwise Cop1
Buff:	dc.l Buff0		;buffer to draw into(not shown).
CopPtr:	dc.l Cop		;Shown-Buff Copper.DON'T SEPARATE^^
Black:	blk.w 32,0
White:	blk.w 32,$fff
DBug:	blk.l 8,0
Temp:	blk.l 8,0		;TEMP DATA AT STACKLEVEL 1 (ONE!) ONLY!
Key:	dc.w 0
Caps:	dc.w -1
Amiga:	dc.w -1				;special keys state
Alt:	dc.w -1				;-=OFF(!)
Shift:	dc.w -1
Ctrl:	dc.w -1				;DONT SEP ^5!
KeyTbl:					;clr unused keybytes for final opti.
dc.b "`1234567890-=\",0,"0"
dc.b "qwertyuiop[]",0,"123"
dc.b "asdfghjkl;'",0,0,"456"
dc.b 0,"zxcvbnm,./",0,".789"
dc.b " ",8,9,13,13,27,127,0,0,0,"-",0,31,30,29,28	;28-31=cursor keys
dc.b -7,-8,-9,-10,-11,-12,-13,-14,-15,-16,"()/*+",-6	;F-keys,Help
dc.b -2,-2,-5,-1,-3,-3,-4,-4				;Shift,Ctrl,Alt,<A>
dc.b 128,129,130,131,132,133,134,135
dc.b 136,137,138,139,140,141,142,143,144,145,146,147,148,149,150,151
KeyTblS:				;SHIFTED
dc.b "~!@#$%^&*()_+|",0,"0"
dc.b "QWERTYUIOP{}",0,"123"
dc.b "ASDFGHJKL:",34,0,0,"456"
dc.b 0,"ZXCVBNM<>?",0,".789"
dc.b " ",8,9,13,13,27,127,0,0,0,"-",0,31,30,29,28	;28-31=cursor keys
dc.b -7,-8,-9,-10,-11,-12,-13,-14,-15,-16,"()/*+",-6	;F-keys,Help
dc.b -2,-2,-5,-1,-3,-3,-4,-4				;Shift,Ctrl,Alt,<A>
dc.b 128,129,130,131,132,133,134,135
dc.b 136,137,138,139,140,141,142,143,144,145,146,147,148,149,150,151
EVEN
E:
SECTION SETUP2,DATA_C
**************** EMPTY COPPER ****************
Cop:
dc.w $100,$0200
dc.w $180,0
dc.l -2
**************** COPPER 0 ****************
EVEN
Cop0:
dc.w $08e,$2881
dc.w $090,$28c1
dc.w $092,$38
dc.w $094,$d0
dc.w $102,0
dc.w $104,36
dc.w $108,BWid-40
dc.w $10a,BWid-40
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,$0ff,$192,$000,$194,$000,$196,$000
dc.w $198,$f0f,$19a,$000,$19c,$000,$19e,$000
dc.w $1a0,$fff,$1a2,$f52,$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,$fff
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
IF Buffs=2
**************** COPPER 1 ****************
EVEN
Cop1:
dc.w $08e,$2881
dc.w $090,$28c1
dc.w $092,$38
dc.w $094,$d0
dc.w $102,0
dc.w $104,36
dc.w $108,BWid-40
dc.w $10a,BWid-40
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,$0ff,$192,$000,$194,$000,$196,$000
dc.w $198,$f0f,$19a,$000,$19c,$000,$19e,$000
dc.w $1a0,$fff,$1a2,$f52,$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,$fff
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
ENDC
END
