success using SelectableInt in cnttzd test
[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
17 from soc.decoder.power_decoder import create_pdecode
18 from nmigen.back.pysim import Simulator, Delay
19 from nmigen import Module, Signal
20
21 from soc.decoder.pseudo.parser import GardenSnakeCompiler
22 from soc.decoder.selectable_int import SelectableInt
23
24 ####### Test code #######
25
26 bpermd = r"""
27 perm <- [0] * 8
28 if index < 64:
29 index <- (RS)[8*i:8*i+7]
30 RA <- [0]*56 || perm[0:7]
31 print (RA)
32 """
33
34 bpermd = r"""
35 if index < 64 then index <- 0
36 else index <- 5
37 do while index < 5
38 index <- 0
39 leave
40 for i = 0 to 7
41 index <- 0
42 """
43
44 _bpermd = r"""
45 for i = 0 to 7
46 index <- (RS)[8*i:8*i+7]
47 if index < 64 then
48 permi <- (RB)[index]
49 else
50 permi <- 0
51 RA <- [0]*56|| perm[0:7]
52 """
53
54 cnttzd = """
55 n <- 0
56 do while n < 64
57 if (RS)[63-n] = 0b1 then
58 leave
59 n <- n + 1
60 RA <- EXTZ64(n)
61 print (RA)
62 """
63
64 #code = testreg
65 code = cnttzd
66 #code = bpermd
67
68 def tolist(num):
69 l = []
70 for i in range(64):
71 l.append(1 if (num & (1<<i)) else 0)
72 l.reverse()
73 return l
74
75
76 def get_reg_hex(reg):
77 return hex(reg.value)
78
79
80 class GPR(dict):
81 def __init__(self, sd, regfile):
82 dict.__init__(self)
83 self.sd = sd
84 self.regfile = regfile
85 for i in range(32):
86 self[i] = SelectableInt(0, 64)
87
88 def set_form(self, form):
89 self.form = form
90
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))
96 l = list(rnum)
97 print (l[0]._as_const())
98 #for x in rnum:
99 #print (x, x.value, dir(x))
100 #print (x.value, dir(x.value))
101 print (list(rnum))
102 return self.regfile[rnum]
103
104
105 def test():
106
107 gsc = GardenSnakeCompiler()
108
109 # XXX unused! see GPR instead
110 gsc.regfile = {}
111 for i in range(32):
112 gsc.regfile[i] = 0
113 gsc.gpr = GPR(gsc.parser.sd, gsc.regfile)
114
115 _compile = gsc.compile
116
117 tree = _compile(code, mode="single", filename="string")
118 import ast
119 tree = ast.fix_missing_locations(tree)
120 print ( ast.dump(tree) )
121
122 print ("astor dump")
123 print (astor.dump_tree(tree))
124 print ("to source")
125 source = astor.to_source(tree)
126 print (source)
127
128 #sys.exit(0)
129
130 # Set up the GardenSnake run-time environment
131 def print_(*args):
132 print ("args", args)
133 print ("-->", " ".join(map(str,args)))
134
135 def listconcat(l1, l2):
136 return l1 + l2
137
138 from soc.decoder.helpers import (EXTS64, EXTZ64, ROTL64, ROTL32, MASK,)
139
140 d = {}
141 d["print"] = print_
142 d["EXTS64"] = EXTS64
143 d["EXTZ64"] = EXTZ64
144 d["concat"] = listconcat
145 d["GPR"] = gsc.gpr
146
147 form = 'X'
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():
152 #print (k, f)
153 #d[k] = getform[k]
154
155 compiled_code = compile(source, mode="exec", filename="<string>")
156
157 m = Module()
158 comb = m.d.comb
159 instruction = Signal(32)
160
161 m.submodules.decode = decode = gsc.parser.sd
162 comb += decode.raw_opcode_in.eq(instruction)
163 sim = Simulator(m)
164
165 instr = [0x11111117]
166
167 def process():
168 for ins in instr:
169 print("0x{:X}".format(ins & 0xffffffff))
170
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.
174 yield Delay(1e-6)
175
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]))
181
182 exec (compiled_code, d)
183 print ("Done")
184
185 print (d.keys())
186
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'])
191 print ("RS", x)
192
193 for wname in gsc.parser.write_regs:
194 reg = getform[wname]
195 print ("write regs", wname, d[wname], reg)
196 regidx = yield reg
197 gsc.gpr[regidx] = d[wname]
198
199 sim.add_process(process)
200 with sim.write_vcd("simulator.vcd", "simulator.gtkw",
201 traces=[decode.ports()]):
202 sim.run()
203
204 for i in range(len(gsc.gpr)):
205 print ("regfile", i, get_reg_hex(gsc.gpr[i]))
206
207
208 if __name__ == '__main__':
209 test()