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