2016-08-11 12:41:07 +00:00
|
|
|
/*
|
|
|
|
* This program source code file is part of KiCad, a free EDA CAD application.
|
|
|
|
*
|
|
|
|
* Copyright (C) 2016 CERN
|
2022-03-09 01:40:59 +00:00
|
|
|
* Copyright (C) 2021-2022 KiCad Developers, see AUTHORS.txt for contributors.
|
2021-01-26 15:23:37 +00:00
|
|
|
*
|
2016-08-11 12:41:07 +00:00
|
|
|
* @author Tomasz Wlostowski <tomasz.wlostowski@cern.ch>
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU General Public License
|
2016-08-11 12:41:43 +00:00
|
|
|
* as published by the Free Software Foundation; either version 3
|
2016-08-11 12:41:07 +00:00
|
|
|
* 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:
|
2016-08-11 12:41:43 +00:00
|
|
|
* https://www.gnu.org/licenses/gpl-3.0.html
|
|
|
|
* or you may search the http://www.gnu.org website for the version 3 license,
|
2016-08-11 12:41:07 +00:00
|
|
|
* or you may write to the Free Software Foundation, Inc.,
|
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
|
|
|
|
*/
|
2016-08-11 12:41:01 +00:00
|
|
|
|
2016-08-11 12:41:07 +00:00
|
|
|
#ifndef NGSPICE_H
|
|
|
|
#define NGSPICE_H
|
2016-08-11 12:41:01 +00:00
|
|
|
|
2022-02-21 03:58:31 +00:00
|
|
|
#include <sim/spice_simulator.h>
|
|
|
|
#include <sim/sim_model.h>
|
|
|
|
#include <sim/sim_value.h>
|
2016-08-11 12:41:07 +00:00
|
|
|
|
2018-03-23 10:12:21 +00:00
|
|
|
#include <wx/dynlib.h>
|
2022-08-04 15:01:29 +00:00
|
|
|
|
2016-08-12 13:56:51 +00:00
|
|
|
#include <ngspice/sharedspice.h>
|
2022-08-04 15:01:29 +00:00
|
|
|
|
2022-02-21 03:58:31 +00:00
|
|
|
#include <enum_vector.h>
|
2016-08-12 13:56:51 +00:00
|
|
|
|
2021-05-05 17:16:32 +00:00
|
|
|
// We have an issue here where NGSPICE incorrectly used bool for years
|
|
|
|
// and defined it to be int when in C-mode. We cannot adjust the function
|
|
|
|
// signatures without re-writing sharedspice.h for KiCad.
|
|
|
|
// Instead, we maintain status-quo for older NGSPICE versions (<=34) and
|
|
|
|
// use the new signatures for newer versions
|
|
|
|
#ifndef NGSPICE_PACKAGE_VERSION
|
|
|
|
typedef bool NG_BOOL;
|
|
|
|
#endif
|
|
|
|
|
2016-08-11 12:41:07 +00:00
|
|
|
class wxDynamicLibrary;
|
|
|
|
|
2022-03-05 23:49:25 +00:00
|
|
|
|
2021-03-18 19:31:02 +00:00
|
|
|
class NGSPICE : public SPICE_SIMULATOR
|
|
|
|
{
|
2016-08-11 12:41:01 +00:00
|
|
|
public:
|
2016-08-11 12:41:07 +00:00
|
|
|
NGSPICE();
|
|
|
|
virtual ~NGSPICE();
|
|
|
|
|
2021-01-26 15:23:37 +00:00
|
|
|
///< @copydoc SPICE_SIMULATOR::Init()
|
2022-03-05 23:49:25 +00:00
|
|
|
void Init( const SPICE_SIMULATOR_SETTINGS* aSettings = nullptr ) override final;
|
|
|
|
|
|
|
|
///< @copydoc SPICE_SIMULATOR::Attach()
|
|
|
|
bool Attach( const std::shared_ptr<SIMULATION_MODEL>& aModel ) override final;
|
2016-08-11 12:42:17 +00:00
|
|
|
|
2022-03-05 23:49:25 +00:00
|
|
|
///< Load a netlist for the simulation
|
|
|
|
bool LoadNetlist( const std::string& aNetlist ) override final;
|
2016-08-11 12:42:17 +00:00
|
|
|
|
2021-01-26 15:23:37 +00:00
|
|
|
///< @copydoc SPICE_SIMULATOR::Run()
|
2022-03-05 23:49:25 +00:00
|
|
|
bool Run() override final;
|
2016-08-11 12:42:17 +00:00
|
|
|
|
2021-01-26 15:23:37 +00:00
|
|
|
///< @copydoc SPICE_SIMULATOR::Stop()
|
2022-03-05 23:49:25 +00:00
|
|
|
bool Stop() override final;
|
2016-08-11 12:42:17 +00:00
|
|
|
|
2021-01-26 15:23:37 +00:00
|
|
|
///< @copydoc SPICE_SIMULATOR::IsRunning()
|
2022-03-05 23:49:25 +00:00
|
|
|
bool IsRunning() override final;
|
2016-08-11 12:42:17 +00:00
|
|
|
|
2021-01-26 15:23:37 +00:00
|
|
|
///< @copydoc SPICE_SIMULATOR::Command()
|
2022-03-05 23:49:25 +00:00
|
|
|
bool Command( const std::string& aCmd ) override final;
|
2016-08-11 12:42:17 +00:00
|
|
|
|
2021-01-26 15:23:37 +00:00
|
|
|
///< @copydoc SPICE_SIMULATOR::GetXAxis()
|
2022-03-05 23:49:25 +00:00
|
|
|
std::string GetXAxis( SIM_TYPE aType ) const override final;
|
2016-08-11 12:41:31 +00:00
|
|
|
|
2021-01-27 22:15:38 +00:00
|
|
|
///< @copydoc SPICE_SIMULATOR::AllPlots()
|
2022-03-05 23:49:25 +00:00
|
|
|
std::vector<std::string> AllPlots() const override final;
|
2020-04-15 01:51:58 +00:00
|
|
|
|
2021-01-26 15:23:37 +00:00
|
|
|
///< @copydoc SPICE_SIMULATOR::GetPlot()
|
2022-03-05 23:49:25 +00:00
|
|
|
std::vector<COMPLEX> GetPlot( const std::string& aName, int aMaxLen = -1 ) override final;
|
2016-08-11 12:42:17 +00:00
|
|
|
|
2021-01-26 15:23:37 +00:00
|
|
|
///< @copydoc SPICE_SIMULATOR::GetRealPlot()
|
2022-03-05 23:49:25 +00:00
|
|
|
std::vector<double> GetRealPlot( const std::string& aName, int aMaxLen = -1 ) override final;
|
2016-08-11 12:42:17 +00:00
|
|
|
|
2021-01-26 15:23:37 +00:00
|
|
|
///< @copydoc SPICE_SIMULATOR::GetImagPlot()
|
2022-03-05 23:49:25 +00:00
|
|
|
std::vector<double> GetImagPlot( const std::string& aName, int aMaxLen = -1 ) override final;
|
2016-08-11 12:42:17 +00:00
|
|
|
|
2021-01-26 15:23:37 +00:00
|
|
|
///< @copydoc SPICE_SIMULATOR::GetMagPlot()
|
2022-03-05 23:49:25 +00:00
|
|
|
std::vector<double> GetMagPlot( const std::string& aName, int aMaxLen = -1 ) override final;
|
2016-08-11 12:42:17 +00:00
|
|
|
|
2021-01-26 15:23:37 +00:00
|
|
|
///< @copydoc SPICE_SIMULATOR::GetPhasePlot()
|
2022-03-05 23:49:25 +00:00
|
|
|
std::vector<double> GetPhasePlot( const std::string& aName, int aMaxLen = -1 ) override final;
|
2016-08-11 12:41:07 +00:00
|
|
|
|
2022-03-05 23:49:25 +00:00
|
|
|
std::vector<std::string> GetSettingCommands() const override final;
|
2021-03-18 19:31:02 +00:00
|
|
|
|
2021-01-26 15:23:37 +00:00
|
|
|
///< @copydoc SPICE_SIMULATOR::GetNetlist()
|
2022-03-05 23:49:25 +00:00
|
|
|
virtual const std::string GetNetlist() const override final;
|
2018-03-03 16:36:26 +00:00
|
|
|
|
2022-05-28 20:40:24 +00:00
|
|
|
///< @copydoc SIMULATOR::Clean()
|
|
|
|
void Clean() override final;
|
|
|
|
|
2016-08-11 12:41:07 +00:00
|
|
|
private:
|
2016-08-15 11:52:13 +00:00
|
|
|
void init();
|
2016-08-11 12:41:01 +00:00
|
|
|
|
2018-03-21 09:42:39 +00:00
|
|
|
// Performs DLL initialization, obtains function pointers
|
|
|
|
void init_dll();
|
|
|
|
|
|
|
|
// ngspice library functions
|
2021-03-18 19:31:02 +00:00
|
|
|
typedef void ( *ngSpice_Init )( SendChar*, SendStat*, ControlledExit*, SendData*, SendInitData*,
|
|
|
|
BGThreadRunning*, void* );
|
|
|
|
typedef int ( *ngSpice_Circ )( char** circarray );
|
|
|
|
typedef int ( *ngSpice_Command )( char* command );
|
|
|
|
typedef pvector_info ( *ngGet_Vec_Info )( char* vecname );
|
2020-04-15 01:51:58 +00:00
|
|
|
typedef char* ( *ngSpice_CurPlot )( void );
|
2021-03-18 19:31:02 +00:00
|
|
|
typedef char** ( *ngSpice_AllPlots )( void );
|
|
|
|
typedef char** ( *ngSpice_AllVecs )( char* plotname );
|
|
|
|
typedef bool ( *ngSpice_Running )( void );
|
2018-03-21 09:42:39 +00:00
|
|
|
|
2021-01-26 15:23:37 +00:00
|
|
|
///< Handle to DLL functions
|
2018-03-21 09:42:39 +00:00
|
|
|
ngSpice_Init m_ngSpice_Init;
|
|
|
|
ngSpice_Circ m_ngSpice_Circ;
|
|
|
|
ngSpice_Command m_ngSpice_Command;
|
|
|
|
ngGet_Vec_Info m_ngGet_Vec_Info;
|
2020-04-15 01:51:58 +00:00
|
|
|
ngSpice_CurPlot m_ngSpice_CurPlot;
|
2018-03-21 09:42:39 +00:00
|
|
|
ngSpice_AllPlots m_ngSpice_AllPlots;
|
|
|
|
ngSpice_AllVecs m_ngSpice_AllVecs;
|
|
|
|
ngSpice_Running m_ngSpice_Running;
|
|
|
|
|
2018-03-23 10:12:21 +00:00
|
|
|
wxDynamicLibrary m_dll;
|
2018-03-21 09:42:39 +00:00
|
|
|
|
2021-05-05 17:16:32 +00:00
|
|
|
|
2021-01-26 15:23:37 +00:00
|
|
|
///< Execute commands from a file
|
2016-10-13 11:25:06 +00:00
|
|
|
bool loadSpinit( const std::string& aFileName );
|
|
|
|
|
2021-01-26 15:23:37 +00:00
|
|
|
///< Check a few different locations for codemodel files and returns one if it exists.
|
2016-10-13 11:25:06 +00:00
|
|
|
std::string findCmPath() const;
|
|
|
|
|
2021-01-26 15:23:37 +00:00
|
|
|
///< Load codemodel files from a directory.
|
2016-10-13 11:25:06 +00:00
|
|
|
bool loadCodemodels( const std::string& aPath );
|
|
|
|
|
2016-08-11 12:42:17 +00:00
|
|
|
// Callback functions
|
2021-05-05 17:49:40 +00:00
|
|
|
static int cbSendChar( char* what, int aId, void* aUser );
|
|
|
|
static int cbSendStat( char* what, int aId, void* aUser );
|
|
|
|
static int cbBGThreadRunning( NG_BOOL aFinished, int aId, void* aUser );
|
|
|
|
static int cbControlledExit( int aStatus, NG_BOOL aImmediate, NG_BOOL aExitOnQuit, int aId,
|
|
|
|
void* aUser );
|
2016-08-11 12:41:17 +00:00
|
|
|
|
2021-07-09 19:35:47 +00:00
|
|
|
// Assure ngspice is in a valid state and reinitializes it if need be.
|
2018-03-21 16:22:56 +00:00
|
|
|
void validate();
|
2016-08-18 16:10:00 +00:00
|
|
|
|
2021-07-09 19:35:47 +00:00
|
|
|
///< Error flag indicating that ngspice needs to be reloaded.
|
2018-03-21 09:42:39 +00:00
|
|
|
bool m_error;
|
|
|
|
|
2021-07-09 19:35:47 +00:00
|
|
|
///< Ngspice should be initialized only once.
|
2016-08-18 16:10:00 +00:00
|
|
|
static bool m_initialized;
|
2018-03-03 16:36:26 +00:00
|
|
|
|
2021-07-09 19:35:47 +00:00
|
|
|
///< Current netlist
|
2018-03-03 16:36:26 +00:00
|
|
|
std::string m_netlist;
|
2016-08-11 12:41:01 +00:00
|
|
|
};
|
|
|
|
|
2016-08-11 12:41:07 +00:00
|
|
|
#endif /* NGSPICE_H */
|