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

Subversion Repositories mips32r1

[/] [mips32r1/] [trunk/] [Software/] [demos/] [util/] [bintohex.c] - Blame information for rev 2

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 2 ayersg
/*
2
 * File          : bintohex.c
3
 * Project       : University of Utah, XUM Project
4
 * Creator(s)    : Grant Ayers (ayers@cs.utah.edu)
5
 *
6
 * Modification History:
7
 *   Rev   Date         Initials  Description of Change
8
 *   1.0   4-1-2011     GEA       Initial design.
9
 *
10
 * Standards/Formatting:
11
 *   C, 8 hard tab, 80 column
12
 *
13
 * Description:
14
 *   Converts binary data into human-readable hex data.
15
 *   This is useful for FPGA block RAM initialization data,
16
 *   which is typically read in from a file in hex format.
17
 *   For block RAM cores, a .COE file is required, which is
18
 *   basically a hex file with some additional syntax. This
19
 *   utility will output in either format and can pad with
20
 *   zeros to a certain length.
21
 */
22
#include <stdio.h>
23
#include <stdlib.h>
24
#include <unistd.h>
25
 
26
 
27
void usage(void);
28
unsigned int doEndian(unsigned int val, int bigEndian);
29
 
30
int main(int argc, char **argv)
31
{
32
        FILE *file;
33
        char *i_name, *o_name;
34
        int  i_size;
35
        unsigned int *input;
36
        unsigned int data = 0;
37
        int  pad_length = 0;
38
        int  endian = -1;
39
        int  coe = 0;
40
        int  ch, i;
41
 
42
        while ((ch = getopt(argc, argv, "chbp:")) != -1)
43
        {
44
                switch (ch)
45
                {
46
                        case 'c':
47
                                coe = 1;
48
                                break;
49
                        case 'h':
50
                                usage();
51
                                break;
52
                        case 'b':
53
                                endian = 1;
54
                                break;
55
                        case 'p':
56
                                pad_length = (int)strtol(optarg,
57
                                        (char **)NULL, 10);
58
                                break;
59
                        default:
60
                                usage();
61
                }
62
        }
63
 
64
        argc -= optind;
65
        argv += optind;
66
 
67
        if (argc != 2)
68
        {
69
                usage();
70
        }
71
 
72
        i_name = argv[0];
73
        o_name = argv[1];
74
 
75
 
76
        /* Read the input file */
77
        file = fopen(i_name, "rb");
78
        if (file == NULL) {
79
                fprintf(stderr, "Error: Could not open \"%s\".\n", i_name);
80
                exit(1);
81
        }
82
        fseek(file, 0L, SEEK_END);
83
        i_size = (int)ftell(file);
84
        if ((i_size < 0) || (ftell(file) > (long)i_size)) {
85
                fprintf(stderr, "Error: Input file is too large.\n");
86
                exit(1);
87
        }
88
        fseek(file, 0L, SEEK_SET);
89
        input = (unsigned int*)malloc(i_size);
90
        if (input == NULL) {
91
                fprintf(stderr, "Error: Could not allocate %d bytes of "
92
                        "memory.\n", i_size);
93
                exit(1);
94
        }
95
        if (fread(input, 1, i_size, file) != i_size) {
96
                fprintf(stderr, "Error reading input file.\n");
97
                exit(1);
98
        }
99
        fclose(file);
100
 
101
        /* Write the output file */
102
        file = fopen(o_name, "wb+");
103
        if (file == NULL) {
104
                fprintf(stderr, "Error: Could not open \"%s\" for "
105
                        "writing.\n", o_name);
106
                exit(1);
107
        }
108
        if (coe) {
109
                fprintf(file, "memory_initialization_radix=16;\n"
110
                        "memory_initialization_vector=\n");
111
        }
112
        for (i=0; i<(i_size/4); i++) {
113
                if (i != 0) {
114
                        if (coe) {
115
                                fprintf(file, ",\n");
116
                        }
117
                        else {
118
                                fprintf(file, "\n");
119
                        }
120
                }
121
                fprintf(file, "%08x", doEndian(input[i], endian));
122
        }
123
        if (coe) {
124
                fprintf(file, ";\n");
125
        }
126
        else {
127
                fprintf(file, "\n");
128
        }
129
        for (i=((i_size/4)*4); i<(i_size); i++) {
130
                if (endian < 0) {
131
                        data <<= 8;
132
                        data |= (0x000000FF & ((char*)input)[i]);
133
                }
134
                else {
135
                        data >>= 8;
136
                        data |= (0xFF000000 & (((char*)input)[i] << 24));
137
                }
138
        }
139
        if ((i_size%4) != 0) {
140
                if (coe) {
141
                        fprintf(file, "%08x;\n", data);
142
                }
143
                else {
144
                        fprintf(file, "%08x\n", data);
145
                }
146
        }
147
 
148
        /* Pad the output for non-COE files */
149
        if ((pad_length > 0) && !coe) {
150
                ch = (i_size/4) + (((i_size%4) != 0) ? 1 : 0);
151
                for (i=ch; i<pad_length; i++) {
152
                        fprintf(file, "00000000\n");
153
                }
154
        }
155
 
156
        fclose(file);
157
 
158
        return 0;
159
}
160
 
161
void usage(void)
162
{
163
        printf("Usage: bintohex [-p <pad length>] [-b (Big Endian)] "
164
                "[-c (Make COE file)] <input> <output>\n");
165
        exit(1);
166
}
167
 
168
unsigned int doEndian(unsigned int val, int bigEndian)
169
{
170
        if (bigEndian == 1) {
171
                return (((val >> 24)&0xff) | ((val<<8)&0xff0000) |
172
                        ((val>>8)&0xff00) | ((val<<24)&0xff000000));
173
        }
174
        else {
175
                return val;
176
        }
177
}
178
 

powered by: WebSVN 2.1.0

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