program KOS;
var  

	i,time,t2:byte=0;
	@define basex 16
	@define bgCol $21
	cx:byte = @basex;
	cy:byte = 16;
	curCol : byte=$56;
	curPos : byte = 0;
	curKey : byte=$20;
	zp,tp,cmd,arg:pointer;

	// String commands
	cmdList : string = "ls";
	cmdHelp : string = "?";
	cmdLoad : string = "load";
	cmdCls : string = "cls";
	
	music : incsid("music/jingle.sid",2);
	
@export "images/logo.flf" "images/logo.bin"  256
	logo : incbin("images/logo.bin",$21F00);    

	sInput : array[64] of byte;
	sSplit : array[64] of byte;

/*
  Scrolls the screen down 
*/
procedure Scroll();
var 
   i: byte;
begin
	okvc_src_page:=0;
	if (cy>=29*8) offpage then
	begin
		drawRect(0,240,255,16,@bgCol);
		for i:=0 to 16 step 2 do
		begin
			blit(0,2, 0,0,255,253);
			waitforvsync();
		end;
		cy:=cy-16;
	end;
end;

procedure printText(s:pointer);
var
	 i:byte=0;
begin
	i:=0;
	while (s[i]<>0) do
	begin
		PrintChar(s[i], 0, cx+1,cy+1);
		PrintChar(s[i],curCol, cx,cy);
		cx:=cx+8;
		inc(i);
	end;
	cx:=@basex;
	cy:=cy+8;
	Scroll();	

end;


procedure printSingleChar(p:byte);
begin
	PrintChar(p,curCol, cx,cy);
	cx:=cx+8;
	if (cx>=29*8) then
	begin
		cx:=@basex;
		cy:=cy+8;
	end;
	sInput[curPos]:=p;
	inc(curPos);

end;


procedure ListFiles();
var 
  tmp:byte;
begin
	ResetFileList();
	zp:=#OKVC_FILE;
	tmp:=curCol;
	curCol:=37;
	while (zp[0]<>0) do
	begin
		printText(zp);
		ReadNextFile();
	end;
	curCol:=tmp;
end;


procedure PrintHelp();
begin
	printText("KOS 0.02 commands:");
	printText("  ls : list files");
	printText("  load [file] : load file");
	printText("  ? : this text");
	printText("  cls: clear screen");

end;


procedure Run();
var 
  ok:byte=false;
begin
	ok:=false;
	zp:=#sInput;
	if (zp[0]=0) then
		return();
	
	StrToLower(#sInput);
	strSplit(#sInput, #sSplit, key_space);

	//printText(sSplit);
	
	cmd:=strgetfromindex(#sSplit,0);
	arg:=strgetfromindex(#sSplit,1);
	if (strcmp(cmd,cmdList)) then 
	begin
		ListFiles();
		ok:=true;
	end;		
	if (strcmp(cmd,cmdHelp)) then 
	begin
		PrintHelp();
		ok:=true;
	end;		
	if (strcmp(cmd,cmdCls)) then 
	begin
		ClearScreen(@bgCol);
		cx:=16;
		cy:=16;
		ok:=true;
	end;		
	if (strcmp(cmd,cmdLoad)) then 
	begin
		memcpy(arg,0,#okvc_file,16);
		LoadFile();
		printText("Could not load:");
		printText(arg);

		ok:=true;
	end;		
	
	if (ok=false) then
		printText("Syntax error");

//	if (StrCmp(sInput,"")

end;


interrupt Input();
var 
	key,ok:byte;
begin
	StartIRQ(0);
	clearsound();
	
	key := okvc_input_key;
	drawRect(cx,cy,8,8,@bgCol);
	ok:=true;
	//PrintChar(curKey,curCol, cx,cy);
	if (key=13) then
	begin
		cx:=@basex;
		cy:=cy+8;
		Scroll();

		ok:=false;
		sInput[curPos]:=0;
		curPos:=0;
		Run();
	end;
	if (key=127) then
	begin
		cx:=cx-8;
		if (cx<@basex) then cx:=@basex;
		ok:=false;
		curPos:=curPos-1;
	end;
	if (key=19) then
	begin
				cy-=8;
			ok:=false;	
	end;
	if (key=21) then
	begin
				cy+=8;
			ok:=false;	
	end;
	if (key=20) then
	begin
				cx+=8;
			ok:=false;	
	end;
	if (key=18) then
	begin
				cx-=8;
			ok:=false;	
	end;



	
	if (ok)then
	begin
		curKey:=key;	
		printSingleChar(key);
	end;
	CloseIRQ();
end;


procedure SetupScreen();
var 
  i:byte;
begin
	okvc_border_color:=$41;
	clearscreen(@bgCol);
	i:=curCol;
	blit(0,32, 0,0, 255,255);	
	asm(";tay");
	curCol:=okvc_random;
	curCol:=curCol & %11111000;
//	curCol:=4;
	printText("64k ram + 1024k vram");
	curCol:=curCol+2;
	printText("MOS6502 + SID");
	curCol:=curCol+2;
	printText("OK64 KOS v0.02.");
	curCol:=curCol+1;
	printText("'?' for help");
	curCol:=curCol+1;
	printText("");
	printText("Available programs in ROM:");
	curCol:=curCol+2;
	curCol:=i;
	ListFiles();	
	curCol:=curCol+1;
	printText("Ready.");
end;


procedure Main();
begin
	while(true) do 
	begin
		t2:=t2+1;
		if (t2&1=0) then
			time:=time+16;
		
	
		if (time=128) then
		begin
			drawRect(cx,cy,8,8,@bgCol);
		end;
		if (time=0) then
		begin
			drawRect(cx,cy,8,8,curCol);
		end;
		waitforvsync();
		call(SIDFILE_1_PLAY);		
		
		okvc_channel1_vol:=sine[t2*4];
		okvc_channel2_vol:=sine[t2*4+128];
	end;
end;




begin
	InputIRQ(Input());
	initsid(SIDFILE_1_INIT);
//	StartupSound();
	SetupScreen();
//	StartupSound();
	Main();
	
end.
