📄 oaverilogvalue.cpp
字号:
// *****************************************************************************// *****************************************************************************// oaVerilogValue.cpp//// This file contains the implementation for the Value and// ValueList classes.//// *****************************************************************************// Except as specified in the OpenAccess terms of use of Cadence or Silicon// Integration Initiative, this material may not be copied, modified,// re-published, uploaded, executed, or distributed in any way, in any medium,// in whole or in part, without prior written permission from Cadence.//// Copyright 2003-2005 Cadence Design Systems, Inc.// All Rights Reserved.//// $Author: sailajad $// $Revision: 1.22 $// $Date: 2005/07/17 21:07:32 $// $State: Exp $// *****************************************************************************// *****************************************************************************#include "oaVerilogInPvt.h"BEGIN_VERILOG_NAMESPACE// *****************************************************************************// Value::Value()//// These are the constructors for the Value class. They intialize the// Value given an argument that is one of several different types.// *****************************************************************************Value::Value(): type(DecimalValue), numBits(32), sval(7){}Value::Value(oaInt4 init): type(DecimalValue), numBits(32), sval(64){ sval.format("%d", init);}Value::Value(oaUInt4 init): type(DecimalValue), numBits(32), sval(64){ sval.format("%d", init);}Value::Value(const oaString &init): type(StringValue), numBits(0), sval(init){ if (isNumber(sval)) { type = NumAsStringValue; numBits = calcNumBits(type, sval); }}Value::Value(const oaString &initStr, ValueTypeEnum initType, oaUInt4 initNumBits): type(initType), numBits(initNumBits), sval(initStr){}Value::Value(const Value &init): type(init.getType()), numBits(init.getNumBits()), sval(init.getString()){}// *****************************************************************************// Value::operator=()//// These are the assignment operators that handle assigning a Value, // oaInt4, oaDouble, oaString, or a char* to a Value object.// *****************************************************************************Value&Value::operator=(const Value &rhs){ if (&rhs != this) { sval = rhs.getString(); numBits = rhs.getNumBits(); type = rhs.getType(); } return *this;}Value&Value::operator=(const oaInt4 rhs){ sval.format(64, "%d", rhs); numBits = 32; type = DecimalValue; return *this;}Value&Value::operator=(const oaUInt4 rhs){ sval.format(64, "%d", rhs); numBits = 32; type = DecimalValue; return *this;}Value&Value::operator=(const oaDouble rhs){ sval.format(64, "%e", rhs); numBits = 0; type = DoubleValue; return *this;}Value&Value::operator=(const oaString &rhs){ sval = rhs; numBits = 0; type = StringValue; return *this;}Value&Value::operator=(const char *rhs){ sval = rhs; numBits = 0; type = StringValue; return *this;}// *****************************************************************************// Value::testBit()//// This function returns true if the bit at the given index is 1 and false// otherwise. If the value is not a BinaryValue, then the value will be// converted to a BinaryValue value. Binary values are 0 indexed with the least// signigicant bit occupying the n'th byte of the sval. This function assumes // that the type is not a string or a floating point number.// *****************************************************************************oaBooleanValue::testBit(oaUInt4 index){ if (type != PaddedBinaryValue) { convertToBinary(oaString(sval), numBits, type, sval, numBits, type); } return ((const char*) sval)[numBits - index - 1] == '1';}// *****************************************************************************// Value::convertToBinary()//// This function converts the Value to a binary representation.// *****************************************************************************voidValue::convertToBinary(const oaString &input, const oaUInt4 numInputBits, const ValueTypeEnum inputType, oaString &output, oaUInt4 &numOutputBits, ValueTypeEnum &outputType){ switch (inputType) { case StringValue: case NumAsStringValue: stringToBinary(input, numInputBits, output, numOutputBits); break; case DecimalValue: decimalToBinary(input, numInputBits, output); numOutputBits = output.getLength(); break; case OctalValue: octalToBinary(input, numInputBits, output); numOutputBits = output.getLength(); break; case HexValue: hexToBinary(input, numInputBits, output); numOutputBits = output.getLength(); break; case BinaryValue: numOutputBits = output.getLength(); break; case DoubleValue: throw Error(ConvertDoubleToBinary, NULL, (const char*) input); } if (numOutputBits < numInputBits) { oaUInt4 numZeros = numInputBits - numOutputBits; oaString zeros(numZeros); for (oaUInt4 i = 0; i < numZeros; i++) { zeros += "0"; } output = zeros + output; numOutputBits = numInputBits; } outputType = PaddedBinaryValue;}// *****************************************************************************// Value::stringToBinary()//// This function converts a string into a binary number. If the string does not// contain a Verilog base token or if is not a number, then this function // converts the string to a 1'b0.// *****************************************************************************voidValue::stringToBinary(const oaString &input, const oaUInt4 numInputBits, oaString &output, oaUInt4 &numOutputBits){ oaUInt4 baseIdx = input.index('\''); ValueTypeEnum type; if (baseIdx != input.getLength() && baseIdx != input.getLength() - 1) { oaChar *buf = new oaChar[input.getLength() - baseIdx - 1]; strcpy(buf, ((const char*) input) + baseIdx + 2); type = calcType(oaChar(input[baseIdx + 1])); numOutputBits = calcNumBits(type, buf); output = buf; delete [] buf; } else if (isNumber(input)) { type = DecimalValue; numOutputBits = 32; output = input; } else { output = "0"; numOutputBits = 1; return; } convertToBinary(oaString(output), numOutputBits, type, output, numOutputBits, type);}// *****************************************************************************// Value::calcType()//// This function calculates the type of this value based on the given character.// *****************************************************************************ValueTypeEnumValue::calcType(const oaChar typeChar){ switch (typeChar) { case 'd': case 'D': return DecimalValue; case 'b': case 'B': return BinaryValue; case 'o': case 'O': return OctalValue; case 'h': case 'H': return HexValue; default: return StringValue; }}// *****************************************************************************// Value::calcNumBits()//// This function calculates the number of bits that this value represents. If // the number of bits is given before the base specification, then that is the// value that is used as the number of bits. Otherwise, the number of bits is// computed based on the type of the value and the length of the string that is// representing the value.// *****************************************************************************oaUInt4Value::calcNumBits(ValueTypeEnum valueType, const oaString &valueString){ oaUInt4 bits; oaUInt4 baseIdx = valueString.index('\''); if (baseIdx != valueString.getLength() && baseIdx != valueString.getLength() - 1) { if (baseIdx != 0) { oaChar *numBitsBuf = new oaChar[baseIdx + 1]; strncpy(numBitsBuf, valueString, baseIdx); numBitsBuf[baseIdx] = '\0'; bits = atoi(numBitsBuf); delete [] numBitsBuf; } else { oaString buf(((const char*) valueString) + baseIdx + 2); return calcNumBits(calcType(valueString[baseIdx + 1]), buf); } } else { switch (valueType) { case DecimalValue: bits = 32; break; case HexValue: bits = valueString.getLength() * 4; break; case OctalValue: bits = valueString.getLength() * 3;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -