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

Subversion Repositories ion

[/] [ion/] [trunk/] [src/] [bin2hdl.py] - Blame information for rev 24

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

Line No. Rev Author Line
1 2 ja_rd
""" xcxcxc
2
 
3
 
4
"""
5
import sys
6
import getopt
7
import math
8
 
9
 
10
def usage():
11
    print "usage:"
12
    print "python bin2hdl.py [arguments]\n"
13
    print "Inserts data in VHDL template\n"
14
    print "ALL of the following arguments should be given, in any order:"
15
    print "{c|code} <filename>        Code binary image file name"
16
    print "{v|vhdl} <filename>        VHDL template"
17
    print "{a|architecture} <name>    Name of target VHDL architecture"
18
    print "{e|entity} <name>          Name of target VHDL entity"
19
    print "{o|output} <filename>      Target VHDL file name"
20
    print "code_size <number>         Size of code memory in words (decimal)"
21
    print "data_size <number>         Size of data memory in words (decimal)"
22
    print ""
23
    print "Additionally, any of these arguments can be given:"
24 24 ja_rd
    print "{s|sim_len} <number>       Length of simulation in clock cycles"
25 2 ja_rd
    print "{d|data} <filename>        Data binary image file name"
26
    print "{h|help}                   Display some help text and exit"
27
    print "{i|indent} <number>        Indentation in VHDL tables (decimal)"
28
 
29
def help():
30
    print "\nPurpose:\n"
31
    print "Reads the code and data binary files and 'slices' them in byte"
32
    print "columns."
33
    print "The data columns are converted to VHDL strings and then inserted"
34
    print "into the vhdl template, in place of tags @code0@ .. @code3@ and "
35
    print "@data0@ .. @data3@. Column 0 is LSB and column3 is MSB.\n"
36
    print "Other template tags are replaced as follows:"
37
    print "@entity_name@         : Name of entity in target vhdl file"
38
    print "@arch_name@           : Name of architecture in target vhdl file"
39 24 ja_rd
    print "@sim_len@             : Length of simulation in clock cycles"
40 2 ja_rd
    print "@code_table_size@     : Size of code RAM block, in words"
41
    print "@code_addr_size@      : ceil(Log2(@code_table_size@))"
42
    print "@data_table_size@     : Size of data RAM block, in words"
43
    print "@data_addr_size@      : ceil(Log2(@data_table_size@))"
44
 
45
 
46
def build_vhdl_tables(code,table_size, indent_size):
47
    # Build the four byte column tables. [0] is LSB, [3] is MSB
48
    tables = [[0 for i in range(table_size)] for i in range(4)]
49
 
50
    # Separate binary data into byte columns
51
    # (here's where data endianess matters, we're assuming big endian)
52
    byte = 0    # byte 0 is LSB, 3 is MSB
53
    index = 0   # index into column table    
54
    for c in code:
55
        #print str(ord(c)) + " " +  str(byte) + " " + str(index)
56
        tables[3-byte][index] = ord(c)
57
        #for k in tables:
58
        #    print k[0:4]
59
        byte = byte + 1
60
        if byte == 4:
61
            byte = 0
62
            index = index + 1
63
 
64
    # Write the data for each of the four column tables as a VHDL byte
65
    # constant table.
66
    vhdl_data_strings = [" "*indent_size]*4
67
 
68
    for j in range(4):
69
        col = 0
70
        word = len(tables[j])
71
        for c in tables[j]:
72
            word = word - 1
73
            if word > 0:
74
                item = "X\"%02X\"," % c
75
            else:
76
                item = "X\"%02X\"" % c
77
            col = col + 1
78
            if col == 8:
79
                col = 0
80
                item = item + "\n" + " "*indent_size
81
            vhdl_data_strings[j] = vhdl_data_strings[j] + item
82
 
83
    return vhdl_data_strings
84
 
85
def main(argv):
86
    code_filename = ""          # file with code sections (text+reginfo+rodata)
87
    data_filename = ""          # file with data sections (data+bss)
88
    vhdl_filename = ""          # name of vhdl template file
89
    entity_name = "mips_tb"     # name of vhdl entity to be generated
90
    arch_name = "testbench"     # name of vhdl architecture to be generated
91
    target_filename = "tb.vhdl" # name of target vhdl file
92
    indent = 4                  # indentation for table data, in spaces
93
    code_table_size = -1        # size of VHDL table
94
    data_table_size = -1        # size of VHDL table
95
    bin_words = 0               # size of binary file in 32-bit words 
96 24 ja_rd
    simulation_length = 22000   # length of logic simulation in clock cycles
97 2 ja_rd
 
98
    #
99
 
100
    try:
101 24 ja_rd
        opts, args = getopt.getopt(argv, "hc:d:v:a:e:o:i:s:",
102 2 ja_rd
        ["help", "code=", "data=", "vhdl=", "architecture=",
103 24 ja_rd
         "entity=", "output=", "indent=", "sim_len=",
104
         "code_size=", "data_size="])
105 2 ja_rd
    except getopt.GetoptError:
106
        usage()
107
        sys.exit(2)
108
 
109
    # Parse coommand line parameters
110
    for opt, arg in opts:
111
        if opt in ("-h", "--help"):
112
            usage()
113
            help()
114
            exit(1)
115
        if opt in ("-v", "--vhdl"):
116
            vhdl_filename = arg
117
        elif opt in ("-o", "--output"):
118
            target_filename = arg
119
        elif opt in ("-c", "--code"):
120
            code_filename = arg
121
        elif opt in ("-d", "--data"):
122
            data_filename = arg
123
        elif opt in ("-a", "--architecture"):
124
            arch_name = arg
125
        elif opt in ("-e", "--entity"):
126
            entity_name = arg
127
        elif opt in ("-i", "--indent"):
128
            indent = int(arg)
129 24 ja_rd
        elif opt in ("-s", "--sim_len"):
130
            simulation_length = int(arg)
131 2 ja_rd
        elif opt == "--code_size":
132
            code_table_size = int(arg)
133
        elif opt == "--data_size":
134
            data_table_size = int(arg)
135
 
136
    # See if all mandatory options are there
137
    if code_filename=="" or vhdl_filename=="" or \
138
       code_table_size < 0 or data_table_size<0:
139
        usage()
140
        sys.exit(2)
141
 
142
 
143
    # Open binary code and data input files and read them into buffers
144
    try:
145
        fin = open(code_filename, "rb")
146
        code = fin.read()
147
        fin.close()
148
    except IOError:
149
        print "Binary File %s not found" % code_filename
150
 
151
    if data_filename != "":
152
        try:
153
            fin = open(data_filename, "rb")
154
            data = fin.read()
155
            fin.close()
156
        except IOError:
157
            print "Binary File %s not found" % data_filename
158
 
159
    #print "Read " + str(len(code)) + " bytes."
160
 
161
    # Make sure the code and data will fit in the tables
162
    bin_words = len(code) / 4
163
    if bin_words > code_table_size:
164
        print "Code does not fit table: " + str(bin_words) + " words,",
165
        print str(code_table_size) + " table entries"
166
        sys.exit(1)
167
 
168
    if data_filename != "":
169
        # FIXME We're not checking for BSS size here, only .data (?)
170
        bin_words = len(data) / 4
171
        if bin_words > data_table_size:
172
            print "Data does not fit table: " + str(bin_words) + " words,",
173
            print str(data_table_size) + " table entries"
174
            sys.exit(1)
175
 
176
 
177
    # Build the VHDL strings for each slice of both code and data tables
178
    vhdl_code_strings = build_vhdl_tables(code, code_table_size, indent)
179
    if data_filename != "":
180
        vhdl_data_strings = build_vhdl_tables(data, data_table_size, indent)
181
    else:
182
        # In case we didn't get a data binary, we want the vhdl compilation 
183
        # to fail when @data@ tags are used, just to catch the error
184
        vhdl_data_strings = ["error: missing data binary file"]*4
185
 
186
    # Now start scanning the VHDL template, inserting data where needed
187
 
188
    # Read template file...
189
    fin = open(vhdl_filename, "r")
190
    vhdl_lines = fin.readlines()
191
    fin.close()
192
 
193
    # ...and build the keyword and replacement tables
194
    keywords = ["@code0@","@code1@","@code2@","@code3@",
195
                "@data0@","@data1@","@data2@","@data3@",
196
                "@entity_name@","@arch_name@",
197 24 ja_rd
                "@sim_len@",
198 2 ja_rd
                "@code_table_size@","@code_addr_size@",
199
                "@data_table_size@","@data_addr_size@"];
200
    replacement = vhdl_code_strings + vhdl_data_strings + \
201
                 [entity_name, arch_name,
202 24 ja_rd
                  str(simulation_length),
203 2 ja_rd
                  str(code_table_size),
204
                  str(int(math.floor(math.log(code_table_size,2)))),
205
                  str(data_table_size),
206
                  str(int(math.floor(math.log(data_table_size,2))))]
207
 
208
    # Now traverse the template lines replacing any keywords with the proper 
209
    # vhdl stuff we just built above.
210
    output = ""
211
    for vhdl_line in vhdl_lines:
212
        temp = vhdl_line
213
        for i in range(len(keywords)):
214
            if temp.rfind(keywords[i]) >= 0:
215
                temp = temp.replace(keywords[i], replacement[i])
216
                # uncomment this break to check for ONE keyword per line only
217
                #break
218
        output = output + temp
219
 
220
    try:
221
        fout = open(target_filename, "w")
222
        fout.write(output)
223
        fout.close()
224
        print "Wrote VHDL file '%s'" % target_filename
225
    except IOError:
226
        print "Could not write to file %s" % target_filename
227
 
228
 
229
    sys.exit(0)
230
 
231
 
232
 
233
if __name__ == "__main__":
234
    main(sys.argv[1:])
235
 
236
    sys.exit(0)
237
 

powered by: WebSVN 2.1.0

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