Difference between revisions of "Ocs package"

From Octave
Jump to navigation Jump to search
Line 19: Line 19:
 
Below we show three methods for constructing the circuit data structure
 
Below we show three methods for constructing the circuit data structure
  
__TOC__
+
* [[Ocs package#Build the AND GATE structure directly| using an Octave script ]]
 
+
* [[Ocs package#Build the AND gate circuit structure parsing a .spc file| parsing a SPICE netlist]]
{{Code|Load the AND circuit structure parsing a .spc file |<syntaxhighlight lang="octave" style="font-size:13px">
+
* [[Ocs package#Build the AND gate circuit structure parsing an IFF netlist| parsing an IFF netlist]]
outstruct = prs_spice ("and");
 
</syntaxhighlight>
 
}}
 
 
 
{{Code|Load the AND circuit structure parsing an IFF netlist |<syntaxhighlight lang="octave" style="font-size:13px">
 
outstruct = prs_iff ("and");
 
</syntaxhighlight>
 
}}
 
 
 
  
 +
Once the circuit data structure is loaded the simulation can be started by the following commands
  
 
{{Code|Run the AND gate simulation |<syntaxhighlight lang="octave" style="font-size:13px">
 
{{Code|Run the AND gate simulation |<syntaxhighlight lang="octave" style="font-size:13px">
Line 45: Line 37:
  
  
 +
==== Build the AND GATE structure directly ====
 +
 +
 +
 +
==== Build the AND gate circuit structure parsing an IFF netlist ====
 +
 +
{{Code|Load the AND circuit structure parsing an IFF netlist |<syntaxhighlight lang="octave" style="font-size:13px">
 +
outstruct = prs_iff ("and");
 +
</syntaxhighlight>
 +
}}
  
==== Using an IFF netlist ====
 
 
{{Code|IFF netlist for the AND gate (.cir file)|<syntaxhighlight lang="text" style="font-size:13px">
 
{{Code|IFF netlist for the AND gate (.cir file)|<syntaxhighlight lang="text" style="font-size:13px">
 
% 0.1b1
 
% 0.1b1
Line 172: Line 173:
  
 
endfunction
 
endfunction
 +
</syntaxhighlight>
 +
}}
 +
 +
 +
==== Build the AND gate circuit structure parsing a .spc file ====
 +
 +
{{Code|Load the AND circuit structure parsing a .spc file |<syntaxhighlight lang="octave" style="font-size:13px">
 +
outstruct = prs_spice ("and");
 
</syntaxhighlight>
 
</syntaxhighlight>
 
}}
 
}}

Revision as of 23:01, 21 September 2015

OCS : Octave Circuit Simulator

History and Motivation

Problem Formulation

Data Structure

File Formats

Tutorials

A CMOS AND GATE

Schematic for a CMOS AND gate

Here we show how to set up the simulation of the CMOS AND gate in the figure. The circuit has

  • 9 Elements
    • 6 MOSFETs (3 n-type + 3 p-type)
    • 3 Voltage sources

Below we show three methods for constructing the circuit data structure

Once the circuit data structure is loaded the simulation can be started by the following commands

Code: Run the AND gate simulation
x         = [.5 .5 .33 .66 .5 1 0 0 1 ]';
t         = linspace (0, .5, 100);
pltvars   = {"Va", "Vb", "Va_and_b"};
dmp       = .2;
tol       = 1e-15;
maxit     = 100;
out       = tst_backward_euler (outstruct, x, t, tol, maxit, pltvars);


Build the AND GATE structure directly

Build the AND gate circuit structure parsing an IFF netlist

Code: Load the AND circuit structure parsing an IFF netlist
outstruct = prs_iff ("and");
Code: IFF netlist for the AND gate (.cir file)
% 0.1b1
% A Simple CMOS AND GATE
%
% N-Mosfets
% There are 3 N-Mosfets
Mnmosfet simple 4 3
3 3
k          Vth		rd
1e-4       0.1		1e7
1e-4       0.1		1e7
1e-4       0.1		1e7
1 3 4 0 
2 0 3 0 
4 0 5 0 
%
% P-Mosfets
Mpmosfet simple 4 3
3 3
k           Vth		rd
-1e-4       -0.1	1e7
-1e-4       -0.1	1e7
-1e-4       -0.1	1e7
1 6 4 6 
2 6 4 6 
4 6 5 6
%
% Input voltage sources
Mvoltagesources sinwave 2 4
2 4
Ampl      f       delay     shift
0.5       1       0.0       0.5
0.5       2       0.25      0.5
1 0  
2 0  
END
%
% Power supply
Mvoltagesources DC  2 1
1 1
V
1
6 0  
END
Code: IFF netlist for the AND gate (.nms file)
% 0.1b1
1 Va
2 Vb
5 Va_and_b
6 Vdd
7 I1
8 I2 
9 I3
Code: Model evaluator file for simple MOSFET models
function [a,b,c] = Mnmosfet (string, parameters, parameternames, extvar, intvar, t)   
  
  switch string

    case 'simple',
      
      rd = 1e6;
      
      for ii=1:length(parameternames)
        eval([parameternames{ii} "=",...
              num2str(parameters(ii)) " ;"])    
      endfor
      
      vg   = extvar(1);
      vs   = extvar(2);
      vd   = extvar(3);
      vb   = extvar(4);
      
      vgs  = vg-vs;
      vds  = vd-vs;
      
      if (vgs < Vth)
        
        
        gm = 0;
        gd = 1/rd;
        id = vds*gd;
        
      elseif ((vgs-Vth)>=(vds))&(vds>=0)
        
        id = k*((vgs-Vth)*vds-(vds^2)/2)+vds/rd;
        gm = k*vds;
        gd = k*(vgs-Vth-vds)+1/rd;
        
      elseif ((vgs-Vth)>=(vds))&(vds<0)
        
        gm = 0;
        gd = 1/rd;
        id = vds*gd;
        
      else # (i.e. if 0 <= vgs-vth <= vds)
        
        id = k*(vgs-Vth)^2/2+vds/rd;
        gm = k*(vgs-Vth);
        gd = 1/rd;
        
      endif
      
      a = zeros(4);
      
      b = [ 0     0         0   0;
           -gm   (gm+gd)   -gd  0; 
            gm  -(gm+gd)    gd  0;
            0     0         0   0];
      
      c = [0 -id id 0]';
      break;

    otherwise

      error(["Mnmosfet: unknown option " string]);
      
  endswitch

endfunction


Build the AND gate circuit structure parsing a .spc file

Code: Load the AND circuit structure parsing a .spc file
outstruct = prs_spice ("and");