summaryrefslogtreecommitdiff
path: root/src/systemc/dt/int/sc_int_base.cc
blob: d5b005b933405a70229493dec2c19426f8b93ada (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
/*****************************************************************************

  Licensed to Accellera Systems Initiative Inc. (Accellera) under one or
  more contributor license agreements.  See the NOTICE file distributed
  with this work for additional information regarding copyright ownership.
  Accellera licenses this file to you under the Apache License, Version 2.0
  (the "License"); you may not use this file except in compliance with the
  License.  You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

  Unless required by applicable law or agreed to in writing, software
  distributed under the License is distributed on an "AS IS" BASIS,
  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
  implied.  See the License for the specific language governing
  permissions and limitations under the License.

 *****************************************************************************/

/*****************************************************************************

  sc_int_base.cpp -- contains interface definitions between sc_int and
                sc_signed, sc_unsigned, and definitions for sc_int_subref.

  Original Author: Ali Dasdan, Synopsys, Inc.

 *****************************************************************************/

/*****************************************************************************

  MODIFICATION LOG - modifiers, enter your name, affiliation, date and
  changes you are making here.

      Name, Affiliation, Date:
  Description of Modification:

 *****************************************************************************/


// $Log: sc_int_base.cpp,v $
// Revision 1.5  2011/02/18 20:19:14  acg
//  Andy Goodrich: updating Copyright notice.
//
// Revision 1.4  2010/02/04 22:23:29  acg
//  Andy Goodrich: fixed bug in concatenation reads for part selections,
//  the mask being used was 32 bits and should have been 64 bits.
//
// Revision 1.3  2008/06/19 17:47:56  acg
//  Andy Goodrich: fixes for bugs. See 2.2.1 RELEASENOTES.
//
// Revision 1.2  2007/11/04 21:27:00  acg
//  Andy Goodrich: changes to make sure the proper value is returned from
//  concat_get_data().
//
// Revision 1.1.1.1  2006/12/15 20:20:05  acg
// SystemC 2.3
//
// Revision 1.3  2006/01/13 18:49:31  acg
// Added $Log command so that CVS check in comments are reproduced in the
// source.
//

#include <sstream>

#include "systemc/ext/dt/bit/sc_bv_base.hh"
#include "systemc/ext/dt/bit/sc_lv_base.hh"
#include "systemc/ext/dt/fx/sc_fix.hh"
#include "systemc/ext/dt/fx/scfx_other_defs.hh"
#include "systemc/ext/dt/int/sc_int_base.hh"
#include "systemc/ext/dt/int/sc_signed.hh"
#include "systemc/ext/dt/int/sc_uint_base.hh"
#include "systemc/ext/dt/int/sc_unsigned.hh"
#include "systemc/ext/dt/misc/sc_concatref.hh"

// explicit template instantiations
namespace sc_core
{

template class sc_vpool<sc_dt::sc_int_bitref>;
template class sc_vpool<sc_dt::sc_int_subref>;

} // namespace sc_core

namespace sc_dt
{

// to avoid code bloat in sc_int_concref<T1,T2>

void
sc_int_concref_invalid_length(int length)
{
    std::stringstream msg;
    msg << "sc_int_concref<T1,T2> initialization: length = " << length <<
           "violates 1 <= length <= " << SC_INTWIDTH;
    SC_REPORT_ERROR("out of bounds", msg.str().c_str());
    sc_core::sc_abort(); // can't recover from here
}


// ----------------------------------------------------------------------------
//  CLASS : sc_int_bitref
//
//  Proxy class for sc_int bit selection (r-value and l-value).
// ----------------------------------------------------------------------------

sc_core::sc_vpool<sc_int_bitref> sc_int_bitref::m_pool(9);

// concatenation methods:

// #### OPTIMIZE
void sc_int_bitref::concat_set(int64 src, int low_i)
{
    sc_int_base aa(1);
    *this = aa = (low_i < 64) ? src >> low_i : src >> 63;
}

void sc_int_bitref::concat_set(const sc_signed &src, int low_i)
{
    sc_int_base aa(1);
    if (low_i < src.length())
        *this = aa = 1 & (src >> low_i);
    else
        *this = aa = (src < 0) ? (int_type)-1 : 0;
}

void sc_int_bitref::concat_set(const sc_unsigned &src, int low_i)
{
    sc_int_base aa(1);
    if (low_i < src.length())
        *this = aa = 1 & (src >> low_i);
    else
        *this = aa = 0;
}

void sc_int_bitref::concat_set(uint64 src, int low_i)
{
    sc_int_base aa(1);
    *this = aa = (low_i < 64) ? src >> low_i : 0;
}


// other methods
void
sc_int_bitref::scan(::std::istream &is)
{
    bool b;
    is >> b;
    *this = b;
}


// ----------------------------------------------------------------------------
//  CLASS : sc_int_subref_r
//
//  Proxy class for sc_int part selection (l-value).
// ----------------------------------------------------------------------------

bool
sc_int_subref_r::concat_get_ctrl(sc_digit *dst_p, int low_i) const
{
    int dst_i; // Word in dst_p now processing.
    int end_i; // Highest order word in dst_p to process.
    int high_i; // Index of high order bit in dst_p to set.
    uint_type mask; // Mask for bits to extract or keep.

    dst_i = low_i / BITS_PER_DIGIT;
    high_i = low_i + (m_left - m_right);
    end_i = high_i / BITS_PER_DIGIT;
    mask = ~mask_int[m_left][m_right];

    // PROCESS THE FIRST WORD:
    dst_p[dst_i] = (sc_digit)(dst_p[dst_i] & mask);
    switch (end_i - dst_i) {
      // BITS ARE ACROSS TWO WORDS:
      case 1:
        dst_i++;
        dst_p[dst_i] = 0;
        break;

      // BITS ARE ACROSS THREE WORDS:
      case 2:
        dst_i++;
        dst_p[dst_i++] = 0;
        dst_p[dst_i] = 0;
        break;

      // BITS ARE ACROSS FOUR WORDS:
      case 3:
        dst_i++;
        dst_p[dst_i++] = 0;
        dst_p[dst_i++] = 0;
        dst_p[dst_i] = 0;
        break;
    }
    return false;
}

bool
sc_int_subref_r::concat_get_data(sc_digit *dst_p, int low_i) const
{
    int dst_i; // Word in dst_p now processing.
    int end_i; // Highest order word in dst_p to process.
    int high_i; // Index of high order bit in dst_p to set.
    int left_shift; // Left shift for val.
    uint_type mask; // Mask for bits to extract or keep.
    bool non_zero; // True if value inserted is non-zero.
    uint_type val; // Selection value extracted from m_obj_p.

    dst_i = low_i / BITS_PER_DIGIT;
    left_shift = low_i % BITS_PER_DIGIT;
    high_i = low_i + (m_left-m_right);
    end_i = high_i / BITS_PER_DIGIT;
    mask = ~mask_int[m_left][m_right];
    val = (m_obj_p->m_val & mask) >> m_right;
    non_zero = val != 0;

    // PROCESS THE FIRST WORD:
    mask = ~(~UINT_ZERO << left_shift);
    dst_p[dst_i] = (sc_digit)((dst_p[dst_i] & mask) |
                ((val << left_shift) & DIGIT_MASK));

    switch (end_i - dst_i) {
      // BITS ARE ACROSS TWO WORDS:
      case 1:
        dst_i++;
        val >>= (BITS_PER_DIGIT - left_shift);
        dst_p[dst_i] = (sc_digit)(val & DIGIT_MASK);
        break;

      // BITS ARE ACROSS THREE WORDS:
      case 2:
        dst_i++;
        val >>= (BITS_PER_DIGIT - left_shift);
        dst_p[dst_i++] = (sc_digit)(val & DIGIT_MASK);
        val >>= BITS_PER_DIGIT;
        dst_p[dst_i] = (sc_digit)val;
        break;

      // BITS ARE ACROSS FOUR WORDS:
      case 3:
        dst_i++;
        val >>= (BITS_PER_DIGIT - left_shift);
        dst_p[dst_i++] = (sc_digit)(val & DIGIT_MASK);
        val >>= BITS_PER_DIGIT;
        dst_p[dst_i++] = (sc_digit)(val & DIGIT_MASK);
        val >>= BITS_PER_DIGIT;
        dst_p[dst_i] = (sc_digit)val;
        break;
    }
    return non_zero;
}

// ----------------------------------------------------------------------------
//  CLASS : sc_int_subref
//
//  Proxy class for sc_int part selection (r-value and l-value).
// ----------------------------------------------------------------------------

sc_core::sc_vpool<sc_int_subref> sc_int_subref::m_pool(9);

// assignment operators

sc_int_subref &
sc_int_subref::operator = (int_type v)
{
    int_type val = m_obj_p->m_val;
    uint_type mask = mask_int[m_left][m_right];
    val &= mask;
    val |= (v << m_right) & ~mask;
    m_obj_p->m_val = val;
    m_obj_p->extend_sign();
    return *this;
}

sc_int_subref &
sc_int_subref::operator = (const sc_signed &a)
{
    sc_int_base aa(length());
    return (*this = aa = a);
}

sc_int_subref &
sc_int_subref::operator = (const sc_unsigned &a)
{
    sc_int_base aa(length());
    return (*this = aa = a);
}

sc_int_subref &
sc_int_subref::operator = (const sc_bv_base &a)
{
    sc_int_base aa(length());
    return (*this = aa = a);
}

sc_int_subref &
sc_int_subref::operator = (const sc_lv_base &a)
{
    sc_int_base aa(length());
    return (*this = aa = a);
}


// concatenation methods:
// #### OPTIMIZE
void
sc_int_subref::concat_set(int64 src, int low_i)
{
    sc_int_base aa(length());
    *this = aa = (low_i < 64) ? src >> low_i : src >> 63;
}

void
sc_int_subref::concat_set(const sc_signed &src, int low_i)
{
    sc_int_base aa(length());
    if (low_i < src.length())
        *this = aa = src >> low_i;
    else
        *this = (src < 0) ? (int_type)-1 : 0;
}

void
sc_int_subref::concat_set(const sc_unsigned &src, int low_i)
{
    sc_int_base aa(length());
    if (low_i < src.length())
        *this = aa = src >> low_i;
    else
        *this = 0;
}

void
sc_int_subref::concat_set(uint64 src, int low_i)
{
    sc_int_base aa (length());
    *this = aa = (low_i < 64) ? src >> low_i : 0;
}


// other methods
void
sc_int_subref::scan(::std::istream &is)
{
    std::string s;
    is >> s;
    *this = s.c_str();
}


// ----------------------------------------------------------------------------
//  CLASS : sc_int_base
//
//  Base class for sc_int.
// ----------------------------------------------------------------------------

// support methods
void
sc_int_base::invalid_length() const
{
    std::stringstream msg;
    msg << "sc_int[_base] initialization: length = " << m_len <<
           " violates 1 <= length <= " << SC_INTWIDTH;
    SC_REPORT_ERROR("out of bounds", msg.str().c_str());
    sc_core::sc_abort(); // can't recover from here
}

void
sc_int_base::invalid_index(int i) const
{
    std::stringstream msg;
    msg << "sc_int[_base] bit selection: index = " << i <<
           " violates 0 <= index <= " << (m_len - 1);
    SC_REPORT_ERROR("out of bounds", msg.str().c_str());
    sc_core::sc_abort(); // can't recover from here
}

void
sc_int_base::invalid_range(int l, int r) const
{
    std::stringstream msg;
    msg << "sc_int[_base] part selection: " <<
           "left = " << l << ", right = " << r << " violates " <<
           (m_len-1) << " >= left >= right >= 0";
    SC_REPORT_ERROR("out of bounds", msg.str().c_str());
    sc_core::sc_abort(); // can't recover from here
}

void
sc_int_base::check_value() const
{
    int_type limit = (int_type)1 << (m_len - 1);
    if (m_val < -limit || m_val >= limit) {
        std::stringstream msg;
        msg << "sc_int[_base]: value does not fit into a length of " << m_len;
        SC_REPORT_WARNING("out of bounds", msg.str().c_str());
    }
}


// constructors
sc_int_base::sc_int_base(const sc_bv_base &v) :
    m_val(0), m_len(v.length()), m_ulen(SC_INTWIDTH - m_len)
{
    check_length();
    *this = v;
}
sc_int_base::sc_int_base(const sc_lv_base &v) :
    m_val(0), m_len(v.length()), m_ulen(SC_INTWIDTH - m_len)
{
    check_length();
    *this = v;
}
sc_int_base::sc_int_base(const sc_uint_subref_r &v) :
    m_val(0), m_len(v.length()), m_ulen(SC_INTWIDTH - m_len)
{
    check_length();
    *this = v.to_uint64();
}
sc_int_base::sc_int_base(const sc_signed_subref_r &v) :
    m_val(0), m_len(v.length()), m_ulen(SC_INTWIDTH - m_len)
{
    check_length();
    *this = v.to_uint64();
}
sc_int_base::sc_int_base(const sc_unsigned_subref_r &v) :
    m_val(0), m_len(v.length()), m_ulen(SC_INTWIDTH - m_len)
{
    check_length();
    *this = v.to_uint64();
}

sc_int_base::sc_int_base(const sc_signed &a) :
    m_val(0), m_len(a.length()), m_ulen(SC_INTWIDTH - m_len)
{
    check_length();
    *this = a.to_int64();
}

sc_int_base::sc_int_base(const sc_unsigned &a) :
    m_val(0), m_len(a.length()), m_ulen(SC_INTWIDTH - m_len)
{
    check_length();
    *this = a.to_int64();
}


// assignment operators
sc_int_base &
sc_int_base::operator = (const sc_signed &a)
{
    int minlen = sc_min(m_len, a.length());
    int i = 0;
    for (; i < minlen; ++i) {
        set(i, a.test(i));
    }
    bool sgn = a.sign();
    for (; i < m_len; ++i) {
        // sign extension
        set(i, sgn);
    }
    extend_sign();
    return *this;
}

sc_int_base &
sc_int_base::operator = (const sc_unsigned &a)
{
    int minlen = sc_min(m_len, a.length());
    int i = 0;
    for (; i < minlen; ++i) {
        set(i, a.test(i));
    }
    for (; i < m_len; ++i) {
        // zero extension
        set(i, 0);
    }
    extend_sign();
    return *this;
}


sc_int_base &
sc_int_base::operator = (const sc_bv_base &a)
{
    int minlen = sc_min(m_len, a.length());
    int i = 0;
    for (; i < minlen; ++i) {
        set(i, a.get_bit(i));
    }
    for (; i < m_len; ++i) {
        // zero extension
        set(i, 0);
    }
    extend_sign();
    return *this;
}

sc_int_base &
sc_int_base::operator = (const sc_lv_base &a)
{
    int minlen = sc_min(m_len, a.length());
    int i = 0;
    for (; i < minlen; ++i) {
        set(i, sc_logic(a.get_bit(i)).to_bool());
    }
    for (; i < m_len; ++i) {
        // zero extension
        set(i, 0);
    }
    extend_sign();
    return *this;
}

sc_int_base &
sc_int_base::operator = (const char *a)
{
    if (a == 0) {
        SC_REPORT_ERROR("conversion failed",
                        "character string is zero");
    } else if (*a == 0) {
        SC_REPORT_ERROR("conversion failed",
                        "character string is empty");
    } else try {
        int len = m_len;
        sc_fix aa(a, len, len, SC_TRN, SC_WRAP, 0, SC_ON);
        return this->operator = (aa);
    } catch(const sc_core::sc_report &) {
        std::stringstream msg;
        msg << "character string '" << a << "' is not valid";
        SC_REPORT_ERROR("conversion failed", msg.str().c_str());
    }
    return *this;
}

// explicit conversion to character string
const std::string
sc_int_base::to_string(sc_numrep numrep) const
{
    int len = m_len;
    sc_fix aa(*this, len, len, SC_TRN, SC_WRAP, 0, SC_ON);
    return aa.to_string(numrep);
}

const std::string
sc_int_base::to_string(sc_numrep numrep, bool w_prefix) const
{
    int len = m_len;
    sc_fix aa(*this, len, len, SC_TRN, SC_WRAP, 0, SC_ON);
    return aa.to_string(numrep, w_prefix);
}


// reduce methods
bool sc_int_base::and_reduce() const { return (m_val == int_type(-1)); }
bool sc_int_base::or_reduce() const { return (m_val != int_type(0)); }

bool
sc_int_base::xor_reduce() const
{
    uint_type mask = ~UINT_ZERO;
    uint_type val = m_val & (mask >> m_ulen);
    int n = SC_INTWIDTH;
    do {
        n >>= 1;
        mask >>= n;
        val = ((val & (mask << n)) >> n) ^ (val & mask);
    } while (n != 1);
    return (val != uint_type(0));
}

bool
sc_int_base::concat_get_ctrl(sc_digit *dst_p, int low_i) const
{
    int dst_i; // Word in dst_p now processing.
    int end_i; // Highest order word in dst_p to process.
    int left_shift; // Left shift for val.
    uint_type mask; // Mask for bits to extract or keep.

    dst_i = low_i / BITS_PER_DIGIT;
    left_shift = low_i % BITS_PER_DIGIT;
    end_i = (low_i + (m_len - 1)) / BITS_PER_DIGIT;

    mask = ~(~UINT_ZERO << left_shift);
    dst_p[dst_i] = (sc_digit)(dst_p[dst_i] & mask);
        dst_i++;
        for (; dst_i <= end_i; dst_i++)
            dst_p[dst_i] = 0;
        return false;
}

//-----------------------------------------------------------------------------
//"sc_int_base::concat_get_data"
//
// This method transfers the value of this object instance to the supplied
// array of sc_unsigned digits starting with the bit specified by low_i within
// the array of digits.
//
// Notes:
//   (1) we don't worry about masking the high order data we transfer since
//       concat_get_data() is called from low order bit to high order bit. So
//       the bits above where we place ours will be filled in by someone else.
//
//   dst_p -> array of sc_unsigned digits to be filled in.
//   low_i =  first bit within dst_p to be set.
//-----------------------------------------------------------------------------
bool
sc_int_base::concat_get_data(sc_digit *dst_p, int low_i) const
{
    int dst_i; // Word in dst_p now processing.
    int end_i; // Highest order word in dst_p to process.
    int high_i; // Index of high order bit in dst_p to set.
    int left_shift; // Left shift for val.
    uint_type mask; // Mask for bits to extract or keep.
    bool non_zero; // True if value inserted is non-zero.
    uint_type val; // Value for this object.

    dst_i = low_i / BITS_PER_DIGIT;
    left_shift = low_i % BITS_PER_DIGIT;
    high_i = low_i + (m_len - 1);
    end_i = high_i / BITS_PER_DIGIT;
    val = m_val;
    non_zero = val != 0;

    // MASK OFF DATA TO BE TRANSFERRED BASED ON WIDTH:
    if (m_len < 64) {
        mask = ~(~UINT_ZERO << m_len);
        val &=  mask;
    }

    // PROCESS THE FIRST WORD:
    mask = (~UINT_ZERO << left_shift);
    dst_p[dst_i] = (sc_digit)((dst_p[dst_i] & ~mask) |
            ((val <<left_shift) & DIGIT_MASK));
    switch (end_i - dst_i) {
      // BITS ARE ACROSS TWO WORDS:
      case 1:
        dst_i++;
        val >>= (BITS_PER_DIGIT - left_shift);
        dst_p[dst_i] = (sc_digit)val;
        break;

      // BITS ARE ACROSS THREE WORDS:
      case 2:
        dst_i++;
        val >>= (BITS_PER_DIGIT - left_shift);
        dst_p[dst_i++] = ((sc_digit)val) & DIGIT_MASK;
        val >>= BITS_PER_DIGIT;
        dst_p[dst_i] = (sc_digit)val;
        break;

      // BITS ARE ACROSS FOUR WORDS:
      case 3:
        dst_i++;
        val >>= (BITS_PER_DIGIT - left_shift);
        dst_p[dst_i++] = (sc_digit)(val & DIGIT_MASK);
        val >>= BITS_PER_DIGIT;
        dst_p[dst_i++] = (sc_digit)(val & DIGIT_MASK);
        val >>= BITS_PER_DIGIT;
        dst_p[dst_i] = (sc_digit)val;
        break;
    }
    return non_zero;
}

// #### OPTIMIZE
void
sc_int_base::concat_set(int64 src, int low_i)
{
    *this = (low_i < 64) ? src >> low_i : src >> 63;
}

void
sc_int_base::concat_set(const sc_signed &src, int low_i)
{
    if (low_i < src.length())
        *this = src >> low_i;
    else
        *this = (src < 0) ? (int_type)-1 : 0;
}

void
sc_int_base::concat_set(const sc_unsigned &src, int low_i)
{
    if (low_i < src.length())
        *this = src >> low_i;
    else
        *this = 0;
}

void
sc_int_base::concat_set(uint64 src, int low_i)
{
    *this = (low_i < 64) ? src >> low_i : 0;
}

// other methods
void
sc_int_base::scan(::std::istream &is)
{
    std::string s;
    is >> s;
    *this = s.c_str();
}

} // namespace sc_dt;