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

Subversion Repositories mblite

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

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

Line No. Rev Author Line
1 7 takar
/*******************************************************************************
2
*
3
*   File:        bin2vhd_4x8b.c
4
*   Description: Converts a 32-bit big endian .bin file into a VHDL description
5
*                containing 4 initialized 8-bit RAM instances to be used with
6
*                the mbLite (ram0 containing the msb's, ram3 the lsb's).
7
*   Syntax:      bin2vhd_4x8b INFILENAME OUTFILENAME ABITS
8
*                with ABITS representing the number of address bits
9
*                ( equal to ceil(log2(MEMORY DEPTH)) ).
10
*
11
*   Author:      Rene van Leuken, edited and extended by Huib
12
*   Date:        this version, February 2010
13
*
14
*   Note:        No checks, e.g. on inputfile being a multiple of 4 bytes
15
*
16
********************************************************************************/
17
 
18
#include <stdio.h>
19
#include <string.h>
20
 
21
 
22
unsigned power (unsigned base, unsigned n) {
23
    unsigned p;
24
 
25
    for( p = 1; n > 0; --n)
26
        p = p*base;
27
    return p;
28
}
29
 
30
void print_help(char * name)
31
{
32
    fprintf(stderr, "%s converts a binary file into a VHDL ram file\n", name);
33
    fprintf(stderr, "Usage: %s INFILE OUTFILE ABITS\n", name);
34
    fprintf(stderr, "where ABITS (number of address bits) is log2(MEMORY DEPTH)\n");
35
}
36
 
37
 
38
int main(int argc, char *argv[]) {
39
 
40
    FILE *infile, *outfile;
41
    int c[4], insize;
42
    unsigned ram_size;
43
    unsigned i = 0;
44
    unsigned m = 0;
45
 
46
    if (argc != 4) {
47
        print_help(argv[0]);
48
        return(1);
49
    }
50
 
51
    infile = fopen(argv[1], "rb");
52
    if (!infile) {
53
        printf("Cannot open file %s\n", argv[1]);
54
        return(1);
55
    }
56
 
57
    outfile = fopen(argv[2], "w");
58
    if (!outfile) {
59
        printf("Cannot open file %s\n", argv[2]);
60
        return(1);
61
    }
62
 
63
    if (strlen(argv[3]) <= 0) {
64
        printf("Argument ABITS missing", argv[3]);
65
        return(1);
66
    }
67
 
68
    ram_size = power(2, atoi(argv[3])) * 1;
69
    // determine the size of the input file in bytes
70
    fseek(infile, 0, SEEK_END);
71
    insize = ftell(infile);
72
    rewind(infile);
73
    if (insize/4 > ram_size) {
74
        printf("RAM size (%d words) too small (at least %d words needed",
75
                                                                ram_size, insize/4);
76
        return(1);
77
    }
78
 
79
 
80
    fprintf(outfile,"\
81
--------------------------------------------------------------------------------\n\
82
--\n\
83
--    Filename    : dmem4.vhd\n\
84
--    Entity      : dmem4\n\
85
--    Input from  : %s\n\
86
--    Description : Single Port Synchronous Random Access (Instruction) Memory\n\
87
--                  with 4 write enable ports.\n\
88
--    Author      : Rene van Leuken, modified by Huib\n\
89
--    Company     : Delft University of Technology\n\
90
--\n\
91
--------------------------------------------------------------------------------\n\
92
\n\
93
LIBRARY ieee;\n\
94
USE ieee.std_logic_1164.ALL;\n\
95
USE ieee.std_logic_unsigned.ALL;\n\
96
USE ieee.numeric_std.all;\n\
97
\n\n\
98
ENTITY dmem4 IS\n\
99
    GENERIC (\n\
100
        WIDTH_g : POSITIVE := 32;\n\
101
        ABITS_g : POSITIVE := %s\n\
102
        );\n\
103
    PORT (\n\
104
        dat_o : OUT STD_LOGIC_VECTOR (WIDTH_g -1 DOWNTO 0);\n\
105
        dat_i :  IN STD_LOGIC_VECTOR (WIDTH_g -1 DOWNTO 0);\n\
106
        adr_i :  IN STD_LOGIC_VECTOR (ABITS_g -1 DOWNTO 0);\n\
107
        wre_i :  IN STD_LOGIC_VECTOR (3 DOWNTO 0);\n\
108
        ena_i :  IN STD_LOGIC;\n\
109
        clk_i :  IN STD_LOGIC\n\
110
    );\n\
111
END dmem4;\n\
112
\n\n\
113
ARCHITECTURE arch OF dmem4 IS\n\
114
\n\
115
  SIGNAL di0, di1, di2, di3 : STD_LOGIC_VECTOR (WIDTH_g/4 -1 DOWNTO 0);\n\
116
  SIGNAL do0, do1, do2, do3 : STD_LOGIC_VECTOR (WIDTH_g/4 -1 DOWNTO 0);\n\
117
\n\
118
  TYPE ram_type IS ARRAY (0 TO 2**ABITS_g -1) OF STD_LOGIC_VECTOR (WIDTH_g/4 -1 DOWNTO 0);\n\
119
", argv[1], argv[3] );
120
 
121
    for ( m = 0; m < 4; m++ ) {
122
        infile = freopen(argv[1], "rb", infile);
123
        i = 0;
124
 
125
        fprintf(outfile,"\
126
\nSIGNAL ram%d : ram_type := (", m);
127
 
128
    while (i < insize) {
129
        c[0] = fgetc(infile);
130
        c[1] = fgetc(infile);
131
        c[2] = fgetc(infile);
132
        c[3] = fgetc(infile);
133
        if ((i % 32) == 0 ) { fprintf(outfile,"\n     "); }
134
        fprintf(outfile," X\"%.2X", (unsigned char) c[m] & 0x0ff);
135
        if (i < insize-4) { fprintf(outfile,"\","); }
136
            else { fprintf(outfile,"\""); }
137
        i += 4;
138
    }
139
        // Fill rest of ram if not full yet
140
        i = i/4;
141
        while (i < ram_size) {
142
            fprintf(outfile,",");
143
            if ((i % 8) == 0 ) { fprintf(outfile,"\n     "); }
144
               fprintf(outfile," X\"");
145
            fprintf(outfile,"00");
146
            fprintf(outfile,"\"");
147
            i++;
148
        }
149
 
150
        fprintf(outfile," );\n");
151
    }
152
 
153
    fprintf(outfile,"\n\
154
\n\
155
    ATTRIBUTE syn_ramstyle : STRING;\n\
156
    ATTRIBUTE syn_ramstyle OF ram0,ram1,ram2,ram3 : SIGNAL IS \"block_ram\";\n\
157
\n\
158
BEGIN\n\
159
\n\
160
    dat_o <= do0 & do1 & do2 & do3;\n\
161
    \n\
162
    di3 <= dat_i(  WIDTH_g/4 -1 DOWNTO         0);\n\
163
    di2 <= dat_i(  WIDTH_g/2 -1 DOWNTO   WIDTH_g/4);\n\
164
    di1 <= dat_i(3*WIDTH_g/4 -1 DOWNTO   WIDTH_g/2);\n\
165
    di0 <= dat_i(  WIDTH_g   -1 DOWNTO 3*WIDTH_g/4);\n\
166
\n\
167
    do3 <= ram3(TO_INTEGER(UNSIGNED(adr_i)));\n\
168
    do2 <= ram2(TO_INTEGER(UNSIGNED(adr_i)));\n\
169
    do1 <= ram1(TO_INTEGER(UNSIGNED(adr_i)));\n\
170
    do0 <= ram0(TO_INTEGER(UNSIGNED(adr_i)));\n\
171
\n\
172
    PROCESS(clk_i)\n\
173
    BEGIN\n\
174
        -- wre: 3 downto 0, while di0..di3 in byte reversed format\n\
175
        IF RISING_EDGE(clk_i) THEN\n\
176
            IF  ena_i = '1' THEN\n\
177
                IF wre_i(0) = '1' THEN\n\
178
                    ram3(TO_INTEGER(UNSIGNED(adr_i))) <= di3;\n\
179
                END IF;\n\
180
                IF wre_i(1) = '1' THEN\n\
181
                    ram2(TO_INTEGER(UNSIGNED(adr_i))) <= di2;\n\
182
                END IF;\n\
183
                IF wre_i(2) = '1' THEN\n\
184
                    ram1(TO_INTEGER(UNSIGNED(adr_i))) <= di1;\n\
185
                END IF;\n\
186
                IF wre_i(3) = '1' THEN\n\
187
                    ram0(TO_INTEGER(UNSIGNED(adr_i))) <= di0;\n\
188
                END IF;\n\
189
            END IF;\n\
190
        END IF;\n\
191
    END PROCESS;\n\
192
\n\
193
END ARCHITECTURE arch;\n\
194
\n\
195
-- [EOF]\n\
196
");
197
 
198
  fclose(infile);
199
  fclose(outfile);
200
 
201
  return 0;
202
 
203
}

powered by: WebSVN 2.1.0

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