-
Notifications
You must be signed in to change notification settings - Fork 4
/
genh.py
272 lines (240 loc) · 9.75 KB
/
genh.py
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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# Copyright 2020-2022 F4PGA Authors
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
# SPDX-License-Identifier: Apache-2.0
# File: genh.py
# Author: Brent Nelson
# Created: 25 June 2020
# Description:
# Outputs .h file format
import bitMapping
import pathlib
import argparse
import patch_mem
import os
import json
import parseutil
import parseutil.misc as misc
class Mem:
def __init__(self, name, words, bits):
self.name = name
self.words = words
self.bits = bits
def toString(self):
return "name={}, words={}, bits = {}".format(
self.name, self.words, self.bits
)
##############################################################################################
# Create the bitmappings for a design
##############################################################################################
def genh(
mddName,
memName,
words, # Number of words in init.mem file
bits, # Number of bits per word in init.memfile
verbose,
printMappings
):
# 1. Load the MDD file.
mdd_data = patch_mem.readAndFilterMDDData(mddName, memName)
# Add some info to the mdd_data
tilegridname = os.environ["XRAY_DIR"] + "/database/" + os.environ[
"XRAY_DATABASE"] + "/" + os.environ["XRAY_PART"] + "/tilegrid.json"
with open(tilegridname) as f:
tilegrid = json.load(f)
for m in mdd_data:
tilegridinfo = tilegrid[m.tile]
m.baseaddr = int(tilegridinfo["bits"]["BLOCK_RAM"]["baseaddr"], 16)
m.numframes = int(tilegridinfo["bits"]["BLOCK_RAM"]["frames"])
# 2. Load the segment data from the prjxray database.
# This uses the environment variables set by prjxray
# Passing it into createBitMappings() each time will save a lot of time since it can be reused for all
# the BRAMs in a design.
segs = bitMapping.loadSegs()
# 3. Define the data structure to hold the mappings that are returned.
# Format is: [word, bit, tileName, bits (width of each init.mem word), fasmY, fasmINITP, fasmLine, fasmBit, frameAddr, frameBitOffset]
mappings = []
# 4. Create the bitmappings for each BRAM Primitive
if printMappings:
print("Cell = {}".format(memName))
for cell in mdd_data:
mappings = bitMapping.createBitMapping(
segs, # The segs info
words, # Depth of memory
bits, # Width of memory
cell, # The BRAM primitive to process
mappings, # The returned mappings data structure
verbose,
printMappings
)
# Inner function for use in sort below
def mapSort(m):
# Need a key that is ascending for the order we want
return m.word * m.bits + m.bit
# 5. Sort the mappings to enable fast lookups
mappings.sort(key=mapSort)
return (mappings, mdd_data)
# This is the main driver program to generate the .c and .h files
if __name__ == "__main__":
# Method to figure out if string already in the ranges set
def inRanges(ranges, s):
for r in ranges:
if r == s:
return True
return False
parser = argparse.ArgumentParser()
parser.add_argument("mddname", help='Name of mdd file to use')
#parser.add_argument("memname", help='Name of memory')
parser.add_argument(
'outfile',
help='Name root of .h and .c files to write (without extension)'
)
parser.add_argument("--verbose", action='store_true')
parser.add_argument(
"--extendedoutput",
help='Print out short mapping record info into .c file',
action='store_true'
)
parser.add_argument(
"--printmappings", action='store_true', help='Print the mapping info'
)
args = parser.parse_args()
# Make a list of the memories in the design and their sizes
mddMemoryNames = misc.getMDDMemories(args.mddname)
print("Here are the memories in this design:")
for m in mddMemoryNames.keys():
print(" {} = {}".format(m, mddMemoryNames[m]))
# Now, make 3 parallel lists: the memory names, the mappings for each memory, the mdd entries for each memory
# We will loop across those below
memnamesLst = []
mappingsLst = []
mddsLst = []
for m in mddMemoryNames.keys():
tmp_mappings, tmp_mdd_data = genh(
args.mddname, m, int(mddMemoryNames[m][0]),
int(mddMemoryNames[m][1]), args.verbose, args.printmappings
)
mappingsLst.append(tmp_mappings)
mddsLst.append(tmp_mdd_data)
memnamesLst.append(m)
# Now output the .h file
with open(args.outfile + ".h", 'w') as f:
f.write('#include "bert_types.h"\n\n')
f.write('#define NUM_LOGICAL {}\n'.format(len(mddMemoryNames.keys())))
f.write('\n')
f.write("// local name for each memory\n")
for i, m in enumerate(mddMemoryNames.keys()):
s = m.replace("/", "_")
f.write('#define {} {}\n'.format(s.upper(), i))
f.write('\n')
f.write("extern const char * logical_names[NUM_LOGICAL];\n")
f.write(
"extern struct logical_memory logical_memories[NUM_LOGICAL];\n\n"
)
# Next, output the .c file
with open(args.outfile + ".c", "w") as f:
f.write('#include "bert_types.h"\n\n')
f.write('#define NUM_LOGICAL {}\n'.format(len(mddMemoryNames.keys())))
f.write('\n')
f.write("// local name for each memory\n")
for i, m in enumerate(mddMemoryNames.keys()):
s = m.replace("/", "_")
f.write('#define {} {}\n'.format(s.upper(), i))
f.write('\n')
f.write('const char * logical_names[]={\n')
for i, m in enumerate(mddMemoryNames.keys()):
if i < len(mddMemoryNames.keys()) - 1:
f.write(" \"" + "/top/" + m + "\"" + ",\n")
else:
f.write(" \"" + "/top/" + m + "\"" + "\n")
f.write("};\n\n")
# Loop across each memory to output the needed information
for i in range(len(mappingsLst)):
mdd_data = mddsLst[i]
mappings = mappingsLst[i]
memname = memnamesLst[i]
# Figure out the ranges of frames for a given memory
# Be sure to eliminate duplicates
numRanges = len(mdd_data)
ranges = set()
for m in mdd_data:
s = "{" + "0x{:08x},{}".format(m.baseaddr, m.numframes) + "}"
if not inRanges(ranges, s):
ranges.add(s)
# Output those ranges for this memory
f.write(
'struct frame_range mem{}_frame_ranges[{}]= \n'.format(
i, len(ranges)
)
)
f.write("{\n")
for j, r in enumerate(ranges):
if j < len(ranges) - 1:
f.write(" " + r + ",\n")
else:
f.write(" " + r + "\n")
f.write("};\n\n")
# Output the actual mappings now. This is taken from the mappings info returned from calling createBitMapping() above.
# They are output in word/bit order as in word0/bit0, word0/bit1, ..., word1/bit0, word1/bit1, ...
f.write(
'struct bit_loc mem{}_bitlocs[{}]='.format(
i, mddMemoryNames[memname][0] * mddMemoryNames[memname][1]
) + '{\n'
)
for j, m in enumerate(mappings):
if j < len(mappings) - 1:
s = ' {' + '0x{:08x}, '.format(
m.frameAddr
) + '{:6d}'.format(m.frameBitOffset) + '},'
s += ' \t// [{}][{}]'.format(m.word, m.bit)
if args.extendedoutput:
s += ' \t ' + m.toString()
else:
s = ' {' + '0x{:08x}, '.format(
m.frameAddr
) + '{:6d}'.format(m.frameBitOffset) + '},'
s += ' \t// [{}][{}]'.format(m.word, m.bit)
if args.extendedoutput:
s += ' \t' + m.toString()
f.write(s + "\n")
f.write("};\n")
f.write("\n")
# Finally write the struct info for each memory and be done
f.write('struct logical_memory logical_memories[NUM_LOGICAL] =\n')
f.write('{\n')
for i in range(len(mappingsLst)):
f.write(' {')
if i < len(mappingsLst) - 1:
f.write(
'{},{},{},mem{}_frame_ranges,mem{}_bitlocs'.format(
len(ranges), mddMemoryNames[memname][1],
mddMemoryNames[memname][0], i, i
)
)
f.write('},')
s = memnamesLst[i].replace("/", "_")
f.write(' // {} {}\n'.format(s.upper(), i))
else:
f.write(
'{},{},{},mem{}_frame_ranges,mem{}_bitlocs'.format(
len(ranges), mddMemoryNames[memname][1],
mddMemoryNames[memname][0], i, i
)
)
f.write('}')
s = memnamesLst[i].replace("/", "_")
f.write(' // {} {}'.format(s.upper(), i))
f.write('\n};\n')