Skip to content
Snippets Groups Projects
Commit a2510954 authored by Mitchel Humpherys's avatar Mitchel Humpherys
Browse files

lrdp-v2: register.py: add support for setting the register value

The Register class currently can't handle setting the `value'
attribute. Fix this. Also add a `zero' convenience method for zeroing
out a register.

After adding the following doctest:

    >>> abc.value = 0
    >>> abc.value
    0

but before fixing __setattr__, I get:

    **********************************************************************
    File "register.py", line 42, in __main__.Register
    Failed example:
        abc.value = 0
    Exception raised:
        Traceback (most recent call last):
          File "/usr/lib/python2.7/doctest.py", line 1289, in __run
            compileflags, 1) in test.globs
          File "<doctest __main__.Register[10]>", line 1, in <module>
            abc.value = 0
          File "register.py", line 106, in __setattr__
            raise AttributeError
        AttributeError
    **********************************************************************
    File "register.py", line 43, in __main__.Register
    Failed example:
        abc.value
    Expected:
        0
    Got:
        49
    **********************************************************************
    1 items had failures:
       2 of  20 in __main__.Register
    ***Test Failed*** 2 failures.

After the fix in __setattr__, all tests pass successfully.

Change-Id: I6d39261b139aae0def58f38a4291baad651b10e3
parent 779e720e
Branches
No related tags found
No related merge requests found
......@@ -39,6 +39,15 @@ class Register(object):
>>> abc.other = 0x3
>>> hex(abc.value)
'0x31'
>>> abc.value = 0
>>> abc.value
0
>>> abc.value = 42
>>> abc.value
42
>>> abc.zero()
>>> abc.value
0
You can also overlay fields on top of each other without problems:
......@@ -84,6 +93,9 @@ class Register(object):
"""
self._regs[field] = bitrange
def zero(self):
object.__setattr__(self, 'value', 0)
def __dir__(self):
return self.__dict__.keys() + self._regs.keys()
......@@ -96,6 +108,9 @@ class Register(object):
return bitops.bvalsel(msb, lsb, self.value)
def __setattr__(self, name, newvalue):
if name == 'value':
object.__setattr__(self, 'value', newvalue)
return
if name not in self._regs:
raise AttributeError
if self.value is None:
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment