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

Subversion Repositories socgen

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

Go to most recent revision | 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 106 jt_eaton
        int line = 0;
64 101 jt_eaton
        int filename_index=1;
65
        int bytes_per_line=1;
66
        int bytes_per_line_index=2;
67 106 jt_eaton
        int min_num_bytes_index=3;
68
        int min_num_bytes = 0;
69 101 jt_eaton
 
70
 
71
        if(argc < 3) {
72
          fprintf(stderr,"\n\tInsufficient options.\n");
73
          fprintf(stderr,"\tPlease specify, in this order: a binary file to\n");
74
          fprintf(stderr,"\tconvert and the number of bytes of data to putput\n");
75
          fprintf(stderr,"\tper line.\n");
76
          exit(1);
77
        }
78
 
79
        fd = fopen( argv[filename_index], "r" );
80
 
81 106 jt_eaton
 
82
 
83
 
84
 
85
 
86
 
87
 
88 101 jt_eaton
        bytes_per_line = atoi(argv[bytes_per_line_index]);
89 106 jt_eaton
 
90
 
91
 
92
 
93
        if(argc > 3) {
94
        min_num_bytes = atoi(argv[min_num_bytes_index]);
95
        }
96
 
97
 
98
 
99 101 jt_eaton
 
100
        if ((bytes_per_line == 0) || (bytes_per_line > 8))
101
          {
102
            fprintf(stderr,"bytes per line incorrect or missing: %s\n",argv[bytes_per_line_index]);
103
            exit(1);
104
          }
105
 
106 106 jt_eaton
 
107
 
108
 
109
 
110
 
111
 
112
 
113 101 jt_eaton
 
114
        if (fd == NULL) {
115
                fprintf(stderr,"failed to open input file: %s\n",argv[1]);
116
                exit(1);
117
        }
118
 
119
        i=0;
120 106 jt_eaton
        line=0;
121 101 jt_eaton
 
122
        // Now write out the binary data to hex format
123
        while ((c = fgetc(fd)) != EOF) {
124
                printf("%.2x", (unsigned int) c);
125
                if (++i == bytes_per_line) {
126
                        printf("\n");
127 106 jt_eaton
                        line++;
128 101 jt_eaton
                        i = 0;
129
                }
130
        }
131
 
132 106 jt_eaton
 
133
        while (i) {
134
                printf("00");
135
                if (++i == bytes_per_line) {
136
                        printf("\n");
137
                        line++;
138
                        i = 0;
139
                }
140
        }
141
 
142
 
143
 
144
        while (line < min_num_bytes) {
145
                printf("00000000\n");
146
                line++;
147
        }
148
 
149
 
150
 
151
 
152
 
153 101 jt_eaton
        return 0;
154
}

powered by: WebSVN 2.1.0

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