-
Notifications
You must be signed in to change notification settings - Fork 2
/
ml_ahb_gen.py
174 lines (133 loc) · 4.11 KB
/
ml_ahb_gen.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
# A chisel generator for hasti multi-layer interconnect
import AutoVivification as av
import json
import argparse
# The template for the main file
tpl_file="""
package Ahbmli
import Chisel._
import cde.{{Parameters, Field}}
import junctions._
class Ahbmli extends Module {{
implicit val p = Parameters.empty
val io = new Bundle {{
{master_ios}
{slave_ios}
}}
{decoding_fns}
{xbar_inst}
{master_connect}
{slave_connect}
}}
class AhbmliTests(c: Ahbmli) extends Tester(c) {{
step(1)
}}
object Ahbmli {{
def main(args: Array[String]): Unit = {{
val tutArgs = args.slice(1, args.length)
chiselMainTest(tutArgs, () => Module(new Ahbmli())) {{
c => new AhbmliTests(c) }}
}}
}}
"""
# smaller templates for code snippets (see string.format in Python doc if you are not familiar
# with the format)
tpl_master_ios = " val {m_name} = new HastiMasterIO().flip\n"
tpl_slave_ios = " val {s_name} = new HastiSlaveIO().flip\n"
tpl_master_connect = " xbar.io.masters ({m_idx}) <> io.{m_name}\n"
tpl_slave_connect = " io.{s_name} <> xbar.io.slaves({s_idx})\n"
tpl_decoding_fns = " val {fn_name} = (addr: UInt) => addr ({addr_msb},{addr_lsb}) === UInt ({addr_val})\n"
tpl_xbar_inst = " val xbar = Module(new HastiXbar({nb_masters}, Seq({fn_list})))"
#ml_ahb = av.AutoVivification()
#
#
#ml_ahb['masters'] = ['jtag','dmem','imem']
#
#ml_ahb['slaves']['codemem']['address_range'] = [31,28]
#ml_ahb['slaves']['codemem']['address_value'] = 0
#
#ml_ahb['slaves']['datamem']['address_range'] = [31,28]
#ml_ahb['slaves']['datamem']['address_value'] = 2
#
#
#
#print(json.dumps (ml_ahb,
# sort_keys=True,
# indent=4, separators=(',', ': ')))
#
def master_connect(spec):
txt = ""
for i,m in enumerate(spec['masters']):
d = dict()
d['m_name'] = m
d['m_idx'] = i
txt += tpl_master_connect.format(**d)
return txt
def slave_connect(spec):
txt = ""
for i,s in enumerate(spec['slaves'].keys()):
d = dict()
d['s_name'] = s
d['s_idx'] = i
txt += tpl_slave_connect.format(**d)
return txt
def decoding_fns(spec):
txt = ""
for i,s in enumerate(spec['slaves'].keys()):
d = dict()
d['fn_name'] = s + "_afn"
d['addr_msb'],d['addr_lsb'] = spec['slaves'][s]['address_range']
d['addr_val']= spec['slaves'][s]['address_value']
txt += tpl_decoding_fns.format(**d)
return txt
def master_ios(spec):
txt = ""
for m in spec['masters']:
d = dict()
d['m_name'] = m
txt += tpl_master_ios.format(**d)
return txt
def slave_ios(spec):
txt = ""
for s in spec['slaves'].keys():
d = dict()
d['s_name'] = s
txt += tpl_slave_ios.format(**d)
return txt
def xbar_inst(spec):
txt = ""
d = dict()
d['fn_list']= ','.join([s+"_afn" for s in spec['slaves'].keys()])
d['nb_masters'] = len(spec['masters'])
txt += tpl_xbar_inst.format(**d)
return txt
def get_args():
"""
Get command line arguments
"""
parser = argparse.ArgumentParser(description="""
A Chisel generator for HASTI (AHB multilayer interconnect)
""")
parser.add_argument('--json', action='store', dest='json',
help='JSON file for the configuartion of the interconnect')
parser.add_argument('--outdir', action='store', dest='outdir',
help='Directory where to store the chisel file')
parser.add_argument('--version', action='version', version='%(prog)s 0.1')
return parser.parse_args()
if __name__ == '__main__':
args = get_args()
ml_ahb = None
with open(args.json) as f:
print "-I- Reading JSON file " + args.json
ml_ahb = json.load(f)
d=dict()
d['master_connect'] = master_connect(ml_ahb)
d['slave_connect'] = slave_connect(ml_ahb)
d['decoding_fns'] = decoding_fns(ml_ahb)
d['xbar_inst'] = xbar_inst(ml_ahb)
d['master_ios'] = master_ios(ml_ahb)
d['slave_ios'] = slave_ios(ml_ahb)
txt = tpl_file.format(**d)
outfile_name = args.outdir + "/Ahbmli.scala"
with open(outfile_name,'w') as f:
f.write(txt)