OpenCores
URL https://opencores.org/ocsvn/an-fpga-implementation-of-low-latency-noc-based-mpsoc/an-fpga-implementation-of-low-latency-noc-based-mpsoc/trunk

Subversion Repositories an-fpga-implementation-of-low-latency-noc-based-mpsoc

[/] [an-fpga-implementation-of-low-latency-noc-based-mpsoc/] [trunk/] [mpsoc/] [src_c/] [bin2mem/] [main.c] - Blame information for rev 48

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 48 alirezamon
 
2
#include <stdio.h>
3
#include <unistd.h>
4
#include <stdlib.h>
5
#include <ctype.h>
6
 
7
#define DEFAULT_OUT_FILE_NAME           "out0.mem"
8
 
9
 
10
char *in_file_name,  *out_file_name;
11
int mem_width = 32;
12
 
13
 
14
void usage (void)
15
{
16
        printf("Usage: ./bin2mem  <options>  \n");
17
        printf("\nOptions: \n");
18
        printf("         -w <file name>: memory width in bits.\n");
19
        printf("         -f <file name>: input bin file name.\n");
20
        printf("         -o <file name>: output mif file name.\n");
21
}
22
 
23
 
24
void processArgs (int argc, char **argv )
25
{
26
   char c;
27
 
28
     opterr = 0;
29
 
30
   while ((c = getopt (argc, argv, "f:o:hw:")) != -1)
31
      {
32
         switch (c)
33
            {
34
 
35
                case 'f':
36
                        in_file_name = optarg;
37
                        break;
38
                case 'o':
39
                        out_file_name =  optarg;
40
                        break;
41
            case 'w':
42
                sscanf(optarg, "%u", &mem_width);
43
                        break;
44
                case 'h':
45
                        usage();
46
                        exit(1);
47
                        break;
48
                case '?':
49
                  if (isprint (optopt))
50
                          fprintf (stderr, "Unknown option `-%c'.\n", optopt);
51
                  else
52
                          fprintf (stderr,   "Unknown option character `\\x%x'.\n",   optopt);
53
                default:
54
                        usage();
55
                        exit(1);
56
            }
57
      }
58
}
59
 
60
 
61
 
62
 
63
int main ( int argc, char **argv ){
64
         FILE *fin, *fout;
65
         unsigned int sz_byte;
66
         unsigned int sz_word;
67
         int c;
68
         char buff[100];
69
 
70
        processArgs (argc,argv );
71
        unsigned int byte_num_in_word = (mem_width/8);
72
 
73
        if (in_file_name == NULL) {usage();exit(1);}
74
        if (out_file_name == NULL) out_file_name = DEFAULT_OUT_FILE_NAME;
75
 
76
        fin = fopen(in_file_name, "rb");
77
        if (fin == NULL)
78
    {
79
      printf("Error while opening the file %s.\n",in_file_name);
80
      exit(EXIT_FAILURE);
81
    }
82
    fout=fopen(out_file_name,"wb");
83
        if(fout==NULL){printf("Erro Output file cannot be created %s\n",out_file_name); exit(1);}
84
 
85
 
86
        //get bin file size
87
        fseek(fin, 0L, SEEK_END);
88
        sz_byte = ftell(fin);
89
        sz_word = (sz_byte % byte_num_in_word)==0 ? sz_byte/byte_num_in_word : sz_byte/byte_num_in_word +1;
90
        fseek(fin, 0L, SEEK_SET);
91
 
92
 
93
        //fprintf(fout,"-- Copyright (C) 2013 Alireza Monemi\n\n");
94
        //fprintf(fout,"WIDTH=%u;\nDEPTH=%d;\nADDRESS_RADIX=HEX;\nDATA_RADIX=HEX;\n\nCONTENT BEGIN\n", mem_width,sz_word);
95
        int i,j;
96
        for(i=0;i<sz_word;i++){
97
                fprintf(fout,"@%X ", i);
98
                for(j=0;j<byte_num_in_word;j++){
99
                        buff[(j+1)*2]=0;
100
                        c=fgetc(fin);
101
                        if( feof(fin) ) {
102
                                break ;
103
                        }
104
                        sprintf(buff+(j*2),"%02X" , c);
105
                }
106
            fprintf(fout,"%s ",buff);
107
        }
108
 
109
 
110
        //fprintf(fout,"END;\n");
111
 
112
        fclose(fin);
113
        fclose(fout);
114
 
115
 
116
}
117
 

powered by: WebSVN 2.1.0

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