コード例 #1
0
class Register:
    """H-x800 register class."""
    def __init__(self, data=0, name=None, width=16):
        if data < 0 or data > (2**width - 1):
            raise ValueError("Invalid value for %d-bit register!" % width)
        if name and name not in REGISTER_NAMES:
            raise ValueError("Invalid register name %s!" % name)
        if width != 16 and width != 24:
            raise ValueError("Invalid register width %d, must be 16 or 24!" %
                             width)
        self._data = BitField(0,
                              width=width,
                              numbering=BitField.BIT_SCHEME_MSB_1,
                              order=BitField.BIT_ORDER_MSB_LEFT)
        self._data[1:width] = data
        self._name = name
        self._width = width

    @property
    def value(self):
        return int(self._data)

    @value.setter
    def value(self, value):
        if value < 0 or value > (2**self._width - 1):
            raise ValueError("Invalid value for %d-bit register!" %
                             self._width)
        self._data[1:self._width] = value

    @property
    def name(self):
        return self._name

    @property
    def width(self):
        return int(self._width)

    def __getitem__(self, key):
        if isinstance(key, slice):
            return self._data[key.start:key.stop]
        else:
            return self._data[key]

    def __setitem__(self, key, value):
        if isinstance(key, slice):
            self._data[key.start:key.stop] = value
        else:
            self._data[key] = value

    def __len__(self):
        return self.width

    def __repr__(self):
        return "0o%06o" % self.value

    def clear(self):
        self._data.clear()
コード例 #2
0
def test_msb_left_msb1_48bit():
    b = BitField(0,
                 width=48,
                 numbering=BitField.BIT_SCHEME_MSB_1,
                 order=BitField.BIT_ORDER_MSB_LEFT)
    print("TEST: check initial value")
    my_assert(b.value, 0)
    print("TEST: check maxval")
    my_assert(b.maxval, 2**48 - 1)
    print("TEST: check width")
    my_assert(b.width, 48)
    print("TEST: check length")
    my_assert(len(b), 48)
    print("TEST: set slice [33:48] = 65535")
    b[33:48] = 0b1111111111111111
    my_assert(b.value, 65535)
    print("TEST: set value to zero")
    b[1:48] = 0
    my_assert(b.value, 0)
    print("TEST: set value to all ones using slice")
    b[1:48] = b.maxval
    my_assert(b.value, 2**48 - 1)
    print("TEST: set value to all zeros using slice")
    b[1:48] = 0
    my_assert(b.value, 0)
    print("TEST: set value to all ones using set")
    b.set()
    my_assert(b.value, 2**48 - 1)
    print("TEST: set value to all zeros using clear")
    b.clear()
    my_assert(b.value, 0)
    print("TEST: set high 12 bits to test-pattern")
    b[1:12] = 0b101010101010
    my_assert(b.value, 187604171489280)
    print("TEST: set value to all zeros using clear")
    b.clear()
    my_assert(b.value, 0)
    print("TEST: set 2nd 12 bits to test-pattern")
    b[13:24] = 0b101010101010
    my_assert(b.value, 45801799680)
    print("TEST: set value to all zeros using clear")
    b.clear()
    my_assert(b.value, 0)
    print("TEST: set 3rd 12 bits to test-pattern")
    b[25:36] = 0b101010101010
    my_assert(b.value, 11182080)
    print("TEST: set value to all zeros using clear")
    b.clear()
    my_assert(b.value, 0)
    print("TEST: set low 12 bits to test-pattern")
    b[37:48] = 0b101010101010
    my_assert(b.value, 2730)
    print("TEST: set value to all zeros using clear")
    b.clear()
    my_assert(b.value, 0)
コード例 #3
0
def test_msb_left_msb1_12bit():
    b = BitField(0,
                 width=12,
                 numbering=BitField.BIT_SCHEME_MSB_1,
                 order=BitField.BIT_ORDER_MSB_LEFT)
    print("TEST: check initial value")
    my_assert(b.value, 0)
    print("TEST: indices=", b.indices)
    print("TEST: check maxval")
    my_assert(b.maxval, 4095)
    print("TEST: check width")
    my_assert(b.width, 12)
    print("TEST: check length")
    my_assert(len(b), 12)
    print("TEST: set slice [9:12] = 15")
    b[9:12] = 0b1111
    my_assert(b.value, 15)
    print("TEST: set value to zero")
    b[1:12] = 0
    my_assert(b.value, 0)
    print("TEST: set value to all ones using slice")
    b[1:12] = b.maxval
    my_assert(b.value, 4095)
    print("TEST: set value to all zeros using slice")
    b[1:12] = 0
    my_assert(b.value, 0)
    print("TEST: set value to all ones using set")
    b.set()
    my_assert(b.value, 4095)
    print("TEST: set value to all zeros using clear")
    b.clear()
    my_assert(b.value, 0)
    print("TEST: set high 6 bits to test-pattern")
    b[1:6] = 0b101010
    my_assert(b.value, 2688)
    print("TEST: set value to all zeros using clear")
    b.clear()
    my_assert(b.value, 0)
    print("TEST: set low 6 bits to test-pattern")
    b[7:12] = 0b101010
    my_assert(b.value, 42)
    print("TEST: set value to all zeros using clear")
    b.clear()
    my_assert(b.value, 0)
    print("TEST: ripple a one right")
    for i in range(12):
        b.set(i + 1)
        my_assert(b.value, 2**(12 - i - 1))
        b.clear(i + 1)
        my_assert(b.value, 0)
    print("TEST: ripple a one left")
    for i in range(12):
        b.set(12 - i)
        my_assert(b.value, 2**i)
        b.clear(12 - i)
        my_assert(b.value, 0)
    print("TEST: shift a one right, filling")
    good = 0
    for i in range(12):
        b.set(i + 1)
        good += 2**(12 - i - 1)
        my_assert(b.value, good)
    b.clear()
    my_assert(b.value, 0)
    print("TEST: shift a one left, filling")
    good = 0
    for i in range(12):
        b.set(12 - i)
        good += 2**i
        my_assert(b.value, good)
    b.clear()
    my_assert(b.value, 0)