-
Notifications
You must be signed in to change notification settings - Fork 769
Expand file tree
/
Copy pathget_kge.py
More file actions
executable file
·82 lines (65 loc) · 2.85 KB
/
Copy pathget_kge.py
File metadata and controls
executable file
·82 lines (65 loc) · 2.85 KB
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
#!/usr/bin/env python3
# Copyright lowRISC contributors.
# Licensed under the Apache License, Version 2.0, see LICENSE for details.
# SPDX-License-Identifier: Apache-2.0
# Parse a yosys area report and give a kGE equivalent
import argparse
def read_lib(lib_file_path, ref_cell):
with open(lib_file_path, 'r') as f:
lib_file = f.readlines()
cell_dict = {}
weighted_dict = {}
cell_name = None
for line_idx, line in enumerate(lib_file):
if line.startswith(' cell ('):
if cell_name is not None:
raise RuntimeError('{}:{} Found cell while searching for area'
.format(lib_file_path, line_idx + 1))
cell_name = line.split()[1].strip('()')
elif line.startswith('\tarea'):
if cell_name is None:
raise RuntimeError('{}:{} Found area while searching for cell'
.format(lib_file_path, line_idx + 1))
try:
cell_area = line.split()[2].strip(';')
cell_dict[cell_name] = float(cell_area)
cell_name = None
except (IndexError, ValueError):
raise RuntimeError('{}:{} Area declaration misformatted'
.format(lib_file_path, line_idx + 1))
if ref_cell not in cell_dict:
raise RuntimeError('Specified reference cell: {} was not found in '
'library: {}' .format(ref_cell, lib_file_path))
for cell in cell_dict:
weighted_dict[cell] = cell_dict[cell] / cell_dict[ref_cell]
return weighted_dict
def get_kge(report_path, weighted_dict):
with open(report_path, 'r') as f:
report = f.readlines()
ge = 0.0
for line_idx, line in enumerate(report):
data = line.split()
if len(data) < 3:
continue
cell_name = data[2]
weight = weighted_dict.get(cell_name)
if weight is not None:
try:
count = float(data[0])
ge += count * weight
except (IndexError, ValueError):
raise RuntimeError('{}:{} Cell {} matched but was misformatted'
.format(report_path, line_idx + 1, cell_name))
print("Area in kGE = ", round(ge/1000, 2))
def main():
arg_parser = argparse.ArgumentParser(
description="""Calculate kGE from a Yosys report and LIB file""")
arg_parser.add_argument('lib_file_path', help='Path to the LIB file')
arg_parser.add_argument('report_path', help='Path to the report')
arg_parser.add_argument('--cell', help='Reference cell (default:NAND2_X1)',
default='NAND2_X1')
args = arg_parser.parse_args()
weighted_dict = read_lib(args.lib_file_path, args.cell)
get_kge(args.report_path, weighted_dict)
if __name__ == "__main__":
main()