-
Notifications
You must be signed in to change notification settings - Fork 4
/
patch_mem.py
152 lines (132 loc) · 4.84 KB
/
patch_mem.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
#!/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
import sys
import os
import fasm
import fasm.output
import parseutil.parse_mdd as mddutil
import parseutil.fasmread as fasmutil
import parseutil.parse_init_test as initutil
import cProfile
import pathlib
from prjxray.db import Database
from prjxray import fasm_disassembler
def patch_mem(
fasm=None,
init=None,
mdd=None,
outfile=None,
selectedMemToPatch=None,
verbose=False,
partial=False
):
assert fasm is not None
assert init is not None
assert mdd is not None
assert selectedMemToPatch is not None
mdd_data = mddutil.readAndFilterMDDData(mdd, selectedMemToPatch, verbose)
# Get all the FASM tuples
fasm_tups = read_fasm(fasm)
# Get everything BUT the INIT ones selected for patching
cleared_tups = fasmutil.clear_init(fasm_tups, mdd_data)
# Create the new tuples from initfile contents
memfasm = initutil.initfile_to_memfasm(
infile=init,
fasm_tups=fasm_tups,
memfasm_name='temp_mem.fasm',
mdd=mdd_data
)
if partial == True:
# Find any and all non-INIT tuples located at the patched BRAM
frame_tups = []
for data in mdd_data:
for tup in cleared_tups:
if tup[0] != None and tup.set_feature.feature.find(data.tile
) != -1:
frame_tups.append(tup)
# Merge the newly found non_INIT tuples located at the BRAM with the
# new memory tuples to create a partial FASM file
merged = merge_tuples(cleared_tups=frame_tups, mem_tups=memfasm)
write_fasm(outfile, merged)
print("In partial")
else:
# Merge all non-INIT tuples (cleared_tups) in with the new memory tuples
# to create a new complete FASM file
merged = merge_tuples(cleared_tups=cleared_tups, mem_tups=memfasm)
write_fasm(outfile, merged)
print("We ain't in Kansas no more")
print("Patching done...")
def write_fasm(outfile, merged_tups):
with open(outfile, 'w+') as out:
out.write(fasm.fasm_tuple_to_string(merged_tups))
def merge_tuples(cleared_tups, mem_tups):
db_path = os.getenv("XRAY_DATABASE_DIR")
db_type = os.getenv("XRAY_DATABASE")
db_path = os.path.join(db_path, db_type)
db = Database(db_path, os.getenv("XRAY_PART"))
grid = db.grid()
if type(cleared_tups) is not list:
cleared_tups = list(cleared_tups)
if type(mem_tups) is not list:
mem_tups = list(mem_tups)
all_tups = fasmutil.chain_tuples(cleared_tups, mem_tups)
merged = fasm.output.merge_and_sort(all_tups, sort_key=grid.tile_key)
return merged
def read_fasm(fname):
fasm_tuples = fasmutil.get_fasm_tups(fname)
#TODO: Why are tiles being computed if not being used?
tiles = fasmutil.get_in_use_tiles(fasm_tuples)
tiles = fasmutil.get_tile_data(tups=fasm_tuples, in_use=tiles)
return fasm_tuples
if __name__ == "__main__":
if len(sys.argv) == 6:
patch_mem(
sys.argv[1], sys.argv[2], sys.argv[3], sys.argv[4], sys.argv[5]
)
elif len(sys.argv) == 7 and sys.argv[6] == "--partial":
if sys.argv[6] == "--partial":
patch_mem(
sys.argv[1],
sys.argv[2],
sys.argv[3],
sys.argv[4],
sys.argv[5],
partial=True
)
elif sys.argv[6] == "--verbose":
patch_mem(
sys.argv[1],
sys.argv[2],
sys.argv[3],
sys.argv[4],
sys.argv[5],
verbose=True
)
else:
print(
"Usage: patch_mem fasmFile newMemContents mddFile patchedFasmFile memName [--verbose] [--partial]"
)
elif len(sys.argv) == 8:
patch_mem(
sys.argv[1], sys.argv[2], sys.argv[3], sys.argv[4], sys.argv[5],
True, True
)
else:
print(
"Usage: patch_mem fasmFile newMemContents mddFile patchedFasmFile memName [--verbose] [--partial]"
)