1 # Based on GardenSnake - a parser generator demonstration program
2 # GardenSnake was released into the Public Domain by Andrew Dalke.
4 # Portions of this work are derived from Python's Grammar definition
5 # and may be covered under the Python copyright and license
7 # Andrew Dalke / Dalke Scientific Software, LLC
8 # 30 August 2006 / Cape Town, South Africa
10 # Modifications for inclusion in PLY distribution
12 from pprint
import pprint
14 from ply
import lex
, yacc
18 from soc
.decoder
.power_decoder
import create_pdecode
19 from nmigen
.back
.pysim
import Simulator
, Delay
20 from nmigen
import Module
, Signal
22 from soc
.decoder
.pseudo
.parser
import GardenSnakeCompiler
23 from soc
.decoder
.selectable_int
import SelectableInt
, selectconcat
25 ####### Test code #######
30 index <- (RS)[8*i:8*i+7]
31 RA <- [0]*56 || perm[0:7]
36 if index < 64 then index <- 0
47 index <- (RS)[8*i:8*i+7]
52 RA <- [0]*56|| perm[0:7]
59 if (RS)[63-n] = 0b1 then
69 else if a > EXTS(SI) then
78 in_range <- ((x | y) &
80 in_range <- (x + y) - (a + b)
85 src1 <- EXTZ((RA)[56:63])
87 in_range <- src21lo <= src1 & src1 <= src21hi
101 RT <- (RA) + EXTS(SI || [0]*16)
116 l
.append(1 if (num
& (1 << i
)) else 0)
121 def get_reg_hex(reg
):
122 return hex(reg
.value
)
126 def __init__(self
, sd
, regfile
):
129 self
.regfile
= regfile
131 self
[i
] = SelectableInt(0, 64)
133 def set_form(self
, form
):
136 def ___getitem__(self
, attr
):
137 print("GPR getitem", attr
)
138 getform
= self
.sd
.sigforms
[self
.form
]
139 rnum
= getattr(getform
, attr
)
140 print("GPR get", rnum
, rnum
, dir(rnum
))
142 print(l
[0]._as
_const
())
144 #print (x, x.value, dir(x))
145 #print (x.value, dir(x.value))
147 return self
.regfile
[rnum
]
150 def convert_to_python(pcode
):
152 gsc
= GardenSnakeCompiler()
154 tree
= gsc
.compile(pcode
, mode
="exec", filename
="string")
155 tree
= ast
.fix_missing_locations(tree
)
156 regsused
= {'read_regs': gsc
.parser
.read_regs
,
157 'write_regs': gsc
.parser
.write_regs
,
158 'uninit_regs': gsc
.parser
.uninit_regs
}
159 return astor
.to_source(tree
), regsused
164 gsc
= GardenSnakeCompiler()
166 # XXX unused! see GPR instead
170 gsc
.gpr
= GPR(gsc
.parser
.sd
, gsc
.regfile
)
172 _compile
= gsc
.compile
174 tree
= _compile(code
, mode
="single", filename
="string")
175 tree
= ast
.fix_missing_locations(tree
)
176 print(ast
.dump(tree
))
179 print(astor
.dump_tree(tree
))
181 source
= astor
.to_source(tree
)
186 # Set up the GardenSnake run-time environment
189 print("-->", " ".join(map(str, args
)))
191 from soc
.decoder
.helpers
import (EXTS64
, EXTZ64
, ROTL64
, ROTL32
, MASK
,)
197 d
["SelectableInt"] = SelectableInt
198 d
["concat"] = selectconcat
202 gsc
.gpr
.set_form(form
)
203 getform
= gsc
.parser
.sd
.sigforms
[form
]._asdict
()
204 #print ("getform", form)
205 # for k, f in getform.items():
209 compiled_code
= compile(source
, mode
="exec", filename
="<string>")
213 instruction
= Signal(32)
215 m
.submodules
.decode
= decode
= gsc
.parser
.sd
216 comb
+= decode
.raw_opcode_in
.eq(instruction
)
223 print("0x{:X}".format(ins
& 0xffffffff))
225 # ask the decoder to decode this binary data (endian'd)
226 yield decode
.bigendian
.eq(0) # little / big?
227 yield instruction
.eq(ins
) # raw binary instr.
230 # uninitialised regs, drop them into dict for function
231 for rname
in gsc
.parser
.uninit_regs
:
232 d
[rname
] = SelectableInt(0, 64) # uninitialised (to zero)
233 print("uninitialised", rname
, get_reg_hex(d
[rname
]))
235 # read regs, drop them into dict for function
236 for rname
in gsc
.parser
.read_regs
:
237 regidx
= yield getattr(decode
.sigforms
['X'], rname
)
238 d
[rname
] = gsc
.gpr
[regidx
]
239 print("read reg", rname
, regidx
, get_reg_hex(d
[rname
]))
241 exec(compiled_code
, d
) # code gets executed here in dict "d"
244 print(d
.keys()) # shows the variables that may have been created
246 print(decode
.sigforms
['X'])
247 x
= yield decode
.sigforms
['X'].RS
248 ra
= yield decode
.sigforms
['X'].RA
249 print("RA", ra
, d
['RA'])
252 for wname
in gsc
.parser
.write_regs
:
254 print("write regs", wname
, d
[wname
], reg
)
256 gsc
.gpr
[regidx
] = d
[wname
]
258 sim
.add_process(process
)
259 with sim
.write_vcd("simulator.vcd", "simulator.gtkw",
260 traces
=decode
.ports()):
263 for i
in range(len(gsc
.gpr
)):
264 print("regfile", i
, get_reg_hex(gsc
.gpr
[i
]))
267 if __name__
== '__main__':