//---------------------------------------------------------------------------------------------------------------------
// Allborder TV Noise (2x1 mode)
//---------------------------------------------------------------------------------------------------------------------
// Code: Cruzer/CML Sports, November 2010
// Asm: KickAss 3.14
//---------------------------------------------------------------------------------------------------------------------
.var randomize = false
//---------------------------------------------------------------------------------------------------------------------
.pc = $0326
.word start
.byte $ed,$f6
//---------------------------------------------------------------------------------------------------------------------
start:
	//generate noise gfx for the sprites...
.define noiseSrc0, noiseSrc1 {
	.if (randomize) {
		.var noiseSrc0 = $e000 + random()*$2000
		.var noiseSrc1 = $e000 + random()*$2000
		.print ".var noiseSrc0 = $" + toHexString(noiseSrc0)
		.print ".var noiseSrc1 = $" + toHexString(noiseSrc1)
	} else {
		.var noiseSrc0 = $f7a5
		.var noiseSrc1 = $fc6d
	}
}
!loop:
	jsr $e3b3	//waste some cycles to get more counter difference
	lda $dc04
	eor $d012
	eor noiseSrc0,y
out:	sta $c000,y
	iny
	bne !loop-
	lda #$3f
	dcp out+2
	bne !loop-
	
	sei
	
	//setup sprite positons. The y's are displaced to get different
	//gfx in each sprite when they're stretched...
	lda #$48
	ldx #$0f
!:	dex
	sta $d000,x
	pha
	txa
	sta $d001-2,x
	pla
	sec
	sbc #$30
	dex
	bpl !-

	lda #$f0
	sta $d000

	//turn on sprites + x-expansion...
	stx $d015
	stx $d01d
	
	//set sprite colors...
	ldx #$08
!:	sta $d027-1,x
	dex
	bne !-
	
	//setup sprite pointers, different ones on each screen so they can
	//be switched with d018...	
!loop:	ldy #$08
	txa
sp:	sta $43f7,y
	dey
	bne sp
	inx
	lda sp+2
	clc
	adc #$04
	sta sp+2
	bcc !loop-
	
	//open lower border for 1st frame...
	lda #$fa
!:	cmp $d012
	bne !-
	sty $d011
	
	sty $7fff	//clear ghostbyte
	sty $bfff	//clear ghostbyte
	dey	//set y to $ff
	
!:	cpx $d012
	bne !-
	
	lda #$c1
	sta $d021
	bne goStart
//---------------------------------------------------------------------------------------------------------------------
mainLoop:
	
	//rotate sprite x's to reduce vertical artifacts...
rotate:
	ldx #$10
!:	lda $d000-2,x
	sta $d002-2,x
	dex
	dex
	bne !-
	lda $d010
	sta $d000-$ff,y	//-$ff,y for timing
	
	asl $fe
	txa
	adc $fe
goStart:	sta $fe
	sta $d010

	cmp #$83	//avoid having spr 0 to the right since d017 stretching fucks up its gfx
	beq rotate
	
	//reset d011, but keep screen turned off for sprite-only mode with no badlines..
	ldx #$0b
	stx $d011
!:
	cpx $d012
	bne !-

	//loop from top to beginning of lower border...
	ldx #$12
lineLoop0:
	sta $d418-$ff,y	//sound ("-$ff,y" for timing)
	
	stx rom0+1
	lda $dc04
	
	inc $d017	//stretch sprites
	sty $d017
	
	sty $d016	//open sideborder
	inc $d016	//RMW instruction + VIC DMA at the right place = automagic stable raster

rom0:	eor $a000	//"random" screen for sprite pointers
	sta $d018
	
	inx
	bne lineLoop0
	
	stx $d011	//open lower border
	
	
	//same kinda deal for lower part of screen...
	
	ldx #$d8
lineLoop1:
	inc $d017
	sty $d017
	
	sta $d418
	lda $dc04
	
	sty $d016
	inc $d016

	stx rom1+1
	
rom1:	eor $a000
	sta $d018
	
	inx
	bne lineLoop1
	
	stx $d017
	
	anc #$01	//select between two vic banks
	adc #$01
	sta $dd00
	
	lda $dc04	//use counter to select random rom page
	ora #$a0
	sta rom0+2
	sta rom1+2

	jmp mainLoop
//---------------------------------------------------------------------------------------------------------------------

