
unit ram
{
	public const uint16 ID = 0;
	
	public link unit RS {}
	public link unit RW {}
	public link unit E {}
	
	command.RS = RS;
	command.RW = RW;
	command.E = E;
	command.ID = ID;
	command =
	#include "$run$_command.txt"
	
	//---------------------------------------------------
	//提供的接口
	//[i] interface_char char;
	unit char
	{
		link void clear() {}
		link void clear_line( int32 l ) {}
		link void write_char( int32 l, int32 c, [#.code int8*?] string ) {}
		public int8 PerWidth;
	}
	char.clear = clear;
	char.clear_line = clear_line;
	char.write_char = print_line;
	
	[uint8*64] buffer;
	bool changed;
	
	//---------------------------------------------------
	//[i] function_void OS_init;
	public void OS_init()
	{
		char.PerWidth = 1;
		command.OS_init();
		clear();
		
		changed = true;
	}
	//---------------------------------------------------
	//[i] function_void OS_thread;
	public void OS_thread()
	{
		if( changed ) {
			changed = false;
			command.refresh( buffer );
		}
	}
	//---------------------------------------------------
	//清空
	//[i] function_void clear;
	public void clear()
	{
		clear_line( 1 );
		clear_line( 2 );
		clear_line( 3 );
		clear_line( 4 );
	}
	//---------------------------------------------------
	//清空第 n 行
	//[i] function_void clear_line int32;
	public void clear_line( int32 line )
	{
		uint8 Addr = 0;
		switch( line ) {
			== 1: Addr = 0; break;
			== 2:	Addr = 32; break;
			== 3: Addr = 16; break;
			== 4:	Addr = 48; break;
			default: return;
		}
		loop( 16 ) {
			buffer[Addr] = ' ';
			Addr + 1;
		}
		changed = true;
	}
	//---------------------------------------------------
	//第 n 行显示
	//[i] function_void print_line int32 int32 int32;
	public void print_line( int32 line, int32 column, int32 data )
	{
		int8 c = (int8)(int16)column - 1;
		if( c < 0 || c > 15 ) {
			return;
		}
		uint8 Addr = 0;
		switch( line ) {
			== 1: Addr = 0; break;
			== 2:	Addr = 32; break;
			== 3: Addr = 16; break;
			== 4:	Addr = 48; break;
			default: return;
		}
		Addr = Addr + (uint)c;
		
		uint16 dd = (uint16)(uint)data;
		
		if( dd.8(uint8) != 0 ) {
			
			char.PerWidth = 2;
			
			//由于ST7920不支持奇数位显示汉字，所以这里处理一下，向后顺延一位
			if( Addr % 2 != 0 ) {
				buffer[Addr] = ' ';
				Addr + 1;
				char.PerWidth = 3;
			}
			buffer[Addr] = dd.0(uint8);
			Addr + 1;
			buffer[Addr] = dd.8(uint8);
		}
		else {
			buffer[Addr] = dd.0(uint8);
			char.PerWidth = 1;
		}
		changed = true;
	}
}












