qsimtovcd.c 3.17 KB
#include <stdio.h>

main(int ac,char **av)
{
    FILE *f;
    char line[1024];
    int i,j,k,j1;
    char str[256],str1[256],str2[256];
    char c0,c1;
    struct { char c0,c1; int width; char name[64];} tab[1024];
    int n;
    int t,oldt;

    if(ac!=2)
    {
	printf("usage: qsimtovcd dump.qsim > dump.vcd\n");
	return;
    }
    printf("$version\n");
    printf("	Chronologic Simulation VCS version 2.2\n");
    printf("$end\n");
    printf("\n");
    printf("$date\n");
    printf("	Tue Feb 21 17:35:16 PST 1995\n");
    printf("$end\n");
    printf("\n");
    printf("$timescale\n");
    printf("	1s\n");
    printf("$end\n");
    printf("\n");
    printf("\n");
    printf("$scope module main $end\n");

    c0 = '!';
    c1 = 0;
    n = 0;
    oldt = 0;
    f = fopen(av[1],"r");
    if(f==0)
	return;
    while(fgets(line,1023,f))
    {
	for(i=0;line[i];i++);
	line[i-1] = 0;
	if(line[10]=='.' && i>32)
	{
	    for(j1=11;line[j1] &&(line[j1]!='>' || line[j1-1]!='-');j1++);
	    if(!line[j1])
		continue;
	    for(j=j1-3;line[j]!=' ' && j>0;j--);
	    for(k=j+1;k<=j1-3;k++)
		str[k-j-1] = line[k];
	    str[k-j-1] = 0;
	    strcpy(str1,&line[j1+2]);
	    for(i=0;i<n;i++)
		if(!strcmp(str,tab[i].name))
		    break;
	    if(i==n)
	    {
		strcpy(tab[i].name,str);
		tab[i].c0 = c0;
		tab[i].c1 = c1;
		for(j=0;str1[j];j++);
		if(str1[0]=='\'')
		    j -= 2;
		else
		    j = 1;
		tab[i].width = j;
		if(c1==0)
		    printf("$var wire %6d %c    %s  $end\n",j,c0,str);
		else
		    printf("$var wire %6d %c%c   %s  $end\n",j,c1,c0,str);
		if(c0!='~') 
		    c0++;
		else
		{
		    c0 = '!';
		    if(c1!=0)
			c1++;
		    else
			c1 = '!';
		}
		n++;
 	    }
	}
    }
    fclose(f);
    printf("$upscope $end\n");
    printf("$enddefinitions $end\n");
    printf("#0\n");
    printf("$dumpvars\n");
    f = fopen(av[1],"r");
    if(f==0)
	return;
    while(fgets(line,1023,f))
    {
	for(i=0;line[i];i++);
	line[i-1] = 0;
	if(line[10]=='.' && i>32)
	{
	    for(j1=11;line[j1] &&(line[j1]!='>' || line[j1-1]!='-');j1++);
	    if(!line[j1])
		continue;
	    for(j=j1-3;line[j]!=' ' && j>0;j--);
	    for(k=j+1;k<=j1-3;k++)
		str[k-j-1] = line[k];
	    str[k-j-1] = 0;
	    strcpy(str1,&line[j1+2]);
	    for(j=10;line[j]!=' ' && j>0;j--);
	    for(k=j+1;k<=9;k++)
		str2[k-j-1] = line[k];
	    str2[k-j-1] = 0;
	    t = atoi(str2)*100+(line[11]-'0')*10+line[12]-'0';
	    for(i=0;i<n;i++)
		if(!strcmp(str,tab[i].name))
		    break;
	    if(i!=n)
	    {
		if(t!=oldt)
		   printf("#%d\n",t);
		oldt = t;
		if(tab[i].width==1)
		{
		    if(str1[0]=='u'||str1[0]=='q'||str1[0]=='r'||str1[0]=='x')
			printf("x");
		    else if(str1[0]=='0')
			printf("0");
		    else if(str1[0]=='1')
			printf("1");
		    else if(str1[0]=='s'||str1[0]=='t'||str1[0]=='z')
			printf("z");
		}
		else
		{
		    printf("b");
		    for(j=0;j<tab[i].width;j++)
		    {
			if(str1[2+j]=='u'||str1[2+j]=='q'||str1[2+j]=='r'||str1[2+j]=='x')
			    printf("x");
			else if(str1[2+j]=='0')
			    printf("0");
			else if(str1[2+j]=='1')
			    printf("1");
			else if(str1[2+j]=='s'||str1[2+j]=='t'||str1[2+j]=='z')
			    printf("z");
		    }
		    printf(" ");
		}
		if(tab[i].c1)
		    printf("%c",tab[i].c1);
		printf("%c\n",tab[i].c0);
 	    }
	}
    }
}