2022-04-12 14:37:06 +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/sim_model_subckt.h>
|
2022-06-12 03:39:13 +00:00
|
|
|
#include <sim/spice_grammar.h>
|
2022-09-22 05:38:45 +00:00
|
|
|
|
|
|
|
#include <fmt/core.h>
|
2022-04-12 14:37:06 +00:00
|
|
|
#include <pegtl.hpp>
|
|
|
|
#include <pegtl/contrib/parse_tree.hpp>
|
|
|
|
|
|
|
|
|
2022-05-31 02:55:48 +00:00
|
|
|
namespace SIM_MODEL_SUBCKT_SPICE_PARSER
|
2022-04-12 14:37:06 +00:00
|
|
|
{
|
2022-05-31 02:55:48 +00:00
|
|
|
using namespace SPICE_GRAMMAR;
|
2022-04-12 14:37:06 +00:00
|
|
|
|
|
|
|
template <typename Rule> struct spiceUnitSelector : std::false_type {};
|
|
|
|
|
|
|
|
template <> struct spiceUnitSelector<dotSubckt> : std::true_type {};
|
|
|
|
template <> struct spiceUnitSelector<modelName> : std::true_type {};
|
|
|
|
template <> struct spiceUnitSelector<dotSubcktPinName> : std::true_type {};
|
2022-08-30 09:58:20 +00:00
|
|
|
template <> struct spiceUnitSelector<dotSubcktParams> : std::true_type {};
|
2022-04-12 14:37:06 +00:00
|
|
|
template <> struct spiceUnitSelector<param> : std::true_type {};
|
2022-06-21 02:22:52 +00:00
|
|
|
template <> struct spiceUnitSelector<number<SIM_VALUE::TYPE_INT, NOTATION::SPICE>>
|
2022-04-12 14:37:06 +00:00
|
|
|
: std::true_type {};
|
2022-06-21 02:22:52 +00:00
|
|
|
template <> struct spiceUnitSelector<number<SIM_VALUE::TYPE_FLOAT, NOTATION::SPICE>>
|
2022-04-12 14:37:06 +00:00
|
|
|
: std::true_type {};
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-10-10 23:23:00 +00:00
|
|
|
std::string SPICE_GENERATOR_SUBCKT::ModelLine( const SPICE_ITEM& aItem ) const
|
2022-09-14 07:19:25 +00:00
|
|
|
{
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-10-10 23:23:00 +00:00
|
|
|
std::vector<std::string> SPICE_GENERATOR_SUBCKT::CurrentNames( const SPICE_ITEM& aItem ) const
|
2022-09-14 07:19:25 +00:00
|
|
|
{
|
2022-09-22 05:38:45 +00:00
|
|
|
std::vector<std::string> currentNames;
|
2022-09-14 07:19:25 +00:00
|
|
|
|
2022-09-15 03:25:57 +00:00
|
|
|
for( const SIM_MODEL::PIN& pin : GetPins() )
|
2022-10-10 23:23:00 +00:00
|
|
|
currentNames.push_back( fmt::format( "I({}:{})", ItemName( aItem ), pin.name ) );
|
2022-09-14 07:19:25 +00:00
|
|
|
|
|
|
|
return currentNames;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-10-04 15:17:41 +00:00
|
|
|
void SPICE_MODEL_PARSER_SUBCKT::ReadModel( const SIM_LIBRARY_SPICE& aLibrary,
|
|
|
|
const std::string& aSpiceCode )
|
2022-04-12 14:37:06 +00:00
|
|
|
{
|
2022-09-22 05:38:45 +00:00
|
|
|
tao::pegtl::string_input<> in( aSpiceCode, "from_content" );
|
2022-04-12 14:37:06 +00:00
|
|
|
std::unique_ptr<tao::pegtl::parse_tree::node> root;
|
|
|
|
|
|
|
|
try
|
|
|
|
{
|
2022-05-31 02:55:48 +00:00
|
|
|
root = tao::pegtl::parse_tree::parse<SIM_MODEL_SUBCKT_SPICE_PARSER::spiceUnitGrammar,
|
2022-09-17 06:27:47 +00:00
|
|
|
SIM_MODEL_SUBCKT_SPICE_PARSER::spiceUnitSelector,
|
2022-09-17 06:46:45 +00:00
|
|
|
tao::pegtl::nothing,
|
2022-10-15 17:31:15 +00:00
|
|
|
SIM_MODEL_SUBCKT_SPICE_PARSER::control>( in );
|
2022-04-12 14:37:06 +00:00
|
|
|
}
|
|
|
|
catch( const tao::pegtl::parse_error& e )
|
|
|
|
{
|
2022-06-21 02:22:52 +00:00
|
|
|
THROW_IO_ERROR( e.what() );
|
2022-04-12 14:37:06 +00:00
|
|
|
}
|
|
|
|
|
2022-09-19 05:18:06 +00:00
|
|
|
SIM_MODEL_SUBCKT& model = static_cast<SIM_MODEL_SUBCKT&>( m_model );
|
|
|
|
|
2022-04-12 14:37:06 +00:00
|
|
|
for( const auto& node : root->children )
|
|
|
|
{
|
2022-05-31 02:55:48 +00:00
|
|
|
if( node->is_type<SIM_MODEL_SUBCKT_SPICE_PARSER::dotSubckt>() )
|
2022-04-12 14:37:06 +00:00
|
|
|
{
|
|
|
|
for( const auto& subnode : node->children )
|
|
|
|
{
|
2022-05-31 02:55:48 +00:00
|
|
|
if( subnode->is_type<SIM_MODEL_SUBCKT_SPICE_PARSER::modelName>() )
|
2022-04-12 14:37:06 +00:00
|
|
|
{
|
|
|
|
}
|
2022-05-31 02:55:48 +00:00
|
|
|
else if( subnode->is_type<SIM_MODEL_SUBCKT_SPICE_PARSER::dotSubcktPinName>() )
|
2022-04-12 14:37:06 +00:00
|
|
|
{
|
2022-10-15 17:31:15 +00:00
|
|
|
model.AddPin( { subnode->string(), fmt::format( "{}", model.GetPinCount() + 1 ) } );
|
2022-04-12 14:37:06 +00:00
|
|
|
}
|
2022-08-30 09:58:20 +00:00
|
|
|
else if( subnode->is_type<SIM_MODEL_SUBCKT_SPICE_PARSER::dotSubcktParams>() )
|
2022-04-12 14:37:06 +00:00
|
|
|
{
|
2022-06-21 02:22:52 +00:00
|
|
|
for( const auto& subsubnode : subnode->children )
|
|
|
|
{
|
|
|
|
if( subsubnode->is_type<SIM_MODEL_SUBCKT_SPICE_PARSER::param>() )
|
|
|
|
{
|
2022-09-19 05:18:06 +00:00
|
|
|
model.m_paramInfos.push_back( std::make_unique<SIM_MODEL::PARAM::INFO>() );
|
|
|
|
model.m_paramInfos.back()->name = subsubnode->string();
|
|
|
|
model.m_paramInfos.back()->isInstanceParam = true;
|
|
|
|
model.m_paramInfos.back()->isSpiceInstanceParam = true;
|
2022-06-21 02:22:52 +00:00
|
|
|
|
2022-09-19 05:18:06 +00:00
|
|
|
model.AddParam( *model.m_paramInfos.back() );
|
2022-06-21 02:22:52 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
wxFAIL_MSG( "Unhandled parse tree subsubnode" );
|
|
|
|
}
|
|
|
|
}
|
2022-04-12 14:37:06 +00:00
|
|
|
}
|
|
|
|
else if( subnode->is_type<
|
2022-06-21 02:22:52 +00:00
|
|
|
SIM_MODEL_SUBCKT_SPICE_PARSER::number<SIM_VALUE::TYPE_INT,
|
2022-08-22 05:40:23 +00:00
|
|
|
SIM_MODEL_SUBCKT_SPICE_PARSER::NOTATION::SPICE>>()
|
2022-04-12 14:37:06 +00:00
|
|
|
|| subnode->is_type<
|
2022-06-21 02:22:52 +00:00
|
|
|
SIM_MODEL_SUBCKT_SPICE_PARSER::number<SIM_VALUE::TYPE_FLOAT,
|
2022-08-22 05:40:23 +00:00
|
|
|
SIM_MODEL_SUBCKT_SPICE_PARSER::NOTATION::SPICE>>() )
|
2022-04-12 14:37:06 +00:00
|
|
|
{
|
2022-09-19 05:18:06 +00:00
|
|
|
wxASSERT( model.m_paramInfos.size() > 0 );
|
|
|
|
model.m_paramInfos.back()->defaultValue = subnode->string();
|
2022-04-12 14:37:06 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
wxFAIL_MSG( "Unhandled parse tree node" );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-19 05:18:06 +00:00
|
|
|
model.m_spiceCode = aSpiceCode;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
SIM_MODEL_SUBCKT::SIM_MODEL_SUBCKT() :
|
|
|
|
SIM_MODEL_SPICE( TYPE::SUBCKT,
|
|
|
|
std::make_unique<SPICE_GENERATOR_SUBCKT>( *this ),
|
|
|
|
std::make_unique<SPICE_MODEL_PARSER_SUBCKT>( *this ) )
|
|
|
|
{
|
2022-04-12 14:37:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void SIM_MODEL_SUBCKT::SetBaseModel( const SIM_MODEL& aBaseModel )
|
|
|
|
{
|
|
|
|
SIM_MODEL::SetBaseModel( aBaseModel );
|
|
|
|
|
|
|
|
// Pins aren't constant for subcircuits, so they need to be copied from the base model.
|
2022-06-21 02:22:52 +00:00
|
|
|
for( const PIN& pin : GetBaseModel()->GetPins() )
|
|
|
|
AddPin( pin );
|
2022-04-12 14:37:06 +00:00
|
|
|
|
|
|
|
// Same for parameters.
|
2022-06-21 02:22:52 +00:00
|
|
|
for( const PARAM& param : GetBaseModel()->GetParams() )
|
|
|
|
AddParam( param.info );
|
2022-04-12 14:37:06 +00:00
|
|
|
}
|
2022-08-23 22:06:16 +00:00
|
|
|
|
|
|
|
|
|
|
|
void SIM_MODEL_SUBCKT::CreatePins( unsigned aSymbolPinCount )
|
|
|
|
{
|
|
|
|
SIM_MODEL::CreatePins( aSymbolPinCount );
|
|
|
|
|
|
|
|
// Reset the pins to Not Connected. Linear order is not as common, and reordering the pins is
|
|
|
|
// more effort in the GUI than assigning them from scratch.
|
|
|
|
for( int pinIndex = 0; pinIndex < GetPinCount(); ++pinIndex )
|
|
|
|
SetPinSymbolPinNumber( pinIndex, "" );
|
|
|
|
}
|