HDL Counter
R2026bFree-running, count-limited, or modulo hardware counter
HDL Counter block

To add a block to a model, double-click the canvas and start typing the block name. Then, select the block from the list.
Libraries:
HDL Coder /
Sources
Description
The HDL Counter block models a free-running, count-limited, or modulo hardware counter. The block supports signed and unsigned integer and fixed-point data types and outputs the counter value at the current sample time.
You can set Counter type to one of these values:
Free running— The counter increments or decrements by the value of the Step value parameter until it wraps at the representable limit of the configured output data type.Count limited— The counter rolls over only when the counter value lands exactly on the value of the Count to value parameter. If the step value does not equal to the value of the Count to value parameter, the counter continues in the current direction until it reaches the representable limit of the configured output data type.Modulo— The counter wraps at the modulo boundary. If the value of the Step value parameter crosses the boundary, the block applies the remaining step distance after wrapping.
Use this block to create HDL-compatible counter logic in a Simulink model. You can configure the block to count up or down, reset or load the counter value, enable or disable counting, and detect when the counter reaches a configured limit.
You can input reset, load, enable, and direction data by enabling the optional rst, load, enb, and dir ports.
During simulation, the block does not report warnings or errors that result from wrap
on overflow. To report these warnings, see Simulink.restoreDiagnostic.
Examples
Simple Counter Model
This model counts up from zero to a threshold value of 15, then wraps back to zero. You can change the threshold value by changing the value of the Constant block connected to the count_threshold port. The Enable port specifies whether the counter counts upward or holds the previous value. You can change the counter action by changing the value of the Constant block connected to the Enable port. A value of 1 indicates that the counter counts upward. A value of 0 holds the previous count value.
Limitations
If the bit width of an input signal 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 supports only scalar inputs and outputs.
When Counter type is
Modulo, the load value must be in the range defined by the Count from and Count to value parameters to avoid simulation mismatches.
Ports
Input
Counter reset signal, specified as a scalar. When this signal is 1
(true), the block resets the count to the value of the
Initial value parameter.
Dependencies
To enable this port, select the Local reset port parameter.
Data Types: Boolean
Counter load trigger, specified as a scalar. When this signal is
1 (true), the block loads the value from the
load_val port.
Dependencies
To enable this port, select the Load ports parameter.
Data Types: Boolean
Counter load value, specified as a scalar. The block loads this value
into the counter when the load signal is 1
(true).
Dependencies
To enable this port, select the Load ports parameter.
Data Types: int8 | int16 | int32 | int64 | uint8 | uint16 | uint32 | uint64 | fixed point
Count enable signal, specified as a scalar. When this signal is
1 (true), the block updates the counter value.
When this signal is 0 (false), the block holds the
previous counter value.
Dependencies
To enable this port, select the Count enable port parameter.
Data Types: Boolean
Count direction signal, specified as a scalar. Use this port to specify whether to add to or subtract from the next counter value:
When dir is
1, the block adds the value of the Step value parameter to the current counter value.When dir is
0, the block subtracts the value of the Step value parameter from the current counter value.
Dependencies
To enable this port, clear the Count hit output port parameter and select the Count direction port parameter.
Data Types: Boolean
Output
Counter value at the current sample time, returned as a scalar.
Data Types: int8 | int16 | int32 | int64 | uint8 | uint16 | uint32 | uint64 | fixed point
Counter limit status, returned as a scalar. A value of
1 indicates that the counter reached the
configured limit. A value of 0 indicates that the
counter did not reach the configured limit.
When the rst or load signal is 1
(true), the block returns 0 from this
port, regardless of the counter value.
Dependencies
To enable this port, clear the Count direction port parameter and select the Count hit output port parameter.
Data Types: Boolean
Parameters
Behavior of the counter. Select one of these options:
Free running— The counter increments or decrements by the value of the Step value parameter until it wraps at the representable limit of the configured output data type.Count limited— The counter increments or decrements by the value of the Step value parameter until the counter value is equal to the value of the Count to value parameter. If the value of the Step value parameter causes the counter to skip the value of the Count to value parameter, the counter continues counting in the current direction until it reaches the representable limit of the configured output data type.Modulo— The counter increments or decrements by the value of the Step value parameter and wraps at the modulo boundary by using the wrapping step value.
Programmatic Use
Block parameter:
CountType |
| Type: character vector | string scalar |
Value:
"Free running" | "Count
limited" | "Modulo" |
Default:
"Free running" |
Value to reset the counter. This value must be an integer in the representable range of
the output data type. For an unsigned output data type, the range is
[0, 2^(Word length) - 1]. For a signed output data
type, the range is [-2^(Word length - 1), 2^(Word length - 1) -
1].
Dependencies
When Counter type is set to Count
limited or Modulo, the
value of the Initial value parameter must be
between the values of the Count from value and
Count to value parameters.
Programmatic Use
Block parameter:
CountInit |
| Type: character vector | string scalar |
| Value: integer in the representable range of the output data type, in quotes |
Default:
"0" |
Value that the block adds to or subtracts from the current counter value at each sample time.
When you enable the dir input port, the block uses the value of the Step value parameter and the dir port value to determine the count direction.
Programmatic Use
Block parameter:
CountStep |
| Type: character vector | string scalar |
| Value: integer in quotes |
Default:
"1" |
Threshold of the counter. This value must be an integer in the representable range of the
output data type. The block uses this value to determine rollover behavior
when the Counter type parameter is Count
limited or Modulo.
When Counter type is set to
Count limited, the counter rolls over if the counter value is equal to the value of the Count to value parameter. If the value of the Step value parameter causes the counter to skip the value of the Count to value parameter, the counter continues counting until it reaches the representable limit of the configured output data type.When Counter type is
Modulo, the counter wraps at the modulo boundary and applies the wrapping step value.
Dependencies
To enable this parameter, set Counter type to
Count limited or
Modulo.
Programmatic Use
Block parameter:
CountMax |
| Type: character vector | string scalar |
| Value: integer in the representable range of the output data type, in quotes |
Default:
"25" |
Source of rollover start value. Use this parameter to specify whether the block uses the value of the Initial value or Count from value parameter as the rollover start value.
Initial value— The block uses the value of the Initial value parameter as the rollover start value.Specify— The block uses the value of the Count from value parameter as the rollover start value.
When you set Counter type to Modulo, the
block can roll over to a wrapping step value that differs from the rollover
start value.
Programmatic Use
Block parameter:
CountFromType |
| Type: character vector | string scalar |
Value:
"Initial value" |
"Specify" |
Default:
"Initial value" |
Rollover start value. This value must be an integer in the representable range of the
output data type. The block uses this value when you set Count
from parameter to Specify.
Dependencies
To enable this parameter, set Count from to
Specify.
Programmatic Use
Block parameter:
CountFrom |
| Type: character vector | string scalar |
| Value: integer in the representable range of the output data type, in quotes |
Default:
"0" |
Select this parameter to enable the rst input port.
Programmatic Use
Block parameter:
CountResetPort |
| Type: character vector | string scalar |
Value:
"off" | "on" |
Default:
"off" |
Select this parameter to enable the load and load_val input ports.
Programmatic Use
Block parameter:
CountLoadPort |
| Type: character vector | string scalar |
Value:
"off" | "on" |
Default:
"off" |
Select this parameter to enable the enb input port.
Programmatic Use
Block parameter:
CountEnbPort |
| Type: character vector | string scalar |
Value:
"off" | "on" |
Default:
"off" |
Select this parameter to enable the dir input port.
Dependencies
To enable this parameter, clear the Count hit output port parameter.
Programmatic Use
Block parameter:
CountDirPort |
| Type: character vector | string scalar |
Value:
"off" | "on" |
Default:
"off" |
Select this parameter to enable the Count hit output port.
Dependencies
To enable this parameter, clear the Count direction port parameter.
Programmatic Use
Block parameter:
CountHitOutputPort |
| Type: character vector | string scalar |
Value:
"off" | "on" |
Default:
"off" |
Signedness of the output data.
Programmatic Use
Block parameter:
CountDataType |
| Type: character vector | string scalar |
Value:
"Unsigned" | "Signed" |
Default:
"Unsigned" |
Counter word length. For an unsigned output data type, the minimum word length is
1. For a signed output data type, the minimum word
length is 2. The maximum word length is
125.
Programmatic Use
Block parameter:
CountWordLen |
| Type: character vector | string scalar |
| Value: integer in quotes |
Default:
"8" |
Counter fraction length. The value must be an integer greater than or equal to zero.
Dependencies
When Counter type is set to Modulo, the
Fraction length must be
0.
Programmatic Use
Block parameter:
CountFracLen |
| Type: character vector | string scalar |
| Value: integer greater than or equal to zero in quotes |
Default:
"0" |
Counter sample time.
Dependencies
To enable this parameter, clear these parameters:
Local reset port
Load ports
Count enable port
Count direction port
When you select any of these parameters, the block inherits the sample time from the input ports.
Programmatic Use
Block parameter:
CountSampTime |
| Type: character vector | string scalar |
| Value: integer greater than or equal to one in quotes |
Default:
"1" |
Algorithms
At each sample time, the block applies the enabled control signals before it applies the counter behavior specified by the Counter type parameter. The block applies the control signals in this order:
The local reset value from the rst port.
The load trigger value from the load port.
The count enable value from the enb port.
The count direction value from the dir port.
If the value at the rst port is 1, the block resets
the counter to the value of the Initial value parameter. If the
value at the load port is 1, the block loads
the value from the load_val port. If the value at the
enb port is 0, the block holds the
current counter value. If the value at the enb port is
1, the block computes the next count value based on the value
of the Step value parameter, the dir port
value, and the Counter type parameter.
The counter behavior depends on control ports that you enable. This table shows the priority of the control signals and how the counter value updates in response to those signals.
| Local Reset, rst | Load Trigger, load | Count Enable, enb | Count Direction, dir | Next Counter Value |
|---|---|---|---|---|
1 | – | – | – | The value of the Initial value parameter |
0 | 1 | – | – | The value of the load_val port |
0 | 0 | 0 | – | The current counter value |
0 | 0 | 1 | 1 | The current counter value + the value of the Step value parameter |
0 | 0 | 1 | 0 | The current counter value - the value of the Step value parameter |
When you enable the dir input port, the block determines the count direction by using the value of the Step value parameter and the value at the dir port.
| Count Direction, dir | Step Value Sign | Counter Count Direction |
|---|---|---|
| 1 | Positive | Up |
| 1 | Negative | Down |
| 0 | Positive | Down |
| 0 | Negative | Up |
When you set Counter type to Free running,
the counter increments or decrements by the value of the Step
value parameter until the value wraps at the representable limit of
the configured output data type. For an unsigned counter that counts up, the upper
limit is 2^(Word length) - 1.
This figure illustrates free-running counter operation. In this example, Word
length is 4, Initial value is
2, load_val is 7, and
Step value is 1. When
Enb is 1, the counter increments by one.
When rst is 1, the counter resets to the
initial value, 2. When dir is
0, the counter decrements from 4 to
3 at time step 11s. When
Load is 1, the counter loads the value
from the load_val port.

When Counter type is Count limited, the
counter rolls over when the count value is equal to the value of the Count
to value parameter. If the value of the Step
value parameter causes the counter to skip the value of the
Count to value parameter, the counter continues counting in
the current direction until it reaches the representable limit of the configured
output data type. To make the counter roll over at the value of the Count
to value parameter, configure the Initial value,
Step value, and Count to value
parameters so that the counter lands exactly on the value of the Count to
value parameter.
This behavior can be counterintuitive for down counters when Counter
type is Count limited and the configured
settings cause the counter to skip the value of the Count to
value parameter. For example, if the counter starts at
0, counts down, and Count to value is
3, the counter can alternate between 0 and
3 because it wraps through the representable range and lands
on the value of the Count to value parameter.
When Counter type is Modulo, the counter
wraps at the modulo boundary. When the value of the Step value
parameter causes the counter to cross the boundary, the block applies a wrapping
step value so that the counter preserves the remaining step distance after the
boundary crossing.
For an up counter, the wrapping step value is:
Step value - (Count to value + 1) + Count from value.
For a down counter, the wrapping step value is:
Count from value - Step value + (Count to value + 1).
Use the Count from parameter to specify the rollover start value. When
you set Count from to Initial value,
the counter uses Initial value as the rollover reference.
When you set Count from to Specify, the
counter uses the value of the Count from value parameter as the
rollover reference.
This table summarizes how the counter behaves when the count sequence does not land exactly on the value of the Count to value parameter.
| Counter Type Setting | Behavior When Step Skips Count to value | Rollover Behavior |
|---|---|---|
Count limited | The counter continues counting in the current direction until it reaches the representable data type limit. | The counter rolls over according to the configured count settings. |
Modulo | The counter wraps at the modulo boundary. | The counter applies the remaining step distance after wrapping. |
This figure illustrates how count-limited and modulo counters operate.
In this example, Word length is 4,
Initial value is 2, Step
value is 3, and Count to
value is 12.

When Counter type is Count limited, and the
count value reaches 11, the next count value is
14 because the counter does not land exactly on the value of
the Count to value parameter, 12. Because
the upper representable limit is 15, (2^4 -
1), the counter continues until it reaches that limit and then rolls over
according to the configured count settings.
When Counter type is set to Modulo and the
count value reaches 11, the counter wraps by applying the
remaining step distance after the modulo boundary crossing:
3 - (12 + 1) + 2 = -8
The next count value is:
11 + (-8) = 3
When the counter counts down and reaches 4, the counter wraps
up by using this wrapping step value:
2 - 3 + (12 + 1) = 12
The next count value is 12.
To avoid unexpected rollover behavior when Counter type is
Count limited, configure the Initial
value, Step value, and Count to
value parameters so that the counter lands exactly on the value of
the Count to value parameter. To wrap when the step crosses a
boundary, set Counter type to
Modulo.
Extended Capabilities
C/C++ Code Generation
Generate C and C++ code using Simulink® Coder™.
HDL Coder™ provides additional configuration options that affect HDL implementation and synthesized logic.
This block has one default HDL architecture.
| General | |
|---|---|
| ConstrainedOutputPipeline | Number of registers to place at
the outputs by moving existing delays in the design. Distributed pipelining
does not redistribute these registers. The default value 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 value 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 value is
|
| SynthesisAttributes |
Specifies the synthesis attributes for the blocks and block output signals in the model. The generated HDL code contains these attributes. For more information, see SynthesisAttributes. |
Version History
Introduced in R2014aUse the SynthesisAttributes HDL block property to specify the synthesis attributes for the block and its output signals. HDL Coder includes these attributes in the generated HDL code.
MATLAB Command
You clicked a link that corresponds to this MATLAB command:
Run the command by entering it in the MATLAB Command Window. Web browsers do not support MATLAB commands.
Seleccione un país/idioma
Seleccione un país/idioma para obtener contenido traducido, si está disponible, y ver eventos y ofertas de productos y servicios locales. Según su ubicación geográfica, recomendamos que seleccione: .
También puede seleccionar uno de estos países/idiomas:
Cómo obtener el mejor rendimiento
Seleccione China (en idioma chino o inglés) para obtener el mejor rendimiento. Los sitios web de otros países no están optimizados para ser accedidos desde su ubicación geográfica.
América
- América Latina (Español)
- Canada (English)
- United States (English)
Europa
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)
