decode power fields from Forms, section 1.6 of 2.07B ISA
authorLuke Kenneth Casson Leighton <lkcl@lkcl.net>
Thu, 5 Mar 2020 21:56:02 +0000 (21:56 +0000)
committerLuke Kenneth Casson Leighton <lkcl@lkcl.net>
Thu, 5 Mar 2020 21:56:02 +0000 (21:56 +0000)
src/decoder/power_fields.py [new file with mode: 0644]

diff --git a/src/decoder/power_fields.py b/src/decoder/power_fields.py
new file mode 100644 (file)
index 0000000..96abcac
--- /dev/null
@@ -0,0 +1,88 @@
+def decode_fields():
+    with open("fields.txt") as f:
+        txt = f.readlines()
+    forms = {}
+    reading_data = False
+    for l in txt:
+        print ("line", l)
+        l = l.strip()
+        if len(l) == 0:
+            continue
+        if reading_data:
+            if l[0] == '#':
+                reading_data = False
+            else:
+                forms[heading].append(l)
+        if not reading_data:
+            assert l[0] == '#'
+            heading = l[1:].strip()
+            if heading.startswith('1.6.28'): # skip instruction fields for now
+                break
+            heading = heading.split(' ')[-1]
+            print ("heading", heading)
+            reading_data = True
+            forms[heading] = []
+
+    res = {}
+    for hdr, form in forms.items():
+        print ("heading", hdr)
+        res[hdr] = decode_form(form)
+    return res
+
+def decode_form_header(hdr):
+    res = {}
+    count = 0
+    hdr = hdr.strip()
+    print (hdr.split('|'))
+    for f in hdr.split("|"):
+        if not f:
+            continue
+        if f[0].isdigit():
+            idx = int(f.strip().split(' ')[0])
+            res[count] = idx
+        count += len(f) + 1
+    return res
+
+def decode_line(header, line):
+    line = line.strip()
+    res = {}
+    count = 0
+    print ("line", line)
+    prev_fieldname = None
+    for f in line.split("|"):
+        if not f:
+            continue
+        end = count + len(f) + 1
+        fieldname = f.strip()
+        if not fieldname or fieldname.startswith('/'):
+            count = end
+            continue
+        bitstart = header[count]
+        if prev_fieldname is not None:
+            res[prev_fieldname] = (res[prev_fieldname], bitstart)
+        res[fieldname] = bitstart
+        count = end
+        prev_fieldname = fieldname
+    res[prev_fieldname] = (bitstart, 32)
+    return res
+
+def decode_form(form):
+    header = decode_form_header(form[0])
+    res = []
+    print ("header", header)
+    for line in form[1:]:
+        dec = decode_line(header, line)
+        if dec:
+            res.append(dec)
+    return res
+
+    
+if __name__ == '__main__':
+    forms = decode_fields()
+    for hdr, form in forms.items():
+        print ()
+        print (hdr)
+        for l in form:
+            print ("line", l)
+            for k, v in l.items():
+                print ("%s: %d-%d" % (k, v[0], v[1]))