File size: 5,453 Bytes
7df9186
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
import datetime
import os
import subprocess
import getopt
import sys
import re


def config_romx(dsp_flavor=0, tool_rev=0, board_type=0):
    if isinstance(tool_rev, str) and tool_rev.isdigit():
        tool_rev = eval(tool_rev)
    else:
        tool_rev = 0
    if isinstance(dsp_flavor, str) and dsp_flavor.isdigit():
        dsp_flavor = eval(dsp_flavor)
    else:
        dsp_flavor = 0
    # HARDWARE mnemonics
    board_dict = {
        'mebt': 1,
        'interim': 2,
        'fcm': 3,
        'avnet': 4,
        'uxo': 5,
        'llrf4': 6,
        'av5t': 7,
        'sp601': 8,
        'sp605': 9,
        'ml505': 10,
        'ml506': 11,
        'fllrf': 12,
        'spec': 13,
        'lx150t': 14,
        'cutewr': 15,
        'llrf46': 16,
        'ac701': 17,
        'ml605': 18,
        'kc705': 19,
        'bmb7_kintex': 20,
        'qf2_kintex': 21,
        'vc707': 22,
        'test': 99
    }
    print('board_type {}'.format(board_type))
    board_name = board_type  # maybe
    if board_type is None:
        board_type = 0
    elif isinstance(board_type, str):
        if board_type in board_dict:
            board_type = board_dict[board_type]
        elif board_type.isdigit():
            board_type = eval(board_type)
        else:
            board_type = 0
    else:
        board_type = 0
    # NUM codes, must be less than 256
    user_dict = {
        'ldoolitt': 1,
        'larry': 1,
        'lrdoolit': 1,
        'cswanson': 2,
        'kasemir': 3,
        'hengjie': 4,
        'qcf': 4,
        'crofford': 5,
        'meddeler': 6,
        'baptiste': 7,
        'llrf_oper': 8,
        'hyaver': 9,
        'dim': 10,
        'begcbp': 11,
        'ghuang': 12,
        'luser': 13,
        'kstefan': 14,
        'cserrano': 15,
        'asalom': 16,
        'du': 17,
        'yangjin': 18,
        'lilima': 19,
        'ernesto': 20,
        'vkvytla': 21
    }
    try:
        # p3 the output is bytes, changing that to a regular string
        git_sha = subprocess.check_output(['git', 'rev-parse',
                                           'HEAD']).decode("utf-8")
    except Exception:
        git_sha = 40 * 'f'
    print(git_sha)
    version_number = 0
    try:
        git_version = subprocess.check_output(['git', 'describe', '--tag', '--always'])
        m = re.match(r'\D*(\d*)\.[\s\S]*', git_version)
        if m:
            version_number = eval(m.group(1))
    except subprocess.CalledProcessError:
        print('config_crunch.py: Subprocess ERROR')
    except Exception:
        print('config_crunch.py: Generic exception, using version_number 0')

    try:
        code_is_dirty = 0 if ('nothing' in subprocess.check_output(
            ['git', 'status']).split('\n')[-2]) else 1
    except Exception:
        code_is_dirty = 0

    user = os.getenv('USER', 'NONE')
    datatimestr = datetime.datetime.now()
    year = format(int(datatimestr.strftime('%y')), '02x')
    month = format(int(datatimestr.strftime('%m')), '02x')
    date = format(int(datatimestr.strftime('%d')), '02x')
    hour = format(int(datatimestr.strftime('%H')), '02x')
    minute = format(int(datatimestr.strftime('%M')), '02x')
    comments = [
        'magic', 'dsp flavor', 'year', 'month', 'day', 'hour', 'minute',
        'code is (dirty), () commits after the latest tag',
        'tool rev (%s)' % tool_rev,
        'user (%s)' % user,
        'board type (%s)' % board_name, 'version number',
        'start of git commit SHA1'
    ] + ['...'] * 18 + ['end of git commit SHA1']

    str_config = ''.join([
        '55',
        format(dsp_flavor, '02x'),
        year,
        month,
        date,
        hour,
        minute,
        # datetime.datetime.now().strftime('%y%m%d%H%M'),
        format(code_is_dirty, '02x'),
        format(tool_rev, '02x'),
        # format(user, '02x'),
        format(user_dict[user] if user in user_dict else 99, '02x'),
        format(board_type, '02x'),
        format(version_number, '02x'),
        git_sha[:40]
        # format(git_sha, '040x')
    ])

    config_case = '\n'.join([
        '''\t5'h%s: data = 8'h%s; // %s''' %
        (format(i, '02x'), str_config[i * 2:i * 2 + 2], comments[i])
        for i in range(32)
    ])

    # print ''.join(str_config)
    # print config_case
    config_romx_v = '''\
// Machine generated by python config_crunch.py
`timescale 1ns / 1ns

module config_romx(
\tinput [4:0] address,
\toutput reg [7:0] data
);

always @(address) case(address)
{}
endcase

endmodule
'''
    Y = config_romx_v.format(config_case)
    return Y


def usage():
    print('python config_crunch.py --HARDWARE aaa --TOOL bbb --DSP_FLAVOR ccc')


if __name__ == "__main__":
    opts, args = getopt.getopt(
        sys.argv[1:], 'h:t:f:o:',
        ['HARDWARE=', 'TOOL=', 'DSP_FLAVOR=', 'OUTPUT='])
    dsp_flavor = 2
    tool_rev = '1.0'
    board_type = 'BOARD_llrf4'
    filename = 'config_romx.v'
    for opt, arg in opts:
        if opt in ('-h', '--HARDWARE'):
            board_type = arg
        elif opt in ('-t', '--TOOL'):
            tool_rev = arg
        elif opt in ('-f', '--DSP_FLAVOR'):
            dsp_flavor = arg
        elif opt in ('-o', '--OUTPUT'):
            filename = arg
        else:
            print('BAD')
            usage()
            exit(1)
    with open(filename, 'w') as f:
        f.write(
            config_romx(
                dsp_flavor=dsp_flavor, tool_rev=tool_rev, board_type=board_type))