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

Subversion Repositories light8080

[/] [light8080/] [trunk/] [tools/] [obj2hdl/] [src/] [obj2hdl.py] - Blame information for rev 89

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

Line No. Rev Author Line
1 73 ja_rd
 
2
 
3
import sys
4
import getopt
5
 
6
default_template = (
7
    "-- @obj_pkg_name@ -- Object code in VHDL constant table for "\
8
    "BRAM initialization.",
9
    "-- Generated automatically with script 'build_rom.py'.",
10
    "",
11
    "library ieee;",
12
    "use ieee.std_logic_1164.all;",
13
    "use ieee.numeric_std.all;",
14
    "use work.l80pkg.all;",
15
    "",
16
    "package @obj_pkg_name@ is",
17
    "",
18
    "constant @constant@ : obj_code_t(0 to @obj_size@) := (",
19
    "    @obj_bytes@",
20
    "    );",
21
    "",
22
    "end package @obj_pkg_name@;",
23
    )
24
 
25
 
26
class command_line_params():
27
    def __init__(self):
28
        self.template_file = None
29
        self.package_name = None
30
        self.constant_name = "object_code"
31
        self.indent = 2
32
        self.proj_name = None
33
 
34
 
35
 
36
def usage():
37
    """Print usage instructions"""
38
    print ""
39
    print "usage:"
40
    print "python build_rom.py [arguments]\n"
41
    print "Builds VHDL ROM constant from template and Intel HEX object file.\n"
42
    print "ALL of the following arguments should be given, in any order:"
43
    print "{f|file} <filename>        Object code file name"
44
    print "{c|constant} <name>        Name of target VHDL constant"
45
    print "{p|package} <name>         Name of target VHDL package"
46
    print "{n|name} <name>            Name of project (used only in comment)"
47
    print "{o|output} <filename>      Target VHDL file name"
48
    print ""
49
    print "Additionally, any of these arguments can be given:"
50
    print "{v|vhdl} <filename>        VHDL template"
51
    print "         (defaults to templates/obj_code_kg_template.vhdl)"
52
    print "{i|indent} <number>        Indentation in VHDL tables (decimal)"
53
    print "         (defaults to 4)"
54
 
55
 
56
def help():
57
    """Print help message a bit longer than usage message."""
58
    print "\nPurpose:\n"
59
    print "Reads the code and data binary files and 'slices' them in byte"
60
    print "columns."
61
    print "The data columns are converted to VHDL strings and then inserted"
62
    print "into the vhdl template, in place of tags @code0@ .. @code3@ and "
63
    print "@data0@ .. @data3@. Column 0 is LSB and column3 is MSB.\n"
64
    print "Tags like @data31@ and @data20@ etc. can be used to initialize"
65
    print "memories in 16-bit buses, also split in byte columns.\n"
66
    print "Template tags are replaced as follows:"
67
    print "@obj_pkg_name@        : Name of package in target vhdl file"
68
    print "@const_name@          : Name of constant (VHDL table)"
69
    print "@obj_size@            : Total size of code table in bytes"
70
 
71
 
72
def parse_hex_line(line):
73
    """Parse code line in HEX object file."""
74
    line = line.strip()
75
    slen = int(line[1:3],16)
76
    sloc = int(line[3:7],16)
77
    stype = line[7:9]
78
    sdata = line[9:len(line)-2]
79
    schk = int(line[len(line)-2:],16)
80
 
81
    csum = slen + int(sloc / 256) + (sloc % 256) + int(stype,16)
82
    bytes = [0, ] * slen
83
    for i in range(slen):
84
        sbyte = int(sdata[i*2:i*2+2],16)
85
        bytes[i] = sbyte;
86
        csum = csum + sbyte
87
 
88
    csum = ~csum
89
    csum = csum + 1
90
    csum = csum % 256
91
    if csum != schk:
92
        return (None, None)
93
 
94
    return (sloc, bytes)
95
 
96
 
97
def read_ihex_file(ihex_filename):
98
    """
99
    """
100
    xcode = [0, ] * 65536
101
    bottom = 100000
102
    top = -1
103
    (xcode, top, bottom)
104
 
105
    fin = open(ihex_filename, "r")
106
    ihex_lines = fin.readlines()
107
    fin.close()
108
 
109
    total_bytes = 0
110
    for line in ihex_lines:
111
        (address, bytes) = parse_hex_line(line)
112
        if address == None:
113
            print "Checksum error!"
114
            sys.exit(1)
115
        total_bytes = total_bytes + len(bytes)
116
        for i in range(len(bytes)):
117
            xcode[address + i] = bytes[i]
118
 
119
        if address < bottom:
120
            bottom = address
121
 
122
        if (address + len(bytes)) > top:
123
            top = (address + len(bytes))
124
 
125
    print "Read %d bytes from file '%s'" % (total_bytes, ihex_filename)
126
    print "Code range %04xh to %04xh" % (bottom, top)
127
    return (xcode, total_bytes, bottom, top)
128
 
129
 
130
def build_vhdl_code(template_filename, xcode, rom_size, params):
131
 
132
    if not template_filename:
133
        lines = default_template
134
    else:
135
        fin = open(template_filename, "r")
136
        lines = fin.readlines()
137
        fin.close()
138
 
139
    vhdl_code = ""
140
 
141
    for line in lines:
142
        line = line.strip()
143
 
144
        if line.rfind("@obj_bytes@") >= 0:
145
            obj_str = "    "
146
            for i in range(rom_size):
147
                if i != (rom_size-1):
148
                    sbyte = "X\"%02x\", " % xcode[i]
149
                else:
150
                    sbyte = "X\"%02x\" " % xcode[i]
151
                obj_str = obj_str + sbyte
152
                if (i % 8) == 7:
153
                    obj_str = obj_str + "\n    "
154
 
155
            line = line.replace("@obj_bytes@",obj_str)
156
 
157
        if line.rfind("@obj_size@") >= 0:
158
            line = line.replace("@obj_size@","%d" % (rom_size-1))
159
 
160
        if line.rfind("@constant@") >= 0:
161
            line = line.replace("@constant@", params.constant_name)
162
 
163
        if line.rfind("@obj_pkg_name@") >= 0:
164
            line = line.replace("@obj_pkg_name@","obj_code_pkg")
165
 
166
        vhdl_code = vhdl_code + line + "\n"
167
 
168
    return vhdl_code
169
 
170
 
171
def main(argv):
172
 
173
    try:
174
        opts, _ = getopt.getopt(argv, "hf:n:p:c:o:i:v:",
175
        ["help", "file=", "name=", "package=", "constant=",
176
         "output=", "indent=", "vhdl=", ])
177
    except getopt.GetoptError, err:
178
        print ""
179
        print err
180
        usage()
181
        sys.exit(2)
182
 
183
    # Give default values to command line parameters
184
    params = command_line_params()
185
 
186
    # Parse coommand line parameters
187
    for opt, arg in opts:
188
        if opt in ("-h", "--help"):
189
            usage()
190
            help()
191
            exit(1)
192
        if opt in ("-v", "--vhdl"):
193
            params.template_file = arg
194
        elif opt in ("-o", "--output"):
195
            target_filename = arg
196
        elif opt in ("-c", "--constant"):
197
            params.constant_name = arg
198
        elif opt in ("-f", "--file"):
199
            hex_filename = arg
200
        elif opt in ("-p", "--package"):
201
            params.package_name = arg
202
        elif opt in ("-n", "--name"):
203
            params.proj_name = arg
204
        elif opt in ("-i", "--indent"):
205
            params.indent = int(arg)
206
 
207
    if not target_filename:
208
        print "Missing target file name."
209
        usage()
210
        sys.exit(2)
211
 
212
 
213
    (xcode, total_bytes, bottom, top) = read_ihex_file(hex_filename);
214
    vhdl_code = build_vhdl_code(params.template_file, xcode, top, params);
215
 
216
    fout = None
217
    try:
218
        fout = open(target_filename, "w")
219
        fout.write(vhdl_code)
220
        fout.close()
221
        print "VHDL code table written to %s" % target_filename
222
    except:
223
        print "Trouble opening %s for output" % target_filename
224
    finally:
225
        if fout: fout.close()
226
 
227
 
228
 
229
 
230
 
231
if __name__ == "__main__":
232
    main(sys.argv[1:])
233
 
234
    sys.exit(0)
235
 
236
 

powered by: WebSVN 2.1.0

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