]> rtime.felk.cvut.cz Git - fpga/lx-cpu1/tumbl.git/blob - utils/bin2mem.c
Add scripts and programs for firmware
[fpga/lx-cpu1/tumbl.git] / utils / bin2mem.c
1 /* Converts bin file to mem file */
2
3 #include <stdio.h>
4 #include <string.h>
5
6 void print_help(char * name)
7 {
8         fprintf(stderr, "%s converts a binary file into a mem-file\n", name);
9         fprintf(stderr, "Usage: %s INFILE OUTFILE\n", name);
10 }
11
12 int main(int argc, char *argv[])
13 {
14         FILE *infile, *outfile;
15         int whi, wlo, bhi, blo;
16         unsigned int addr;
17
18         if (argc != 3)
19         {
20                 print_help(argv[0]);
21                 return(1);
22         }
23
24         infile = fopen(argv[1], "rb");
25         if (!infile)
26         {
27                 printf("Cannot open file %s\n", argv[1]);
28                 return(1);
29         }
30
31         outfile = fopen(argv[2], "w");
32         if (!outfile)
33         {
34                 printf("Cannot open file %s\n", argv[2]);
35                 return(1);
36         }
37
38         fprintf(outfile,"// memory data file (do not edit the following line - required for mem load use)\n"
39                         "// format=hex addressradix=h dataradix=h version=1.0 wordsperline=1\n");
40
41         addr = 0;
42         while ((whi = fgetc(infile)) != EOF)
43         {
44                 fprintf(outfile,"@%x ", addr);
45
46                 if ((wlo = fgetc(infile)) == EOF)
47                         break;
48                 if ((bhi = fgetc(infile)) == EOF)
49                         break;
50                 if ((blo = fgetc(infile)) == EOF)
51                         break;
52
53                 fprintf(outfile, "%.2x%.2x%.2x%.2x\n", whi,wlo,bhi,blo);
54                 addr++;
55         }
56
57         fclose(infile);
58         fclose(outfile);
59
60         return 0;
61 }