]> rtime.felk.cvut.cz Git - fpga/plasma.git/blob - tools/tracehex.c
Local copy of Plasma MIPS project.
[fpga/plasma.git] / tools / tracehex.c
1 /***********************************************************
2 | tracehex by Steve Rhoads 12/25/01
3 | This tool modifies trace files from the free VHDL simulator 
4 | http://www.symphonyeda.com/.
5 | The binary numbers are converted to hex values.
6 ************************************************************/
7 #include <stdio.h>
8 #include <stdlib.h>
9 #include <string.h>
10 #include <ctype.h>
11
12 #define BUF_SIZE (1024*1024*4)
13 #define LINE_SIZE 10000
14
15 char drop_char[10000];
16
17 int main(int argc, char *argv[])
18 {
19    FILE *file;
20    char *buf,*ptr_in,*ptr_out,*line_store,*line;
21    char *line_start,*source_start;
22    int bytes,digits,value,isbinary,col,col_num,row,drop_cnt;
23    int col_index,line_index,back_count,drop_start=0;
24    int digits_length=0;
25    (void)argc;
26    (void)argv;
27
28    printf("tracehex\n");
29
30    /* Reading trace.txt */
31    file=fopen("trace.txt","r");
32    if(file==NULL) {
33       printf("Can't open file\n");
34       return -1;
35    }
36    line_store=(char*)malloc(LINE_SIZE);
37    line_store[0]=' ';
38    line=line_store+1;
39    buf=(char*)malloc(BUF_SIZE*2);
40    if(buf==NULL) {
41       printf("Can't malloc!\n");
42       return -1;
43    }
44    ptr_out=buf+BUF_SIZE;
45    bytes=fread(buf,1,BUF_SIZE-1,file);
46    buf[bytes]=0;
47    fclose(file);
48
49    digits=0;
50    value=0;
51    isbinary=0;
52    col=0;
53    col_num=0;
54    row=0;
55    line_start=ptr_out;
56    source_start=buf;
57    for(ptr_in=strstr(buf,"=");*ptr_in;++ptr_in) {
58       ++col;
59       if(drop_start==0&&*ptr_in==' ') {
60          for(drop_start=3;drop_start<30;++drop_start) {
61             if(ptr_in[drop_start]!=' ') {
62                break;
63             }
64          }
65          for(;drop_start<30;++drop_start) {
66             if(ptr_in[drop_start]==' ') {
67                break;
68             }
69          }
70          drop_start-=2;
71       }
72       if(col<4) {
73          drop_char[col]=1;
74          continue;
75       }
76       if(drop_start<=col&&col<=drop_start+2) {
77          drop_char[col]=1;
78          continue;
79       }
80       if(col<drop_start) {
81          *ptr_out++=*ptr_in;
82          continue;
83       }
84       
85       /* convert binary number to hex */
86       if(isbinary&&(*ptr_in=='0'||*ptr_in=='1')) {
87          value=value*2+*ptr_in-'0';
88          ++digits;
89          drop_char[col_num++]=1;
90       } else if(isbinary&&*ptr_in=='Z') {
91          value=1000;
92          ++digits;
93          drop_char[col_num++]=1;
94       } else if(isbinary&&(*ptr_in=='U'||*ptr_in=='X')) {
95          value=10000;
96          ++digits;
97          drop_char[col_num++]=1;
98       } else {
99          if(*ptr_in=='\n') {
100             col=0;
101             isbinary=0;
102             ++row;
103          }
104          if(isspace(*ptr_in)) {
105             if(col>10) {
106                isbinary=1;
107                col_num=col;
108                for(digits_length=1;!isspace(ptr_in[digits_length]);++digits_length) ;
109                --digits_length;
110             }
111          } else {
112             isbinary=0;
113          }
114          *ptr_out++=*ptr_in;
115          digits=0;
116          value=0;
117       }
118       /* convert every four binary digits to a hex digit */
119       if(digits&&(digits_length%4)==0) {
120          drop_char[--col_num]=0;
121          if(value<100) {
122             *ptr_out++=value<10?value+'0':value-10+'A';
123          } else if(value<5000) {
124             *ptr_out++='Z';
125          } else {
126             *ptr_out++='U';
127          }
128          digits=0;
129          value=0;
130       }
131       --digits_length;
132    }
133    *ptr_out=0;
134
135    /* now process the header */
136    file=fopen("trace2.txt","w");
137    col=0;
138    line[0]=0;
139    for(ptr_in=buf;*ptr_in;++ptr_in) {
140       if(*ptr_in=='=') {
141          break;
142       }
143       line[col++]=*ptr_in;
144       if(*ptr_in=='\n') {
145          line[col]=0;
146          line_index=0;
147          for(col_index=0;col_index<col;++col_index) {
148             if(drop_char[col_index]) {
149                back_count=0;
150                while(line[line_index-back_count]!=' '&&back_count<10) {
151                   ++back_count;
152                }
153                if(line[line_index-back_count-1]!=' ') {
154                   --back_count;
155                }
156                strcpy(line+line_index-back_count,line+line_index-back_count+1);
157             } else {
158                ++line_index;
159             }
160          }
161          fprintf(file,"%s",line);
162          col=0;
163       }
164    }
165    drop_cnt=0;
166    for(col_index=13;col_index<sizeof(drop_char);++col_index) {
167       if(drop_char[col_index]) {
168          ++drop_cnt;
169       }
170    }
171    fprintf(file,"%s",buf+BUF_SIZE+drop_cnt);
172
173    fclose(file);
174    free(line_store);
175    free(buf);
176    return 0;
177 }