Move IBIS model Spice code generation to SIM_MODEL_KIBIS

IBIS drivers are temporarily broken. Will fix is subsequent commits.
This commit is contained in:
Mikolaj Wielgus 2022-10-08 02:59:44 +02:00
parent a5da46459c
commit 994274fe9f
9 changed files with 173 additions and 277 deletions

View File

@ -25,6 +25,7 @@
#include <../../pcbnew/ibis/kibis.h>
#include "netlist_exporter_spice.h"
#include <sim/sim_library_spice.h>
#include <sim/sim_model_raw_spice.h>
#include <sim/spice_grammar.h>
#include <common.h>
@ -187,267 +188,12 @@ bool NETLIST_EXPORTER_SPICE::ReadSchematicAndLibraries( unsigned aNetlistOptions
ITEM spiceItem;
// @TODO This is to be removed once ngspice gets ibis support
/*if( SIM_MODEL::GetFieldValue( &( symbol->GetFields() ), SIM_MODEL::DEVICE_TYPE_FIELD )
== "IBIS" )
{
if( !readRefName( sheet, *symbol, spiceItem, refNames ) )
return false;
wxString ibisFile = SIM_MODEL::GetFieldValue( &( symbol->GetFields() ),
SIM_LIBRARY::LIBRARY_FIELD );
wxString ibisModel = SIM_MODEL::GetFieldValue( &( symbol->GetFields() ),
SIM_LIBRARY_KIBIS::MODEL_FIELD );
wxString ibisPin = SIM_MODEL::GetFieldValue( &( symbol->GetFields() ),
SIM_LIBRARY_KIBIS::PIN_FIELD );
wxString ibisComp = SIM_MODEL::GetFieldValue( &( symbol->GetFields() ),
SIM_LIBRARY::NAME_FIELD );
wxString modelType =
SIM_MODEL::GetFieldValue( &( symbol->GetFields() ), SIM_MODEL::TYPE_FIELD );
wxString absolutePath = m_schematic->Prj().AbsolutePath( ibisFile );
KIBIS vkibis = KIBIS( std::string( absolutePath.c_str() ) );
if( !vkibis.m_valid )
{
wxLogTrace( "IBIS:", wxT( "Can't parse ibis file '%s'" ), ibisFile );
return false;
}
KIBIS_MODEL* kmodel = vkibis.GetModel( std::string( ibisModel.c_str() ) );
if( !kmodel )
{
wxLogTrace( "IBIS:", wxT( "Can't find model '%s'" ), ibisModel );
return false;
}
if( !kmodel->m_valid )
{
wxLogTrace( "IBIS:", wxT( "Invalid model '%s'" ), ibisModel );
return false;
}
KIBIS_COMPONENT* kcomp = vkibis.GetComponent( std::string( ibisComp.c_str() ) );
if( !kcomp )
{
wxLogTrace( "IBIS:", wxT( "Can't find model '%s'" ), ibisComp );
return false;
}
if( !kcomp->m_valid )
{
wxLogTrace( "IBIS:", wxT( "Invalid model '%s'" ), ibisComp );
return false;
}
KIBIS_PIN* kpin = kcomp->GetPin( std::string( ibisPin.c_str() ) );
if( !kpin )
{
wxLogTrace( "IBIS:", wxT( "Can't find model '%s'" ), ibisPin );
return false;
}
if( !kpin->m_valid )
{
wxLogTrace( "IBIS:", wxT( "Invalid model '%s'" ), ibisPin );
return false;
}
std::string modelData;
std::string modelName =
std::string( symbol->GetRef( &sheet ).c_str() ) + "_ibisGenerated";
spiceItem.libraryPath =
cacheDir.GetPath() + "/" + symbol->GetRef( &sheet ) + ".cache";
wxFile cacheFile( spiceItem.libraryPath, wxFile::write );
if( !cacheFile.IsOpened() )
{
wxLogTrace( "IBIS:", wxT( "Cannot open file for writing: '%s'" ),
spiceItem.libraryPath );
return false;
}
spiceItem.model =
SIM_MODEL::Create( static_cast<unsigned>( m_sortedSymbolPinList.size() ),
symbol->GetFields() );
SIM_MODEL::TYPE type = spiceItem.model->GetType();
switch( type )
{
case SIM_MODEL::TYPE::KIBIS_DEVICE:
case SIM_MODEL::TYPE::KIBIS_DRIVER_DC:
case SIM_MODEL::TYPE::KIBIS_DRIVER_RECT:
case SIM_MODEL::TYPE::KIBIS_DRIVER_PRBS:
case SIM_MODEL::TYPE::KIBIS_DIFFDEVICE:
case SIM_MODEL::TYPE::KIBIS_DIFFDRIVER:
break;
default:
wxLogTrace( "IBIS:", wxT( "Invalid ibis type: '%s'" ),
symbol->GetRef( &sheet ) );
continue;
}
m_libraries.try_emplace( spiceItem.libraryPath,
SIM_LIBRARY::Create( spiceItem.libraryPath ) );
readNameField( *symbol, spiceItem );
if( !readModel( *symbol, spiceItem ) )
continue;
spiceItem.modelName = modelName;
readPinNumbers( *symbol, spiceItem );
readPinNetNames( *symbol, spiceItem, ncCounter );
KIBIS_PARAMETER kparams;
const SIM_MODEL::PARAM* mparam = nullptr;
mparam = spiceItem.model->FindParam( "vcc" );
if( mparam )
kparams.SetCornerFromString( kparams.m_supply, mparam->value->ToString() );
mparam = spiceItem.model->FindParam( "rpin" );
if( mparam )
kparams.SetCornerFromString( kparams.m_Rpin, mparam->value->ToString() );
mparam = spiceItem.model->FindParam( "lpin" );
if( mparam )
kparams.SetCornerFromString( kparams.m_Lpin, mparam->value->ToString() );
mparam = spiceItem.model->FindParam( "cpin" );
if( mparam )
kparams.SetCornerFromString( kparams.m_Cpin, mparam->value->ToString() );
mparam = spiceItem.model->FindParam( "ccomp" );
if( mparam )
kparams.SetCornerFromString( kparams.m_Ccomp, mparam->value->ToString() );
switch( type )
{
case SIM_MODEL::TYPE::KIBIS_DEVICE:
kpin->writeSpiceDevice( &modelData, modelName, *kmodel, kparams );
break;
case SIM_MODEL::TYPE::KIBIS_DIFFDEVICE:
kpin->writeSpiceDiffDevice( &modelData, modelName, *kmodel, kparams );
break;
case SIM_MODEL::TYPE::KIBIS_DIFFDRIVER:
kpin->writeSpiceDiffDriver( &modelData, modelName, *kmodel, kparams );
break;
case SIM_MODEL::TYPE::KIBIS_DRIVER_DC:
{
std::string paramValue = spiceItem.model->FindParam( "dc" )->value->ToString();
if( paramValue == "hi-Z" )
{
kparams.m_waveform =
static_cast<KIBIS_WAVEFORM*>( new KIBIS_WAVEFORM_HIGH_Z() );
}
else if( paramValue == "low" )
{
kparams.m_waveform =
static_cast<KIBIS_WAVEFORM*>( new KIBIS_WAVEFORM_STUCK_LOW() );
}
else if( paramValue == "high" )
{
kparams.m_waveform =
static_cast<KIBIS_WAVEFORM*>( new KIBIS_WAVEFORM_STUCK_HIGH() );
}
kpin->writeSpiceDriver( &modelData, modelName, *kmodel, kparams );
break;
}
case SIM_MODEL::TYPE::KIBIS_DRIVER_RECT:
{
kparams.m_waveform = static_cast<KIBIS_WAVEFORM*>(
new KIBIS_WAVEFORM_RECTANGULAR() );
mparam = spiceItem.model->FindParam( "ton" );
if( mparam )
static_cast<KIBIS_WAVEFORM_RECTANGULAR*>( kparams.m_waveform )
->m_ton = static_cast<SIM_VALUE_FLOAT*>( mparam->value.get() )->Get()
.value_or( 1 );
mparam = spiceItem.model->FindParam( "toff" );
if( mparam )
static_cast<KIBIS_WAVEFORM_RECTANGULAR*>( kparams.m_waveform )
->m_toff = static_cast<SIM_VALUE_FLOAT*>( mparam->value.get() )->Get()
.value_or( 1 );
mparam = spiceItem.model->FindParam( "delay" );
if( mparam )
static_cast<KIBIS_WAVEFORM_RECTANGULAR*>( kparams.m_waveform )
->m_delay = static_cast<SIM_VALUE_FLOAT*>( mparam->value.get() )->Get()
.value_or( 0 );
kpin->writeSpiceDriver( &modelData, modelName, *kmodel, kparams );
break;
}
case SIM_MODEL::TYPE::KIBIS_DRIVER_PRBS:
{
kparams.m_waveform = static_cast<KIBIS_WAVEFORM*>(
new KIBIS_WAVEFORM_PRBS() );
mparam = spiceItem.model->FindParam( "f0" );
if( mparam )
static_cast<KIBIS_WAVEFORM_PRBS*>( kparams.m_waveform )
->m_bitrate = static_cast<SIM_VALUE_FLOAT*>( mparam->value.get() )->Get()
.value_or( 0 );
mparam = spiceItem.model->FindParam( "bits" );
if( mparam )
static_cast<KIBIS_WAVEFORM_PRBS*>( kparams.m_waveform )
->m_bits = static_cast<SIM_VALUE_FLOAT*>( mparam->value.get() )->Get()
.value_or( 0 );
mparam = spiceItem.model->FindParam( "delay" );
if( mparam )
static_cast<KIBIS_WAVEFORM_PRBS*>( kparams.m_waveform )
->m_delay = static_cast<SIM_VALUE_FLOAT*>( mparam->value.get() )->Get()
.value_or( 0 );
kpin->writeSpiceDriver( &modelData, modelName, *kmodel, kparams );
break;
}
default:
continue;
}
cacheFile.Write( wxString( modelData ) );
m_items.push_back( std::move( spiceItem ) );
continue;
}*/
try
{
if( !readRefName( sheet, *symbol, spiceItem, refNames ) )
return false;
readModel( *symbol, spiceItem );
readModel( sheet, *symbol, spiceItem );
readPinNumbers( *symbol, spiceItem );
readPinNetNames( *symbol, spiceItem, ncCounter );
@ -574,12 +320,13 @@ bool NETLIST_EXPORTER_SPICE::readRefName( SCH_SHEET_PATH& aSheet, SCH_SYMBOL& aS
}
void NETLIST_EXPORTER_SPICE::readModel( SCH_SYMBOL& aSymbol, ITEM& aItem )
void NETLIST_EXPORTER_SPICE::readModel( SCH_SHEET_PATH& aSheet, SCH_SYMBOL& aSymbol, ITEM& aItem )
{
auto [modelName, model] = m_libMgr.CreateModel( aSymbol );
aItem.modelName = ( modelName != "" ) ? modelName : ( "__" + aItem.refName );
aItem.model = &model;
// Special cases for raw Spice models and KIBIS.
if( auto rawSpiceModel = dynamic_cast<const SIM_MODEL_RAW_SPICE*>( aItem.model ) )
{
int libParamIndex = static_cast<int>( SIM_MODEL_RAW_SPICE::SPICE_PARAM::LIB );
@ -588,6 +335,31 @@ void NETLIST_EXPORTER_SPICE::readModel( SCH_SYMBOL& aSymbol, ITEM& aItem )
if( path != "" )
m_rawIncludes.insert( path );
}
else if( auto kibisModel = dynamic_cast<const SIM_MODEL_KIBIS*>( aItem.model ) )
{
aItem.modelName = aItem.model->GetFieldValue( &aSymbol.GetFields(),
SIM_LIBRARY_KIBIS::MODEL_FIELD );
wxFileName cacheDir;
cacheDir.AssignDir( PATHS::GetUserCachePath() );
cacheDir.AppendDir( wxT( "ibis" ) );
std::string libraryPath = fmt::format( "{}/{}.cache",
std::string( cacheDir.GetPath() ),
std::string( aSymbol.GetRef( &aSheet ) ) );
wxFile cacheFile( libraryPath, wxFile::write );
if( !cacheFile.IsOpened() )
{
DisplayErrorMessage( nullptr,
wxString::Format( _( "Could not open file '%s' to write IBIS model" ),
libraryPath ) );
}
std::string modelData = kibisModel->GenerateSpiceDriver( aSymbol.GetFields() );
cacheFile.Write( wxString( modelData ) );
m_rawIncludes.insert( libraryPath );
}
}
@ -643,7 +415,10 @@ void NETLIST_EXPORTER_SPICE::writeInclude( OUTPUTFORMATTER& aFormatter, unsigned
void NETLIST_EXPORTER_SPICE::writeIncludes( OUTPUTFORMATTER& aFormatter, unsigned aNetlistOptions )
{
for( auto&& [path, library] : m_libMgr.GetLibraries() )
{
if( dynamic_cast<const SIM_LIBRARY_SPICE*>( &library.get() ) )
writeInclude( aFormatter, aNetlistOptions, path );
}
for( const std::string& path : m_rawIncludes )
writeInclude( aFormatter, aNetlistOptions, path );

View File

@ -51,7 +51,6 @@ public:
struct ITEM
{
std::string refName;
std::string libraryPath;
std::vector<std::string> pinNumbers;
std::vector<std::string> pinNetNames;
const SIM_MODEL* model;
@ -131,7 +130,7 @@ protected:
private:
bool readRefName( SCH_SHEET_PATH& aSheet, SCH_SYMBOL& aSymbol, ITEM& aItem,
std::set<std::string>& aRefNames );
void readModel( SCH_SYMBOL& aSymbol, ITEM& aItem );
void readModel( SCH_SHEET_PATH& aSheet, SCH_SYMBOL& aSymbol, ITEM& aItem );
void readPinNumbers( SCH_SYMBOL& aSymbol, ITEM& aItem );
void readPinNetNames( SCH_SYMBOL& aSymbol, ITEM& aItem, int& aNcCounter );

View File

@ -35,7 +35,7 @@ SIM_LIB_MGR::SIM_LIB_MGR( const PROJECT& aPrj ) : m_project( aPrj )
}
std::pair<std::string, SIM_MODEL&> SIM_LIB_MGR::CreateModel( SCH_SYMBOL& aSymbol )
SIM_LIBRARY::MODEL SIM_LIB_MGR::CreateModel( SCH_SYMBOL& aSymbol )
{
std::vector<LIB_PIN*> pins = aSymbol.GetLibPins();
SCH_FIELD* libraryField = aSymbol.FindField( SIM_LIBRARY::LIBRARY_FIELD );
@ -90,7 +90,7 @@ std::pair<std::string, SIM_MODEL&> SIM_LIB_MGR::CreateModel( SCH_SYMBOL& aSymbol
aSymbol.GetFields() ) );
}
return std::pair<std::string, SIM_MODEL&>( baseModelName, *m_models.back() );
return { baseModelName, *m_models.back() };
}

View File

@ -30,10 +30,11 @@
#include <vector>
#include <string>
#include <sim/sim_library.h>
#include <sim/sim_model.h>
class PROJECT;
class SCH_SYMBOL;
class SIM_LIBRARY;
class SIM_MODEL;
class SIM_LIB_MGR
@ -43,7 +44,7 @@ public:
virtual ~SIM_LIB_MGR() = default;
// TODO: The argument can be made const.
std::pair<std::string, SIM_MODEL&> CreateModel( SCH_SYMBOL& aSymbol );
SIM_LIBRARY::MODEL CreateModel( SCH_SYMBOL& aSymbol );
SIM_LIBRARY& CreateLibrary( const std::string& aLibraryPath );

View File

@ -74,7 +74,6 @@ public:
std::vector<MODEL> GetModels() const;
std::string GetFilePath() const { return m_filePath; }
std::string GetError() const { return m_error; }
DEFINE_ENUM_CLASS_WITH_ITERATOR( LIBRARY_TYPE, NONE, KIBIS, SPICE )
@ -85,7 +84,6 @@ protected:
std::vector<std::unique_ptr<SIM_MODEL>> m_models;
std::string m_filePath;
std::string m_error;
};

View File

@ -33,7 +33,7 @@
void SIM_LIBRARY_KIBIS::ReadFile( const std::string& aFilePath, SIM_MODEL::TYPE aType )
{
SIM_LIBRARY::ReadFile( aFilePath );
m_kibis = KIBIS( std::string( aFilePath.c_str() ) );
m_kibis = KIBIS( aFilePath );
if( !m_kibis.m_valid )
{
@ -58,6 +58,7 @@ void SIM_LIBRARY_KIBIS::ReadFile( const std::string& aFilePath, SIM_MODEL::TYPE
}
}
bool SIM_LIBRARY_KIBIS::InitModel( SIM_MODEL_KIBIS& aModel, wxString aCompName )
{
for( KIBIS_COMPONENT& kcomp : m_kibis.m_components )

View File

@ -24,9 +24,7 @@
#include <../../pcbnew/ibis/kibis.h>
#include <sim/sim_model_kibis.h>
#include <sim/sim_library_kibis.h>
#include <locale_io.h>
#include <paths.h>
#include <fmt/core.h>
@ -156,6 +154,124 @@ bool SIM_MODEL_KIBIS::ChangePin( SIM_LIBRARY_KIBIS& aLib, std::string aPinNumber
}
void SIM_MODEL_KIBIS::SetBaseModel( const SIM_MODEL& aBaseModel )
{
}
std::string SIM_MODEL_KIBIS::GenerateSpiceDriver( const std::vector<SCH_FIELD>& aFields ) const
{
std::string ibisLibFilename = SIM_MODEL::GetFieldValue( &aFields, SIM_LIBRARY::LIBRARY_FIELD );
std::string ibisCompName = SIM_MODEL::GetFieldValue( &aFields, SIM_LIBRARY::NAME_FIELD );
std::string ibisPinName = SIM_MODEL::GetFieldValue( &aFields, SIM_LIBRARY_KIBIS::PIN_FIELD );
std::string ibisModelName = SIM_MODEL::GetFieldValue( &aFields, SIM_LIBRARY_KIBIS::MODEL_FIELD );
KIBIS kibis( ibisLibFilename );
KIBIS_COMPONENT* kcomp = kibis.GetComponent( std::string( ibisCompName ) );
if( !kcomp )
THROW_IO_ERROR( wxString::Format( _( "Could not find IBIS component '%s'" ), ibisCompName ) );
KIBIS_PIN* kpin = kcomp->GetPin( ibisPinName );
if( !kpin )
{
THROW_IO_ERROR( wxString::Format( _( "Could not find IBIS pin '%s' in component '%s'" ),
ibisPinName,
ibisCompName ) );
}
if( !kpin->m_valid )
{
THROW_IO_ERROR( wxString::Format( _( "Invalid IBIS pin '%s' in component '%s'" ),
ibisPinName,
ibisCompName ) );
}
KIBIS_MODEL* kmodel = kibis.GetModel( ibisModelName );
if( !kmodel )
THROW_IO_ERROR( wxString::Format( _( "Could not find IBIS model '%s'" ), ibisModelName ) );
if( !kmodel->m_valid )
THROW_IO_ERROR( wxString::Format( _( "Invalid IBIS model '%s'" ), ibisModelName ) );
KIBIS_PARAMETER kparams;
kparams.SetCornerFromString( kparams.m_supply, FindParam( "vcc" )->value->ToString() );
kparams.SetCornerFromString( kparams.m_Rpin, FindParam( "rpin" )->value->ToString() );
kparams.SetCornerFromString( kparams.m_Lpin, FindParam( "lpin" )->value->ToString() );
kparams.SetCornerFromString( kparams.m_Cpin, FindParam( "cpin" )->value->ToString() );
//kparams.SetCornerFromString( kparams.m_Ccomp, FindParam( "ccomp" )->value->ToString() );
std::string result;
switch( GetType() )
{
case SIM_MODEL::TYPE::KIBIS_DEVICE:
kpin->writeSpiceDevice( &result, ibisModelName, *kmodel, kparams );
break;
case SIM_MODEL::TYPE::KIBIS_DRIVER_DC:
{
std::string paramValue = FindParam( "dc" )->value->ToString();
if( paramValue == "hi-Z" )
{
kparams.m_waveform =
static_cast<KIBIS_WAVEFORM*>( new KIBIS_WAVEFORM_HIGH_Z() );
}
else if( paramValue == "low" )
{
kparams.m_waveform =
static_cast<KIBIS_WAVEFORM*>( new KIBIS_WAVEFORM_STUCK_LOW() );
}
else if( paramValue == "high" )
{
kparams.m_waveform =
static_cast<KIBIS_WAVEFORM*>( new KIBIS_WAVEFORM_STUCK_HIGH() );
}
kpin->writeSpiceDriver( &result, ibisModelName, *kmodel, kparams );
break;
}
case SIM_MODEL::TYPE::KIBIS_DRIVER_RECT:
{
KIBIS_WAVEFORM_RECTANGULAR* waveform = new KIBIS_WAVEFORM_RECTANGULAR();
waveform->m_ton = static_cast<SIM_VALUE_FLOAT&>( *FindParam( "ton" )->value ).Get().value_or( 1 );
waveform->m_toff = static_cast<SIM_VALUE_FLOAT&>( *FindParam( "toff" )->value ).Get().value_or( 1 );
waveform->m_delay = static_cast<SIM_VALUE_FLOAT&>( *FindParam( "delay" )->value ).Get().value_or( 0 );
kparams.m_waveform = waveform;
kpin->writeSpiceDriver( &result, ibisModelName, *kmodel, kparams );
break;
}
case SIM_MODEL::TYPE::KIBIS_DRIVER_PRBS:
{
KIBIS_WAVEFORM_PRBS* waveform = new KIBIS_WAVEFORM_PRBS();
waveform->m_bitrate = static_cast<SIM_VALUE_FLOAT&>( *FindParam( "f0" )->value ).Get().value_or( 0 );
waveform->m_bits = static_cast<SIM_VALUE_FLOAT&>( *FindParam( "bits" )->value ).Get().value_or( 0 );
waveform->m_delay = static_cast<SIM_VALUE_FLOAT&>( *FindParam( "delay" )->value ).Get().value_or( 0 );
kparams.m_waveform = waveform;
kpin->writeSpiceDriver( &result, ibisModelName, *kmodel, kparams );
break;
}
default:
wxFAIL_MSG( "Unknown IBIS model type" );
return "";
}
return result;
}
std::vector<SIM_MODEL::PARAM::INFO> SIM_MODEL_KIBIS::makeParamInfos( TYPE aType )
{
std::vector<PARAM::INFO> paramInfos;

View File

@ -24,8 +24,10 @@
#ifndef SIM_MODEL_KIBIS_H
#define SIM_MODEL_KIBIS_H
#include <../../pcbnew/ibis/kibis.h>
#include <sim/sim_model.h>
#include <sim/spice_generator.h>
#include <project.h>
class SIM_LIBRARY_KIBIS;
@ -85,10 +87,12 @@ public:
* */
bool ChangePin( SIM_LIBRARY_KIBIS& aLib, std::string aPinNumber );
void SetBaseModel( const SIM_MODEL& aBaseModel ) override;
std::string GenerateSpiceDriver( const std::vector<SCH_FIELD>& aFields ) const;
protected:
void CreatePins( unsigned aSymbolPinCount ) override;
std::vector<std::pair<std::string, std::string>> m_ibisPins;
std::string m_componentName;
private:
bool requiresSpiceModelLine() const override { return true; }
@ -99,6 +103,8 @@ private:
static std::vector<PARAM::INFO> makePrbsWaveformParamInfos();
std::vector<std::string> m_ibisModels;
std::vector<std::pair<std::string, std::string>> m_ibisPins;
std::string m_componentName;
};
#endif // SIM_MODEL_KIBIS_H

View File

@ -887,7 +887,7 @@ int SCH_EDITOR_CONTROL::SimProbe( const TOOL_EVENT& aEvent )
std::vector<LIB_PIN*> pins = symbol->GetLibPins();
SIM_LIB_MGR mgr( m_frame->Prj() );
SIM_MODEL& model = mgr.CreateModel( *symbol ).second;
SIM_MODEL& model = mgr.CreateModel( *symbol ).model;
auto ref = std::string( symbol->GetRef( &m_frame->GetCurrentSheet() ).ToUTF8() );
std::vector<std::string> currentNames =