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  scfx_utils.cpp -
23
24  Original Author: Martin Janssen, Synopsys, Inc.
25
26 *****************************************************************************/
27
28/*****************************************************************************
29
30  MODIFICATION LOG - modifiers, enter your name, affiliation, date and
31  changes you are making here.
32
33      Name, Affiliation, Date:
34  Description of Modification:
35
36 *****************************************************************************/
37
38
39// $Log: scfx_utils.cpp,v $
40// Revision 1.1.1.1  2006/12/15 20:20:04  acg
41// SystemC 2.3
42//
43// Revision 1.3  2006/01/13 18:53:58  acg
44// Andy Goodrich: added $Log command so that CVS comments are reproduced in
45// the source.
46//
47
48#include "systemc/ext/dt/fx/scfx_utils.hh"
49
50namespace sc_dt
51{
52
53void
54scfx_tc2csd(scfx_string &s, int w_prefix)
55{
56    if (w_prefix != 0) {
57        SC_ASSERT_(s[0] == '0' && s[1] == 'c' &&
58                   s[2] == 's' && s[3] == 'd', "invalid prefix");
59    }
60
61    scfx_string csd;
62
63    // copy bits from 's' into 'csd'; skip prefix, point, and exponent
64    int i = 0;
65    int j = (w_prefix != 0 ? 4 : 0);
66    while (s[j]) {
67        if (s[j] == '0' || s[j] == '1')
68            csd[i ++] = s[j];
69        else if (s[j] != '.')
70            break;
71        ++j;
72    }
73    csd[i] = '\0';
74
75    // convert 'csd' from two's complement to csd
76    --i;
77    while (i >= 0) {
78        if (csd[i] == '0') {
79            --i;
80        } else {
81            if (i > 0 && csd[i - 1] == '0') {
82                --i;
83            } else if (i == 0) {
84                csd[i--] = '-';
85            } else {
86                csd[i--] = '-';
87                while (i >= 0 && csd[i] == '1')
88                    csd[i--] = '0';
89                if (i > 0)
90                    csd[i] = '1';
91                else if (i == 0)
92                    csd[i--] = '1';
93            }
94        }
95    }
96
97    // copy bits from 'csd' back into 's'
98    i = 0;
99    j = (w_prefix != 0 ? 4 : 0);
100    while (csd[i]) {
101        if (s[j] == '.')
102            ++j;
103        s[j++] = csd[i++];
104    }
105}
106
107
108void
109scfx_csd2tc(scfx_string &csd)
110{
111    SC_ASSERT_(csd[0] == '0' && csd[1] == 'c' &&
112               csd[2] == 's' && csd[3] == 'd', "invalid prefix");
113
114    scfx_string s;
115
116    // copy bits from 'csd' into 's'; skip prefix, point, and exponent
117    int i = 0;
118    s[i++] = '0';
119    int j = 4;
120    while (csd[j]) {
121        if (csd[j] == '-' || csd[j] == '0' || csd[j] == '1')
122            s[i++] = csd[j];
123        else if (csd[j] != '.')
124            break;
125        ++j;
126    }
127    s[i] = '\0';
128
129    // convert 's' from csd to two's complement
130    int len = i;
131    i = 1;
132    while (i < len) {
133        while (i < len && s[i] != '-')
134            i++;
135        if (i < len) {
136            j = i++;
137            s[j--] = '1';
138            while (j >= 0 && s[j] == '0')
139                s[j--] = '1';
140            if (j >= 0)
141                s[j] = '0';
142        }
143    }
144
145    // copy bits from 's' back into 'csd'
146    j = csd.length();
147    csd[j + 1] = '\0';
148    while (j > 4) {
149        csd[j] = csd[j - 1];
150        --j;
151    }
152
153    i = 0;
154    j = 4;
155    while (s[i]) {
156        if (csd[j] == '.')
157            ++j;
158        csd[j++] = s[i++];
159    }
160}
161
162} // namespace sc_dt
163