1 |
2 |
sinclairrf |
################################################################################
|
2 |
|
|
#
|
3 |
|
|
# Copyright 2012, Sinclair R.F., Inc.
|
4 |
|
|
#
|
5 |
|
|
################################################################################
|
6 |
|
|
|
7 |
|
|
import re
|
8 |
|
|
|
9 |
|
|
from ssbccUtil import SSBCCException
|
10 |
|
|
|
11 |
|
|
class SSBCCperipheral:
|
12 |
|
|
"""Base class for peripherals"""
|
13 |
|
|
|
14 |
|
|
def __init__(self,peripheralFile,config,param_list,loc):
|
15 |
|
|
"""
|
16 |
|
|
Prototype constructor.
|
17 |
|
|
peripheralFile the full path name of the peripheral source
|
18 |
|
|
Note: "__file__" doesn't work because 'execfile" and
|
19 |
|
|
"exec" are used to load the python script for the
|
20 |
|
|
peripheral.
|
21 |
|
|
config the ssbccConfig object for the processor core
|
22 |
|
|
param_list parameter list for the processor
|
23 |
|
|
loc file name and line number for error messages
|
24 |
|
|
"""
|
25 |
|
|
pass;
|
26 |
|
|
|
27 |
|
|
def AddAttr(self,config,name,value,reformat,loc,optFn=None):
|
28 |
|
|
"""
|
29 |
|
|
Add attribute to the peripheral:
|
30 |
|
|
config ssbccConfig object for the procedssor core
|
31 |
|
|
name attribute name
|
32 |
|
|
value possibly optional value for the attribute
|
33 |
|
|
reformat regular expression format for the attribute value
|
34 |
|
|
Note: reformat=None means the attribute can only be set to True
|
35 |
|
|
loc file name and line number for error messages
|
36 |
|
|
optFn optional function to set stored type
|
37 |
|
|
"""
|
38 |
|
|
if hasattr(self,name):
|
39 |
|
|
raise SSBCCException('%s repeated at %s' % (name,loc,));
|
40 |
|
|
if reformat == None:
|
41 |
|
|
if value != None:
|
42 |
|
|
raise SSBCCException('No parameter allowed for %s at %s' % (name,loc,));
|
43 |
|
|
setattr(self,name,True);
|
44 |
|
|
else:
|
45 |
|
|
if value == None:
|
46 |
|
|
raise SSBCCException('%s missing value at %s' % (name,loc,));
|
47 |
|
|
if not re.match(reformat,value):
|
48 |
|
|
raise SSBCCException('I/O symbol at %s does not match required format "%s": "%s"' % (loc,reformat,value,));
|
49 |
|
|
if optFn != None:
|
50 |
|
|
value = optFn(value);
|
51 |
|
|
setattr(self,name,value);
|
52 |
|
|
|
53 |
|
|
def AddRateMethod(self,config,name,param_arg,loc):
|
54 |
|
|
"""
|
55 |
|
|
Add parameter or fraction for rates such as timer rates or baud rates:
|
56 |
|
|
config ssbccConfig object for the procedssor core
|
57 |
|
|
name attribute name
|
58 |
|
|
param constant, parameter, or fraction of the two to specify the
|
59 |
|
|
clock counts between events
|
60 |
|
|
loc file name and line number for error messages
|
61 |
|
|
"""
|
62 |
|
|
if hasattr(self,name):
|
63 |
|
|
raise SSBCCException('%s repeated at %s' % (name,loc,));
|
64 |
|
|
if param_arg.find('/') < 0:
|
65 |
|
|
if self.IsInt(param_arg):
|
66 |
|
|
setattr(self,name,str(self.ParseInt(param_arg)));
|
67 |
|
|
elif self.IsParameter(config,param_arg):
|
68 |
|
|
set(self,name,param_arg);
|
69 |
|
|
else:
|
70 |
|
|
raise SSBCCException('%s with no "/" must be an integer or a previously declared parameter at %s' % (name,loc,));
|
71 |
|
|
else:
|
72 |
|
|
ratearg = re.findall('([^/]+)',param_arg);
|
73 |
|
|
if len(ratearg) == 2:
|
74 |
|
|
if not self.IsInt(ratearg[0]) and not self.IsParameter(config,ratearg[0]):
|
75 |
|
|
raise SSBCCException('Numerator in %s must be an integer or a previously declared parameter at %s' % (name,loc,));
|
76 |
|
|
if not self.IsInt(ratearg[1]) and not self.IsParameter(config,ratearg[1]):
|
77 |
|
|
raise SSBCCException('Denominator in %s must be an integer or a previously declared parameter at %s' % (name,loc,));
|
78 |
|
|
for ix in range(2):
|
79 |
|
|
if self.IsInt(ratearg[ix]):
|
80 |
|
|
ratearg[ix] = str(self.ParseInt(ratearg[ix]));
|
81 |
|
|
setattr(self,name,'('+ratearg[0]+'+'+ratearg[1]+'/2)/'+ratearg[1]);
|
82 |
|
|
if not hasattr(self,name):
|
83 |
|
|
raise SSBCCException('Bad %s value at %s: "%s"' % (name,loc,param_arg,));
|
84 |
|
|
|
85 |
|
|
def GenAssembly(self,config):
|
86 |
|
|
"""
|
87 |
|
|
Used to generate any assembly modules associated with the peripheral.
|
88 |
|
|
"""
|
89 |
|
|
pass;
|
90 |
|
|
|
91 |
|
|
def GenHDL(self,fp,config):
|
92 |
|
|
"""
|
93 |
|
|
Generate the peripheral HDL.
|
94 |
|
|
fp file pointer for the output processor
|
95 |
|
|
config ssbccConfig object for the procedssor core
|
96 |
|
|
"""
|
97 |
|
|
if config.Get('hdl') == 'Verilog':
|
98 |
|
|
self.GenVerilog(fp,config);
|
99 |
|
|
elif config.Get('hdl') == 'VHDL':
|
100 |
|
|
self.GenVHDL(fp,config);
|
101 |
|
|
else:
|
102 |
|
|
raise SSBCCException('HDL "%s" not implemented' % config.Get('hdl'));
|
103 |
|
|
|
104 |
|
|
def GenVerilog(self,fp,config):
|
105 |
|
|
"""
|
106 |
|
|
Generate the Verilog version of the peripheral.
|
107 |
|
|
Raise an exception if there is no Verilog version of the peripheral.
|
108 |
|
|
"""
|
109 |
|
|
raise Exception('Verilog is not implemented for this peripheral');
|
110 |
|
|
|
111 |
|
|
def GenVerilogFinal(self,config,body):
|
112 |
|
|
"""
|
113 |
|
|
Clean up the peripheral code.
|
114 |
|
|
Change "$clog2" to "clog2" for simulators and synthesis tools that don't
|
115 |
|
|
recognize or process "$clog2."
|
116 |
|
|
"""
|
117 |
|
|
if config.Get('define_clog2'):
|
118 |
|
|
body = re.sub('\$clog2','clog2',body);
|
119 |
|
|
return body;
|
120 |
|
|
|
121 |
|
|
def GenVHDL(self,fp,config):
|
122 |
|
|
"""
|
123 |
|
|
Generate the VHDL version of the peripheral.
|
124 |
|
|
Raise an exception if there is no VHDL version of the peripheral.
|
125 |
|
|
"""
|
126 |
|
|
raise Exception('VHDL is not implemented for this peripheral');
|
127 |
|
|
|
128 |
|
|
def IsInt(self,value):
|
129 |
|
|
"""
|
130 |
|
|
Test the string to see if it is a well-formatted integer.
|
131 |
|
|
Allow underscores as per Verilog.
|
132 |
|
|
"""
|
133 |
|
|
if re.match(r'[1-9][0-9_]*$',value):
|
134 |
|
|
return True;
|
135 |
|
|
else:
|
136 |
|
|
return False;
|
137 |
|
|
|
138 |
|
|
def IsIntExpr(self,value):
|
139 |
|
|
"""
|
140 |
|
|
Test the string to see if it is a well-formatted integer or multiplication
|
141 |
|
|
of two integers.
|
142 |
|
|
Allow underscores as per Verilog.
|
143 |
|
|
"""
|
144 |
|
|
if re.match(r'[1-9][0-9_]*',value):
|
145 |
|
|
return True;
|
146 |
|
|
elif re.match(r'\([1-9][0-9_]*(\*[1-9][0-9_]*)+\)',value):
|
147 |
|
|
return True;
|
148 |
|
|
else:
|
149 |
|
|
return False;
|
150 |
|
|
|
151 |
|
|
def IsParameter(self,config,name):
|
152 |
|
|
"""
|
153 |
|
|
See if the provided symbol name is a parameter in the processor core.
|
154 |
|
|
config ssbccConfig object for the procedssor core
|
155 |
|
|
name symbol name
|
156 |
|
|
"""
|
157 |
|
|
return config.IsParameter(name);
|
158 |
|
|
|
159 |
|
|
def LoadCore(self,filename,extension):
|
160 |
|
|
"""
|
161 |
|
|
Read the source HDL for the peripheral from the same directory as the python
|
162 |
|
|
script.
|
163 |
|
|
filename name for the python peripheral (usually "__file__")
|
164 |
|
|
extension the string such as ".v" or ".vhd" required by the HDL\n
|
165 |
|
|
Note: The '.' must be included in the extension. For example, the UART
|
166 |
|
|
peripheral uses '_Rx.v' and '_Tx.v' or similar to invoke the UART_Tx
|
167 |
|
|
and UART_Rx HDL files.
|
168 |
|
|
"""
|
169 |
|
|
hdlName = re.sub(r'\.py$',extension,filename);
|
170 |
|
|
fp = open(hdlName,'r');
|
171 |
|
|
body = fp.read();
|
172 |
|
|
fp.close();
|
173 |
|
|
return body;
|
174 |
|
|
|
175 |
|
|
def ParseInt(self,value):
|
176 |
|
|
"""
|
177 |
|
|
Convert a well-formatted integer string to an integer.
|
178 |
|
|
Allow underscores as per Verilog.
|
179 |
|
|
Note: If this routine is called, then the value should have already been
|
180 |
|
|
verified to be a well-formatted integer string.
|
181 |
|
|
"""
|
182 |
|
|
if not self.IsInt(value):
|
183 |
|
|
raise Exception('Program Bug -- shouldn\'t call with a badly formatted integer');
|
184 |
|
|
return int(re.sub('_','',value));
|
185 |
|
|
|
186 |
|
|
def ParseIntExpr(self,value):
|
187 |
|
|
"""
|
188 |
|
|
Convert a string containing well-formatted integer or multiplication of two
|
189 |
|
|
integers.
|
190 |
|
|
Allow underscores as per Verilog.
|
191 |
|
|
Note: If this routine is called, then the value should have already been
|
192 |
|
|
verified to be a well-formatted integer string.
|
193 |
|
|
"""
|
194 |
|
|
if not self.IsIntExpr(value):
|
195 |
|
|
raise Exception('Program Bug -- shouldn\'t call with a badly formatted integer expression');
|
196 |
|
|
return eval(re.sub('_','',value));
|