OpenCores
URL https://opencores.org/ocsvn/socgen/socgen/trunk

Subversion Repositories socgen

[/] [socgen/] [trunk/] [tools/] [utils/] [bin2abs.c] - Blame information for rev 116

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 101 jt_eaton
/*$$HEADER*/
2
/******************************************************************************/
3
/*                                                                            */
4
/*                    H E A D E R   I N F O R M A T I O N                     */
5
/*                                                                            */
6
/******************************************************************************/
7
 
8
// Project Name                   : ORPSoC v2
9
// File Name                      : bin2abs.c
10
// Prepared By                    : 
11
// Project Start                  : 
12
 
13
/*$$COPYRIGHT NOTICE*/
14
/******************************************************************************/
15
/*                                                                            */
16
/*                      C O P Y R I G H T   N O T I C E                       */
17
/*                                                                            */
18
/******************************************************************************/
19
/*
20
  This library is free software; you can redistribute it and/or
21
  modify it under the terms of the GNU Lesser General Public
22
  License as published by the Free Software Foundation;
23
  version 2.1 of the License, a copy of which is available from
24
  http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt.
25
 
26
  This library is distributed in the hope that it will be useful,
27
  but WITHOUT ANY WARRANTY; without even the implied warranty of
28
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
29
  Lesser General Public License for more details.
30
 
31
  You should have received a copy of the GNU Lesser General Public
32
  License along with this library; if not, write to the Free Software
33
  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
34
*/
35
 
36
/*$$DESCRIPTION*/
37
/******************************************************************************/
38
/*                                                                            */
39
/*                           D E S C R I P T I O N                            */
40
/*                                                                            */
41
/******************************************************************************/
42
//
43
// Generates basic ASCII hex output to stdout from binary file input
44
// Compile and run the program with no options for usage.
45
//
46
 
47
#include <stdio.h>
48
#include <stdlib.h>
49
#include <string.h>
50
/* Number of bytes before line is broken
51
   For example if target flash is 8 bits wide,
52
   define BREAK as 1. If it is 16 bits wide,
53
   define it as 2 etc.
54
*/
55
#define BREAK 1
56
 
57
int main(int argc, char **argv)
58
{
59
 
60
        FILE  *fd;
61
        int c;
62
        int i = 0;
63 116 jt_eaton
        int j = 1;
64 106 jt_eaton
        int line = 0;
65 101 jt_eaton
        int filename_index=1;
66
        int bytes_per_line=1;
67
        int bytes_per_line_index=2;
68 116 jt_eaton
        int min_num_bytes = 0;
69 106 jt_eaton
        int min_num_bytes_index=3;
70 116 jt_eaton
        int byte_lane=0;
71
        int byte_lane_index=4;
72 101 jt_eaton
 
73
 
74
        if(argc < 3) {
75
          fprintf(stderr,"\n\tInsufficient options.\n");
76
          fprintf(stderr,"\tPlease specify, in this order: a binary file to\n");
77
          fprintf(stderr,"\tconvert and the number of bytes of data to putput\n");
78
          fprintf(stderr,"\tper line.\n");
79
          exit(1);
80
        }
81
 
82
        fd = fopen( argv[filename_index], "r" );
83
        bytes_per_line = atoi(argv[bytes_per_line_index]);
84 106 jt_eaton
 
85
        if(argc > 3) {
86
        min_num_bytes = atoi(argv[min_num_bytes_index]);
87
        }
88
 
89
 
90 116 jt_eaton
        if(argc > 4) {
91
        byte_lane = atoi(argv[byte_lane_index]);
92
        }
93 106 jt_eaton
 
94 101 jt_eaton
 
95 116 jt_eaton
        if ((bytes_per_line == 0) || (bytes_per_line > 8) || (byte_lane > bytes_per_line )    )
96 101 jt_eaton
          {
97
            fprintf(stderr,"bytes per line incorrect or missing: %s\n",argv[bytes_per_line_index]);
98
            exit(1);
99
          }
100
 
101 106 jt_eaton
 
102 101 jt_eaton
 
103
        if (fd == NULL) {
104
                fprintf(stderr,"failed to open input file: %s\n",argv[1]);
105
                exit(1);
106
        }
107
 
108
        i=0;
109 106 jt_eaton
        line=0;
110 101 jt_eaton
 
111 116 jt_eaton
        if(byte_lane)
112
          {
113 101 jt_eaton
        // Now write out the binary data to hex format
114
        while ((c = fgetc(fd)) != EOF) {
115 116 jt_eaton
          j=i+1;
116
          if(byte_lane == j)
117
            {   printf("%.2x", (unsigned int) c);
118
 
119
            }
120
                if (++i == bytes_per_line) {
121
                        printf("\n");
122
                        line++;
123
                        i = 0;
124
                }
125
        }
126
 
127
 
128
        while (i) {
129
          j=i+1;
130
          if(byte_lane == j)
131
            {   printf("00");
132
 
133
            }
134
 
135
 
136
                if (++i == bytes_per_line) {
137
                        printf("\n");
138
                        line++;
139
                        i = 0;
140
                }
141
        }
142
 
143
 
144
 
145
        while (line < min_num_bytes) {
146
                printf("00\n");
147
                line++;
148
        }
149
          }
150
      else
151
 
152
          {
153
            // no byte lane
154
        // Now write out the binary data to hex format
155
        while ((c = fgetc(fd)) != EOF) {
156 101 jt_eaton
                printf("%.2x", (unsigned int) c);
157
                if (++i == bytes_per_line) {
158
                        printf("\n");
159 106 jt_eaton
                        line++;
160 101 jt_eaton
                        i = 0;
161
                }
162
        }
163
 
164 106 jt_eaton
 
165
        while (i) {
166
                printf("00");
167
                if (++i == bytes_per_line) {
168
                        printf("\n");
169
                        line++;
170
                        i = 0;
171
                }
172
        }
173
 
174
 
175
 
176
        while (line < min_num_bytes) {
177
                printf("00000000\n");
178
                line++;
179
        }
180 116 jt_eaton
          }
181 106 jt_eaton
 
182
 
183
 
184
 
185 101 jt_eaton
        return 0;
186
}

powered by: WebSVN 2.1.0

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