scc 2025.09
SystemC components library
sc_logic_7.h
1/*******************************************************************************
2 * Copyright 2016, 2018 MINRES Technologies GmbH
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 *******************************************************************************/
16
17#ifndef INCL_SYSC_CORE_SC_LOGIC_7_H_
18#define INCL_SYSC_CORE_SC_LOGIC_7_H_
19#include <array>
20#include <cstdio>
21
22#include "sysc/datatypes/bit/sc_bit.h"
23#include "sysc/kernel/sc_macros.h"
24#include "sysc/utils/sc_mempool.h"
25
31namespace scc {
32namespace dt {
33
34// classes defined in this module
35class sc_logic_7;
36
37// ----------------------------------------------------------------------------
38// ENUM : sc_logic_7_value_t
39//
40// Enumeration of values for sc_logic_7.
41// ----------------------------------------------------------------------------
42
43enum sc_logic_7_value_t { Log_0 = 0, Log_1, Log_L, Log_H, Log_Z, Log_X, Log_U };
44
45// ----------------------------------------------------------------------------
46// CLASS : sc_logic_7
47//
48// Four-valued logic type.
49// ----------------------------------------------------------------------------
50
51class sc_logic_7 {
52private:
53 // support methods
54
55 static void invalid_value(sc_logic_7_value_t);
56 static void invalid_value(char);
57 static void invalid_value(int);
58
59 static sc_logic_7_value_t to_value(sc_logic_7_value_t v) {
60 if(v < Log_0 || v > Log_X) {
61 invalid_value(v);
62 }
63 return v;
64 }
65
66 static sc_logic_7_value_t to_value(bool b) { return (b ? Log_1 : Log_0); }
67
68 static sc_logic_7_value_t to_value(char c) {
69 sc_logic_7_value_t v;
70 unsigned int index = (int)c;
71 if(index > 127) {
72 invalid_value(c);
73 v = Log_X;
74 } else {
75 v = char_to_logic[index];
76 if(v < Log_0 || v > Log_X) {
77 invalid_value(c);
78 }
79 }
80 return v;
81 }
82
83 static sc_logic_7_value_t to_value(int i) {
84 if(i < 0 || i > 3) {
85 invalid_value(i);
86 }
87 return sc_logic_7_value_t(i);
88 }
89
90 void invalid_01() const;
91
92public:
93 // conversion tables
94
95 static std::array<const sc_logic_7_value_t, 128> char_to_logic;
96 static std::array<const char, 7> logic_to_char;
97 static std::array<std::array<const sc_logic_7_value_t, 7>, 7> and_table;
98 static std::array<std::array<const sc_logic_7_value_t, 7>, 7> or_table;
99 static std::array<std::array<const sc_logic_7_value_t, 7>, 7> xor_table;
100 static std::array<const sc_logic_7_value_t, 7> not_table;
101
102 // constructors
103
104 sc_logic_7() = default;
105
106 sc_logic_7(const sc_logic_7& a) = default;
107
108 sc_logic_7(sc_logic_7_value_t v)
109 : m_val(to_value(v)) {}
110
111 explicit sc_logic_7(bool a)
112 : m_val(to_value(a)) {}
113
114 explicit sc_logic_7(char a)
115 : m_val(to_value(a)) {}
116
117 explicit sc_logic_7(int a)
118 : m_val(to_value(a)) {}
119
120 explicit sc_logic_7(const ::sc_dt::sc_bit& a)
121 : m_val(to_value(a.to_bool())) {}
122
123 // destructor
124
125 ~sc_logic_7() = default;
126
127 // (bitwise) assignment operators
128
129#define DEFN_ASN_OP_T(op, tp) \
130 sc_logic_7& operator op(tp v) { \
131 *this op sc_logic_7(v); \
132 return *this; \
133 }
134
135#define DEFN_ASN_OP(op) \
136 DEFN_ASN_OP_T(op, sc_logic_7_value_t) \
137 DEFN_ASN_OP_T(op, bool) \
138 DEFN_ASN_OP_T(op, char) \
139 DEFN_ASN_OP_T(op, int) \
140 DEFN_ASN_OP_T(op, const ::sc_dt::sc_bit&)
141
142 sc_logic_7& operator=(const sc_logic_7& a) = default;
143
144 sc_logic_7& operator&=(const sc_logic_7& b) {
145 m_val = and_table[m_val][b.m_val];
146 return *this;
147 }
148
149 sc_logic_7& operator|=(const sc_logic_7& b) {
150 m_val = or_table[m_val][b.m_val];
151 return *this;
152 }
153
154 sc_logic_7& operator^=(const sc_logic_7& b) {
155 m_val = xor_table[m_val][b.m_val];
156 return *this;
157 }
158
159 DEFN_ASN_OP(=)
160 DEFN_ASN_OP(&=)
161 DEFN_ASN_OP(|=)
162 DEFN_ASN_OP(^=)
163
164#undef DEFN_ASN_OP_T
165#undef DEFN_ASN_OP
166
167 // bitwise operators and functions
168
169 friend const sc_logic_7 operator&(const sc_logic_7&, const sc_logic_7&);
170 friend const sc_logic_7 operator|(const sc_logic_7&, const sc_logic_7&);
171 friend const sc_logic_7 operator^(const sc_logic_7&, const sc_logic_7&);
172
173 // relational operators
174
175 friend bool operator==(const sc_logic_7&, const sc_logic_7&);
176 friend bool operator!=(const sc_logic_7&, const sc_logic_7&);
177
178 // bitwise complement
179
180 const sc_logic_7 operator~() const { return sc_logic_7(not_table[m_val]); }
181
182 sc_logic_7& b_not() {
183 m_val = not_table[m_val];
184 return *this;
185 }
186
187 // explicit conversions
188
189 sc_logic_7_value_t value() const { return m_val; }
190
191 bool is_01() const { return ((int)m_val == Log_0 || (int)m_val == Log_1); }
192
193 bool to_bool() const {
194 if(!is_01()) {
195 invalid_01();
196 }
197 return ((int)m_val != Log_0);
198 }
199
200 char to_char() const { return logic_to_char[m_val]; }
201
202 // other methods
203
204 void print(::std::ostream& os = ::std::cout) const { os << to_char(); }
205
206 void scan(::std::istream& is = ::std::cin);
207
208 // memory (de)allocation
209
210 static void* operator new(std::size_t, void* p) // placement new
211 {
212 return p;
213 }
214
215 static void* operator new(std::size_t sz) { return sc_core::sc_mempool::allocate(sz); }
216
217 static void operator delete(void* p, std::size_t sz) { sc_core::sc_mempool::release(p, sz); }
218
219 static void* operator new[](std::size_t sz) { return sc_core::sc_mempool::allocate(sz); }
220
221 static void operator delete[](void* p, std::size_t sz) { sc_core::sc_mempool::release(p, sz); }
222
223private:
224 sc_logic_7_value_t m_val = Log_U;
225
226private:
227 // disabled
228 explicit sc_logic_7(const char*);
229 sc_logic_7& operator=(const char*);
230};
231
232// ----------------------------------------------------------------------------
233
234// bitwise operators
235
236inline const sc_logic_7 operator&(const sc_logic_7& a, const sc_logic_7& b) { return sc_logic_7(sc_logic_7::and_table[a.m_val][b.m_val]); }
237
238inline const sc_logic_7 operator|(const sc_logic_7& a, const sc_logic_7& b) { return sc_logic_7(sc_logic_7::or_table[a.m_val][b.m_val]); }
239
240inline const sc_logic_7 operator^(const sc_logic_7& a, const sc_logic_7& b) { return sc_logic_7(sc_logic_7::xor_table[a.m_val][b.m_val]); }
241
242#define DEFN_BIN_OP_T(ret, op, tp) \
243 inline ret operator op(const sc_logic_7& a, tp b) { return (a op sc_logic_7(b)); } \
244 inline ret operator op(tp a, const sc_logic_7& b) { return (sc_logic_7(a) op b); }
245
246#define DEFN_BIN_OP(ret, op) \
247 DEFN_BIN_OP_T(ret, op, sc_logic_7_value_t) \
248 DEFN_BIN_OP_T(ret, op, bool) \
249 DEFN_BIN_OP_T(ret, op, char) \
250 DEFN_BIN_OP_T(ret, op, int)
251
252DEFN_BIN_OP(const sc_logic_7, &)
253DEFN_BIN_OP(const sc_logic_7, |)
254DEFN_BIN_OP(const sc_logic_7, ^)
255
256// relational operators and functions
257
258inline bool operator==(const sc_logic_7& a, const sc_logic_7& b) { return ((int)a.m_val == b.m_val); }
259
260inline bool operator!=(const sc_logic_7& a, const sc_logic_7& b) { return ((int)a.m_val != b.m_val); }
261
262DEFN_BIN_OP(bool, ==)
263DEFN_BIN_OP(bool, !=)
264
265#undef DEFN_BIN_OP_T
266#undef DEFN_BIN_OP
267
268// ----------------------------------------------------------------------------
269
270inline ::std::ostream& operator<<(::std::ostream& os, const sc_logic_7& a) {
271 a.print(os);
272 return os;
273}
274
275inline ::std::istream& operator>>(::std::istream& is, sc_logic_7& a) {
276 a.scan(is);
277 return is;
278}
279
280extern const sc_logic_7 SC_LOGIC7_0;
281extern const sc_logic_7 SC_LOGIC7_1;
282extern const sc_logic_7 SC_LOGIC7_Z;
283extern const sc_logic_7 SC_LOGIC7_X;
284
285} // namespace dt
286} // namespace scc // end of scc-sysc
288#endif /* INCL_SYSC_CORE_SC_LOGIC_7_H_ */
SCC TLM utilities.
trace_types operator|(trace_types lhs, trace_types rhs)
operator overload to allow boolean or operations on trace_types
Definition tracer_base.h:52