112854Sgabeblack@google.com/*****************************************************************************
212854Sgabeblack@google.com
312854Sgabeblack@google.com  Licensed to Accellera Systems Initiative Inc. (Accellera) under one or
412854Sgabeblack@google.com  more contributor license agreements.  See the NOTICE file distributed
512854Sgabeblack@google.com  with this work for additional information regarding copyright ownership.
612854Sgabeblack@google.com  Accellera licenses this file to you under the Apache License, Version 2.0
712854Sgabeblack@google.com  (the "License"); you may not use this file except in compliance with the
812854Sgabeblack@google.com  License.  You may obtain a copy of the License at
912854Sgabeblack@google.com
1012854Sgabeblack@google.com    http://www.apache.org/licenses/LICENSE-2.0
1112854Sgabeblack@google.com
1212854Sgabeblack@google.com  Unless required by applicable law or agreed to in writing, software
1312854Sgabeblack@google.com  distributed under the License is distributed on an "AS IS" BASIS,
1412854Sgabeblack@google.com  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
1512854Sgabeblack@google.com  implied.  See the License for the specific language governing
1612854Sgabeblack@google.com  permissions and limitations under the License.
1712854Sgabeblack@google.com
1812854Sgabeblack@google.com *****************************************************************************/
1912854Sgabeblack@google.com
2012854Sgabeblack@google.com/*****************************************************************************
2112854Sgabeblack@google.com
2212854Sgabeblack@google.com  scfx_utils.cpp -
2312854Sgabeblack@google.com
2412854Sgabeblack@google.com  Original Author: Martin Janssen, Synopsys, Inc.
2512854Sgabeblack@google.com
2612854Sgabeblack@google.com *****************************************************************************/
2712854Sgabeblack@google.com
2812854Sgabeblack@google.com/*****************************************************************************
2912854Sgabeblack@google.com
3012854Sgabeblack@google.com  MODIFICATION LOG - modifiers, enter your name, affiliation, date and
3112854Sgabeblack@google.com  changes you are making here.
3212854Sgabeblack@google.com
3312854Sgabeblack@google.com      Name, Affiliation, Date:
3412854Sgabeblack@google.com  Description of Modification:
3512854Sgabeblack@google.com
3612854Sgabeblack@google.com *****************************************************************************/
3712854Sgabeblack@google.com
3812854Sgabeblack@google.com
3912854Sgabeblack@google.com// $Log: scfx_utils.cpp,v $
4012854Sgabeblack@google.com// Revision 1.1.1.1  2006/12/15 20:20:04  acg
4112854Sgabeblack@google.com// SystemC 2.3
4212854Sgabeblack@google.com//
4312854Sgabeblack@google.com// Revision 1.3  2006/01/13 18:53:58  acg
4412854Sgabeblack@google.com// Andy Goodrich: added $Log command so that CVS comments are reproduced in
4512854Sgabeblack@google.com// the source.
4612854Sgabeblack@google.com//
4712854Sgabeblack@google.com
4812854Sgabeblack@google.com#include "systemc/ext/dt/fx/scfx_utils.hh"
4912854Sgabeblack@google.com
5012854Sgabeblack@google.comnamespace sc_dt
5112854Sgabeblack@google.com{
5212854Sgabeblack@google.com
5312854Sgabeblack@google.comvoid
5412854Sgabeblack@google.comscfx_tc2csd(scfx_string &s, int w_prefix)
5512854Sgabeblack@google.com{
5612854Sgabeblack@google.com    if (w_prefix != 0) {
5712854Sgabeblack@google.com        SC_ASSERT_(s[0] == '0' && s[1] == 'c' &&
5812854Sgabeblack@google.com                   s[2] == 's' && s[3] == 'd', "invalid prefix");
5912854Sgabeblack@google.com    }
6012854Sgabeblack@google.com
6112854Sgabeblack@google.com    scfx_string csd;
6212854Sgabeblack@google.com
6312854Sgabeblack@google.com    // copy bits from 's' into 'csd'; skip prefix, point, and exponent
6412854Sgabeblack@google.com    int i = 0;
6512854Sgabeblack@google.com    int j = (w_prefix != 0 ? 4 : 0);
6612854Sgabeblack@google.com    while (s[j]) {
6712854Sgabeblack@google.com        if (s[j] == '0' || s[j] == '1')
6812854Sgabeblack@google.com            csd[i ++] = s[j];
6912854Sgabeblack@google.com        else if (s[j] != '.')
7012854Sgabeblack@google.com            break;
7112854Sgabeblack@google.com        ++j;
7212854Sgabeblack@google.com    }
7312854Sgabeblack@google.com    csd[i] = '\0';
7412854Sgabeblack@google.com
7512854Sgabeblack@google.com    // convert 'csd' from two's complement to csd
7612854Sgabeblack@google.com    --i;
7712854Sgabeblack@google.com    while (i >= 0) {
7812854Sgabeblack@google.com        if (csd[i] == '0') {
7912854Sgabeblack@google.com            --i;
8012854Sgabeblack@google.com        } else {
8112854Sgabeblack@google.com            if (i > 0 && csd[i - 1] == '0') {
8212854Sgabeblack@google.com                --i;
8312854Sgabeblack@google.com            } else if (i == 0) {
8412854Sgabeblack@google.com                csd[i--] = '-';
8512854Sgabeblack@google.com            } else {
8612854Sgabeblack@google.com                csd[i--] = '-';
8712854Sgabeblack@google.com                while (i >= 0 && csd[i] == '1')
8812854Sgabeblack@google.com                    csd[i--] = '0';
8912854Sgabeblack@google.com                if (i > 0)
9012854Sgabeblack@google.com                    csd[i] = '1';
9112854Sgabeblack@google.com                else if (i == 0)
9212854Sgabeblack@google.com                    csd[i--] = '1';
9312854Sgabeblack@google.com            }
9412854Sgabeblack@google.com        }
9512854Sgabeblack@google.com    }
9612854Sgabeblack@google.com
9712854Sgabeblack@google.com    // copy bits from 'csd' back into 's'
9812854Sgabeblack@google.com    i = 0;
9912854Sgabeblack@google.com    j = (w_prefix != 0 ? 4 : 0);
10012854Sgabeblack@google.com    while (csd[i]) {
10112854Sgabeblack@google.com        if (s[j] == '.')
10212854Sgabeblack@google.com            ++j;
10312854Sgabeblack@google.com        s[j++] = csd[i++];
10412854Sgabeblack@google.com    }
10512854Sgabeblack@google.com}
10612854Sgabeblack@google.com
10712854Sgabeblack@google.com
10812854Sgabeblack@google.comvoid
10912854Sgabeblack@google.comscfx_csd2tc(scfx_string &csd)
11012854Sgabeblack@google.com{
11112854Sgabeblack@google.com    SC_ASSERT_(csd[0] == '0' && csd[1] == 'c' &&
11212854Sgabeblack@google.com               csd[2] == 's' && csd[3] == 'd', "invalid prefix");
11312854Sgabeblack@google.com
11412854Sgabeblack@google.com    scfx_string s;
11512854Sgabeblack@google.com
11612854Sgabeblack@google.com    // copy bits from 'csd' into 's'; skip prefix, point, and exponent
11712854Sgabeblack@google.com    int i = 0;
11812854Sgabeblack@google.com    s[i++] = '0';
11912854Sgabeblack@google.com    int j = 4;
12012854Sgabeblack@google.com    while (csd[j]) {
12112854Sgabeblack@google.com        if (csd[j] == '-' || csd[j] == '0' || csd[j] == '1')
12212854Sgabeblack@google.com            s[i++] = csd[j];
12312854Sgabeblack@google.com        else if (csd[j] != '.')
12412854Sgabeblack@google.com            break;
12512854Sgabeblack@google.com        ++j;
12612854Sgabeblack@google.com    }
12712854Sgabeblack@google.com    s[i] = '\0';
12812854Sgabeblack@google.com
12912854Sgabeblack@google.com    // convert 's' from csd to two's complement
13012854Sgabeblack@google.com    int len = i;
13112854Sgabeblack@google.com    i = 1;
13212854Sgabeblack@google.com    while (i < len) {
13312854Sgabeblack@google.com        while (i < len && s[i] != '-')
13412854Sgabeblack@google.com            i++;
13512854Sgabeblack@google.com        if (i < len) {
13612854Sgabeblack@google.com            j = i++;
13712854Sgabeblack@google.com            s[j--] = '1';
13812854Sgabeblack@google.com            while (j >= 0 && s[j] == '0')
13912854Sgabeblack@google.com                s[j--] = '1';
14012854Sgabeblack@google.com            if (j >= 0)
14112854Sgabeblack@google.com                s[j] = '0';
14212854Sgabeblack@google.com        }
14312854Sgabeblack@google.com    }
14412854Sgabeblack@google.com
14512854Sgabeblack@google.com    // copy bits from 's' back into 'csd'
14612854Sgabeblack@google.com    j = csd.length();
14712854Sgabeblack@google.com    csd[j + 1] = '\0';
14812854Sgabeblack@google.com    while (j > 4) {
14912854Sgabeblack@google.com        csd[j] = csd[j - 1];
15012854Sgabeblack@google.com        --j;
15112854Sgabeblack@google.com    }
15212854Sgabeblack@google.com
15312854Sgabeblack@google.com    i = 0;
15412854Sgabeblack@google.com    j = 4;
15512854Sgabeblack@google.com    while (s[i]) {
15612854Sgabeblack@google.com        if (csd[j] == '.')
15712854Sgabeblack@google.com            ++j;
15812854Sgabeblack@google.com        csd[j++] = s[i++];
15912854Sgabeblack@google.com    }
16012854Sgabeblack@google.com}
16112854Sgabeblack@google.com
16212854Sgabeblack@google.com} // namespace sc_dt
163