create example Object class (annoyingly)
authorLuke Kenneth Casson Leighton <lkcl@lkcl.net>
Sat, 20 Apr 2019 22:52:33 +0000 (23:52 +0100)
committerLuke Kenneth Casson Leighton <lkcl@lkcl.net>
Sat, 20 Apr 2019 22:52:33 +0000 (23:52 +0100)
src/add/fpcommon/pack.py
src/add/singlepipe.py

index 0f75d46ce2f29eb62b90fce7256293978d16dea8..97d6911105f2121d0bcadefc47cdbe90a956f38f 100644 (file)
@@ -8,24 +8,16 @@ from nmigen.cli import main, verilog
 from fpbase import FPNumOut
 from fpbase import FPState
 from fpcommon.roundz import FPRoundData
+from singlepipe import Object
 
 
-class FPPackData:
+class FPPackData(Object):
 
     def __init__(self, width, id_wid):
+        Object.__init__(self)
         self.z = Signal(width, reset_less=True)
         self.mid = Signal(id_wid, reset_less=True)
 
-    def __iter__(self):
-        yield self.z
-        yield self.mid
-
-    def eq(self, i):
-        return [self.z.eq(i.z), self.mid.eq(i.mid)]
-
-    def ports(self):
-        return list(self)
-
 
 class FPPackMod:
 
index 0b9bf6315c06111491cee0b5d5dda6b3e11675b3..753dec1b6f9ea85084c47bbc62323e979ac96666 100644 (file)
@@ -179,6 +179,36 @@ from collections.abc import Sequence
 from queue import Queue
 
 
+class Object:
+    def __init__(self):
+        self.fields = {}
+
+    def __setattr__(self, k, v):
+        print ("kv", k, v)
+        if (k.startswith('_') or k in ["fields", "name", "src_loc"] or
+           k in dir(Object) or "fields" not in self.__dict__):
+            return object.__setattr__(self, k, v)
+        self.fields[k] = v
+
+    def __getattr__(self, k):
+        if k in self.fields:
+            return self.fields[k]
+        return object.__getattr__(self, k)
+
+    def __iter__(self):
+        for x in self.fields.values():
+            yield x
+
+    def eq(self, inp):
+        res = []
+        for (o, i) in zip(self, inp):
+            res.append(eq(o, i))
+        return res
+
+    def ports(self):
+        return list(self)
+
+
 class RecordObject(Record):
     def __init__(self, layout=None, name=None):
         Record.__init__(self, layout=layout or [], name=None)