Line 4... |
Line 4... |
#
|
#
|
################################################################################
|
################################################################################
|
|
|
import re
|
import re
|
|
|
|
from ssbccUtil import IsIntExpr
|
from ssbccUtil import IsPosInt
|
from ssbccUtil import IsPosInt
|
from ssbccUtil import IsPowerOf2
|
from ssbccUtil import IsPowerOf2
|
|
from ssbccUtil import ParseIntExpr
|
from ssbccUtil import SSBCCException
|
from ssbccUtil import SSBCCException
|
|
|
class SSBCCperipheral:
|
class SSBCCperipheral:
|
"""Base class for peripherals"""
|
"""Base class for peripherals"""
|
|
|
Line 99... |
Line 101... |
Virtual method to generate the VHDL version of the peripheral.
|
Virtual method to generate the VHDL version of the peripheral.
|
Raise an exception if there is no VHDL version of the peripheral.
|
Raise an exception if there is no VHDL version of the peripheral.
|
"""
|
"""
|
raise Exception('VHDL is not implemented for this peripheral');
|
raise Exception('VHDL is not implemented for this peripheral');
|
|
|
def IsIntExpr(self,value):
|
|
"""
|
|
Test the string to see if it is a well-formatted integer or multiplication
|
|
of two integers.
|
|
Allow underscores as per Verilog.
|
|
"""
|
|
if re.match(r'[1-9][0-9_]*',value):
|
|
return True;
|
|
elif re.match(r'\([1-9][0-9_]*(\*[1-9][0-9_]*)+\)',value):
|
|
return True;
|
|
else:
|
|
return False;
|
|
|
|
def IsParameter(self,config,name):
|
|
"""
|
|
See if the provided symbol name is a parameter in the processor core.
|
|
config ssbccConfig object for the procedssor core
|
|
name symbol name
|
|
"""
|
|
return config.IsParameter(name);
|
|
|
|
def LoadCore(self,filename,extension):
|
def LoadCore(self,filename,extension):
|
"""
|
"""
|
Read the source HDL for the peripheral from the same directory as the python
|
Read the source HDL for the peripheral from the same directory as the python
|
script.
|
script.
|
filename name for the python peripheral (usually "__file__")
|
filename name for the python peripheral (usually "__file__")
|
Line 136... |
Line 117... |
fp = open(hdlName,'r');
|
fp = open(hdlName,'r');
|
body = fp.read();
|
body = fp.read();
|
fp.close();
|
fp.close();
|
return body;
|
return body;
|
|
|
def ParseIntExpr(self,value):
|
|
"""
|
|
Convert a string containing well-formatted integer or multiplication of two
|
|
integers.
|
|
Allow underscores as per Verilog.
|
|
Note: If this routine is called, then the value should have already been
|
|
verified to be a well-formatted integer string.
|
|
"""
|
|
if not self.IsIntExpr(value):
|
|
raise Exception('Program Bug -- shouldn\'t call with a badly formatted integer expression');
|
|
return eval(re.sub('_','',value));
|
|
|
|
##############################################################################
|
##############################################################################
|
#
|
#
|
# Methods to supplement python intrisics for the optFn argument of AddAttr
|
# Methods to supplement python intrisics for the optFn argument of AddAttr
|
#
|
#
|
# Note: AddAttr embelleshes exception messages with the symbol name and
|
# Note: AddAttr embelleshes exception messages with the symbol name and
|
Line 178... |
Line 147... |
if not config.IsConstant(value):
|
if not config.IsConstant(value):
|
raise SSBCCException('Unrecognized constant');
|
raise SSBCCException('Unrecognized constant');
|
value = config.constants[value];
|
value = config.constants[value];
|
if not IsPosInt(value):
|
if not IsPosInt(value):
|
raise SSBCCException('Must be a constant positive integer');
|
raise SSBCCException('Must be a constant positive integer');
|
value = self.ParseIntExpr(value);
|
value = ParseIntExpr(value);
|
if not IsPowerOf2(value):
|
if not IsPowerOf2(value):
|
raise SSBCCException('Must be a power of 2');
|
raise SSBCCException('Must be a power of 2');
|
if not (lowLimit <= value <= highLimit):
|
if not (lowLimit <= value <= highLimit):
|
raise SSBCCException('Must be between %d and %d inclusive' % (lowLimit,highLimit,));
|
raise SSBCCException('Must be between %d and %d inclusive' % (lowLimit,highLimit,));
|
return value;
|
return value;
|
Line 192... |
Line 161... |
Return the integer value of the argument if it is a power of 2. Otherwise
|
Return the integer value of the argument if it is a power of 2. Otherwise
|
throw an error.
|
throw an error.
|
"""
|
"""
|
if not IsPosInt(value):
|
if not IsPosInt(value):
|
raise SSBCCException('Not a positive integer');
|
raise SSBCCException('Not a positive integer');
|
value = self.ParseIntExpr(value);
|
value = ParseIntExpr(value);
|
if not IsPowerOf2(value):
|
if not IsPowerOf2(value):
|
raise SSBCCException('Not a power of 2');
|
raise SSBCCException('Not a power of 2');
|
if value < minValue:
|
if value < minValue:
|
raise SSBCCException('Must be at least %d' % minValue);
|
raise SSBCCException('Must be at least %d' % minValue);
|
return value;
|
return value;
|
Line 206... |
Line 175... |
Return the integer value of the argument unless it is out of bounds.\n
|
Return the integer value of the argument unless it is out of bounds.\n
|
Note: maxValue=0 means that there is no upper limit.
|
Note: maxValue=0 means that there is no upper limit.
|
"""
|
"""
|
if not IsPosInt(value):
|
if not IsPosInt(value):
|
raise SSBCCException('Not a positive integer');
|
raise SSBCCException('Not a positive integer');
|
value = self.ParseIntExpr(value);
|
value = ParseIntExpr(value);
|
if (maxValue != 0) and (value > maxValue):
|
if (maxValue != 0) and (value > maxValue):
|
raise SSBCCException('Out of bounds -- can be at most %d' % maxValue);
|
raise SSBCCException('Out of bounds -- can be at most %d' % maxValue);
|
return value;
|
return value;
|
|
|
def RateMethod(self,config,value):
|
def RateMethod(self,config,value):
|
Line 224... |
Line 193... |
L_DIVISION_RATIO
|
L_DIVISION_RATIO
|
G_CLOCK_FREQUENCY_HZ/19200
|
G_CLOCK_FREQUENCY_HZ/19200
|
G_CLOCK_FREQUENCY_HZ/L_BAUD_RATE
|
G_CLOCK_FREQUENCY_HZ/L_BAUD_RATE
|
100_000_000/G_BAUD_RATE
|
100_000_000/G_BAUD_RATE
|
"""
|
"""
|
if value.find('/') < 0:
|
def TestAndGetValue(self,config,value,position):
|
if self.IsIntExpr(value):
|
if IsIntExpr(value):
|
return str(self.ParseIntExpr(value));
|
return str(ParseIntExpr(value));
|
elif self.IsParameter(config,value):
|
elif config.IsConstant(value):
|
|
value = config.constants[value];
|
|
if not value > 0:
|
|
raise SSBCCException('Constant "%s" must be positive');
|
|
return str(value);
|
|
elif config.IsParameter(value):
|
return value;
|
return value;
|
else:
|
else:
|
raise SSBCCException('Value must be a positive integer or a previously declared parameter');
|
raise SSBCCException('%s must be a positive integer or a previously declared positive constant or a parameter', position);
|
|
if value.find('/') < 0:
|
|
return TestAndGetValue(self,config,value,'Value');
|
else:
|
else:
|
ratearg = re.findall('([^/]+)',value);
|
ratearg = re.findall('([^/]+)',value);
|
if len(ratearg) != 2:
|
if len(ratearg) != 2:
|
raise SSBCCException('Only one "/" allowed in expression');
|
raise SSBCCException('Only one "/" allowed in expression');
|
if not self.IsIntExpr(ratearg[0]) and not self.IsParameter(config,ratearg[0]):
|
ratearg[0] = TestAndGetValue(self,config,ratearg[0],'Numerator');
|
raise SSBCCException('Numerator must be an integer or a previously declared parameter');
|
ratearg[1] = TestAndGetValue(self,config,ratearg[1],'Denominator');
|
if not self.IsIntExpr(ratearg[1]) and not self.IsParameter(config,ratearg[1]):
|
|
raise SSBCCException('Denominator must be an integer or a previously declared parameter');
|
|
return '(%s+%s/2)/%s' % (ratearg[0],ratearg[1],ratearg[1],);
|
return '(%s+%s/2)/%s' % (ratearg[0],ratearg[1],ratearg[1],);
|
|
|
No newline at end of file
|
No newline at end of file
|