SystemC 3.0.0
Accellera SystemC proof-of-concept library
sc_logic.h
Go to the documentation of this file.
1/*****************************************************************************
2
3 Licensed to Accellera Systems Initiative Inc. (Accellera) under one or
4 more contributor license agreements. See the NOTICE file distributed
5 with this work for additional information regarding copyright ownership.
6 Accellera licenses this file to you under the Apache License, Version 2.0
7 (the "License"); you may not use this file except in compliance with the
8 License. You may obtain a copy of the License at
9
10 http://www.apache.org/licenses/LICENSE-2.0
11
12 Unless required by applicable law or agreed to in writing, software
13 distributed under the License is distributed on an "AS IS" BASIS,
14 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
15 implied. See the License for the specific language governing
16 permissions and limitations under the License.
17
18 *****************************************************************************/
19
20/*****************************************************************************
21
22 sc_logic.h -- C++ implementation of logic type. Behaves
23 pretty much the same way as HDLs except with 4 values.
24
25 Original Author: Stan Y. Liao, Synopsys, Inc.
26
27 *****************************************************************************/
28
29/*****************************************************************************
30
31 MODIFICATION LOG - modifiers, enter your name, affiliation, date and
32 changes you are making here.
33
34 Name, Affiliation, Date:
35 Description of Modification:
36
37 *****************************************************************************/
38
39// $Log: sc_logic.h,v $
40// Revision 1.3 2011/08/07 18:54:19 acg
41// Philipp A. Hartmann: remove friend function declarations that implement
42// code, and clean up how bit and logic operators are defined in general.
43//
44// Revision 1.2 2011/01/25 20:50:37 acg
45// Andy Goodrich: changes for IEEE 1666 2011.
46//
47// Revision 1.1.1.1 2006/12/15 20:20:04 acg
48// SystemC 2.3
49//
50// Revision 1.5 2006/12/02 21:00:57 acg
51// Andy Goodrich: fixes for concatenation support.
52//
53// Revision 1.4 2006/05/08 17:49:59 acg
54// Andy Goodrich: Added David Long's declarations for friend operators,
55// functions, and methods, to keep the Microsoft compiler happy.
56//
57// Revision 1.3 2006/01/13 18:53:53 acg
58// Andy Goodrich: added $Log command so that CVS comments are reproduced in
59// the source.
60//
61
62#ifndef SC_LOGIC_H
63#define SC_LOGIC_H
64
65
66#include <cstdio>
71
72
73namespace sc_dt
74{
75
76// classes defined in this module
77class sc_logic;
78
79
80// ----------------------------------------------------------------------------
81// ENUM : sc_logic_value_t
82//
83// Enumeration of values for sc_logic.
84// ----------------------------------------------------------------------------
85
87{
88 Log_0 = 0,
91 Log_X
92};
93
94// ----------------------------------------------------------------------------
95// CLASS : sc_logic
96//
97// Four-valued logic type.
98// ----------------------------------------------------------------------------
99
101{
102private:
103
104 // support methods
105
106 static void invalid_value( sc_logic_value_t );
107 static void invalid_value( char );
108 static void invalid_value( int );
109
110 static sc_logic_value_t to_value( sc_logic_value_t v )
111 {
112 if( v < Log_0 || v > Log_X ) {
113 invalid_value( v );
114 // may continue, if suppressed
115 v = Log_X;
116 }
117 return v;
118 }
119
120 static sc_logic_value_t to_value( bool b )
121 { return ( b ? Log_1 : Log_0 ); }
122
123 static sc_logic_value_t to_value( char c )
124 {
125 unsigned int index = (int)c;
126 if ( index > 127 )
127 {
128 invalid_value( c );
129 // may continue, if suppressed
130 index = 127; // aka Log_X
131 }
132 return char_to_logic[index];
133 }
134
135 static sc_logic_value_t to_value( int i )
136 {
137 if( i < Log_0 || i > Log_X ) {
138 invalid_value( i );
139 // may continue, if suppressed
140 i = Log_X;
141 }
142 return sc_logic_value_t( i );
143 }
144
145
146 void invalid_01() const;
147
148public:
149
150 // conversion tables
151
152 static const sc_logic_value_t char_to_logic[128];
153 static const char logic_to_char[4];
154 static const sc_logic_value_t and_table[4][4];
155 static const sc_logic_value_t or_table[4][4];
156 static const sc_logic_value_t xor_table[4][4];
157 static const sc_logic_value_t not_table[4];
158
159
160 // constructors
161
163 : m_val( Log_X )
164 {}
165
166 sc_logic( const sc_logic& a )
167 : m_val( a.m_val )
168 {}
169
171 : m_val( to_value( v ) )
172 {}
173
174 explicit sc_logic( bool a )
175 : m_val( to_value( a ) )
176 {}
177
178 explicit sc_logic( char a )
179 : m_val( to_value( a ) )
180 {}
181
182 explicit sc_logic( int a )
183 : m_val( to_value( a ) )
184 {}
185
186 explicit sc_logic( const sc_bit& a )
187 : m_val( to_value( a.to_bool() ) )
188 {}
189
190
191 // destructor
192
194 {}
195
196
197 // (bitwise) assignment operators
198
199#define DEFN_ASN_OP_T(op,tp) \
200 sc_logic& operator op ( tp v ) \
201 { *this op sc_logic( v ); return *this; }
202
203#define DEFN_ASN_OP(op) \
204 DEFN_ASN_OP_T(op, sc_logic_value_t) \
205 DEFN_ASN_OP_T(op, bool) \
206 DEFN_ASN_OP_T(op, char) \
207 DEFN_ASN_OP_T(op, int ) \
208 DEFN_ASN_OP_T(op, sc_bit )
209
210 sc_logic& operator = ( const sc_logic& a )
211 { m_val = a.m_val; return *this; }
212
214 { m_val = and_table[m_val][b.m_val]; return *this; }
215
217 { m_val = or_table[m_val][b.m_val]; return *this; }
218
220 { m_val = xor_table[m_val][b.m_val]; return *this; }
221
222 DEFN_ASN_OP(=)
223 DEFN_ASN_OP(&=)
224 DEFN_ASN_OP(|=)
225 DEFN_ASN_OP(^=)
226
227#undef DEFN_ASN_OP_T
228#undef DEFN_ASN_OP
229
230
231 // bitwise operators and functions
232
233
234 friend const sc_logic operator & ( const sc_logic&, const sc_logic& );
235 friend const sc_logic operator | ( const sc_logic&, const sc_logic& );
236 friend const sc_logic operator ^ ( const sc_logic&, const sc_logic& );
237
238 // relational operators
239
240 friend bool operator == ( const sc_logic&, const sc_logic& );
241 friend bool operator != ( const sc_logic&, const sc_logic& );
242
243 // bitwise complement
244
245 const sc_logic operator ~ () const
246 { return sc_logic( not_table[m_val] ); }
247
249 { m_val = not_table[m_val]; return *this; }
250
251
252 // explicit conversions
253
255 { return m_val; }
256
257
258 bool is_01() const
259 { return ( (int) m_val == Log_0 || (int) m_val == Log_1 ); }
260
261 bool to_bool() const
262 { if( ! is_01() ) { invalid_01(); } return ( (int) m_val != Log_0 ); }
263
264 char to_char() const
265 { return logic_to_char[m_val]; }
266
267
268 // other methods
269
270 void print( ::std::ostream& os = ::std::cout ) const
271 { os << to_char(); }
272
273 void scan( ::std::istream& is = ::std::cin );
274
275
276 // memory (de)allocation
277
278 static void* operator new( std::size_t, void* p ) // placement new
279 { return p; }
280
281 static void* operator new( std::size_t sz )
282 { return sc_core::sc_mempool::allocate( sz ); }
283
284 static void operator delete( void* p, std::size_t sz )
285 { sc_core::sc_mempool::release( p, sz ); }
286
287 static void* operator new [] ( std::size_t sz )
288 { return sc_core::sc_mempool::allocate( sz ); }
289
290 static void operator delete [] ( void* p, std::size_t sz )
291 { sc_core::sc_mempool::release( p, sz ); }
292
293private:
294
295 sc_logic_value_t m_val;
296
297private:
298
299 // disabled
300 explicit sc_logic( const char* );
301 sc_logic& operator = ( const char* );
302};
303
304// ----------------------------------------------------------------------------
305
306// bitwise operators
307
308inline const sc_logic operator & ( const sc_logic& a, const sc_logic& b )
309 { return sc_logic( sc_logic::and_table[a.m_val][b.m_val] ); }
310
311inline const sc_logic operator | ( const sc_logic& a, const sc_logic& b )
312 { return sc_logic( sc_logic::or_table[a.m_val][b.m_val] ); }
313
314inline const sc_logic operator ^ ( const sc_logic& a, const sc_logic& b )
315 { return sc_logic( sc_logic::xor_table[a.m_val][b.m_val] ); }
316
317#define DEFN_BIN_OP_T(ret,op,tp) \
318 inline ret operator op ( const sc_logic& a, tp b ) \
319 { return ( a op sc_logic( b ) ); } \
320 inline ret operator op ( tp a, const sc_logic& b ) \
321 { return ( sc_logic( a ) op b ); }
322
323#define DEFN_BIN_OP(ret,op) \
324 DEFN_BIN_OP_T(ret,op,sc_logic_value_t) \
325 DEFN_BIN_OP_T(ret,op,bool) \
326 DEFN_BIN_OP_T(ret,op,char) \
327 DEFN_BIN_OP_T(ret,op,int)
328
329DEFN_BIN_OP(const sc_logic,&)
330DEFN_BIN_OP(const sc_logic,|)
331DEFN_BIN_OP(const sc_logic,^)
332
333// relational operators and functions
334
335inline bool operator == ( const sc_logic& a, const sc_logic& b )
336 { return ( (int) a.m_val == b.m_val ); }
337
338inline bool operator != ( const sc_logic& a, const sc_logic& b )
339 { return ( (int) a.m_val != b.m_val ); }
340
341DEFN_BIN_OP(bool,==)
342DEFN_BIN_OP(bool,!=)
343
344#undef DEFN_BIN_OP_T
345#undef DEFN_BIN_OP
346
347// ----------------------------------------------------------------------------
348
349inline
350::std::ostream&
351operator << ( ::std::ostream& os, const sc_logic& a )
352{
353 a.print( os );
354 return os;
355}
356
357inline
358::std::istream&
359operator >> ( ::std::istream& is, sc_logic& a )
360{
361 a.scan( is );
362 return is;
363}
364
365
366extern SC_API const sc_logic SC_LOGIC_0;
367extern SC_API const sc_logic SC_LOGIC_1;
368extern SC_API const sc_logic SC_LOGIC_Z;
369extern SC_API const sc_logic SC_LOGIC_X;
370
371// #ifdef SC_DT_DEPRECATED
372extern SC_API const sc_logic sc_logic_0;
373extern SC_API const sc_logic sc_logic_1;
374extern SC_API const sc_logic sc_logic_Z;
375extern SC_API const sc_logic sc_logic_X;
376// #endif
377
378} // namespace sc_dt
379
380#endif
#define DEFN_ASN_OP(op)
Definition: sc_logic.h:203
#define DEFN_BIN_OP(ret, op)
Definition: sc_logic.h:323
#define SC_API
Definition: sc_cmnhdr.h:148
X & operator|=(sc_proxy< X > &px, const sc_proxy< Y > &py)
Definition: sc_lv_base.h:474
sc_logic_value_t
Definition: sc_logic.h:87
@ Log_Z
Definition: sc_logic.h:90
@ Log_1
Definition: sc_logic.h:89
@ Log_X
Definition: sc_logic.h:91
@ Log_0
Definition: sc_logic.h:88
SC_API const sc_logic SC_LOGIC_0
SC_API const sc_logic SC_LOGIC_X
SC_API const sc_logic sc_logic_1
sc_bit operator&(const sc_bit &a, const sc_bit &b)
Definition: sc_bit.h:331
sc_bit operator^(const sc_bit &a, const sc_bit &b)
Definition: sc_bit.h:337
X & operator^=(sc_proxy< X > &px, const sc_proxy< Y > &py)
Definition: sc_lv_base.h:613
bool operator==(const sc_bit &a, const sc_bit &b)
Definition: sc_bit.h:285
class SC_API sc_logic
Definition: sc_signal_ifs.h:39
SC_API const sc_logic sc_logic_0
SC_API const sc_logic sc_logic_X
SC_API const sc_logic SC_LOGIC_Z
X & operator&=(sc_proxy< X > &px, const sc_proxy< Y > &py)
Definition: sc_lv_base.h:340
SC_API const sc_logic SC_LOGIC_1
bool operator!=(const sc_bit &a, const sc_bit &b)
Definition: sc_bit.h:288
inline::std::istream & operator>>(::std::istream &is, sc_bit &a)
Definition: sc_bit.h:396
SC_API const sc_logic sc_logic_Z
inline::std::ostream & operator<<(::std::ostream &os, const sc_bit &a)
Definition: sc_bit.h:388
uint64 const sc_uint_base int b
Definition: sc_fxval.h:955
sc_bit operator~(const sc_bit &a)
Definition: sc_bit.h:312
sc_bit operator|(const sc_bit &a, const sc_bit &b)
Definition: sc_bit.h:334
static const sc_logic_value_t or_table[4][4]
Definition: sc_logic.h:155
sc_logic(const sc_bit &a)
Definition: sc_logic.h:186
sc_logic(bool a)
Definition: sc_logic.h:174
static const sc_logic_value_t xor_table[4][4]
Definition: sc_logic.h:156
static const sc_logic_value_t and_table[4][4]
Definition: sc_logic.h:154
sc_logic(const sc_logic &a)
Definition: sc_logic.h:166
sc_logic(char a)
Definition: sc_logic.h:178
bool is_01() const
Definition: sc_logic.h:258
char to_char() const
Definition: sc_logic.h:264
void scan(::std::istream &is=::std::cin)
sc_logic(sc_logic_value_t v)
Definition: sc_logic.h:170
sc_logic(int a)
Definition: sc_logic.h:182
sc_logic & b_not()
Definition: sc_logic.h:248
bool to_bool() const
Definition: sc_logic.h:261
void print(::std::ostream &os=::std::cout) const
Definition: sc_logic.h:270
sc_logic_value_t value() const
Definition: sc_logic.h:254
static void release(void *p, std::size_t sz)
static void * allocate(std::size_t sz)