00001 #!/usr/local/bin/python
00002 #
00003 # Copyright 2004
00004 # by
00005 # The Board of Trustees of the
00006 # Leland Stanford Junior University.
00007 # All rights reserved.
00008 #
00009
00010
00011 __facility__ = "Online"
00012 __abstract__ = "Bit field utility methods"
00013 __author__ = "Selim Tuvi <stuvi@slac.stanford.edu> SLAC - GLAST I&T/Online"
00014 __date__ = ("$Date: 2004/12/09 02:08:39 $").split(' ')[1]
00015 __version__ = "$Revision: 2.5 $"
00016 __release__ = "$Name: R04-12-00 $"
00017 __credits__ = "SLAC"
00018
00019 import LATTE.copyright_SLAC
00020
00021 import sys
00022 import string
00023 import inspect
00024 import struct
00025
00026 class bf(object):
00027 def __init__(self,value=0L):
00028 self._d = long(value)
00029
00030 def set(self, value):
00031 self._d = long(value)
00032
00033 def __getitem__(self, index):
00034 return (self._d >> index) & 1
00035
00036 def __setitem__(self,index,value):
00037 value = (value&1L)<<index
00038 mask = 1L<<index
00039 self._d = (self._d & ~mask) | value
00040
00041 def __getslice__(self, start, end):
00042 mask = 2**(end - start) -1
00043 return (self._d >> start) & mask
00044
00045 def __setslice__(self, start, end, value):
00046 mask = 2**(end - start) -1
00047 value = (long(value) & mask) << start
00048 mask = mask << start
00049 self._d = (self._d & ~mask) | value
00050 return (self._d >> start) & mask
00051
00052 def getOnBits(self):
00053 value = self._d
00054 i = 0
00055 bits = []
00056 while value != 0:
00057 bit = value & 1L
00058 if bit: bits.append(i)
00059 value = value >> 1
00060 i+=1
00061 return bits
00062
00063 def __int__(self):
00064 return self._d
00065
00066 def __str__(self):
00067 return str(self._d)
00068
00069 class BitField(object):
00070 def __init__(self, ui, bf_def):
00071 self.bf_ui = bf(ui)
00072 self.bf_def = bf_def
00073
00074 def set(self, ui):
00075 self.bf_ui.set(ui)
00076
00077 def __setattr__(self, attribute, val):
00078 if attribute == 'ui':
00079 self.__dict__['bf_ui'] = bf(val)
00080 return
00081 elif attribute in ('bf_ui', 'bf_def'):
00082 self.__dict__[attribute] = val
00083 return
00084 else:
00085 for (bitField, fromBit, toBit) in self.bf_def:
00086 if attribute.lower() == bitField:
00087 self.bf_ui[fromBit:toBit] = val
00088 return
00089 self.__dict__[attribute] = val
00090
00091 def __getattr__(self, attribute):
00092 if attribute == 'ui':
00093 return int(self.__dict__['bf_ui'])
00094 elif attribute in ('bf_ui', 'bf_def'):
00095 return self.__dict__[attribute]
00096 else:
00097 for (bitField, fromBit, toBit) in self.bf_def:
00098 if attribute == bitField:
00099 return self.bf_ui[fromBit:toBit]
00100 return self.__dict__[attribute]
00101
00102 def __int__(self):
00103 return int(self.__dict__['bf_ui'])
00104
00105 def __str__(self):
00106 return str(self.__dict__['bf_ui'])
00107
00108
00109 def hexConvert(s, size=1, outPutStyle="0x%s "):
00110 """
00111 Convert binary string s
00112 to a hex format
00113
00114 Keyword arguments:
00115
00116 'size' -- How large should each hex value be
00117
00118 'outPutStyle' -- How should each value be formatted
00119 """
00120 res=[]
00121 while s:
00122 temp=s[:size]
00123 s=s[size:]
00124 l=[]
00125 for i in temp:
00126 l.append("%02x"%ord(i))
00127 val=string.join(l,'')
00128 res.append(outPutStyle%val)
00129 return string.join(res,'')
00130
00131 def lhex(x, size=8):
00132 xs = string.lower(hex(x))
00133 l = len(xs)
00134 if xs[l-1] == "l":
00135 xs = xs[2:l-1]
00136 else:
00137 xs = xs[2:l]
00138 l = len(xs)
00139 xs = '0'*(size-l) + xs
00140 return xs
00141
00142 def q(cond,on_true,on_false):
00143 if cond:
00144 if not inspect.isfunction(on_true):
00145 return on_true
00146 else:
00147 return apply(on_true)
00148 else:
00149 if not inspect.isfunction(on_false):
00150 return on_false
00151 else:
00152 return apply(on_false)
00153
00154 def dump_cells(data):
00155 fmt = '!%dI' %(len(data) >> 2)
00156 words = struct.unpack(fmt, data)
00157 counter = 0
00158 for word in words:
00159 if ((counter%4) == 0): print '\n ',
00160 print '0x%08x' %(word),
00161 counter += 1
00162 print
00163
00164 def population(mask):
00165 n = 0
00166 while (mask != 0):
00167 n = n+1
00168 mask = mask & (mask-1)
00169 return n