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