2022-09-18 05:27:47 +00:00
|
|
|
/*
|
|
|
|
* This program source code file is part of KiCad, a free EDA CAD application.
|
|
|
|
*
|
|
|
|
* Copyright (C) 2022 Mikolaj Wielgus
|
2023-02-22 09:10:06 +00:00
|
|
|
* Copyright (C) 2022-2023 KiCad Developers, see AUTHORS.txt for contributors.
|
2022-09-18 05:27:47 +00:00
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU General Public License
|
|
|
|
* as published by the Free Software Foundation; either version 3
|
|
|
|
* of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; if not, you may find one here:
|
|
|
|
* https://www.gnu.org/licenses/gpl-3.0.html
|
|
|
|
* or you may search the http://www.gnu.org website for the version 3 license,
|
|
|
|
* or you may write to the Free Software Foundation, Inc.,
|
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <sim/sim_model_raw_spice.h>
|
2022-12-21 00:38:52 +00:00
|
|
|
#include <sim/sim_model_serializer.h>
|
2022-09-22 05:38:45 +00:00
|
|
|
|
|
|
|
#include <boost/algorithm/string/predicate.hpp>
|
|
|
|
#include <fmt/core.h>
|
2022-09-18 05:27:47 +00:00
|
|
|
#include <pegtl/contrib/parse_tree.hpp>
|
|
|
|
|
|
|
|
|
|
|
|
namespace SIM_MODEL_RAW_SPICE_PARSER
|
|
|
|
{
|
2022-12-21 00:38:52 +00:00
|
|
|
using namespace SIM_MODEL_SERIALIZER_GRAMMAR;
|
2022-09-18 05:27:47 +00:00
|
|
|
|
|
|
|
template <typename Rule> struct legacyPinSequenceSelector : std::false_type {};
|
|
|
|
template <> struct legacyPinSequenceSelector<legacyPinNumber> : std::true_type {};
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-10-10 23:23:00 +00:00
|
|
|
std::string SPICE_GENERATOR_RAW_SPICE::ModelLine( const SPICE_ITEM& aItem ) const
|
2022-09-18 05:27:47 +00:00
|
|
|
{
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-10-10 23:23:00 +00:00
|
|
|
std::string SPICE_GENERATOR_RAW_SPICE::ItemName( const SPICE_ITEM& aItem ) const
|
2022-09-18 05:27:47 +00:00
|
|
|
{
|
2023-02-22 09:10:06 +00:00
|
|
|
std::string type = m_model.GetParam( (int) SIM_MODEL_RAW_SPICE::SPICE_PARAM::TYPE ).value;
|
2022-09-18 05:27:47 +00:00
|
|
|
|
2023-02-22 09:10:06 +00:00
|
|
|
if( aItem.refName != "" && boost::starts_with( aItem.refName, type ) )
|
2022-10-10 23:23:00 +00:00
|
|
|
return aItem.refName;
|
2022-09-18 05:27:47 +00:00
|
|
|
else
|
2023-02-22 09:10:06 +00:00
|
|
|
return fmt::format( "{}{}", type, aItem.refName );
|
2022-09-18 05:27:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-10-10 23:23:00 +00:00
|
|
|
std::string SPICE_GENERATOR_RAW_SPICE::ItemPins( const SPICE_ITEM& aItem ) const
|
2022-09-18 05:27:47 +00:00
|
|
|
{
|
2022-09-22 05:38:45 +00:00
|
|
|
std::string result;
|
2023-02-02 12:44:13 +00:00
|
|
|
int ncCounter = 0;
|
2022-09-18 05:27:47 +00:00
|
|
|
|
2023-01-14 02:43:34 +00:00
|
|
|
if( !GetPins().empty() )
|
2022-09-18 05:27:47 +00:00
|
|
|
{
|
2024-05-10 19:53:30 +00:00
|
|
|
for( const SIM_MODEL_PIN& pin : GetPins() )
|
2022-09-18 05:27:47 +00:00
|
|
|
{
|
2023-01-14 02:43:34 +00:00
|
|
|
auto it = std::find( aItem.pinNumbers.begin(), aItem.pinNumbers.end(),
|
|
|
|
pin.symbolPinNumber );
|
|
|
|
|
|
|
|
if( it != aItem.pinNumbers.end() )
|
|
|
|
{
|
|
|
|
long symbolPinIndex = std::distance( aItem.pinNumbers.begin(), it );
|
|
|
|
result.append( fmt::format( " {}", aItem.pinNetNames.at( symbolPinIndex ) ) );
|
|
|
|
}
|
2023-02-02 12:44:13 +00:00
|
|
|
else
|
|
|
|
{
|
|
|
|
result.append( fmt::format( " NC-{}-{}", aItem.refName, ncCounter++ ) );
|
|
|
|
}
|
2022-09-18 05:27:47 +00:00
|
|
|
}
|
|
|
|
}
|
2023-01-14 02:43:34 +00:00
|
|
|
else
|
|
|
|
{
|
|
|
|
// If we don't know what pins the model has, just output the symbol's pins
|
|
|
|
|
|
|
|
for( const std::string& pinNetName : aItem.pinNetNames )
|
|
|
|
result.append( fmt::format( " {}", pinNetName ) );
|
|
|
|
}
|
2022-09-18 05:27:47 +00:00
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-10-10 23:23:00 +00:00
|
|
|
std::string SPICE_GENERATOR_RAW_SPICE::ItemModelName( const SPICE_ITEM& aItem ) const
|
2022-09-18 05:27:47 +00:00
|
|
|
{
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-09-22 05:38:45 +00:00
|
|
|
std::string SPICE_GENERATOR_RAW_SPICE::ItemParams() const
|
2022-09-18 05:27:47 +00:00
|
|
|
{
|
2022-09-22 05:38:45 +00:00
|
|
|
std::string result;
|
2022-09-18 05:27:47 +00:00
|
|
|
|
|
|
|
for( const SIM_MODEL::PARAM& param : GetInstanceParams() )
|
|
|
|
{
|
2022-09-22 05:38:45 +00:00
|
|
|
if( param.info.name == "model" )
|
2023-03-03 18:17:42 +00:00
|
|
|
result.append( " " + SIM_VALUE::ToSpice( param.value ) );
|
2022-09-18 05:27:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-10-10 23:23:00 +00:00
|
|
|
std::string SPICE_GENERATOR_RAW_SPICE::Preview( const SPICE_ITEM& aItem ) const
|
2022-09-18 05:27:47 +00:00
|
|
|
{
|
2022-10-10 23:23:00 +00:00
|
|
|
SPICE_ITEM item = aItem;
|
|
|
|
item.refName = "";
|
2022-09-18 05:27:47 +00:00
|
|
|
|
|
|
|
for( int i = 0; i < m_model.GetPinCount(); ++i )
|
|
|
|
{
|
2022-10-10 23:23:00 +00:00
|
|
|
item.pinNumbers.push_back( fmt::format( "{}", i + 1 ) );
|
|
|
|
item.pinNetNames.push_back( fmt::format( "{}", i + 1 ) );
|
2022-09-18 05:27:47 +00:00
|
|
|
}
|
|
|
|
|
2022-10-10 23:23:00 +00:00
|
|
|
return ItemLine( item );
|
2022-09-18 05:27:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-12-31 18:51:47 +00:00
|
|
|
SIM_MODEL_RAW_SPICE::SIM_MODEL_RAW_SPICE( const std::string& aSpiceSource ) :
|
|
|
|
SIM_MODEL( TYPE::RAWSPICE, std::make_unique<SPICE_GENERATOR_RAW_SPICE>( *this ) ),
|
|
|
|
m_spiceCode( aSpiceSource )
|
2022-09-18 05:27:47 +00:00
|
|
|
{
|
|
|
|
static std::vector<PARAM::INFO> paramInfos = makeParamInfos();
|
|
|
|
|
|
|
|
for( const PARAM::INFO& paramInfo : paramInfos )
|
|
|
|
AddParam( paramInfo );
|
2022-12-31 18:51:47 +00:00
|
|
|
|
|
|
|
SetParamValue( "model", aSpiceSource );
|
2022-09-18 05:27:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2024-05-10 19:53:30 +00:00
|
|
|
void SIM_MODEL_RAW_SPICE::AssignSymbolPinNumberToModelPin( const std::string& aModelPinName,
|
|
|
|
const wxString& aSymbolPinNumber )
|
2022-09-18 05:27:47 +00:00
|
|
|
{
|
2024-05-10 19:53:30 +00:00
|
|
|
// SPICE doesn't name model inputs so we have to assume they're indexes here.
|
|
|
|
int pinIndex = (int) strtol( aModelPinName.c_str(), nullptr, 10 );
|
|
|
|
|
|
|
|
if( pinIndex > 0 )
|
2022-12-22 20:21:10 +00:00
|
|
|
{
|
2024-05-10 16:54:32 +00:00
|
|
|
while( (int)m_modelPins.size() < pinIndex )
|
2024-05-10 19:53:30 +00:00
|
|
|
m_modelPins.push_back( { fmt::format( "{}", m_modelPins.size() + 1 ), wxEmptyString } );
|
2022-12-22 20:21:10 +00:00
|
|
|
|
2024-05-10 19:53:30 +00:00
|
|
|
m_modelPins[ --pinIndex /* convert to 0-based */ ].symbolPinNumber = aSymbolPinNumber;
|
|
|
|
}
|
2022-09-18 05:27:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
std::vector<SIM_MODEL::PARAM::INFO> SIM_MODEL_RAW_SPICE::makeParamInfos()
|
|
|
|
{
|
|
|
|
std::vector<PARAM::INFO> paramInfos;
|
|
|
|
|
|
|
|
for( SPICE_PARAM spiceParam : SPICE_PARAM_ITERATOR() )
|
|
|
|
{
|
|
|
|
PARAM::INFO paramInfo;
|
|
|
|
|
|
|
|
switch( spiceParam )
|
|
|
|
{
|
|
|
|
case SPICE_PARAM::TYPE:
|
|
|
|
paramInfo.name = "type";
|
|
|
|
paramInfo.type = SIM_VALUE::TYPE_STRING;
|
|
|
|
paramInfo.unit = "";
|
|
|
|
paramInfo.category = SIM_MODEL::PARAM::CATEGORY::PRINCIPAL;
|
|
|
|
paramInfo.defaultValue = "";
|
|
|
|
paramInfo.description = "Spice element type";
|
|
|
|
paramInfo.isSpiceInstanceParam = true;
|
|
|
|
|
|
|
|
paramInfos.push_back( paramInfo );
|
|
|
|
break;
|
|
|
|
|
|
|
|
case SPICE_PARAM::MODEL:
|
|
|
|
paramInfo.name = "model";
|
|
|
|
paramInfo.type = SIM_VALUE::TYPE_STRING;
|
|
|
|
paramInfo.unit = "";
|
|
|
|
paramInfo.category = SIM_MODEL::PARAM::CATEGORY::PRINCIPAL;
|
|
|
|
paramInfo.defaultValue = "";
|
|
|
|
paramInfo.description = "Model name or value";
|
|
|
|
paramInfo.isSpiceInstanceParam = true;
|
|
|
|
|
|
|
|
paramInfos.push_back( paramInfo );
|
|
|
|
break;
|
|
|
|
|
|
|
|
case SPICE_PARAM::LIB:
|
|
|
|
paramInfo.name = "lib";
|
|
|
|
paramInfo.type = SIM_VALUE::TYPE_STRING;
|
|
|
|
paramInfo.unit = "";
|
|
|
|
paramInfo.category = SIM_MODEL::PARAM::CATEGORY::PRINCIPAL;
|
|
|
|
paramInfo.defaultValue = "";
|
|
|
|
paramInfo.description = "Library path to include";
|
|
|
|
paramInfo.isSpiceInstanceParam = true;
|
|
|
|
|
|
|
|
paramInfos.push_back( paramInfo );
|
|
|
|
break;
|
|
|
|
|
|
|
|
case SPICE_PARAM::_ENUM_END:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return paramInfos;
|
|
|
|
}
|