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

📄 avrddxicc.c

📁 Proteus_里调试AVR单片机的.ddx翻译程序
💻 C
字号:
/*  说明:
    用ImageCraft 的 icc AVR C 编译器生成的目标文件包括了*.HEX 文件和*.LST 文件. 其中*.HEX 文件用来在Proteus 里当作机器码执行. 而调试时需要*.LST 文件. 可是icc AVR 生成的*.LST 文件不能被Proteus 识别. 所以需要用以上的AVRddxIcc.exe 将*.LST 翻译成Proteus 能识别的*.SDI 文件.
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int fgetline(char *buffer,FILE *infile);
void translate(FILE *outfile,char *buffer);
int fgetstart(FILE *infile);

void Usage(char *programName)
{
	fprintf(stderr,"AVR Debug Data Extractin for icc ACR C compiler.\n\
usage: avrddx2 <lstfile>\n");
	/* Modify here to add your usage message when the program is
	 * called without arguments */
}

/* returns the index of the first argument that is not an option; i.e.
   does not start with a dash or a slash
*/
int HandleOptions(int argc,char *argv[])
{
	int i,firstnonoption=0;

	for (i=1; i< argc;i++) {
		if (argv[i][0] == '/' || argv[i][0] == '-') {
			switch (argv[i][1]) {
				/* An argument -? means help is requested */
				case '?':
					Usage(argv[0]);
					break;
				case 'h':
				case 'H':
					if (!stricmp(argv[i]+1,"help")) {
						Usage(argv[0]);
						break;
					}
					/* If the option -h means anything else
					 * in your application add code here
					 * Note: this falls through to the default
					 * to print an "unknow option" message
					*/
				/* add your option switches here */
				default:
					fprintf(stderr,"unknown option %s\n",argv[i]);
					break;
			}
		}
		else {
			firstnonoption = i;
			break;
		}
	}
	return firstnonoption;
}

/* Get a line from the file.
	buffer		To store the got content from file.It is provided from external of body.
	inflie		The source of data.

	return value is the number of got characters(It do not consist of 'enter'.)
		or zero if up to EOF. */
int fgetline(char *buffer,FILE *infile)
{
	char *r;

   	r = fgets(buffer,1024,infile);
   	if(r == NULL){
   		buffer[0] =0;  //清除缓冲区串
   		return 0;  //文件结束返回0
   	}
	else return strlen(buffer);
}

/* Translate .lst file to .sdi file. */
void Translate(FILE *outfile,char *buffer)
{
	static char slable[256]={0};	/* Store nearest lable. */
	char sbuf[1024];	/* The copy of buffer. */
	char savebuf[1027];	/* The final result. */
	char sa[256];
	char *bp;
	int i1,i2;

	savebuf[0] = 0;
	strcpy(sbuf,buffer);
	bp = strtok(sbuf," \t\n");
	if(!bp) return;
	if(bp[0] == '_'){	/* Get the lable if there had it. */
		strcpy(slable,bp);
		return;
	}
	else if(bp[0] >= '0' && bp[0] <= '9'){	/* Translate instruction. */
		strcpy(sa,bp);
		i1 = strtol(sa,NULL,16);
		i2 = i1 * 2;
		sprintf(sa,"%06X",i2);	/* Address */
		strcpy(savebuf,sa);
		strcat(savebuf,",");
		bp = strtok(NULL," \t\n");
		strcat(savebuf,bp);		/* Code */
		strcat(savebuf,",");
		if(strlen(slable)){
			strcpy(sa,slable);	/* Lable */
			sa[strlen(sa)-1] = 0;
			strcat(savebuf,sa);
			strcat(savebuf,",");
			strcat(savebuf,slable);
			slable[0] = 0;
		}
		else{
			strcat(savebuf,",");
		}
		sprintf(sa," %04X",i1);	/* Address Lable*/
		strcat(savebuf,sa);
		bp = strtok(NULL," ");
		strcat(savebuf,"      ");
		strcat(savebuf,bp);		/* Operation */
//		strcat(savebuf,"\n");
	}
	else{
		strcpy(savebuf,",,,");
		strcat(savebuf,buffer);
	}
	fputs(savebuf,outfile);
}

/* Find start of code.
	infile		Input data source.

	return value is the address of word. */
int fgetstart(FILE *infile)
{
	char sbuf[1024];
	char *bp;
	int rt;

	while(fgetline(sbuf,infile)){
		bp = strtok(sbuf," \t\n");
		if(!strcmp(bp,"__start:")){
			fgetline(sbuf,infile);
			fgetline(sbuf,infile);
			bp = strtok(sbuf," \t\n");
			rt = strtol(bp,NULL,16);
			return rt;
		}
	}
	return -1;
}

//mian函数
int main(int argc,char *argv[])
{
	int i;
	char *lstfilename;
	char sdifilename[256];
	FILE *lstfile;	/* Input stream of file. */
	FILE *sdifile;	/* Output stream of file. */
	char buffer[1024]={0};	/* The buffer when get data from file. */
	char *cp1;
	int lc = 0;	/* The total that lines in input file. */
	int rt;

	if (argc == 1) {
		/* If no arguments we call the Usage routine and exit */
		Usage(argv[0]);
		return 1;
	}
	/* handle the program options */
	HandleOptions(argc,argv);

	lstfilename = argv[1];
	lstfile = fopen(lstfilename,"rt");	/* Open input file. */
	if(!lstfile){
		fprintf(stderr,"Can not open .lst file.\n");
		return 1;
	}
	strcpy(sdifilename,lstfilename);	/* Build output file name. */
	cp1 = strrchr(sdifilename,'.');
	if(cp1){
		*cp1 =0;
		cp1++;
	}
	strcat(sdifilename,".SDI");
	sdifile = fopen(sdifilename,"wt");	/* Open output file. */
	if(!sdifile){
		fprintf(stderr,"Can not create .sdi file.\n");
		return 1;
	}

	rt = fgetstart(lstfile);	/* Find out start. */
	if(rt == -1){
	   fprintf(stderr,"Can not find entry.\n");
	   return 2;
	}
	if(rt == 0) rt = 1;
	sprintf(buffer,"000000,C%03X,start,start:     RJMP\t__text_start\n",rt);	/* Boot */
	fputs(buffer,sdifile);
	fseek(lstfile,0,SEEK_SET);
	do{
		rt = fgetline(buffer,lstfile);
		lc++;
		Translate(sdifile,buffer);
	}while(rt != 0);
	fclose(lstfile);
	fclose(sdifile);
	return 0;
}

⌨️ 快捷键说明

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