program Twister;
var  
	
	@VicMemoryConfig "all"
	

const twisterAddr:address = $2000;
   	xp: byte; 
	twister: incbin("resources/data/twister.bin", twisterAddr);
//	charset: incbin("charsets/charset_8x8.bin", $2000);
	zp,zp2,p1:pointer;
	

@startblock $2A00 "vars"
	x,y,i,j,time,ttime,k,tt,m,t1,t2 : byte = 0;
	table : array[320] of byte;
	sine2 : array[256] of byte;
	eights : array[9] of byte = (0,0,8,16,24,32,40,48,56,64);
@endblock


@startblock $3000 "code"



procedure InitTwister();
begin
	
	DefineScreen();
	screenmemory:=$1000;	
	k:=64;
	for y:=0 to screen_width do begin
		for x:=0 to 8 do begin
			screenmemory[x+8]:=k;
			inc(k);
		end;
		screenmemory:=screenmemory+screen_width;

	end;

end;


procedure UpdateTwister();
begin
	//zp:=@twister;
	zp2:=$1400+64*8;
	t1:=time*3+0 + sine[time];
	t2:=-time*2+99;
	k:=time*8;


	xp:=0;
	for y:=0 to screen_height do begin
		for x:=0 to 8 do begin
//			l:=0;
			tt:=sine2[t1+xp];
			m:=sine2[t2-xp];
			tt:=(m+tt-k)&%11111110;
			zp:=toPointer(table[tt+1], table[tt]);
			
			asm("
			ldx #8
			clc

blah:
			ldy eights,x
			lda (zp),y
			sta (zp2),y
			dex
			bne  blah
			");
			zp2:=zp2+1;
			inc(xp);
//			inc(xp);
//			xp:=xp+3;
		end;
		zp2:=zp2+8*7;
	end;

end;

procedure InitTable();
begin
	zp:=#twisterAddr;
	p1:=#table;
	k:=0;
	for i:=0 to 128 do begin
		j:=i*2;
		p1[j]:=lo(zp);
		p1[j+1]:=hi(zp);
		zp:=zp+1;
		inc(k);
		if (k=8) then begin
			k:=0;
			zp := zp + 7*8;
		end;
		if (i=63) then 	zp:=#twister;

	end;

end;

procedure InitSine();
begin
	for i:=0 to 256 do sine2[i]:=sine[i]/4;

end;

procedure MainRoutine();
begin
	SCREEN_BG_COLOR:=PURPLE;
	fill(^$9400,YELLOW+8,0);
	fill(^$9500,YELLOW+8,0);
	fill(^$1000,$20,0);
	fill(^$1100,$20,0);
	InitTwister();
	InitTable();
	InitSine();
	blockmemcpy(^$8000,^$1400,8);
	AUX_COLOR_AND_VOLUME:=6*16;
//	copycharsetfromrom(^$2000);
	setcharsetlocation($1400);
	while (true) do begin
		UpdateTwister();
		inc(time);
		inc(ttime);
	end;

end;


@endblock

begin
	MainRoutine();
end.
