📄 boundary.cpp
字号:
// Boundary.cpp : Defines the entry point for the application.
//
#include "stdafx.h"
#include "resource.h"
#include <stdio.h>
#include <malloc.h>
#include "winio.h"
#define MAX_LOADSTRING 100
// Global Variables:
HINSTANCE hInst; // current instance
TCHAR szTitle[MAX_LOADSTRING]; // The title bar text
TCHAR szWindowClass[MAX_LOADSTRING]; // The title bar text
// Foward declarations of functions included in this code module:
ATOM MyRegisterClass(HINSTANCE hInstance);
BOOL InitInstance(HINSTANCE, int);
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
LRESULT CALLBACK About(HWND, UINT, WPARAM, LPARAM);
//============================================================
//=======================Port Control=========================
unsigned char pp_data = 0, pp_status = 0, pp_control = 0;
BOOL OpenPort()
{
char path[256];
GetCurrentDirectory(255,path);
strcat(path,"\\winio.sys");
if(InitializeWinIo())return TRUE;
InstallWinIoDriver(path,FALSE);
if(!InitializeWinIo())return FALSE;
return TRUE;
}
void ClosePort()
{
ShutdownWinIo();
}
void SetTMS(BOOL LogicValue)
{
pp_data = LogicValue?(pp_data | 0xf0) : (pp_data & 0x0f);
SetPortVal(0x378, pp_data, 1);
}
void SetTCK(BOOL LogicValue)
{
pp_control = LogicValue?0x0f: 0x00;
SetPortVal(0x37A, pp_control, 1);
}
void SetTDI(BOOL LogicValue)
{
pp_data = LogicValue?pp_data | 0x0f:pp_data & 0xf0;
SetPortVal(0x378, pp_data, 1);
}
BOOL LoadTDO()
{
DWORD status;
GetPortVal(0x379, &status, 1);
pp_status = (unsigned char)status;
return((pp_status|0xf7) == 0xff);
}
//============================================================
//=======================JTAG Control=========================
double tictacPerMicro;
BOOL JTAGing;
void init_timer()
{
LARGE_INTEGER litmp;
QueryPerformanceFrequency(&litmp);
tictacPerMicro = (double)litmp.QuadPart/1000000.;
}
void zizz()
{
/* MSG msg;
GetMessage(&msg,NULL,0,0);
TranslateMessage(&msg);
DispatchMessage(&msg);*/
LARGE_INTEGER litmp;
LONGLONG QPart1,QPart2;;
double dfMinus;
QueryPerformanceCounter(&litmp);
QPart1 = litmp.QuadPart; // 获得初始值
do{
QueryPerformanceCounter(&litmp);
QPart2 = litmp.QuadPart;// 获得中止值
dfMinus = (double)(QPart2-QPart1);
}while(dfMinus < tictacPerMicro);
}
void TickTCK()
{
zizz();
SetTCK(0);
zizz();
zizz();
SetTCK(1);
zizz();
}
BOOL CheckIdCode(char * TargetCode, int Length)
{
BOOL begin[]={0,1,0,0};
int i;
SetTMS(1);
for(i=0;i<6;i++)
if(JTAGing)TickTCK();
else return TRUE;
for(i=0;i<4;i++)
{
SetTMS(begin[i]);
if(JTAGing)TickTCK();
else return TRUE;
}
SetTMS(0);
for(i=0;i<Length;i++)
{
if(TargetCode[i] != 'X')
if(LoadTDO() ^ (TargetCode[i] == '1'))
{
SetTMS(1);
for(i=0;i<6;i++)
if(JTAGing)TickTCK();
else return TRUE;
return FALSE;
}
if(JTAGing)TickTCK();
else return TRUE;
}
SetTMS(1);
for(i=0;i<6;i++)
if(JTAGing)TickTCK();
else return TRUE;
return TRUE;
}
void SetInstruction(char * TargetInstruction, int Length)
{
BOOL begin[]={0,1,1,0,0};
int i;
SetTMS(1);
for(i=0;i<6;i++)
if(JTAGing)TickTCK();
else return;
for(i=0;i<5;i++)
{
SetTMS(begin[i]);
if(JTAGing)TickTCK();
else return;
}
SetTMS(0);
for(i=0;i<Length;i++)
{
SetTDI(TargetInstruction[i] == '1');
if(JTAGing)TickTCK();
else return;
}
SetTMS(1);
for(i=0;i<3;i++)
if(JTAGing)TickTCK();
else return;
}
void ExchangeData(char * data_in, char * data_out, int Length)
{
int i;
SetTMS(0);
if(JTAGing)TickTCK();
else return;
if(JTAGing)TickTCK();
else return;
for(i=0;i<Length;i++)
{
SetTDI(data_in[i] == '1');
data_out[i] = LoadTDO()?'1':'0';
if(JTAGing)TickTCK();
else return;
}
SetTMS(1);
for(i=0;i<3;i++)
if(JTAGing)TickTCK();
else return;
}
void ExTest(char * TargetInstruction, int InstructionLength, char * data_in, char * data_out, int dataLength)
{
int i;
SetInstruction(TargetInstruction, InstructionLength);
ExchangeData(data_in, data_out, dataLength);
SetTMS(1);
for(i=0;i<6;i++)
if(JTAGing)TickTCK();
else return;
}
//======================================================================
//=========================Type Define==================================
struct DeviceID
{
int Length;
char *IdCode;
};
struct JTAGInstruction
{
int Length;
char *ExTest;
};
struct BoundaryRegister
{
char Cell[32];
char Signal[32];
char Pin[32];
char Type[32];
char SafeVal;
int ControlReg;
char ControlVal;
char DisableVal;
};
struct BoundaryRegisters
{
int Length;
struct BoundaryRegister * Register;
};
struct Device
{
char *Name;
struct JTAGInstruction Instruction;
struct DeviceID ID;
struct BoundaryRegisters Boundary;
struct Device * Next;
struct Device * Prev;
};
Device *FirstDevice = NULL, *CurrentDevice = NULL, *LastDevice = NULL;
static Device DeviceChain;
char *SafeCode = NULL,*TestCode = NULL,*IDCode = NULL, *SafeRet = NULL,*TestRet = NULL;
BOOL TestLogic = FALSE;
//=================================================================
//================Test Data Loading & Building=====================
BOOL ReadLine(FILE* hfile, char * buffer)
{
char ch;
int count;
count = 0;
while( !feof( hfile ) )
{
ch = fgetc( hfile);
if((ch==0x0d) || (ch == 0x0a)||(ch == 0x1a))ch = 0;
else if((ch<0x20) || (ch > 0x7e)) ch = 0x20;
if((ch>=97) && (ch<=126))ch -= 32;
if(count == 0)
{
if(ch == 32)continue;
else if(ch == 0)continue;
buffer[count] = ch;
count++;
continue;
}
if(ch == 32)if(buffer[count-1] == 32)continue;
if(ch == '-')if(buffer[count-1] == '-')buffer[count-1] = 0;
buffer[count] = ch;
count ++;
if(ch == 0)
{
if(buffer[0] != 0)break;
else count = 0;
}
};
buffer[count] = 0;
if(buffer[strlen(buffer)-1] == ' ')buffer[strlen(buffer)-1] = 0;
return TRUE;
};
BOOL MatchWord(char * str1, char* str2)
{
char i = 0;
BOOL result = TRUE;
while(str1[i] != 0)
{
if(str2[i]!=str1[i])
{
result = FALSE;
break;
}
i++;
}
return result;
};
BOOL FillDevice(Device * chip, char * FileName)
{
int i,j,temp;
FILE* hfile;
char buffer[1024],targetstr[255],targetstr2[255];
int tNum,ofset;
char tCell[16];
char tSignal[32];
char tPin[16];
char tType[16];
char tSafeVal;
int tControlReg;
char tControlVal;
char tDisableVal;
strcpy(buffer,"LIBRARY\\");
strcat(buffer,FileName);
strcat(buffer,".BSD");
hfile = fopen(buffer,"r");
if(hfile == NULL)return(FALSE);
chip->Next = NULL;
chip->Prev = NULL;
//--------------------------------------
//---- Set Name
chip->Name = (char*)malloc(strlen(FileName)+1);
strcpy(chip->Name,FileName);
//--------------------------------------
//----find out instruction length
rewind(hfile);
strcpy(targetstr,"ATTRIBUTE INSTRUCTION_LENGTH OF ");
strcat(targetstr,FileName);
strcat(targetstr," : ENTITY IS");
while(!feof(hfile))
{
ReadLine(hfile, buffer);
if(MatchWord(targetstr,buffer))
{
sscanf(buffer+strlen(targetstr),"%d",&chip->Instruction.Length);
break;
}
}
//--------------------------------------
//----find out Extest instruction
rewind(hfile);
strcpy(targetstr,"\"EXTEST (");
chip->Instruction.ExTest = (char*)malloc(chip->Instruction.Length+1);
chip->Instruction.ExTest[chip->Instruction.Length]=0;
while(!feof(hfile))
{
ReadLine(hfile, buffer);
if(MatchWord(targetstr,buffer))
{
for(i=0;i<chip->Instruction.Length;i++)
chip->Instruction.ExTest[i] = buffer[strlen(targetstr)+i];
break;
}
}
//--------------------------------------
//----find out IDCODE
rewind(hfile);
strcpy(targetstr,"ATTRIBUTE IDCODE_REGISTER OF ");
strcat(targetstr,FileName);
strcat(targetstr," : ENTITY IS");
while(!feof(hfile))
{
ReadLine(hfile, buffer);
if(MatchWord(targetstr,buffer))
{
ReadLine(hfile, buffer);
i=1;
while((buffer[i]=='0')||(buffer[i]=='1')||(buffer[i]=='X'))i++;
chip->ID.Length = i - 1;
chip->ID.IdCode = (char*)malloc(chip->ID.Length+1);
chip->ID.IdCode[chip->ID.Length]=0;
for(j=1;j<i;j++)
chip->ID.IdCode[j-1] = buffer[j];
break;
}
}
if(feof(hfile)) ///chip has no IDCODE
{
chip->ID.Length = 1;
chip->ID.IdCode = (char*)malloc(chip->ID.Length+1);
chip->ID.IdCode[1]=0;
chip->ID.IdCode[0]='X';
}
//--------------------------------------
//----find out boundary Length
rewind(hfile);
strcpy(targetstr,"ATTRIBUTE BOUNDARY_LENGTH OF ");
strcat(targetstr,FileName);
strcat(targetstr," : ENTITY IS");
while(!feof(hfile))
{
ReadLine(hfile, buffer);
if(MatchWord(targetstr,buffer))
{
sscanf(buffer+strlen(targetstr),"%d",&chip->Boundary.Length);
break;
}
}
//--------------------------------------
//----fill boundary
rewind(hfile);
chip->Boundary.Register = (struct BoundaryRegister *)malloc(sizeof(struct BoundaryRegister)*chip->Boundary.Length);
strcpy(targetstr,"ATTRIBUTE BOUNDARY_REGISTER OF ");
strcat(targetstr,FileName);
strcat(targetstr," : ENTITY IS");
while(!feof(hfile))
{
ReadLine(hfile, buffer);
if(MatchWord(targetstr,buffer))
{
while(!feof(hfile))
{
ReadLine(hfile, buffer);
if(buffer[0] != '"')continue;
temp = strlen(buffer);
for(i=0;i<temp;i++)
if(buffer[i] == '(')
{
buffer[i] = ' ';
break;
}
for(i=strlen(buffer);i>0;i--)
if(buffer[i] == ')')
{
buffer[i] = ' ';
break;
}
temp = strlen(buffer);
for(i=0;i<temp;i++)
{
if(buffer[i] == '"')buffer[i] = ' ';
if(buffer[i] == ',')buffer[i] = ' ';
if(buffer[i] == '&')buffer[i] = 0;
}
j = sscanf(buffer,"%d %s %s %s %c %d %c %c", &tNum, tCell, tSignal, tType, &tSafeVal, &tControlReg, &tControlVal, &tDisableVal);
if(j != 8)
{
tControlReg = -1;
tControlVal= 'X';
tDisableVal = 'X';
}
strcpy(chip->Boundary.Register[tNum].Cell, tCell);
strcpy(chip->Boundary.Register[tNum].Signal, tSignal);
strcpy(chip->Boundary.Register[tNum].Type, tType);
chip->Boundary.Register[tNum].SafeVal = tSafeVal;
chip->Boundary.Register[tNum].ControlReg = tControlReg;
chip->Boundary.Register[tNum].ControlVal = tControlVal;
chip->Boundary.Register[tNum].DisableVal = tDisableVal;
}
break;
}
}
//--------------------------------------
//----find Pin
for(i=0;i<chip->Boundary.Length;i++)
{
rewind(hfile);
targetstr[0] = '"';
strcpy(targetstr+1,chip->Boundary.Register[i].Signal);
if(targetstr[1] == 42)
{
chip->Boundary.Register[i].Pin[0] = 0;
continue;
}
tNum = 0;
temp = strlen(targetstr);
for(j=0;j<temp;j++)
if(targetstr[j] == '(')
{
targetstr[j] = 0;
sscanf(targetstr+j+1,"%d",&tNum);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -