%-------------------------------
% techno stuff
%-------------------------------
Eox = 34.5e-12;
tox = 9e-9;
Cox = Eox / tox;

Un  = 350e-4;
LDn = 0.08e-6;
LAMBDAn = 0.1;
VTHn = 0.7;

Up  = 100e-4;
LDp = 0.09e-6;
LAMBDAp = 0.2;
VTHp = 0.8;

%-------------------------------
% Biasing Current
%-------------------------------
VDD = 3;
Iss = 10e-6;
Io6 = 100e-6;

Cout = 1e-12;
Cy = 0.2e-12;

VEG1 = 0.2;
VEG6=0.2;

%-------------------------------
% EX II.(2) 1st stage pole
%-------------------------------
ron1=1/(LAMBDAn*(Iss/2));
rop1=1/(LAMBDAp*(Iss/2));
Ry = (ron1*rop1) / (ron1+rop1);
wpy = 1/(Ry*Cy)
% >> wpy = 7.5e6 rad/sec << %
fpy = wpy /(2*pi)
% >> fpy = 1.1937 MHz << %
%-------------------------------
% EX II.(2) 2nd stage pole
%-------------------------------
ron2=1/(LAMBDAn*Io6);
rop2=1/(LAMBDAp*Io6);
Rout = (ron2*rop2) / (ron2+rop2);
wpout = 1/(Rout*Cout)
% >> wpout = 30.0e6 rad/sec << %
fpout = wpout /(2*pi)
% >> fpout = 4.7746 MHz << %


%-------------------------------
% EX II.(1) small signal gain
%-------------------------------
gm1 = (2*(Iss/2))/VEG1;
Av1 = gm1 * Ry
gm6 = (2*Io6)/VEG6;
Av2 = gm6 * Rout
Av = Av1 *Av2
AvdB = 20*log10(Av)
% >> AvdB = 60.9151 dB << %

%-------------------------------
% Phase margin
%-------------------------------
ApoutdB = AvdB - 20*log10(wpout/wpy);
w1 = wpout * 10^(ApoutdB/40);
phase = (-atan(w1/wpy) -atan(w1/wpout))*180/pi;
phase_margin = (-atan(w1/wpy) -atan(w1/wpout))*180/pi - (-180)
% >> phase_margin = 4.2930 degrees << %

%-------------------------------
% Compensation for Cc = 0.1pF
%-------------------------------
wpoutc = gm6 / (Cy + Cout)
% >> wpoutc = 833.33e6 rad/sec << %
fpoutc = wpoutc/(2*pi)
% >> fpoutc = 132.63 MHz << %

Cc = 0.1e-12;
wpyc = 1/((Cy+(1+Av2)*Cc)*Ry)
% >> wpyc = 412.84e3 rad/sec << %
fpyc = wpyc/(2*pi)
% >> fpyc = 65.706 kHz << %

w1c = wpyc * 10^(AvdB/20)
phasec = (-atan(w1c/wpyc) -atan(w1c/wpoutc))*180/pi
phase_marginc = (-atan(w1c/wpyc) -atan(w1c/wpoutc))*180/pi - (-180)
% >> phase_marginc = 61.2206 degrees << %