OpenCores
URL https://opencores.org/ocsvn/an-fpga-implementation-of-low-latency-noc-based-mpsoc/an-fpga-implementation-of-low-latency-noc-based-mpsoc/trunk

Subversion Repositories an-fpga-implementation-of-low-latency-noc-based-mpsoc

[/] [an-fpga-implementation-of-low-latency-noc-based-mpsoc/] [trunk/] [mpsoc/] [src_c/] [bin2mif/] [conv.p] - Blame information for rev 48

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 48 alirezamon
 
2
# encoding: utf-8
3
'''
4
@author:     Bruno Duarte Gouveia
5
 
6
@copyright:  2012 organization_name. All rights reserved.
7
 
8
@contact:    bgouveia@gmail.com
9
@deffield    updated: Updated
10
'''
11
 
12
import sys
13
import os
14
import struct
15
import numpy as np
16
 
17
 
18
from argparse import ArgumentParser
19
from argparse import RawDescriptionHelpFormatter
20
 
21
__all__ = []
22
__version__ = 0.1
23
__date__ = '2012-11-18'
24
__updated__ = '2012-11-18'
25
 
26
DEBUG = 1
27
TESTRUN = 0
28
PROFILE = 0
29
 
30
class CLIError(Exception):
31
    '''Generic exception to raise and log different fatal errors.'''
32
    def __init__(self, msg):
33
        super(CLIError).__init__(type(self))
34
        self.msg = "E: %s" % msg
35
    def __str__(self):
36
        return self.msg
37
    def __unicode__(self):
38
        return self.msg
39
 
40
def parsefile(filename,wordsize):
41
 
42
    dump=[]
43
    read_data=None;
44
    with open (filename,'rb') as inputfile:
45
        read_data = np.fromfile(inputfile,dtype=np.uint8)
46
 
47
    wordsizeinbytes=wordsize/8;
48
    wordcounter=0
49
    count=0
50
    tempstring=""
51
    for byte in read_data:
52
        s=hex(byte)[2:]
53
        if len(s) % 2 != 0 :
54
            s='0'+s
55
        tempstring+=s
56
        count=(count+1)%wordsizeinbytes
57
 
58
        if count==0:
59
            dump.append(tempstring)
60
            tempstring=""
61
            wordcounter+=1
62
    return [dump,wordcounter]
63
 
64
def writefile(filename,wordsize,data):
65
 
66
    f=open(filename,"w")
67
 
68
    f.write("WIDTH=")
69
    f.write(str(wordsize))
70
    f.write(";\n")
71
    f.write("DEPTH=")
72
    f.write(str(len(data)))
73
    f.write(";\n\n")
74
 
75
    f.write("ADDRESS_RADIX=HEX;\nDATA_RADIX=HEX;\n\nCONTENT BEGIN\n")
76
 
77
    for i in range(0,len(data)):
78
        f.write("\t")
79
        f.write(hex(i)[2:])
80
        f.write("   :   ")
81
        f.write(data[i])
82
        f.write(";\n")
83
 
84
    f.write("END;\n")
85
 
86
 
87
 
88
def main(argv=None): # IGNORE:C0111
89
    '''Command line options.'''
90
 
91
    if argv is None:
92
        argv = sys.argv
93
    else:
94
        sys.argv.extend(argv)
95
 
96
    program_name = os.path.basename(sys.argv[0])
97
    program_version = "v%s" % __version__
98
    program_build_date = str(__updated__)
99
    program_version_message = '%%(prog)s %s (%s)' % (program_version, program_build_date)
100
    program_shortdesc = __import__('__main__').__doc__.split("\n")[1]
101
    program_license = '''%s
102
  Created by Bruno Gouveia on %s.
103
  Copyright 2012 organization_name. All rights reserved.
104
 
105
  Licensed under the Apache License 2.0
106
  http://www.apache.org/licenses/LICENSE-2.0
107
 
108
  Distributed on an "AS IS" basis without warranties
109
  or conditions of any kind, either express or implied.
110
USAGE
111
''' % (program_shortdesc, str(__date__))
112
 
113
    try:
114
        # Setup argument parser
115
        parser = ArgumentParser(description=program_license, formatter_class=RawDescriptionHelpFormatter)
116
        parser.add_argument(dest="inputfile", help="path to input file  [default: %(default)s]", metavar="inputfile")
117
        parser.add_argument(dest="outputfile", help="path to output file  [default: %(default)s]", metavar="outputfile")
118
        parser.add_argument(dest="wordsize", help="size of wordsize  [default: %(default)s]", metavar="wordsize", nargs='?',default=8)
119
 
120
        # Process arguments
121
        args = parser.parse_args()
122
 
123
        inputfile=args.inputfile
124
        outputfile=args.outputfile
125
        wordsize=int(args.wordsize)
126
 
127
        print "input:" , inputfile
128
        print "output:" , outputfile
129
        print "wordsize:" , wordsize
130
 
131
        result=parsefile(inputfile,wordsize)
132
        writefile(outputfile,wordsize,result[0])
133
        return 0
134
    except KeyboardInterrupt:
135
        ### handle keyboard interrupt ###
136
        return 0
137
    except Exception, e:
138
        if DEBUG or TESTRUN:
139
            raise(e)
140
        indent = len(program_name) * " "
141
        sys.stderr.write(program_name + ": " + repr(e) + "\n")
142
        sys.stderr.write(indent + "  for help use --help")
143
        return 2
144
 
145
if __name__ == "__main__":
146
    sys.exit(main())

powered by: WebSVN 2.1.0

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