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