📄 config.cpp
字号:
//SIGNAL DEFINE:
//D4 D3 D2 D1 D0
//nSTATUS CONF_DONE DCLK nCONFIG DATA0
//
#include <iostream.h>
#include <conio.h>
#include <stdio.h>
#include <time.h>
#include <io.h>
#include <dos.h>
#include <stdlib.h>
#include <dir.h>
#define CONFIGPORT 0x310
#define STATUSPORT 0x318
#define RBFLENGTH 59215
#define ONEUS 12
FILE * readin;
unsigned char outbuf,condat=0;
unsigned char data[RBFLENGTH];
long int length;
void ReadRBF(char *filename)
{
long int i=0;
if((readin=fopen(filename,"rb"))== NULL)
{
fprintf(stderr, "Cannot open input file.\n");
exit(0);
}
cout<<filename<<endl;
while(!feof(readin))
{
fread(&data[i],sizeof(unsigned char),1,readin);
// fread(&data[i],1,1,readin);
i++;
}
length=i-1;
fclose(readin);
cout<<"Config data length:"<<length<<"bytes"<<endl;
}
//debug condat d2d1d0 ,use binary format;
void debug(unsigned char hexdat)
{
switch(hexdat)
{
case 0x00:cout<<"000\t";break;
case 0x01:cout<<"001\t";break;
case 0x02:cout<<"010\t";break;
case 0x03:cout<<"011\t";break;
case 0x04:cout<<"100\t";break;
case 0x05:cout<<"101\t";break;
case 0x06:cout<<"110\t";break;
case 0x07:cout<<"111\t";break;
case 0x08:cout<<"1000\t";break;
case 0x09:cout<<"1001\t";break;
case 0x0a:cout<<"1010\t";break;
case 0x0b:cout<<"1011\t";break;
case 0x0c:cout<<"1100\t";break;
case 0x0d:cout<<"1101\t";break;
case 0x0e:cout<<"1110\t";break;
case 0x0f:cout<<"1111\t";break;
}
}
// Data0输出 use D0
void Set_Data0(int high)
{
unsigned char out;
out=high?0x01:0x00;
condat=condat&0x06|out;
// cout<<"D0 "<<high<<" ";
// debug(condat);
outportb(CONFIGPORT,condat);
}
// 设置nCONFIG电平
void Set_nCONFIG(int high)
{
//nconfig use D1
unsigned char out;
out=high?0x02:0x00;
condat=condat&0x05|out;
// cout<<"D1 "<<high<<" ";
// debug(condat);
outportb(CONFIGPORT,condat);
}
// 输出DCLK use D2
void Set_DCLK(int high)
{
unsigned char out;
out=high?0x04:0x00;
condat=condat&0x03|out;
// cout<<"D2 "<<high<<" ";
// debug(condat);
outportb(CONFIGPORT,condat);
}
// 读nSTATUS状态 use D4
int Read_nSTATUS(void)
{
// cout<<"nSTATUS ";
if (inp(STATUSPORT)&0x10) return 1; //need inport statement as condition
else return 0;
}
//use D3
int Read_nCONF_Done(void)
{
if (inp(STATUSPORT)&0x08) return 1; //need inport statement as condition
else return 0;
}
void Delay(int us)
{
int i,j;
// cout<<"Dly"<<us<<" ";
for(i=0;i<us;i++)
{
for(j=0;j<ONEUS;j++); //ONEUS need testing for confirmation
}
}
int ConfigDataDown(void)
{
long int i=0;
int status,j;
Set_nCONFIG(0); // nCONFIG="0",使FPGA进入配置状态
Set_DCLK(0);
Delay(5);
status=Read_nSTATUS();
// cout<<status<<endl;
if ( status == 1)
{ // 检测nSTATUS,如果为"0",表明FPGA已响应配置要求,可开始进行配置。否则报错
cout<<"Error1 found !";
return 0;
}
Set_nCONFIG(1);
Delay(5);
// 开始输出配置数据:
while(i < RBFLENGTH)
// while(i < 1000) //for test;
{
outbuf=data[i];
for (j=0; j<8; j++)
{ // DCLK="0"时,在Data0上放置数据(LSB first)
Set_Data0(outbuf&0x01);
Set_DCLK(1); // DCLK->"1",使FPGA读入数据
outbuf >>= 1; // 准备下一位数据
if (Read_nSTATUS() == 0) //formal state
// if (Read_nSTATUS() == 1) //for test
{ // 检测nSTATUS,如果为"0",表明FPGA配置出错
cout<<"Error2 found when i="<<i<<endl;
exit (0);
}
Set_DCLK(0);
}
// cout<<endl;
i++;
}
// FPGA初始化:
// ACEX 1K需要10个周期
for(i=0; i<10; i++)
{
Set_DCLK(1);
Delay(300);
Set_DCLK(0);
Delay(300);
}
Set_Data0(0);
if (Read_nCONF_Done() == 0)
{ // 检测nCONF_Done,如果为"0",表明FPGA配置未成功
cout<<"Error3 found !";
return 0;
}
return 1; // 成功返回
}
void main()
{
int save;
char olddir[30];
save=getdisk();
getcurdir(0,olddir);
// setdisk(5);
// cout<<getdisk()<<olddir<<endl;
// cout<<chdir("\\sxw\\config");
ReadRBF("f:\\sxw\\config\\aaa.rbf");
setdisk(save);
chdir("\\");
chdir(olddir);
// getch();
// for(i=0;i<length;i++)
// {
// printf("%x\t",data[i]);
// }
clock_t start, end;
start = clock();
ConfigDataDown();
end = clock();
printf("Time elapsed: %f second.\n", (end - start) / CLK_TCK);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -