1 from lib2to3
.pytree
import Node
, Leaf
2 from lib2to3
.pgen2
import token
3 from lib2to3
.pygram
import python_symbols
as syms
5 preamble
= """# this file has been generated by sv2nmigen
7 from nmigen import Signal, Module, Const, Cat, Elaboratable
14 def port_decl_do_not_use(comment
, dt
, name
):
15 if dt
is None or dt
.dims
is None:
19 # XXX TODO, better checking, should be using data structure... *sigh*
20 width
= width
[1:-1] # strip brackets
21 width
= width
.split(':')
22 assert width
[0] == '0'
24 return 'self.%s = Signal(%s) # %s' % (name
, width
, comment
)
31 def __init__(self
, comment
, dt
, name
):
32 self
.comment
= comment
37 return port_decl_do_not_use(self
.comment
, self
.dt
, self
.name
)
41 def __init__(self
, left
, op
, right
):
48 def __init__(self
, outputfn
):
49 self
.outputfn
= outputfn
50 self
.outputfile
= None
56 if(self
.outputfile
is None):
57 self
.outputfile
= open(self
.outputfn
, "w")
58 self
.outputfile
.write(preamble
)
62 self
.outputfile
.write(str(p
)+"\n")
69 def assign3(self
, left
, op
, right
):
70 return Assignment(left
, op
, right
)
72 def indent(self
, count
):
74 return Leaf(token
.INDENT
, '>>> '*count
)
76 return Leaf(token
.INDENT
, ' '*4*count
)
78 def dedent(self
, count
):
79 return Leaf(token
.DEDENT
, '')
82 return Leaf(token
.NEWLINE
, '\n')
84 def port_decl(self
, comment
, dt
, name
):
85 port
= PortDecl(comment
, dt
, name
)
89 def isPort(self
, name
):
91 if(str(p
.name
) == str(name
)):
95 def initFunc(self
, ports
, params
):
96 params
= [Leaf(token
.LPAR
, '('), Leaf(
97 token
.NAME
, "self")] + [Leaf(token
.RPAR
, ')')]
98 # TODO handle sv params
99 fn
= [Leaf(token
.NAME
, 'def'),
100 Leaf(token
.NAME
, '__init__', prefix
=' '),
101 Node(syms
.parameters
, params
),
102 Leaf(token
.COLON
, ':'),
105 fndef
= Node(syms
.funcdef
, fn
)
106 stmts
= Node(syms
.stmt
, [fndef
])
108 stmts
.children
.append(self
.indent(2))
109 stmts
.children
.append(port
.initNode())
110 stmts
.children
.append(self
.nl())
113 def elaborateFunc(self
):
114 params
= [Leaf(token
.LPAR
, '('), Leaf(
115 token
.NAME
, "self, platform=None"), Leaf(token
.RPAR
, ')')]
116 fn
= [Leaf(token
.NAME
, 'def'),
117 Leaf(token
.NAME
, 'elaborate', prefix
=' '),
118 Node(syms
.parameters
, params
),
119 Leaf(token
.COLON
, ':'),
122 fndef
= Node(syms
.funcdef
, fn
)
123 stmts
= Node(syms
.stmt
, [fndef
])
124 stmts
.children
.append(self
.indent(2))
125 stmts
.children
.append(Leaf(token
.STRING
, "m = Module()"))
126 stmts
.children
.append(self
.nl())
130 hasdims
= (len(w
) >= 4)
131 stmts
.children
.append(self
.indent(2))
132 stmts
.children
.append(Leaf(token
.STRING
, wirename
))
133 stmts
.children
.append(Leaf(token
.STRING
, " = Signal("))
135 stmts
.children
.append(Leaf(token
.STRING
, str(w
[3])))
136 stmts
.children
.append(Leaf(token
.STRING
, ")"))
137 stmts
.children
.append(self
.nl())
139 for a
in self
.assign
:
140 stmts
.children
.append(self
.indent(2))
141 # m.d.sync += self.left.eq(right)
142 stmts
.children
.append(Leaf(token
.STRING
, "m.d.comb += "))
143 if(self
.isPort(a
.left
)):
144 stmts
.children
.append(Leaf(token
.STRING
, "self."))
145 stmts
.children
.append(Leaf(token
.STRING
, a
.left
))
146 stmts
.children
.append(Leaf(token
.STRING
, ".eq("))
147 if(self
.isPort(a
.right
)):
148 stmts
.children
.append(Leaf(token
.STRING
, "self."))
149 stmts
.children
.append(Leaf(token
.STRING
, a
.right
))
150 stmts
.children
.append(Leaf(token
.STRING
, ")"))
151 stmts
.children
.append(self
.nl())
153 stmts
.children
.append(self
.indent(2))
154 stmts
.children
.append(Leaf(token
.STRING
, "return m"))
155 stmts
.children
.append(self
.nl())
158 def module_1(self
, p
):
161 clsname
= [Leaf(token
.NAME
, 'class'),
162 Leaf(token
.NAME
, p
[4], prefix
=' '),
163 Leaf(token
.LPAR
, '('),
164 Leaf(token
.NAME
, 'Elaboratable'),
165 Leaf(token
.LPAR
, ')'),
166 Leaf(token
.COLON
, ':'),
170 suite
= Node(syms
.suite
, [Leaf(token
.NEWLINE
, '\n'),
172 self
.initFunc(ports
, params
),
177 clsdecl
= Node(syms
.classdef
, clsname
+ [suite
])
178 clsdecl
= Node(syms
.compound_stmt
, [clsdecl
])
180 self
.printpy(str(clsdecl
))
183 def module_item_2(self
, signaltype
, dims
, mlist
):
184 if(signaltype
== "wire"):
187 self
.wires
.append(m
+dims
)
191 def appendComments(self
, data
):
193 self
.outputfile
.write(data
)
194 #lines = data.split("\n")
196 # self.printpy("#"+line)
198 # combinatorical assign
199 def cont_assign_1(self
, p
):
200 self
.assign
+= [Assignment(p
[1], p
[2], p
[3])]
202 def always_comb(self
, p3
, p1
):
205 for s
in slist
.statements
: