add comments, get example working
[soc.git] / src / soc / decoder / power_pseudo.py
1 # Based on GardenSnake - a parser generator demonstration program
2 # GardenSnake was released into the Public Domain by Andrew Dalke.
3
4 # Portions of this work are derived from Python's Grammar definition
5 # and may be covered under the Python copyright and license
6 #
7 # Andrew Dalke / Dalke Scientific Software, LLC
8 # 30 August 2006 / Cape Town, South Africa
9
10 # Modifications for inclusion in PLY distribution
11 import sys
12 from pprint import pprint
13 from copy import copy
14 from ply import lex, yacc
15 import astor
16 import ast
17
18 from soc.decoder.power_decoder import create_pdecode
19 from nmigen.back.pysim import Simulator, Delay
20 from nmigen import Module, Signal
21
22 from soc.decoder.pseudo.parser import GardenSnakeCompiler
23 from soc.decoder.selectable_int import SelectableInt, selectconcat
24
25 ####### Test code #######
26
27 bpermd = r"""
28 perm <- [0] * 8
29 if index < 64:
30 index <- (RS)[8*i:8*i+7]
31 RA <- [0]*56 || perm[0:7]
32 print (RA)
33 """
34
35 bpermd = r"""
36 if index < 64 then index <- 0
37 else index <- 5
38 do while index < 5
39 index <- 0
40 leave
41 for i = 0 to 7
42 index <- 0
43 """
44
45 _bpermd = r"""
46 for i = 0 to 7
47 index <- (RS)[8*i:8*i+7]
48 if index < 64 then
49 permi <- (RB)[index]
50 else
51 permi <- 0
52 RA <- [0]*56|| perm[0:7]
53 """
54
55 cnttzd = """
56 n <- 0
57 do while n < 64
58 print (n)
59 if (RS)[63-n] = 0b1 then
60 leave
61 n <- n + 1
62 RA <- EXTZ64(n)
63 print (RA)
64 """
65
66 cmpi = """
67 if a < EXTS(SI) then
68 c <- 0b100
69 else if a > EXTS(SI) then
70 c <- 0b010
71 """
72
73 cmpi = """
74 RA[0:1] <- 0b11
75 """
76
77 cmpi = """
78 in_range <- ((x | y) &
79 (a | b))
80 in_range <- (x + y) - (a + b)
81 """
82
83 cmpi = """
84 (RA)[0:1] <- 1
85 src1 <- EXTZ((RA)[56:63])
86 CR[4*BF+32] <- 0b0
87 in_range <- src21lo <= src1 & src1 <= src21hi
88 """
89
90 cmpeqb = """
91 src1 <- GPR[RA]
92 src1 <- src1[0:56]
93 """
94
95 addpcis = """
96 D <- d0||d1||d2
97 """
98
99 #code = testreg
100 code = cnttzd
101 #code = cmpi
102 #code = cmpeqb
103 #code = addpcis
104 #code = bpermd
105
106 def tolist(num):
107 l = []
108 for i in range(64):
109 l.append(1 if (num & (1<<i)) else 0)
110 l.reverse()
111 return l
112
113
114 def get_reg_hex(reg):
115 return hex(reg.value)
116
117
118 class GPR(dict):
119 def __init__(self, sd, regfile):
120 dict.__init__(self)
121 self.sd = sd
122 self.regfile = regfile
123 for i in range(32):
124 self[i] = SelectableInt(0, 64)
125
126 def set_form(self, form):
127 self.form = form
128
129 def ___getitem__(self, attr):
130 print ("GPR getitem", attr)
131 getform = self.sd.sigforms[self.form]
132 rnum = getattr(getform, attr)
133 print ("GPR get", rnum, rnum, dir(rnum))
134 l = list(rnum)
135 print (l[0]._as_const())
136 #for x in rnum:
137 #print (x, x.value, dir(x))
138 #print (x.value, dir(x.value))
139 print (list(rnum))
140 return self.regfile[rnum]
141
142
143 def convert_to_python(pcode):
144
145 gsc = GardenSnakeCompiler()
146
147 tree = gsc.compile(pcode, mode="exec", filename="string")
148 tree = ast.fix_missing_locations(tree)
149 regsused = {'read_regs': gsc.parser.read_regs,
150 'write_regs': gsc.parser.write_regs,
151 'uninit_regs': gsc.parser.uninit_regs}
152 return astor.to_source(tree), regsused
153
154
155 def test():
156
157 gsc = GardenSnakeCompiler()
158
159 # XXX unused! see GPR instead
160 gsc.regfile = {}
161 for i in range(32):
162 gsc.regfile[i] = 0
163 gsc.gpr = GPR(gsc.parser.sd, gsc.regfile)
164
165 _compile = gsc.compile
166
167 tree = _compile(code, mode="single", filename="string")
168 tree = ast.fix_missing_locations(tree)
169 print ( ast.dump(tree) )
170
171 print ("astor dump")
172 print (astor.dump_tree(tree))
173 print ("to source")
174 source = astor.to_source(tree)
175 print (source)
176
177 #sys.exit(0)
178
179 # Set up the GardenSnake run-time environment
180 def print_(*args):
181 print ("args", args)
182 print ("-->", " ".join(map(str,args)))
183
184 from soc.decoder.helpers import (EXTS64, EXTZ64, ROTL64, ROTL32, MASK,)
185
186 d = {}
187 d["print"] = print_
188 d["EXTS64"] = EXTS64
189 d["EXTZ64"] = EXTZ64
190 d["SelectableInt"] = SelectableInt
191 d["concat"] = selectconcat
192 d["GPR"] = gsc.gpr
193
194 form = 'X'
195 gsc.gpr.set_form(form)
196 getform = gsc.parser.sd.sigforms[form]._asdict()
197 #print ("getform", form)
198 #for k, f in getform.items():
199 #print (k, f)
200 #d[k] = getform[k]
201
202 compiled_code = compile(source, mode="exec", filename="<string>")
203
204 m = Module()
205 comb = m.d.comb
206 instruction = Signal(32)
207
208 m.submodules.decode = decode = gsc.parser.sd
209 comb += decode.raw_opcode_in.eq(instruction)
210 sim = Simulator(m)
211
212 instr = [0x11111117]
213
214 def process():
215 for ins in instr:
216 print("0x{:X}".format(ins & 0xffffffff))
217
218 # ask the decoder to decode this binary data (endian'd)
219 yield decode.bigendian.eq(0) # little / big?
220 yield instruction.eq(ins) # raw binary instr.
221 yield Delay(1e-6)
222
223 # uninitialised regs, drop them into dict for function
224 for rname in gsc.parser.uninit_regs:
225 d[rname] = SelectableInt(0, 64) # uninitialised (to zero)
226 print ("uninitialised", rname, get_reg_hex(d[rname]))
227
228 # read regs, drop them into dict for function
229 for rname in gsc.parser.read_regs:
230 regidx = yield getattr(decode.sigforms['X'], rname)
231 d[rname] = gsc.gpr[regidx]
232 print ("read reg", rname, regidx, get_reg_hex(d[rname]))
233
234 exec (compiled_code, d) # code gets executed here in dict "d"
235 print ("Done")
236
237 print (d.keys()) # shows the variables that may have been created
238
239 print (decode.sigforms['X'])
240 x = yield decode.sigforms['X'].RS
241 ra = yield decode.sigforms['X'].RA
242 print ("RA", ra, d['RA'])
243 print ("RS", x)
244
245 for wname in gsc.parser.write_regs:
246 reg = getform[wname]
247 print ("write regs", wname, d[wname], reg)
248 regidx = yield reg
249 gsc.gpr[regidx] = d[wname]
250
251 sim.add_process(process)
252 with sim.write_vcd("simulator.vcd", "simulator.gtkw",
253 traces=decode.ports()):
254 sim.run()
255
256 for i in range(len(gsc.gpr)):
257 print ("regfile", i, get_reg_hex(gsc.gpr[i]))
258
259
260 if __name__ == '__main__':
261 test()