⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 newcon.cpp

📁 在Dos下对FPGA进行在线配置的源程序, 需要读入FPGA的二进制配置文件, 当然端口操作需要特定的硬件支持.
💻 CPP
字号:
//SIGNAL DEFINE:
//D4			D3		D2		D1		D0
//CONF_DONE   	nSTATUS DCLK	nCONFIG	DATA0
//
#include <iostream.h>
#include <conio.h>
#include <stdio.h>
#include <io.h>
#include <dos.h>
#include <stdlib.h>
#include <dir.h>

#define CONFIGPORT 0x310
#define STATUSPORT 0x318
#define RBFLENGTH 59215
#define ONEUS 28
#define DATA0 0x00
#define nCONFIG 0x02
#define DCLK 0x04
#define nSTATUS 0x08
#define nCONF_Done 0x10

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;
	}
}


void Set(int signal,int level)
{
	unsigned char keep,out;
	out=level?signal:0x00;	
	keep=(~signal)&0x07;
	condat=condat&keep|out;
	cout<<signal<<" "<<level<<" ";
	debug(condat);
	outportb(CONFIGPORT,condat);		
}
//// 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);
//}

int Read(unsigned char select)
{
	cout<<select<<" ";
	if (inp(STATUSPORT)&select) return 0;  //need inport statement as condition
	else return 1;
}
// 读nSTATUS状态   use D3
//int Read_nSTATUS(void)
//{
//	cout<<"nSTATUS ";
//	if (inport(STATUSPORT)&0x08) return 0;  //need inport statement as condition
//	else return 1;
//}
//
////use D4
//int Read_nCONF_Done(void)
//{
//	if (inport(STATUSPORT)&0x10) 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)
{
	int i=0,j;
	Set(nCONFIG,0); // nCONFIG="0",使FPGA进入配置状态
	Set(DCLK,0);
	Delay(5);
	if (Read(nSTATUS) == 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 !";
				exit (0);
			}
			Set(DCLK,0);
		}
		cout<<endl;
		i++;
	}

// FPGA初始化:
// ACEX 1K需要10个周期
	for(i=0; i<10; i++)
	{
		Set(DCLK,1);
		Delay(100);
		Set(DCLK,0);
		Delay(100);
	}
	Set(DATA0,0);
	if (Read(nCONF_Done) == 0)
	{ // 检测nCONF_Done,如果为"0",表明FPGA配置未成功
		cout<<"Error3 found !";
		return 0;
	}
	return 1; // 成功返回
}


void main()
{
//	long int i=0;
	char tempdir[30];
	cout<<chdir("D:\\sxw\\config")<<endl;
	getcurdir(0,tempdir);
	cout<<tempdir<<endl;
	ReadRBF("output.rbf");
//	getch();

//	for(i=0;i<length;i++)
//	{
//		printf("%x\t",data[i]);
//	}
	ConfigDataDown();

}


⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -