add placeholder-variant pipeline stage of Record
authorLuke Kenneth Casson Leighton <lkcl@lkcl.net>
Sun, 24 Mar 2019 10:34:40 +0000 (10:34 +0000)
committerLuke Kenneth Casson Leighton <lkcl@lkcl.net>
Sun, 24 Mar 2019 10:34:40 +0000 (10:34 +0000)
src/add/example_buf_pipe.py
src/add/test_buf_pipe.py

index 578aed8e6cbe20e7d08b838c3267b305df43765b..005dbc172cbfa9aba2868a2450c2afd96efe610f 100644 (file)
@@ -187,9 +187,14 @@ def eq(o, i):
         if isinstance(ao, Record):
             for idx, (field_name, field_shape, _) in enumerate(ao.layout):
                 if isinstance(field_shape, Layout):
-                    rres = eq(ao.fields[field_name], ai.fields[field_name])
+                    val = ai.fields
                 else:
-                    rres = eq(ao.fields[field_name], ai[field_name])
+                    val = ai
+                if hasattr(val, field_name):
+                    val = getattr(val, field_name)
+                else:
+                    val = val[field_name]
+                rres = eq(ao.fields[field_name], val)
                 res += rres
         else:
             rres = ao.eq(ai)
index 8e575dc8374dd1de3a452458aaaae6f9a972cfd8..f3f49da8f6259f583831cc732b1783a09e6a9fca 100644 (file)
@@ -147,6 +147,15 @@ def test3_resultfn(o_data, expected, i, o):
                 "%d-%d data %x not match %x\n" \
                 % (i, o, o_data, expected)
 
+def data_placeholder():
+        data = []
+        for i in range(num_tests):
+            d = PlaceHolder()
+            d.src1 = randint(0, 1<<16-1)
+            d.src2 = randint(0, 1<<16-1)
+            data.append(d)
+        return data
+
 def data_dict():
         data = []
         for i in range(num_tests):
@@ -408,6 +417,30 @@ class ExampleAddRecordStage:
                 'src2': i.src2 + 1}
 
 
+class ExampleAddRecordPlaceHolderStage:
+    """ example use of a Record, with a placeholder as the processing result
+    """
+
+    record_spec = [('src1', 16), ('src2', 16)]
+    def ispec(self):
+        """ returns a tuple of input signals which will be the incoming data
+        """
+        return Record(self.record_spec)
+
+    def ospec(self):
+        return Record(self.record_spec)
+
+    def process(self, i):
+        """ process the input data (sums the values in the tuple) and returns it
+        """
+        o = PlaceHolder()
+        o.src1 = i.src1 + 1
+        o.src2 = i.src2 + 1
+        return o
+
+class PlaceHolder: pass
+
+
 class ExampleAddRecordPipe(UnbufferedPipeline):
     """ an example of how to use the combinatorial pipeline.
     """
@@ -424,6 +457,23 @@ def test7_resultfn(o_data, expected, i, o):
                 % (i, o, repr(o_data), repr(expected))
 
 
+class ExampleAddRecordPlaceHolderPipe(UnbufferedPipeline):
+    """ an example of how to use the combinatorial pipeline.
+    """
+
+    def __init__(self):
+        stage = ExampleAddRecordPlaceHolderStage()
+        UnbufferedPipeline.__init__(self, stage)
+
+
+def test11_resultfn(o_data, expected, i, o):
+    res1 = expected.src1 + 1
+    res2 = expected.src2 + 1
+    assert o_data['src1'] == res1 and o_data['src2'] == res2, \
+                "%d-%d data %s not match %s\n" \
+                % (i, o, repr(o_data), repr(expected))
+
+
 class Example2OpClass:
     """ an example of a class used to store 2 operands.
         requires an eq function, to conform with the pipeline stage API
@@ -575,4 +625,10 @@ if __name__ == '__main__':
     with open("test_ltbufpipe10.il", "w") as f:
         f.write(vl)
 
+    print ("test 11")
+    dut = ExampleAddRecordPlaceHolderPipe()
+    data=data_placeholder()
+    test = Test5(dut, test11_resultfn, data=data)
+    run_simulation(dut, [test.send, test.rcv], vcd_name="test_addrecord.vcd")
+