function sb2sl121(blkname,in,fl,cod,timer)
%SB2SL121 converts blocks with an equal number of inputs and outputs.
%	SB2SL121(BLKNAME,IN,FL,COD,TIMER) converts a SystemBuild block
%	to a SIMULINK block.  BLKNAME is a string containing the name of
%	the block. The parameters are set up for this block based on the
%	SystemBuild file coding integer IN and real number FL.
%
%	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 10/2/92 -- 10/6/92
%	Copyright (c) 1990-93 by The MathWorks, Inc.
%	$Revision: 1.12 $  $Date: 1993/06/09 22:09:40 $


% 121 works for 2:8; 3:1 2 3 4 5 6 7 8; 5:1 2 6 7 8; 7:1; 8:1 2 3 4 5 6; 

if in(2) == 5
  if in(3) == 1
    temp = 'sin(u[1])'; sh = 'sin(u)';
  elseif in(3) == 2
    temp = 'cos(u[1])'; sh = 'cos(u)';
  elseif in(3) == 6  
    temp = 'asin(u[1])'; sh = 'asin(u)';
  elseif in(3) == 7
    temp = 'cos(asin(u[1]))';  sh = 'cos(asin(u))';
  elseif in(3) == 8
    temp = 'acos(u[1])'; sh = 'acos(u)';
  end;
elseif in(2) == 8
  if in(3) == 1        %42
    temp = 'sqrt(u[1])'; sh = 'sqrt(u)';
  elseif in(3) == 2        %43
    temp = 'ln(u[1])'; sh = 'ln(u)';
  elseif in(3) == 3        %44
    temp = 'power(2.71828182846,u[1])'; sh = 'e^u';
  elseif in(3) == 4        %45
%    temp = 'u[1]*u[1]'; 
     sh = 'u^a';
  elseif in(3) == 5        %46
%    temp = 'power(10,u[1])'; 
    sh = 'a^u';
  elseif in(3) == 6        %47
    temp = '(u[1]>=0)-(u[1]<0)*sqrt(abs(u[1]))'; sh = 'sign(abs(u))';
  end;
elseif in(2) == 3 & in(3) == 5        %16
  temp = 'abs(u[1])'; sh = 'Abs';
elseif in(2) == 2 
  if in(3) == 8
    %data type transfer
    % in(in(17)-3) --> input type  1->real, 2->int, 3->logic
    % in(in(17)-2) --> output type
    % in(in(17)-1) --> operating type
    if in(in(17)-2) >= in(in(17)-3) 
      temp = 'u[1]';
    elseif in(in(17)-3)== 1 & in(in(17)-2) == 2
      if in(in(17)-1) == 1
        temp = 'ceil(u[1]+0.5)';
      else
        temp = 'floor(u[1])';
      end;
    elseif in(in(17)-2) == 3
      temp = 'u[1] ~= 0';
    end;
    sh = 'Data Type\nTransfer';
  elseif in(3) == 3
    %polynomial need to get parameter each round
    in(6) = in(in(17)+3)+1;
    for i=1:in(6)
      if i==1
        temp = num2str(fl(1));
      elseif i==2
        temp = [temp '*u[1] + ' num2str(fl(2))];
      else
        temp = ['(' temp ')*u[1] + ' num2str(fl(i))];
      end;
    end;
    sh = 'Polynomial\nAlgebra';
  end;
elseif in(2) == 11
  sh = 'STOP';
end;
if in(4) <= 1 & in(5) <=1
  if in(2) == 7 & in(3) ==1
    set_param(blkname,'Gain',num2str(fl(1)));
  elseif in(2) == 3
    if in(3) == 1 | in(3) == 6 | in(3) == 9
      temp = in(in(17)+3);
      if in(3) == 6, temp = in(in(17)+4); end;
      [tmp1,ind] = sort(fl(1:temp)');
      tmp2 = fl(temp+1:2*temp)';
      tmp2 = tmp2(ind);
      if length(mat2str(tmp1)) >= 255 | length(mat2str(tmp2)) >= 255
        load sb2sltmp
	app = sb2slbnm(blkname);
        x = ['O' num2str(xn+1) app]; y = ['O' num2str(xn+2) app]; 
        eval([x '= tmp1;']);
	appsave('sb2sltmp', x, tmp1);
        disp([x ' is used as index of look up table in ' blkname])
        eval([y '= tmp2;']);
	appsave('sb2sltmp', y, tmp2);
        disp([y ' is used as table content of look up table in ' blkname])
        %xs=[xs x y];
        xn=xn+2;
	appsave('sb2sltmp', 'xn', xn);
        %eval(xs)
        set_param(blkname,'Input_Values',x,...
                        'Output_Values',y);
      else
        set_param(blkname,'Input_Values',mat2str(tmp1),...
                        'Output_Values',mat2str(tmp2));
      end;
    elseif in(3) == 2
      set_param(blkname,...
           'Lower_value',num2str(-fl(1)),...
           'Upper_value',num2str(fl(1)));
    elseif in(3) == 3
      set_param(blkname,...
           'Lower Limit',num2str(-fl(1)),...
           'Upper Limit',num2str(fl(1)));
    elseif in(3) == 4
      set_param(blkname,...
           'Lower Limit',num2str(fl(1)),...
           'Upper Limit',num2str(fl(2)));
    elseif in(3) == 5
%        set_param(blkname,'Expr',temp);
    elseif in(3) == 7
      set_param(blkname,...
	'Expr','(2*(u>0)-1)*(abs(u)+q/2-rem(abs(u)+q/2,q))',...
         'Mask Display',...
	'plot([10,0],[5,5],[5.5,5.5],[0,10],[1,2,2,3,3,4,4,5,5,6,6,7,7,8,8,9,9],[1,1,2,2,3,3,4,4,5,5,6,6,7,7,8,8,9])',...
		'Mask Type','Quantizer')
      set_param(blkname,...
		'Mask Dialogue','Discretizes input at given interval.|Quantization interval:',...
		'Mask Translate','q = @1;',...
		'Mask Help','Quantizes input in given intervals. ',...
		'Mask Entries',[num2str(fl(1)) '\/'])
    elseif in(3) == 8
      set_param(blkname,'Input_Values','[-1,0,0,1]',...
	'Output_Values',mat2str([-fl(2)-fl(1) -fl(1) fl(1) fl(1)+fl(2)]));
    elseif in(3) == 10
        % may not happen
    end;
  else
    set_param(blkname,'Expr',temp);
  end;
else
  for i=1:in(4)
    if in(2) == 7 & in(3) ==1
      add_block('built-in/Gain',[blkname '/Fcn' num2str(i)]);
      set_param([blkname '/Fcn' num2str(i)],'Gain',num2str(fl(i)));
      sh = 'Gain';
    elseif in(2) == 11 & in(3) == 7
      add_block('built-in/Stop simulation',[blkname '/Fcn' num2str(i)]);      
    elseif in(2) == 3
      if in(3) == 2
        add_block('built-in/Dead Zone',[blkname '/Fcn' num2str(i)]);
        set_param([blkname '/Fcn' num2str(i)],...
           'Lower_value',num2str(-fl(i)),...
           'Upper_value',num2str(fl(i)));
        sh = 'plot([-2,2,0,0,0,0,-1,-2,-1,1,2],[0,0,0,2,-2,0,0,-1,0,0,1])';
      elseif in(3) == 3
        add_block('built-in/Saturation',[blkname '/Fcn' num2str(i)]);
        set_param([blkname '/Fcn' num2str(i)],...
           'Lower Limit',num2str(-fl(i)),...
           'Upper Limit',num2str(fl(i)));
        sh = 'plot([-2,2,0,0,0,0,-1,-2,-1,1,2],[0,0,0,2,-2,0,-1,-1,-1,1,1])';
      elseif in(3) == 4
        add_block('built-in/Saturation',[blkname '/Fcn' num2str(i)]);
        set_param([blkname '/Fcn' num2str(i)],...
           'Lower Limit',num2str(fl(i)),...
           'Upper Limit',num2str(fl(i+in(5))));
        sh = 'plot([-2,2,0,0,0,0,-1,-2,-1,1,2],[0,0,0,2,-2,0,-1,-1,-1,1.2,1.2])';
      elseif in(3) == 5
        add_block('built-in/Fcn',[blkname '/Fcn' num2str(i)]);
        set_param([blkname '/Fcn' num2str(i)],'Expr',temp);
%        sh = 'plot([-2,2,0,0,0,0,-2,0,2],[0,0,0,2.5,-.5,0,2,0,2])';
      elseif in(3) == 7
	add_block('built-in/Fcn',[blkname '/Fcn' num2str(i)]);
        set_param([blkname '/Fcn' num2str(i)],'Expr',...
           ['(2*(u>0)-1)*(abs(u)+q/2-rem(abs(u)+q/2,q))'],'Mask Display',...
   	   'plot([10,0],[5,5],[5.5,5.5],[0,10],[1,2,2,3,3,4,4,5,5,6,6,7,7,8,8,9,9],[1,1,2,2,3,3,4,4,5,5,6,6,7,7,8,8,9])',...
	   'Mask Type','Quantizer');
	set_param([blkname '/Fcn' num2str(i)],...
	   'Mask Dialogue',...
	   'Discretizes input at given interval.|Quantization interval:',...
		'Mask Translate','q = @1;',...
		'Mask Help','Quantizes input in given intervals. ',...
		'Mask Entries',[num2str(fl(i)) '\/'])
        sh = 'plot([10,0],[5,5],[5.5,5.5],[0,10],[1,2,2,3,3,4,4,5,5,6,6,7,7,8,8,9,9],[1,1,2,2,3,3,4,4,5,5,6,6,7,7,8,8,9])';
      elseif in(3) == 8 
	add_block('built-in/Look Up Table',[blkname '/Fcn' num2str(i)]);
	set_param([blkname '/Fcn' num2str(i)],...
		'Input_Values','[-1,0,0,1]',...
		'Output_Values',mat2str([-fl(i+in(5))-fl(i) -fl(i) fl(i) fl(i)+fl(i+in(5))]));
        sh = ['plot([-1,1],[0,0],[0,0],[-2,2],[-1,0,0,1],[-2,-1,1,2])'];
      elseif in(3) == 9
        %need to figure out
      end;
    else
      if in(2) == 8 & in(3) ==4
          temp = ['power(u[1],' num2str(fl(i)) ')'];
      elseif in(2) == 8 & in(3) == 5        %46
          temp = ['power(' num2str(fl(i)) ',u[1])']; 
      elseif in(2) == 2 & in(3) == 3
        for j=1:in(6)
          if j==1
            temp = num2str(fl(i+(j-1)*in(5)));
          elseif j==2
            temp = [temp '*u[1] + ' num2str(fl(i+(j-1)*in(5)))];
          else
            temp = ['(' temp ')*u[1] + ' num2str(fl(i+(j-1)*in(5)))];
          end;
        end;
      end;
      add_block('built-in/Fcn',[blkname '/Fcn' num2str(i)]);
      set_param([blkname '/Fcn' num2str(i)],'Expr',temp);
	%,'position',[160,75*i-50,275,75*i-10]);
    end;
    set_param([blkname '/Fcn' num2str(i)],...
	'position',[160,75*i-50,275,75*i-10]);
    set_param([blkname '/input' num2str(i)],'Port',num2str(i),...
	'position',[50,75*i-40,70,75*i-20]);
    autoline(blkname,['input' num2str(i) '/1'], ['Fcn' num2str(i) '/1']);
    if ~(in(2) == 11 & in(3) == 7)
      set_param([blkname '/output' num2str(i)],'Port',num2str(i),...
	'position',[380,75*i-40,400,75*i-20]);
      autoline(blkname,['Fcn' num2str(i) '/1'], ['output' num2str(i) '/1']);
    end;
  end;
  set_param(blkname,'Mask Display',sh);
end;



