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

Subversion Repositories mblite

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

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 7 takar
/*******************************************************************************
2
*
3
*   File:        bin2vhd_32b.c
4
*   Description: Converts a 32-bit big endian .bin file into a VHDL description
5
*                containing an initialized 32-bit RAM instance to be used with
6
*                the mbLite.
7
*   Syntax:      bin2vhd_32b 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
*   Modified:    Register after DOUT removed and inserted in 'decode.vhd' (Huib)
14
*
15
*   Note:        No checks, e.g. on inputfile being a multiple of 4 bytes
16
*
17
********************************************************************************/
18
 
19
#include <stdio.h>
20
#include <string.h>
21
 
22
 
23
unsigned power (unsigned base, unsigned n) {
24
    unsigned p;
25
 
26
    for( p = 1; n > 0; --n)
27
        p = p*base;
28
    return p;
29
}
30
 
31
void print_help(char * name)
32
{
33
    fprintf(stderr, "%s converts a binary file into a VHDL rom file\n", name);
34
    fprintf(stderr, "Usage: %s INFILE OUTFILE ABITS\n", name);
35
    fprintf(stderr, "where ABITS (number of address bits) is log2(MEMORY DEPTH)\n");
36
}
37
 
38
 
39
int main (int argc, char *argv[]) {
40
 
41
    FILE *infile, *outfile;
42
    int      c, insize;
43
    unsigned ram_size;
44
    unsigned i = 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])) * 4;     // 32 bits data bus!
69
 
70
    // determine the size of the input file in bytes
71
    fseek(infile, 0, SEEK_END);
72
    insize = ftell(infile);
73
    rewind(infile);
74
    if (insize > ram_size) {
75
        printf("RAM size (%d words) too small (at least %d words needed",
76
                                                            ram_size/4, insize/4);
77
        return(1);
78
    }
79
 
80
    fprintf(outfile,"\
81
--------------------------------------------------------------------------------\n\
82
--\n\
83
--    Filename    : imem.vhd\n\
84
--    Entity      : imem\n\
85
--    Input from  : %s\n\
86
--    Description : Single Port Synchronous Random Access (Instruction) Memory\n\
87
--                  for the mbLite processor.\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 imem 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;\n\
108
        ena_i :  IN STD_LOGIC;\n\
109
        clk_i :  IN STD_LOGIC\n\
110
        );\n\
111
END imem;\n\
112
\n\n\
113
ARCHITECTURE arch OF imem IS\n\
114
    TYPE ram_type IS array (0 TO 2**ABITS_g -1) OF STD_LOGIC_VECTOR (WIDTH_g -1 DOWNTO 0);\n\
115
    SIGNAL ram : ram_type := (",     argv[1], argv[3] );
116
 
117
    while (i < insize) {
118
        c = fgetc(infile);
119
        if ((i % 32) == 0 ) { fprintf(outfile,"\n     "); }
120
        if ((i %  4) == 0 ) { fprintf(outfile," X\""); }
121
        fprintf(outfile,"%.2X", (unsigned char) c & 0x0ff);
122
        if ((i %  4) == 3) {
123
            if (i < insize-4) { fprintf(outfile,"\","); }
124
            else { fprintf(outfile,"\""); }
125
        }
126
        i++;
127
    }
128
 
129
    // Fill rest of ram if not full yet
130
    while (i < ram_size) {
131
        if ((i %  4) == 0 ) { fprintf(outfile,","); }
132
        if ((i % 32) == 0 ) { fprintf(outfile,"\n     "); }
133
        if ((i %  4) == 0 ) { fprintf(outfile," X\""); }
134
        fprintf(outfile,"00");
135
        if ((i % 4) == 3 ) { fprintf(outfile,"\""); }
136
        i++;
137
    }
138
 
139
    fprintf(outfile," );\n\
140
\n\
141
    ATTRIBUTE syn_ramstyle : STRING;\n\
142
    ATTRIBUTE syn_ramstyle OF ram : SIGNAL IS \"block_ram\";\n\
143
\n\
144
BEGIN\n\
145
\n\
146
    -- for future use (enable programming ...)\n\
147
    PROCESS(clk_i)\n\
148
    BEGIN\n\
149
        IF RISING_EDGE(clk_i) THEN\n\
150
            IF ena_i = '1' THEN\n\
151
                IF wre_i = '1' THEN\n\
152
                    ram(TO_INTEGER(UNSIGNED(adr_i))) <= dat_i;\n\
153
                END IF;\n\
154
                dat_o <= ram(TO_INTEGER(UNSIGNED(adr_i)));\n\
155
           END IF;\n\
156
        END IF;\n\
157
    END PROCESS;\n\
158
\n\
159
END ARCHITECTURE arch;\n\
160
\n\
161
-- [EOF]\n\
162
");
163
 
164
    fclose(infile);
165
    fclose(outfile);
166
    return 0;
167
}
168
 

powered by: WebSVN 2.1.0

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