	Section	Demo,Code_C

;	Opt	C-,O+,w-
;	opt	d+

size1	= 80
size2	= 130
size3	= 70

speed1	= 6
speed2	= 8
speed3	= 10
speed4	= 12

aa	bsr	Start
bb	rts

Start:	bsr	rndinit

	lea	$dff000,a5		; hardware base address

	Move.w	$1c(a5),OldInt	; Save Old Interupts
	Move.w	$02(a5),OldDma	; Save Old DMA

	Move.w	#$7fff,$9a(a5)	; Clear DMA
	Move.w	#$7fff,$96(a5)	; Clear Interupts
	Move.w	#$7fff,$9c(a5)	; Clear Interupt Requests
	
	Move.b	#$7f,$bfed01	; kill timers (rem me for disk)
	Move.l	$6c.w,OldV3		; save level 3 int

	Move.l	#my_level3,$6c.w	; put new copper interupt
	Move.l	#my_copper,$80(a5) 	; Address of copper 1
	Move.w	#$c010,$9a(a5)	; Start interupts

	Move.w	#$83ef,$96(a5)	; Start DMA ( 83ff for disk dma)
	Move.w	#1,$88(a5)		; Strobe for copper start

	move.l	#screena,d0
	move.w	d0,sb_plane0_lo
	swap	d0
	move.w	d0,sb_plane0_hi
	swap	d0
	
	move.l	#screenb,d0
	move.w	d0,sb_plane1_lo
	swap	d0
	move.w	d0,sb_plane1_hi
	swap	d0
	
	move.l	#screenc,d0
	move.w	d0,sb_plane2_lo
	swap	d0
	move.w	d0,sb_plane2_hi
	swap	d0
	
	move.l	#screend,d0
	move.w	d0,sb_plane3_lo
	swap	d0
	move.w	d0,sb_plane3_hi
	swap	d0
	
	move.l	#screene,d0
	move.w	d0,sb_plane4_lo
	swap	d0
	move.w	d0,sb_plane4_hi
	swap	d0

wait:	bsr	sb_setup
	btst	#6,$bfe001
	bne.s	wait		; wait for lmb

	lea	$dff000,a5		; hardware base address
	move.l	#0,$80(a5)		; blank copper list
	move.l	#0,$84(a5)		; if gfx lib not open (slayer boot)
	move.w	#0,$180(a5)		; bgc to black
	
	Move.l	OldV3,$6c.w		; restore old l3
	
	Lea	GfxLib,a1		; Pointer to Library Text
	move.l	4.w,a6
	jsr	-132(a6)		; forbid
	Move.l	4.w,a6		; Exec
	Moveq.l	#0,d0		; Clear D0
	Jsr	-$228(a6)		; Open Library
	cmp.l	#0,d0
	beq	no_gfx_lib		; if gfx lib not open dont restore copper lib (slayer boot)
	
	Move.l	d0,a1			
	Move.l	$26(a1),$80(a5)	; restore copper 1
	Move.l	$32(a1),$84(a5)	; restore copper 2
no_gfx_lib:

	Move.w	OldInt,d0		; start old interupts
	Or.w	#$8000,d0
	Move.w	d0,$9a(a5)
	Move.w	OldDma,d0		; start old DMA
	Or.w	#$8000,d0
	Move.w	d0,$96(a5)
	Move.b	#$9b,$bfed01	; Start Timers
	Move.l	4.w,a6		; Close Gfx lib
	Jsr	-$19e(a6)
	move.l	4.w,a6
	jsr	-138(a6)		; permit

	move.w	#0,$bfec00		; clear kbd

	Moveq.l	#0,d0		; No errors
	Rts			; Exit


my_level3:	Movem.l	d0-d7/a0-a6,-(a7)	; New copper
	Move.w	#$10,$dff09c	; Serviced Interupt

	btst	#10,$dff016
	bne	exit

	lea	shade_map,a0
	move.w	2(a0),d7
	move.w	#31,d0
.cycle	move.w	6(a0),2(a0)
	add.l	#4,a0
	dbf	d0,.cycle
	move.w	d7,-2(a0)

Exit:	Movem.l	(a7)+,d0-d7/a0-a6
	Rte			; Return from Interupt

	*************************************************

	include	"src:shadebobs/rnd.i"

	*************************************************

sb_setup:	move.w	#2,x1step
	move.w	#2,y1step

	move.w	#speed1,d0
	bsr	rnd
	asl.w	#1,d1
	move.w	d1,x2step
	move.w	#speed2,d0
	bsr	rnd
	asl.w	#1,d1
	move.w	d1,y2step

	move.w	#speed3,d0
	bsr	rnd
	asl.w	#1,d1
	move.w	d1,x3step
	move.w	#speed4,d0
	bsr	rnd
	asl.w	#1,d1
	move.w	d1,y3step

	bsr	sb_cls
	
	move.w	#9,d0
	bsr	rnd
	asl.w	#6,d1
	lea	shade_colours,a0
	ext.l	d1
	add.l	d1,a0
	lea	shade_map-4,a1
	move.w	#31,d0
.col	move.w	(a0)+,2(a1)
	add.l	#4,a1
	dbf	d0,.col

.loop	Move.l	$dff004,d2
	And.l	#$0001ff00,d2	
	Cmp.l	#$00001000,d2
	Bne.s	.loop

.sinok	bsr	sb_do

	btst	#6,$bfe001
	beq	.exit

	cmp.w	#512,lcount
	blt	.loop

	add.l	#4,brush_ptr
	move.l	brush_ptr,a0
	cmp.l	#-1,(a0)
	bne	.exit
	move.l	#brushlist,brush_ptr
	
.exit	rts

	*************************************************
	
sb_cls:	Move.l	#screena,a0

.wait1	Btst	#14,$dff002
	Bne	.wait1
	
	Move.l	#$1f00000,$dff040
	Move.l	a0,$dff054
	move.l	#0,$dff074
	move.l	#0,$dff066
	Move	#256*64+20,$dff058
	
	Move.l	#screenb,a0

.wait2	Btst	#14,$dff002
	Bne	.wait2
	
	Move.l	#$1f00000,$dff040
	Move.l	a0,$dff054
	move.l	#0,$dff074
	move.l	#0,$dff066
	Move	#256*64+20,$dff058

	Move.l	#screenc,a0

.wait3	Btst	#14,$dff002
	Bne	.wait3
	
	Move.l	#$1f00000,$dff040
	Move.l	a0,$dff054
	move.l	#0,$dff074
	move.l	#0,$dff066
	Move	#256*64+20,$dff058

	Move.l	#screend,a0

.wait4	Btst	#14,$dff002
	Bne	.wait4
	
	Move.l	#$1f00000,$dff040
	Move.l	a0,$dff054
	move.l	#0,$dff074
	move.l	#0,$dff066
	Move	#256*64+20,$dff058

	Move.l	#screene,a0

.wait5	Btst	#14,$dff002
	Bne	.wait5
	
	Move.l	#$1f00000,$dff040
	Move.l	a0,$dff054
	move.l	#0,$dff074
	move.l	#0,$dff066
	Move	#256*64+20,$dff058
	
	move.w	#0,lcount
	
	rts
	
	***********************************************************
		
sb_do:	add.w	#1,lcount

	Lea	$dff000,a5

.wait	Btst	#14,$dff002
	Bne	.wait

	lea	sintable,a3
	lea	128(a3),a4

	move.w	sinepos,d4
	move.w	cosinepos,d5

	move.w	(a3,d4.w),d0
	move.w	(a4,d5.w),d1
	muls	#size1,d0
	muls	#size1,d1
	swap	d0
	swap	d1

	move.w	sinepos2,d4
	move.w	cosinepos2,d5

	move.w	(a3,d4.w),d2
	move.w	(a4,d5.w),d3
	muls	#size2,d2
	muls	#size3,d3
	swap	d2
	swap	d3
	add.w	d2,d0
	add.w	d3,d1

	move.w	sinepos3,d4
	move.w	cosinepos3,d5

	move.w	(a3,d4.w),d2
	move.w	(a4,d5.w),d3
	muls	#size2,d2
	muls	#size3,d3
	swap	d2
	swap	d3
	add.w	d2,d0
	add.w	d3,d1

	movem.l	d0-d1,-(a7)
	add.w	#160-16,d0
	add.w	#128-16,d1
	bsr	plot_bob
	movem.l	(a7)+,d0-d1

	movem.l	d0-d1,-(a7)
	not.w	d1
	add.w	#160-16,d0
	add.w	#128-16,d1
	bsr	plot_bob
	movem.l	(a7)+,d0-d1
	
	move.w	x2step,d1
	add.w	d1,sinepos2
	and.w	#$3fe,sinepos2
	move.w	y2step,d1
	add.w	d1,cosinepos2
	and.w	#$3fe,cosinepos2
	
	move.w	x3step,d1
	add.w	d1,sinepos3
	and.w	#$3fe,sinepos3
	move.w	y3step,d1
	add.w	d1,cosinepos3
	and.w	#$3fe,cosinepos3
	
	rts

	*************************************************

plot_bob:	movem.l	d0-d7/a0-a6,-(a7)

	lea	screena,a0
	lea	screenb,a1
	lea	screenc,a2
	lea	screend,a3
	lea	screene,a4

	move.l	brush_ptr,a6
	move.l	(a6),a6

	move.w	d0,d5
	and.w	#15,d5
	lsr.w	#3,d0
	ext.l	d0
	add.l	d0,a0
	add.l	d0,a1
	add.l	d0,a2
	add.l	d0,a3
	add.l	d0,a4
	
	mulu	#40,d1
	ext.l	d1
	add.l	d1,a0
	add.l	d1,a1
	add.l	d1,a2
	add.l	d1,a3
	add.l	d1,a4
	
	mulu	#$1000,d5
	or.w	#$b5a,d5

.wait	Btst	#14,$dff002
	Bne	.wait

	; plane 1

	Move.l	a0,$dff054		; dest mem d
	Move.l	a0,$dff048		; dest mem c
	Move.l	a6,$dff050		; source mem a
	Move.w	#$ffff,$dff044	; blit 1st word mask source a
	move.w	#$ffff,$dff046	; blit last word mask source a
	move.w	d5,$dff040
	move.w	#0,$dff064		; source for a
	move.w	#0,$dff042		; blit cont reg 1
	Move.w	#34,$dff066		; modulo for d
	Move.w	#34,$dff060		; modulo for c
	Move.w	#(31*64)+3,$dff058	; blit height,width

	and.w	#$f000,d5
	or.w	#$0f9a,d5

.wait2	Btst	#14,$dff002
	Bne	.wait2

	; plane 2

	move.l	a6,$dff050		; source mem a
	move.l	a0,$dff04c		; dest mem b
	move.l	a1,$dff048		; dest mem c
	move.l	a1,$dff054		; dest mem d
	move.w	#$ffff,$dff044	; blit 1st word mask source a
	move.w	#$ffff,$dff046	; blit last word mask source a
	move.w	d5,$dff040		; blit cont reg 0
	move.w	#0,$dff042		; blit cont reg 1
	move.w	#00,$dff064		; modulo for a
	move.w	#34,$dff062		; modulo for b
	move.w	#34,$dff066		; modulo for d
	move.w	#34,$dff060		; modulo for c
	Move.w	#(31*64)+3,$dff058	; blit height,width

	and.w	#$f000,d5
	or.w	#$0f10,d5

.wait3	Btst	#14,$dff002
	Bne	.wait3
	
	; temp smear 1
	

	move.l	a6,$dff050		; source mem a
	move.l	a0,$dff04c		; dest mem b
	move.l	a1,$dff048		; dest mem c
	move.l	#tempsmear1,$dff054	; dest mem d
	move.w	#$ffff,$dff044	; blit 1st word mask source a
	move.w	#$ffff,$dff046	; blit last word mask source a
	move.w	d5,$dff040		; blit cont reg 0
	move.w	#0,$dff042		; blit cont reg 1
	move.w	#00,$dff064		; modulo for a
	move.w	#34,$dff062		; modulo for b
	move.w	#34,$dff060		; modulo for c
	move.w	#0,$dff066		; modulo for d
	Move.w	#(31*64)+3,$dff058	; blit height,width

.wait4	Btst	#14,$dff002
	Bne	.wait4

	; plane 3

	move.l	#tempsmear1,$dff050	; source mem a
	move.l	a2,$dff04c		; dest mem b
	move.l	a2,$dff054		; dest mem d
	move.w	#$ffff,$dff044	; blit 1st word mask source a
	move.w	#$ffff,$dff046	; blit last word mask source a
	move.w	#$d3c,$dff040	; blit cont reg 0
	move.w	#0,$dff042		; blit cont reg 1
	move.w	#00,$dff064		; modulo for a
	move.w	#34,$dff062		; modulo for b
	move.w	#34,$dff066		; modulo for d
	Move.w	#(31*64)+3,$dff058	; blit height,width

.wait5	Btst	#14,$dff002
	Bne	.wait5

	; plane 4

	move.l	#tempsmear1,$dff050	; source mem a
	move.l	a2,$dff04c		; source mem b
	move.l	a3,$dff048		; source mem c
	move.l	a3,$dff054		; dest mem d
	move.w	#$ffff,$dff044	; blit 1st word mask source a
	move.w	#$ffff,$dff046	; blit last word mask source a
	move.w	#$f9a,$dff040	; blit cont reg 0
	move.w	#0,$dff042		; blit cont reg 1
	move.w	#00,$dff064		; modulo for a
	move.w	#34,$dff062		; modulo for b
	move.w	#34,$dff060		; modulo for c
	move.w	#34,$dff066		; modulo for d
	Move.w	#(31*64)+3,$dff058	; blit height,width

.wait6	Btst	#14,$dff002
	Bne	.wait6

	; temp smear 2

	move.l	#tempsmear1,$dff050	; source mem a
	move.l	a2,$dff04c		; source mem b
	move.l	a3,$dff048		; source mem c
	move.l	#tempsmear2,$dff054	; dest mem d
	move.w	#$ffff,$dff044	; blit 1st word mask source a
	move.w	#$ffff,$dff046	; blit last word mask source a
	move.w	#$f10,$dff040	; blit cont reg 0
	move.w	#0,$dff042		; blit cont reg 1
	move.w	#0,$dff064		; modulo for a
	move.w	#34,$dff062		; modulo for b
	move.w	#34,$dff060		; modulo for c
	move.w	#0,$dff066		; modulo for d
	Move.w	#(31*64)+3,$dff058	; blit height,width

.wait7	Btst	#14,$dff002
	Bne	.wait7

	; plane 5

	move.l	#tempsmear2,$dff050	; source mem a
	move.l	a4,$dff04c		; dest mem b
	move.l	a4,$dff054		; dest mem d
	move.w	#$ffff,$dff044	; blit 1st word mask source a
	move.w	#$ffff,$dff046	; blit last word mask source a
	move.w	#$d3c,$dff040	; blit cont reg 0
	move.w	#0,$dff042		; blit cont reg 1
	move.w	#00,$dff064		; modulo for a
	move.w	#34,$dff062		; modulo for b
	move.w	#34,$dff066		; modulo for d
	Move.w	#(31*64)+3,$dff058	; blit height,width

.wait8	Btst	#14,$dff002
	Bne	.wait8

	movem.l	(a7)+,d0-d7/a0-a6
	rts

	*************************************************

sinepos:	dc.w	0
cosinepos:	dc.w	0
sinepos2:	dc.w	0
cosinepos2:	dc.w	0
sinepos3:	dc.w	0
cosinepos3:	dc.w	0

x1step:	dc.w	10
y1step:	dc.w	80
x2step:	dc.w	10
y2step:	dc.w	80
x3step:	dc.w	10
y3step:	dc.w	80

sinoffset:	dc.w	0


lcount:	dc.w	0
		incdir	 src:shadebobs/
sintable:		incbin	"sin.3fe"
		incbin	"sin.3fe"
		incbin	"sin.3fe"
		incbin	"sin.3fe"


shade_colours:	dc.w	$000,$00f,$11f,$22f,$33f,$44f,$55f,$66f,$77f,$88f,$99f,$aaf,$bbf,$ccf,$ddf,$eef,$fff,$fee,$fdd,$fcc,$fbb,$faa,$f99,$f88,$f77,$f66,$f55,$f44,$f33,$f22,$f11,$f00
		dc.w	$000,$002,$004,$006,$008,$00a,$00c,$00e,$f00,$f0f,$e0e,$c0c,$a0a,$808,$606,$404,$202,$400,$600,$800,$a00,$c00,$e00,$f00,$ff0,$ee0,$cc0,$a00,$880,$660,$440,$220
		dc.w	$000,$200,$400,$600,$800,$a00,$c00,$e00,$f00,$ff0,$ee0,$cc0,$a00,$880,$660,$440,$220,$004,$006,$008,$00a,$00c,$00e,$f00,$f0f,$e0e,$c0c,$a0a,$808,$606,$404,$202
		dc.w	$000,$f00,$f11,$f22,$f33,$f44,$f55,$f66,$f77,$f88,$f99,$faa,$fbb,$fcc,$fdd,$fee,$fff,$eef,$ddf,$ccf,$bbf,$aaf,$99f,$88f,$77f,$66f,$55f,$44f,$33f,$22f,$11f,$00f
		dc.w	$000,$00f,$11f,$22f,$33f,$44f,$55f,$66f,$77f,$88f,$99f,$aaf,$bbf,$ccf,$ddf,$eef,$fff,$fee,$fdd,$fcc,$fbb,$faa,$f99,$f88,$f77,$f66,$f55,$f44,$f33,$f22,$f11,$f00
		dc.w	$000,$002,$004,$006,$008,$00a,$00c,$00e,$f00,$f0f,$e0e,$c0c,$a0a,$808,$606,$404,$202,$400,$600,$800,$a00,$c00,$e00,$f00,$ff0,$ee0,$cc0,$a00,$880,$660,$440,$220
		dc.w	$000,$200,$400,$600,$800,$a00,$c00,$e00,$f00,$ff0,$ee0,$cc0,$a00,$880,$660,$440,$220,$004,$006,$008,$00a,$00c,$00e,$f00,$f0f,$e0e,$c0c,$a0a,$808,$606,$404,$202
		dc.w	$000,$f00,$f11,$f22,$f33,$f44,$f55,$f66,$f77,$f88,$f99,$faa,$fbb,$fcc,$fdd,$fee,$fff,$eef,$ddf,$ccf,$bbf,$aaf,$99f,$88f,$77f,$66f,$55f,$44f,$33f,$22f,$11f,$00f
		dc.w	$000,$00f,$11f,$12f,$22f,$23f,$33f,$43f,$44f,$45f,$55f,$65f,$66f,$76f,$77f,$78f,$88f,$98f,$99f,$9af,$aaf,$baf,$bbf,$bcf,$ccf,$dcf,$ddf,$def,$eef,$fef,$fff,$fff
		
	*************************************************
	
my_copper:		dc.w	$100,$5200	; 4 bp - lo res
		dc.w	$104,$a	; bp control reg
		dc.w	$108,0	; bp modulo - odd
		dc.w	$10a,0	; bp modulo - even
		dc.w	$180,0	; colour 0 to black
		dc.w	$102,0
		
		dc.w	$08e,$2c81	; bp window start left
		dc.w	$090,$2cc1	; bp window bot right
		dc.w	$094,$d0	; bp stop horz
		dc.w	$92,$38

		dc.w	$120,0,$122,0
		dc.w	$124,0,$126,0
		dc.w	$128,0,$12a,0
		dc.w	$12c,0,$12e,0
		dc.w	$130,0,$132,0
		dc.w	$134,0,$136,0
		dc.w	$138,0,$13a,0
		dc.w	$13c,0,$13e,0
		

		dc.w	  $e0
sb_plane0_hi:	dc.w	0,$e2
sb_plane0_lo:	dc.w	0,$e4
sb_plane1_hi:	dc.w	0,$e6
sb_plane1_lo:	dc.w	0,$e8
sb_plane2_hi:	dc.w	0,$ea
sb_plane2_lo:	dc.w	0,$ec
sb_plane3_hi:	dc.w	0,$ee
sb_plane3_lo:	dc.w	0,$f0
sb_plane4_hi:	dc.w	0,$f2
sb_plane4_lo:	dc.w	0

		
		dc.w	$180,$000
shade_map:		dc.w	$182,$11f
		dc.w	$184,$22f
		dc.w	$186,$33f
		dc.w	$188,$44f
		dc.w	$18a,$55f
		dc.w	$18c,$66f
		dc.w	$18e,$77f
		dc.w	$190,$88f
		dc.w	$192,$99f
		dc.w	$194,$aaf
		dc.w	$196,$bbf
		dc.w	$198,$ccf
		dc.w	$19a,$ddf
		dc.w	$19c,$eef
		dc.w	$19e,$fff
		dc.w	$1a0,$fee
		dc.w	$1a2,$fdd
		dc.w	$1a4,$fcc
		dc.w	$1a6,$fbb
		dc.w	$1a8,$faa
		dc.w	$1aa,$f99
		dc.w	$1ac,$f88
		dc.w	$1ae,$f77
		dc.w	$1b0,$f66
		dc.w	$1b2,$f55
		dc.w	$1b4,$f44
		dc.w	$1b6,$f33
		dc.w	$1b8,$f22
		dc.w	$1ba,$f11
		dc.w	$1bc,$f00
		dc.w	$1be,$f00
		
		dc.w	$ffe1,$fffe	; end of ntsc screen
		dc.w	$9c,$8010	; irq set bits - restart copper
		dc.w	$3001,$ff00	; end of pal screen
		dc.w	$ffff,$fffe	; end of copper list

tempsmear1:		ds.l	384
tempsmear2:		ds.l	384

oldint:		dc.l	0
olddma:		dc.l	0
oldv3:		dc.l	0
gfxlib:		dc.b	"graphics.library",0
		even

brushlist:		dc.l	brush1
		dc.l	brush2
		dc.l	brush3
		dc.l	brush4
		dc.l	brush3
		dc.l	brush4
		dc.l	-1

brush_ptr:		dc.l	brushlist

brush1:		incbin	"shadeb1.bin"
brush2:		incbin	"shadeb2.bin"
brush3:		incbin	"shadeb3.bin"
brush4:		incbin	"shadeb6.bin"


screena:		ds.l	10*256
screenb:		ds.l	10*256
screenc:		ds.l	10*256
screend:		ds.l	10*256
screene:		ds.l	10*256


