-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathfasm2init.py
183 lines (160 loc) · 6.56 KB
/
fasm2init.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
#!/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: fasm2Init.py
# Author: Brent Nelson
# Created: 25 June 2020
# Description:
# Will convert a FASM file for a memory to the equivalent init.mem file
# Will compare it to the original
import os
import sys
import glob
import parseutil
import parseutil.misc as misc
import argparse
import json
import pathlib
import struct
import DbgParser
import bitMapping
import patch_mem
import re
import parseutil.misc as misc
# Check the bits for a complete memory
def fasm2init(
baseDir, # pathlib.Path
memName, # str
mdd, # pathlib.Path
initFile, # pathlib.Path
origInitFile, # if not None then will do checking between it and re-created initFile on the previous line
fasmFile, # pathlib.Path
verbose, # bool
printmappings # bool
):
designName = baseDir.name
# 0. Read the MDD data and filter out the ones we want for this memory
mdd_data = patch_mem.readAndFilterMDDData(mdd, memName)
words, initbitwidth = misc.getMDDMemorySize(mdd_data)
# 1. Get the mapping info
print("Loading mappings for {}...".format(designName))
mappings = bitMapping.createBitMappings(
baseDir, # The directory where the design lives
memName,
mdd,
False,
printmappings
)
print(" Done loading mappings")
# 2. Read the fasm file for this cell and collect the INIT/INITP lines
init0lines, init0plines, init1lines, init1plines = misc.readInitStringsFromFASMFile(
fasmFile
)
newInitBits = [[None for j in range(initbitwidth)] for k in range(words)]
# 3. Handle each cell
for cell in mdd_data:
# inits will be indexed as inits[y01][initinitp]
inits = [[None for j in range(2)] for k in range(2)]
# Convert the FASM lines into the proper format strings
# Store them in a multi-dimensional array indexed by y01 and INITP/INIT (True/False)
inits[0][False] = misc.processInitLines("0s", init0lines, cell, False)
inits[0][True] = misc.processInitLines("0ps", init0plines, cell, True)
inits[1][False] = misc.processInitLines("1s", init1lines, cell, False)
inits[1][True] = misc.processInitLines("1ps", init1plines, cell, True)
for w in range(words):
for b in range(initbitwidth):
if w < cell.addr_beg or w > cell.addr_end:
continue
if b < cell.slice_beg or b > cell.slice_end:
continue
# Get the bit from the FASM line
mapping = bitMapping.findMapping(w, b, initbitwidth, mappings)
assert mapping is not None, "{} {} {}".format(
w, b, initbitwidth
)
# Now get the actual bit
fasmbit = inits[mapping.fasmY][mapping.fasmINITP][
mapping.fasmLine][mapping.fasmBit]
# Put the bit into the array
newInitBits[w][b] = fasmbit
# 4. Now, create real init array
newInitFile = []
for w in range(words):
wd = ""
for b in range(initbitwidth):
if newInitBits[w][b] is None:
print("ERROR: None at {}:{}".format(w, b))
else:
wd += newInitBits[w][b]
newInitFile.append(wd[::-1]) # Don't forget to reverse it
# 5. Do checking if asked
if origInitFile is not None:
print(" Checking with original...")
origInit = parseutil.parse_init_test.read_initfile(
origInitFile, initbitwidth, reverse=False
)
for w in range(words):
for b in range(initbitwidth):
if newInitFile[w][b] != origInit[w][b]:
print(
"Mismatch: {}:{} {} {}".format(
w, b, newInitFile[w][b], origInit[w][b]
)
)
sys.exit(1)
print(" Everything checked out successfully!!!")
# 6. Finally, write it out
with initFile.open('w') as f:
for lin in newInitFile:
f.write(lin[::-1] + "\n")
# 7. If we got here we were successful
print(" Initfile {} re-created successfully!".format(initFile))
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument(
"baseDir", help='Directory where design sub-directories are located.'
)
parser.add_argument(
"memname", help='Name of memory to check (as in "mem/ram")'
)
parser.add_argument("mddname", help='Name of mdd file)')
parser.add_argument("--verbose", action='store_true')
parser.add_argument("--check", action='store_true')
parser.add_argument(
"--printmappings", action='store_true', help='Print the mapping info'
)
args = parser.parse_args()
baseDir = pathlib.Path(args.baseDir).resolve()
designName = baseDir.name
fasm2init(
baseDir, args.memname, baseDir / args.mddname,
baseDir / "init/fromFasm.mem",
baseDir / "init/init.mem" if args.check == True else None,
baseDir / "real.fasm", args.verbose, args.printmappings
)
print("")
#################################################################################################################
# fasm2init.py will take a .fasm file and re-create a new init.mem-type file from them.
# It deposits the new file into baseDir/init/fromFasm.mem
# If the --check flag is true it will compare what it constructs against the values from baseDir/init/init.mem (used mainly for testing)
# If there is a mismatch it will print out an error message.
# A typical run of this program is:
# python fasm2init.py testing/tests/master/128b1 mem/ram 128b1.mdd
# Or, you could do:
# python fasm2init.py testing/tests/master/128b1 mem/ram 128b1.mdd --check
# The only difference is the second one does the check against baseDir/init/init.mem.
#################################################################################################################