d07e54cad87f4b36e4c081fa4ffc392f8af68cee
[soc.git] / src / soc / decoder / isa / caller.py
1 from functools import wraps
2 from soc.decoder.orderedset import OrderedSet
3 from soc.decoder.selectable_int import SelectableInt, selectconcat
4 from collections import namedtuple
5 import math
6
7 instruction_info = namedtuple('instruction_info',
8 'func read_regs uninit_regs write_regs op_fields form asmregs')
9
10
11 def create_args(reglist, extra=None):
12 args = OrderedSet()
13 for reg in reglist:
14 args.add(reg)
15 args = list(args)
16 if extra:
17 args = [extra] + args
18 return args
19
20
21 class Mem:
22
23 def __init__(self, bytes_per_word=8):
24 self.mem = {}
25 self.bytes_per_word = bytes_per_word
26 self.word_log2 = math.ceil(math.log2(bytes_per_word))
27
28 def _get_shifter_mask(self, width, remainder):
29 shifter = ((self.bytes_per_word - width) - remainder) * \
30 8 # bits per byte
31 mask = (1 << (width * 8)) - 1
32 return shifter, mask
33
34 # TODO: Implement ld/st of lesser width
35 def ld(self, address, width=8):
36 remainder = address & (self.bytes_per_word - 1)
37 address = address >> self.word_log2
38 assert remainder & (width - 1) == 0, "Unaligned access unsupported!"
39 if address in self.mem:
40 val = self.mem[address]
41 else:
42 val = 0
43
44 if width != self.bytes_per_word:
45 shifter, mask = self._get_shifter_mask(width, remainder)
46 val = val & (mask << shifter)
47 val >>= shifter
48 print("Read {:x} from addr {:x}".format(val, address))
49 return val
50
51 def st(self, address, value, width=8):
52 remainder = address & (self.bytes_per_word - 1)
53 address = address >> self.word_log2
54 assert remainder & (width - 1) == 0, "Unaligned access unsupported!"
55 print("Writing {:x} to addr {:x}".format(value, address))
56 if width != self.bytes_per_word:
57 if address in self.mem:
58 val = self.mem[address]
59 else:
60 val = 0
61 shifter, mask = self._get_shifter_mask(width, remainder)
62 val &= ~(mask << shifter)
63 val |= value << shifter
64 self.mem[address] = val
65 else:
66 self.mem[address] = value
67
68 def __call__(self, addr, sz):
69 val = self.ld(addr.value, sz)
70 print ("memread", addr, sz, val)
71 return SelectableInt(val, sz*8)
72
73 def memassign(self, addr, sz, val):
74 print ("memassign", addr, sz, val)
75 self.st(addr.value, val.value, sz)
76
77
78 class GPR(dict):
79 def __init__(self, decoder, regfile):
80 dict.__init__(self)
81 self.sd = decoder
82 for i in range(32):
83 self[i] = SelectableInt(regfile[i], 64)
84
85 def __call__(self, ridx):
86 return self[ridx]
87
88 def set_form(self, form):
89 self.form = form
90
91 def getz(self, rnum):
92 #rnum = rnum.value # only SelectableInt allowed
93 print("GPR getzero", rnum)
94 if rnum == 0:
95 return SelectableInt(0, 64)
96 return self[rnum]
97
98 def _get_regnum(self, attr):
99 getform = self.sd.sigforms[self.form]
100 rnum = getattr(getform, attr)
101 return rnum
102
103 def ___getitem__(self, attr):
104 print("GPR getitem", attr)
105 rnum = self._get_regnum(attr)
106 return self.regfile[rnum]
107
108 def dump(self):
109 for i in range(0, len(self), 8):
110 s = []
111 for j in range(8):
112 s.append("%08x" % self[i+j].value)
113 s = ' '.join(s)
114 print("reg", "%2d" % i, s)
115
116 class PC:
117 def __init__(self, pc_init=0):
118 self.CIA = SelectableInt(pc_init, 64)
119 self.NIA = self.CIA + SelectableInt(4, 64)
120
121 def update(self, namespace):
122 self.CIA = self.NIA
123 self.NIA = self.CIA + SelectableInt(4, 64)
124 namespace['CIA'] = self.CIA
125 namespace['NIA'] = self.NIA
126
127
128 class ISACaller:
129 # decoder2 - an instance of power_decoder2
130 # regfile - a list of initial values for the registers
131 def __init__(self, decoder2, regfile):
132 self.gpr = GPR(decoder2, regfile)
133 self.mem = Mem()
134 self.pc = PC()
135 # TODO, needed here:
136 # 4.4.4 III p971 SPR (same as GPR except for SPRs - best done as a dict
137 # FPR (same as GPR except for FP nums)
138 # 4.2.2 p124 FPSCR (definitely "separate" - not in SPR)
139 # note that mffs, mcrfs, mtfsf "manage" this FPSCR
140 # 2.3.1 CR (and sub-fields CR0..CR6 - CR0 SO comes from XER.SO)
141 # note that mfocrf, mfcr, mtcr, mtocrf, mcrxrx "manage" CRs
142 # 2.3.2 LR (actually SPR #8)
143 # 2.3.3 CTR (actually SPR #9)
144 # 2.3.4 TAR (actually SPR #815)
145 # 3.2.2 p45 XER (actually SPR #0)
146 # 3.2.3 p46 p232 VRSAVE (actually SPR #256)
147
148 self.namespace = {'GPR': self.gpr,
149 'MEM': self.mem,
150 'memassign': self.memassign,
151 'NIA': self.pc.NIA,
152 'CIA': self.pc.CIA,
153 }
154
155 self.decoder = decoder2
156
157 def memassign(self, ea, sz, val):
158 self.mem.memassign(ea, sz, val)
159
160 def prep_namespace(self, formname, op_fields):
161 # TODO: get field names from form in decoder*1* (not decoder2)
162 # decoder2 is hand-created, and decoder1.sigform is auto-generated
163 # from spec
164 # then "yield" fields only from op_fields rather than hard-coded
165 # list, here.
166 fields = self.decoder.sigforms[formname]
167 for name in fields._fields:
168 if name not in ["RA", "RB", "RT"]:
169 sig = getattr(fields, name)
170 val = yield sig
171 self.namespace[name] = SelectableInt(val, sig.width)
172
173 def call(self, name):
174 # TODO, asmregs is from the spec, e.g. add RT,RA,RB
175 # see http://bugs.libre-riscv.org/show_bug.cgi?id=282
176 info = self.instrs[name]
177 yield from self.prep_namespace(info.form, info.op_fields)
178
179 input_names = create_args(info.read_regs | info.uninit_regs)
180 print(input_names)
181
182 inputs = []
183 for name in input_names:
184 regnum = yield getattr(self.decoder, name)
185 regname = "_" + name
186 self.namespace[regname] = regnum
187 print('reading reg %d' % regnum)
188 inputs.append(self.gpr(regnum))
189 print(inputs)
190 results = info.func(self, *inputs)
191 print(results)
192
193 if info.write_regs:
194 output_names = create_args(info.write_regs)
195 for name, output in zip(output_names, results):
196 regnum = yield getattr(self.decoder, name)
197 print('writing reg %d' % regnum)
198 if output.bits > 64:
199 output = SelectableInt(output.value, 64)
200 self.gpr[regnum] = output
201 self.pc.update(self.namespace)
202
203
204 def inject():
205 """ Decorator factory. """
206 def variable_injector(func):
207 @wraps(func)
208 def decorator(*args, **kwargs):
209 try:
210 func_globals = func.__globals__ # Python 2.6+
211 except AttributeError:
212 func_globals = func.func_globals # Earlier versions.
213
214 context = args[0].namespace
215 saved_values = func_globals.copy() # Shallow copy of dict.
216 func_globals.update(context)
217
218 result = func(*args, **kwargs)
219 #exec (func.__code__, func_globals)
220
221 #finally:
222 # func_globals = saved_values # Undo changes.
223
224 return result
225
226 return decorator
227
228 return variable_injector
229