program Full_screen_char_compression;
var  
   	


	const charLoc1:address=$5000;
	const charLoc2:address=charLoc1 + $800;
	const charLoc3:address=charLoc2 + $800;
	const charLoc4:address=charLoc3 + $800;

	
	@requirefile "data/13_screen0.bin" "Please run the raytracer file example13_compressed_chars.fjo before compiling this program" 	

	screens1: incbin("data/13_screen0.bin",6000);
	screens2: incbin("data/13_screen1.bin",8000);
	screens3: incbin("data/13_screen2.bin",10000);
	screens4: incbin("data/13_screen3.bin",12000);
	
	charset1 : incbin("data/13_charset0.bin",charLoc1);
	charset2 : incbin("data/13_charset1.bin",charLoc2);
	charset3 : incbin("data/13_charset2.bin",charLoc3);
	charset4 : incbin("data/13_charset3.bin",charLoc4);
	sp:pointer;
	frame:byte=0;
	time:byte=0;

	const screenLoc:address= $4400;	
	
	const maxFrames : byte=8;

@startblock $8000 "Unrolled copy"

procedure UpdateFrame();
begin
	// For each frame, simply set the 
	// character set. In this example, 2 images are contained in 1 character set
	if (frame=6) then setcharsetlocation($2800);// 2x frames
	if (frame=4) then setcharsetlocation($2000);// 2x frames
	if (frame=2) then setcharsetlocation($1800);// 2x frames
	if (frame=0) then setcharsetlocation($1000);// 2x frames
	
	// FAST full-screen copying using unrolled loops
	memcpyunroll(sp,0,screenLoc,^256);
	asm(" inc sp+1 "); // Quick way of saying sp:=sp+256;
	memcpyunroll(sp,0,screenLoc+^256,256);
	asm(" inc sp+1 ");
	memcpyunroll(sp,0,screenLoc+^256*^2,256);
	asm(" inc sp+1 ");
	memcpyunroll(sp,0,screenLoc+^256*^3,232);
	
	sp:=sp+232; // Add upp to 40x25 = 1000
 	inc(frame);
 	// Reset frame
	if(frame=maxFrames) then
	begin
		frame:=0;
		sp:=#screens1;
		
	end;
end;
@endblock


// Simple raster routine
interrupt Raster();
begin
	startirq(0);
	inc(time);
	if(time&1=0) then UpdateFrame();
	closeirq();
end;	


begin
	sp:=#screens1;
	
	setbank(vic_bank1);
	setmulticolormode();
	
	screen_bg_col:=black;
	screen_fg_col:=black;
	multicolor_char_col[1]:=cyan;
	multicolor_char_col[2]:=blue;
	clearscreen(purple+8,screen_col_loc);

	setcharsetlocation(@charLoc1);
	disableciainterrupts();
	setmemoryconfig(1,0,0);
	StartRasterchain(Raster(), 0,0);
	Loop();

end.
