treatment of negative constant

This commit is contained in:
Roselyne Chotin 2012-08-09 16:31:36 +00:00
parent 5c593f4161
commit a5bfc0e816
1 changed files with 20 additions and 10 deletions

View File

@ -52,6 +52,7 @@ from Hurricane import *
from st_net import * from st_net import *
from st_instance import Inst from st_instance import Inst
from st_generate import Generate from st_generate import Generate
from util_Misc import ilog2, iexp2
import types import types
@ -137,7 +138,10 @@ class Constant ( Model ) :
modelName = "constant" modelName = "constant"
modelName += "_" modelName += "_"
modelName += param['nb'] if param['nb'][0] == '-':
modelName = modelName + 'm' + param['nb'][1:]
else:
modelName += param['nb']
return modelName return modelName
@ -150,14 +154,14 @@ class Constant ( Model ) :
if type ( nb ) != types.StringType : raise "\n[Stratus ERROR] Constant : the argument must be a string.\n" if type ( nb ) != types.StringType : raise "\n[Stratus ERROR] Constant : the argument must be a string.\n"
### String representing a binary number ( from the LSB to the MSB ) ### ### String representing a binary number ( from the LSB to the MSB ) ###
bin = 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]+)", 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 bin : if bina :
result = bin.group(1) result = bina.group(1)
string = "" string = ""
for i in range ( len (result)-1, -1, -1 ) : string += result[i] for i in range ( len (result)-1, -1, -1 ) : string += result[i]
# The parameter is an hexadecimal number # The parameter is an hexadecimal number
@ -174,15 +178,21 @@ class Constant ( Model ) :
elif dec : elif dec :
num = int ( nb ) num = int ( nb )
string = "" string = ""
if not num : if not num :
string = "0" string = "0"
else : else :
while num : if num < 0 :
num2 = num width = ilog2(-num)
num /= 2 mask = iexp2(width) - 1
if ( num * 2 ) != num2 : string += "1" string = bin(num & mask)
else : string += "0" 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 :