
    *** MiniStartup by Photon ***

	INCLUDE "PhotonsMiniWrapper1.04!.S"

********** Symbols **********

	INCLUDE "Blitter-Register-List.S"

********** Macros **********

WAITBLIT:MACRO
	tst (a6)			;A1000 blitwait bug fix
.wb\@:	btst #6,DMACONR(a6)
	bne.s .wb\@		;use "bne.s *-4" in incompatible assemblers
	ENDM

********** Demo **********		;Demo-specific non-startup code below.

w	=320				;screen width, height, depth
h	=238				;wave amplitudes added + bob height
bpls	=4				;handy values:
bpl	=w/16*2				;byte-width of 1 bitplane line
bwid	=bpls*bpl			;byte-width of 1 pixel line (all bpls)

MaxBobs	=11

Demo:					;a4=VBR, a6=Custom Registers Base addr
    *--- init ---*
	move.l #VBint,$6c(a4)
	move.w #$c020,$9a(a6)
	move.w #$87c0,$96(a6)
    *--- clear old bob ptrs ---*
	lea LastBobs,a0
	lea LastBobs2,a1
	moveq #MaxBobs-1,d0
.l0:	clr.l (a0)+
	clr.l (a1)+
	dbf d0,.l0
    *--- clear screens ---*
	lea Screen,a1
	bsr.w ClearScreen
	lea Screen2,a1
	bsr.w ClearScreen
	WAITBLIT
    *--- start copper ---*
	lea Screen,a0
	moveq #bpl,d0
	lea BplPtrs+2,a1
	moveq #bpls-1,d1
	bsr.w PokePtrs

	move.l #Copper,$80(a6)

********************  main loop  ********************
MainLoop:
	move.w #$12c,d0			;No buffering, so wait until raster
	bsr.w WaitRaster		;is below the Display Window.
    *--- swap buffers ---*
	movem.l DrawBuffer(PC),a2-a3
	exg a2,a3
	movem.l a2-a3,DrawBuffer	;draw into a2, show a3
    *--- show one... ---*
	move.l a3,a0
	moveq #bpl,d0
	lea BplPtrs+2,a1
	moveq #bpls-1,d1
	bsr.w PokePtrs
    *--- ...draw into the other ---*

    *--- move starts of curves ---*	;the "nose" of the snake
	move.w #1023*2,d3		;keep within curve size, used below
	lea WaveSpeeds(PC),a0
	lea WaveOffsets(PC),a1
	REPT 4
	move.w (a0)+,d0
	add.w d0,(a1)
	and.w d3,(a1)+
	ENDR
    *--- init for bob blits ---*
	bsr.w WaitBlitter
	move.l #$ffff0000,BLTAFWM(a6)	;mask extra word due to shift
	move.w #bpl-6-2,BLTCMOD(a6)	;C modulo, screen
	move.w #-2,BLTBMOD(a6)		;B modulo, mask
	move.w #-2,BLTAMOD(a6)		;A modulo, bob
	move.w #bpl-6-2,BLTDMOD(a6)	;D modulo, screen
	move.w #45*bpls*64+4,d2		;blitsize 45 high x 4 words, used below
    *--- clear bobs ---*		;the old ones left on this screen
	move.l DrawBuffer(PC),a4	;read from...
	lea -MaxBobs*4(a4),a4		;double buffered lastbob screen addr's

	REPT MaxBobs
	WAITBLIT
	move.l #$01000000,BLTCON0(a6)
	move.l (a4)+,BLTDPTH(a6)
	beq.s *+6
	move.w d2,BLTSIZE(a6)
	ENDR
    *--- preset params for DrawBob ---*
gul:
	lea Bob,a0
	move.l DrawBuffer(PC),a1
	add.l #192/2*bwid+bpl/2-4,a1	;wave center of screen
	lea Mask,a2

    *--- draw bobs ---*

	lea Sin(PC),a5			;our sine table
	movem.w WaveOffsets(PC),d4-d7	;our offset values into said table
	move.l DrawBuffer(PC),a4	;write to...
	lea -MaxBobs*4(a4),a4		;double buffered lastbob screen addr's
    *--- bob loop ---*
.bobl:
	REPT MaxBobs
	move.w (a5,d4.w),d0		;x
	move.w (a5,d5.w),d1		;y
	add.w d1,d1
	add.w (a5,d6.w),d0		;x
	add.w (a5,d7.w),d1		;y

	asr.w #1,d0			;scaled add for variation
	asr.w #2,d1			;feel free to scale with muls

	bsr.w DrawBob
	move.l a3,(a4)+			;save bob's screen addr
    *--- step to next bob ---*
	lea WaveSteps(PC),a3
	add.w (a3)+,d4
	add.w (a3)+,d5
	add.w (a3)+,d6
	add.w (a3)+,d7
	move.w #1023*2,d3		;keep within curve size, used below
	and.w d3,d4
	and.w d3,d5
	and.w d3,d6
	and.w d3,d7
	ENDR
    *--- main loop end ---*
	btst #6,$bfe001			;Left mouse button not pressed?
	bne.w MainLoop			;then loop
    *--- exit ---*
	rts

********** Demo Routines **********

DrawBob:			;a0-a2=bob/screen/mask, d0-d2=x,y, blit size
    *--- calc shift ---*
	moveq #15,d3
	and.w d0,d3
	add.w d3,d3
	add.w d3,d3
    *--- calc screen addr ---*
	asr.w #4,d0
	add.w d0,d0			;horizontal byte offset
	muls #bwid,d1			;vertical line offset, table possible
	add.w d0,d1
	lea (a1,d1.w),a3
    *--- last:poke only nec. regs ---*
	WAITBLIT
	move.l XtblCookie(PC,d3.w),BLTCON0(a6)	;shifts+minterm
	move.l a3,BLTCPTH(a6)		;C dest
	move.l a0,BLTBPTH(a6)		;B mask (movem.l possible for these 3)
	move.l a2,BLTAPTH(a6)		;A src
	move.l a3,BLTDPTH(a6)		;D dest
	move.w d2,BLTSIZE(a6)		;blit size
	rts				;a3=screen address of bob

XtblCookie:				;look up BLTCON0+1 from X-shift
	dc.l $0fca0000,$1fca1000,$2fca2000,$3fca3000
	dc.l $4fca4000,$5fca5000,$6fca6000,$7fca7000
	dc.l $8fca8000,$9fca9000,$afcaa000,$bfcab000
	dc.l $cfcac000,$dfcad000,$efcae000,$ffcaf000

PokePtrs:				;Generic, poke ptrs into copper list
.bpll:	move.l a0,d2
	swap d2
	move.w d2,(a1)			;high word of address
	move.w a0,4(a1)			;low word of address
	addq.w #8,a1			;skip two copper instructions
	add.l d0,a0			;next ptr
	dbf d1,.bpll
	rts

ClearScreen:				;a1=screen destination address to clear
	bsr.w WaitBlitter
	clr.w $66(a6)			;destination modulo
	move.l #$01000000,$40(a6)	;set operation type in BLTCON0/1
	move.l a1,$54(a6)		;destination address
	move.w #h*bpls*64+bpl/2,$58(a6)	;blitter operation size
	rts

VBint:					;Blank template VERTB interrupt
	movem.l d0/a6,-(sp)		;Save used registers
	lea $dff000,a6
	btst #5,$1f(a6)			;check if it's our vertb int.
	beq.s .notvb
    *--- do stuff here ---*
	moveq #$20,d0			;poll irq bit
	move.w d0,$9c(a6)
	move.w d0,$9c(a6)
.notvb:	movem.l (sp)+,d0/a6		;restore
	rte

********** Fastmem Data **********
    *--- double buffering base ptrs ---*

DrawBuffer:	dc.l Screen2
ViewBuffer:	dc.l Screen

WaveSpeeds:	;all numbers must be even, since the sine values are word size.
	dc.w -9*2,5*2,7*2,-11*2
WaveOffsets:
	dc.w 400*2,300*2,200*2,100*2
WaveSteps:
	dc.w 55*2,16*2,-37*2,85*2

Sin:
	INCBIN "Sine1024w.bin"		;amplitude 512 for precision
SinEnd:

Cos	=Sin+((SinEnd-Sin)/4)&$fffffffe	;quarter turn offset

*******************************************************************************
	SECTION ChipData,DATA_C		;declared data that must be in chipmem
*******************************************************************************

Copper:
	dc.w $1fc,0			;Slow fetch mode, remove if AGA demo.
	dc.w $8e,$3681			;238h display window top, left
	dc.w $90,$24c1			;and bottom, right.
	dc.w $92,$38			;Standard bitplane dma fetch start
	dc.w $94,$d0			;and stop for standard screen.

	dc.w $108,bwid-bpl		;modulos
	dc.w $10a,bwid-bpl

	dc.w $102,0			;Scroll register (and playfield pri)

Palette:				;sprightly bob colors!
	dc.w $180,$8ba,$182,$456,$184,$777,$186,$688
	dc.w $188,$a24,$18a,$977,$18c,$c46,$18e,$d68
	dc.w $190,$344,$192,$9aa,$194,$da9,$196,$e89
	dc.w $198,$ddb,$19a,$ffd,$19c,$ffe,$19e,$000

BplPtrs:
	dc.w $e0,0
	dc.w $e2,0
	dc.w $e4,0
	dc.w $e6,0
	dc.w $e8,0
	dc.w $ea,0
	dc.w $ec,0
	dc.w $ee,0
	dc.w $100,bpls*$1000+$200	;enable bitplanes

	dc.w $ffdf,$fffe		;allow VPOS>$ff
	dc.w $ffff,$fffe		;magic value to end copperlist
CopperE:

    *--- graphics ---*

Bob:
	INCBIN "amigabob"

Mask:
	INCBIN "amigabob.mask"

*******************************************************************************
	SECTION ChipBuffers,BSS_C	;BSS doesn't count toward exe size
*******************************************************************************

    *--- data for buffer 1 ---*
LastBobs:
	ds.l MaxBobs
Screen:	ds.b h*bwid			;Define storage for screen

    *--- data for buffer 2 ---*

LastBobs2:
	ds.l MaxBobs
Screen2:ds.b h*bwid			;two buffers

	END
