; Vintage Computing Christmas Challenge 2023
; https://logiker.com/Vintage-Computing-Christmas-Challenge-2023
; Adam Sampson, for the Commodore Amiga 500 as a floppy bootblock

	SECTION "code",CODE

; There will be 12 bytes at the start of the bootblock:
;bootblock:
;	dc.b "DOS",0	; ID
;	dc.l 0		; checksum (computed)
;	dc.l 880	; rootblock

; Custom chip registers
custom  EQU $dff000
cop1lch EQU $080
cop1lcl EQU $082
copjmp1 EQU $088
diwstrt EQU $08e
diwstop EQU $090
ddfstrt EQU $092
ddfstop EQU $094
dmacon  EQU $096
intena  EQU $09a
bpl1pth EQU $0e0
bpl1ptl EQU $0e2
bpl2pth EQU $0e4
bpl2ptl EQU $0e6
bplcon0 EQU $100
bplcon1 EQU $102
bpl1mod EQU $108
bpl2mod EQU $10a
color00 EQU $180
color01 EQU $182
color09 EQU $192

start:	lea custom, a7

	move.w #%111000111000111, d7	; flip direction every 3 lines
	move.w d7, intena(a7)	; disable interrupts (clear INTEN)

	move.l #$30000, a1	; clear bitplane data
	moveq #51, d0		; 26 chars * 8 lines / 4 = 52
cloop:	clr.l (a1)+
	dbra d0, cloop

	sub.w #$b0, a1		; back to $30020
	lea (copout-1,pc), a2	; draw "      *     *     *      "
	moveq #4, d0		; 5 lines for each star
sloop:	move.b (a2)+, (a1)
	move.b (a1), 6(a1)
	move.b (a1), 12(a1)
	adda.w #26, a1
	dbra d0, sloop

	lea (copout,pc), a2	; generate the rest of the copperlist
	move.w #$6001, d1
	moveq #$0003, d2
	moveq #$0000, d4

	moveq #18, d6
lloop:	move.w d1, (a2)+	; wait for line
	add.w #$0800, d1
	move.w #$ff00, (a2)+

	move.w #bpl1ptl, (a2)+	; playfield 1 bitplane
	move.w d2, (a2)+
	move.w #bpl2ptl, (a2)+	; playfield 2 bitplane
	move.w #6, (a2)		; opposite offset from playfield 1
	sub.w d2, (a2)+

	move.w #bplcon1, (a2)+	; bitplane scroll delays
	move.w d4, (a2)+
	eor.w #$0088, d4	; offset both by 1 char on alternate lines

	lsr.l #1, d7		; move left or right
	bcc.b right
	subq #2, d2
right:	addq #1, d2
	dbra d6, lloop		; d6 is -1 after this

	move.w d6, (a2)+	; end of copperlist
	move.w #$fffe, (a2)+

	lea (cop,pc), a0	; set the copperlist address
	move.l a0, cop1lch(a7)
	move.w copjmp1(a7), d0
	move.w #$8380, dmacon(a7)	; enable bitplane and copper DMA

hang:	bra.b hang		; and now the copper will do the work

cop:	dc.w bplcon0, $2600	; dual playfield, low res, 1 bp each
	dc.w bpl1pth, $0003	; playfield 1, bitplane at $30000
	dc.w bpl1mod, $0000
	dc.w bpl2pth, $0003	; playfield 2, bitplane at $30000
	dc.w bpl2mod, $0000

	dc.w diwstrt, $60d9	; 152x152 window, centred for PAL
	dc.w diwstop, $f871
	dc.w ddfstrt, $0060	; HWM suggests $64/$c4, but that doesn't work
	dc.w ddfstop, $00c0	; on real OCS or AGA

	dc.w color01, $fff	; playfield 1 colour
	dc.w color09, $cff	; playfield 2 colour
	dc.w color00, $066	; background colour - $66 is first byte of star
copout:	dc.b %00111100		; rest of star - will be overwritten
	dc.b %11111111
	dc.b %00111100
	dc.b %01100110
