bcd_conversion

extras/bcd_conversion.vhdl

Dependencies

sizing

Description

This package provides functions and components for performing conversion between binary and packed Binary Coded Decimal (BCD). The functions to_bcd() and to_binary() can be used to create synthesizable combinational logic for performing a conversion. In synthesized code they are best used with shorter arrays comprising only a few digits. For larger numbers, the components binary_to_bcd and bcd_to_binary can be used to perform a conversion over multiple clock cycles. The utility function decimal_size() can be used to determine the number of decimal digits in a BCD array. Its result must be multiplied by 4 to get the length of a packed BCD array.

Example usage

signal binary  : unsigned(7 downto 0);
constant DSIZE : natural := decimal_size(2**binary'length - 1);
signal bcd : unsigned(DSIZE*4-1 downto 0);
...
bcd <= to_bcd(binary);

Components

binary_to_bcd

component binary_to_bcd is generic ( RESET_ACTIVE_LEVEL : std_ulogic ); port ( --# {{clocks|}} Clock : in std_ulogic; Reset : in std_ulogic; --# {{control|}} Convert : in std_ulogic; Done : out std_ulogic; --# {{data|}} Binary : in unsigned; BCD : out unsigned ); end component;


bcd_conversion.binary_to_bcd

Convert a binary input to BCD encoding. A conversion by asserting Convert. The BCD output is valid when the Done signal goes high.

This component will operate with any size binary array of 4 bits or larger and produces a BCD array whose length is 4 times the value returned by the decimal_size() function. The conversion of an n-bit binary number will take n cycles to complete.

Generics:
  • RESET_ACTIVE_LEVEL (std_ulogic) – Asynch. reset control level
Port:
  • Clock (in std_ulogic) – System clock
  • Reset (in std_ulogic) – Asynchronous reset
  • Convert (in std_ulogic) – Start conversion when high
  • Done (out std_ulogic) – Indicates completed conversion
  • Binary (in unsigned) – Binary data to convert
  • BCD (out unsigned) – Converted output. Retained until next conversion

bcd_to_binary

component bcd_to_binary is generic ( RESET_ACTIVE_LEVEL : std_ulogic ); port ( --# {{clocks|}} Clock : in std_ulogic; Reset : in std_ulogic; --# {{control|}} Convert : in std_ulogic; Done : out std_ulogic; --# {{data|}} BCD : in unsigned; Binary : out unsigned ); end component;


bcd_conversion.bcd_to_binary

Convert a BCD encoded input to binary. A conversion by asserting Convert. The Binary output is valid when the Done signal goes high.

The length of the input must be a multiple of four. The binary array produced will be large enough to hold the maximum decimal value of the BCD input. Its length will be bit_size(10**(Bcd'length/4) - 1). The conversion of a BCD number to an n-bit binary number will take n+3 cycles to complete.

Generics:
  • RESET_ACTIVE_LEVEL (std_ulogic) – Asynch. reset control level
Port:
  • Clock (in std_ulogic) – System clock
  • Reset (in std_ulogic) – Asynchronous reset
  • Convert (in std_ulogic) – Start conversion when high
  • Done (out std_ulogic) – Indicates completed conversion
  • BCD (in unsigned) – BCD data to convert
  • Binary (out unsigned) – Converted output. Retained until next conversion

Subprograms

bcd_conversion.decimal_size (n : natural) → natural
Calculate the number of decimal digits needed to represent a number n.
Parameters:
  • n (natural) – Value to calculate digits for
Returns:

Decimal digits for n.

bcd_conversion.to_bcd (Binary : unsigned) → unsigned
Convert binary number to BCD encoding This uses the double-dabble algorithm to perform the BCD conversion. It will operate with any size binary array and return a BCD array whose length is 4 times the value returned by the decimal_size function.
Parameters:
  • Binary (unsigned) – Binary encoded value
Returns:

BCD encoded result.

bcd_conversion.to_binary (Bcd : unsigned) → unsigned
Convert a BCD number to binary encoding This uses the double-dabble algorithm in reverse. The length of the input must be a multiple of four. The returned binary array will be large enough to hold the maximum decimal value of the BCD input. Its length will be bit_size(10**(Bcd’length/4) - 1).
Parameters:
  • Bcd (unsigned) – BCD encoded value
Returns:

Binary encoded result.