// Model generated by Verilog-A Distiller.
// (c)2026 Arpad Buermen. All rights reserved.
// SPDX-License-Identifier: AGPL-3.0-or-later
// 
// SPICE flavour: ngspice
// Version      : ngspice-44-549-ga4ff5a204
// Origin       : https://sourceforge.net/u/arpadbuermen/ngspice/ci/vadng-pre-48/tree/
// Date         : 2026-08-18 12:18:08 UTC
// Module name  : sp_capacitor
// Model variant: default
// 
// Notes:
//   Parameter m was replaced with builtin Verilog-A parameter $mfactor.
//
//   All parameters and output variables refer to a single parallel instance,
//   even if $mfactor>1.
//
//   Model parameters tc1, tc2, w, l, bv_max, and cap were renamed to 
//   model_tc1, model_tc2, model_w, model_l, model_bv_max, and model_cap 
//   due to name conflicts with instance parameters. 
//   
//   Parameter ic was removed. Initial conditions should be set via the 
//   simulator's initial conditions mechanism.
//
// SPICE variable mapping
//   scale -> $simparam("scale", 1)
//
// CKTcircuit member mapping
//   CKTnomTemp -> ('tnom', '($simparam("tnom", 27)+273.15)')
//      CKTtemp -> (None, '$temperature')
//
// Source files and copyright information:
//   cap/capdefs.h
//     Copyright 1990 Regents of the University of California.  All rights reserved.
//     Author: 1985 Thomas L. Quarles
//     Modified: September 2003 Paolo Nenzi
//   cap/cap.c
//     Copyright 1990 Regents of the University of California.  All rights reserved.
//     Author: 1985 Thomas L. Quarles
//     Modified: September 2003 - Paolo Nenzi
//   cap/capparam.c
//   cap/capmpar.c
//   cap/capask.c
//   cap/capmask.c
//   cap/capsetup.c
//   cap/captemp.c
//   cap/capload.c
//     Copyright 1990 Regents of the University of California.  All rights reserved.
//     Author: 1985 Thomas L. Quarles
//     Modified: September 2003 Paolo Nenzi

`include "constants.vams"
`include "disciplines.vams"

module sp_capacitor(pos, neg);
  inout pos, neg;
  electrical pos, neg;

  // Instance parameters
  (* desc = "Device capacitance", type = "instance" *) parameter real capacitance = 0;
  aliasparam cap = capacitance;
  aliasparam c = capacitance;
  (* desc = "Instance operating temperature", type = "instance" *) parameter real temp = 0;
  (* desc = "Instance temperature difference from the rest of the circuit", type = "instance" *) parameter real dtemp = 0.0;
  (* desc = "Device width", type = "instance" *) parameter real w = 0;
  (* desc = "Device length", type = "instance" *) parameter real l = 0;
  (* desc = "First order temp. coefficient", type = "instance" *) parameter real tc1 = 0;
  (* desc = "Second order temp. coefficient", type = "instance" *) parameter real tc2 = 0;
  (* desc = "maximum voltage over capacitance", type = "instance" *) parameter real bv_max = 0;
  (* desc = "Scale factor", type = "instance" *) parameter real scale = 1.0;

  // Model parameters
  (* desc = "Model capacitance" *) parameter real model_cap = 0.0;
  (* desc = "Bottom Capacitance per area" *) parameter real cj = 0;
  aliasparam cox = cj;
  (* desc = "Sidewall capacitance per meter" *) parameter real cjsw = 0.0;
  aliasparam capsw = cjsw;
  (* desc = "Default width" *) parameter real defw = 1e-05;
  aliasparam model_w = defw;
  (* desc = "Default length" *) parameter real defl = 0.0;
  aliasparam model_l = defl;
  (* desc = "width correction factor" *) parameter real narrow = 0.0;
  (* desc = "length correction factor" *) parameter real short = 0.0;
  (* desc = "length and width correction factor" *) parameter real del = 0.0;
  (* desc = "First order temp. coefficient" *) parameter real model_tc1 = 0.0;
  (* desc = "Second order temp. coefficient" *) parameter real model_tc2 = 0.0;
  (* desc = "Parameter measurement temperature" *) parameter real tnom = 0;
  (* desc = "Relative dielectric constant" *) parameter real di = 0.0;
  (* desc = "Insulator thickness" *) parameter real thick = 0.0;
  (* desc = "maximum voltage over capacitance" *) parameter real model_bv_max = 1e+99;

  // Instance output variables

  // Model output variables

  // Instance fields
  real CAPtemp = 0;
  real CAPdtemp = 0;
  real CAPcapac = 0;
  real CAPcapacinst = 0;
  real CAPwidth = 0;
  real CAPlength = 0;
  real i_CAPbv_max = 0;
  integer CAPtempGiven = 0;
  integer CAPwidthGiven = 0;
  integer CAPlengthGiven = 0;

  // Model fields
  real CAPtnom = 0;
  real CAPcj = 0;
  real CAPnarrow = 0;
  real CAPshort = 0;
  integer CAPtnomGiven = 0;

  // States
  real CAPqcap = 0;
  real CAPccap = 0;

  // Model parameter access locals

  // Instance parameter access locals

  // Device setup locals

  // Device temperature dependence locals
  real difference = 0;
  real factor = 0;
  real temp_tc1 = 0;
  real temp_tc2 = 0;

  // Device load locals
  integer limited = 0;
  real vcap = 0;
  real geq = 0;
  real ceq = 0;

  // Device noise locals

  // Global constants
  real CONSTroot2 = sqrt(2.0);
  real CONSTvt0 = 1.38064852e-23*(27+273.15)/1.6021766208e-19;
  real CONSTKoverQ = 1.38064852e-23/1.6021766208e-19;
  real CONSTe = exp(1.0);
  real VACONSTcharge = 1.6021766208e-19;
  real VACONSTboltz = 1.38064852e-23;
  real VACONST_tnom = ($simparam("tnom", 27)+273.15);
  real cpscale = $simparam("scale", 1);

  // Evaluation constants

  // Translated function definitions

  // Analog block
  analog begin

    // Instance field assignments
    if ($param_given(capacitance)) begin
      CAPcapac = capacitance;
      CAPcapacinst = CAPcapac;
    end
    if ($param_given(temp)) begin
      CAPtemp = temp+273.15;
      CAPtempGiven = 1;
    end
    if ($param_given(dtemp)) begin
      CAPdtemp = dtemp;
    end
    if ($param_given(w)) begin
      CAPwidth = w*cpscale;
      CAPwidthGiven = 1;
    end
    if ($param_given(l)) begin
      CAPlength = l*cpscale;
      CAPlengthGiven = 1;
    end
    if ($param_given(bv_max)) begin
      i_CAPbv_max = bv_max;
    end

    // Model field assignments
    if ($param_given(cj)) begin
      CAPcj = cj;
    end
    if ($param_given(narrow)) begin
      CAPnarrow = narrow;
    end
    if ($param_given(short)) begin
      CAPshort = short;
    end
    if ($param_given(tnom)) begin
      CAPtnom = tnom+273.15;
      CAPtnomGiven = 1;
    end

    // Evaluation constants

    // Tag: setup, function: CAPsetup
    if (!$param_given(narrow)) begin
      CAPnarrow = 0.0;
    end
    if (!$param_given(short)) begin
      CAPshort = 0.0;
    end
    if (!CAPtnomGiven) begin
      CAPtnom = VACONST_tnom;
    end
    if (!$param_given(cj)) begin
      if ($param_given(thick)&&thick>0.0) begin
        if ($param_given(di)) begin
          CAPcj = di*(1.0/(4.0*3.1415926535897932384626433832795*1E-7*299792458*299792458))/thick;
        end else begin
          CAPcj = 3.9*(1.0/(4.0*3.1415926535897932384626433832795*1E-7*299792458*299792458))/thick;
        end
      end else begin
        CAPcj = 0.0;
      end
    end
    if ($param_given(del)) begin
      if (!$param_given(narrow)) begin
        CAPnarrow = 2*del;
      end
      if (!$param_given(short)) begin
        CAPshort = 2*del;
      end
    end
    if (!CAPlengthGiven) begin
      CAPlength = 0;
    end
    if (!$param_given(bv_max)) begin
      i_CAPbv_max = model_bv_max;
    end

    // Tag: temp, function: CAPtemp
    if (!CAPtempGiven) begin
      CAPtemp = $temperature;
      if (!$param_given(dtemp)) begin
        CAPdtemp = 0.0;
      end
    end else begin
      CAPdtemp = 0.0;
      if ($param_given(dtemp)) begin
        $warning("Instance temperature specified, dtemp ignored");
      end
    end
    if (!CAPwidthGiven) begin
      CAPwidth = defw;
    end
    if (!$param_given(capacitance)) begin
      if (!$param_given(model_cap)) begin
        CAPcapac = CAPcj*(CAPwidth-CAPnarrow)*(CAPlength-CAPshort)+cjsw*2*(CAPlength-CAPshort+(CAPwidth-CAPnarrow));
      end else begin
        CAPcapac = model_cap;
      end
    end else begin
      CAPcapac = CAPcapacinst;
    end
    difference = CAPtemp+CAPdtemp-CAPtnom;
    if ($param_given(tc1)) begin
      temp_tc1 = tc1;
    end else begin
      temp_tc1 = model_tc1;
    end
    if ($param_given(tc2)) begin
      temp_tc2 = tc2;
    end else begin
      temp_tc2 = model_tc2;
    end
    factor = 1.0+temp_tc1*difference+temp_tc2*difference*difference;
    CAPcapac = CAPcapac*factor*scale;

    // Tag: load, function: CAPload
    limited = 0;
    vcap = V(pos, neg);
    CAPqcap = CAPcapac*vcap;
    geq = 0;
    CAPccap = ddt(CAPqcap);
    ceq = CAPccap;
    
    // Residuals
    I(pos) <+ geq*V(pos, neg)+ceq;
    I(neg) <+ geq*V(neg, pos)+-ceq;

    // Instance output variable computation

    // Model output variable computation

  end
endmodule
