verilog like class w/ bitslicing & int/long classtype
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 -- http://mail.python.org/mailman/listinfo/python-list
Re: verilog like class w/ bitslicing & int/long classtype
mark.sea...@gmail.com wrote: 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). 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 -- http://mail.python.org/mailman/listinfo/python-list
Re: verilog like class w/ bitslicing & int/long classtype
On Jan 30, 9:02 am, mark.sea...@gmail.com wrote: > 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 -- http://mail.python.org/mailman/listinfo/python-list
Re: verilog like class w/ bitslicing & int/long classtype
On Jan 30, 9:55 am, Stef Mientki wrote: > 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 :-) -- http://mail.python.org/mailman/listinfo/python-list
Re: verilog like class w/ bitslicing & int/long classtype
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. -- http://mail.python.org/mailman/listinfo/python-list
Re: verilog like class w/ bitslicing & int/long classtype
mark.sea...@gmail.com wrote: 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. -- http://mail.python.org/mailman/listinfo/python-list 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 -- http://mail.python.org/mailman/listinfo/python-list
Re: verilog like class w/ bitslicing & int/long classtype
mark.sea...@gmail.com wrote: 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. -- http://mail.python.org/mailman/listinfo/python-list 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 -- http://mail.python.org/mailman/listinfo/python-list
Re: verilog like class w/ bitslicing & int/long classtype
On Jan 29, 3:13 pm, Stef Mientki wrote: > mark.sea...@gmail.com wrote: > > 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. > > -- > >http://mail.python.org/mailman/listinfo/python-list > > 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. -- http://mail.python.org/mailman/listinfo/python-list
Re: verilog like class w/ bitslicing & int/long classtype
John Machin wrote: On Jan 30, 9:02 am, mark.sea...@gmail.com wrote: 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 -- http://mail.python.org/mailman/listinfo/python-list
Re: verilog like class w/ bitslicing & int/long classtype
On Fri, Jan 30, 2009 at 9:02 AM, wrote: > 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 > is it so bad to type: >>> shadow_register[:] 170 ?? I like this because it is quite clear as to what you are doing, and is consistent with the bit slicing you have (already) implemented. Cheers, -- http://mail.python.org/mailman/listinfo/python-list
Re: verilog like class w/ bitslicing & int/long classtype
On Jan 29, 3:13 pm, Stef Mientki wrote: > mark.sea...@gmail.com wrote: > > 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. > > -- > >http://mail.python.org/mailman/listinfo/python-list > > 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 -- http://mail.python.org/mailman/listinfo/python-list
Re: verilog like class w/ bitslicing & int/long classtype
On Fri, 30 Jan 2009 00:25:03 +0100, Stef Mientki wrote: > 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 -- http://mail.python.org/mailman/listinfo/python-list
Re: verilog like class w/ bitslicing & int/long classtype
Marc 'BlackJack' Rintsch wrote: On Fri, 30 Jan 2009 00:25:03 +0100, Stef Mientki wrote: 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__ >>print shadow_register 170 with _str__ = __repr__ >>print shadow_register 0xAA cheers, Stef -- http://mail.python.org/mailman/listinfo/python-list
Re: verilog like class w/ bitslicing & int/long classtype
Quoth Stef Mientki : > Marc 'BlackJack' Rintsch wrote: > > On Fri, 30 Jan 2009 00:25:03 +0100, Stef Mientki wrote: > > > > > >> 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__ > >>print shadow_register > 170 > > with _str__ = __repr__ > >>print shadow_register > 0xAA >>> '__str__' in dir(170) True That is, the superclass (int) has an __str__, so you have to override it. --RDM -- http://mail.python.org/mailman/listinfo/python-list
Re: verilog like class w/ bitslicing & int/long classtype
On Fri, 30 Jan 2009 21:59:34 +0100, Stef Mientki wrote: > Marc 'BlackJack' Rintsch wrote: >> On Fri, 30 Jan 2009 00:25:03 +0100, Stef Mientki wrote: >> >> >>> 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__ > >>print shadow_register > 170 > > with _str__ = __repr__ > >>print shadow_register > 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 -- http://mail.python.org/mailman/listinfo/python-list