% mosmovie2
%
% Animation of MOSFET channel
n=20

% Number of frames (should be even!)
nframes = 100;
% Set up plot
figure(2)
set(gcf,'units','normalized')
set(gcf,'Position',[.2 .2 .65 .7])

% Plot for showing where on V-I curve
xv=-0.5+(0:nframes-1)/nframes;
% Triode part
yi=polyval([-1.2 .1 .85],xv(1:nframes/2));
% Active part
yi=[yi polyval([.1 .85],xv(((nframes/2)+1):nframes))];
    
grid off
% Generate the movie and use getframe to capture each frame. 
for k = 1:nframes
   clf
   axis([-1 1 -1 1])
   axis square
   axis off
   %Wafer surface
   line([-1 1],[0 0],'Color',[0 0 0])
   %Substrate
   rectangle('Position',[-1,-1,2,1],'FaceColor',[.9 .6 .6],'EdgeColor',[0 0 0])
   %Depletion regions around source and drain
   % Source (always the same)
   rectangle('Position',[-.95,-.45,.6,.6],'FaceColor',[1 1 1],'EdgeColor',[1 1 1],'Curvature',[.2,.2])
   % Drain (changes with vds)
   rectangle('Position',[.35-.1*(k/nframes),-.45-.1*(k/nframes),.6+.2*(k/nframes),.6+.2*(k/nframes)],'FaceColor',[1 1 1],'EdgeColor',[1 1 1],'Curvature',[.2,.2])
   % Depletion region in channel
   patch([-.5, -.5, .5,  .5],[0, -.3, -.3-.1*(k/nframes), 0],[1 1 1],'EdgeColor','none')
   % Channel (depends on operating region)
   if k<nframes/2     
       % continuous channel
       patch([-.45, -.45, .45,  .45],[0, -.2, -.2*(1-(2*k/nframes)), 0],[.5 .5 1],'EdgeColor','none')
   else
       % pinched off channel
       pinchpt=.45-((k/(2*nframes))-0.25);   % point where channel pinches off
       patch([-.45, -.45, pinchpt],[0, -.2,  0],[.5 .5 1],'EdgeColor','none')
   end
       % Source, drain diffusions (always the same)
   rectangle('Position',[-.85,-.35,.4,.4],'FaceColor',[.2 .2 1],'EdgeColor',[.2 .2 1],'Curvature',[.2,.2])
   rectangle('Position',[.45,-.35,.4,.4],'FaceColor',[.2 .2 1],'EdgeColor',[.2 .2 1],'Curvature',[.2,.2])
   
   %Wafer surface
   rectangle('Position',[-1,0,2,1],'FaceColor',[1 1 1],'EdgeColor',[1 1 1])
   % Gate oxide and gate
   rectangle('Position',[-0.475,0,.95,.1],'FaceColor',[.7 .7 .7],'EdgeColor',[.7 .7 .7])
   rectangle('Position',[-0.475,.1,.95,.02],'FaceColor',[0 0 0],'EdgeColor',[0 0 0])
   % Wafer surface
   line([-1 1],[0 0],'Color',[0 0 0])
   
   % V-I plot
   line([-0.5 -0.5],[0.5, 0.9],'Color',[0 0 0])
   line([-0.5 0.5],[0.5, 0.5],'Color',[0 0 0])
   %label axes
   text(-0.52,0.9,'I_{D}','HorizontalAlignment','right','FontSize',16,'FontWeight','bold')
   text(0.52,0.55,'V_{DS}','HorizontalAlignment','left','FontSize',16,'FontWeight','bold')
   line(xv,yi,'LineWidth',2)
   % Dotted line for triode-active boundary
   line([-.5:0.01:.1],polyval([1.4 1.4 .85],[-.5:0.01:.1]),'LineStyle',':','Color',[0.1 0.1 0.1],'LineWidth',1.5)
   % This point on V-I plot
   rectangle('Position',[xv(k)-0.02,yi(k)-0.02,.04, .04],'FaceColor',[1 1 0],'EdgeColor',[.0 0 0 ],'Curvature',[1,1])
   
   % LAbel threshold voltage
   text(0,-0.9,'V_{tn}=+1.0V','HorizontalAlignment','center','VerticalAlignment','bottom','FontSize',16,'FontWeight','bold')
   % Terminals
   % Gate
   rectangle('Position',[-0.03,.25,.06, .06],'FaceColor',[1 1 1],'EdgeColor',[.0 0 0 ],'Curvature',[1,1],'LineWidth',2)
   text(0,.3,'V_{G}=+3.0V','HorizontalAlignment','center','VerticalAlignment','bottom','FontSize',16,'FontWeight','bold')
   line([0 0],[.1 .25],'Linewidth',2,'color',[0 0 0])
   % Source
   rectangle('Position',[-0.68,.25,.06, .06],'FaceColor',[1 1 1],'EdgeColor',[.0 0 0 ],'Curvature',[1,1],'LineWidth',2)
   text(-0.65,.3,'V_{S}=0','HorizontalAlignment','center','VerticalAlignment','bottom','FontSize',16,'FontWeight','bold')
   line([-0.65 -0.65],[0 .25],'Linewidth',2,'color',[0 0 0])
   rectangle('Position',[-0.75,.0,.2,.017],'FaceColor',[0 0 0],'EdgeColor',[0 0 0])
% Drain
   rectangle('Position',[0.62,.25,.06, .06],'FaceColor',[1 1 1],'EdgeColor',[.0 0 0 ],'Curvature',[1,1],'LineWidth',2)
   text(0.5,.3,['V_{D}=' num2str(k/25,'%+5.1f') ],'HorizontalAlignment','left','VerticalAlignment','bottom','FontSize',16,'FontWeight','bold')
   line([0.65 0.65],[0 .25],'Linewidth',2,'color',[0 0 0])
   rectangle('Position',[0.55,.0,.2,.017],'FaceColor',[0 0 0],'EdgeColor',[0 0 0])

   % in saturated region?
   if k>nframes/2
       line([pinchpt pinchpt],[.1 .25],'Linewidth',2,'color',[0 0 0])
       patch([pinchpt pinchpt-0.02 pinchpt+0.02],[.1 .17 .17],[0 0 0],'EdgeColor','none')
       line([pinchpt pinchpt],[-.6 0],'Linewidth',2,'color',[0 0 0])
       line([pinchpt-0.08 pinchpt],[-.6 -.6],'Linewidth',2,'color',[0 0 0])
       patch([pinchpt pinchpt-0.02 pinchpt+0.02],[0 -.07 -.07],[0 0 0],'EdgeColor','none')
       text(pinchpt-0.1,-0.60,'V_{G}-V(x)=V_{tn}','HorizontalAlignment','right','VerticalAlignment','middle','FontSize',16,'FontWeight','bold')
   end
   
    %Decide color of label based on xv
   if (k<nframes/2)
       % Triode
       tri_color=[0 0 0];
       act_color=[0.8 0.8 0.8];
   elseif (k==nframes/2)
       % Triode-Active boundary
       tri_color=[0.4 0.4 0.4];
       act_color=[0.4 0.4 0.4];
     else
       % Must be Active
         tri_color=[0.8 0.8 0.8];
       act_color=[0 0 0];
   end
   text(-0.3,0.9,'TRIODE','HorizontalAlignment','center','VerticalAlignment','bottom', ...
       'FontSize',12,'FontWeight','bold','Color',tri_color)
   text(0.3,0.9,'ACTIVE','HorizontalAlignment','center','VerticalAlignment','bottom', ...
       'FontSize',12,'FontWeight','bold','Color',act_color)
  
   M(k) = getframe;
end
% Finally, play the movie once 
movie(M,1)