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

Subversion Repositories openfire2

[/] [openfire2/] [trunk/] [utils/] [bin2bram.c] - Blame information for rev 6

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 2 toni32
// bin2bram : program to fill the template "bootram" with the provided
2
//            binary code
3
 
4
#include <stdio.h>
5
#include <stdlib.h>
6
#include <string.h>
7
 
8
#define MAX_RAM         2048
9
#define MAX_LINE                128
10
#define DUMP_TOKEN      "$DUMP_INIT_RAM"
11
#define DUMP_LINE               "defparam MEM%u.INIT_%02X = 256'h%s;\n" /* memory content */
12
#define DUMP_INITIAL_1  "defparam MEM%u.INIT_%c = 9'h0%02X;\n"  /* dword 0 at startupt */
13
#define DUMP_INITIAL_2  "defparam MEM%u.SRVAL_%c = 9'h0%02X;\n" /* dword 0 at reset */
14
 
15
int main(int argn, char **argc)
16
{
17
  char line[MAX_LINE], hexcodes[MAX_LINE], tmp[MAX_LINE];
18
  char memory[MAX_RAM][4];              // code loaded from BIN file
19
  int count, offset = 0;
20
  FILE *f, *ftemplate;
21
 
22
  if(argn != 4)
23
  {
24
    printf("usage: %s <template file> <binary file> <output file>\n", argc[0]);
25
    exit(1);
26
  }
27
 
28
  memset(memory, 0, MAX_RAM * 4);                                // initialize memory
29
  if( (f = fopen(argc[2], "rb")) == NULL )      // open binary file
30
  {
31
    printf("error: binary file <%s> does not exist\n", argc[2]);
32
    exit(1);
33
  }
34
 
35
  do
36
  {
37
    count = (unsigned short) fread(memory[offset++], 4, 1, f);  // read instructions (32 bits)
38
  } while(count > 0);
39
  fclose(f);
40
 
41
  if( (ftemplate = fopen(argc[1], "r")) == NULL )       // open template file
42
  {
43
    printf("error: template file <%s> does not exist\n", argc[1]);
44
    exit(1);
45
  }
46
 
47
  if( (f = fopen(argc[3], "w")) == NULL )                               // create destination file
48
  {
49
    printf("error: output file <%s> can't be created\n", argc[3]);
50
    exit(1);
51
  }
52
 
53
  while(!feof(ftemplate))               // read the template file
54
  {
55
         line[0] =0;
56
    fgets(line, MAX_LINE - 1, ftemplate);               // read a template line
57
    if( strstr(line, DUMP_TOKEN) != NULL )      // dump memory token found?
58
    {
59
      int ptr1 = 0, ptr2, byte;
60
 
61
           for(byte = 0; byte < 4; byte++)
62
      {
63
        fprintf(f, DUMP_INITIAL_1, 3 - byte, 'A', memory[0][byte] & 0xff);
64
        fprintf(f, DUMP_INITIAL_1, 3 - byte, 'B', memory[0][byte] & 0xff);
65
        fprintf(f, DUMP_INITIAL_2, 3 - byte, 'A', memory[0][byte] & 0xff);
66
        fprintf(f, DUMP_INITIAL_2, 3 - byte, 'B', memory[0][byte] & 0xff);
67
        fprintf(f, "\n");
68
      }
69
 
70
      while(ptr1 <= offset)
71
      {
72
        for(byte = 0; byte < 4; byte++)
73
        {
74
          hexcodes[0] = 0;                                                // initialize line
75
                    ptr2 = ptr1 + 31;                                   // start from the end of the line
76
          while(ptr2 >= ptr1)
77
          {
78
            sprintf(tmp, "%02X", (memory[ptr2--][byte]) & 0xff);        // hexcode (big endian)
79
            strcat(hexcodes, tmp);                                                      // concatenate to the hexcode string
80
          }
81
          sprintf(tmp, DUMP_LINE, 3 - byte, ptr1 / 32, hexcodes);       // compose defparam line
82
          fputs(tmp, f);                // write to file
83
        }
84
        fputs("\n", f);         // space between 32 byte blocks
85
        ptr1 += (unsigned short) 32;
86
      }
87
    }
88
    else fputs(line, f);                                                        // if not, copy the template line
89
  }
90
  fclose(ftemplate);
91
  fclose(f);
92
  return 0;
93
}

powered by: WebSVN 2.1.0

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