- add int parameter for constant insteed of only string

- add hexa and octal for representation
This commit is contained in:
Roselyne Chotin 2013-04-30 08:01:42 +00:00
parent ecf22a10c1
commit c09ab25abc
1 changed files with 43 additions and 45 deletions

View File

@ -119,17 +119,19 @@ class Constant ( Model ) :
Model.__init__ ( self, name, param ) Model.__init__ ( self, name, param )
self.string = Constant.getString ( param ) self.string = Constant.getString ( param )
self.nbit = len ( self.string ) if 'nbit' in param: self.nbit = param['nbit']
else: self.nbit = len(self.string)
self.string = self.string[0] * (self.nbit - len(self.string)) + self.string
def Interface ( self ) : def Interface ( self ) :
self.outPut = SignalOut ( "output0", self.nbit ) self.outPut = SignalOut ( "o", self.nbit )
self.vdd = VddIn ( "vdd" ) self.vdd = VddIn ( "vdd" )
self.vss = VssIn ( "vss" ) self.vss = VssIn ( "vss" )
def Netlist ( self ) : def Netlist ( self ) :
i = 0 i = 0
for letter in self.string : for letter in self.string[::-1] :
if letter == "1" : self.outPut[i] <= One ( 1 ) if letter == "1" : self.outPut[i] <= One ( 1 )
else : self.outPut[i] <= Zero ( 1 ) else : self.outPut[i] <= Zero ( 1 )
i += 1 i += 1
@ -151,53 +153,49 @@ class Constant ( Model ) :
nb = param['nb'] nb = param['nb']
# Error : if nb is not a string # Error : if nb is not a string
if type ( nb ) != types.StringType : raise "\n[Stratus ERROR] Constant : the argument must be a string.\n" if type ( nb ) not in [types.StringType, types.IntType, types.LongType] : raise "\n[Stratus ERROR] Constant : the argument must be a string, int or long.\n"
### String representing a binary number ( from the LSB to the MSB ) ### ### String representing a binary number ( from the LSB to the MSB ) ###
if isinstance(nb, types.StringType):
bina = re.search ( "0[bB]([0-1]+)", nb ) bina = re.search ( "0[bB]([0-1]+)", nb )
hexa = re.search ( "0[xX]([0-9,A-F]+)", nb ) hexa = re.search ( "0[xX]([0-9,A-F,a-f]+)", nb )
oct = re.search ( "0[oO]([0-7]+)", nb ) oct = re.search ( "0[oO]([0-7]+)", nb )
dec = re.search ( "([0-9]+)", nb ) dec = re.search ( "([0-9]+)", nb )
# The parameter is a binary number # The parameter is a binary number
if bina : if bina :
result = bina.group(1) result = bina.group(1)
string = "" base = 2
for i in range ( len (result)-1, -1, -1 ) : string += result[i]
# The parameter is an hexadecimal number # The parameter is an hexadecimal number
elif hexa : elif hexa :
result = hexa.group(1) result = hexa.group(1)
string = "" base = 16
raise "Hexa not ready yet."
# The parameter is an octal number # The parameter is an octal number
elif oct : elif oct :
result = oct.group(1) result = oct.group(1)
string = "" base = 8
raise "Octal not ready yet."
# The parameter is a decimal number # The parameter is a decimal number
elif dec : elif dec :
num = int ( nb ) result = oct.group(1)
string = "" base = 10
if not num :
string = "0"
else :
if num < 0 :
width = ilog2(-num)
mask = iexp2(width) - 1
string = bin(num & mask)
string = string[2:]
else:
while num :
num2 = num
num /= 2
if ( num * 2 ) != num2 : string += "1"
else : string += "0"
# Error if the string does not belong to the previous categories # Error if the string does not belong to the previous categories
else : else :
raise "\n[Stratus ERROR] Const : the argument must be a string representing a number in decimal, binary (0b) or hexa (0x).\n" raise "\n[Stratus ERROR] Const : the argument must be a string representing a number in decimal, binary (0b) or hexa (0x).\n"
nbit = len(result)
nbint = int(result,base)
### nb is int or long
else:
nbint = nb
nbit = nb.bit_length()
if (nbint < 0):
string = '1' + bin(nbint & (2**(nbit+1)-1))[2:]
else:
string = '0' + bin(nbint)[2:]
return string return string
getString = staticmethod ( getString ) getString = staticmethod ( getString )