-
-
[原创]GAL16V8反汇编源程序
-
发表于: 2007-9-14 21:51 7080
-
//GAL16V8反汇编源程序JED2ABEL.C
#i nclude <stdio.h>
#i nclude <io.h>
#i nclude <conio.h>
#i nclude <string.h>
#i nclude <fcntl.h>
#i nclude <alloc.h>
#i nclude <stdlib.h>
#i nclude <sys\stat.h>
unsigned int getfusessum(unsigned char *str);
unsigned int getfusessum(unsigned char *str){
unsigned char ch;
unsigned int fusessum=0;
unsigned int i,j;
for(i=0;i<275;i++){
ch=0;
for(j=0;j<8;j++){
ch>>=1;
if(*str++=='1') ch|=0x80;
}
fusessum+=ch;
}
return fusessum;
}
unsigned char getstrhex(unsigned char *str);
unsigned char getstrhex(unsigned char *str)
{
unsigned char ch=0;
unsigned int i;
for(i=0;i<8;i++){
ch<<=1;
if(*str++=='1') ch++;
}
return ch;
}
main(int argc,char *argv[])
{
FILE *fp1,*fp2,*fp3;
unsigned int x,y,z,i,j,k;
unsigned char *filename1,*filename2,*str,*ptr,*ctr,ch,ck;
unsigned char *p1=".jed",*p2=".pld";
unsigned long pccount,address=0,addressx=0;
unsigned char STX=0x02,ETX=0x03;
unsigned int SYN=2192,AC0=2193,XORN=2120;
unsigned char *buffer="Jed2Abel Version 3.01 Copyright (c) 1993,2008 HotPower";
unsigned char fuses[2200];
unsigned char outpins[]={2,1,3,19,4,18,5,17,6,14,7,13,8,12,9,11};//P16V8S
unsigned char outpinc[]={2,1,3,18,4,17,5,16,6,15,7,14,8,13,9,11};//P16V8C
unsigned char outpinr[]={2,19,3,18,4,17,5,16,6,15,7,14,8,13,9,12};//P16V8R
unsigned char *pinname[]={
"P01 ",
"P02 ",
"P03 ",
"P04 ",
"P05 ",
"P06 ",
"P07 ",
"P08 ",
"P09 ",
"GND ",
"P11 ",
"P12 ",
"P13 ",
"P14 ",
"P15 ",
"P16 ",
"P17 ",
"P18 ",
"P19 ",
"VCC "};
unsigned char *outpin;
unsigned int crc0=0,crc1=0;
unsigned int crcs0=0,crcs1=0;
unsigned int fusesum=0;
unsigned int pin;
puts(buffer);
if (argc<2 || argc>3) {
puts("Syntax: Jed2Abel Jedfilename[.JED] [Abelfilename][.PLD]");
exit(1);
}
filename1=(unsigned char*)calloc(64,sizeof(char));
filename2=(unsigned char*)calloc(64,sizeof(char));
str=argv[1];
ptr=(argc==3) ? argv[2] : filename2;
strcpy(filename1,str);
strcpy(filename2,ptr);
str=filename1;
ptr=filename2;
strlwr(str);
strlwr(ptr);
if(islower(*str)) *str^=0x20;
if(islower(*str)) *ptr^=0x20;
while (*str && (*str!='.')) str++;
if (*str!='.') strcat(filename1,p1);
if ((fp1=fopen(filename1,"rb+"))==NULL) {
printf("Not found Jedfile:[");
printf(filename1);
puts("]");
free(filename1);
free(filename2);
exit(1);
}
while(!feof(fp1) && (ch=fgetc(fp1))!=STX);
if(ch!=STX){
free(filename1);
free(filename2);
exit(1);
}
crc0=ch;
while(!feof(fp1) && (ch=fgetc(fp1))!=ETX) crc0+=ch;
crc0+=ch;
if(ch==ETX){
address=ftell(fp1);
fscanf(fp1,"%04X",&crcs0);
}
if(crc0!=crcs0){//CRC出错
printf("MoveData_CRC Error!!!\r\n");
if(ch==ETX){
printf("OverWrite(Y/N)\n");
ch=getch();
printf("\r\n");
if(ch=='Y' || ch=='y'){
fseek(fp1,address,SEEK_SET);
fprintf(fp1,"%04X",crc0);//强行改写CRC错误!
}
else{
free(filename1);
free(filename2);
exit(1);
}
}
else{
printf("OverWrite(Y/N)\n");
ch=getch();
printf("\r\n");
if(ch=='Y' || ch=='y'){
fseek(fp1,address,SEEK_SET);
fprintf(fp1,"%04X",crc0);//强行改写CRC错误!
}
else{
free(filename1);
free(filename2);
exit(1);
}
}
}
rewind(fp1);
while(!feof(fp1)){
while(!feof(fp1) && fgetc(fp1)!='Q');
if(!feof(fp1) && fgetc(fp1)=='F'){
fscanf(fp1,"%04d",&fusesum);//QF2194
break;
}
}
if(feof(fp1) || fgetc(fp1)!='*' || fusesum!=2194){
printf("No GAL16V8 JedFile!!!\n");
printf("OverWrite(Y/N)\n");
ch=getch();
printf("\r\n");
if(ch=='Y' || ch=='y'){
rewind(fp1);
fusesum=2194;//强行反汇编
}
else{
free(filename1);
free(filename2);
exit(1);
}
}
str=fuses;
for(i=0;i<2200;i++) *str++='0';
ck=0;
while(!feof(fp1)){
ch=fgetc(fp1);
if(ch=='L' && ck==0x0a) break;
ck=ch;
}
while(ch=='L'){
address=ftell(fp1);
pccount=0;
while(!feof(fp1) && (ch=fgetc(fp1))>='0' && ch<='9'){
pccount*=10;
pccount+=ch&0x0f;
}
str=fuses+pccount;
while(ch!='*'){
if(ch=='1' || ch=='0'){
*str++=ch;
pccount++;
}
ch=fgetc(fp1);
}
addressx=ftell(fp1);
while(!feof(fp1) && (ch=fgetc(fp1))!='L' && ch!='C' && ch!='V');
}
while(!feof(fp1) && ch=='V'){
while(!feof(fp1) && (ch=fgetc(fp1))!='*' && (ch=fgetc(fp1))!='\n');
ch=fgetc(fp1);
}
while(!feof(fp1) && ch!='C') ch=fgetc(fp1);
if(ch=='C'){
address=ftell(fp1);
fscanf(fp1,"%04X",&crcs1);
}
str=fuses;
crc1=getfusessum(str);
if(crc1!=crcs1){
printf("FusesData_CRC Error!!!\n");
if(ch=='C'){
printf("OverWrite(Y/N)\n");
ch=getch();
printf("\r\n");
if(ch=='Y' || ch=='y'){
fseek(fp1,address,SEEK_SET);
fprintf(fp1,"%04X",crc1);
}
else{
free(filename1);
free(filename2);
exit(1);
}
}
}
if (argc==2) {
str=filename1;
while (*str!='.') *ptr++=*str++;
*ptr=0;
strcat(filename2,p2);
}
else {
while (*ptr && (*ptr!='.')) ptr++;
if (*ptr==0) strcat(filename2,p2);
else {
ptr++;
printf("Jed2Abel Abelfile:[");
printf(filename2);
printf("] (Y/N)?");
ch=getch();
printf("\r\n");
if (ch=='Y' || ch=='y') {
printf("Jed2Abel Convert (Y/N)?");
ch=getch();
printf("\r\n");
if ((ch&=0x0df) !='Y') {
fclose(fp1);
free(filename1);
free(filename2);
exit(1);
}
}
else {
fclose(fp1);
free(filename1);
free(filename2);
exit(1);
}
}
}
if (_chmod(filename2,1,0x20)==0x20) {
printf("Overwrite existing Abelfile:[");
printf(filename2);
printf("] (Y/N)?");
ch=getch();
printf("\r\n");
if ((ch&0x0df)!='Y') {
fclose(fp1);
free(filename1);
free(filename2);
exit(1);
}
}
if ((fp2=fopen(filename2,"wt"))==NULL) {
printf("Not open Abelfile:[");
printf(filename2);
puts("]");
fclose(fp1);
free(filename1);
free(filename2);
exit(1);
}
for(pin=0;pin<20;pin++){
ctr=pinname[pin];
while(*ctr && *ctr!=' ') ctr++;
*ctr=0;
}
ptr=filename2;
while(*ptr&&*ptr!='.') ptr++;
ctr=ptr;
*ctr++='.';//用户定义管脚文件默认"*.PIN"
*ctr++='P';
*ctr++='i';
*ctr++='n';
*ctr=0;
if ((fp3=fopen(filename2,"rt"))!=NULL){//用户定义管脚文件"pldfilename.pin"
while(!feof(fp3)){
while(!feof(fp3) && (ch=fgetc(fp3))!='P');//只认大写字母P
pin=0;
while(!feof(fp3) && (ch=fgetc(fp3))>='0' && ch<='9'){//只认数字管脚号
pin*=10;
pin+=ch&0x0f;
}
if(ch=='=' && pin>0 && pin<20){//P1=....P20=...
ctr=buffer;
while(!feof(fp3) && (ch=fgetc(fp3))!=';'){//每个管脚定义以分号;结尾
*ctr++=ch;
}
*ctr=0;
*(ctr+8)=0;
if(ch==';'){
ctr=buffer;
strcpy(pinname[pin-1],ctr);//管脚定义名称
P2=ABC;<CR>...P13=OUT;<CR>...
}
}
}
fclose(fp3);
}
else{
if(fuses[SYN]=='0' && fuses[AC0]=='1'){
strcpy(pinname[0],"Clk");
strcpy(pinname[10],"OE");
}
}
[培训]内核驱动高级班,冲击BAT一流互联网大厂工作,每周日13:00-18:00直播授课