
    *** 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	=256
bpls	=1				;handy values:
bpl	=w/16*2				;byte-width of 1 bitplane line
bwid	=bpls*bpl			;byte-width of 1 pixel line (all bpls)

fonth	=25				;height of the scroll buffer
paddingh=16				;pad to allow 45 degree slopes
fontbpl	=320/8

WaveSpeed=8				;movement of ptr across sine values

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 screens ---*
	lea Screen,a1
	bsr.w ClearScreen
	lea Screen2,a1
	bsr.w ClearScreen
    *--- scale up 3 sine waves ---*	;by 1 screen line
	lea Sin(PC),a0
	move.w #(SinEnd-Sin)/2-1,d0
.l0:	move.w (a0),d1
	muls #bwid,d1
	move.w d1,(a0)+
	dbf d0,.l0	

    *--- start copper ---*
	lea Screen,a0
	moveq #bpl,d0
	lea BplPtrs+2,a1
	moveq #bpls-1,d1
	bsr.w PokePtrs

	bsr.s WaitBlitter		;wait out last clearscreen blit before
	move.l #Copper,$80(a6)		;...showing it.

********************  MAIN LOOP  ********************
MainLoop:
.pause:
	move.w #$12c,d0			;No buffering, so wait until raster
	bsr.w WaitRaster		;is below the Display Window.

	btst #2,$16(a6)			;right mouse button=pause
	beq.s .pause
    *--- 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 sine pointer ---*
	move.l SineP(PC),a4		;ampl 115, period 720, * bwid
	addq.l #WaveSpeed,a4		;increase sine pointer
	cmp.l #Sin1Period,a4
	blo.s .nowrap
	lea -(Sin1Period-Sin)(a4),a4	;wrap it
.nowrap:
	move.l a4,SineP
    *--- init for Sine ---*
	bsr.w WaitBlitter
	move.w #$ffff,BLTALWM(a6)	;for 1-word blits, masks are ANDed
	move.w #bpl-2,BLTBMOD(a6)	;B modulo, screen
	move.w #fontbpl-2,BLTAMOD(a6)	;A modulo, scrollbuf
	move.w #bpl-2,BLTDMOD(a6)	;D modulo, screen
	clr.w BLTCON1(a6)		;BLTCON0 varies, this doesn't.

	lea ScrollBuffer+paddingh*fontbpl,a0
	lea 115*bwid(a2),a2		;dest addr ptr (increasing in x)
	move.w #fonth*64+1,d4		;normal blit size

    *--- sine scroller loop ---*

	moveq #w/16-1,d7
.wordl:
	lea SliceMasks(PC),a5		;easy table to save 16 x ror.w in-loop

	lea -paddingh*fontbpl(a0),a3
	lea -paddingh*bwid(a2),a1	;copy an extra high slice=clear around.
	add.w (a4)+,a1
	WAITBLIT
	move.w #$09f0,BLTCON0(a6)	;first pixel slice of 16:copy operation
	move.w (a5)+,BLTAFWM(a6)	;slice mask
	move.l a3,BLTAPTH(a6)		;ScrollBuffer source
	move.l a1,BLTDPTH(a6)		;Screen destination
	move.w #(fonth+paddingh*2)*64+1,BLTSIZE(a6)

	WAITBLIT
	move.w #$0dfc,BLTCON0(a6)	;then, OR

	REPT 15				;...15 slices to the screen.

	move.l a2,a1
	add.w (a4)+,a1
	WAITBLIT
	move.w (a5)+,BLTAFWM(a6)
	move.l a1,BLTBPTH(a6)		;Screen source for OR
	move.l a0,BLTAPTH(a6)
	move.l a1,BLTDPTH(a6)
	move.w d4,BLTSIZE(a6)

	ENDR

	addq.w #2,a0
	addq.w #2,a2
	dbf d7,.wordl

	btst #6,$bfe001			;Left mouse button not pressed?
	bne.w MainLoop			;then loop
    *--- exit ---*
	rts

SliceMasks:				;single bits being shifted rightward.
	dc.w $8000,$4000,$2000,$1000
	dc.w $800,$400,$200,$100
	dc.w $80,$40,$20,$10
	dc.w $8,$4,$2,$1

********** Demo Routines **********
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 **********
SineP:	dc.l Sin

    *--- double buffering base ptrs ---*

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

Sin:
	INCBIN "Sine720w.bin"		;amplitude 115, period 720
Sin1Period:				;(pointer wraps here)
	INCBIN "Sine720w.bin"		;repeated twice=no in-loop wrap check
SinEnd:

*******************************************************************************
	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,$2c81			;238h display window top, left
	dc.w $90,$2cc1			;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:
	dc.w $180,$300			;color01 set below

BplPtrs:
	dc.w $e0,0
	dc.w $e2,0
	dc.w $100,bpls*$1000+$200	;enable bitplanes
    *--- a coppershade for style ---*
	dc.w $2c07,$fffe
	dc.w $182,$f11
	dc.w $3407,$fffe
	dc.w $182,$f22
	dc.w $3c07,$fffe
	dc.w $182,$f33
	dc.w $4407,$fffe
	dc.w $182,$f44
	dc.w $4c07,$fffe
	dc.w $182,$f55
	dc.w $5407,$fffe
	dc.w $182,$f66
	dc.w $5c07,$fffe
	dc.w $182,$f77
	dc.w $6407,$fffe
	dc.w $182,$f88
	dc.w $6c07,$fffe
	dc.w $182,$f99
	dc.w $7407,$fffe
	dc.w $182,$faa
	dc.w $7c07,$fffe
	dc.w $182,$fbb
	dc.w $8407,$fffe
	dc.w $182,$fcc
	dc.w $8c07,$fffe
	dc.w $182,$fdd
	dc.w $9407,$fffe
	dc.w $182,$fee
	dc.w $9c07,$fffe
	dc.w $182,$fff
	dc.w $a407,$fffe
	dc.w $182,$fee
	dc.w $ac07,$fffe
	dc.w $182,$fdd
	dc.w $b407,$fffe
	dc.w $182,$fcc
	dc.w $bc07,$fffe
	dc.w $182,$fbb
	dc.w $c407,$fffe
	dc.w $182,$faa
	dc.w $cc07,$fffe
	dc.w $182,$f99
	dc.w $d407,$fffe
	dc.w $182,$f88
	dc.w $dc07,$fffe
	dc.w $182,$f77
	dc.w $e407,$fffe
	dc.w $182,$f66
	dc.w $ec07,$fffe
	dc.w $182,$f55
	dc.w $f407,$fffe
	dc.w $182,$f44
	dc.w $fc07,$fffe
	dc.w $182,$f33

	dc.w $ffdf,$fffe		;allow VPOS>$ff
	
	dc.w $0407,$fffe
	dc.w $182,$f22
	dc.w $0c07,$fffe
	dc.w $182,$f11
	dc.w $1407,$fffe
	dc.w $182,$f00
	dc.w $1c07,$fffe
	dc.w $182,$e00
	dc.w $2407,$fffe
	dc.w $182,$d00

	dc.w $ffff,$fffe		;magic value to end copperlist
CopperE:

    *--- graphics ---*

ScrollBuffer:	dcb.b paddingh*fontbpl,0
		INCBIN "skruvfont.1bpl.secret"	;pf, always these secrets...
		dcb.b paddingh*fontbpl,0

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

    *--- data for buffer 1 ---*
	ds.b paddingh*bwid		;required to not touch, not even clear,
					;memory outside allocated space.
Screen:	ds.b h*bwid			;Define storage for screen
	ds.b paddingh*bwid		;not really required since we just
					;clear where there's no pixels anyway.
					;Kept for correctness.
    *--- data for buffer 2 ---*
Screen2:ds.b h*bwid			;two buffers
	ds.b paddingh*bwid		;required to not touch, not even clear,
					;memory outside allocated space.
	END
