

//ҺԪ,޹ش

//2015.4.15 .... ~~ 
//ҹһش: 2004Һʼͨ, Ļʾ,
//һλͨĻ, ͻһֱ, ٳ.
//鿴ֲҲ֪, ...... ǲ, , 74595, ...
//ʱ... 취þ, żȻŰѸλеö, в,
//һԷǳȶ, ÿθλȷ, ֮Ļʾ...
//¸λ, εԴ, ˺ö, Ѱ...... ǳ

//2016.1.17 ̫Ҫ... ִclear֮ٸλ, ȶ!
//ĩ, ڰ1602. 2004ҺŪ, 8λ,4λ,ת4λڵȶԺ,
//ڸλ, Ҫľǳʼ֮, , Ȼٴεóʼ

	//=================================================================================
	//ṩĽӿ
	//[i] interface_char char;
	unit char
	{
		link void clear() {}
		link void clear_line( int32 l ) {}
		link void write_char( int32 l, int32 c, [int8*?] string ) {}
		public const int8 PerWidth = 1;
	}
	char.clear = clear;
	char.clear_line = clear_line;
	char.write_char = print_line;
	
	[uint8*32] buffer;
	bool changed;
	
	//---------------------------------------------------
	//ʼ
	//[i] function_void OS_init;
	public void OS_init()
	{
		CMD.port_init();
		
		InitReg();
		clear();
		OS_thread();
		
		//2016.1.17 ̫Ҫ... ִclear֮ٸλ, ȶ!
		InitReg();
		
		clear();
	}
	//---------------------------------------------------
	//߳ɨ
	//[i] function_void OS_thread;
	public void OS_thread()
	{
		if( changed ) {
			changed = false;
			
			SetColume( 0x80, 0 );
			SetColume( 0xC0, 16 );
		}
	}
	void SetColume( uint8 p, uint8 j )
	{
		loop( 16 ) {
			uint8 c = buffer[j];
			if( c > 0x80 ) {
				c & 0x7F;
				buffer[j] = c;
				set_char( p, c );
			}
			p + 1;
			j + 1;
		}
	}
	//---------------------------------------------------
	//
	//[i] function_void clear;
	public void clear()
	{
		clear_line( 1 );
		clear_line( 2 );
	}
	//---------------------------------------------------
	//յ n 
	//[i] function_void clear_line int32;
	public void clear_line( int32 line )
	{
		if( line < 1 || line > 2 ) {
			return;
		}
		uint8 p = ((uint)(int8)(int16)line - 1) * 16;
		uint8 x80 = 0x80;
		loop( 16 ) {
			buffer[p] = ' ' + x80;
			p + 1;
		}
		changed = true;
	}
	//---------------------------------------------------
	// n ʾ
	//[i] function_void print_line int32 int32 int32;
	public void print_line( int32 line, int32 column, int32 d )
	{
		if( line < 1 || line > 2 ) {
			return;
		}
		if( column < 1 || column > 16 ) {
			return;
		}
		uint8 p = ((uint)(int8)(int16)line - 1) * 16;
		uint8 start = (uint)(int8)(int16)column - 1;
		
		int8 data = (int8)(int16)d;
		if( data < 0x20 ) {
			data + '0';
		}
		uint8 x80 = 0x80;
		buffer[p + start] = (uint)data + x80;
		changed = true;
	}
	
	#include "$run$.txt"
	
	
	
	