-
Notifications
You must be signed in to change notification settings - Fork 36
/
generate-ant
executable file
·167 lines (142 loc) · 4.7 KB
/
generate-ant
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
#!/usr/bin/env python3
import argparse
import struct
import os.path
class ant(object):
def __init__(self):
self.size = 0
self.entry_p = 0
self.entry_v = 0
self.asids = {}
def attach(self, asid, nn_configuration):
if asid in self.asids:
self.asids[asid].attach(nn_configuration)
else:
self.asids[asid] = ant_entry(asid)
self.asids[asid].attach(nn_configuration)
if self.size < asid + 1:
self.size = asid + 1
def print(self):
for key, value in sorted(self.asids.items()):
print("{}:".format(int(key)))
value.print()
def emit(self):
header = """\
.align 4
antp_os:
.dword 0x{:>016x} /* size_t size */
.dword asid_0 /* char * entry_p */
.dword 0x{:>016x} /* ant_entry * entry_v */
.align 4
antp_dana:\
"""
print(header.format(self.size, self.entry_v))
for a in range(self.size):
if a in self.asids:
self.asids[a].emit()
else:
self.asids[a] = ant_entry(a)
self.asids[a].emit()
class ant_entry(object):
def __init__(self, asid):
self.asid = asid
self.num_configs = 0
self.num_valid = 0
self.asid_nnid_p = 0
self.asid_nnid_v = 0
self.transaction_io = 0
self.nnids = []
def attach(self, nn_configuration):
self.num_configs += 1
self.nnids.append(nn_config(nn_configuration, self.num_valid))
self.num_valid += 1
def print(self):
for n in self.nnids:
n.print()
def emit(self):
header = """\
asid_{}:
.word 0x{:>08x} /* int num_configs */
.word 0x{:>08x} /* int num_valid */
.dword {} /* char * asid_nnid_p */
.dword 0x{:>016x} /* nn_config * asid_nnid_v */
.dword 0x{:>016x} /* io * transaction_io\ */\
"""
print(header.format(
self.asid,
self.num_configs,
self.num_valid,
("asid_{}_nnid_0").format(self.asid) if self.num_configs else ("{:>016x}").format(0),
0,
self.transaction_io))
for n in self.nnids:
n.emit(self.asid)
class nn_config(object):
def __init__(self, config, nnid):
self.size = 0
self.elements_per_block = 0
self.nnid = nnid
self.filename = config
self.bits = []
with open(config, mode='rb') as f:
bytes = 0
for word in iter(lambda: f.read(4), b''):
self.bits.append(word)
bytes += 4 * 8
self.size = int(bytes / 64) # Magic Number (TL2 beat width, I think)
# The Elements per Blcok is bits [6:4] of the first word
self.elements_per_block = 1 << (((struct.unpack("<L", self.bits[0])[0] >> 4) & 3) + 2)
def emit(self, asid):
header = """\
.align 4
asid_{}_nnid_{}:
.dword 0x{:>016x} /* size_t size */
.dword 0x{:>016x} /* size_t elements_per_block */
.dword 0x{:>016x} /* xlen_t * config_raw */
.dword asid_{}_nnid_{}_config /* char * config_p */
.dword 0x{:>016x} /* char * config_v */
.align 6
asid_{}_nnid_{}_config: /* {} */\
"""
print(header.format(asid, self.nnid,
self.size,
self.elements_per_block,
0,
asid, self.nnid,
0,
asid, self.nnid, self.filename))
list(map(lambda x: print(".word 0x{:>08x}".format(struct.unpack("<L", x)[0])), self.bits))
# class io(object):
# class queue(object):
def parse_arguments():
def asid_nn(s):
asid, nn = s.split(',')
try:
int(asid)
except ValueError:
msg = "ASID '{}' must be an int".format(asid)
raise argparse.ArgumentTypeError(msg)
if not os.path.isfile(nn):
msg = "Unable to find NN Config '{}' (did you misspell it?)".format(nn)
raise argparse.ArgumentTypeError(msg)
return int(asid), nn
parser = argparse.ArgumentParser(
description='Populate an ASID--NNID Table assembly data region')
parser.add_argument(
'--verbose', dest='verbose', action='store_true', default=False,
help='print debugging information (default: %(default)s)')
parser.add_argument(
'-a', '--attach', dest='asid_nns', type=asid_nn, action='append',
metavar="ASID,NN_FILE", required=True,
help='%(metavar)s tuples to attach (at least one is required)'),
return parser.parse_args()
def main():
args = parse_arguments()
t = ant()
for an in args.asid_nns:
t.attach(*an)
if (args.verbose):
t.print()
t.emit()
if __name__ == '__main__':
main()