/*
16 bars boot disc based on 510b+ from nyd2022
*/

;	@com.wudsn.ide.asm.hardware=ATARI8BIT
;	@com.wudsn.ide.asm.outputfileextension=.atr

	opt l-
;---- ATARI addresses
hposp0	=	$D000
hposp1	=	$D001
hposp2	=	$D002
hposp3	=	$D003
sizep0	=	$D008
sizep1	=	$D009
sizep2	=	$D00A
sizep3	=	$D00B
trig0	=	$D010
trig1	=	$D011
colpm0	=	$D012
colpm1	=	$D013
colpm2	=	$D014
colpm3	=	$D015
colbak	=	$D01A
color2	=	$D018
gtictl	=	$D01B
pmcntl	=	$D01D
consol	=	$D01F
skctl	=	$D20F
dmactl	=	$D400
pmbase	=	$D407
wsync	=	$D40A
nmien	=	$D40E
;---
	opt l+

compileto	equ $2000
sectors	equ ((fin-compileto)/128)+1

	opt f+				;Activate fill mode
	opt h-				;no headers
;;; ATR disk header
        .word $0296, $8, $80
:10	.he 00
	org compileto
;--------------------------- boot init
	.proc boot
	.he 00		;always 0
	dta b(sectors)	;how many sctors to load
	.wo boot	;load address
	.wo init	;init address (seems not to be used)
	jmp start
;	mva #$24 710
	.endp
;--------------------------- boot init end
	.proc init
;you can start an intro here
	rts
	.endp
;---------------------------
	.zpvar	.byte ct,wt,mx,shape,rega,regx,posp1,posp0

ri	equ $a0			;needs 16 bytes
pos	equ $b0			;needs 16 bytes
col	equ $c0			;needs 16 bytes
pmg	equ $2000
p0	equ $2000+$400
p1	equ $2000+$500
p2	equ $2000+$600
p3	equ $2000+$700

;---------------------
	.local DL
	.he 70 80
:12	.he 70			;have to use it on boot disc again
;	.ds 96			;mega cheat :) 12 times $70 or just 96 times 0 if area is clear
	.he 42
	.wo txt
	.he 41
	.wo DL

txt	dta d'NYD 2022',d'  16bars_510b+  '*,d' by PPs '

	.endl
;---------------------
	.local dli
	sta rega
	stx regx
	mva posp0 hposp0
	mva posp1 hposp1
	dec posp0
	inc posp1

	mva #%00111001 dmactl	;set screen width (32b,dma,players)
	sta wsync
	mva #$21 gtictl
;--- additional pm
	rol			;$42	red
	sta colpm2
	rol			;$84	blue
	sta colpm3
;--- additional pm
;stripes
	ldx #0
@	lda start.area,x
	sta colbak
	and #$7f
	sta colpm0
	sta colpm1
	lda shape
	ror
	ror
	ror
	sta color2
	sta wsync
	inx
	cpx #210
	bne @-
	lda #0
	sta colpm0
	sta colpm1
	ldx regx
	lda rega
	rti
	.endl
;---------------------
	.proc start
	.proc main
	jsr clear_area
	mwa #DL 560
	sta pmbase		;-> high byte is $20 ;)
	sta 513			;-> high of DLI is $20, too ;)
	mva <dli 512
;--- set colors			;.he 00 10 20 30 40 50 60 70 80 90 a0 b0 c0 d0 e0 f0
	.local setcol
	ldx #15
lx	lda #$f0
	sta col,x
	sub #$10
	sta lx+1
	dex
	bpl lx
	.endl
;---
	stx shape		;$ff
	stx wt
	stx ct
;---------------------
	.local set_pm
	txa			;x and a @$ff
@	sta p0,x
	sta p1,x
	dex
	bne @-
;--- additional pm
	ldx #7
;	lda #$f0
@	sta p2+113,x
	sta p3+113,x
	dex
	bpl @-
	mva #$70 posp0
	sta posp1
	mva #$40 hposp2
	mva #$a0 hposp3
;--- additional pm
	.endl
;---------------------
	mva #3 sizep0
	sta sizep1
;--- additional pm
	sta sizep2
	sta sizep3
;--- additional pm
	sta pmcntl		;enable players and missiles
	mva #$c0 nmien		;DLI + VBI on
lp
;Test ob Ende
/*	lda trig0
	beq @+
	lda trig1
	beq @+
	lda skctl
	and #$04
	beq @+
	lda consol
	and #1
	bne noend
@	jmp $e477		;cold reset
noend
*/	dec wt
	lda wt
	bpl out
	mva #2 wt
	sta 77
	dec ct
out
;---------------------
	.local stripes
;run thru the 16 stripes
	ldx #15
	stx mx
lp_str
	lda ri,x
	beq hoch
runter
	lda pos,x
	cmp #196
	beq change_up
;runter geht noch
	inc pos,x
	lda pos,x
	tay
	jsr copyshape
	jmp next
change_up			;up again
	ldx mx
	dec ri,x
hoch
	lda pos,x
	beq change_down
;hoch geht noch
	dec pos,x
	lda pos,x
	tay
	jsr copyshape
	jmp next

change_down			;down again
	ldx mx
	inc ri,x
	jmp runter
next
	dec mx
	ldx mx
	bpl lp_str
	.endl
;---------------------
	lda ct
	bne lp
	dec ct			;set back 2 $ff
	jsr chg_shape
	jmp lp
	.endp
;---------------------
	.local copyshape
;first
	lda #0
	sta area,y
;1
	lda col,x
	sta area+1,y
	sta area+12,y
;2
	lda col,x
	ora #2
	sta area+2,y
	sta area+11,y
;3
	lda col,x
	ora #4
	sta area+3,y
	sta area+10,y
;4
	lda col,x
	ora #6
	sta area+4,y
	sta area+9,y
;5
	lda col,x
	ora #8
	sta area+5,y
	sta area+8,y
;6
	lda col,x
	ora #10
	sta area+6,y
	sta area+7,y
;last
	lda #0
	sta area+13,y
	rts
	.endl
;---------------------
	.local clear_area
	ldx #15
@	lda posi,x
	sta pos,x
	lda #1
	sta ri,x
	dex
	bpl @-
	inx
	txa			;0
@	sta area,x
	dex
	bne @-
	rts
;----------
posi	.he 00 0c 18 24 30 3c 48 54 60 6c 78 84 90 9c a8 b4
	.endl
;---------------------
	.local chg_shape
	jsr clear_area
	inc shape
	lda shape
	cmp #4
	beq sixteenth
	cmp #3
	beq eighth
	cmp #2
	beq quarter
	cmp #1
	beq half
;all				;was set during clear_area
	rts
half
	ldx #7
	lda #0
@	sta ri+8,x
	dex
	bpl @-
	rts
quarter
	ldx #4
	lda #0
@	sta ri+4,x
	sta ri+12,x
	dex
	bpl @-
	rts
eighth
	ldx #1
	lda #0
@	sta ri+2,x
	sta ri+6,x
	sta ri+10,x
	sta ri+14,x
	dex
	bpl @-
	rts
sixteenth
	ldx #15
	lda #0
@	sta ri+1,x
	dex
	dex
	bpl @-
	stx shape		;reset shape color value to initial $ff
	rts
	.endl
;---------------------
area				;area of stripes
;	.ds 210			;not needed on disc boot
;---------------------
	.endp
;---------------------------
fin
	.if <fin < 128
;---
	.rept 128-<fin	;Sektor noch auffüllen
	.he 00
	.endr
;----
	.else
;----
	.rept 256-<fin
	.he 00
	.endr
;----
	.endif
;---------------------------
	.rept 359-sectors	;SD disc will be our goal
:128	.he 00
	.endr
;---------------------------
;sector 360 := VTOC
;---------------------------
	.he 02			;DOS 2 DIR
;	.he d0 02		;720 Sectors disc
	.he c5 02
	.he 00 00		;0 free sectors
	.he 00 00 00 00 00	;unused space in VTOC
;10-99 -> mark all as used
:90	.he 00
;100-127 -> unused
:28	.he 00
;---------------------------
;Sector 361-368 := DIR
;---------------------------
	.he 22			;file locked + inuse
	.he 00 00		;file size in sectors
	.he 00 00		;start sector number
	dta '-----------'

	.he 22			;file locked + inuse
	.wo sectors		;file size in sectors
	.he 01 00		;start sector number
	dta '16 bars PPs'

	.he 22			;file locked + inuse
	.he 00 00		;file size in sectors
	.he 00 00		;start sector number
	dta '-----------'

	.rept 5
:16	.he 00			;no file
	.endr

	.rept 7
:128	.he 00
	.endr
;---------------------------
	.rept 352
:128	.he 00
	.endr