2022-09-15 03:25:57 +00:00
|
|
|
/*
|
|
|
|
* This program source code file is part of KiCad, a free EDA CAD application.
|
|
|
|
*
|
|
|
|
* Copyright (C) 2022 Mikolaj Wielgus
|
|
|
|
* Copyright (C) 2022 KiCad Developers, see AUTHORS.txt for contributors.
|
|
|
|
*
|
|
|
|
* 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/spice_generator.h>
|
|
|
|
|
2022-09-22 05:38:45 +00:00
|
|
|
#include <boost/algorithm/string/predicate.hpp>
|
|
|
|
#include <boost/algorithm/string/trim.hpp>
|
|
|
|
#include <fmt/core.h>
|
2022-09-15 03:25:57 +00:00
|
|
|
|
|
|
|
|
2022-10-10 23:23:00 +00:00
|
|
|
std::string SPICE_GENERATOR::ModelName( const SPICE_ITEM& aItem ) const
|
2022-10-10 11:35:18 +00:00
|
|
|
{
|
2022-10-10 23:23:00 +00:00
|
|
|
if( aItem.baseModelName == "" )
|
|
|
|
return fmt::format( "__{}", aItem.refName );
|
2022-10-10 11:35:18 +00:00
|
|
|
|
|
|
|
// FIXME: This ModelLine() call is relatively expensive.
|
2022-10-10 23:23:00 +00:00
|
|
|
if( ModelLine( aItem ) != "" )
|
|
|
|
return fmt::format( "{}.{}", aItem.refName, aItem.baseModelName );
|
2022-10-10 11:35:18 +00:00
|
|
|
|
2022-10-10 23:23:00 +00:00
|
|
|
return aItem.baseModelName;
|
2022-10-10 11:35:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-10-10 23:23:00 +00:00
|
|
|
std::string SPICE_GENERATOR::ModelLine( const SPICE_ITEM& aItem ) const
|
2022-09-22 05:38:45 +00:00
|
|
|
{
|
2022-09-15 03:25:57 +00:00
|
|
|
if( !m_model.HasSpiceNonInstanceOverrides() && !m_model.requiresSpiceModelLine() )
|
|
|
|
return "";
|
|
|
|
|
2022-12-14 13:11:30 +00:00
|
|
|
std::string result;
|
2022-09-15 03:25:57 +00:00
|
|
|
|
2022-10-10 23:23:00 +00:00
|
|
|
result.append( fmt::format( ".model {} ", aItem.modelName ) );
|
2022-09-22 05:38:45 +00:00
|
|
|
size_t indentLength = result.length();
|
2022-09-15 03:25:57 +00:00
|
|
|
|
2022-09-22 05:38:45 +00:00
|
|
|
result.append( fmt::format( "{}\n", m_model.GetSpiceInfo().modelType ) );
|
2022-09-15 03:25:57 +00:00
|
|
|
|
|
|
|
for( const SIM_MODEL::PARAM& param : m_model.GetParams() )
|
|
|
|
{
|
|
|
|
if( param.info.isSpiceInstanceParam )
|
|
|
|
continue;
|
|
|
|
|
2022-12-14 13:11:30 +00:00
|
|
|
std::string name;
|
|
|
|
std::string value;
|
|
|
|
|
|
|
|
if( !param.info.spiceModelName.empty() )
|
|
|
|
{
|
|
|
|
name = param.info.spiceModelName;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// Because of collisions with instance parameters, we append some model parameters
|
|
|
|
// with "_".
|
|
|
|
if( boost::ends_with( param.info.name, "_" ) )
|
2022-12-23 12:26:33 +00:00
|
|
|
name = param.info.name.substr( 0, param.info.name.length() - 1 );
|
2022-12-14 13:11:30 +00:00
|
|
|
else
|
|
|
|
name = param.info.name;
|
|
|
|
}
|
|
|
|
|
|
|
|
value = param.value->ToSpiceString();
|
2022-09-15 03:25:57 +00:00
|
|
|
|
|
|
|
if( value == "" )
|
|
|
|
continue;
|
|
|
|
|
2022-12-14 13:11:30 +00:00
|
|
|
result.append( fmt::format( "+{}{}={}\n",
|
|
|
|
std::string( indentLength - 1, ' ' ),
|
|
|
|
name,
|
|
|
|
value ) );
|
2022-09-15 03:25:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-10-10 23:23:00 +00:00
|
|
|
std::string SPICE_GENERATOR::ItemLine( const SPICE_ITEM& aItem ) const
|
2022-09-15 03:25:57 +00:00
|
|
|
{
|
2022-10-10 23:23:00 +00:00
|
|
|
SPICE_ITEM item = aItem;
|
2022-09-15 03:25:57 +00:00
|
|
|
|
2022-10-10 23:23:00 +00:00
|
|
|
if( item.pinNumbers.empty() )
|
|
|
|
{
|
|
|
|
for( int i = 0; i < m_model.GetPinCount(); ++i )
|
|
|
|
item.pinNumbers.push_back( fmt::format( "{}", i + 1 ) );
|
|
|
|
}
|
2022-09-15 03:25:57 +00:00
|
|
|
|
2022-10-10 23:23:00 +00:00
|
|
|
if( item.pinNetNames.empty() )
|
|
|
|
{
|
|
|
|
for( const SIM_MODEL::PIN& pin : GetPins() )
|
|
|
|
item.pinNetNames.push_back( pin.name );
|
|
|
|
}
|
2022-09-15 03:25:57 +00:00
|
|
|
|
2022-09-22 05:38:45 +00:00
|
|
|
std::string result;
|
2022-10-10 23:23:00 +00:00
|
|
|
result.append( ItemName( aItem ) );
|
|
|
|
result.append( ItemPins( aItem ) );
|
|
|
|
result.append( ItemModelName( aItem ) );
|
2022-09-22 05:38:45 +00:00
|
|
|
result.append( ItemParams() );
|
|
|
|
result.append( "\n" );
|
2022-09-15 03:25:57 +00:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-10-10 23:23:00 +00:00
|
|
|
std::string SPICE_GENERATOR::ItemName( const SPICE_ITEM& aItem ) const
|
2022-09-15 03:25:57 +00:00
|
|
|
{
|
2022-10-10 23:23:00 +00:00
|
|
|
if( aItem.refName != "" && boost::starts_with( aItem.refName, m_model.GetSpiceInfo().itemType ) )
|
|
|
|
return aItem.refName;
|
2022-09-15 03:25:57 +00:00
|
|
|
else
|
2022-10-10 23:23:00 +00:00
|
|
|
return fmt::format( "{}{}", m_model.GetSpiceInfo().itemType, aItem.refName );
|
2022-09-15 03:25:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-10-10 23:23:00 +00:00
|
|
|
std::string SPICE_GENERATOR::ItemPins( const SPICE_ITEM& aItem ) const
|
2022-09-15 03:25:57 +00:00
|
|
|
{
|
2022-09-22 05:38:45 +00:00
|
|
|
std::string result;
|
2022-09-15 03:25:57 +00:00
|
|
|
int ncCounter = 0;
|
|
|
|
|
|
|
|
for( const SIM_MODEL::PIN& pin : GetPins() )
|
|
|
|
{
|
2022-10-10 23:23:00 +00:00
|
|
|
auto it = std::find( aItem.pinNumbers.begin(), aItem.pinNumbers.end(),
|
2022-09-15 03:25:57 +00:00
|
|
|
pin.symbolPinNumber );
|
|
|
|
|
2022-10-10 23:23:00 +00:00
|
|
|
if( it == aItem.pinNumbers.end() )
|
2022-12-22 20:21:10 +00:00
|
|
|
{
|
2022-10-10 23:23:00 +00:00
|
|
|
result.append( fmt::format( " NC-{}-{}", aItem.refName, ncCounter++ ) );
|
2022-12-22 20:21:10 +00:00
|
|
|
}
|
2022-09-15 03:25:57 +00:00
|
|
|
else
|
|
|
|
{
|
2022-10-10 23:23:00 +00:00
|
|
|
long symbolPinIndex = std::distance( aItem.pinNumbers.begin(), it );
|
|
|
|
result.append( fmt::format( " {}", aItem.pinNetNames.at( symbolPinIndex ) ) );
|
2022-09-15 03:25:57 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-10-10 23:23:00 +00:00
|
|
|
std::string SPICE_GENERATOR::ItemModelName( const SPICE_ITEM& aItem ) const
|
2022-09-15 03:25:57 +00:00
|
|
|
{
|
2022-10-10 23:23:00 +00:00
|
|
|
return fmt::format( " {}", aItem.modelName );
|
2022-09-15 03:25:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-09-22 05:38:45 +00:00
|
|
|
std::string SPICE_GENERATOR::ItemParams() const
|
2022-09-15 03:25:57 +00:00
|
|
|
{
|
2022-09-22 05:38:45 +00:00
|
|
|
std::string result;
|
2022-09-15 03:25:57 +00:00
|
|
|
|
|
|
|
for( const SIM_MODEL::PARAM& param : GetInstanceParams() )
|
|
|
|
{
|
2022-12-16 16:31:39 +00:00
|
|
|
std::string name = param.info.spiceInstanceName.empty() ? param.info.name
|
|
|
|
: param.info.spiceInstanceName;
|
2022-09-22 05:38:45 +00:00
|
|
|
std::string value = param.value->ToSpiceString();
|
2022-09-15 03:25:57 +00:00
|
|
|
|
|
|
|
if( value != "" )
|
2022-09-22 05:38:45 +00:00
|
|
|
result.append( fmt::format( " {}={}", name, value ) );
|
2022-09-15 03:25:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-10-24 01:47:48 +00:00
|
|
|
std::string SPICE_GENERATOR::TunerCommand( const SPICE_ITEM& aItem,
|
|
|
|
const SIM_VALUE_FLOAT& aValue ) const
|
2022-09-15 03:25:57 +00:00
|
|
|
{
|
2022-10-24 01:47:48 +00:00
|
|
|
// No tuning available by default.
|
2022-09-15 03:25:57 +00:00
|
|
|
return "";
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-10-10 23:23:00 +00:00
|
|
|
std::vector<std::string> SPICE_GENERATOR::CurrentNames( const SPICE_ITEM& aItem ) const
|
2022-09-15 03:25:57 +00:00
|
|
|
{
|
2022-10-10 23:23:00 +00:00
|
|
|
return { fmt::format( "I({})", ItemName( aItem ) ) };
|
2022-09-15 03:25:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-10-10 23:23:00 +00:00
|
|
|
std::string SPICE_GENERATOR::Preview( const SPICE_ITEM& aItem ) const
|
2022-09-15 03:25:57 +00:00
|
|
|
{
|
2022-10-10 23:23:00 +00:00
|
|
|
std::string spiceCode = ModelLine( aItem );
|
2022-09-15 03:25:57 +00:00
|
|
|
|
2022-10-10 23:23:00 +00:00
|
|
|
std::string itemLine = ItemLine( aItem );
|
2022-09-15 03:25:57 +00:00
|
|
|
if( spiceCode != "" )
|
2022-09-22 05:38:45 +00:00
|
|
|
spiceCode.append( "\n" );
|
2022-09-15 03:25:57 +00:00
|
|
|
|
2022-09-22 05:38:45 +00:00
|
|
|
spiceCode.append( itemLine );
|
|
|
|
return boost::trim_copy( spiceCode );
|
2022-09-15 03:25:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
std::vector<std::reference_wrapper<const SIM_MODEL::PARAM>> SPICE_GENERATOR::GetInstanceParams() const
|
|
|
|
{
|
|
|
|
std::vector<std::reference_wrapper<const SIM_MODEL::PARAM>> instanceParams;
|
|
|
|
|
|
|
|
for( const SIM_MODEL::PARAM& param : m_model.GetParams() )
|
|
|
|
{
|
|
|
|
if( param.info.isSpiceInstanceParam )
|
|
|
|
instanceParams.emplace_back( param );
|
|
|
|
}
|
|
|
|
|
|
|
|
return instanceParams;
|
|
|
|
}
|
|
|
|
|
|
|
|
|