Implementing an 8 bit fixed point register

Grant Edwards grante at visi.com
Tue Jul 1 15:38:29 EDT 2008


On 2008-07-01, Terry Reedy <tjreedy at udel.edu> wrote:
>
>
> nickooooola wrote:
>> Hello to all
>> I'm about to write a simulator for a microcontroller in python
>> (why python? because I love it!!!)
>> 
>> but I have a problem.
>> 
>> The registry of this processor are all 8 bit long (and 10 bit for some
>> other strange register)
>> and I need to simulate the fixed point behaviour of the register,
>> and to access the single bit.
>
> In Python3, I would use a (mutable) bytearray.
>
> IDLE 3.0b1
> >>> reg1 = bytearray((0,)*8) # or *10 for 10 bits
> >>> reg1
> bytearray(b'\x00\x00\x00\x00\x00\x00\x00\x00')
> >>> reg1[1]=1
> >>> reg1[1]
> 1
> >>> tuple(reg1)
> (0, 1, 0, 0, 0, 0, 0, 0)
>
> A bytearray subclass could enforce that all 'bits' (stored as bytes) are 
> 0 or 1, have a customized representation to your taste, and add methods 
> like .flipall().

It seems like implementing ALU operations on such arrays would
be a lot more work than implementing bit-indexing on a type
derived on a more "integer" like base. I'm pretty fuzzy on how
one sub-classes basic things like integers, so maybe I'm all
wet, and adding __getitem__ and __setitem__ to an integer type
isn't even possible.

-- 
Grant Edwards                   grante             Yow! Do you guys know we
                                  at               just passed thru a BLACK
                               visi.com            HOLE in space?



More information about the Python-list mailing list