// kkapret - 256 byte intro - spc 4 raww orgy 2008
// asm: sjasm 1.07
// psndcj: 15feb08

			DEVICE ZXSPECTRUM128

START	equ #8000
back_bf	equ	#a000	//[#1800]	temp buffer for background

background	equ #e000	//[#1800]generating background (fill with different square)
		
rnd	equ #0000	//!#xx00 rom - used for pseudo-rnd noise =)

			org START
; ---------------------------------------------------------------------------
//	di							//skip
	ld sp,#da00
	ld bc,#000d:ld hl,#ffff
clear
	push hl:djnz clear
	ld h,b,l,b
	dec c:jr nz,clear
	
// generate background
	ld de,rnd
generate1
//	rnd-coords of up-left square corner
	ld a,(de):or high background:ld h,a		//and #3f:or #e0 === or #e0
	ld l,e
//  calculate available size
	ld lx,0									
	
check_square0
	inc lx
check_square	//check place for square
	push hl
		ld c,b
		ld a,#B6	//op-cod OR (HL)
		call square
	pop hl
	and a:jr z,check_square0
	dec lx:jr z,next_square
//draw square
	inc de:ld a,(de):ld c,a
	ld a,#77		//op-cod LD (HL),A
	call square

next_square
	inc de
	bit 3,d:jr z,generate1		//exit if de = #0800

	
// optimize background
	ld hl,background,de,back_bf
optim1
	inc l:ld a,(hl):and #7:add a:add a:add a:or #40
	dec l:or (hl):ld (de),a
	inc de
	inc l:jr nz,optim1
	inc h:jr nz,optim1
	
	
// main loop -------------------------------------

	ld a,#17
mainloop

//	ei:halt				//skip
	ei
	halt

	
	xor #0a
	ld bc,#7ffd:out (c),a
		exa
	xor a:out (#fe),a

mus_play
//output to ay-ports
	ld hl,ay_data:xor a
musout	ld b,#ff:out (c),a
		ld b,#bf:outi
		inc a
musend	cp 14:jr nz,musout
// music
	ld hl,ay_data+9
	ld a,(hl):and a:jr z,$+2+1:dec (hl)
	
	ld l,low ay_data+2
	ld a,8:add (hl):ld (hl),a
	inc hl
	ld a,1:adc (hl):and #f:ld (hl),a

	ld a,10,(musend+1),a

	
	
	
// copy atributes to screen
	ld sp,#db00
back_pointer	ld hl,back_bf+#1f:ld c,l
back_copy1	ld b,#10
back_copy2	ld d,(hl):dec l:ld e,(hl):dec l:push de:djnz back_copy2
	ld l,c:inc h
	ld a,h:cp high back_bf+24:jr nz,back_copy1

	
	
// fill pixel_screen 
cur_gfx	ld h,#ff:ld l,h
//	ld b,0
fill_screen
 .12 push hl
 	djnz fill_screen

// movement of background
	ld a,h:sla a,a:jr nz,no_move
		ld hl,back_pointer+1:inc (hl)
		dec a
no_move
	ld (cur_gfx+1),a 	

// music-beat =)
timer	ld a,24:dec a:jr nz,timer_e
		ld a,#f,(ay_data+9),a
		ld hl,#03a0,(ay_data+2),hl
		ld a,24
timer_e	ld (timer+1),a

		exa
	jp mainloop
	
	
		
ay_data	//playing sound
	db #10,#0D,#A0,#03,#88,#06,#00,#38,#10,#0E,#10,#34,#00,#0E 


square	//universal code-routine - used in check_square and draw_square
		ld (check2),a
		ld a,c:and #7
		ld c,lx
check1	ld b,lx:push hl
check2	or (hl):inc l:djnz check2
		pop hl:inc h
		dec c:jr nz,check1
		ret

	

	
e_all
/////////////////////////////////////////////////save snapshot 


	SAVESNA "demo.sna", START	
//	LABELSLIST "labels.txt"
//	savebin "intro.cod",START,e_all-START
