	opt	h+

os_font	equ	$e000

	org	$2000
fnt	org	*+$400
pm0	org	*+$100
pm1	org	*+$100
pm2	org	*+$100
pm3	org	*+$100
dl	org	*+$100		
sine	org	*+$100
	
st	jsr	make_dl
	
	stx	$d208			; 15KHz clock for POKEY

	dex				; $01 in X after return, after INX we have zero again :)
	
	ldy 	#$3f			; make sine table,  approximated by parabola ;)

vl0	clc				; Accumulate the delta (normal 16-bit addition)
dlt0	lda 	#0		
val0	adc 	#0
	sta 	val0+1

dlt1	lda	#0
val1	adc 	#0
	sta 	val1+1
 
	sta 	sine+$c0,x		; Reflect the value around for a sine wave
	sta 	sine+$80,y
	eor 	#$ff
	sta 	sine+$40,x
	sta 	sine+$00,y
	
	clc
	lda	#$10			; Increase the delta, which creates the "acceleration" for a parabola
	adc	dlt0+1
	sta	dlt0+1
	bcc	*+5
	inc	dlt1+1
	
	inx				; do the loop
	dey
	bpl 	vl0	


; make new font procedure (thiner font based on OS-ROM)

	ldx	#$03
	
	stx	$d20f			; init POKEY

	ldy	#$00

ma0	lda	os_font,y
	sta	ma2+1
	lsr	@
ma2	and	#0
ma1	sta	fnt+1,y
	iny
	bne	ma0
	inc	ma0+2
	inc	ma1+2
	dex
	bpl	ma0

	lda	>fnt			; set CHARSET
	sta	$2f4
	lda	>dl			; set DL (Hi-byte)
	sta	$231

	inx				; make X=0 
	stx	$230			; set DL (Lo-byte)

s0	lda	$d20a			; generate random stars
	sta	pm0,x
	lda	$d20a
	sta	pm1,x
	lda	$d20a
	sta	pm2,x
	lda	$d20a
	sta	pm3,x

	cpx	#$08			; font copy loop
	bcs	s1

	lda	fnt+['S'-32]*8,x	; copy "S" data
	sta	fnt+['%'-32]*8,x	; into "Œ" place
	
s1	cpx	#$04
	bcs	s2
	
	lda	ctab,x			; set PM colors!
	sta	$2c0,x

s2	inx
	bne	s0

	stx	fnt			; clear 1st byte of charset (b'coz fnt+1)

	lda	#$04			; make "Œ"
	sta	fnt+['%'-32]*8,x	; into "Œ" place
	asl	@
	sta	fnt+1+['%'-32]*8,x	; into "Œ" place

	inx				; X=1 now
	stx	$d00d			; set PM shapes, without DMA
	stx	$d00e
	stx	$d00f
	stx	$d010
	
	lda	#$af			; set volume and distortion type for POKEY channels #0 & #1
	sta	$d201
	sta	$d203
	
loop	lda	#$0f			; wait for needed rasterline
	cmp	$d40b
	bne	*-3
	sta	$d40a			; HSYNC!
	
hsc	lda	#$0f			; HSCROLL counter
	sta	$d404			

	lsr	$4d			; kill Atract!

cnt	ldx	#$00			; make scroll "jump effect" by changeing DL begining 
	lda	sine,x			
	lsr	@
	lsr	@
	pha
	adc	<dl
	sta	$d402
	
	pla				; make some sound effects!
	adc	#$80
	sta	$d200
	adc	#$01
	sta	$d202

	clc				; jump index calculation
	lda	#$03
	adc	cnt+1
	and	#$7f
	sta	cnt+1

	ldx	#$3f			; starfield HEIGHT

l0	lda	pm0,x			; move from pgm x-pos tables to the pgm pos. rgisters
	sta	$d000
	lda	pm1,x
	sta	$d001
	lda	pm2,x
	sta	$d002
	lda	pm3,x
	sta	$d003

	lda	sine+40,x		; color the text with sine table values
	lsr	@
	lsr	@
	sta	$d016

	sta	$d40a			; HSYNC!
	dex
	bpl	l0

	stx	$d000			; hide the sprites
	stx	$d001
	stx	$d002
	stx	$d003
	
	ldx	#$3f			; starfield HEIGHT

l1	inc	pm0,x			; do the move! (change stars x-pos)
	clc
	lda	#2
	adc	pm1,x
	sta	pm1,x
	lda	#3
	adc	pm2,x
	sta	pm2,x
	lda	#4
	adc	pm3,x
	sta	pm3,x

	dex				; decrement loop counter
	bpl	l1			; end of loop?
	
; H-scroll procedure
	
	dec	hsc+1			; dec. H-scroll
	bne	sc0			; skip when not zero

	lda	#$07			; reload H-scroll
	sta	hsc+1		
	inw	dl+$41			; increment scr position
	
	lda	dl+$41			; end of scroll check!
	cmp	<eof
	lda	dl+$42
	sbc	>eof
	bcc	sc0			; scrolling not finished, do the jump!
	
	lda	<scr			; reload scr pointer!
	sta	dl+$41
	lda	>scr
	sta	dl+$42
	
sc0	jmp	loop			; infinity Loop!
	
; make Display list procedure

make_dl	ldx	#$40			; some of empty lines
	lda	#$00
	jsr	put
	lda	#$57			; antic mode $0f with set of screen pointer
	jsr	put
	lda	<scr
	jsr	put
	lda	>scr
	jsr	put
		
	lda	#$41			; jump to the begining of display list
	jsr	put
	lda	<dl
	jsr	put
	lda	>dl

put	sta	dl			; put byte procedure 
	inc	put+1
	bne	*+5
	inc	put+2
	dex
	bne	put
	inx
	rts

scr	dta	d'                       WSZYSTKIEO NAJLEPSZEGO PECU%! $100 LAT!'
eof	dta	d'                     '
ctab	dta	$74,$86,$9c,$af

	run	st
	