2018-10-19 23:24:02 +02:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
|
'''
|
|
|
|
|
Takes input FASM and outputs optionally canonical FASM.
|
|
|
|
|
'''
|
|
|
|
|
|
|
|
|
|
import os
|
|
|
|
|
import fasm
|
|
|
|
|
from prjxray import db
|
|
|
|
|
|
2018-10-20 01:19:22 +02:00
|
|
|
|
2018-10-19 23:24:02 +02:00
|
|
|
def process_fasm(db_root, fasm_file, canonical):
|
|
|
|
|
database = db.Database(db_root)
|
|
|
|
|
grid = database.grid()
|
|
|
|
|
|
|
|
|
|
for fasm_line in fasm.parse_fasm_filename(fasm_file):
|
|
|
|
|
if not fasm_line.set_feature:
|
|
|
|
|
if not canonical:
|
|
|
|
|
yield fasm_line
|
|
|
|
|
|
|
|
|
|
for feature in fasm.canonical_features(fasm_line.set_feature):
|
|
|
|
|
parts = feature.feature.split('.')
|
|
|
|
|
tile = parts[0]
|
|
|
|
|
|
|
|
|
|
gridinfo = grid.gridinfo_at_tilename(tile)
|
|
|
|
|
tile_segbits = database.get_tile_segbits(gridinfo.tile_type)
|
|
|
|
|
|
|
|
|
|
address = 0
|
|
|
|
|
if feature.start is not None:
|
|
|
|
|
address = feature.start
|
|
|
|
|
|
2018-10-20 01:19:22 +02:00
|
|
|
feature_name = '{}.{}'.format(
|
|
|
|
|
gridinfo.tile_type, '.'.join(parts[1:]))
|
2018-10-19 23:24:02 +02:00
|
|
|
|
|
|
|
|
# Convert feature to bits. If no bits are set, feature is
|
|
|
|
|
# psuedo pip, and should not be output from canonical FASM.
|
2018-10-20 01:19:22 +02:00
|
|
|
bits = tuple(
|
|
|
|
|
tile_segbits.feature_to_bits(feature_name, address=address))
|
2018-10-19 23:24:02 +02:00
|
|
|
if len(bits) == 0 and canonical:
|
|
|
|
|
continue
|
|
|
|
|
|
|
|
|
|
# In canonical output, only output the canonical features.
|
|
|
|
|
if canonical:
|
|
|
|
|
yield fasm.FasmLine(
|
2018-10-20 01:19:22 +02:00
|
|
|
set_feature=feature,
|
|
|
|
|
annotations=None,
|
|
|
|
|
comment=None,
|
|
|
|
|
)
|
2018-10-19 23:24:02 +02:00
|
|
|
|
|
|
|
|
# If not in canonical mode, output original FASM line
|
|
|
|
|
if not canonical:
|
|
|
|
|
yield fasm_line
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def run(db_root, fasm_file, canonical):
|
2018-10-20 01:19:22 +02:00
|
|
|
print(
|
|
|
|
|
fasm.fasm_tuple_to_string(
|
|
|
|
|
process_fasm(db_root, fasm_file, canonical), canonical=canonical))
|
|
|
|
|
|
2018-10-19 23:24:02 +02:00
|
|
|
|
|
|
|
|
def main():
|
|
|
|
|
import argparse
|
|
|
|
|
|
|
|
|
|
parser = argparse.ArgumentParser(
|
2018-10-20 01:19:22 +02:00
|
|
|
description=
|
|
|
|
|
'Read FASM file, check against database, and output optionally canonical FASM.'
|
|
|
|
|
)
|
2018-10-19 23:24:02 +02:00
|
|
|
|
|
|
|
|
database_dir = os.getenv("XRAY_DATABASE_DIR")
|
|
|
|
|
database = os.getenv("XRAY_DATABASE")
|
|
|
|
|
db_root_kwargs = {}
|
|
|
|
|
if database_dir is None or database is None:
|
|
|
|
|
db_root_kwargs['required'] = True
|
|
|
|
|
else:
|
|
|
|
|
db_root_kwargs['required'] = False
|
|
|
|
|
db_root_kwargs['default'] = os.path.join(database_dir, database)
|
|
|
|
|
|
2018-10-20 01:19:22 +02:00
|
|
|
parser.add_argument('--db_root', help="Database root.", **db_root_kwargs)
|
2018-10-19 23:24:02 +02:00
|
|
|
parser.add_argument('fasm_file', help='Input FASM file')
|
2018-10-20 01:19:22 +02:00
|
|
|
parser.add_argument(
|
|
|
|
|
'--canonical', help='Output canonical bitstream.', action='store_true')
|
2018-10-19 23:24:02 +02:00
|
|
|
args = parser.parse_args()
|
|
|
|
|
|
|
|
|
|
run(args.db_root, args.fasm_file, args.canonical)
|
|
|
|
|
|
2018-10-20 01:19:22 +02:00
|
|
|
|
2018-10-19 23:24:02 +02:00
|
|
|
if __name__ == '__main__':
|
|
|
|
|
main()
|