verilog like class w/ bitslicing & int/long classtype

mark.seagoe at gmail.com mark.seagoe at gmail.com
Thu Jan 29 17:02:53 EST 2009


I'm trying to make a script environment with datatypes (or classes)
for accessing hardware registers.  At the top level, I would like the
ability to bitwise ops if bit slice brackets are used, but if no
brackets are used, I would like it to write/read the whole value.

For example, if I have something like:

>>> shadow_register = MyRegClass(0xAA)
>>> shadow_register
170
>>> shadow_register[7:4] = 3  # <== changes value to 0x3A
>>> shadow_register
58
>>> shadow_register = 0x89
>>> shadow_register
137
>>> shadow_register[4:1]
4

I have the bitslice part working.  But of course as expected, if I
type
>>> shadow_register
<__main__.boo object at 0x00A130D0>

I wanted to avoid having something like shadow_register.value just
because it's clumsier.  I read about the __new__() class for
overriding the classtype, like:

print 'foo'
class foo(object):
    def __new__(*args): return 0

but if I stick in a __init__(self, val): method, then it chokes saying
val is a global name that's not defined.

Now I know that I have to live with the fact that I can't have
>>> shadow_register = 0x89
Because it will get reassigned from my class value to a newly
intialized memory location (int object).  But can I have it read the
value without the .value extension?  Is this even possible?  Maybe
there's a way to override the = operator?  (Go easy on me - I'm a
hardware guy).

Thx,
Mark






More information about the Python-list mailing list