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 34

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

powered by: WebSVN 2.1.0

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