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

Subversion Repositories openrisc

[/] [openrisc/] [trunk/] [orpsocv2/] [sw/] [utils/] [bin2vmem.c] - Blame information for rev 45

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 6 julius
/*$$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                      : bin2vmem.c
10
// Prepared By                    : jb, jb@orsoc.se
11
// Project Start                  : 2009-05-13
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 VMEM output to stdout from binary images.
44
// Use with redirection like: ./bin2vmem app.bin > app.vmem
45
// To change either the number of bytes per word or word per line, change
46
// the following defines.
47
// Currently output is WORD addressed, NOT byte addressed
48
// eg: @00000000 00000000 00000000 00000000 00000000
49
//     @00000004 00000000 00000000 00000000 00000000
50
//     @00000008 00000000 00000000 00000000 00000000
51
//     @0000000c 00000000 00000000 00000000 00000000
52
//     etc..
53
//
54 45 julius
// OR
55
// 
56
// Output a list of the words, one per line, as Synplify appears to like
57
// specify this option with the -synfmt switch on the command line after
58
// the input file
59
// eg: ./bin2vmem data.bin -synfmt > data.vmem
60
//
61 6 julius
 
62
#define WORDS_PER_LINE 4
63
#define BYTES_PER_WORD 4
64
 
65 45 julius
#define FILENAME_CMDLINE_INDEX 1
66
#define FMT_CMDLINE_INDEX 2
67
 
68
#define FMT_WITH_ADDR 0
69
#define FMT_SYN 1
70
 
71 6 julius
#include <stdio.h>
72
#include <stdlib.h>
73
#include <string.h>
74
 
75
int main(int argc, char **argv)
76
{
77
 
78
        FILE  *fd;
79
        int c;
80
        int i = 0;
81
        int write_size_word=0; // Disabled by default
82
        unsigned int image_size;
83 45 julius
        int output_fmt = FMT_WITH_ADDR; // 0 - standard 4 per line with address, 1 - synfmt
84 6 julius
 
85
        // Counters keeping track of what we've printed
86
        int current_addr = 0;
87
        int word_counter = 0;
88
        int byte_counter = 0;
89
 
90
        if(argc < 2) {
91
          fprintf(stderr,"\n\tInsufficient options.\n");
92
          fprintf(stderr,"\tPlease specify a binary file to convert to VMEM\n");
93
          fprintf(stderr,"\n\tbin2vmem - creates vmem output to stdout from bin\n");
94 45 julius
          fprintf(stderr,"\n\tBy default the output is word addressed 32-bit words\n");
95
          fprintf(stderr,"\tSpecify -synfmt on the command line after the filename\n");
96
          fprintf(stderr,"\tto output in the alterative format, which is a simple\n");
97
          fprintf(stderr,"\tlist of the data words.\n");
98
 
99
          fprintf(stderr,"\n");
100 6 julius
          exit(1);
101
        }
102
 
103 45 julius
        fd = fopen( argv[FILENAME_CMDLINE_INDEX], "r" );
104
 
105
        if (argc > 2) // check for the -synfmt switch
106
          {
107
            if (strcmp("-synfmt", argv[FMT_CMDLINE_INDEX]) == 0)
108
              output_fmt = FMT_SYN; // synthesis friendly format - single column, no addr
109
          }
110
 
111 6 julius
        if (fd == NULL) {
112
                fprintf(stderr,"failed to open input file: %s\n",argv[1]);
113
                exit(1);
114
        }
115
 
116
        fseek(fd, 0, SEEK_END);
117
        image_size = ftell(fd);
118
        fseek(fd,0,SEEK_SET);
119
 
120
        if (write_size_word)
121
          {
122
            // or1200 startup method of determining size of boot image we're copying by reading out
123
            // the very first word in flash is used. Determine the length of this file
124
            fseek(fd, 0, SEEK_END);
125
            image_size = ftell(fd);
126
            fseek(fd,0,SEEK_SET);
127
 
128
            // Now we should have the size of the file in bytes. Let's ensure it's a word multiple
129
            image_size+=3;
130
            image_size &= 0xfffffffc;
131
 
132
            // Sanity check on image size
133
            if (image_size < 8){
134
              fprintf(stderr, "Bad binary image. Size too small\n");
135
              return 1;
136
            }
137
 
138
            // Now write out the image size
139
            printf("@%8x", current_addr);
140
            printf("%8x", image_size);
141
            current_addr += WORDS_PER_LINE * BYTES_PER_WORD;
142
          }
143
 
144
 
145 45 julius
        // Fix for the current bootloader software! Skip the first 4 
146
        // bytes of application data. Hopefully it's not important. 030509 -- jb
147 6 julius
        //for(i=0;i<4;i++)
148
        //  c=fgetc(fd);
149
        i=0;
150
        int starting_new_line  = 1;
151 45 julius
        // Now write out the binary data to specified format. Either
152
        // more complicated, addressed format:
153
        // VMEM format: @ADDRESSS XXXXXXXX XXXXXXXX XXXXXXXX XXXXXXXX
154
        // or simple, synplifyfriendly format which is just a list of
155
        // the words
156
 
157 6 julius
        while ((c = fgetc(fd)) != EOF) {
158
 
159 45 julius
          if (output_fmt == FMT_WITH_ADDR) // Default format
160 6 julius
            {
161 45 julius
 
162
              if (starting_new_line)
163
                {
164
                  // New line - print the current addr and then increment it
165
                  printf("@%.8x", current_addr);
166
                  current_addr += WORDS_PER_LINE;
167
                  starting_new_line = 0;
168
                }
169
              if (byte_counter == 0)
170
                printf(" ");
171
 
172
              printf("%.2x", (unsigned int) c); // now print the actual char
173
 
174
              byte_counter++;
175
 
176
              if (byte_counter == BYTES_PER_WORD)
177
                {
178
                  word_counter++;
179
                  byte_counter=0;
180
                }
181
              if (word_counter == WORDS_PER_LINE)
182
                {
183
                  printf("\n");
184
                  word_counter = 0;
185
                  starting_new_line = 1;
186
                }
187
            } // End of FMT_WITH_ADDR
188
          else if (output_fmt == FMT_SYN) // simple list of data words
189 6 julius
            {
190 45 julius
              printf("%.2x", (unsigned int) c); // now print the actual char
191
              byte_counter++;
192
              if (byte_counter == BYTES_PER_WORD)
193
                {
194
                  printf("\n");
195
                  byte_counter=0;
196
                }
197 6 julius
            }
198 45 julius
 
199
 
200 6 julius
        }
201 45 julius
 
202 6 julius
        return 0;
203
}

powered by: WebSVN 2.1.0

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