% mosmovie3
%
% Animation of MOSFET channel for accumulation depletion inversion
n=20

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

% Plot for showing where on acum - depl- inv
% Note bad practice: assumes nframes=100
xv=[0:-0.1:-1 -1 -1 -1 -1 -1 -1 -1 -1 -1:0.05:+3];
    
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])


     if (xv(k)<0.01)
       % Accumulation
       patch([-.35, -.35, .35,  .35],[0, 0.1*xv(k), 0.1*xv(k), 0],[.9-.1*xv(k) .6+.3*xv(k) .6+.3*xv(k)],'EdgeColor','none')
   %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 (always the same)
   rectangle('Position',[.35,-.45,.6,.6],'FaceColor',[1 1 1],'EdgeColor',[1 1 1],'Curvature',[.2,.2])

     elseif ((xv(k)>0)&&(xv(k)<1))
       % Depletion
       patch([-.5, -.5, .5,  .5],[0, -0.3*xv(k), -0.3*xv(k), 0],[1 1 1],'EdgeColor','none')
    %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 (always the same)
   rectangle('Position',[.35,-.45,.6,.6],'FaceColor',[1 1 1],'EdgeColor',[1 1 1],'Curvature',[.2,.2])
   else
       % Must be Inversion
       % Keep depletion region
       patch([-.5, -.5, .5,  .5],[0, -0.3, -0.3, 0],[1 1 1],'EdgeColor','none')
    %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 (always the same)
   rectangle('Position',[.35,-.45,.6,.6],'FaceColor',[1 1 1],'EdgeColor',[1 1 1],'Curvature',[.2,.2])
      % Add inversion layer
       patch([-.6, -.6, .6,  .6],[0, -0.0666*xv(k), -0.0666*xv(k), 0],[1-0.2*xv(k) 1-0.2*xv(k) 1],'EdgeColor','none')
      end
     
 %%%%  % 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, -.25, -.25*(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, -.25,  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])
   
   % Accumulation-Depletion-Inversion line for plot
   line([0 0],[0.65, 0.75],'Color',[0 0 0])       % 0V tick
   line([0.25 0.25],[0.65, 0.75],'Color',[0 0 0])   % Vtn tick
   line([-0.8 0.8],[0.7, 0.7],'Color',[0 0 0])    % axis
   %label axis
   text(0,0.65,'0 V','HorizontalAlignment','center','VerticalAlignment','top', ...
       'FontSize',16,'FontWeight','bold')
   text(0.25,0.65,'V_{tn}','HorizontalAlignment','center','VerticalAlignment','top', ...
       'FontSize',16,'FontWeight','bold')
   text(0.82,0.7,'V_{GS}','HorizontalAlignment','left','FontSize',16,'FontWeight','bold')
   %label regions
   %Decide color of label based on xv
   if (xv(k)<0)
       % Accumulation
       acc_color=[0 0 0];
       depl_color=[0.8 0.8 0.8];
       inv_color=[0.8 0.8 0.8];
   elseif (xv(k)==0)
       % Accumulation-Depletion boundary
       acc_color=[0.4 0.4 0.4];
       depl_color=[0.4 0.4 0.4];
       inv_color=[0.8 0.8 0.8];
   elseif ((xv(k)>0)&&(xv(k)<1))
       % Depletion
       acc_color=[0.8 0.8 0.8];
       depl_color=[0 0 0];
       inv_color=[0.8 0.8 0.8];
   elseif (xv(k)==1)
       % Depletion-Inversion boundary
       acc_color=[0.8 0.8 0.8];
       depl_color=[0.4 0.4 0.4];
       inv_color=[0.4 0.4 0.4];
   else
       % Must be Inversion
       acc_color=[0.8 0.8 0.8];
       depl_color=[0.8 0.8 0.8];
       inv_color=[0 0 0];
   end
   text(-0.4,0.77,'ACCUMULATION','HorizontalAlignment','center','VerticalAlignment','bottom', ...
       'FontSize',12,'FontWeight','bold','Color',acc_color)
   text(0.125,0.77,'DEPLETION','HorizontalAlignment','center','VerticalAlignment','bottom', ...
       'FontSize',12,'FontWeight','bold','Color',depl_color)
   text(0.5,0.77,'INVERSION','HorizontalAlignment','center','VerticalAlignment','bottom', ...
       'FontSize',12,'FontWeight','bold','Color',inv_color)

   % This point on V-I plot
   rectangle('Position',[0.25*xv(k)-0.02,0.68,.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}=' num2str(xv(k),'%+5.1f') ],'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}=0' ],'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])

   
   
   M(k) = getframe;
end
% Finally, play the movie once 
movie(M,1)