44#include < cstdint>
55#include < utility>
66#include < exception>
7+ #include < string_view>
78
89namespace hk {
910
11+ /* * Logic type.
12+ *
13+ * This is a logic-type a smaller version of VHDL's std_logic.
14+ *
15+ */
1016enum class logic : uint8_t {
17+ /* * False */
1118 F,
19+ /* * True */
1220 T,
21+ /* * Error */
1322 X,
23+ /* * Don't Care (third state) */
1424 _,
1525};
1626
17- template <size_t N>
18- [[nodiscard]] constexpr auto make_logic_truth_table (char const *str)
27+ /* * Calculate a truth-table and pack it into a integer.
28+ *
29+ * @param str A set of values represented by '0', '1', 'X', '-'. The left
30+ * most character will places in the least significant 2 bits.
31+ */
32+ [[nodiscard]] constexpr uint32_t make_logic_truth_table (std::string_view str)
1933{
20- using int_type = std::conditional_t <N == 16 , uint32_t , uint8_t >;
21- auto r = int_type{};
34+ auto r = uint32_t {0 };
2235
23- for (auto i = N ; i != 0 ; --i) {
36+ for (auto i = str. size () ; i != 0 ; --i) {
2437 r <<= 2 ;
2538 switch (str[i - 1 ]) {
26- case ' F' :
2739 case ' 0' :
2840 r |= std::to_underlying (logic::F);
2941 break ;
30- case ' T' :
3142 case ' 1' :
3243 r |= std::to_underlying (logic::T);
3344 break ;
3445 case ' X' :
3546 r |= std::to_underlying (logic::X);
3647 break ;
37- case ' _' :
3848 case ' -' :
3949 r |= std::to_underlying (logic::_);
4050 break ;
@@ -45,11 +55,25 @@ template<size_t N>
4555 return r;
4656}
4757
48- struct logic_binary_truth_table {
58+ /* * Create a truth table for logic functions.
59+ *
60+ */
61+ struct logic_truth_table {
4962 uint32_t _table;
5063
51- [[nodiscard]] constexpr logic_binary_truth_table (char const str[17 ]) : _table(make_logic_truth_table<16 >(str)) {}
52-
64+ /* * Create a truth table.
65+ *
66+ * @param str A set of values represented by '0', '1', 'X', '-'. The left
67+ * most character will places in the least significant 2 bits.
68+ */
69+ [[nodiscard]] constexpr logic_truth_table (std::string_view str) : _table(make_logic_truth_table(str)) {}
70+
71+ /* * Extract a value from the table.
72+ *
73+ * @param lhs Index on the minor axis.
74+ * @param rhs Index on the major axis.
75+ * @return The value at the given coordinates
76+ */
5377 [[nodiscard]] constexpr logic operator [](logic lhs, logic rhs) const noexcept
5478 {
5579 auto tmp = _table;
@@ -58,13 +82,12 @@ struct logic_binary_truth_table {
5882 tmp &= 0b11 ;
5983 return static_cast <logic>(tmp);
6084 }
61- };
62-
63- struct logic_unary_truth_table {
64- uint8_t _table;
65-
66- [[nodiscard]] constexpr logic_unary_truth_table (char const str[5 ]) : _table(make_logic_truth_table<4 >(str)) {}
6785
86+ /* * Extract a value from the table.
87+ *
88+ * @param lhs Index in the table.
89+ * @return The value at the given index
90+ */
6891 [[nodiscard]] constexpr logic operator [](logic rhs) const noexcept
6992 {
7093 auto tmp = _table;
@@ -107,7 +130,7 @@ struct logic_unary_truth_table {
107130 */
108131[[nodiscard]] constexpr logic operator |(logic lhs, logic rhs) noexcept
109132{
110- constexpr auto tt = logic_binary_truth_table {" 01X-11X1XXXX-1X-" };
133+ constexpr auto tt = logic_truth_table {" 01X-11X1XXXX-1X-" };
111134 return tt[lhs, rhs];
112135}
113136
@@ -122,7 +145,7 @@ struct logic_unary_truth_table {
122145 */
123146[[nodiscard]] constexpr logic operator &(logic lhs, logic rhs) noexcept
124147{
125- constexpr auto tt = logic_binary_truth_table {" 00X001X-XXXX0-X-" };
148+ constexpr auto tt = logic_truth_table {" 00X001X-XXXX0-X-" };
126149 return tt[lhs, rhs];
127150}
128151
@@ -137,7 +160,7 @@ struct logic_unary_truth_table {
137160 */
138161[[nodiscard]] constexpr logic operator ^(logic lhs, logic rhs) noexcept
139162{
140- constexpr auto tt = logic_binary_truth_table {" 01X-10X-XXXX--X-" };
163+ constexpr auto tt = logic_truth_table {" 01X-10X-XXXX--X-" };
141164 return tt[lhs, rhs];
142165}
143166
@@ -149,7 +172,7 @@ struct logic_unary_truth_table {
149172 */
150173[[nodiscard]] constexpr logic operator ~(logic rhs) noexcept
151174{
152- constexpr auto tt = logic_unary_truth_table {" 10X-" };
175+ constexpr auto tt = logic_truth_table {" 10X-" };
153176 return tt[rhs];
154177}
155178
0 commit comments