output as class not set of functions
[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 if (RS)[63-n] = 0b1 then
59 leave
60 n <- n + 1
61 RA <- EXTZ64(n)
62 print (RA)
63 """
64
65 cmpi = """
66 if a < EXTS(SI) then
67 c <- 0b100
68 else if a > EXTS(SI) then
69 c <- 0b010
70 """
71
72 cmpi = """
73 RA[0:1] <- 0b11
74 """
75
76 cmpi = """
77 in_range <- ((x | y) &
78 (a | b))
79 in_range <- (x + y) - (a + b)
80 """
81
82 cmpi = """
83 (RA)[0:1] <- 1
84 src1 <- EXTZ((RA)[56:63])
85 CR[4*BF+32] <- 0b0
86 in_range <- src21lo <= src1 & src1 <= src21hi
87 """
88
89 cmpeqb = """
90 src1 <- GPR[RA]
91 src1 <- src1[0:56]
92 """
93
94 addpcis = """
95 D <- d0||d1||d2
96 """
97
98 #code = testreg
99 #code = cnttzd
100 #code = cmpi
101 #code = cmpeqb
102 code = addpcis
103 #code = bpermd
104
105 def tolist(num):
106 l = []
107 for i in range(64):
108 l.append(1 if (num & (1<<i)) else 0)
109 l.reverse()
110 return l
111
112
113 def get_reg_hex(reg):
114 return hex(reg.value)
115
116
117 class GPR(dict):
118 def __init__(self, sd, regfile):
119 dict.__init__(self)
120 self.sd = sd
121 self.regfile = regfile
122 for i in range(32):
123 self[i] = SelectableInt(0, 64)
124
125 def set_form(self, form):
126 self.form = form
127
128 def ___getitem__(self, attr):
129 print ("GPR getitem", attr)
130 getform = self.sd.sigforms[self.form]
131 rnum = getattr(getform, attr)
132 print ("GPR get", rnum, rnum, dir(rnum))
133 l = list(rnum)
134 print (l[0]._as_const())
135 #for x in rnum:
136 #print (x, x.value, dir(x))
137 #print (x.value, dir(x.value))
138 print (list(rnum))
139 return self.regfile[rnum]
140
141
142 def convert_to_python(pcode):
143
144 gsc = GardenSnakeCompiler()
145
146 tree = gsc.compile(pcode, mode="exec", filename="string")
147 tree = ast.fix_missing_locations(tree)
148 regsused = {'read_regs': gsc.parser.read_regs,
149 'write_regs': gsc.parser.write_regs,
150 'uninit_regs': gsc.parser.uninit_regs}
151 return astor.to_source(tree), regsused
152
153
154 def test():
155
156 gsc = GardenSnakeCompiler()
157
158 # XXX unused! see GPR instead
159 gsc.regfile = {}
160 for i in range(32):
161 gsc.regfile[i] = 0
162 gsc.gpr = GPR(gsc.parser.sd, gsc.regfile)
163
164 _compile = gsc.compile
165
166 tree = _compile(code, mode="single", filename="string")
167 tree = ast.fix_missing_locations(tree)
168 print ( ast.dump(tree) )
169
170 print ("astor dump")
171 print (astor.dump_tree(tree))
172 print ("to source")
173 source = astor.to_source(tree)
174 print (source)
175
176 #sys.exit(0)
177
178 # Set up the GardenSnake run-time environment
179 def print_(*args):
180 print ("args", args)
181 print ("-->", " ".join(map(str,args)))
182
183 from soc.decoder.helpers import (EXTS64, EXTZ64, ROTL64, ROTL32, MASK,)
184
185 d = {}
186 d["print"] = print_
187 d["EXTS64"] = EXTS64
188 d["EXTZ64"] = EXTZ64
189 d["SelectableInt"] = SelectableInt
190 d["concat"] = selectconcat
191 d["GPR"] = gsc.gpr
192
193 form = 'X'
194 gsc.gpr.set_form(form)
195 getform = gsc.parser.sd.sigforms[form]._asdict()
196 #print ("getform", form)
197 #for k, f in getform.items():
198 #print (k, f)
199 #d[k] = getform[k]
200
201 compiled_code = compile(source, mode="exec", filename="<string>")
202
203 m = Module()
204 comb = m.d.comb
205 instruction = Signal(32)
206
207 m.submodules.decode = decode = gsc.parser.sd
208 comb += decode.raw_opcode_in.eq(instruction)
209 sim = Simulator(m)
210
211 instr = [0x11111117]
212
213 def process():
214 for ins in instr:
215 print("0x{:X}".format(ins & 0xffffffff))
216
217 # ask the decoder to decode this binary data (endian'd)
218 yield decode.bigendian.eq(0) # little / big?
219 yield instruction.eq(ins) # raw binary instr.
220 yield Delay(1e-6)
221
222 # read regs, drop them into dict for function
223 for rname in gsc.parser.uninit_regs:
224 d[rname] = SelectableInt(0, 64) # uninitialised (to zero)
225 print ("uninitialised", rname, get_reg_hex(d[rname]))
226
227 for rname in gsc.parser.read_regs:
228 regidx = yield getattr(decode.sigforms['X'], rname)
229 d[rname] = gsc.gpr[regidx]
230 print ("read reg", rname, regidx, get_reg_hex(d[rname]))
231
232 exec (compiled_code, d)
233 print ("Done")
234
235 print (d.keys())
236
237 print (decode.sigforms['X'])
238 x = yield decode.sigforms['X'].RS
239 ra = yield decode.sigforms['X'].RA
240 print ("RA", ra, d['RA'])
241 print ("RS", x)
242
243 for wname in gsc.parser.write_regs:
244 reg = getform[wname]
245 print ("write regs", wname, d[wname], reg)
246 regidx = yield reg
247 gsc.gpr[regidx] = d[wname]
248
249 sim.add_process(process)
250 with sim.write_vcd("simulator.vcd", "simulator.gtkw",
251 traces=decode.ports()):
252 sim.run()
253
254 for i in range(len(gsc.gpr)):
255 print ("regfile", i, get_reg_hex(gsc.gpr[i]))
256
257
258 if __name__ == '__main__':
259 test()