| 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 | 46 | julius |         int bytes_per_word = BYTES_PER_WORD;
 | 
      
         | 85 | 6 | julius |  
 | 
      
         | 86 |  |  |         // Counters keeping track of what we've printed
 | 
      
         | 87 |  |  |         int current_addr = 0;
 | 
      
         | 88 |  |  |         int word_counter = 0;
 | 
      
         | 89 |  |  |         int byte_counter = 0;
 | 
      
         | 90 |  |  |  
 | 
      
         | 91 |  |  |         if(argc < 2) {
 | 
      
         | 92 |  |  |           fprintf(stderr,"\n\tInsufficient options.\n");
 | 
      
         | 93 |  |  |           fprintf(stderr,"\tPlease specify a binary file to convert to VMEM\n");
 | 
      
         | 94 |  |  |           fprintf(stderr,"\n\tbin2vmem - creates vmem output to stdout from bin\n");
 | 
      
         | 95 | 45 | julius |           fprintf(stderr,"\n\tBy default the output is word addressed 32-bit words\n");
 | 
      
         | 96 |  |  |           fprintf(stderr,"\tSpecify -synfmt on the command line after the filename\n");
 | 
      
         | 97 |  |  |           fprintf(stderr,"\tto output in the alterative format, which is a simple\n");
 | 
      
         | 98 | 46 | julius |           fprintf(stderr,"\tlist of the data words. The default bytes per word is 4.\n");
 | 
      
         | 99 | 45 | julius |  
 | 
      
         | 100 |  |  |           fprintf(stderr,"\n");
 | 
      
         | 101 | 46 | julius |           fprintf(stderr,"\tAdditionally, to specify the bytes per word (per line)\n");
 | 
      
         | 102 |  |  |           fprintf(stderr,"\twhen specifying the -synfmt simple list format, use the\n");
 | 
      
         | 103 |  |  |           fprintf(stderr,"\tswitch -bpw=N after specifying the -synfmt option. Example:\n");
 | 
      
         | 104 |  |  |           fprintf(stderr,"\t\t./bin2vmem prog.bin -synfmt -bpw=2 > prog.vmem\n");
 | 
      
         | 105 |  |  |           fprintf(stderr,"\n");
 | 
      
         | 106 | 6 | julius |           exit(1);
 | 
      
         | 107 |  |  |         }
 | 
      
         | 108 |  |  |  
 | 
      
         | 109 | 45 | julius |         fd = fopen( argv[FILENAME_CMDLINE_INDEX], "r" );
 | 
      
         | 110 |  |  |  
 | 
      
         | 111 |  |  |         if (argc > 2) // check for the -synfmt switch
 | 
      
         | 112 |  |  |           {
 | 
      
         | 113 |  |  |             if (strcmp("-synfmt", argv[FMT_CMDLINE_INDEX]) == 0)
 | 
      
         | 114 | 46 | julius |               {
 | 
      
         | 115 |  |  |                 output_fmt = FMT_SYN; // synthesis friendly format - single column, no addr
 | 
      
         | 116 |  |  |                 int bytes_per_word_tmp;
 | 
      
         | 117 |  |  |                 if (argc > 3) // Check for extra bytes per word option
 | 
      
         | 118 |  |  |                   if (1 == sscanf(argv[FMT_CMDLINE_INDEX+1], "-bpw=%d", &bytes_per_word_tmp))
 | 
      
         | 119 |  |  |                     {
 | 
      
         | 120 |  |  |                       if(bytes_per_word_tmp < 4 && bytes_per_word_tmp > 0)
 | 
      
         | 121 |  |  |                         {
 | 
      
         | 122 |  |  |                           //printf("# Bytes per word: %d\n", bytes_per_word_tmp);
 | 
      
         | 123 |  |  |                           bytes_per_word = bytes_per_word_tmp;
 | 
      
         | 124 |  |  |                         }
 | 
      
         | 125 |  |  |  
 | 
      
         | 126 |  |  |                     }
 | 
      
         | 127 |  |  |               }
 | 
      
         | 128 |  |  |  
 | 
      
         | 129 | 45 | julius |           }
 | 
      
         | 130 |  |  |  
 | 
      
         | 131 | 6 | julius |         if (fd == NULL) {
 | 
      
         | 132 |  |  |                 fprintf(stderr,"failed to open input file: %s\n",argv[1]);
 | 
      
         | 133 |  |  |                 exit(1);
 | 
      
         | 134 |  |  |         }
 | 
      
         | 135 |  |  |  
 | 
      
         | 136 |  |  |         fseek(fd, 0, SEEK_END);
 | 
      
         | 137 |  |  |         image_size = ftell(fd);
 | 
      
         | 138 |  |  |         fseek(fd,0,SEEK_SET);
 | 
      
         | 139 |  |  |  
 | 
      
         | 140 |  |  |         if (write_size_word)
 | 
      
         | 141 |  |  |           {
 | 
      
         | 142 |  |  |             // or1200 startup method of determining size of boot image we're copying by reading out
 | 
      
         | 143 |  |  |             // the very first word in flash is used. Determine the length of this file
 | 
      
         | 144 |  |  |             fseek(fd, 0, SEEK_END);
 | 
      
         | 145 |  |  |             image_size = ftell(fd);
 | 
      
         | 146 |  |  |             fseek(fd,0,SEEK_SET);
 | 
      
         | 147 |  |  |  
 | 
      
         | 148 |  |  |             // Now we should have the size of the file in bytes. Let's ensure it's a word multiple
 | 
      
         | 149 |  |  |             image_size+=3;
 | 
      
         | 150 |  |  |             image_size &= 0xfffffffc;
 | 
      
         | 151 |  |  |  
 | 
      
         | 152 |  |  |             // Sanity check on image size
 | 
      
         | 153 |  |  |             if (image_size < 8){
 | 
      
         | 154 |  |  |               fprintf(stderr, "Bad binary image. Size too small\n");
 | 
      
         | 155 |  |  |               return 1;
 | 
      
         | 156 |  |  |             }
 | 
      
         | 157 |  |  |  
 | 
      
         | 158 |  |  |             // Now write out the image size
 | 
      
         | 159 |  |  |             printf("@%8x", current_addr);
 | 
      
         | 160 |  |  |             printf("%8x", image_size);
 | 
      
         | 161 | 46 | julius |             current_addr += WORDS_PER_LINE * bytes_per_word;
 | 
      
         | 162 | 6 | julius |           }
 | 
      
         | 163 |  |  |  
 | 
      
         | 164 |  |  |  
 | 
      
         | 165 | 45 | julius |         // Fix for the current bootloader software! Skip the first 4 
 | 
      
         | 166 |  |  |         // bytes of application data. Hopefully it's not important. 030509 -- jb
 | 
      
         | 167 | 6 | julius |         //for(i=0;i<4;i++)
 | 
      
         | 168 |  |  |         //  c=fgetc(fd);
 | 
      
         | 169 |  |  |         i=0;
 | 
      
         | 170 |  |  |         int starting_new_line  = 1;
 | 
      
         | 171 | 45 | julius |         // Now write out the binary data to specified format. Either
 | 
      
         | 172 |  |  |         // more complicated, addressed format:
 | 
      
         | 173 |  |  |         // VMEM format: @ADDRESSS XXXXXXXX XXXXXXXX XXXXXXXX XXXXXXXX
 | 
      
         | 174 |  |  |         // or simple, synplifyfriendly format which is just a list of
 | 
      
         | 175 |  |  |         // the words
 | 
      
         | 176 |  |  |  
 | 
      
         | 177 | 6 | julius |         while ((c = fgetc(fd)) != EOF) {
 | 
      
         | 178 |  |  |  
 | 
      
         | 179 | 45 | julius |           if (output_fmt == FMT_WITH_ADDR) // Default format
 | 
      
         | 180 | 6 | julius |             {
 | 
      
         | 181 | 45 | julius |  
 | 
      
         | 182 |  |  |               if (starting_new_line)
 | 
      
         | 183 |  |  |                 {
 | 
      
         | 184 |  |  |                   // New line - print the current addr and then increment it
 | 
      
         | 185 |  |  |                   printf("@%.8x", current_addr);
 | 
      
         | 186 |  |  |                   current_addr += WORDS_PER_LINE;
 | 
      
         | 187 |  |  |                   starting_new_line = 0;
 | 
      
         | 188 |  |  |                 }
 | 
      
         | 189 |  |  |               if (byte_counter == 0)
 | 
      
         | 190 |  |  |                 printf(" ");
 | 
      
         | 191 |  |  |  
 | 
      
         | 192 |  |  |               printf("%.2x", (unsigned int) c); // now print the actual char
 | 
      
         | 193 |  |  |  
 | 
      
         | 194 |  |  |               byte_counter++;
 | 
      
         | 195 |  |  |  
 | 
      
         | 196 | 46 | julius |               if (byte_counter == bytes_per_word)
 | 
      
         | 197 | 45 | julius |                 {
 | 
      
         | 198 |  |  |                   word_counter++;
 | 
      
         | 199 |  |  |                   byte_counter=0;
 | 
      
         | 200 |  |  |                 }
 | 
      
         | 201 |  |  |               if (word_counter == WORDS_PER_LINE)
 | 
      
         | 202 |  |  |                 {
 | 
      
         | 203 |  |  |                   printf("\n");
 | 
      
         | 204 |  |  |                   word_counter = 0;
 | 
      
         | 205 |  |  |                   starting_new_line = 1;
 | 
      
         | 206 |  |  |                 }
 | 
      
         | 207 |  |  |             } // End of FMT_WITH_ADDR
 | 
      
         | 208 |  |  |           else if (output_fmt == FMT_SYN) // simple list of data words
 | 
      
         | 209 | 6 | julius |             {
 | 
      
         | 210 | 45 | julius |               printf("%.2x", (unsigned int) c); // now print the actual char
 | 
      
         | 211 |  |  |               byte_counter++;
 | 
      
         | 212 | 46 | julius |               if (byte_counter == bytes_per_word)
 | 
      
         | 213 | 45 | julius |                 {
 | 
      
         | 214 |  |  |                   printf("\n");
 | 
      
         | 215 |  |  |                   byte_counter=0;
 | 
      
         | 216 |  |  |                 }
 | 
      
         | 217 | 6 | julius |             }
 | 
      
         | 218 | 45 | julius |  
 | 
      
         | 219 |  |  |  
 | 
      
         | 220 | 6 | julius |         }
 | 
      
         | 221 | 45 | julius |  
 | 
      
         | 222 | 6 | julius |         return 0;
 | 
      
         | 223 |  |  | }
 |