add stbcix and lwzcix to power_enum list
[soc.git] / src / soc / decoder / power_fields.py
index e0c01427bf68398f55efd51be155c73d0ceffae8..02c2dc7025fa425e5a1d0613519dc445f52fe677 100644 (file)
@@ -1,15 +1,17 @@
 from collections import OrderedDict, namedtuple
-from soc.decoder.power_enums import download_wiki_file
+from soc.decoder.power_enums import find_wiki_file
 
 
 class BitRange(OrderedDict):
     """BitRange: remaps from straight indices (0,1,2..) to bit numbers
     """
+
     def __getitem__(self, subscript):
         if isinstance(subscript, slice):
-            return list(self)[subscript]
+            return list(self.values())[subscript]
         else:
-            return self[subscript]
+            return OrderedDict.__getitem__(self, subscript)
+
 
 def decode_instructions(form):
     res = {}
@@ -29,6 +31,7 @@ def decode_instructions(form):
             accum.append(l.strip())
     return res
 
+
 def decode_form_header(hdr):
     res = {}
     count = 0
@@ -42,6 +45,7 @@ def decode_form_header(hdr):
         count += len(f) + 1
     return res
 
+
 def find_unique(d, key):
     if key not in d:
         return key
@@ -87,10 +91,10 @@ def decode_form(form):
     fields = {}
     falternate = {}
     for l in res:
-        for k, (start,end) in l.items():
+        for k, (start, end) in l.items():
             if k in fields:
                 if (start, end) == fields[k]:
-                    continue # already in and matching for this Form
+                    continue  # already in and matching for this Form
                 if k in falternate:
                     alternate = "%s_%d" % (k, falternate[k])
                     if (start, end) == fields[alternate]:
@@ -104,10 +108,15 @@ def decode_form(form):
 
 class DecodeFields:
 
-    def __init__(self, bitkls=BitRange, bitargs=(), fname="fields.text"):
+    def __init__(self, bitkls=BitRange, bitargs=(), fname=None,
+                 name_on_wiki=None):
         self.bitkls = bitkls
         self.bitargs = bitargs
-        self.fname = download_wiki_file(fname)
+        if fname is None:
+            assert name_on_wiki is None
+            fname = "fields.txt"
+            name_on_wiki = "fields.text"
+        self.fname = find_wiki_file(name_on_wiki)
 
     def create_specs(self):
         self.forms, self.instrs = self.decode_fields()
@@ -138,7 +147,7 @@ class DecodeFields:
             "LK": self.FormI.LK,
             "AA": self.FormB.AA,
             "Rc": self.FormX.Rc,
-            "OE": self.FormXO.Rc,
+            "OE": self.FormXO.OE,
             "BD": self.FormB.BD,
             "BF": self.FormX.BF,
             "CR": self.FormXL.XO,
@@ -178,8 +187,8 @@ class DecodeFields:
             if not reading_data:
                 assert l[0] == '#'
                 heading = l[1:].strip()
-                #if heading.startswith('1.6.28'): # skip instr fields for now
-                    #break
+                # if heading.startswith('1.6.28'): # skip instr fields for now
+                #     break
                 heading = heading.split(' ')[-1]
                 reading_data = True
                 forms[heading] = []
@@ -192,17 +201,27 @@ class DecodeFields:
                 i = decode_instructions(form)
                 for form, field in i.items():
                     inst[form] = self.decode_instruction_fields(field)
-            #else:
-            #    res[hdr] = decode_form(form)
+            # else:
+            #     res[hdr] = decode_form(form)
         return res, inst
 
     def decode_instruction_fields(self, fields):
         res = {}
         for field in fields:
             f, spec = field.strip().split(" ")
+            ss = spec[1:-1].split(",")
+            fs = f.split(",")
+            if len(fs) > 1:
+                individualfields = []
+                for f0, s0 in zip(fs, ss):
+                    txt = "%s (%s)" % (f0, s0)
+                    individualfields.append(txt)
+                if len(fs) > 1:
+                    res.update(self.decode_instruction_fields(
+                        individualfields))
             d = self.bitkls(*self.bitargs)
             idx = 0
-            for s in spec[1:-1].split(","):
+            for s in ss:
                 s = s.split(':')
                 if len(s) == 1:
                     d[idx] = int(s[0])
@@ -220,7 +239,12 @@ class DecodeFields:
 
         return res
 
+
 if __name__ == '__main__':
     dec = DecodeFields()
     dec.create_specs()
     forms, instrs = dec.forms, dec.instrs
+    for form, fields in instrs.items():
+        print("Form", form)
+        for field, bits in fields.items():
+            print("\tfield", field, bits)