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
17 from soc
.decoder
.power_decoder
import create_pdecode
18 from nmigen
.back
.pysim
import Simulator
, Delay
19 from nmigen
import Module
, Signal
21 from soc
.decoder
.pseudo
.parser
import GardenSnakeCompiler
22 from soc
.decoder
.selectable_int
import SelectableInt
24 ####### Test code #######
29 index <- (RS)[8*i:8*i+7]
30 RA <- [0]*56 || perm[0:7]
35 if index < 64 then index <- 0
46 index <- (RS)[8*i:8*i+7]
51 RA <- [0]*56|| perm[0:7]
57 if (RS)[63-n] = 0b1 then
71 l
.append(1 if (num
& (1<<i
)) else 0)
81 def __init__(self
, sd
, regfile
):
84 self
.regfile
= regfile
86 self
[i
] = SelectableInt(0, 64)
88 def set_form(self
, form
):
91 def ___getitem__(self
, attr
):
92 print ("GPR getitem", attr
)
93 getform
= self
.sd
.sigforms
[self
.form
]
94 rnum
= getattr(getform
, attr
)
95 print ("GPR get", rnum
, rnum
, dir(rnum
))
97 print (l
[0]._as
_const
())
99 #print (x, x.value, dir(x))
100 #print (x.value, dir(x.value))
102 return self
.regfile
[rnum
]
107 gsc
= GardenSnakeCompiler()
109 # XXX unused! see GPR instead
113 gsc
.gpr
= GPR(gsc
.parser
.sd
, gsc
.regfile
)
115 _compile
= gsc
.compile
117 tree
= _compile(code
, mode
="single", filename
="string")
119 tree
= ast
.fix_missing_locations(tree
)
120 print ( ast
.dump(tree
) )
123 print (astor
.dump_tree(tree
))
125 source
= astor
.to_source(tree
)
130 # Set up the GardenSnake run-time environment
133 print ("-->", " ".join(map(str,args
)))
135 def listconcat(l1
, l2
):
138 from soc
.decoder
.helpers
import (EXTS64
, EXTZ64
, ROTL64
, ROTL32
, MASK
,)
144 d
["concat"] = listconcat
148 gsc
.gpr
.set_form(form
)
149 getform
= gsc
.parser
.sd
.sigforms
[form
]._asdict
()
150 #print ("getform", form)
151 #for k, f in getform.items():
155 compiled_code
= compile(source
, mode
="exec", filename
="<string>")
159 instruction
= Signal(32)
161 m
.submodules
.decode
= decode
= gsc
.parser
.sd
162 comb
+= decode
.raw_opcode_in
.eq(instruction
)
169 print("0x{:X}".format(ins
& 0xffffffff))
171 # ask the decoder to decode this binary data (endian'd)
172 yield decode
.bigendian
.eq(0) # little / big?
173 yield instruction
.eq(ins
) # raw binary instr.
176 # read regs, drop them into dict for function
177 for rname
in gsc
.parser
.read_regs
:
178 regidx
= yield getattr(decode
.sigforms
['X'], rname
)
179 d
[rname
] = gsc
.gpr
[regidx
]
180 print ("read reg", rname
, regidx
, get_reg_hex(d
[rname
]))
182 exec (compiled_code
, d
)
187 print (decode
.sigforms
['X'])
188 x
= yield decode
.sigforms
['X'].RS
189 ra
= yield decode
.sigforms
['X'].RA
190 print ("RA", ra
, d
['RA'])
193 for wname
in gsc
.parser
.write_regs
:
195 print ("write regs", wname
, d
[wname
], reg
)
197 gsc
.gpr
[regidx
] = d
[wname
]
199 sim
.add_process(process
)
200 with sim
.write_vcd("simulator.vcd", "simulator.gtkw",
201 traces
=[decode
.ports()]):
204 for i
in range(len(gsc
.gpr
)):
205 print ("regfile", i
, get_reg_hex(gsc
.gpr
[i
]))
208 if __name__
== '__main__':