remove rc and lk columns
[libreriscv.git] / openpower / sv_analysis.py
1 #!/usr/bin/env python3
2 import csv
3 import os
4 from os.path import dirname, join
5 from glob import glob
6 from collections import OrderedDict
7
8
9 def find_wiki_file(name):
10 filedir = os.path.dirname(os.path.abspath(__file__))
11 tabledir = join(filedir, 'isatables')
12 file_path = join(tabledir, name)
13 return file_path
14
15 def get_csv(name):
16 file_path = find_wiki_file(name)
17 with open(file_path, 'r') as csvfile:
18 reader = csv.DictReader(csvfile)
19 return list(reader)
20
21 def blank_key(row):
22 #for v in row.values():
23 # if 'SPR' in v: # skip all SPRs
24 # return True
25 for v in row.values():
26 if v:
27 return False
28 return True
29
30
31 keycolumns = ['in1', 'in2', 'in3', 'out', 'CR in', 'CR out',
32 'ldst len'] # don't think we need these: , 'rc', 'lk']
33
34 def create_key(row):
35 res = OrderedDict()
36 #print ("row", row)
37 for key in keycolumns:
38 # registers IN
39 if key in ['in1', 'in2', 'in3']:
40 # TODO: replace this with a counter row['in']
41 # will need row['in'] initialising to 0 *outside* of the for-loop
42 if row[key].startswith('R'):
43 res[key] = 'R'
44 else:
45 res[key] = '0'
46 # registers OUT
47 if key == 'out':
48 if row[key].startswith('R'):
49 res[key] = 'R'
50 else:
51 res[key] = '0'
52 # CRs
53 if key.startswith('CR'):
54 if row[key].startswith('NONE'):
55 res[key] = '0'
56 else:
57 res[key] = '1'
58 # LDST len
59 if key.startswith('ldst'):
60 if row[key].startswith('NONE'):
61 res[key] = '0'
62 else:
63 res[key] = '1'
64 # rc, lk
65 if key in ['rc', 'lk']:
66 if row[key] == 'ONE':
67 res[key] = '1'
68 elif row[key] == 'NONE':
69 res[key] = '0'
70 else:
71 res[key] = 'R'
72 if key == 'lk':
73 res[key] = row[key]
74
75 return res
76
77 def dformat(d):
78 res = []
79 for k, v in d.items():
80 res.append("%s: %s" % (k, v))
81 return ' '.join(res)
82
83 def tformat(d):
84 return ' | '.join(d) + "|"
85
86
87 def process_csvs():
88 csvs = {}
89 bykey = {}
90 primarykeys = set()
91 dictkeys = OrderedDict()
92
93 pth = find_wiki_file("*.csv")
94 for fname in glob(pth):
95 if 'valid' in fname:
96 continue
97 if 'test' in fname:
98 continue
99 if 'sprs' in fname:
100 continue
101 #print (fname)
102 csvname = os.path.split(fname)[1]
103 csv = get_csv(fname)
104 csvs[fname] = csv
105 for row in csv:
106 if blank_key(row):
107 continue
108 dkey = create_key(row)
109 key = tuple(dkey.values())
110 dictkeys[key] = dkey
111 primarykeys.add(key)
112 if key not in bykey:
113 bykey[key] = []
114 bykey[key].append((csvname, row['opcode'], row['comment'],
115 row['form'].upper() + '-Form'))
116
117 primarykeys = list(primarykeys)
118 primarykeys.sort()
119
120 print ("# keys")
121 print ()
122 print ('[[!table data="""')
123 print (tformat(keycolumns)) # TODO use alternative here which has
124 # 'in' rather than in1, in2, in3
125
126 for key in primarykeys:
127 print (tformat(dictkeys[key].values()))
128 print ('"""]]')
129 print ()
130
131 for key in primarykeys:
132 print ("## ", dformat(dictkeys[key]))
133 print ()
134 print ('[[!table data="""')
135 print (tformat(['CSV', 'opcode', 'asm', 'form']))
136 rows = bykey[key]
137 rows.sort()
138 for row in rows:
139 print (tformat(row))
140 print ('"""]]')
141 print ()
142
143 bykey = {}
144 for fname, csv in csvs.items():
145 key
146
147 if __name__ == '__main__':
148 process_csvs()