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

Subversion Repositories minsoc

[/] [minsoc/] [branches/] [rc-1.0/] [sw/] [utils/] [bin2vmem.c] - Blame information for rev 130

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

Line No. Rev Author Line
1 2 rfajardo
/*$$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
 
55
#define WORDS_PER_LINE 4
56
#define BYTES_PER_WORD 4
57
 
58
#include <stdio.h>
59
#include <stdlib.h>
60
#include <string.h>
61
 
62
int main(int argc, char **argv)
63
{
64
 
65
        FILE  *fd;
66
        int c;
67
        int i = 0;
68
        int write_size_word=0; // Disabled by default
69
        int filename_index=1;
70
        unsigned int image_size;
71
 
72
        // Counters keeping track of what we've printed
73
        int current_addr = 0;
74
        int word_counter = 0;
75
        int byte_counter = 0;
76
 
77
        if(argc < 2) {
78
          fprintf(stderr,"\n\tInsufficient options.\n");
79
          fprintf(stderr,"\tPlease specify a binary file to convert to VMEM\n");
80
          fprintf(stderr,"\n\tbin2vmem - creates vmem output to stdout from bin\n");
81
          exit(1);
82
        }
83
 
84
        fd = fopen( argv[filename_index], "r" );
85
 
86
        if (fd == NULL) {
87
                fprintf(stderr,"failed to open input file: %s\n",argv[1]);
88
                exit(1);
89
        }
90
 
91
        fseek(fd, 0, SEEK_END);
92
        image_size = ftell(fd);
93
        fseek(fd,0,SEEK_SET);
94
 
95
        if (write_size_word)
96
          {
97
            // or1200 startup method of determining size of boot image we're copying by reading out
98
            // the very first word in flash is used. Determine the length of this file
99
            fseek(fd, 0, SEEK_END);
100
            image_size = ftell(fd);
101
            fseek(fd,0,SEEK_SET);
102
 
103
            // Now we should have the size of the file in bytes. Let's ensure it's a word multiple
104
            image_size+=3;
105
            image_size &= 0xfffffffc;
106
 
107
            // Sanity check on image size
108
            if (image_size < 8){
109
              fprintf(stderr, "Bad binary image. Size too small\n");
110
              return 1;
111
            }
112
 
113
            // Now write out the image size
114
            printf("@%8x", current_addr);
115
            printf("%8x", image_size);
116
            current_addr += WORDS_PER_LINE * BYTES_PER_WORD;
117
          }
118
        else
119
          {
120
          }
121
 
122
 
123
        // Fix for the current bootloader software! Skip the first 4 bytes of application data. Hopefully it's not important. 030509 -- jb
124
        //for(i=0;i<4;i++)
125
        //  c=fgetc(fd);
126
        i=0;
127
        int starting_new_line  = 1;
128
        // Now write out the binary data to VMEM format: @ADDRESSS XXXXXXXX XXXXXXXX XXXXXXXX XXXXXXXX
129
        while ((c = fgetc(fd)) != EOF) {
130
          if (starting_new_line)
131
            {
132
              // New line - print the current addr and then increment it
133
              printf("@%.8x", current_addr);
134
              //current_addr += WORDS_PER_LINE * BYTES_PER_WORD;
135
              current_addr += WORDS_PER_LINE;
136
              starting_new_line = 0;
137
            }
138
          if (byte_counter == 0)
139
            printf(" ");
140
 
141
          printf("%.2x", (unsigned int) c); // now print the actual char
142
 
143
          byte_counter++;
144
 
145
          if (byte_counter == BYTES_PER_WORD)
146
            {
147
              word_counter++;
148
              byte_counter=0;
149
            }
150
          if (word_counter == WORDS_PER_LINE)
151
            {
152
              printf("\n");
153
              word_counter = 0;
154
              starting_new_line = 1;
155
            }
156
        }
157
 
158
        return 0;
159
}

powered by: WebSVN 2.1.0

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