432,306 Members | 1,621 Online
Need help? Post your question and get tips & solutions from a community of 432,306 IT Pros & Developers. It's quick & easy.

# Implementing an 8 bit fixed point register

 P: n/a 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. f.x. (this is a pseudo python session, only for understanding) >>reg1 = fixed_int(8)reg2 = fixed_int(10)reg1[0].set() or >>reg1[0] = 1 # or True? how to rapresent a binary bitreg1[0] 1 >>reg1[1] 0 >>reg1[9] >>reg2 = 0x7FE # in binary 11111111110 , or 11 bit longreg2 0x7FE #or 1111111110, the memorization truncate the upper bits ( or perhaps generate an exception?) >>reg2 += 0x02 #the result is 10000000000, again not contained in 10 bitreg2 0x00 # truncated again >>myprocessor.flags['z'] 1 # or True? Z flag indicated an overflow in arithmetic operations Is possibile to do so in python? thanks Jul 1 '08 #1
8 Replies

 P: n/a On 1 Jul, 08:57, nickooooola reg1 = fixed_int(8)reg2 = fixed_int(10)reg1[0].set() or >reg1[0] = 1 # or True? how to rapresent a binary bitreg1[0] 1 >reg1[1] 0 >reg1[9] >>reg2 = 0x7FE # in binary 11111111110 , or 11 bit long >reg2 0x7FE #or 1111111110, the memorization truncate the upper bits ( or perhaps generate an exception?)>>reg2 += 0x02 #the result is 10000000000, again not contained in 10 bit >reg2 0x00 # truncated again>>myprocessor.flags['z'] 1 # or True? Z flag indicated an overflow in arithmetic operations Is possibile to do so in python? thanks I am not sure if it is exactly what you are looking for (it may be a bit low level), but myHDL may be interesting. It provides low level H/ W simulation capabilities. http://myhdl.jandecaluwe.com/doku.php Steven Jul 1 '08 #2

 P: n/a nickooooola wrote: Hello to all I'm about to write a simulator for a microcontroller in python (why python? because I love it!!!) [...] 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. f.x. (this is a pseudo python session, only for understanding) >>>reg1 = fixed_int(8)reg2 = fixed_int(10)reg1[0].set() or >>>reg1[0] = 1 # or True? how to rapresent a binary bitreg1[0] 1 >>>reg1[1] 0 >>>reg1[9] >>>reg2 = 0x7FE # in binary 11111111110 , or 11 bit longreg2 0x7FE #or 1111111110, the memorization truncate the upper bits ( or perhaps generate an exception?) >>>reg2 += 0x02 #the result is 10000000000, again not contained in 10 bitreg2 0x00 # truncated again >>>myprocessor.flags['z'] 1 # or True? Z flag indicated an overflow in arithmetic operations Is possibile to do so in python? I did this for a PIC, and generally just used brute force, e.g. temp = reg2 + 0x02 reg2 = temp & 0xFF if temp & 0x100: flags |= CARRY_MASK else: flags &= ~CARRY_MASK if temp & 0xFF: flags &= ~ZERO_MASK else: flags |= ZERO_MASK Since it was a PIC, and there were only a half-dozen arithmetic/logical operations, the code stayed in this form. With something bigger, it would have been attractive to wrap these operations up in a class, as you suggest.. (just a sketch) .. class fixed_register (object): def __init__ (self, size, val=0): self.mask = ~(-1 << size) self.ovfmask = 1 << size self.val = val & self.mask with appropriate __add__, __sub__, etc. Perhaps __getitem__, __setitem__ to manipulate bits. Good Luck, Mel. Jul 1 '08 #3

 P: n/a On 2008-07-01, nickooooola >>reg1 = fixed_int(8)reg2 = fixed_int(10)reg1[0].set() or >>>reg1[0] = 1 # or True? how to rapresent a binary bitreg1[0] 1 >>>reg1[1] 0 >>>reg1[9] >>>reg2 = 0x7FE # in binary 11111111110 , or 11 bit longreg2 0x7FE #or 1111111110, the memorization truncate the upper bits ( or perhaps generate an exception?) >>>reg2 += 0x02 #the result is 10000000000, again not contained in 10 bitreg2 0x00 # truncated again >>>myprocessor.flags['z'] 1 # or True? Z flag indicated an overflow in arithmetic operations Is possibile to do so in python? Yes. Everything shown above is possible. If you really want to get clever, you'll want to read up on the __setattr__, __getattr__, __getitem__, and __setitem__ object methods. They'll allow you to define special handling for the semantics below: foo.something = x # calls foo.__setattr___('something',x) x = foo.something # calls foo.__getattr___('something,) foo[n] = x # calls foo.__setitem__(n,x) x = foo[n] # calls foo.__getitme__(n) In the latter two cases, you can support slicing if you want. That could allows you to grab a "bitfield" out of a register: x = processor.regA[4:7] # get bits 4,5,6 processor.regA[4:7] # set bits 4,5,6 Just remember that in Python slices are traditionally half-open intervals -- they don't include the "right" endpoint. That's going to confuse people who are more used to reading processor data sheets where bit-ranges are traditionally closed intervals. You _could_ implment your __[sg]etitem__ slice handling so that they're treated as closed intervals. That will be a lot more intuitive to people used to dealing with microprocessor register definitions, but could confuse an experienced Python programmer. Writing methods for __setattr__ and __getattr__ is a little tricky. It's very easy to end up with infinite recursion. It's not that hard to fix/avoid, but it takes a while to get the hang of it. -- Grant Edwards grante Yow! at visi.com Jul 1 '08 #4

 P: n/a Thanks to all for the responses! to MEl: I also want to build a pic simulator, but I want to do this as "python for big project" learning exercise, I have used python in the past only for small script and now I want to use it for something more "big". do you have some code to share? myhdl seems interesting, I think that I can take a blick on its source code...I think that must have something like the 8 bit register class that I need somewhere I try what you people have said, and if it turns to be something useful, I report it to the community. Nicola Ps excuse me for my english... Jul 1 '08 #5

 P: n/a 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 bitsreg1 bytearray(b'\x00\x00\x00\x00\x00\x00\x00\x00') >>reg1[1]=1reg1[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(). The overhead of using 8 bytes instead of 1 to hold the object value is actually small compared to the minimum object size of 16 bytes (on Win32XP). >>sys.getsizeof(reg1) 24 In Python2.x, you can use the array module to make equivalent mutable arrays of chars. Terry Jan Reedy Jul 1 '08 #6

 P: n/a On 2008-07-01, Terry Reedy nickooooola wrote: >Hello to allI'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 someother 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 bitsreg1 bytearray(b'\x00\x00\x00\x00\x00\x00\x00\x00') >reg1[1]=1reg1[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? Jul 1 '08 #7