Free-running or count-limited hardware counter
HDL Coder / Sources
The HDL Counter block models a free-running, count-limited, or modulo
hardware counter that supports signed and unsigned integer and fixed-point data types.
The counter emits its value for the current sample time. During simulation, this block
does not report warnings or errors due to wrap on overflow. To report these warnings,
see Simulink.restoreDiagnostic
.
By default, the counter does not have input ports. The counter counts up from an initial value to a threshold value based on the Counter type, the Count to value, and the Word length. The output data type of the counter depends on the Counter output data, Word length, and Fraction length.
rst
— Local reset portLocal reset port for the counter that when high resets the count value.
To enable this port, set Local reset port.
Data Types: Boolean
load
— Load portLoad port that when high sets the counter to the load value,
load_val
.
To enable this port, set Load ports.
Data Types: Boolean
load_val
— Load port valueData value to load for setting the count value when high input is given to the load port.
To enable this port, set Load ports.
Data Types: Boolean
enb
— Count enable portEnable signal that specifies whether the counter should count from the previous value. When this signal is high, the counter counts continues up or down depending on the direction. When this signal is low, the counter holds the previous value.
To enable this port, set Count enable port.
Data Types: Boolean
dir
— Count direction portCount direction that specifies whether to count up or count down. This port interacts with Step value to determine count direction.
1
: This value is the default that
results in an up counter. The Step
value is added to the current counter value
to compute the next value.
0
: This value results in a down
counter. The Step value is subtracted
from the current counter value to compute the next
value.
To enable this port, set Count direction port.
Data Types: Boolean
count
— Counter valueThis is the counter value. By default, if you do not enable the input ports, the counter counts up to a value that is determined based on the Counter type, the Count to value, and the Word length.
Data Types: int8
| int16
| int32
| int64
| uint8
| uint16
| uint32
| uint64
| fixed point
Counter type
— Counter behaviorFree running
(default) | Count limited
| Modulo
Counter behavior that determines whether to model a free running, count-limited, or module hardware counter.
Free running
(default): The counter
continues to increment or decrement by the Step
value until reset.
Count limited
: The counter
increments or decrements by the Step value
until it is exactly equal to the Count to
value. If the Step value
value is such that the count value does not exactly equal the
Count to value, then it can continue
counting to a threshold value that is determined by the word
length.
Modulo
: The counter increments or
decrements by the Step value until it
reaches the Count to value. If the
Step value value is such that the count
value does not exactly equal the Count to
value, then the counter wraps to a value that is
determined by the wrapping step value.
Block parameter:
CountType |
Type: character vector |
Value:
'Free running' | 'Count
limited' | 'Modulo' |
Default:
'Free running' |
Initial value
— Counter value after reset0
(default)The value to which the counter resets. The default value is
0
.
Block parameter:
CountInit |
Type: character vector |
Value: An integer greater than or equal to zero |
Default:
'0' |
Step value
— Step value for count1
(default)Value added to counter at each sample time. The default value is
1
.
Block parameter:
CountStep |
Type: character vector |
Value: An integer greater than or equal to zero |
Default:
'1' |
Count to value
— Threshold count value25
(default)When you use a Count limited
counter, if the
count is exactly equal to Count to value, the count
restarts at the Initial value. If the count value
exceeds the Count to value, the counter continues
counting to a threshold value that depends on the Word
length. The default is 25
.
When you use a Modulo
counter, if the count reaches the
Count to value, the count restarts at a value that
is determined by the wrapping step value.
To enable this parameter, set Counter type to
Count limited
or
Modulo
.
Block parameter:
CountMax |
Type: character vector |
Value: An integer greater than or equal to zero |
Default:
'25' |
Count from
— Criteria for count from valueInitial value
(default) | Specify
Specifies the parameter that sets the start value after rollover when you
use a Count limited
or Free
running
counter. When you use a
Modulo
counter, the counter can rollover to a
wrapping step value that is different from the value to count from. When set
to Specify
, the Count from
value parameter is the start value after rollover. The
default is Initial value
.
Block parameter:
CountFromType |
Type: character vector |
Value:
'Initial value' |
'Specify' |
Default:
'Initial value' |
Count from value
— Initial value to count from0
(default)Counter value after rollover when Count from is set
to Specify
. The default is 0.
Block parameter:
CountFrom |
Type: character vector |
Value:
'Initial value' |
'Specify' |
Default:
'Initial value' |
Local reset port
— Local port to reset counteroff
(default) | on
When selected, creates a local reset port, rst
.
Block parameter:
CountResetPort |
Type: character vector |
Value:
'off' | 'on' |
Default:
'off' |
Load ports
— Port for load valueoff
(default) | on
When selected, creates a load data port, load_val
, and
load trigger port, load
.
Block parameter:
CountLoadPort |
Type: character vector |
Value:
'off' | 'on' |
Default:
'off' |
Count enable port
— Port to enable countingoff
(default) | on
When selected, creates a count enable port, enb
.
Block parameter:
CountEnbPort |
Type: character vector |
Value:
'off' | 'on' |
Default:
'off' |
Count direction port
— Port for count directionoff
(default) | on
When selected, creates a count direction port,
dir
.
Block parameter:
CountDirPort |
Type: character vector |
Value:
'off' | 'on' |
Default:
'off' |
Counter output data is
— Output data type signednessUnsigned
(default) | Signed
Output data type signedness. The default is
Unsigned
.
Block parameter:
CountDataType |
Type: character vector |
Value:
'Unsigned' | 'Signed' |
Default:
'off' |
Word length
— Counter word length8
(default)Bit width, including sign bit, for an integer counter; word length for a
fixed-point data type counter. The minimum value if Output data
type is Unsigned
is
1
, 2
if
Signed
. The maximum value is
125
. The default is 8
.
Block parameter:
CountWordLen |
Type: character vector |
Value: An integer greater than or equal to one |
Default:
'8' |
Fraction length
— Counter fraction length0
(default)Fixed-point data type fraction length. The default is
0
.
Block parameter:
CountFracLen |
Type: character vector |
Value: An integer greater than or equal to zero |
Default:
'0' |
Sample time
— Counter sample time1
(default)Sample time. The default is 1
. This parameter is not
available, and the block inherits its sample time from the input ports when
any of these parameters is selected:
Local reset port
Load ports
Count enable port
Count direction port
Block parameter:
CountSampTime |
Type: character vector |
Value: An integer greater than or equal to one |
Default:
'1' |
By default, when you do not enable the control ports, the counter counts upwards
from zero in free-running mode. In this mode, the counter increments in steps of one
at each sample time unit until it reaches the threshold value. The count threshold
value is calculated as 2^(Word length) - 1
. When it reaches the
count value, the counter resets to the initial value.
The counter behavior depends on control ports that you specify. This table shows the priority of the control signals and how the counter value is updated in relation to the control signals.
Local reset, rst | Load trigger, load | Count enable, enb | Count direction, dir | Next Counter Value |
---|---|---|---|---|
1 | – | – | – | Initial value |
0 | 1 | – | – | load_val value |
0 | 0 | 0 | – | Current value |
0 | 0 | 1 | 1 | Current value + step value |
0 | 0 | 1 | 0 | Current value - step value |
The Step value parameter and optional count direction port,
dir
, interact to determine the actual count
direction.
dir Signal Value | Step Value Sign | Actual Count Direction |
---|---|---|
1 | + (positive) | Up |
1 | - (negative) | Down |
0 | + (positive) | Down |
0 | - (negative) | Up |
This figure illustrates the free running mode of operation. In this example, the
counter has a Word length of 4
. The
Initial value is 2
, load value is
7
, and the Step value is
1
. When the Enb signal is high, the
counter increments by steps of one. When Rst signal becomes
high, the counter resets to the initial value, 2
. When the
Dir port goes low, the counter decrements from
4
to 3
at time step
11s
. The count value is tied to the load value when the
Load port becomes high.
You can use the Counter type parameter to specify the counter
behavior. The Count limited
mode of the counter wraps the
count to the initial value when the counter exactly reaches the Count to
value. If the counter does not exactly reach the Count to
value, it can exceed this value. For an up counter, the count value
can reach a threshold value that is calculated as 2^(Word length) -
1
. For a down counter, the count value can reach the Initial
value. When it reaches or exactly matches this threshold value, the
counter resets to a value that is determined by the wrapping step value.
The Modulo
mode of the counter wraps the count when it
reaches or exactly matches the Count to value. Instead of
restarting at the initial value, the counter wraps back to a value that is
determined by a wrapping step value. For an up counter, the wrapping step value is
calculated as step value - (count to value + 1) + count from
value. For a down counter, the wrapping step value is calculated as
count from value - step value + (count to value +
1).
This figure illustrates the Count limited
and
Modulo
modes of operation. In this example, the
counter has a Word length of 4
,
Initial value of 2
, Step
value of 3
, and Count to
value of 12
.
In the Count limited
mode, as the count value reaches
11
, it exceeds the Count to value and
reaches 14
. As the threshold value is 15
(2^4-1)
, the counter resets to a value that is determined by the
wrapping step value. When counting down, the counter exceeds the Initial
value and can reach zero. It then resets to a value that is
determined by the wrapping step value.
In the Modulo
mode, as the count value reaches
11
, the counter resets to a value that is determined by the
wrapping step value. The wrapping step value is 3 - (12 + 1) + 2 =
-8
. Therefore, the counter resets to the value 3
(11 + (-8)
. When counting down, as the count value reaches
4
, the counter resets to a value that is determined by the
wrapping step value. The wrapping step value is 2 - 3 + (12 + 1) =
12
. Therefore, the counter resets to the value
12
.
HDL Coder™ provides additional configuration options that affect HDL implementation and synthesized logic.
This block has a single, default HDL architecture.
General | |
---|---|
ConstrainedOutputPipeline | Number of registers to place at
the outputs by moving existing delays within your design. Distributed
pipelining does not redistribute these registers. The default is
|
InputPipeline | Number of input pipeline stages
to insert in the generated code. Distributed pipelining and constrained
output pipelining can move these registers. The default is
|
OutputPipeline | Number of output pipeline stages
to insert in the generated code. Distributed pipelining and constrained
output pipelining can move these registers. The default is
|
If the bitwidth of the input signal to a HDL Counter exceeds the data type limit, the generated HDL code can produce incorrect simulation results. To accommodate the larger bit width, use a larger data type.
The block does not support vectors. Only scalar types are supported for block inputs and outputs.
Load value used in Modulo
counter mode must
be within the range of the Count from and
Count to value to avoid simulation
mismatches.