program MyProgram;
@use "tables/luminosities"
@use "compression/compression"
var  
   	k2,keep,x,y,i,time,curFrame,j,k,l,curLine: byte = 0; 
	zp,tp,cp:pointer;
	
@export "charsets/rom.flf" "charsets/rom.bin" 127

// Charset to base the text of 
	charset:incbin("charsets/rom.bin",$4000);
	
// Number of columns	in the original image (see "N=30" in the example15_cycle.fjo file)

@define noCols 30

@requirefile "data/ccycle_tst_1.bin" "Please run the Fjong raytracer file 'example15_cycle.fjo' before compiling this program"

// Background color
@define bgCol 0
// Place to put the expanded font 
@define expandPos $D000
// Disable kernal
@define useKernal 0

// Include the actual images (generated by example15_cycle.fjo)
	d1:incbin("data/ccycle_tst_1.bin",$2000);
	d2:incbin("data/ccycle_tst_2.bin",$6000);
	d3:incbin("data/ccycle_tst_3.bin",$A000);

// Lookup table for i/8	
	eights: array[64] of byte = buildtable("i/8");
// This table contains the actual colour information for each cell, 30*7 colors in total .
// Updates each / every 3rd frame	
	cols : array[@noCols*8] of byte = buildtable("0");
// A lookup table for the expanded font (each 8x8=64 bytes)
	table1 : array[128] of integer = buildtable("@expandPos + 64*i");
// A lookup table 
	lp : array[48] of byte = buildTable("(i&7)*8");

	text : cstring=("     TRSE WILL MAKE YOU RICH AND FAMOUS   PROBABLY         NOT            ",0);

// Include the unrolled colour setting data generated with example15_cycle.fjo
@include "data/cycle_tst_unroll1.ras"
// Make sure the unrolled code is stored somewhere nice and tidy
@startblock "$4800" "Unroll2"
@include "data/cycle_tst_unroll3.ras"
@endblock
// Third frame
@startblock "$C000" "Unroll"

@include "data/cycle_tst_unroll2.ras"

@endblock

// Method that updates the actual text colours stored in the "cols" tab
procedure UpdateTextColors();
begin
	// Start with the *back* of the list!
	cp:=#cols + @noCols*7;
	// Stored at $D000, so enable all ram (remember to not write to $Dxxx)
	enableallram();

	// Go through all the columns
	for j:=0 to @noCols offpage do 
	begin
		// Current row							
		l:=(j+curLine);
		// Current character
		k2:=tp[eights[l]];
		// Lookup colum
		zp:=table1[k2] + lp[l];
		// Unrolled copying of 7 bytes of data + shift the colour
		asm("
		
		ldy #0

	   repeat 7

		lda (zp),y
		cmp #@bgCol
		beq cont_h[i]
		adc time
		and #31
		tax
		lda Luminosities_fromBlue,x	
cont_h[i]:
		sta (cp),y	
		iny
	
		repend
		");
		// Move back 7 
		cp:=cp - 7;
	end;
	// Restore memory layout
	poke(^01,0,keep);
	// Increase a column
	inc(curLine);
	// At the end of a character? Reset curLine and increase text pointer!
	if (curLine=8) then begin
		tp:=tp+1;
		curLine:=0;
		if (tp[0]=0) then tp:=#text;
	end;
end;


/*
	Updates the actual frame. There are 3 frames of data, each corresponding to
	an unrolled method for updating the colour data. The actual colours
	are defined in the "cols" table 
*/
procedure UpdateFrame();
begin
	if (curFrame=0) then 
	begin
		setbank(VIC_BANK0);
		cycle_tst_unroll1_unroll();
	end;
	if (curFrame=1) then 
	begin
		setbank(VIC_BANK1);
		cycle_tst_unroll2_unroll();
	end;
	
	if (curFrame=2) then 
	begin
		setbank(VIC_BANK2);
		cycle_tst_unroll3_unroll();
	end;
		
	curFrame-=1;
	
	if (curFrame=255) then begin 	
		curFrame:=2; 
		UpdateTextColors();
	end;

end;

// Main irq
interrupt Raster();
begin
	startirq(@useKernal);
	
	setbitmapmode();
	setmulticolormode();
	setcharsetandscreenlocation($2000,$400);
	
	
	UpdateFrame();
	inc(time);
	closeirq();
end;



/*
	Expands a 1-bitplane charset to memory chunked
*/
procedure ExpandText();
begin
	zp:=#charset;
	keep:=peek(^01,0);
	enableallram();
	cp:=@expandPos;
	// Go through 33 chars
	for i:=0 to 33 do begin
		// Each char has height 8
		for y:=0 to 8 do begin
			// Each char has width 8
			for x:=0 to 8 do 
			begin
				k:=1-getbit(zp[x],7-y);
				// if bit is set, then paint the cell with some crazy value
				if (k=0) then 
					k:=(x*2+i+y) 
//					k:=(x*11+i+y) try me! 
					//k:=x+1 // try me!
					//k:=y+1 // try me!
					// k:=1 // try me!
				else 
					k:=@bgCol; // if not, background colour
				
				// Set the actual colour value
				cp[x]:=k;		
			end;
			// Increase the pointer
			cp:=cp+8;
		end;
		// Increase source data pointer
		zp:=zp+8;
	end;
	// restore memory 
	poke(^01,0,keep);
end;



begin
	disableciainterrupts();
	setmemoryconfig(1,@useKernal,0);

	SCREEN_FG_COL:=@bgCol;
	SCREEN_BG_COL:=@bgCol;
	// Expand the bit font 8x8x33 bytes 
	ExpandText();
	// initialise text pointer	
	tp:=#text;
	
	StartRasterChain(Raster(),0,@useKernal);
	// Everything runs in the irq
	loop();
end.
