00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047 #include "sysc/kernel/sc_macros.h"
00048 #include "sysc/datatypes/int/sc_signed.h"
00049 #include "sysc/datatypes/int/sc_unsigned.h"
00050 #include "sysc/datatypes/int/sc_int_base.h"
00051 #include "sysc/datatypes/int/sc_uint_base.h"
00052 #include "sysc/datatypes/int/sc_signed.h"
00053 #include "sysc/datatypes/int/sc_int_ids.h"
00054 #include "sysc/datatypes/bit/sc_bv_base.h"
00055 #include "sysc/datatypes/bit/sc_lv_base.h"
00056 #include "sysc/datatypes/misc/sc_concatref.h"
00057 #include "sysc/datatypes/fx/sc_fix.h"
00058 #include "sysc/datatypes/fx/scfx_other_defs.h"
00059
00060
00061 namespace sc_dt
00062 {
00063
00064
00065
00066 void
00067 sc_int_concref_invalid_length( int length )
00068 {
00069 char msg[BUFSIZ];
00070 std::sprintf( msg,
00071 "sc_int_concref<T1,T2> initialization: length = %d "
00072 "violates 1 <= length <= %d",
00073 length, SC_INTWIDTH );
00074 SC_REPORT_ERROR( sc_core::SC_ID_OUT_OF_BOUNDS_, msg );
00075 }
00076
00077
00078
00079
00080
00081
00082
00083
00084 sc_core::sc_vpool<sc_int_bitref> sc_int_bitref::m_pool(9);
00085
00086
00087
00088
00089 void sc_int_bitref::concat_set(int64 src, int low_i)
00090 {
00091 sc_int_base aa( 1 );
00092 *this = aa = (low_i < 64) ? src >> low_i : src >> 63;
00093 }
00094
00095 void sc_int_bitref::concat_set(const sc_signed& src, int low_i)
00096 {
00097 sc_int_base aa( 1 );
00098 if ( low_i < src.length() )
00099 *this = aa = 1 & (src >> low_i);
00100 else
00101 *this = aa = (src < 0) ? (int_type)-1 : 0;
00102 }
00103
00104 void sc_int_bitref::concat_set(const sc_unsigned& src, int low_i)
00105 {
00106 sc_int_base aa( 1 );
00107 if ( low_i < src.length() )
00108 *this = aa = 1 & (src >> low_i);
00109 else
00110 *this = aa = 0;
00111 }
00112
00113 void sc_int_bitref::concat_set(uint64 src, int low_i)
00114 {
00115 sc_int_base aa( 1 );
00116 *this = aa = (low_i < 64) ? src >> low_i : 0;
00117 }
00118
00119
00120
00121
00122 void
00123 sc_int_bitref::scan( ::std::istream& is )
00124 {
00125 bool b;
00126 is >> b;
00127 *this = b;
00128 }
00129
00130
00131
00132
00133
00134
00135
00136
00137 bool sc_int_subref_r::concat_get_ctrl( sc_digit* dst_p, int low_i ) const
00138 {
00139 int dst_i;
00140 int end_i;
00141 int high_i;
00142 sc_digit mask;
00143
00144 dst_i = low_i / BITS_PER_DIGIT;
00145 high_i = low_i + (m_left-m_right);
00146 end_i = high_i / BITS_PER_DIGIT;
00147 mask = ~mask_int[m_left][m_right];
00148
00149
00150
00151
00152 dst_p[dst_i] = (unsigned long)(dst_p[dst_i] & mask);
00153 switch ( end_i - dst_i )
00154 {
00155
00156
00157 case 1:
00158 dst_i++;
00159 dst_p[dst_i] = 0;
00160 break;
00161
00162
00163
00164 case 2:
00165 dst_i++;
00166 dst_p[dst_i++] = 0;
00167 dst_p[dst_i] = 0;
00168 break;
00169
00170
00171
00172 case 3:
00173 dst_i++;
00174 dst_p[dst_i++] = 0;
00175 dst_p[dst_i++] = 0;
00176 dst_p[dst_i] = 0;
00177 break;
00178 }
00179 return false;
00180 }
00181
00182
00183 bool sc_int_subref_r::concat_get_data( sc_digit* dst_p, int low_i ) const
00184 {
00185 int dst_i;
00186 int end_i;
00187 int high_i;
00188 int left_shift;
00189 sc_digit mask;
00190 bool non_zero;
00191 uint_type val;
00192
00193 dst_i = low_i / BITS_PER_DIGIT;
00194 left_shift = low_i % BITS_PER_DIGIT;
00195 high_i = low_i + (m_left-m_right);
00196 end_i = high_i / BITS_PER_DIGIT;
00197 mask = ~mask_int[m_left][m_right];
00198 val = (m_obj_p->m_val & mask) >> m_right;
00199 non_zero = val != 0;
00200
00201
00202
00203
00204 mask = ~(-1 << left_shift);
00205 dst_p[dst_i] = (unsigned long)((dst_p[dst_i] & mask) |
00206 ((val << left_shift) & DIGIT_MASK));
00207 switch ( end_i - dst_i )
00208 {
00209
00210
00211 case 1:
00212 dst_i++;
00213 val >>= (BITS_PER_DIGIT-left_shift);
00214 dst_p[dst_i] = (unsigned long)(val & DIGIT_MASK);
00215 break;
00216
00217
00218
00219 case 2:
00220 dst_i++;
00221 val >>= (BITS_PER_DIGIT-left_shift);
00222 dst_p[dst_i++] = (unsigned long)(val & DIGIT_MASK);
00223 val >>= BITS_PER_DIGIT;
00224 dst_p[dst_i] = (unsigned long)val;
00225 break;
00226
00227
00228
00229 case 3:
00230 dst_i++;
00231 val >>= (BITS_PER_DIGIT-left_shift);
00232 dst_p[dst_i++] = (unsigned long)(val & DIGIT_MASK);
00233 val >>= BITS_PER_DIGIT;
00234 dst_p[dst_i++] = (unsigned long)val;
00235 val >>= BITS_PER_DIGIT;
00236 dst_p[dst_i] = (unsigned long)val;
00237 break;
00238 }
00239 return non_zero;
00240 }
00241
00242
00243
00244
00245
00246
00247
00248 sc_core::sc_vpool<sc_int_subref> sc_int_subref::m_pool(9);
00249
00250
00251
00252 sc_int_subref&
00253 sc_int_subref::operator = ( int_type v )
00254 {
00255 int_type val = m_obj_p->m_val;
00256 uint_type mask = mask_int[m_left][m_right];
00257 val &= mask;
00258 val |= (v << m_right) & ~mask;
00259 m_obj_p->m_val = val;
00260 m_obj_p->extend_sign();
00261 return *this;
00262 }
00263
00264 sc_int_subref&
00265 sc_int_subref::operator = ( const sc_signed& a )
00266 {
00267 sc_int_base aa( length() );
00268 return ( *this = aa = a );
00269 }
00270
00271 sc_int_subref&
00272 sc_int_subref::operator = ( const sc_unsigned& a )
00273 {
00274 sc_int_base aa( length() );
00275 return ( *this = aa = a );
00276 }
00277
00278 sc_int_subref&
00279 sc_int_subref::operator = ( const sc_bv_base& a )
00280 {
00281 sc_int_base aa( length() );
00282 return ( *this = aa = a );
00283 }
00284
00285 sc_int_subref&
00286 sc_int_subref::operator = ( const sc_lv_base& a )
00287 {
00288 sc_int_base aa( length() );
00289 return ( *this = aa = a );
00290 }
00291
00292
00293
00294
00295
00296 void sc_int_subref::concat_set(int64 src, int low_i)
00297 {
00298 sc_int_base aa ( length() );
00299 *this = aa = (low_i < 64) ? src >> low_i : src >> 63;
00300 }
00301
00302 void sc_int_subref::concat_set(const sc_signed& src, int low_i)
00303 {
00304 sc_int_base aa( length() );
00305 if ( low_i < src.length() )
00306 *this = aa = src >> low_i;
00307 else
00308 *this = (src < 0) ? (int_type)-1 : 0;
00309 }
00310
00311 void sc_int_subref::concat_set(const sc_unsigned& src, int low_i)
00312 {
00313 sc_int_base aa( length() );
00314 if ( low_i < src.length() )
00315 *this = aa = src >> low_i;
00316 else
00317 *this = 0;
00318 }
00319
00320 void sc_int_subref::concat_set(uint64 src, int low_i)
00321 {
00322 sc_int_base aa ( length() );
00323 *this = aa = (low_i < 64) ? src >> low_i : 0;
00324 }
00325
00326
00327
00328
00329 void
00330 sc_int_subref::scan( ::std::istream& is )
00331 {
00332 std::string s;
00333 is >> s;
00334 *this = s.c_str();
00335 }
00336
00337
00338
00339
00340
00341
00342
00343
00344
00345
00346 void
00347 sc_int_base::invalid_length() const
00348 {
00349 char msg[BUFSIZ];
00350 std::sprintf( msg,
00351 "sc_int[_base] initialization: length = %d violates "
00352 "1 <= length <= %d",
00353 m_len, SC_INTWIDTH );
00354 SC_REPORT_ERROR( sc_core::SC_ID_OUT_OF_BOUNDS_, msg );
00355 }
00356
00357 void
00358 sc_int_base::invalid_index( int i ) const
00359 {
00360 char msg[BUFSIZ];
00361 std::sprintf( msg,
00362 "sc_int[_base] bit selection: index = %d violates "
00363 "0 <= index <= %d",
00364 i, m_len - 1 );
00365 SC_REPORT_ERROR( sc_core::SC_ID_OUT_OF_BOUNDS_, msg );
00366 }
00367
00368 void
00369 sc_int_base::invalid_range( int l, int r ) const
00370 {
00371 char msg[BUFSIZ];
00372 std::sprintf( msg,
00373 "sc_int[_base] part selection: left = %d, right = %d violates "
00374 "0 <= right <= left <= %d",
00375 l, r, m_len - 1 );
00376 SC_REPORT_ERROR( sc_core::SC_ID_OUT_OF_BOUNDS_, msg );
00377 }
00378
00379
00380 void
00381 sc_int_base::check_value() const
00382 {
00383 int_type limit = (int_type) 1 << ( m_len - 1 );
00384 if( m_val < -limit || m_val >= limit ) {
00385 char msg[BUFSIZ];
00386 std::sprintf( msg, "sc_int[_base]: value does not fit into a length of %d",
00387 m_len );
00388 SC_REPORT_WARNING( sc_core::SC_ID_OUT_OF_BOUNDS_, msg );
00389 }
00390 }
00391
00392
00393
00394 sc_int_base::sc_int_base( const sc_bv_base& v )
00395 : m_val(0), m_len( v.length() ), m_ulen( SC_INTWIDTH - m_len )
00396 {
00397 check_length();
00398 *this = v;
00399 }
00400 sc_int_base::sc_int_base( const sc_lv_base& v )
00401 : m_val(0), m_len( v.length() ), m_ulen( SC_INTWIDTH - m_len )
00402 {
00403 check_length();
00404 *this = v;
00405 }
00406 sc_int_base::sc_int_base( const sc_uint_subref_r& v )
00407 : m_val(0), m_len( v.length() ), m_ulen( SC_INTWIDTH - m_len )
00408 {
00409 check_length();
00410 *this = v.to_uint64();
00411 }
00412 sc_int_base::sc_int_base( const sc_signed_subref_r& v )
00413 : m_val(0), m_len( v.length() ), m_ulen( SC_INTWIDTH - m_len )
00414 {
00415 check_length();
00416 *this = v.to_uint64();
00417 }
00418 sc_int_base::sc_int_base( const sc_unsigned_subref_r& v )
00419 : m_val(0), m_len( v.length() ), m_ulen( SC_INTWIDTH - m_len )
00420 {
00421 check_length();
00422 *this = v.to_uint64();
00423 }
00424
00425 sc_int_base::sc_int_base( const sc_signed& a )
00426 : m_val( 0 ), m_len( a.length() ), m_ulen( SC_INTWIDTH - m_len )
00427 {
00428 check_length();
00429 #if 0
00430 for( int i = m_len - 1; i >= 0; -- i ) {
00431 set( i, a.test( i ) );
00432 }
00433 extend_sign();
00434 #else
00435 *this = a.to_int64();
00436 #endif
00437 }
00438
00439 sc_int_base::sc_int_base( const sc_unsigned& a )
00440 : m_val( 0 ), m_len( a.length() ), m_ulen( SC_INTWIDTH - m_len )
00441 {
00442 check_length();
00443 #if 0
00444 for( int i = m_len - 1; i >= 0; -- i ) {
00445 set( i, a.test( i ) );
00446 }
00447 extend_sign();
00448 #else
00449 *this = a.to_int64();
00450 #endif
00451 }
00452
00453
00454
00455
00456 sc_int_base&
00457 sc_int_base::operator = ( const sc_signed& a )
00458 {
00459 int minlen = sc_min( m_len, a.length() );
00460 int i = 0;
00461 for( ; i < minlen; ++ i ) {
00462 set( i, a.test( i ) );
00463 }
00464 bool sgn = a.sign();
00465 for( ; i < m_len; ++ i ) {
00466
00467 set( i, sgn );
00468 }
00469 extend_sign();
00470 return *this;
00471 }
00472
00473 sc_int_base&
00474 sc_int_base::operator = ( const sc_unsigned& a )
00475 {
00476 int minlen = sc_min( m_len, a.length() );
00477 int i = 0;
00478 for( ; i < minlen; ++ i ) {
00479 set( i, a.test( i ) );
00480 }
00481 for( ; i < m_len; ++ i ) {
00482
00483 set( i, 0 );
00484 }
00485 extend_sign();
00486 return *this;
00487 }
00488
00489
00490 sc_int_base&
00491 sc_int_base::operator = ( const sc_bv_base& a )
00492 {
00493 int minlen = sc_min( m_len, a.length() );
00494 int i = 0;
00495 for( ; i < minlen; ++ i ) {
00496 set( i, a.get_bit( i ) );
00497 }
00498 for( ; i < m_len; ++ i ) {
00499
00500 set( i, 0 );
00501 }
00502 extend_sign();
00503 return *this;
00504 }
00505
00506 sc_int_base&
00507 sc_int_base::operator = ( const sc_lv_base& a )
00508 {
00509 int minlen = sc_min( m_len, a.length() );
00510 int i = 0;
00511 for( ; i < minlen; ++ i ) {
00512 set( i, sc_logic( a.get_bit( i ) ).to_bool() );
00513 }
00514 for( ; i < m_len; ++ i ) {
00515
00516 set( i, 0 );
00517 }
00518 extend_sign();
00519 return *this;
00520 }
00521
00522 sc_int_base&
00523 sc_int_base::operator = ( const char* a )
00524 {
00525 if( a == 0 ) {
00526 SC_REPORT_ERROR( sc_core::SC_ID_CONVERSION_FAILED_,
00527 "character string is zero" );
00528 }
00529 if( *a == 0 ) {
00530 SC_REPORT_ERROR( sc_core::SC_ID_CONVERSION_FAILED_,
00531 "character string is empty" );
00532 }
00533 try {
00534 int len = m_len;
00535 sc_fix aa( a, len, len, SC_TRN, SC_WRAP, 0, SC_ON );
00536 return this->operator = ( aa );
00537 } catch( sc_core::sc_report ) {
00538 char msg[BUFSIZ];
00539 std::sprintf( msg, "character string '%s' is not valid", a );
00540 SC_REPORT_ERROR( sc_core::SC_ID_CONVERSION_FAILED_, msg );
00541
00542 return *this;
00543 }
00544 }
00545
00546
00547
00548 const std::string
00549 sc_int_base::to_string( sc_numrep numrep ) const
00550 {
00551 int len = m_len;
00552 sc_fix aa( *this, len, len, SC_TRN, SC_WRAP, 0, SC_ON );
00553 return aa.to_string( numrep );
00554 }
00555
00556 const std::string
00557 sc_int_base::to_string( sc_numrep numrep, bool w_prefix ) const
00558 {
00559 int len = m_len;
00560 sc_fix aa( *this, len, len, SC_TRN, SC_WRAP, 0, SC_ON );
00561 return aa.to_string( numrep, w_prefix );
00562 }
00563
00564
00565
00566
00567 bool
00568 sc_int_base::and_reduce() const
00569 {
00570 return ( m_val == int_type( -1 ) );
00571 }
00572
00573 bool
00574 sc_int_base::or_reduce() const
00575 {
00576 return ( m_val != int_type( 0 ) );
00577 }
00578
00579 bool
00580 sc_int_base::xor_reduce() const
00581 {
00582 uint_type mask = ~UINT_ZERO;
00583 uint_type val = m_val & (mask >> m_ulen);
00584 int n = SC_INTWIDTH;
00585 do {
00586 n >>= 1;
00587 mask >>= n;
00588 val = ((val & (mask << n)) >> n) ^ (val & mask);
00589 } while( n != 1 );
00590 return ( val != uint_type( 0 ) );
00591 }
00592
00593
00594 bool sc_int_base::concat_get_ctrl( sc_digit* dst_p, int low_i ) const
00595 {
00596 int dst_i;
00597 int end_i;
00598 int left_shift;
00599 uint_type mask;
00600
00601 dst_i = low_i / BITS_PER_DIGIT;
00602 left_shift = low_i % BITS_PER_DIGIT;
00603 end_i = (low_i + (m_len-1)) / BITS_PER_DIGIT;
00604
00605 mask = ~(-1 << left_shift);
00606 dst_p[dst_i] = (unsigned long)(dst_p[dst_i] & mask);
00607 dst_i++;
00608 for ( ; dst_i <= end_i; dst_i++ ) dst_p[dst_i] = 0;
00609 return false;
00610 }
00611
00612 bool sc_int_base::concat_get_data( sc_digit* dst_p, int low_i ) const
00613 {
00614 int dst_i;
00615 int end_i;
00616 int high_i;
00617 int left_shift;
00618 sc_digit mask;
00619 bool non_zero;
00620 uint_type val;
00621
00622 dst_i = low_i / BITS_PER_DIGIT;
00623 left_shift = low_i % BITS_PER_DIGIT;
00624 high_i = low_i + (m_len-1);
00625 end_i = high_i / BITS_PER_DIGIT;
00626 val = m_val;
00627 non_zero = val != 0;
00628 if ( m_len < 64 )
00629 {
00630 mask = ~((uint_type)-1 << m_len);
00631 val &= mask;
00632 }
00633
00634
00635 mask = ~(-1 << left_shift);
00636 dst_p[dst_i] = (unsigned long)((dst_p[dst_i] & mask) |
00637 ((val <<left_shift) & DIGIT_MASK));
00638 switch ( end_i - dst_i )
00639 {
00640
00641
00642 case 1:
00643 dst_i++;
00644 val >>= (BITS_PER_DIGIT-left_shift);
00645 dst_p[dst_i] = (unsigned long)val;
00646 break;
00647
00648
00649
00650 case 2:
00651 dst_i++;
00652 val >>= (BITS_PER_DIGIT-left_shift);
00653 dst_p[dst_i++] = ((unsigned long)val) & DIGIT_MASK;
00654 val >>= BITS_PER_DIGIT;
00655 dst_p[dst_i] = (unsigned long)val;
00656 break;
00657
00658
00659
00660 case 3:
00661 dst_i++;
00662 val >>= (BITS_PER_DIGIT-left_shift);
00663 dst_p[dst_i++] = (unsigned long)(val & DIGIT_MASK);
00664 val >>= BITS_PER_DIGIT;
00665 dst_p[dst_i++] = (unsigned long)(val & DIGIT_MASK);
00666 val >>= BITS_PER_DIGIT;
00667 dst_p[dst_i] = (unsigned long)val;
00668 break;
00669 }
00670 return non_zero;
00671 }
00672
00673
00674 void sc_int_base::concat_set(int64 src, int low_i)
00675 {
00676 *this = (low_i < 64) ? src >> low_i : src >> 63;
00677 }
00678
00679 void sc_int_base::concat_set(const sc_signed& src, int low_i)
00680 {
00681 if ( low_i < src.length() )
00682 *this = src >> low_i;
00683 else
00684 *this = (src < 0) ? (int_type)-1 : 0;
00685 }
00686
00687 void sc_int_base::concat_set(const sc_unsigned& src, int low_i)
00688 {
00689 if ( low_i < src.length() )
00690 *this = src >> low_i;
00691 else
00692 *this = 0;
00693 }
00694
00695 void sc_int_base::concat_set(uint64 src, int low_i)
00696 {
00697 *this = (low_i < 64) ? src >> low_i : 0;
00698 }
00699
00700
00701
00702 void
00703 sc_int_base::scan( ::std::istream& is )
00704 {
00705 std::string s;
00706 is >> s;
00707 *this = s.c_str();
00708 }
00709
00710 }
00711
00712
00713