Example #1
0
    def test_value_simple(self):
        t = HUnion(
            (uint8_t, "unsigned"),
            (int8_t, "signed"),
        )
        v = t.from_py(None)

        v.unsigned = mask(8)
        self.assertEqual(int(v.signed), -1)

        v.signed = 0
        self.assertEqual(int(v.unsigned), 0)
Example #2
0
    def test_value_simple(self):
        t = HUnion(
                (uint8_t, "unsigned"),
                (int8_t, "signed"),
            )
        v = t.fromPy(None)

        v.unsigned = mask(8)
        self.assertEqual(int(v.signed), -1)

        v.signed = 0
        self.assertEqual(int(v.unsigned), 0)
Example #3
0
    def test_value_array_of_struct_to_bits(self):
        t = HUnion((HStruct(
            (uint16_t, "a"),
            (uint8_t, "b"),
        )[3], "arr"), (Bits(24 * 3), "bits"))

        v = t.fromPy(None)
        for i in range(len(v.arr)):
            v.arr[i] = {"a": i + 1, "b": (i + 1) * 3}

        self.assertEqual(
            int(v.bits), 1 | 3 << 16 | 2 << 24 | 6 << (24 + 16) | 3 <<
            (2 * 24) | 9 << (2 * 24 + 16))
Example #4
0
    def test_assertMembersSameSize(self):
        t = HUnion(
            (uint8_t, "a"),
            (uint8_t, "b"),
            (uint8_t, "c"),
            (uint8_t, "d"),
            )
        self.assertEqual(t.bit_length(), 8)

        with self.assertRaises(TypeError):
            HUnion(
                (uint16_t, "a"),
                (uint8_t, "b"),
            )
Example #5
0
    def test_assertMembersSameSize(self):
        t = HUnion(
            (uint8_t, "a"),
            (uint8_t, "b"),
            (uint8_t, "c"),
            (uint8_t, "d"),
        )
        self.assertEqual(t.bit_length(), 8)

        with self.assertRaises(TypeError):
            HUnion(
                (uint16_t, "a"),
                (uint8_t, "b"),
            )
Example #6
0
 def test_value_array_of_struct_to_bits(self):
     t = HUnion(
             (HStruct(
                 (uint16_t, "a"),
                 (uint8_t, "b"),
                 )[3], "arr"),
             (Bits(24 * 3), "bits")
         )
     
     v = t.fromPy(None)
     for i in range(len(v.arr)):
         v.arr[i] = {"a":i + 1, "b": (i + 1) * 3}
     
     self.assertEqual(int(v.bits), 1 | 3 << 16 | 2 << 24 | 6 << (24 + 16) | 3 << (2 * 24) | 9 << (2 * 24 + 16))
Example #7
0
    def test_value_array_and_bits(self):
        t = HUnion(
            (uint32_t, "bits"),
            (uint8_t[4], "arr"),
        )

        v = t.from_py(None)

        b = (4 << (3 * 8)) | (3 << (2 * 8)) | (2 << 8) | 1
        v.bits = b

        for i, item in enumerate(v.arr):
            self.assertEqual(int(item), i + 1)

        self.assertEqual(int(v.bits), b)
Example #8
0
    def test_value_array_and_bits(self):
        t = HUnion(
                (uint32_t, "bits"),
                (uint8_t[4], "arr"),
            )

        v = t.fromPy(None)

        b = (4 << (3 * 8)) | (3 << (2 * 8)) | (2 << 8) | 1
        v.bits = b

        for i, item in enumerate(v.arr):
            self.assertEqual(int(item), i + 1)
        
        self.assertEqual(int(v.bits), b)
Example #9
0
def _example_AxiS_frameDeparser():
    from hwtLib.types.ctypes import uint64_t, uint8_t, uint16_t, uint32_t

    # t = HStruct(
    #    (uint64_t, "item0"),
    #    (uint64_t, None),  # name = None means field is padding
    #    (uint64_t, "item1"),
    #    (uint8_t, "item2"), (uint8_t, "item3"), (uint16_t, "item4")
    # )
    # t = HUnion(
    #     (HStruct(
    #         (uint64_t, "itemA0"),
    #         (uint64_t, "itemA1")
    #         ), "frameA"),
    #     (HStruct(
    #         (uint32_t, "itemB0"),
    #         (uint32_t, "itemB1"),
    #         (uint32_t, "itemB2"),
    #         (uint32_t, "itemB3")
    #         ), "frameB")
    # )
    t = HUnion(
        (HStruct((uint8_t, "data"), (uint8_t, None)), "u0"),
        (HStruct((uint8_t, None), (uint8_t, "data")), "u1"),
    )

    u = AxiS_frameDeparser(t)
    u.DATA_WIDTH = 16
    return u
Example #10
0
    def test_value_array_toArray(self):
        t = HUnion(
                (uint16_t[2], "arr16b"),
                (int8_t[4], "arr8b"),
            )

        v = t.fromPy(None)

        for i in range(len(v.arr16b)):
            v.arr16b[i] = i + 1

        for i, item in enumerate(v.arr8b):
            if (i + 1) % 2 == 0:
                v = 0
            else:
                v = i // 2 + 1
            self.assertEqual(int(item), v)
Example #11
0
    def test_value_array_toArray(self):
        t = HUnion(
            (uint16_t[2], "arr16b"),
            (int8_t[4], "arr8b"),
        )

        v = t.from_py(None)

        for i in range(len(v.arr16b)):
            v.arr16b[i] = i + 1

        for i, item in enumerate(v.arr8b):
            if (i + 1) % 2 == 0:
                v = 0
            else:
                v = i // 2 + 1
            self.assertEqual(int(item), v)
Example #12
0
    def test_value_struct_and_bits(self):
        t = HUnion(
                (uint16_t, "bits"),
                (HStruct(
                  (uint8_t, "lower"),
                  (uint8_t, "upper"),
                 ), "struct"),
            )

        v = t.fromPy(None)

        v.struct.upper = 1
        self.assertEqual(v.bits.val, 1 << 8)
        self.assertEqual(v.bits.vldMask, mask(8) << 8)
        
        v.struct.lower = 1
        self.assertEqual(v.bits.val, (1 << 8) | 1)
        self.assertEqual(v.bits.vldMask, mask(16))

        v.bits = 2

        self.assertEqual(int(v.struct.lower), 2)
        self.assertEqual(int(v.struct.upper), 0)
Example #13
0
    def test_value_struct_and_bits(self):
        t = HUnion(
            (uint16_t, "bits"),
            (HStruct(
                (uint8_t, "lower"),
                (uint8_t, "upper"),
            ), "struct"),
        )

        v = t.from_py(None)

        v.struct.upper = 1
        self.assertEqual(v.bits.val, 1 << 8)
        self.assertEqual(v.bits.vld_mask, mask(8) << 8)

        v.struct.lower = 1
        self.assertEqual(v.bits.val, (1 << 8) | 1)
        self.assertEqual(v.bits.vld_mask, mask(16))

        v.bits = 2

        self.assertEqual(int(v.struct.lower), 2)
        self.assertEqual(int(v.struct.upper), 0)
Example #14
0
def _example_AxiS_frameParser():
    from hwtLib.types.ctypes import uint32_t, uint64_t
    # t = HStruct(
    #  (uint64_t, "item0"),  # tuples (type, name) where type has to be instance of Bits type
    #  (uint64_t, None),  # name = None means this field will be ignored
    #  (uint64_t, "item1"),
    #  (uint64_t, None),
    #  (uint16_t, "item2"),
    #  (uint16_t, "item3"),
    #  (uint32_t, "item4"),
    #  (uint32_t, None),
    #  (uint64_t, "item5"),  # this word is split on two bus words
    #  (uint32_t, None),
    #  (uint64_t, None),
    #  (uint64_t, None),
    #  (uint64_t, None),
    #  (uint64_t, "item6"),
    #  (uint64_t, "item7"),
    #  (HStruct(
    #      (uint64_t, "item0"),
    #      (uint64_t, "item1"),
    #   ),
    #   "struct0")
    #  )
    # t = HUnion(
    #    (uint32_t, "a"),
    #    (int32_t, "b")
    #    )

    t = HUnion(
        (HStruct(
            (uint64_t, "itemA0"),
            (uint64_t, "itemA1")
        ), "frameA"),
        (HStruct(
            (uint32_t, "itemB0"),
            (uint32_t, "itemB1"),
            (uint32_t, "itemB2"),
            (uint32_t, "itemB3")
        ), "frameB")
    )
    u = AxiS_frameParser(t)
    u.DATA_WIDTH.set(64)
    return u
Example #15
0
 def generate_in_axi_type(self):
     s_w = self.s_w
     axi = self.m
     # type used to describe how to build and parse axi-s frames
     DW = ceil(s_w.DATA_WIDTH / 8) * 8
     data_words_in_axi_word = axi.DATA_WIDTH // DW
     if data_words_in_axi_word <= 1:
         data_type = HStruct((Bits(DW), "data"))
     else:
         assert data_words_in_axi_word > 1, data_words_in_axi_word
         data_fields = []
         for last, i in iter_with_last(range(data_words_in_axi_word)):
             prefix, suffix = (), ()
             if i != 0:
                 prefix = ((Bits(i * DW), None), )
             if not last:
                 suffix = ((Bits(axi.DATA_WIDTH - ((i + 1) * DW)), None), )
             data_fields.append((HStruct(*prefix, (Bits(DW), "data"),
                                         *suffix), f"data{i:d}"))
         data_type = HStruct(
             # union with member for each data position in axi word
             (HUnion(*data_fields), 'data'))
     return data_type, data_words_in_axi_word
Example #16
0
    def test_hunion_type_eq(self):
        t0 = HUnion((HStruct(
            (uint16_t, "a"),
            (uint8_t, "b"),
        )[3], "arr"), (Bits(24 * 3), "bits"))
        t1 = HUnion((HStruct(
            (uint16_t, "a"),
            (uint8_t, "b"),
        )[3], "arr"), (Bits(24 * 3), "bits"))
        self.assertEqual(t0, t1)
        self.assertEqual(t1, t0)

        t1 = HUnion((Bits(24 * 3), "bits"), (HStruct(
            (uint16_t, "a"),
            (uint8_t, "b"),
        )[3], "arr"))

        self.assertEqual(t0, t1)
        self.assertEqual(t1, t0)

        t1 = HUnion(
            (uint32_t, "bits"),
            (uint8_t[4], "arr"),
        )
        self.assertNotEqual(t0, t1)
        self.assertNotEqual(t1, t0)

        t1 = HUnion((Bits(24 * 3), "bbits"), (HStruct(
            (uint16_t, "a"),
            (uint8_t, "b"),
        )[3], "arr"))
        self.assertNotEqual(t0, t1)
        self.assertNotEqual(t1, t0)

        t1 = Bits(24 * 3)
        self.assertNotEqual(t0, t1)
        self.assertNotEqual(t1, t0)

        t1 = HUnion((Bits(24 * 3, signed=False), "bits"), (HStruct(
            (uint16_t, "a"),
            (uint8_t, "b"),
        )[3], "arr"))
        self.assertNotEqual(t0, t1)
        self.assertNotEqual(t1, t0)
Example #17
0
 def test_assertNoPadding(self):
     with self.assertRaises(AssertionError):
         HUnion(
             (uint8_t, None),
             (uint8_t, "b"),
         )
Example #18
0
    (uint64_t, None),
    (uint64_t, "item1_0"),
    (uint64_t, "item1_1"),
    (uint64_t, None),
)

s1field_composit0 = HStruct(
    (uint32_t, "item0"), (uint32_t, "item1"),
)

unionOfStructs = HUnion(
    (HStruct(
        (uint64_t, "itemA0"),
        (uint64_t, "itemA1")
        ), "frameA"),
    (HStruct(
        (uint32_t, "itemB0"),
        (uint32_t, "itemB1"),
        (uint32_t, "itemB2"),
        (uint32_t, "itemB3")
        ), "frameB")
)

unionSimple = HUnion(
    (uint32_t, "a"),
    (int32_t, "b")
)

structStream64 = HStruct(
    (HStream(uint64_t), "streamIn")
)
Example #19
0
import unittest

from hwt.hdl.types.bits import Bits
from hwt.hdl.types.struct import HStruct
from hwt.hdl.types.union import HUnion
from hwt.interfaces.std import Handshaked
from hwt.interfaces.structIntf import StructIntf
from hwt.interfaces.unionIntf import UnionSink, UnionSource
from hwt.interfaces.utils import addClkRstn
from hwt.simulator.simTestCase import SimTestCase
from hwt.synthesizer.interfaceLevel.emptyUnit import EmptyUnit
from hwtLib.types.ctypes import uint16_t, uint8_t, int16_t

union0 = HUnion((uint16_t, "b16"), (HStruct(
    (uint8_t, "b16to8"), (uint8_t, "b8to0")), "struct16"), (HUnion(
        (Bits(16), "b16"),
        (uint16_t, "b16u"),
        (int16_t, "b16s"),
    ), "union"))


class SimpleUnionSlave(EmptyUnit):
    def __init__(self, intfCls, dtype):
        self.dtype = dtype
        self.intfCls = intfCls
        super(SimpleUnionSlave, self).__init__()

    def mkFieldIntf(self, structIntf, frameTmplItem):
        t = frameTmplItem.dtype
        if isinstance(t, HUnion):
            return self.intfCls(t, structIntf._instantiateFieldFn)
        elif isinstance(t, HStruct):
Example #20
0
    (uint32_t, None),
    (uint64_t, "item5"),
    (uint32_t, None),
)

s2_oneFrame = """<FrameTmpl start:0, end:192
     63                                                             0
     -----------------------------------------------------------------
0    |                             item0                             |
1    |             item5             |XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX|
2    |XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX|             item5             |
     -----------------------------------------------------------------
>"""

struct_with_union = HStruct((HUnion(
    (uint8_t, "a"),
    (uint8_t, "b"),
), "u"))

struct_with_union_str = """<FrameTmpl start:0, end:64
     63                                                             0
     -----------------------------------------------------------------
0    |XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX|<union>|
0    |^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^|  u.a  |
0    |^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^|  u.b  |
     -----------------------------------------------------------------
>"""

union0 = HUnion(
    (HStruct(
        (uint8_t, "a0"),
        (uint8_t, "a1"),
Example #21
0
    (uint64_t, None),
    (uint64_t, "item1_0"),
    (uint64_t, "item1_1"),
    (uint64_t, None),
)

s1field_composit0 = HStruct(
    (uint32_t, "item0"), (uint32_t, "item1"),
)

unionOfStructs = HUnion(
    (HStruct(
        (uint64_t, "itemA0"),
        (uint64_t, "itemA1")
        ), "frameA"),
    (HStruct(
        (uint32_t, "itemB0"),
        (uint32_t, "itemB1"),
        (uint32_t, "itemB2"),
        (uint32_t, "itemB3")
        ), "frameB")
)

unionSimple = HUnion(
    (uint32_t, "a"),
    (int32_t, "b")
)

structStream64 = HStruct(
    (HStream(uint64_t), "streamIn")
)