
//PS2解码程序

//---------------------------------------------------
//设计时间: 2017年4月17日
//设计PS2手柄模块

//设计时间: 2017年4月18日 0:28
//折腾一晚上, 库就是不工作!! 代码形式就没问题...
//最后排查问题, 发现数据放到数组里没问题, 放到变量里就不行
//再最后就知道了, 手柄需要时间反应, 读取速度要进行限制...


unit Pack
{
	public const uint16 ID = 0;
	
	link bit CLK_DIR = receive.CLK.D0_DIR;
	link bit CLK_IN = receive.CLK.D0_IN;
	link bit CLK_OUT = receive.CLK.D0_OUT;
	link bit CLK_PUL = receive.CLK.D0_PUL;
	
	link bit DATA_DIR = receive.DATA.D0_DIR;
	link bit DATA_IN = receive.DATA.D0_IN;
	link bit DATA_OUT = receive.DATA.D0_OUT;
	link bit DATA_PUL = receive.DATA.D0_PUL;
	
	link bit CMD_DIR = receive.CMD.D0_DIR;
	link bit CMD_IN = receive.CMD.D0_IN;
	link bit CMD_OUT = receive.CMD.D0_OUT;
	link bit CMD_PUL = receive.CMD.D0_PUL;
	
	link bit ATT_DIR = receive.ATT.D0_DIR;
	link bit ATT_IN = receive.ATT.D0_IN;
	link bit ATT_OUT = receive.ATT.D0_OUT;
	link bit ATT_PUL = receive.ATT.D0_PUL;
	
	
	//[i] linkinterface_IRreceive receive;
	public link unit receive {}
	
	//[i] var_uint32 OS_EventFlag;
	public uint32 OS_EventFlag;
	
	//[i] event key0_press_event;
	//[i] event key0_up_event;
	//[i] event key1_press_event;
	//[i] event key1_up_event;
	//[i] event key2_press_event;
	//[i] event key2_up_event;
	//[i] event key3_press_event;
	//[i] event key3_up_event;
	//[i] event key4_press_event;
	//[i] event key4_up_event;
	//[i] event key5_press_event;
	//[i] event key5_up_event;
	//[i] event key6_press_event;
	//[i] event key6_up_event;
	//[i] event key7_press_event;
	//[i] event key7_up_event;
	
	//[i] event key8_press_event;
	//[i] event key8_up_event;
	//[i] event key9_press_event;
	//[i] event key9_up_event;
	//[i] event key10_press_event;
	//[i] event key10_up_event;
	//[i] event key11_press_event;
	//[i] event key11_up_event;
	//[i] event key12_press_event;
	//[i] event key12_up_event;
	//[i] event key13_press_event;
	//[i] event key13_up_event;
	//[i] event key14_press_event;
	//[i] event key14_up_event;
	//[i] event key15_press_event;
	//[i] event key15_up_event;
	
	//------------------------------------------------------------
	//[i] var_uint16 OS_time;
	public uint8 OS_time;
	
	uint8 K0;
	uint8 K1;
	uint8 LK0;
	uint8 LK1;
	
	uint8 d;
	uint8 d1;
	
	#include "$run$.txt"
	
	//---------------------------------------------------
	//[i] function_void OS_init;
	void OS_init()
	{
		OS_EventFlag = 0;
		OS_time = 100;
		
		CLK_DIR = 1;
		CLK_OUT = 1;
		
		DATA_DIR = 0;
		DATA_PUL = 1;
		
		CMD_DIR = 1;
		CMD_OUT = 0;
		
		ATT_DIR = 1;
		ATT_OUT = 1;
	}
	
	//---------------------------------------------------
	//[i] function_void OS_run;
	void OS_run()
	{
		get_key();
		
		if( d != 90 ) {
			return;
		}
		uint16 k = K0;
		k.8(uint8) = K1;
		uint16 lk = LK0;
		lk.8(uint8) = LK1;
		
		uint32 t = 0;
		loop( 16 ) {
			t << 2;
			bit b = ~k.0(bit) & lk.0(bit);
			t.0(bit) = t.0(bit) | b;
			b = (k.0(bit) & ~lk.0(bit));
			t.1(bit) = t.1(bit) | b;
			k >> 1;
			lk >> 1;
		}
		OS_EventFlag | t;
		
		LK0 = K0;
		LK1 = K1;
	}
}










