Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
Скачиваний:
92
Добавлен:
26.05.2014
Размер:
8.76 Кб
Скачать
//-----------------------------Version 0.44-----------------------------
#include <io.h>
#include <fcntl.h>
#include <sys\stat.h>
#include <process.h>
#include <share.h>
#include <stdio.h>
#include <string.h>
#include <alloc.h>
#include <windows.h>
#include <time.h>
#include <sys/timeb.h>



void	outp_emu ( unsigned AdrPorta, int ValuePorta );
int inp_emu(unsigned AdrPorta);
void derlay(int x){

	if(x < 10) return;

	MSG msg;
	//printf("begin wait for=%d ms\n",x);	
	int v1,v2;
	v1=SetTimer(NULL, v2, x, (FARPROC) NULL);

	while (GetMessage(&msg, NULL, 0, 0)) 
	{
		if(msg.message == 275) {
			KillTimer(NULL, v1);//msg.hwnd
			break;
		}
		//printf("msg=%d\n",msg.message);
		DefWindowProc(msg.hwnd, msg.message, msg.wParam, msg.lParam);
	}

	
	//GetMessage(&msg, NULL, 0, 0);
	//WaitMessage();
	//printf("end wait.\n");
	//Yield();
	}

#undef inp(__portid)
#undef inportb(__portid)
#undef outp(__portid,__value)
#undef outportb(__portid,__value)

#define inp(port) (inp_emu(port))
#define inportb(port) (inp_emu(port))
#define outp(port, data) (outp_emu(port,data))
#define outportb(port, data) (outp_emu(port,data))
#define delay(x) (derlay(x))



struct zmURTK
{
unsigned char wstep[6];
unsigned char rstep[6];
unsigned char SNS0;
unsigned char SNS1;
unsigned char SNS2;
unsigned char DVG0;
unsigned char DVG1;

HWND SIMURTKDD_HWND;//FILE *DataURTKFile;
HWND SIMURTK_HWND; //FILE *DataUserFile;
int DataURTKTxt;
int DataUserTxt;
int GlobalUserData;
int GlobalURTKData;
int mode;
int WhatDVG;
int WhatSNS;
int BufData;


} eURTK={
0,0,0,0,0,0,
0,0,0,0,0,0,
0xFF,
0xFF,
0xFF,//SNS2
0,	//DVG0
0,	//DVG1
0,
0,
0,
0,
0,
0,
0,
0,
0,
0
};



int OpenURTKData(void){

  eURTK.SIMURTKDD_HWND=FindWindow("URTK_Datchik_Data_window",NULL);
  if(eURTK.SIMURTKDD_HWND!=NULL)eURTK.DataURTKTxt=1;else return 0;
  return 1;
}



int OpenUserData(void){//poisk okna Sim URTK
	eURTK.SIMURTK_HWND=FindWindow ("URTK_main_window",NULL);
	if((eURTK.SIMURTK_HWND == NULL)) { MessageBox(NULL,"Sim URTK not found!","Error",MB_OK); return 0;}
	eURTK.DataUserTxt=1;
	// SendMessage(eURTK.SIMURTK_HWND, 0x1300, 0, 1313);
	 return 1;
}


unsigned char GetURTKData(void)
{
// MSG msg;
	static struct timeb t_b[6], t_sync[2];
	//static int count = 0;
	static unsigned char SimSNS[3];
	FILE *flog;
	struct timeb t_e[6];
	int i, bit_dv;	 
	unsigned char tmpByte, inc_changed; 

	//return 0xFF;

	

	if(eURTK.WhatSNS==1)//если он 1ой SNS запрашивает
		{
		eURTK.WhatSNS=2;
		return (eURTK.SNS1);
		}
	if(eURTK.WhatSNS==2)//если он 2ой SNS запрашивает
		{
		eURTK.WhatSNS=0;
		return (eURTK.SNS2);
		}

	flog = fopen("flog.txt","at");

	if(!t_b[0].time){//первая инициализация
		ftime(&t_b[0]);
		t_sync[0] = t_b[0];
		for(i=1; i<6; i++){
			t_b[i] = t_b[i-1];
		}
		
		for(i=0; i<3; i++){
			SimSNS[i] = 0;
		}
	}
	
	//fprintf(flog, "SNS0=%d SNS1=%d SNS2=%d \n", eURTK.SNS0^ 0xFF, eURTK.SNS1^ 0xFF, eURTK.SNS2^ 0xFF);

	//берем текущее время
	ftime(&t_e[0]);
	t_sync[1] = t_e[0];
	for(i=1; i<6; i++){
		t_e[i] = t_e[i-1];		
	}
	





	//устанавливаем связь с 3D
	if(eURTK.DataURTKTxt ==0){ if(!OpenURTKData()) return 0;}
	
	//берем состояние датчиков 3d модели, чтоб проверить не приехала ли она
	if((t_sync[1].time - t_sync[0].time) >= 1){
		char urtkdatabuf[9]="";
		GetWindowText(eURTK.SIMURTKDD_HWND,urtkdatabuf,9);
		sscanf(urtkdatabuf,"%x %x %x",&SimSNS[0],&SimSNS[1],&SimSNS[2]);
		t_sync[0] = t_sync[1];	
		for(i=0; i<3; i++)
			SimSNS[i] ^= 0xFF;
	}
	fprintf(flog, "SimSNS: %d %d %d\n", SimSNS[0], SimSNS[1], SimSNS[2]);



	for(i=0; i<6; i++){
		inc_changed = 0;
		if(	((t_e[i].time-t_b[i].time)>=1) || ( ((t_e[i].time-t_b[i].time)==0) && ((t_e[i].millitm - t_b[i].millitm) > 3) ) ){
			bit_dv = 0x01<<((i%4)*2);
			if(i%2){tmpByte = 0x10;}else{tmpByte = 0x02;}
			if(!(i/4)){
				if((eURTK.DVG0 & bit_dv) || (eURTK.DVG0 & (bit_dv<<1)) ){//M 0-3 включен ли
					//проверяем не приехал ли...
					if( ((eURTK.DVG0 & bit_dv) && (SimSNS[i/2] & tmpByte)) || ((eURTK.DVG0 & (bit_dv<<1)) && (SimSNS[i/2] & (tmpByte>>1))) )
						inc_changed = 0;
					else
						inc_changed = 1;
						//fprintf(flog, "SimSNS: %d %d %d\n", SimSNS[0], SimSNS[1], SimSNS[2]);
						//fprintf(flog, "%d t1=%ld, t2=%ld, mt1=%d, mt2=%d,SNS0=%d SNS1=%d SNS2=%d \n", count, t_b[3].time, t_e[3].time, t_b[3].millitm, t_e[3].millitm, eURTK.SNS0^ 0xFF, eURTK.SNS1^ 0xFF, eURTK.SNS2^ 0xFF);						
				}
			}
			else{
				if((eURTK.DVG1 & bit_dv) || (eURTK.DVG1 & (bit_dv<<1)) ){//M 4-5 включен ли
					//проверяем не приехал ли...
					if( ((eURTK.DVG1 & bit_dv) && (SimSNS[i/2] & tmpByte)) || ((eURTK.DVG1 & (bit_dv<<1)) && (SimSNS[i/2] & (tmpByte>>1))) )
						inc_changed = 0;
					else
						inc_changed = 1;
					//inc_changed = 1;
				}
			}

			
			//ставим конечные биты
			if(!(i/2)){eURTK.SNS0 &= ~((SimSNS[i/2] & tmpByte) | (SimSNS[i/2] & (tmpByte>>1)));}
			else if((i/2)==1){eURTK.SNS1 &= ~((SimSNS[i/2] & tmpByte) | (SimSNS[i/2] & (tmpByte>>1)));}
			else {eURTK.SNS2 &= ~((SimSNS[i/2] & tmpByte) | (SimSNS[i/2] & (tmpByte>>1)));}
			


			if(inc_changed)
			{				
				if(i%2){tmpByte = 0x20;}else{tmpByte = 0x04;}

				if(!(i/2)){eURTK.SNS0 ^= tmpByte;}
				else if((i/2)==1){eURTK.SNS1 ^= tmpByte;}
				else {eURTK.SNS2 ^= tmpByte;}
				t_b[i] = t_e[i];				
			}
		}
	}
	
	
	//fprintf(flog, "%d t1=%ld, t2=%ld, mt1=%d, mt2=%d,SNS0=%d SNS1=%d SNS2=%d \n", count, t_b[3].time, t_e[3].time, t_b[3].millitm, t_e[3].millitm, eURTK.SNS0^ 0xFF, eURTK.SNS1^ 0xFF, eURTK.SNS2^ 0xFF);


	//eURTK.SNS1 = 0xFF;
	//eURTK.SNS2 = 0xFF;	
	
	
	//if(count > 700){
	//eURTK.SNS0 = SimSNS;
	//eURTK.SNS0 = 0x18;
	//eURTK.SNS1 ^= 0x18;
		
	//}
	fclose(flog);	
	
	
	//fprintf(flog, "t1=%ld, t2=%ld, mt1=%d, mt2=%d, \n",t_b.time, t_e.time, t_b.millitm, t_e.millitm);

	/*char urtkdatabuf[9]="";
	GetWindowText(eURTK.SIMURTKDD_HWND,urtkdatabuf,9);
	sscanf(urtkdatabuf,"%x %x %x",&eURTK.SNS0,&eURTK.SNS1,&eURTK.SNS2);
	*/



	eURTK.WhatSNS=1;
	

	return (eURTK.SNS0);

}



void WriteUserData(void)
{

	//if(((eURTK.WhatDVG==0) &&(eURTK.BufData==eURTK.DVG0)) ||((eURTK.WhatDVG==1)&&(eURTK.BufData==eURTK.DVG1))) return;//budem pisat tolko raznye dannye

	if(eURTK.WhatDVG==0) {
		eURTK.DVG1 = eURTK.BufData; 
		return;
	}
	if(eURTK.WhatDVG==1)  
		eURTK.DVG0=eURTK.BufData;

	if(eURTK.DataUserTxt ==0){ 
		if(!OpenUserData()) return; //nashli li
	}	
	
	SendMessage(eURTK.SIMURTK_HWND, 0x1300, eURTK.DVG0,eURTK.DVG1);

}







int inp_emu(unsigned eAdr)
{
unsigned char eData=0;
if(eAdr!=(0x378))return 0;
if(eURTK.rstep[2]) {eData=GetURTKData();eURTK.rstep[3]=1;}
//printf("inp=%d\n", eData);
return (eData);
}




void	outp_emu ( unsigned eAdr, int eData )
{
int RD,A0,WR,A1,B5;
//printf("outp_emu mode=%d\n",eURTK.mode);

if(eAdr==(0x378))
	{
	if((eURTK.rstep[1]==0)&&(eURTK.mode==(0x13))&&(eData==(0xFF))){eURTK.rstep[1]=1;return;}

	if(eURTK.wstep[1]==1){
		eURTK.BufData=eData;
		eURTK.wstep[2]=1;
		return;
		}	

	}
if(eAdr!=(0x37A))return;

RD=(eData&(0x1));
A0=(eData&(0x2))>>1;
WR=(eData&(0x4))>>2;
A1=(eData&(0x8))>>3;
B5=(eData&(0x20))>>5;

//printf("RD=%d,A0=%d,WR=%d,A1=%d,B5=%d\n",RD,A0,WR,A1,B5);

if(eData&(0xD0)) return;

if(eURTK.rstep[3])
	{
	if((WR==1)&&(RD==0)){
		eURTK.rstep[1]=0;
		eURTK.rstep[2]=0;
		eURTK.rstep[3]=0;

		eURTK.wstep[1]=0;
		eURTK.wstep[2]=0;
		eURTK.wstep[3]=0;
		}
	return;
	}

if((A0==0)&&(A1==0))//piwem v RC (opredelyaem re*im)
		{
	if(eURTK.wstep[1]==0)
		{
		if((RD==0)&&(WR==1)&&(B5==0)){eURTK.wstep[1]=1;return;}
		}
	if(eURTK.wstep[2]==1)
		{
		if((RD==0)&&(WR==0)&&(B5==0)){eURTK.wstep[3]=1;return;}
		}
	if(eURTK.wstep[3]==1)
		{
		if((RD==0)&&(WR==1)&&(B5==0))
			{
			//eURTK.wstep[4]=1;
			eURTK.mode=eURTK.BufData;
			eURTK.wstep[1]=0;
			eURTK.wstep[2]=0;
			eURTK.wstep[3]=0;
			return;
			}
		}
	}

if((A0==1)&&(A1==1))//piwem v RD (idet zapis) ili 4itaem
	{
	if(eURTK.rstep[1]==1)
		{
		if((B5)&&(RD)&&(A0)&&(A1)&&(WR)){eURTK.rstep[2]=1;return;}
		}
	
	if(eURTK.wstep[2]==1)
		{
		if((RD==0)&&(WR==0)&&(B5==0)){eURTK.wstep[3]=1;return;}
		}
	if(eURTK.wstep[3]==1)
		{
		if((RD==0)&&(WR==1)&&(B5==0))
			{
			if(eURTK.mode==0x11)
				{
				WriteUserData();
				//printf("piwem=%d",eURTK.BufData);
				if(eURTK.WhatDVG==0)eURTK.WhatDVG=1;else eURTK.WhatDVG=0;
				}
			eURTK.wstep[1]=0;
			eURTK.wstep[2]=0;
			eURTK.wstep[3]=0;
			return;
			}
		}
	//if(eURTK.wstep[1]==0)
	//	{
		if((RD==0)&&(WR==1)&&(B5==0)){eURTK.wstep[1]=1;return;}
	//	}
	}




}
Соседние файлы в папке INCLUDE