- 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,52 +153,48 @@ 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 ) ###
bina = re.search ( "0[bB]([0-1]+)", nb ) if isinstance(nb, types.StringType):
hexa = re.search ( "0[xX]([0-9,A-F]+)", nb ) bina = re.search ( "0[bB]([0-1]+)", nb )
oct = re.search ( "0[oO]([0-7]+)", nb ) hexa = re.search ( "0[xX]([0-9,A-F,a-f]+)", nb )
dec = re.search ( "([0-9]+)", nb ) oct = re.search ( "0[oO]([0-7]+)", 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) base = 16
string = "" # The parameter is an octal number
raise "Hexa not ready yet." elif oct :
# The parameter is an octal number result = oct.group(1)
elif oct : base = 8
result = oct.group(1) # The parameter is a decimal number
string = "" elif dec :
raise "Octal not ready yet." result = oct.group(1)
# The parameter is a decimal number base = 10
elif dec :
num = int ( nb ) # Error if the string does not belong to the previous categories
string = ""
if not num :
string = "0"
else : else :
if num < 0 : raise "\n[Stratus ERROR] Const : the argument must be a string representing a number in decimal, binary (0b) or hexa (0x).\n"
width = ilog2(-num)
mask = iexp2(width) - 1 nbit = len(result)
string = bin(num & mask) nbint = int(result,base)
string = string[2:]
else: ### nb is int or long
while num : else:
num2 = num nbint = nb
num /= 2 nbit = nb.bit_length()
if ( num * 2 ) != num2 : string += "1"
else : string += "0" if (nbint < 0):
string = '1' + bin(nbint & (2**(nbit+1)-1))[2:]
# Error if the string does not belong to the previous categories else:
else : string = '0' + bin(nbint)[2:]
raise "\n[Stratus ERROR] Const : the argument must be a string representing a number in decimal, binary (0b) or hexa (0x).\n"
return string return string