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

Subversion Repositories amber

[/] [amber/] [trunk/] [sw/] [tools/] [amber-bin2mem.c] - Blame information for rev 67

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 2 csantifort
/*----------------------------------------------------------------
2
//                                                              //
3 11 csantifort
//  amber-bin2mem.c                                             //
4 2 csantifort
//                                                              //
5
//  This file is part of the Amber project                      //
6
//  http://www.opencores.org/project,amber                      //
7
//                                                              //
8
//  Description                                                 //
9 11 csantifort
//  Read in a binary file and write it out in                   //
10
//  in Verilog readmem format.                                  //
11 2 csantifort
//                                                              //
12
//  Author(s):                                                  //
13
//      - Conor Santifort, csantifort.amber@gmail.com           //
14
//                                                              //
15
//////////////////////////////////////////////////////////////////
16
//                                                              //
17
// Copyright (C) 2010 Authors and OPENCORES.ORG                 //
18
//                                                              //
19
// This source file may be used and distributed without         //
20
// restriction provided that this copyright statement is not    //
21
// removed from the file and that any derivative work contains  //
22
// the original copyright notice and the associated disclaimer. //
23
//                                                              //
24
// This source file is free software; you can redistribute it   //
25
// and/or modify it under the terms of the GNU Lesser General   //
26
// Public License as published by the Free Software Foundation; //
27
// either version 2.1 of the License, or (at your option) any   //
28
// later version.                                               //
29
//                                                              //
30
// This source is distributed in the hope that it will be       //
31
// useful, but WITHOUT ANY WARRANTY; without even the implied   //
32
// warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR      //
33
// PURPOSE.  See the GNU Lesser General Public License for more //
34
// details.                                                     //
35
//                                                              //
36
// You should have received a copy of the GNU Lesser General    //
37
// Public License along with this source; if not, download it   //
38
// from http://www.opencores.org/lgpl.shtml                     //
39
//                                                              //
40
----------------------------------------------------------------*/
41
 
42
 
43
#include <stdio.h>
44
#include <stdlib.h>
45 67 csantifort
#include "../boot-loader-serial/boot-loader.h"
46 2 csantifort
 
47 11 csantifort
/* Function prototypes */
48 2 csantifort
int fsize(FILE *f);
49 11 csantifort
int conv_hstring(char*, unsigned int*);
50 2 csantifort
 
51
 
52
int main(int argc,char *argv[])
53
{
54
   FILE *infile;
55
   char filename_mem[80], filename_nopath[80];
56
   unsigned char *buf;
57
   FILE *file_mem;
58
   int infile_size, buf_size, i, j;
59 11 csantifort
   int mem_start = FILE_LOAD_BASE;
60
   int ret;
61 2 csantifort
 
62
   if (argc<2){
63 11 csantifort
      fprintf(stderr, "ERROR: no input file specified. Quitting\n");
64 2 csantifort
      exit(1);
65
      }
66
 
67 11 csantifort
 
68 2 csantifort
   infile=fopen(argv[1],"rb");
69
   if(infile==NULL) {
70 11 csantifort
      fprintf(stderr, "ERROR: Can't open %s. Quitting\n", argv[1]);
71 2 csantifort
      exit(1);
72
   }
73
   infile_size = fsize(infile);
74 11 csantifort
 
75
    /* Get the starting offset in mem */
76
   if (argc==3){
77
      if (ret = conv_hstring(argv[2], &mem_start) < 3) {
78
         fprintf(stderr, "ERROR: Parsing address offset %s Quitting\n", argv[2]);
79
         exit(1);
80
         }
81
      }
82 2 csantifort
 
83
   buf=(unsigned char*)malloc(infile_size);
84
   buf_size=fread(buf,1,infile_size,infile);
85
   fclose(infile);
86
 
87
   if ( buf_size != infile_size ) {
88
      fprintf(stderr, "%s ERROR: Input %s file length is %d bytes long, buffer read buf_size %d\n",
89
      argv[0], argv[1], infile_size, buf_size);
90
      exit(1);
91
      }
92
 
93
 
94
   if ( infile_size > 0x1000000 ) {
95
      fprintf(stderr, "%s WARNING: Input %s file length is %d bytes long, greater than boot-loader can handle \n",
96
      argv[0], argv[1], infile_size);
97
      }
98
 
99
   for (i=0;i<buf_size;i+=4) {
100 11 csantifort
        printf("@%08x %02x%02x%02x%02x\n", mem_start + i,
101 2 csantifort
                       buf[i+3], buf[i+2], buf[i+1], buf[i+0]);
102
        }
103
 
104
   free(buf);
105
 
106
   return 0;
107
}
108
 
109
 
110
 
111
/* Return the buf_size of a file in bytes */
112
int fsize( FILE *f )
113
{
114
    int end;
115
 
116
    /* Set current position at end of file */
117
    fseek( f, 0, SEEK_END );
118
 
119
    /* File buf_size in bytes */
120
    end = ftell( f );
121
 
122
    /* Set current position at start of file */
123
    fseek( f, 0, SEEK_SET );
124
 
125
    return end;
126
}
127
 
128
 
129 11 csantifort
int conv_hstring ( char *string, unsigned int * num)
130
{
131
int pos = 0;
132
*num = 0;
133 2 csantifort
 
134 11 csantifort
while (((string[pos] >= '0' && string[pos] <= '9') ||
135
       (string[pos] >= 'a' && string[pos] <= 'f')) && pos < 9) {
136
    if (string[pos] >= '0' && string[pos] <= '9')
137
        *num =  (*num << 4) + ( string[pos++] - '0' );
138
    else
139
        *num =  (*num << 4) + ( string[pos++] - 'a' ) + 10;
140
    }
141
 
142
return pos;
143
}
144
 

powered by: WebSVN 2.1.0

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