Skip to content

Commit

Permalink
netlist: fix bug, prepare for future changes and improve readability (#…
Browse files Browse the repository at this point in the history
…9947)

* netlist: fix bug, prepare for future changes and improve readability

- fix a bug where a net processing error may trigger a nullptr access
- applied some clang-tidy recommendations
- add no_return to plib::terminate
- properly encapsulate dynamic_cast usage
- more review of noexcept
- added a clang-format file. Over time, all source files will be
  processed with clang-format
- Used clang format on a number of files

- Rewrote 74174

- all device constructors now use a struct to pass data on
  to base classes. Neither netlist state nor the name are intended
  to be used in a constructor. After the base class was
  constructed, they can be accessed by state() and name().

- The device construction macros can now be removed. Changes to
  the core will not need to be reflected in constructors.

- Change truth table macros so that going forward NETLIST_END and
  TRUTH_TABLE_END can be replaced by a closing curly brace. netlists can
  than use curly braces enclosed blocks.

- more clang-format
- removed some macros completely
- all derived classes from base_device_t now don't use macros
  any longer.
- as a result, delegator_t was removed. This class was only used
  to support macros :-(
  • Loading branch information
couriersud committed Jun 20, 2022
1 parent 3fbfe0b commit 0dad442
Show file tree
Hide file tree
Showing 82 changed files with 3,653 additions and 2,886 deletions.
10 changes: 10 additions & 0 deletions src/devices/machine/netlist.cpp
Expand Up @@ -1252,6 +1252,16 @@ uint64_t netlist_mame_cpu_device::execute_cycles_to_clocks(uint64_t cycles) cons
return cycles;
}

netlist::netlist_time_ext netlist_mame_cpu_device::nltime_ext_from_clocks(unsigned c) const noexcept
{
return (m_div * c).shr(MDIV_SHIFT);
}

netlist::netlist_time netlist_mame_cpu_device::nltime_from_clocks(unsigned c) const noexcept
{
return static_cast<netlist::netlist_time>((m_div * c).shr(MDIV_SHIFT));
}

void netlist_mame_cpu_device::execute_run()
{
//m_ppc = m_pc; // copy PC to previous PC
Expand Down
15 changes: 5 additions & 10 deletions src/devices/machine/netlist.h
Expand Up @@ -193,16 +193,6 @@ class netlist_mame_cpu_device : public netlist_mame_device,
void update_icount(netlist::netlist_time_ext time) noexcept;
void check_mame_abort_slice() noexcept;

netlist::netlist_time_ext nltime_ext_from_clocks(unsigned c) const noexcept
{
return (m_div * c).shr(MDIV_SHIFT);
}

netlist::netlist_time nltime_from_clocks(unsigned c) const noexcept
{
return static_cast<netlist::netlist_time>((m_div * c).shr(MDIV_SHIFT));
}

protected:
// netlist_mame_device
virtual void nl_register_devices(netlist::nlparse_t &parser) const override;
Expand All @@ -229,6 +219,9 @@ class netlist_mame_cpu_device : public netlist_mame_device,
address_space_config m_program_config;

private:
netlist::netlist_time_ext nltime_ext_from_clocks(unsigned c) const noexcept;
netlist::netlist_time nltime_from_clocks(unsigned c) const noexcept;

int m_icount;
netlist::netlist_time_ext m_div;
netlist::netlist_time_ext m_rem;
Expand All @@ -255,6 +248,8 @@ class netlist_disassembler : public util::disasm_interface

// ----------------------------------------------------------------------------------------
// netlist_mame_sound_input_buffer
//
// This is a wrapper device to provide operator[] on read_stream_view.
// ----------------------------------------------------------------------------------------

class netlist_mame_sound_input_buffer : public read_stream_view
Expand Down
5 changes: 5 additions & 0 deletions src/devices/video/fixfreq.cpp
Expand Up @@ -19,9 +19,14 @@
#include "render.h"
#include "ui/uimain.h"

#include <cstdio>

// for quick and dirty debugging
#define VERBOSE 0
#define LOG_GENERAL (1U << 0)

#define LOG_OUTPUT_STREAM std::cerr

#include "logmacro.h"

#include <algorithm>
Expand Down
75 changes: 27 additions & 48 deletions src/lib/netlist/analog/nld_bjt.cpp
Expand Up @@ -130,32 +130,6 @@ namespace analog

};

// Have a common start for transistors

NETLIB_BASE_OBJECT(QBJT)
{
public:

NETLIB_CONSTRUCTOR_EX(QBJT, const pstring &model = "NPN")
, m_model(*this, "MODEL", model)
, m_qtype(bjt_type::BJT_NPN)
{
}

NETLIB_IS_DYNAMIC(true)

//NETLIB_RESETI();

bjt_type qtype() const noexcept { return m_qtype; }
bool is_qtype(bjt_type atype) const noexcept { return m_qtype == atype; }
void set_qtype(bjt_type atype) noexcept { m_qtype = atype; }
protected:

param_model_t m_model;
private:
bjt_type m_qtype;
};

// -----------------------------------------------------------------------------
// nld_QBJT_switch
// -----------------------------------------------------------------------------
Expand All @@ -174,9 +148,12 @@ namespace analog
// E
//

NETLIB_OBJECT_DERIVED(QBJT_switch, QBJT)
class nld_QBJT_switch : public base_device_t
{
NETLIB_CONSTRUCTOR(QBJT_switch)
public: \
nld_QBJT_switch(constructor_param_t data)
: base_device_t(data)
, m_model(*this, "MODEL", "NPN")
, m_bjt_model(m_model)
, m_RB(*this, "m_RB", NETLIB_DELEGATE(terminal_handler))
, m_RC(*this, "m_RC", NETLIB_DELEGATE(terminal_handler))
Expand Down Expand Up @@ -205,14 +182,17 @@ namespace analog
m_RC.solver()->solve_now();
}

NETLIB_IS_DYNAMIC(true)

NETLIB_UPDATE_PARAMI();
NETLIB_UPDATE_TERMINALSI();

private:
param_model_t m_model;
bjt_model_t m_bjt_model;
nld_two_terminal m_RB;
nld_two_terminal m_RC;
nld_two_terminal m_BC;
NETLIB_NAME(two_terminal) m_RB;
NETLIB_NAME(two_terminal) m_RC;
NETLIB_NAME(two_terminal) m_BC;

nl_fptype m_gB; // base conductance / switch on
nl_fptype m_gC; // collector conductance / switch on
Expand All @@ -226,10 +206,12 @@ namespace analog
// -----------------------------------------------------------------------------


NETLIB_OBJECT_DERIVED(QBJT_EB, QBJT)
class nld_QBJT_EB : public base_device_t
{
public:
NETLIB_CONSTRUCTOR(QBJT_EB)
public: \
nld_QBJT_EB(constructor_param_t data)
: base_device_t(data)
, m_model(*this, "MODEL", "NPN")
, m_bjt_model(m_model)
, m_gD_BC(*this, "m_D_BC")
, m_gD_BE(*this, "m_D_BE")
Expand Down Expand Up @@ -275,17 +257,20 @@ namespace analog
m_D_CB.solver()->solve_now();
}

NETLIB_IS_DYNAMIC(true)

NETLIB_UPDATE_PARAMI();
NETLIB_UPDATE_TERMINALSI();

private:
param_model_t m_model;
bjt_model_t m_bjt_model;
generic_diode<diode_e::BIPOLAR> m_gD_BC;
generic_diode<diode_e::BIPOLAR> m_gD_BE;

nld_two_terminal m_D_CB; // gcc, gce - gcc, gec - gcc, gcc - gce | Ic
nld_two_terminal m_D_EB; // gee, gec - gee, gce - gee, gee - gec | Ie
nld_two_terminal m_D_EC; // 0, -gec, -gcc, 0 | 0
NETLIB_NAME(two_terminal) m_D_CB; // gcc, gce - gcc, gec - gcc, gcc - gce | Ic
NETLIB_NAME(two_terminal) m_D_EB; // gee, gec - gee, gce - gee, gee - gec | Ie
NETLIB_NAME(two_terminal) m_D_EC; // 0, -gec, -gcc, 0 | 0

nl_fptype m_alpha_f;
nl_fptype m_alpha_r;
Expand All @@ -304,8 +289,8 @@ namespace analog
{
if (m_RB.solver() == nullptr && m_RC.solver() == nullptr)
throw nl_exception(MF_DEVICE_FRY_1(this->name()));
NETLIB_NAME(QBJT)::reset();
const auto zero(nlconst::zero());

static constexpr const auto zero(nlconst::zero());

m_state_on = 0;

Expand All @@ -323,9 +308,6 @@ namespace analog
nl_fptype NF = m_bjt_model.m_NF;
//nl_fptype VJE = m_bjt_model.dValue("VJE", 0.75);

// FIXME: check for PNP as well and bail out
set_qtype(m_bjt_model.m_type);

nl_fptype alpha = BF / (nlconst::one() + BF);

diode d(IS, NF);
Expand All @@ -349,7 +331,7 @@ namespace analog

NETLIB_UPDATE_TERMINALS(QBJT_switch)
{
const nl_fptype m = (is_qtype( bjt_type::BJT_NPN) ? 1 : -1);
const nl_fptype m = (m_bjt_model.m_type == bjt_type::BJT_NPN) ? nlconst::one() : -nlconst::one();

const unsigned new_state = (m_RB.deltaV() * m > m_V ) ? 1 : 0;
if (m_state_on ^ new_state)
Expand All @@ -374,7 +356,7 @@ namespace analog
{
if (m_D_EB.solver() == nullptr && m_D_CB.solver() == nullptr)
throw nl_exception(MF_DEVICE_FRY_1(this->name()));
NETLIB_NAME(QBJT)::reset();

if (m_CJE)
{
m_CJE->reset();
Expand All @@ -389,7 +371,7 @@ namespace analog

NETLIB_UPDATE_TERMINALS(QBJT_EB)
{
const nl_fptype polarity(qtype() == bjt_type::BJT_NPN ? nlconst::one() : -nlconst::one());
const nl_fptype polarity(m_bjt_model.m_type == bjt_type::BJT_NPN ? nlconst::one() : -nlconst::one());

m_gD_BE.update_diode(-m_D_EB.deltaV() * polarity);
m_gD_BC.update_diode(-m_D_CB.deltaV() * polarity);
Expand Down Expand Up @@ -422,9 +404,6 @@ namespace analog
nl_fptype NR = m_bjt_model.m_NR;
//nl_fptype VJE = m_m_bjt_model.dValue("VJE", 0.75);

// FIXME: check for PNP as well and bail out
set_qtype(m_bjt_model.m_type);

m_alpha_f = BF / (nlconst::one() + BF);
m_alpha_r = BR / (nlconst::one() + BR);

Expand Down
66 changes: 20 additions & 46 deletions src/lib/netlist/analog/nld_mosfet.cpp
Expand Up @@ -149,44 +149,17 @@ namespace analog
param_model_t::value_base_t<int> m_CAPMOD; //!< Capacitance model (0=no model 2=Meyer)
};

// Have a common start for mosfets

NETLIB_BASE_OBJECT(FET)
{
public:
enum q_type {
FET_NMOS,
FET_PMOS
};

NETLIB_CONSTRUCTOR(FET)
, m_model(*this, "MODEL", "NMOS")
, m_qtype(FET_NMOS)
{
}

NETLIB_IS_DYNAMIC(true)

//NETLIB_RESETI();

q_type qtype() const noexcept { return m_qtype; }
bool is_qtype(q_type atype) const noexcept { return m_qtype == atype; }
void set_qtype(q_type atype) noexcept { m_qtype = atype; }
protected:

param_model_t m_model;
private:
q_type m_qtype;
};

// -----------------------------------------------------------------------------
// nld_QBJT_EB
// nld_MOSFET
// -----------------------------------------------------------------------------

NETLIB_OBJECT_DERIVED(MOSFET, FET)
class nld_MOSFET : public base_device_t
{
public:
NETLIB_CONSTRUCTOR(MOSFET)
public: \
nld_MOSFET(constructor_param_t data)
: base_device_t(data)
, m_model(*this, "MODEL", "NMOS")
, m_DG(*this, "m_DG", NETLIB_DELEGATE(terminal_handler))
, m_SG(*this, "m_SG", NETLIB_DELEGATE(terminal_handler))
, m_SD(*this, "m_SD", NETLIB_DELEGATE(terminal_handler))
Expand All @@ -204,7 +177,7 @@ namespace analog
, m_lambda(nlconst::zero())
, m_Leff(nlconst::zero())
, m_CoxWL(nlconst::zero())
, m_polarity(qtype() == FET_NMOS ? nlconst::one() : -nlconst::one())
//S, m_polarity(qtype() == FET_NMOS ? nlconst::one() : -nlconst::one())
, m_Cgb(nlconst::zero())
, m_Cgs(nlconst::zero())
, m_Cgd(nlconst::zero())
Expand All @@ -222,8 +195,7 @@ namespace analog
connect(m_SG.N(), m_DG.N());
connect(m_DG.P(), m_SD.N());

set_qtype((m_model.type() == "NMOS_DEFAULT") ? FET_NMOS : FET_PMOS);
m_polarity = (qtype() == FET_NMOS ? nlconst::one() : -nlconst::one());
m_polarity = (m_model.type() == "NMOS_DEFAULT" ? nlconst::one() : -nlconst::one());

m_capacitor_model = m_model_acc.m_CAPMOD;
//# printf("capmod %d %g %g\n", m_capacitor_model, (nl_fptype)m_model_acc.m_VTO, m_polarity);
Expand Down Expand Up @@ -296,6 +268,7 @@ namespace analog
//#printf("Cox: %g\n", m_Cox);
}

NETLIB_IS_DYNAMIC(true)
NETLIB_IS_TIMESTEP(true || m_capacitor_model != 0)

NETLIB_TIMESTEPI()
Expand Down Expand Up @@ -328,7 +301,6 @@ namespace analog

NETLIB_RESETI()
{
NETLIB_NAME(FET)::reset();
// Bulk diodes

m_D_BD.set_param(m_model_acc.m_ISD, m_model_acc.m_N, exec().gmin(), constants::T0());
Expand All @@ -351,9 +323,11 @@ namespace analog

private:

nld_two_terminal m_DG;
nld_two_terminal m_SG;
nld_two_terminal m_SD;
param_model_t m_model;

NETLIB_NAME(two_terminal) m_DG;
NETLIB_NAME(two_terminal) m_SG;
NETLIB_NAME(two_terminal) m_SD;

generic_diode<diode_e::MOS> m_D_BD;
#if (!BODY_CONNECTED_TO_SOURCE)
Expand Down Expand Up @@ -594,14 +568,14 @@ namespace analog
const nl_fptype gSSBB = gSS + gBB + gBS + gSB;
const auto zero(nlconst::zero());
// S G
m_SG.set_mat( gSSBB, gSG + gBG, +(IS + IB), // S
gGS + gGB, gGG, IG ); // G
m_SG.set_mat( gSSBB, gSG + gBG, +(IS + IB), // S
gGS + gGB, gGG, IG ); // G
// D G
m_DG.set_mat( gDD, gDG, +ID, // D
gGD, zero, zero ); // G
m_DG.set_mat( gDD, gDG, +ID, // D
gGD, zero, zero ); // G
// S D
m_SD.set_mat( zero, gSD + gBD, zero, // S
gDS + gDB, zero, zero); // D
m_SD.set_mat( zero, gSD + gBD, zero, // S
gDS + gDB, zero, zero ); // D
}

NETLIB_UPDATE_PARAM(MOSFET)
Expand Down

0 comments on commit 0dad442

Please sign in to comment.