| 1 |
2 |
sinclairrf |
################################################################################
|
| 2 |
|
|
#
|
| 3 |
|
|
# Copyright 2013, Sinclair R.F., Inc.
|
| 4 |
|
|
#
|
| 5 |
|
|
################################################################################
|
| 6 |
|
|
|
| 7 |
|
|
import re;
|
| 8 |
|
|
|
| 9 |
|
|
from ssbccPeripheral import SSBCCperipheral
|
| 10 |
|
|
from ssbccUtil import SSBCCException;
|
| 11 |
|
|
|
| 12 |
|
|
class counter(SSBCCperipheral):
|
| 13 |
|
|
"""
|
| 14 |
|
|
Count received strobes.\n
|
| 15 |
|
|
Usage:
|
| 16 |
|
|
PERIPHERAL counter \\
|
| 17 |
|
|
insignal=i_name \\
|
| 18 |
|
|
inport=I_NAME \\
|
| 19 |
|
|
[width=<N> outlatch=O_NAME]\n
|
| 20 |
|
|
Where:
|
| 21 |
|
|
insignal=i_name
|
| 22 |
|
|
specifies the name of the signal input to the micro controller
|
| 23 |
|
|
input=I_NAME
|
| 24 |
|
|
specifies the symbol use to read the count
|
| 25 |
|
|
width=<N>
|
| 26 |
|
|
optionally specifies the width of the counter
|
| 27 |
|
|
Note: The default is 8 bits.
|
| 28 |
|
|
Note: If the width is more than 8 bits then the optional outlatch needs
|
| 29 |
|
|
to be provided. This is strobe outport is used to latch the value
|
| 30 |
|
|
of the counter so that it can be input from its LSB to its MSB.\n
|
| 31 |
|
|
Note: The counter is not cleared when it is read. Software must maintain the
|
| 32 |
|
|
previous value of the count if delta-counts are required.\n
|
| 33 |
|
|
Example: Create an 8-bit count for the number of strobe events received.\n
|
| 34 |
|
|
PORTCOMMENT external strobe (input to 8-bit counter)
|
| 35 |
|
|
PERIPHERAL counter \\
|
| 36 |
|
|
insignal=i_strobe \\
|
| 37 |
|
|
inport=I_STROBE_COUNT\n
|
| 38 |
|
|
Read the count:\n
|
| 39 |
|
|
.inport(I_STROBE_COUNT)\n
|
| 40 |
|
|
Example: Create a 12-bit count for the number of strobe events received.\n
|
| 41 |
|
|
PORTCOMMENT external strobe (input to 12-bit counter)
|
| 42 |
|
|
PERIPHERAL counter \\
|
| 43 |
|
|
insignal=i_strobe \\
|
| 44 |
|
|
inport=I_STROBE_COUNT \\
|
| 45 |
|
|
width=12 \\
|
| 46 |
|
|
outlatch=O_LATCH_STROBE_COUNT\n
|
| 47 |
|
|
Read the count:
|
| 48 |
|
|
; latch the count
|
| 49 |
|
|
.outstrobe(O_LATCH_STROBE_COUNT)
|
| 50 |
|
|
; read the count LSB first
|
| 51 |
|
|
; ( - u_LSB u_MSB )
|
| 52 |
|
|
.inport(I_STROBE_COUNT) .inport(I_STROBE_COUNT)
|
| 53 |
|
|
"""
|
| 54 |
|
|
|
| 55 |
|
|
def __init__(self,peripheralFile,config,param_list,loc):
|
| 56 |
|
|
# Use the externally provided file name for the peripheral
|
| 57 |
|
|
self.peripheralFile = peripheralFile;
|
| 58 |
|
|
# Get the parameters.
|
| 59 |
|
|
allowables = (
|
| 60 |
|
|
('outlatch', r'O_\w+$', None, ),
|
| 61 |
|
|
('inport', r'I_\w+$', None, ),
|
| 62 |
|
|
('insignal', r'i_\w+$', None, ),
|
| 63 |
|
|
('width', r'(9|[1-9]\d*)$', int, ),
|
| 64 |
|
|
);
|
| 65 |
|
|
names = [a[0] for a in allowables];
|
| 66 |
|
|
for param_tuple in param_list:
|
| 67 |
|
|
param = param_tuple[0];
|
| 68 |
|
|
if param not in names:
|
| 69 |
|
|
raise SSBCCException('Unrecognized parameter "%s" at %s' % (param,loc,));
|
| 70 |
|
|
param_test = allowables[names.index(param)];
|
| 71 |
|
|
self.AddAttr(config,param,param_tuple[1],param_test[1],loc,param_test[2]);
|
| 72 |
|
|
# Ensure the optional width is set.
|
| 73 |
|
|
if not hasattr(self,'width'):
|
| 74 |
|
|
self.width=8;
|
| 75 |
|
|
# Ensure the required parameters are provided.
|
| 76 |
|
|
required = ['inport','insignal','width'];
|
| 77 |
|
|
if self.width > 8:
|
| 78 |
|
|
required.append('outlatch');
|
| 79 |
|
|
for paramname in required:
|
| 80 |
|
|
if not hasattr(self,paramname):
|
| 81 |
|
|
raise SSBCCException('Required parameter "%s" is missing at %s' % (paramname,loc,));
|
| 82 |
|
|
# There are no optional parameters.
|
| 83 |
|
|
# Add the I/O port, internal signals, and the INPORT and OUTPORT symbols for this peripheral.
|
| 84 |
|
|
config.AddIO(self.insignal,1,'input',loc);
|
| 85 |
|
|
config.AddSignal('s__%s__inport' % self.insignal, self.width, loc);
|
| 86 |
|
|
self.ix_inport = config.NInports();
|
| 87 |
|
|
config.AddInport((self.inport,
|
| 88 |
|
|
('s__%s__inport' % self.insignal, self.width, 'data', ),
|
| 89 |
|
|
),loc);
|
| 90 |
|
|
self.ix_latch = config.NOutports();
|
| 91 |
|
|
if hasattr(self,'outlatch'):
|
| 92 |
|
|
config.AddOutport((self.outlatch,True,
|
| 93 |
|
|
# empty list
|
| 94 |
|
|
),loc);
|
| 95 |
|
|
|
| 96 |
|
|
def GenVerilog(self,fp,config):
|
| 97 |
|
|
if self.width <= 8:
|
| 98 |
|
|
body = """//
|
| 99 |
|
|
// PERIPHERAL counter: @INSIGNAL@
|
| 100 |
|
|
//
|
| 101 |
|
|
always @ (posedge i_clk)
|
| 102 |
|
|
if (i_rst)
|
| 103 |
|
|
@NAME@ <= @WIDTH@'d0;
|
| 104 |
|
|
else if (@INSIGNAL@)
|
| 105 |
|
|
@NAME@ <= @NAME@ + @WIDTH@'d1;
|
| 106 |
|
|
else
|
| 107 |
|
|
@NAME@ <= @NAME@;
|
| 108 |
|
|
""";
|
| 109 |
|
|
else:
|
| 110 |
|
|
body = """//
|
| 111 |
|
|
// PERIPHERAL counter: @INSIGNAL@
|
| 112 |
|
|
//
|
| 113 |
|
|
reg [@WIDTH-1@:0] s__count = @WIDTH@'d0;
|
| 114 |
|
|
always @ (posedge i_clk)
|
| 115 |
|
|
if (i_rst)
|
| 116 |
|
|
s__count <= @WIDTH@'d0;
|
| 117 |
|
|
else if (@INSIGNAL@)
|
| 118 |
|
|
s__count <= s__count + @WIDTH@'d1;
|
| 119 |
|
|
else
|
| 120 |
|
|
s__count <= s__count;
|
| 121 |
|
|
always @ (posedge i_clk)
|
| 122 |
|
|
if (i_rst)
|
| 123 |
|
|
@NAME@ <= @WIDTH@'d0;
|
| 124 |
|
|
else if (s_outport && (s_T == @IX_LATCH@))
|
| 125 |
|
|
@NAME@ <= s__count;
|
| 126 |
|
|
else if (s_inport && (s_T == @IX_INPORT@))
|
| 127 |
|
|
@NAME@ <= { 8'd0, @NAME@[@WIDTH-1:8@] };
|
| 128 |
|
|
else
|
| 129 |
|
|
@NAME@ <= @NAME@;
|
| 130 |
|
|
""";
|
| 131 |
|
|
for subpair in (
|
| 132 |
|
|
(r'\bs__', 's__@NAME@__', ),
|
| 133 |
|
|
(r'@IX_LATCH@', "8'd%d" % self.ix_latch, ),
|
| 134 |
|
|
(r'@IX_INPORT@', "8'd%d" % self.ix_inport, ),
|
| 135 |
|
|
(r'@WIDTH@', str(self.width), ),
|
| 136 |
|
|
(r'@WIDTH-1@', str(self.width-1), ),
|
| 137 |
|
|
(r'@WIDTH-1:8@', '%d:8' % (self.width-1) if self.width > 9 else '8' ),
|
| 138 |
|
|
(r'@NAME@', 's__@INSIGNAL@__inport', ),
|
| 139 |
|
|
(r'@INSIGNAL@', self.insignal, ),
|
| 140 |
|
|
):
|
| 141 |
|
|
body = re.sub(subpair[0],subpair[1],body);
|
| 142 |
|
|
body = self.GenVerilogFinal(config,body);
|
| 143 |
|
|
fp.write(body);
|