SystemC  2.3.2
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 */
33 /*****************************************************************************
34 
35  MODIFICATION LOG - modifiers, enter your name, affiliation, date and
36  changes you are making here.
37 
38  Name, Affiliation, Date:
39  Description of Modification:
40 
41  *****************************************************************************/
42 
43 // $Log: sc_logic.h,v $
44 // Revision 1.3 2011/08/07 18:54:19 acg
45 // Philipp A. Hartmann: remove friend function declarations that implement
46 // code, and clean up how bit and logic operators are defined in general.
47 //
48 // Revision 1.2 2011/01/25 20:50:37 acg
49 // Andy Goodrich: changes for IEEE 1666 2011.
50 //
51 // Revision 1.1.1.1 2006/12/15 20:20:04 acg
52 // SystemC 2.3
53 //
54 // Revision 1.5 2006/12/02 21:00:57 acg
55 // Andy Goodrich: fixes for concatenation support.
56 //
57 // Revision 1.4 2006/05/08 17:49:59 acg
58 // Andy Goodrich: Added David Long's declarations for friend operators,
59 // functions, and methods, to keep the Microsoft compiler happy.
60 //
61 // Revision 1.3 2006/01/13 18:53:53 acg
62 // Andy Goodrich: added $Log command so that CVS comments are reproduced in
63 // the source.
64 //
65 
66 #ifndef SC_LOGIC_H
67 #define SC_LOGIC_H
68 
69 
70 #include <cstdio>
71 #include "sysc/kernel/sc_cmnhdr.h"
72 #include "sysc/kernel/sc_macros.h"
73 #include "sysc/utils/sc_mempool.h"
75 
76 
77 namespace sc_dt
78 {
79 
80 // classes defined in this module
81 class sc_logic;
82 
83 
91 {
92  Log_0 = 0,
96 };
97 
105 {
106 private:
107 
108  // support methods
109 
110  static void invalid_value( sc_logic_value_t );
111  static void invalid_value( char );
112  static void invalid_value( int );
113 
114  static sc_logic_value_t to_value( sc_logic_value_t v )
115  {
116  if( v < Log_0 || v > Log_X ) {
117  invalid_value( v );
118  // may continue, if suppressed
119  v = Log_X;
120  }
121  return v;
122  }
123 
124  static sc_logic_value_t to_value( bool b )
125  { return ( b ? Log_1 : Log_0 ); }
126 
127  static sc_logic_value_t to_value( char c )
128  {
129  unsigned int index = (int)c;
130  if ( index > 127 )
131  {
132  invalid_value( c );
133  // may continue, if suppressed
134  index = 127; // aka Log_X
135  }
136  return char_to_logic[index];
137  }
138 
139  static sc_logic_value_t to_value( int i )
140  {
141  if( i < Log_0 || i > Log_X ) {
142  invalid_value( i );
143  // may continue, if suppressed
144  i = Log_X;
145  }
146  return sc_logic_value_t( i );
147  }
148 
149 
150  void invalid_01() const;
151 
152 public:
153 
154  // conversion tables
155 
156  static const sc_logic_value_t char_to_logic[128];
157  static const char logic_to_char[4];
158  static const sc_logic_value_t and_table[4][4];
159  static const sc_logic_value_t or_table[4][4];
160  static const sc_logic_value_t xor_table[4][4];
161  static const sc_logic_value_t not_table[4];
162 
163 
164  // constructors
165 
167  : m_val( Log_X )
168  {}
169 
170  sc_logic( const sc_logic& a )
171  : m_val( a.m_val )
172  {}
173 
175  : m_val( to_value( v ) )
176  {}
177 
178  explicit sc_logic( bool a )
179  : m_val( to_value( a ) )
180  {}
181 
182  explicit sc_logic( char a )
183  : m_val( to_value( a ) )
184  {}
185 
186  explicit sc_logic( int a )
187  : m_val( to_value( a ) )
188  {}
189 
190  explicit sc_logic( const sc_bit& a )
191  : m_val( to_value( a.to_bool() ) )
192  {}
193 
194 
195  // destructor
196 
198  {}
199 
200 
201  // (bitwise) assignment operators
202 
203 #define DEFN_ASN_OP_T(op,tp) \
204  sc_logic& operator op ( tp v ) \
205  { *this op sc_logic( v ); return *this; }
206 
207 #define DEFN_ASN_OP(op) \
208  DEFN_ASN_OP_T(op, sc_logic_value_t) \
209  DEFN_ASN_OP_T(op, bool) \
210  DEFN_ASN_OP_T(op, char) \
211  DEFN_ASN_OP_T(op, int ) \
212  DEFN_ASN_OP_T(op, const sc_bit& )
213 
214  sc_logic& operator = ( const sc_logic& a )
215  { m_val = a.m_val; return *this; }
216 
218  { m_val = and_table[m_val][b.m_val]; return *this; }
219 
221  { m_val = or_table[m_val][b.m_val]; return *this; }
222 
224  { m_val = xor_table[m_val][b.m_val]; return *this; }
225 
226  DEFN_ASN_OP(=)
227  DEFN_ASN_OP(&=)
228  DEFN_ASN_OP(|=)
229  DEFN_ASN_OP(^=)
230 
231 #undef DEFN_ASN_OP_T
232 #undef DEFN_ASN_OP
233 
234 
235  // bitwise operators and functions
236 
237 
238  friend const sc_logic operator & ( const sc_logic&, const sc_logic& );
239  friend const sc_logic operator | ( const sc_logic&, const sc_logic& );
240  friend const sc_logic operator ^ ( const sc_logic&, const sc_logic& );
241 
242  // relational operators
243 
244  friend bool operator == ( const sc_logic&, const sc_logic& );
245  friend bool operator != ( const sc_logic&, const sc_logic& );
246 
247  // bitwise complement
248 
249  const sc_logic operator ~ () const
250  { return sc_logic( not_table[m_val] ); }
251 
253  { m_val = not_table[m_val]; return *this; }
254 
255 
256  // explicit conversions
257 
259  { return m_val; }
260 
261 
262  bool is_01() const
263  { return ( (int) m_val == Log_0 || (int) m_val == Log_1 ); }
264 
265  bool to_bool() const
266  { if( ! is_01() ) { invalid_01(); } return ( (int) m_val != Log_0 ); }
267 
268  char to_char() const
269  { return logic_to_char[m_val]; }
270 
271 
272  // other methods
273 
274  void print( ::std::ostream& os = ::std::cout ) const
275  { os << to_char(); }
276 
277  void scan( ::std::istream& is = ::std::cin );
278 
279 
280  // memory (de)allocation
281 
282  static void* operator new( std::size_t, void* p ) // placement new
283  { return p; }
284 
285  static void* operator new( std::size_t sz )
286  { return sc_core::sc_mempool::allocate( sz ); }
287 
288  static void operator delete( void* p, std::size_t sz )
289  { sc_core::sc_mempool::release( p, sz ); }
290 
291  static void* operator new [] ( std::size_t sz )
292  { return sc_core::sc_mempool::allocate( sz ); }
293 
294  static void operator delete [] ( void* p, std::size_t sz )
295  { sc_core::sc_mempool::release( p, sz ); }
296 
297 private:
298 
299  sc_logic_value_t m_val;
300 
301 private:
302 
303  // disabled
304  explicit sc_logic( const char* );
305  sc_logic& operator = ( const char* );
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 inline
350 ::std::ostream&
351 operator << ( ::std::ostream& os, const sc_logic& a )
352 {
353  a.print( os );
354  return os;
355 }
356 
357 inline
358 ::std::istream&
359 operator >> ( ::std::istream& is, sc_logic& a )
360 {
361  a.scan( is );
362  return is;
363 }
364 
365 
366 extern SC_API const sc_logic SC_LOGIC_0;
367 extern SC_API const sc_logic SC_LOGIC_1;
368 extern SC_API const sc_logic SC_LOGIC_Z;
369 extern SC_API const sc_logic SC_LOGIC_X;
370 
371 // #ifdef SC_DT_DEPRECATED
372 extern SC_API const sc_logic sc_logic_0;
373 extern SC_API const sc_logic sc_logic_1;
374 extern SC_API const sc_logic sc_logic_Z;
375 extern SC_API const sc_logic sc_logic_X;
376 // #endif
377 
378 } // namespace sc_dt
379 
380 #endif
static const sc_logic_value_t and_table[4][4]
Definition: sc_logic.h:158
SC_API const sc_logic SC_LOGIC_X
static void release(void *p, std::size_t sz)
void scan(::std::istream &is=::std::cin)
bool operator==(const sc_bit &a, const sc_bit &b)
Definition: sc_bit.h:289
sc_logic(bool a)
Definition: sc_logic.h:178
X & operator&=(sc_proxy< X > &px, const sc_proxy< Y > &py)
Definition: sc_lv_base.h:342
sc_logic(int a)
Definition: sc_logic.h:186
sc_logic(char a)
Definition: sc_logic.h:182
sc_logic(const sc_bit &a)
Definition: sc_logic.h:190
Bit class.
Definition: sc_bit.h:92
SC_API const sc_logic SC_LOGIC_0
X & operator|=(sc_proxy< X > &px, const sc_proxy< Y > &py)
Definition: sc_lv_base.h:444
SC_API const sc_logic sc_logic_Z
SC_API const sc_logic sc_logic_X
Four-valued logic type.
Definition: sc_logic.h:104
sc_logic(sc_logic_value_t v)
Definition: sc_logic.h:174
void print(::std::ostream &os=::std::cout) const
Definition: sc_logic.h:274
sc_logic_value_t
Enumeration of values for sc_logic.
Definition: sc_logic.h:90
uint64 const sc_uint_base int b
Definition: sc_fxval.h:1005
bool operator!=(const sc_bit &a, const sc_bit &b)
Definition: sc_bit.h:292
#define DEFN_ASN_OP(op)
Definition: sc_logic.h:207
const sc_bit operator|(const sc_bit &a, const sc_bit &b)
Definition: sc_bit.h:338
SC_API const sc_logic sc_logic_1
Miscellaneous definitions that are needed by the headers.
static const sc_logic_value_t xor_table[4][4]
Definition: sc_logic.h:160
sc_logic_value_t value() const
Definition: sc_logic.h:258
SC_API const sc_logic SC_LOGIC_1
Bit class.
char to_char() const
Definition: sc_logic.h:268
bool is_01() const
Definition: sc_logic.h:262
SC_API const sc_logic SC_LOGIC_Z
const sc_bit operator^(const sc_bit &a, const sc_bit &b)
Definition: sc_bit.h:341
X & operator^=(sc_proxy< X > &px, const sc_proxy< Y > &py)
Definition: sc_lv_base.h:546
sc_logic & b_not()
Definition: sc_logic.h:252
#define DEFN_BIN_OP(ret, op)
Definition: sc_logic.h:325
const sc_bit operator&(const sc_bit &a, const sc_bit &b)
Definition: sc_bit.h:335
bool to_bool() const
Definition: sc_logic.h:265
sc_logic(const sc_logic &a)
Definition: sc_logic.h:170
static void * allocate(std::size_t sz)
static const sc_logic_value_t or_table[4][4]
Definition: sc_logic.h:159
class SC_API sc_logic
Definition: sc_signal_ifs.h:43
inline ::std::ostream & operator<<(::std::ostream &os, const sc_bit &a)
Definition: sc_bit.h:390
SC_API const sc_logic sc_logic_0
const sc_bit operator~(const sc_bit &a)
Definition: sc_bit.h:316
#define SC_API
Definition: sc_cmnhdr.h:168
inline ::std::istream & operator>>(::std::istream &is, sc_bit &a)
Definition: sc_bit.h:398