function [err, note_name]=sb2slmng(cdn,blkname,posit,in,fl,cod,timer,st);
%SB2SLMNG is the highest level management function for SB2SL.
%	ERR=SB2SLMNG(CDN,BLKNAME,POSIT,IN,FL,COD,TIMER,ST) creates a
%	block with the name BLKNAME, coding number CDN in position POSIT. 
%       The parameters are set up for this block based on the
%       SystemBuild file coding integer IN, real number FL,
%       and string ST.
%
%       COD is a three dimensional vector. The first element indicates
%       whether it is a continuous (0) or discrete (1) time system.
%       The second element indicates whether it does not need (0) or
%       does need (1) a mask. The third element indicates the
%       orientation of the block.
%
%       TIMER is designed for discrete time only. The first element of
%       TIMER is the sampling time. The second element is the initial time.

%	Wes Wang 11/27/92
% 	Copyright (c) 1990-93 by The MathWorks, Inc.
% 	$Revision: 1.27 $  $Date: 1994/09/16 01:13:29 $

%load data
err=0;
note_name = [];
sb2sldat
clear blankt pointer str_ori
ios = in(4:7);

posit(4) = max(posit(4), 10+5*max(in(4),in(5)));
posit(3:4)=posit(1:2)+posit(3:4);

if cdn == 1,
  new_system(blkname);
  set_param(blkname, 'position', posit);
%  disp('Illegal block, report to The MathWorks.')
  if ios(1) > 0   %Define I/O port, so the block has I/O for add_line
    for i=1:ios(1)
       add_block('built-in/Inport',[blkname,'/input',num2str(i)]);
       set_param([blkname '/input' num2str(i)],'position',...
		[10,10+30*i,30,30+30*i],...
		'hide name',0,...
		'Port',num2str(i));
    end;
  end %if
  if ios(2) > 0
    for i=1:ios(2)
       add_block('built-in/Outport',[blkname,'/output',num2str(i)]);
       set_param([blkname '/output' num2str(i)],...
                'position',[40,10+30*i,60,30+30*i],...
		'hide name',0,...
		'Port',num2str(i));
    end;
  end;
  set_param(blkname,'Mask Display','Illigel\nBlock')
  err = sb2slerr(blkname,'We have never seen this block type before');
  return
end;

Func_name = deblank(block(cdn,:));
add1 = ['built-in/' Func_name];
if cdn>=21 & cdn<=27 & cdn~=26 & cod(1)~=0
  add1 = ['built-in/Discrete ' Func_name];
  if cdn == 25
    add1 = ['built-in/Discrete State-space'];
  elseif cdn == 27
    add1 = ['built-in/Unit Delay'];
  end;
end;

if cdn == 2 | cod(2) > 0   % need a mask
  new_system(blkname);
  set_param(blkname, 'position', posit);
  if ios(1) > 0   %Define I/O port, so the block has I/O for add_line
    for i=1:ios(1)
       add_block('built-in/Inport',[blkname,'/input',num2str(i)]);
       set_param([blkname '/input' num2str(i)],'position',...
		[1230,1010,1250,1033],...
		'hide name',0,...
		'Port',num2str(i));
    end;
  end %if
  if ios(2) > 0
    for i=1:ios(2)
       add_block('built-in/Outport',[blkname,'/output',num2str(i)]);
       set_param([blkname '/output' num2str(i)],...
		'hide name',0,...
		'Port',num2str(i));
    end;
  end %if
  if in(2) == 1 %  if cdn == 2
    if in(3) == 1
%      set_param(blkname,'Mask Display',...
%       'plot([1 1 3 3 20 20 21 21 23],[5 6 6 1 1 6 6 7 5],[1 1 3 3 20 20 21 21 23],[5 4 4 9 9 4 4 3 5]); GROUP');
    elseif in(3) == 2
	err = sb2slerr(blkname,'Condition Block (Not Supported)');
    elseif in(3) == 3
        st =  setstr(st');
        indx = find(st=='|');
        st = st(indx(2)+1:indx(3)-1);
        set_param(blkname,'Mask Display',st);
        % bug fix for '/' in the sequence
        pt = find(st == '/');
        if ~isempty(pt)
          for i = length(pt) : -1 : 1
            st = [st(1:pt(i)), '/', st(pt(i)+1:length(st))];
          end;
        end;
        if length(st) >= 255
           st = st(1:254);
        end;
        add_block('built-in/Note',[blkname,'/',st]);
        set_param([blkname,'/',st],'position',[100,50,110,60]);
    else
      err = sb2slerr(blkname,'We have never seen this block type before');
      % will not appear here.
    end;

  elseif in(2) == 2 %  elseif cdn == 5   %general algebraic
    if in(3) == 1
      sb2slga(blkname,in,fl);
    elseif in(3) ==2 %  elseif cdn == 6
      sb2slml(blkname,in);
    elseif in(3) ==3
      sb2sl121(blkname,in,fl,cod,timer);
    elseif in(3) >= 4 & in(3) <= 7 %  elseif cdn >= 8 & cdn <= 11
      sb2slml(blkname,in);
    elseif in(3) == 8
      sb2sl121(blkname,in,fl,cod,timer);
    else
      err = sb2slerr(blkname,'We have never seen this block type before');
    end;

  elseif in(2) == 3
    if (in(3) <=8) & (in(3) ~= 6)
      if in(4) == in(5)
        sb2sl121(blkname,in,fl,cod,timer);
      else  % for the case of 2D Look Up Table
        sb2sl221(blkname,in,fl,cod,timer);
      end;
    elseif in(3) == 6
      if in(4) == 2*in(5)
        sb2sl221(blkname,in,fl,cod,timer);
      elseif in(4) ==1
        sb2sllit(blkname,in,fl);
      else
        err = sb2slerr(blkname,'We did not know there is such input-output pair');
      end;
    elseif in(3) == 9
      sb2sllit(blkname,in,fl) %linear interpolation table
    elseif in(3) == 10
      sb2slmul(blkname,in,fl,st,cod,timer);
    else 
      err = sb2slerr(blkname,'We have never seen this block type before');
    end;

  elseif in(2) == 4 %elseif cdn == 21
    if in(3) == 1
      sb2slss(blkname,in,fl,cod,timer);
    elseif in(3) == 2 %elseif cdn == 22
      sb2sltf(blkname,in,fl,cod,timer);
    elseif in(3) == 3 % elseif cdn == 23
      sb2slgzp(blkname,in,fl,cod,timer);
    elseif in(3) == 4 % elseif cdn == 24
      sb2slgzp(blkname,in,fl,cod,timer);
    elseif in(3) == 5 %  elseif cdn == 25
      sb2slint(blkname,in,fl,cod,timer);
    elseif in(3) == 6
      sb2slhys(blkname,in,fl,cod,timer);
    elseif in(3) == 7
      sb2sldly(blkname,in,fl,cod,timer);
    elseif in(3) == 8 
      sb2sllti(blkname,in,fl,cod,timer);
    elseif in(3) == 9
      sb2slpid(blkname,in,fl,cod,timer);
    elseif in(3) == 10
      sb2slspr(blkname,in,fl,cod,timer);
    else
      err = sb2slerr(blkname,'We have never seen this block type before');
    end;
  elseif in(2) == 5
    if in(3) <= 2 | in(3) >= 6  %sin,cos,asin,cos(asin),acos
      sb2sl121(blkname,in,fl,cod,timer);
    else
      sb2sl221(blkname,in,fl,cod,timer);
    end;   

  elseif in(2) == 6
                   % user defined blocks
    sb2slmat(blkname,in,fl,cod,timer);

  elseif in(2) == 7 % gain block
     sb2sl121(blkname,in,fl,cod,timer);

  elseif in(2) == 8
     sb2sl121(blkname,in,fl,cod,timer);

  elseif in(2) == 9 
    if in(3) == 1  % elseif cdn == 48  %cartesian to polar
      sb2slc2p(blkname);
    elseif in(3) == 2 % elseif cdn == 49  %polar to cartesian
      sb2slp2c(blkname);
    elseif in(3) == 3 %  elseif cdn == 50  %cartesian to spherical
      sb2slc2s(blkname);
    elseif in(3) == 4 %  elseif cdn == 51  % spherical to cartesian
      sb2sls2c(blkname);
    elseif in(3) == 5
      sb2slcoo(blkname,in)
    elseif in(3) == 6
      sb2slcoo(blkname,in)
    else
      err = sb2slerr(blkname,'We have never seen this block type before');
    end;
  elseif in(2) == 10
    % input source, 
    sb2slsc(blkname,in,fl,cod,timer);

  elseif in(2) == 11
    if in(3) == 1
       sb2slga(blkname,in,fl);
    elseif in(3) == 2
       sb2slsft(blkname,in,fl,cod,timer);
    elseif in(3) == 3
       sb2sl221(blkname,in,fl,cod,timer);
    elseif in(3) == 4
       sb2sl221(blkname,in,fl,cod,timer);
    elseif in(3) == 5
        sb2slswt(blkname,in,fl,cod,timer);
    elseif in(3) == 6
       sb2slgtb(blkname,in,fl,cod,timer);
    elseif in(3) == 7 %stop simulation
       sb2sl121(blkname,in,fl,cod,timer);
    elseif in(3) == 8
      err = sb2slerr(blkname,'We have never seen this block type before');
    elseif in(3) == 9
      err = sb2slerr(blkname,'Fuzzy Logic Block (Not Supported)');
    elseif in(3) == 10
      err = sb2slerr(blkname,'Expert System (Not Supported)');
    end;
  elseif in(2) == 13
      err = sb2slerr(blkname,'State Transition Diagram (Not Supported)');
    % to be done
  elseif in(2) == 14
    err = sb2slerr(blkname,'Data Storage (Not Supported)');
  else
    err = sb2slerr(blkname,deblank(face(cdn,:)));
  end;

elseif cdn == 4
%  new_system(blkname);
%  set_param(blkname, 'position', posit);
%  st =  setstr(st');
%  indx = find(st=='|');
%  st = st(indx(2)+1:indx(3)-1);
%  % bug fix for '/' in the sequence
%  pt = find(st == '/');
%  if ~isempty(pt)
%    for i = length(pt) : -1 : 1
%      st = [st(1:pt(i)), '/', st(pt(i)+1:length(st))];
%    end;
%  end;
%  if length(st) >= 255
%     st = st(1:254);
%  end;
%  add_block('built-in/Note',[blkname,'/',st]);
%  set_param([blkname,'/',st],'position',[100,50,110,60]);
%  set_param(blkname,'Mask Display',st);

  st =  setstr(st');
  indx = find(st=='|');
  st = st(indx(2)+1:indx(3)-1);
  % bug fix for '/' in the sequence
  pt = find(st == '/');
  if ~isempty(pt)
    for i = length(pt) : -1 : 1
      st = [st(1:pt(i)), '/', st(pt(i)+1:length(st))];
    end;
  end;
  if length(st) >= 255
     st = st(1:254);
  end;
  blkname = [blkname, setstr(10), st];
  add_block('built-in/Note',blkname);
  posit2 = floor([(posit(1)+posit(3))/2, (posit(2)+posit(4))/2]);
  set_param(blkname, 'position', [posit2 posit2]);
  note_name = blkname;
else
  add_block(add1,blkname);
  set_param(blkname, 'position', posit);
  %The following is a long process if...elseif...elseif...elseif...end;
  if in(2) == 1 %  if cdn == 3
    if in(3) == 2
      fprintf('block '); fprintf(blkname); fprintf(' is not ready yet.\n')
      %this block is not ready yet
      err = sb2slerr(blkname,'Condition Block (Not Supported)',1);
      set_param(blkname,'function name','condblk')
    elseif in(3) == 3
      st =  setstr(st');
      indx = find(st=='|');
      st = st(indx(2)+1:indx(3)-1);
      set_param(blkname,'Mask Display',st);
    else
      err = sb2slerr(blkname,'We have never seen this block block before.');
    end;
  elseif in(2) == 2 %  elseif cdn ==5 %general algebraic
    if in(3) == 1
      sb2slga(blkname,in,fl);
    elseif in(3) ==2 %elseif cdn ==6 %Sum
       for i = 1:in(4)/in(5)
          if (in(in(4)*2+in(8)+10+i)>0)
             sig = [sig '+'];
          else
             sig = [sig '-'];
          end;
       end;
       set_param(blkname,'inputs',sig);
    elseif in(3) == 3
      sb2sl121(blkname,in,fl,cod,timer);
    elseif in(3) >= 4 & in(3) <= 7 %  elseif cdn >= 8 & cdn <= 11
      sb2slml(blkname,in);
    elseif in(3) == 8
      sb2sl121(blkname,in,fl,cod,timer);
    else
      err = sb2slerr(blkname,'We have never seen this block block before.');
    end;
  elseif in(2) == 3 
    if in(4) == in(5)
      sb2sl121(blkname,in,fl,cod,timer);
    else
      sb2sl221(blkname,in,fl,cod,timer);
    end;
  elseif in(2) == 4 %  elseif cdn == 21
    if in(3) == 1
      sb2slss(blkname,in,fl,cod,timer);
    elseif in(3) ==2 %  elseif cdn == 22
      sb2sltf(blkname,in,fl,cod,timer);
    elseif in(3) == 3 %  elseif cdn == 23
      sb2slgzp(blkname,in,fl,cod,timer);
    elseif in(3) == 4 %  elseif cdn == 24
      sb2slgzp(blkname,in,fl,cod,timer);
    elseif in(3) == 5 %  elseif cdn == 25
      sb2slint(blkname,in,fl,cod,timer);
    elseif in(3) == 6
      sb2slhys(blkname,in,fl,cod,timer);
    elseif in(3) == 7
      sb2sldly(blkname,in,fl,cod,timer);
    elseif in(3) == 8
      sb2sllti(blkname,in,fl,cod,timer);
    elseif in(3) == 9
      sb2slpid(blkname,in,fl,cod,timer);
    elseif in(3) == 10
      sb2slsc(blkname,in,fl,cod,timer);
    else
      err = sb2slerr(blkname,'We have never seen this block block before.');
    end;
  elseif in(2) == 5
    if in(3) == 1 %  elseif cdn == 31
      set_param(blkname,'Expr','sin(u[1])');
    elseif in(3) == 2 %  elseif cdn == 32
      set_param(blkname,'Expr','cos(u[1])');
    elseif in(3) >= 3 & in(3) <= 5
      sb2sl221(blkname,in,fl);
    elseif in(3) == 6 %  elseif cdn == 36
      set_param(blkname,'Expr','asin(u[1])');
    elseif in(3) == 7 %  elseif cdn == 37
      set_param(blkname,'Expr','cos(asin(u[1]))');
    elseif in(3) == 8 %  elseif cdn == 38
      set_param(blkname,'Expr','acos(u[1])');
    else
      err = sb2slerr(blkname,'We have never seen this block block before.');
    end;
  elseif in(2) == 6,
    sb2slmat(blkname,in,fl,cod,timer);
  elseif in(2) == 7
    set_param(blkname,'Gain',num2str(fl(1)));
  elseif in(2) == 8 %  elseif cdn == 42
    if in(3) == 1
      set_param(blkname,'Expr','sqrt(u[1])');
    elseif in(3) == 2 %  elseif cdn == 43
      set_param(blkname,'Expr','ln(u[1])');
    elseif in(3) == 3 %  elseif cdn == 44
      set_param(blkname,'Expr','power(2.71828182846,u[1])',...
              'Mask Display','     u\ne');
    elseif in(3) == 4 %  elseif cdn == 45
      set_param(blkname,'Expr','u[1]*u[1]',...
              'Mask Display','     2\nu');
    elseif in(3) == 5 %  elseif cdn == 46
      set_param(blkname,'Expr','power(10,u[1])',...
              'Mask Display','       u\n10');
    elseif in(3) == 6  % elseif cdn == 47
      set_param(blkname,...
        'Expr','(u[1]>=0)-(u[1]<0)*sqrt(abs(u[1]))',...
              'Mask Display','sign(sqrt(u))');
    else
      err = sb2slerr(blkname,'We have never seen this block block before.');
    end;
  elseif in(2) == 9
     sb2sl121(blkname,in,fl,cod,timer);
     % in(2) == 9 will not be happened here.
  elseif in(2) == 10
    % input source, 
    sb2slsc(blkname,in,fl,cod,timer);
  elseif in(2) == 11
    if in(3) == 1
       sb2slga(blkname,in,fl);
    elseif in(3) == 2
       sb2slsft(blkname,in,fl,cod,timer);
    elseif in(3) == 3
       sb2sl221(blkname,in,fl,cod,timer);
%       set_param(blkname,'Mask Display','NOR')
    elseif in(3) == 4
       sb2sl221(blkname,in,fl,cod,timer);
    elseif in(3) == 5
        sb2slswt(blkname,in,fl,cod,timer);
    elseif in(3) == 6
       sb2slgtb(blkname,in,fl,cod,timer);
    elseif in(3) == 7 %stop simulation
      % nothing to be done
    elseif in(3) == 8
      err = sb2slerr(blkname,'We have never seen this type of block before');
    elseif in(3) == 9
      delete_block(blkname);
      new_system(blkname);
      set_param(blkname, 'position', posit);
      add_block('built-in/Inport',[blkname,'/input1']);
      set_param([blkname '/input1'],'position',...
		[10,40,30,60],'hide name',0,'Port','1');
      add_block('built-in/Outport',[blkname,'/output1']);
      set_param([blkname '/output1'],...
                'position',[40,40,60,60],'hide name',0,'Port','1');
      err = sb2slerr(blkname,'Fuzzy Block (Not Supported)',1);
    elseif in(3) == 10
      delete_block(blkname);
      new_system(blkname);
      set_param(blkname, 'position', posit);
      add_block('built-in/Inport',[blkname,'/input1']);
      set_param([blkname '/input1'],'position',...
		[10,40,30,60],'hide name',0,'Port','1');
      add_block('built-in/Outport',[blkname,'/output1']);
      set_param([blkname '/output1'],...
                'position',[40,40,60,60],'hide name',0,'Port','1');
      err = sb2slerr(blkname,'Expert System (Not Supported)',1);
    else
      err = sb2slerr(blkname,'We have never seen this type of block before');
    end;
  elseif in(2) == 13
      delete_block(blkname);
      new_system(blkname);
      set_param(blkname, 'position', posit);
      add_block('built-in/Inport',[blkname,'/input1']);
      set_param([blkname '/input1'],'position',...
		[10,40,30,60],'hide name',0,'Port','1');
      add_block('built-in/Outport',[blkname,'/output1']);
      set_param([blkname '/output1'],...
                'position',[40,40,60,60],'hide name',0,'Port','1');
    err = sb2slerr(blkname,'We are not supporting this block');
  elseif in(2) == 14
      delete_block(blkname);
      new_system(blkname);
      set_param(blkname, 'position', posit);
      add_block('built-in/Inport',[blkname,'/input1']);
      set_param([blkname '/input1'],'position',...
		[10,40,30,60],'hide name',0,'Port','1');
      add_block('built-in/Outport',[blkname,'/output1']);
      set_param([blkname '/output1'],...
                'position',[40,40,60,60],'hide name',0,'Port','1');
    err = sb2slerr(blkname,'We are not supporting this block');
  elseif cdn == 82
    set_param(blkname,'inputs',num2str(ios(1)));
  elseif cdn == 83
    set_param(blkname,'outputs',num2str(ios(2)));
  end;
end;

%set_param(blkname, 'position', posit);
if cod(3)
   set_param(blkname, 'orientation', 2);
end;
tmp =  in(in(17)-3);
if tmp
  if tmp >= 0
    tmp = rem(tmp-1,6)+1;
    set_param(blkname, 'BackGround',bg(tmp));
  else
    tmp = rem(abs(tmp)-1,6) + 1;
    set_param(blkname, 'ForeGround',fg(tmp));
  end;
end;





