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

Subversion Repositories mblite

[/] [mblite/] [trunk/] [sw/] [util/] [bin2rom.c] - Blame information for rev 7

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 2 takar
#include <stdio.h>
2
#include <string.h>
3
 
4
unsigned power(unsigned base,unsigned n) {
5
  unsigned p;
6
  for(p=1; n > 0; --n)
7
     p=p*base;
8
  return p;
9
}
10
 
11
int main(int argc, char *argv[]) {
12
 
13
  FILE *infile, *outfile;
14
  int c;
15
  unsigned program_size;
16
  unsigned i = 0;
17
 
18
  if (argc != 4) {
19
    print_help(argv[0]);
20
    return(1);
21
  }
22
 
23
  infile = fopen(argv[1], "rb");
24
  if (!infile) {
25
    printf("Cannot open file %s\n", argv[1]);
26
    return(1);
27
  }
28
 
29
  outfile = fopen(argv[2], "w");
30
  if (!outfile) {
31
    printf("Cannot open file %s\n", argv[2]);
32
    return(1);
33
  }
34
 
35
  if (strlen(argv[3]) <= 0) {
36
    printf("Argument depth missing", argv[3]);
37
    return(1);
38
  }
39
 
40
  fprintf(outfile,"\
41
----------------------------------------------------------------------------------------------\n\
42
--\n\
43
--      Input file         : pram.vhd\n\
44
--      Design name        : pram\n\
45
--      Author             : Tamar Kranenburg\n\
46
--      Company            : Delft University of Technology\n\
47
--\n\
48
--      Description        : Single Port Synchronous Random Access Memory\n\
49
--\n\
50
----------------------------------------------------------------------------------------------\n\
51
\n\
52
LIBRARY ieee;\n\
53
USE ieee.std_logic_1164.ALL;\n\
54
USE ieee.std_logic_unsigned.ALL;\n\
55
\n\
56
LIBRARY mblite;\n\
57
USE mblite.std_Pkg.ALL;\n\
58
\n\
59
ENTITY pram IS\n\
60
    GENERIC\n\
61
    (\n\
62
        WIDTH : integer := 32;\n\
63
        SIZE  : integer := %s\n\
64
    );\n\
65
    PORT\n\
66
    (\n\
67
        dat_o                   : OUT std_logic_vector(WIDTH - 1 DOWNTO 0);\n\
68
        dat_i                   : IN std_logic_vector(WIDTH - 1 DOWNTO 0);\n\
69
        adr_i                   : IN std_logic_vector(SIZE - 1 DOWNTO 0);\n\
70
        wre_i                   : IN std_logic;\n\
71
        ena_i                   : IN std_logic;\n\
72
        clk_i                   : IN std_logic\n\
73
    );\n\
74
END pram;\n\
75
\n\
76
ARCHITECTURE arch OF pram IS\n\
77
  TYPE ram_type IS array (0 TO 2 ** SIZE - 1) OF std_logic_vector(WIDTH - 1 DOWNTO 0);\n\
78
  SIGNAL ram : ram_type := (", argv[3]);
79
 
80
  while((c = fgetc(infile)) != EOF)
81
  {
82
    if ((i % 32) == 0 ) { fprintf(outfile,"\n    "); }
83
    if ((i % 4) == 0 ) { fprintf(outfile,"X\""); }
84
    fprintf(outfile,"%.2X", (unsigned char) c & 0x0ff);
85
    if ((i % 4) == 3 ) { fprintf(outfile,"\","); }
86
    i++;
87
  }
88
 
89
  fprintf(outfile, "X\"FFFFFFFF\"");
90
  i+=4;
91
  /* Fill rest of ram */
92
 
93
  program_size = power(2, atoi(argv[3])) * 4;
94
 
95
  while(i < program_size)
96
  {
97
    if ((i % 4) == 0 ) { fprintf(outfile,","); }
98
    if ((i % 32) == 0 ) { fprintf(outfile,"\n    "); }
99
    if ((i % 4) == 0 ) { fprintf(outfile,"X\""); }
100
    fprintf(outfile,"00");
101
    if ((i % 4) == 3 ) { fprintf(outfile,"\""); }
102
    i++;
103
  }
104
 
105
  fprintf(outfile,");\n\
106
\n\
107
BEGIN\n\
108
    PROCESS(clk_i)\n\
109
    BEGIN\n\
110
        IF rising_edge(clk_i) THEN\n\
111
            IF notx(adr_i) AND ena_i = '1' THEN\n\
112
                IF wre_i = '1' THEN\n\
113
                    ram(conv_integer(adr_i)) <= dat_i;\n\
114
                END IF;\n\
115
                dat_o <= ram(conv_integer(adr_i));\n\
116
            END IF;\n\
117
        END IF;\n\
118
    END PROCESS;\n\
119
END arch;\n\
120
");
121
 
122
  fclose(infile);
123
  fclose(outfile);
124
 
125
  return 0;
126
 
127
}
128
 
129
int print_help(char * name)
130
{
131
  fprintf(stderr, "Usage: %s INFILE OUTFILE WIDTH DEPTH\n", name);
132
  fprintf(stderr, "%s converts a binary into a VHDL rom file\n", name);
133
  fprintf(stderr, "DEPTH in log(n) elements\n");
134
  return 0;
135
}

powered by: WebSVN 2.1.0

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