|
//菜农HotPower@126.com 2003.8.21 于雁塔菜地//GAL16V8反汇编源程序JED2ABEL.C
#include
#include
#include
#include
#include
#include
#include
#include
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;...P13=OUT;...
}
}
}
fclose(fp3);
}
else{
if(fuses[SYN]=='0' && fuses[AC0]=='1'){
strcpy(pinname[0],"Clk");
strcpy(pinname[10],"OE");
}
}
*ptr=0;
ptr=filename2;
str=filename1;
while(*str&&*str!='.') str++;
*str=0;
str=filename1;
printf("Jed2Abel ");
printf(filename1);
printf(" To ");
puts(filename2);
printf("Jed2Abel ConVert start, Please Wait...\r\n\r\n");
rewind(fp2);
fprintf(fp2,"module _%s\n",str);
fprintf(fp2,"\ntitle 'Jed2Abel Version 3.01 Copyright (c) 1993,2008\n");
fprintf(fp2," Jed2Abel JedFile:[%s.Jed]\n",str);
fprintf(fp2," HotPower 1992.11.19'\n\n");
//fuses[SYN]=='0' && fuses[AC0]=='1' P16V8R
//fuses[SYN]=='1' && fuses[AC0]=='1' P16V8C
//fuses[SYN]=='1' && fuses[AC0]=='0' P16V8S
if(fuses[SYN]=='1' && fuses[AC0]=='0'){
fprintf(fp2," %s device 'P16V8S';\"Make JedFusesFile %s.Jed\n",ptr,ptr);
printf(" %s.Jed device 'P16V8S';\n",ptr);
outpin=outpins;
}
else{
if(fuses[SYN]=='1' && fuses[AC0]=='1'){
fprintf(fp2," %s device 'P16V8C';\"Make JedFusesFile %s.Jed\n",ptr,ptr);
printf("%s.Jed device 'P16V8C';\n",ptr);
outpin=outpinc;
}
else{
if(fuses[SYN]=='0' && fuses[AC0]=='1'){
fprintf(fp2," %s device 'P16V8R';\"Make JedFusesFile %s.Jed\n",ptr,ptr);
printf("%s.Jed device 'P16V8R';\n",ptr);
outpin=outpinr;
}
}
}
fprintf(fp2,"\n");
for(pin=0;pin<9;pin++){
if(pin) fprintf(fp2,",");
fprintf(fp2,"%s",pinname[pin]);
}
fprintf(fp2," Pin ");
for(pin=1;pin<10;pin++){
if(pin>1) fprintf(fp2,",");
fprintf(fp2,"%1d",pin);
}
fprintf(fp2,";\n");
for(pin=10;pin<19;pin++){
if(pin>10) fprintf(fp2,",");
fprintf(fp2,"%s",pinname[pin]);
}
fprintf(fp2," Pin ");
for(pin=11;pin<20;pin++){
if(pin>11) fprintf(fp2,",");
fprintf(fp2,"%2d",pin);
}
fprintf(fp2,";\n\n");
y=0;
for(x=0,pin=19;pin>=12;x++,pin--){
if(fuses[SYN]=='0' && fuses[XORN+x]=='0') y++;
}
if(y){
for(x=0,pin=18;pin>=11;x++,pin--){
if(fuses[SYN]=='0' && fuses[XORN+x]=='0'){
fprintf(fp2,"%s",pinname[pin]);
if(--y) fprintf(fp2,",");
}
}
fprintf(fp2," istype 'reg,pin';\n");
}
y=0;
for(x=0,pin=19;pin>=12;x++,pin--){
if(!(fuses[SYN]=='0' && fuses[XORN+x]=='0')) y++;
}
if(y){
for(x=0,pin=18;pin>=11;x++,pin--){
if(!(fuses[SYN]=='0' && fuses[XORN+x]=='0')){
fprintf(fp2,"%s",pinname[pin]);
if(--y) fprintf(fp2,",");
}
}
fprintf(fp2," istype 'com';\n");
}
y=0;
for(x=0,pin=18;pin>=11;x++,pin--){
//fuses[2048..2055]=1 pos;
//fuses[2048..2055]=0 neg;
if(fuses[2048+x]=='1') y++;
}
if(y){
for(x=0,pin=18;pin>=11;x++,pin--){
if(fuses[2048+x]=='1'){
fprintf(fp2,"%s",pinname[pin]);
if(--y) fprintf(fp2,",");
}
}
fprintf(fp2," istype 'pos';\n");
}
y=0;
for(x=0,pin=19;pin>=12;x++,pin--){
if(fuses[2048+x]=='0') y++;
}
if(y){
for(x=0,pin=18;pin>=11;x++,pin--){
if(fuses[2048+x]=='0'){
fprintf(fp2,"%s",pinname[pin]);
if(--y) fprintf(fp2,",");
}
}
fprintf(fp2," istype 'neg';\n");
}
y=0;
for(x=0,pin=19;pin>=12;x++,pin--){
if(fuses[AC0]=='1' && fuses[XORN+x]=='1') y++;
}
if(y){
for(x=0,pin=18;pin>=11;x++,pin--){
if(fuses[AC0]=='1' && fuses[XORN+x]=='1'){
fprintf(fp2,"%s",pinname[pin]);
if(--y) fprintf(fp2,",");
}
}
fprintf(fp2," istype 'eqn';\n");
}
fprintf(fp2,"\nH,L,X,Z,C,P=1,0,.X.,.Z.,.C.,.P.;\n");
fprintf(fp2,"\nequations\n");
for(x=0,pin=18;pin>=11;x++,pin--){
str=fuses+x*32*8;
ptr=str;
y=0;
z=0;
k=0;
for(i=0;i<32*8;i++){
if(*ptr++=='0') y++;
else z++;
}
if(z){
for(i=0;i<8;i++){
ptr=str+i*32;
y=0;
z=0;
for(j=0;j<32;j++){
if(*ptr++=='0') y++;
else z++;
}
if(k==0){
if(fuses[2048+x]=='0' && !(i==0 && fuses[AC0]=='1' && fuses[XORN+x]=='1')) fprintf(fp2,"!");
else fprintf(fp2," ");
fprintf(fp2,"%s",pinname[pin]);
}
if(i==0){
if(fuses[AC0]=='1' && fuses[XORN+x]=='1'){
fprintf(fp2,".oe =");
}
else k++;
}
else k++;
if(k==1){
if(fuses[SYN]=='0' && fuses[XORN+x]=='0') fprintf(fp2," :=");
else fprintf(fp2," =");
}
if(y==0){
if(k==0){
fprintf(fp2," 1;\n");
}
}
else{
if(z==0){
if(k==0){
fprintf(fp2," 0;\n");
}
}
else{
if(k>1) fprintf(fp2,"\n #");
ptr=fuses+x*32*8;
ptr+=i*32;
z=0;
for(j=0;j<32;j++){
if(*ptr=='0'){
if(z) fprintf(fp2," &");
if(j&0x01) fprintf(fp2," !");
else fprintf(fp2," ");
y=j>>1;
fprintf(fp2,"%s",pinname[*(outpin+y)-1]);
z++;
}
ptr++;
}
if(k==0) fprintf(fp2,";\n");
}
}
}
if(k>1) fprintf(fp2,";\n");
}
else{
fprintf(fp2,"\"%s =Input Pin\n",pinname[pin]);
}
}
fprintf(fp2,"\nfuses \n");
str=fuses;
ch=getstrhex(str+2056);
//fuses[2056..2063] string
fprintf(fp2," [2056..2063]=");
if(ch==0) fprintf(fp2,"'';\n");
else{
if(ch>=' ') fprintf(fp2,"'%c';\n",ch);
else fprintf(fp2,"^h%02X;\n",ch);
}
ch=getstrhex(str+2064);
fprintf(fp2," [2064..2071]=");
if(ch==0) fprintf(fp2,"'';\n");
else{
if(ch>=' ') fprintf(fp2,"'%c';\n",ch);
else fprintf(fp2,"^h%02X;\n",ch);
}
ch=getstrhex(str+2072);
fprintf(fp2," [2072..2079]=");
if(ch==0) fprintf(fp2,"'';\n");
else{
if(ch>=' ') fprintf(fp2,"'%c';\n",ch);
else fprintf(fp2,"^h%02X;\n",ch);
}
ch=getstrhex(str+2080);
fprintf(fp2," [2080..2087]=");
if(ch==0) fprintf(fp2,"'';\n");
else{
if(ch>=' ') fprintf(fp2,"'%c';\n",ch);
else fprintf(fp2,"^h%02X;\n",ch);
}
ch=getstrhex(str+2088);
fprintf(fp2," [2088..2095]=");
if(ch==0) fprintf(fp2,"'';\n");
else{
if(ch>=' ') fprintf(fp2,"'%c';\n",ch);
else fprintf(fp2,"^h%02X;\n",ch);
}
ch=getstrhex(str+2096);
fprintf(fp2," [2096..2103]=");
if(ch==0) fprintf(fp2,"'';\n");
else{
if(ch>=' ') fprintf(fp2,"'%c';\n",ch);
else fprintf(fp2,"^h%02X;\n",ch);
}
ch=getstrhex(str+2104);
fprintf(fp2," [2104..2111]=");
if(ch==0) fprintf(fp2,"'';\n");
else{
if(ch>=' ') fprintf(fp2,"'%c';\n",ch);
else fprintf(fp2,"^h%02X;\n",ch);
}
ch=getstrhex(str+2112);
fprintf(fp2," [2112..2119]=");
if(ch==0) fprintf(fp2,"'';\n");
else{
if(ch>=' ') fprintf(fp2,"'%c';\n",ch);
else fprintf(fp2,"^h%02X;\n",ch);
}
fseek(fp1,addressx,SEEK_SET);
while(!feof(fp1) && (ch=fgetc(fp1))!='V');
y=1;
*(buffer+20)=0;
while(!feof(fp1) && ch=='V'){
fscanf(fp1,"%04d",&pccount);
while(!feof(fp1) && (ch=fgetc(fp1))!=' ');
fprintf(fp2,"\ntest_vectors\"V%04d\n",y++);
fscanf(fp1,"%20c",buffer);
str=buffer;
fprintf(fp2,"([");
z=0;
for(pin=0;pin<19;pin++){
if(*str=='0' || *str=='1' || *str=='C' || *str=='P'){
if(z) fprintf(fp2,",");
fprintf(fp2,"%s",pinname][pin]);
z++;
}
str++;
}
fprintf(fp2,"]->[");
str=buffer;
z=0;
for(pin=0;pin<19;pin++){
if(*str=='H' || *str=='L' || *str=='Z'){
if(z) fprintf(fp2,",");
fprintf(fp2,"%s",pinname][pin]);
z++;
}
str++;
}
fprintf(fp2,"])\n");
str=buffer;
fprintf(fp2," [");
z=0;
for(pin=1;pin<=19;pin++){
if(*str=='0' || *str=='1' || *str=='C' || *str=='P'){
if(z) fprintf(fp2,",");
if(*str=='0') fprintf(fp2," L ");
else{
if(*str=='1') fprintf(fp2," H ");
else fprintf(fp2," %c ",*str);
}
z++;
}
str++;
}
fprintf(fp2,"]->[");
str=buffer;
z=0;
for(pin=1;pin<=19;pin++){
if(*str=='H' || *str=='L' || *str=='Z'){
if(z) fprintf(fp2,",");
fprintf(fp2," %c ",*str);
z++;
}
str++;
}
fprintf(fp2,"];\n");
while(!feof(fp1) && (ch=fgetc(fp1))!='V');
}
str=filename1;
while(*str&&*str!='.') str++;
*str=0;
str=filename1;
fprintf(fp2,"\nend _%s\n",str);
free(filename1);
free(filename2);
fclose(fp1);
|
|