OpenCores
URL https://opencores.org/ocsvn/an-fpga-implementation-of-low-latency-noc-based-mpsoc/an-fpga-implementation-of-low-latency-noc-based-mpsoc/trunk

Subversion Repositories an-fpga-implementation-of-low-latency-noc-based-mpsoc

[/] [an-fpga-implementation-of-low-latency-noc-based-mpsoc/] [trunk/] [mpsoc/] [src_c/] [bin2str/] [main.c] - Blame information for rev 38

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 34 alirezamon
#include <stdio.h>
2
#include <stdlib.h>
3
#include <string.h>
4
#include <unistd.h>
5 38 alirezamon
#include <ctype.h>
6 34 alirezamon
 
7
 
8
 
9
 
10
void bin_str_convert();
11
void hex_str_convert();
12
char *remove_ext (char* , char , char );
13
char *add_ext (char* , char *);
14
 
15
 
16
int bin_enable = 0;
17
int hex_enable = 0;
18
int data_width =32;
19
char * in_file_name;
20
char * out_file_name;
21
 
22
void usage (void)
23
{
24
        printf("Usage: ./bin2str  <options>  \n");
25
        printf("\nOptions: \n");
26
        printf("         -d : memory data width in bit. The default value is 32\n");
27
        printf("         -b : generate output file in Binary string.\n");
28
        printf("         -h : generate output file in Hex string.\n");
29
        printf("         -f <file name>: input bin file  .\n");
30
        printf("         -o <file name>: output ascii text file.\n");
31
 
32
}
33
 
34
void processArgs (int argc, char **argv )
35
{
36
   char c;
37
 
38
     opterr = 0;
39
 
40
   while ((c = getopt (argc, argv, "bhd:f:o:")) != -1)
41
      {
42
         switch (c)
43
            {
44
                case 'd':
45
                        data_width = atoi(optarg);
46
                case 'b':
47
                        bin_enable = 1;
48
                        break;
49
                case 'h':
50
                        hex_enable = 1;
51
                        break;
52
                case 'f':
53
                        in_file_name = optarg;
54
                        break;
55
                case 'o':
56
                        out_file_name =  optarg;
57
                        break;
58
 
59
                case '?':
60
                  if (isprint (optopt))
61
                          fprintf (stderr, "Unknown option `-%c'.\n", optopt);
62
                  else
63
                          fprintf (stderr,   "Unknown option character `\\x%x'.\n",   optopt);
64
                default:
65
                        usage();
66
                        exit(1);
67
            }
68
      }
69
}
70
 
71
 
72
 
73
 
74
int main (int argc, char **argv ){
75
 
76
 
77
        processArgs (argc,argv );
78
        if (in_file_name == NULL) {usage();exit(1);}
79
        if (bin_enable == 0 && hex_enable == 0) {printf("No output file format is selected. One of -b or -h argumet is required.\n\n");usage();exit(1);}
80
        if (data_width == 0 ) {printf("\'0\' is an invalid memory data width.\n\n");usage();exit(1);}
81
        if (out_file_name == NULL) {
82
                out_file_name= remove_ext (in_file_name, '.', '/');
83
        }
84
        if (bin_enable )bin_str_convert();
85
        if (hex_enable )hex_str_convert();
86
 
87
        return 0;
88
}
89
 
90
void bin_str_convert(){
91
        FILE * fin;
92
        FILE * fout;
93
        char * out_name;
94
        char c;
95
        fin = fopen(in_file_name, "rb");
96
 
97
        int i, b,n=0;
98
        if (fin == NULL) {
99
                printf("   Can't open file '%s' for reading.\n", in_file_name);
100
                //return;
101
                exit(1);
102
        }
103
        out_name= add_ext (out_file_name, "memb");
104
        fout = fopen(out_name, "wb");
105
        if (fout == NULL) {
106
                printf("   Can't create file '%s'.\n", out_name);
107
                //return;
108
                exit(1);
109
        }
110
        while (!feof(fin) && !ferror(fin)) {
111
                c=fgetc( fin);
112
                for(i=0;i<8;i++){
113
                        b=(c&0x80)? '1':'0';
114
                        fprintf(fout,"%c",b);
115
                        c<<=1;
116
                        n++;
117
                        if(n==data_width) {
118
                        n=0;
119
                        fprintf(fout,"\n");
120
                        }
121
 
122
                }
123
 
124
 
125
        }
126
        if(n>0){
127
                for(i=n;i<data_width;i++){ fprintf(fout,"0");    }
128
                fprintf(fout,"\n");
129
        }
130
        fclose(fin);
131
        fclose(fout);
132
}
133
 
134
 
135
void hex_str_convert(){
136
        FILE * fin;
137
        FILE * fout;
138
        char * out_name;
139
        char c;
140
        fin = fopen(in_file_name, "rb");
141
 
142
        int i, n=0;
143
        if (fin == NULL) {
144
                printf("   Can't open file '%s' for reading.\n", in_file_name);
145
                //return;
146
                exit(1);
147
        }
148
        out_name= add_ext (out_file_name, "hex");
149
        fout = fopen(out_name, "wb");
150
        if (fout == NULL) {
151
                printf("   Can't create file '%s'.\n", out_name);
152
                //return;
153
                exit(1);
154
        }
155
        while (!feof(fin) && !ferror(fin)) {
156
                c=fgetc( fin);
157
                fprintf(fout,"%02hhX",c);
158
                n+=8;
159
                if(n==data_width) {
160
                        n=0;
161
                        fprintf(fout,"\n");
162
                }
163
        }
164
        if(n>0){
165
                for(i=n;i<data_width;i+=8){ fprintf(fout,"00"); }
166
                fprintf(fout,"\n");
167
        }
168
        fclose(fin);
169
        fclose(fout);
170
}
171
 
172
 
173
// remove_ext: removes the "extension" from a file spec.
174
//   mystr is the string to process.
175
//   dot is the extension separator.
176
//   sep is the path separator (0 means to ignore).
177
// Returns an allocated string identical to the original but
178
//   with the extension removed. It must be freed when you're
179
//   finished with it.
180
// If you pass in NULL or the new string can't be allocated,
181
//   it returns NULL.
182
 
183
char *remove_ext (char* mystr, char dot, char sep) {
184
    char *retstr, *lastdot, *lastsep;
185
 
186
    // Error checks and allocate string.
187
 
188
    if (mystr == NULL)
189
        return NULL;
190
    if ((retstr = malloc (strlen (mystr) + 1)) == NULL)
191
        return NULL;
192
 
193
    // Make a copy and find the relevant characters.
194
 
195
    strcpy (retstr, mystr);
196
    lastdot = strrchr (retstr, dot);
197
    lastsep = (sep == 0) ? NULL : strrchr (retstr, sep);
198
 
199
    // If it has an extension separator.
200
 
201
    if (lastdot != NULL) {
202
        // and it's before the extenstion separator.
203
 
204
        if (lastsep != NULL) {
205
            if (lastsep < lastdot) {
206
                // then remove it.
207
 
208
                *lastdot = '\0';
209
            }
210
        } else {
211
            // Has extension separator with no path separator.
212
 
213
            *lastdot = '\0';
214
        }
215
    }
216
 
217
    // Return the modified string.
218
 
219
    return retstr;
220
}
221
 
222
 
223
char *add_ext (char* mystr, char *ext) {
224
        char *retstr;
225
 
226
    // Error checks and allocate string.
227
 
228
    if (mystr == NULL) return NULL;
229
    if (ext == NULL) return mystr;
230
    if ((retstr = malloc (strlen (mystr) + strlen (ext) + 2)) == NULL) return NULL;
231
    strcpy (retstr, mystr);
232
    strcat(retstr, ".");
233
    strcat(retstr, ext);
234
 
235
    return retstr;
236
 
237
 
238
}
239
 
240
 

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.