hdlypes

VHDL Types for C++ and Python.

class hdltypes::Bit

Bit value type

Implementation of a bit value. Supports the values 0 and 1. Values of this type are immutable. Supports (de)serialization, comparison, and logic operations: “and”, “or”, “xor”, and inversion.

Public Types

enum value_type

Representation of acceptable values of a Bit.

Naming and ordering are public information and must be controlled.

Values:

enumerator _0

0

enumerator _1

1

Public Functions

constexpr Bit() noexcept = default

Default Bit to 0.

explicit constexpr Bit(value_type value) noexcept

Create a Bit given a value_type value.

Note that this function is noexcept, there is no checking on passed values. This is justified because it is difficult to break, except on purpose; while allowing us additional performance.

constexpr value_type value() const noexcept

Obtain the value_type value.

explicit constexpr Bit(Logic a)

Allows a Logic to be converted to a Bit. Fails if the Logic is not 0 or 1.

constexpr operator Logic() const noexcept

Allows a Bit to be converted implicitly to a Logic.

constexpr Bit operator""_b(char c)

UDL convience syntax for Bit::deserialize.

constexpr Bit to_bit(bool b) noexcept

Converts bool values false and true to Bit 0 and 1, respectively.

template<typename IntType, typename std::enable_if<is_integer_type<IntType>::value, int>::type = 0>
constexpr Bit to_bit(const IntType &i)

Converts integer values 0 and 1 into Bit 0 and 1, respectively.

template<typename CharType, typename std::enable_if<is_char_type<CharType>::value, int>::type = 0>
constexpr Bit to_bit(const CharType &c)

Converts character values into Logic. See table below for more details.

'0'      => _0
'1'      => _1

constexpr Bit to_bit(Logic a)

Converts the Logic values 0 and 1 to Bit 0 and 1, respectively.

constexpr Bit to_bit(Bit a) noexcept

Returns the given Bit.

constexpr bool operator==(Bit a, Bit b) noexcept

Value equality.

constexpr bool operator!=(Bit a, Bit b) noexcept

Value inequality.

constexpr Bit operator&(Bit a, Bit b) noexcept

Logical “and” operation. Returns 1 if both arguments are 1.

constexpr Bit &operator&=(Bit &a, Bit b) noexcept

In-place version of the logical “and” operation.

constexpr Bit operator|(Bit a, Bit b) noexcept

Logical “or” operation. Returns 1 if either arguments are 1.

constexpr Bit &operator|=(Bit &a, Bit b) noexcept

In-place version of the logical “or” operation.

constexpr Bit operator^(Bit a, Bit b) noexcept

Logical “xor” operation. Returns 1 if arguments aren’t equivalent.

constexpr Bit &operator^=(Bit &a, Bit b) noexcept

In-place version of the logical “xor” operation.

constexpr Bit operator~(Bit a) noexcept

Logical inversion operation. Returns 1 if given 0, and vice versa.

constexpr Bit &inplace_invert(Bit &a) noexcept

In-place version of the logical “invert” operation.

constexpr bool is01(Bit a) noexcept

Returns true.

template<typename IntType = int>
constexpr IntType to_int(Bit a) noexcept

Converts a Bit 0 or 1 to the integer 0 or 1, respectively.

constexpr bool to_bool(Bit a) noexcept

Converts a Bit 0 or 1 to the boolean false or true, respectively.

template<typename CharType = char>
constexpr CharType to_char(const Bit a) noexcept

Converts a Bit 0 or 1 to the characters '0' and '1', respectively.

class hdltypes::Logic

Logic value type

This effectively models VHDL’s std_ulogic type. See value_type for details on the values of this type.

This type supports logical operations “and”, “or”, “xor”, and inversion. Can be converted “from” and “to” int or bool representations of Logic values. Supports serialization and deserialization as well.

Like VHDL: weak value when operated on become strong (due to implicit buffering) operations on non-0/1 values yield “unknowns” operations involving unassigned result in unassigned results

Public Types

enum value_type

Representation of acceptable values of a Logic.

Naming and ordering are public information and must be controlled.

Values:

enumerator U

Unassigned. Represents the container holding a value of this type did not take on an actual value.

enumerator X

Unknown. Typically a result of doing operations on non-0/1 values.

enumerator _0

0

enumerator _1

1

enumerator Z

High Impedance. Useful for modeling pull-ups and pull-downs.

enumerator W

Weak Unknown. Like X, but the value is weak.

enumerator L

Weak 0. Like 0, but the value is weak.

enumerator H

Weak 1. Like 1, but the value is weak.

enumerator DC

Don’t Care. Like X, but explicit rather than accidental.

Public Functions

constexpr Logic() noexcept = default

Default Logic to U.

explicit constexpr Logic(value_type value) noexcept

Create a Logic given a value_type value.

Note that this function is noexcept, there is no checking on passed values. This is justified because it is difficult to break, except on purpose; while allowing us additional performance.

constexpr value_type value() const noexcept

Obtain the value_type value.

constexpr Logic operator""_l(char c)

UDL convenience syntax for Logic::deserialize.

constexpr Logic to_logic(bool b) noexcept

Converts bool values false and true to Logic 0 and 1, respectively.

template<typename IntType, typename std::enable_if<is_integer_type<IntType>::value, int>::type = 0>
constexpr Logic to_logic(const IntType &i)

Converts integer values 0 and 1 into Logic 0 and 1, respectively.

template<typename CharType, typename std::enable_if<is_char_type<CharType>::value, int>::type = 0>
constexpr Logic to_logic(const CharType &c)

Converts character values into Logic. See table below for more details.

'U'  'u'    => U
'X'  'x'    => X
'0'         => _0
'1'         => _1
'Z'  'z'    => Z
'W'  'w'    => W
'L'  'l'    => L
'H'  'h'    => H
'-'         => DC

constexpr Logic to_logic(Logic a) noexcept

Returns the given Logic.

constexpr bool operator==(Logic a, Logic b) noexcept

Value equality.

constexpr bool operator!=(Logic a, Logic b) noexcept

Value inequality.

constexpr Logic operator&(Logic a, Logic b) noexcept

Logical “and” operation. See implementation for details.

constexpr Logic &operator&=(Logic &a, Logic b) noexcept

Inplace version of the logical “and” operation.

constexpr Logic operator|(Logic a, Logic b) noexcept

Logical “or” operation. See implementation for details.

constexpr Logic &operator|=(Logic &a, Logic b) noexcept

Inplace version of the logical “or” operation.

constexpr Logic operator^(Logic a, Logic b) noexcept

Logical “xor” operation. See implementation for details.

constexpr Logic &operator^=(Logic &a, Logic b) noexcept

Inplace version of the logical “xor” operation.

constexpr Logic operator~(Logic a) noexcept

Logical inversion operation. See implementation for details.

constexpr Logic &inplace_invert(Logic &a) noexcept

Inplace version of the logical “invert” operation.

constexpr bool is01(Logic a) noexcept

Returns true if the value is 0 or 1.

template<typename IntType = int>
constexpr IntType to_int(Logic a)

Converts a Logic 0/L or 1/H to the integer 0 or 1, respectively.

constexpr bool to_bool(Logic a)

Converts a Logic 0/L or 1/H to the integer false or true, respectively.

template<typename CharType = char>
constexpr CharType to_char(Logic a) noexcept

Converts a Logic into a character. See the below details for the mapping.

U       => 'U'
X       => 'X'
_0      => '0'
_1      => '1'
Z       => 'Z'
W       => 'W'
L       => 'L'
H       => 'H'
DC      => '-'

constexpr Logic to_logic(Bit a) noexcept

converts the Bit values 0 and 1 to Logic 0 and 1, respectively.