verilog like class w/ bitslicing & int/long classtype

M

mark.seagoe

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<__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 haveBecause 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
 
J

John Machin

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>

def __str__(self):
return "d" % self.value
 
S

Stef Mientki

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
<__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
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).
Interesting what you're doing. I've struggled with the same issues,
simulating a pic,
never really solved them.
Maybe this is what you're looking for:

class MyRegClass ( object ) :
def __init__ ( self, value ) :
self.Value = value
def __repr__ ( self ) :
line = hex ( self.Value )
line = line [:2] + line [2:].upper()
return line

btw, I'm a hardware guy too, and therefor I've never understood why the
hex function returns lowercase ;-)

cheers,
Stef
 
J

John Machin

  def __repr__ ( self ) :
    line = hex ( self.Value )
    line = line [:2] + line [2:].upper()
    return line

btw, I'm a hardware guy too, and therefor I've never understood why the
hex function returns lowercase ;-)

0xFF?? *Real* hardware guys prefer FFh or 377 :)
 
M

mark.seagoe

Thanks. So far these solutions will return strings. So I can't
really treat it like a variable, yet still perform bitslice on it,
since I need a special class to do bitslice and bit selection, but as
soon as I try to pass it into some other function to check a bit, I
gotta then do another operation to convert back to a variable.

So I can get it to a point where I can do shadow_register[3] and get
the value of any bit, or I can do shadow_register[4:2] and get a
bitslice, and I can write them just as easily at this point. If I add
the __new__ method into the class, the instance obj becomes int, but
is no longer slicable. I think it looks like I can't have it both
ways.
 
S

Stef Mientki

Thanks. So far these solutions will return strings. So I can't
really treat it like a variable, yet still perform bitslice on it,
since I need a special class to do bitslice and bit selection, but as
soon as I try to pass it into some other function to check a bit, I
gotta then do another operation to convert back to a variable.

So I can get it to a point where I can do shadow_register[3] and get
the value of any bit, or I can do shadow_register[4:2] and get a
bitslice, and I can write them just as easily at this point. If I add
the __new__ method into the class, the instance obj becomes int, but
is no longer slicable. I think it looks like I can't have it both
ways.
there are a few vhdl projects,
can't find my links right now
- myhdl
- something like pysystemC

maybe you can find the answer there

cheers,
Stef
 
S

Stef Mientki

Thanks. So far these solutions will return strings. So I can't
really treat it like a variable, yet still perform bitslice on it,
since I need a special class to do bitslice and bit selection, but as
soon as I try to pass it into some other function to check a bit, I
gotta then do another operation to convert back to a variable.

So I can get it to a point where I can do shadow_register[3] and get
the value of any bit, or I can do shadow_register[4:2] and get a
bitslice, and I can write them just as easily at this point. If I add
the __new__ method into the class, the instance obj becomes int, but
is no longer slicable. I think it looks like I can't have it both
ways.
try this:

class MyRegClass ( int ) :
def __init__ ( self, value ) :
self.Value = value
def __repr__ ( self ) :
line = hex ( self.Value )
line = line [:2] + line [2:].upper()
return line
__str__ = __repr__


cheers,
Stef
 
M

mark.seagoe

Thanks.  So far these solutions will return strings.  So I can't
really treat it like a variable, yet still perform bitslice on it,
since I need a special class to do bitslice and bit selection, but as
soon as I try to pass it into some other function to check a bit, I
gotta then do another operation to convert back to a variable.
So I can get it to a point where I can do shadow_register[3] and get
the value of any bit, or I can do shadow_register[4:2] and get a
bitslice, and I can write them just as easily at this point.  If I add
the __new__ method into the class, the instance obj becomes int, but
is no longer slicable.  I think it looks like I can't have it both
ways.

there are a few vhdl projects,
can't find my links right now
- myhdl
- something like pysystemC

maybe you can find the answer there

cheers,
Stef- Hide quoted text -

- Show quoted text -

Thanks, Stef - I'll check them out. I have a feeling they are going
through a preprocessing stage (ie. won't take command in a pure python
window) but it's worth checking out.
 
M

MRAB

John said:
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>

def __str__(self):
return "d" % self.value
That should be:

return "%d" % self.value
 
M

mark.seagoe

Thanks.  So far these solutions will return strings.  So I can't
really treat it like a variable, yet still perform bitslice on it,
since I need a special class to do bitslice and bit selection, but as
soon as I try to pass it into some other function to check a bit, I
gotta then do another operation to convert back to a variable.
So I can get it to a point where I can do shadow_register[3] and get
the value of any bit, or I can do shadow_register[4:2] and get a
bitslice, and I can write them just as easily at this point.  If I add
the __new__ method into the class, the instance obj becomes int, but
is no longer slicable.  I think it looks like I can't have it both
ways.

there are a few vhdl projects,
can't find my links right now
- myhdl
- something like pysystemC

maybe you can find the answer there

cheers,
Stef- Hide quoted text -

- Show quoted text -

Thanks for the tip. I checked out myhdl and it has a class called
intbv which does exactly this, and much more than I need. It's GNU
lesser license though, so not sure how to handle that, if I just want
some concepts of the code as examples, but don't want copy it per se.

Thanks,
Mark
 
M

Marc 'BlackJack' Rintsch

try this:

class MyRegClass ( int ) :
def __init__ ( self, value ) :
self.Value = value
def __repr__ ( self ) :
line = hex ( self.Value )
line = line [:2] + line [2:].upper()
return line

def __repr__(self):
return '0x%X' % self.value
__str__ = __repr__

This is unnecessary, the fallback of `__str__` is `__repr__` already.

Ciao,
Marc 'BlackJack' Rintsch
 
S

Stef Mientki

Marc said:
try this:

class MyRegClass ( int ) :
def __init__ ( self, value ) :
self.Value = value
def __repr__ ( self ) :
line = hex ( self.Value )
line = line [:2] + line [2:].upper()
return line

def __repr__(self):
return '0x%X' % self.value

__str__ = __repr__

This is unnecessary, the fallback of `__str__` is `__repr__` already.
well this is what my Python is doing:

without _str__ = __repr__170

with _str__ = __repr__0xAA

cheers,
Stef
 
R

rdmurray

Quoth Stef Mientki said:
Marc said:
try this:

class MyRegClass ( int ) :
def __init__ ( self, value ) :
self.Value = value
def __repr__ ( self ) :
line = hex ( self.Value )
line = line [:2] + line [2:].upper()
return line

def __repr__(self):
return '0x%X' % self.value

__str__ = __repr__

This is unnecessary, the fallback of `__str__` is `__repr__` already.
well this is what my Python is doing:

without _str__ = __repr__170

with _str__ = __repr__

True

That is, the superclass (int) has an __str__, so you have to override it.

--RDM
 
M

Marc 'BlackJack' Rintsch

Marc said:
try this:

class MyRegClass ( int ) :
def __init__ ( self, value ) :
self.Value = value
def __repr__ ( self ) :
line = hex ( self.Value )
line = line [:2] + line [2:].upper()
return line
def __repr__(self):
return '0x%X' % self.value

__str__ = __repr__
This is unnecessary, the fallback of `__str__` is `__repr__` already.
well this is what my Python is doing:

without _str__ = __repr__170

with _str__ = __repr__0xAA

Then don't inherit from `int`. Why are you doing this anyway? If you
inherit from `int` you shouldn't store the value in an extra attribute
but use the `int`\s value. This way you have one value used by the `int`
methods and one used by your own methods.

Ciao,
Marc 'BlackJack' Rintsch
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,297
Messages
2,571,536
Members
48,284
Latest member
alphabetsalphabets

Latest Threads

Top