e5d1a9697600bcc0032a61b6289f4a72c70da094
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]
58 if (RS)[63-n] = 0b1 then
68 else if a > EXTS(SI) then
85 l
.append(1 if (num
& (1<<i
)) else 0)
95 def __init__(self
, sd
, regfile
):
98 self
.regfile
= regfile
100 self
[i
] = SelectableInt(0, 64)
102 def set_form(self
, form
):
105 def ___getitem__(self
, attr
):
106 print ("GPR getitem", attr
)
107 getform
= self
.sd
.sigforms
[self
.form
]
108 rnum
= getattr(getform
, attr
)
109 print ("GPR get", rnum
, rnum
, dir(rnum
))
111 print (l
[0]._as
_const
())
113 #print (x, x.value, dir(x))
114 #print (x.value, dir(x.value))
116 return self
.regfile
[rnum
]
119 def convert_to_python(pcode
):
121 gsc
= GardenSnakeCompiler()
123 tree
= gsc
.compile(pcode
, mode
="exec", filename
="string")
124 tree
= ast
.fix_missing_locations(tree
)
125 return astor
.to_source(tree
)
130 gsc
= GardenSnakeCompiler()
132 # XXX unused! see GPR instead
136 gsc
.gpr
= GPR(gsc
.parser
.sd
, gsc
.regfile
)
138 _compile
= gsc
.compile
140 tree
= _compile(code
, mode
="single", filename
="string")
141 tree
= ast
.fix_missing_locations(tree
)
142 print ( ast
.dump(tree
) )
145 print (astor
.dump_tree(tree
))
147 source
= astor
.to_source(tree
)
152 # Set up the GardenSnake run-time environment
155 print ("-->", " ".join(map(str,args
)))
157 from soc
.decoder
.helpers
import (EXTS64
, EXTZ64
, ROTL64
, ROTL32
, MASK
,)
163 d
["SelectableInt"] = SelectableInt
164 d
["concat"] = selectconcat
168 gsc
.gpr
.set_form(form
)
169 getform
= gsc
.parser
.sd
.sigforms
[form
]._asdict
()
170 #print ("getform", form)
171 #for k, f in getform.items():
175 compiled_code
= compile(source
, mode
="exec", filename
="<string>")
179 instruction
= Signal(32)
181 m
.submodules
.decode
= decode
= gsc
.parser
.sd
182 comb
+= decode
.raw_opcode_in
.eq(instruction
)
189 print("0x{:X}".format(ins
& 0xffffffff))
191 # ask the decoder to decode this binary data (endian'd)
192 yield decode
.bigendian
.eq(0) # little / big?
193 yield instruction
.eq(ins
) # raw binary instr.
196 # read regs, drop them into dict for function
197 for rname
in gsc
.parser
.read_regs
:
198 regidx
= yield getattr(decode
.sigforms
['X'], rname
)
199 d
[rname
] = gsc
.gpr
[regidx
]
200 print ("read reg", rname
, regidx
, get_reg_hex(d
[rname
]))
202 exec (compiled_code
, d
)
207 print (decode
.sigforms
['X'])
208 x
= yield decode
.sigforms
['X'].RS
209 ra
= yield decode
.sigforms
['X'].RA
210 print ("RA", ra
, d
['RA'])
213 for wname
in gsc
.parser
.write_regs
:
215 print ("write regs", wname
, d
[wname
], reg
)
217 gsc
.gpr
[regidx
] = d
[wname
]
219 sim
.add_process(process
)
220 with sim
.write_vcd("simulator.vcd", "simulator.gtkw",
221 traces
=decode
.ports()):
224 for i
in range(len(gsc
.gpr
)):
225 print ("regfile", i
, get_reg_hex(gsc
.gpr
[i
]))
228 if __name__
== '__main__':