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

Subversion Repositories tv80

[/] [tv80/] [trunk/] [scripts/] [ihex2mem.py] - Blame information for rev 84

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 2 ghutchis
#!/usr/bin/env python
2 5 ghutchis
#
3
# Intel Hex to Verilog Memory format converter
4
#
5
# Copyright (c) 2004 Guy Hutchison (ghutchis@opencores.org)
6
#
7
# Permission is hereby granted, free of charge, to any person obtaining a
8
# copy of this software and associated documentation files (the "Software"),
9
# to deal in the Software without restriction, including without limitation
10
# the rights to use, copy, modify, merge, publish, distribute, sublicense,
11
# and/or sell copies of the Software, and to permit persons to whom the
12
# Software is furnished to do so, subject to the following conditions:
13
#
14
# The above copyright notice and this permission notice shall be included
15
# in all copies or substantial portions of the Software.
16
#
17
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
20
# IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
21
# CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
22
# TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
23
# SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24 2 ghutchis
 
25 5 ghutchis
class mem_image:
26
    def __init__ (self):
27
        self.min = 100000
28
        self.max = -1
29
        self.map = {}
30
        self.bcount = 0
31
 
32
    def load_ihex (self, infile):
33
        ifh = open (infile, 'r')
34
 
35
        line = ifh.readline()
36
        while (line != ''):
37
            if (line[0] == ':'):
38
                rlen = int(line[1:3], 16)
39
                addr = int(line[3:7], 16)
40
                rtyp = int(line[7:9], 16)
41
                ptr = 9
42
                for i in range (0, rlen):
43
                    laddr = addr + i
44
                    val = int(line[9+i*2:9+i*2+2], 16)
45
                    self.map[laddr] = val
46
                    self.bcount += 1
47
                    if (laddr > self.max): self.max = laddr
48
                    if (laddr < self.min): self.min = laddr
49
 
50
            line = ifh.readline()
51
 
52
        ifh.close()
53
 
54
    def save_vmem (self, outfile, start=-1, stop=-1):
55
        if (start == -1): start = self.min
56
        if (stop == -1): stop = self.max
57
 
58
        ofh = open (outfile, 'w')
59
        for addr in range(start, stop+1):
60
            if self.map.has_key (addr):
61
                ofh.write ("@%02x %02x\n" % (addr, self.map[addr]))
62
        ofh.close()
63
 
64 2 ghutchis
def ihex2mem (infile, outfile):
65
    ifh = open (infile, 'r')
66
    ofh = open (outfile, 'w')
67
 
68
    bcount = 0
69
    line = ifh.readline()
70
    while (line != ''):
71
        if (line[0] == ':'):
72
            rlen = int(line[1:3], 16)
73
            addr = int(line[3:7], 16)
74
            rtyp = int(line[7:9], 16)
75
            ptr = 9
76
            for i in range (0, rlen):
77
                val = int(line[9+i*2:9+i*2+2], 16)
78
                ofh.write ("@%02x %02x\n" % (addr+i, val))
79
                bcount += 1
80
 
81
        line = ifh.readline()
82
 
83
    ifh.close()
84
    ofh.close()
85
 
86
    return bcount
87
 
88
def cmdline ():
89
    import sys
90
 
91
    infile = sys.argv[1]
92
    outfile = sys.argv[2]
93
 
94 5 ghutchis
    #bc = ihex2mem (infile, outfile)
95
    conv = mem_image()
96
    conv.load_ihex(infile)
97
    conv.save_vmem(outfile)
98
    print "Converted %d bytes from %s to %s" % (conv.bcount, infile, outfile)
99 2 ghutchis
 
100
if __name__ == '__main__':
101
    cmdline()
102
 

powered by: WebSVN 2.1.0

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