Contenido principal

HDL Counter

R2026b

Free-running, count-limited, or modulo hardware counter

  • HDL Counter block

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

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

expand all

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

expand all

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

expand all

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

expand all

Extended Capabilities

expand all

C/C++ Code Generation
Generate C and C++ code using Simulink® Coder™.

Version History

Introduced in R2014a

expand all