function sb2slint(blkname,in,fl,cod,timer)
%SB2SLINT converts integrator blocks to SIMULINK.
%	SB2SLINT(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 9/30/92
% Copyright (c) 1990-93 by The MathWorks, Inc.
% $Revision: 1.15 $  $Date: 1993/06/09 22:09:40 $

%inic >0 then initial condition ~= 0
gb = in(7+in(8)+in(4)*2);
x0 = zeros(in(6),1);
if gb ~= 0
  for i=1:gb
    indx = 7+in(8)+in(4)*2+gb+i;
    indy = 7+in(8)+in(4)*2+i;
    x0(in(indy)*in(5)+in(indx))=fl(i);
  end;
end;
fl(in(6)+1:in(6)+in(5))=fl(gb+1:gb+in(5));
fl(1:in(6)) = x0;
gb = in(6);
%test --> need superblock for any reason
% if output==state====gain==1 then we don't need a group.
if in(5) == 1 & in(6) == 1 & fl(gb+1) ==1
  test = 0;
else
  test = 1;
end;

if cod(1) == 0
  adbr = 'built-in/Reset integrator';
  adb  = 'built-in/Integrator';
  did  = '1\n-\ns';
else
  adbr = 'built-in/Reset integrator';
  adb  = 'built-in/Discrete State-space';
  did  = 'Discrete\nIntegrator';
end;

if test == 0
  if in(4) >= 3,
    delete_block(blkname);
    add_block(adbr,blkname);
    set_param(blkname, 'Initial','ic',...
		'Mask Display',did,...
		'Mask Type','Reset Integrator',...
		'Mask Dialogue','Integrates first input. If input 2 is not zero then state is set to input 3.|Initial value:',...
		'Mask Help','RESET The reset integrator may be used to reset the value of an integrator state while the simulation is running. If input two is nonzero then the integrator state (also the output value) is set to the value of input three at that time.',...
		'Mask Translate','ic=@1;');
    if gb ~=0
      set_param(blkname,'Mask Entries',[num2str(fl(1)) '\/']);
    else
      set_param(blkname,'Mask Entries','0\/');
    end; %if gb
  else %if in(4)
    if cod(1) == 0
      if gb ~= 0
        set_param(blkname,'Initial',num2str(fl(1)));
      else %if gb
        set_param(blkname,'Initial','0');
      end; % if gb
    else % if cod
      delete_block(blkname);
      add_block(adb,blkname);
      if gb ~= 0
        set_param(blkname,'X0',num2str(fl(1)),...
	  'A','1','B',num2str(timer(1)),'C','1','D','0',...
	  'Sample time',num2str(timer(1)));
      else % if gb
        set_param(blkname,'X0','0',...
	  'A','1','B',num2str(timer(1)),'C','1','D','0',...
	  'Sample time',num2str(timer(1)));
      end; % if gb
    end; % if cod
  end; %if in(4)
else % if test
  %in the case of SISO
  if in(5) == 1 & in(4) == 1
    delete_block(blkname);
    new_system(blkname);
       add_block('built-in/Inport',[blkname,'/input1']);
       add_block('built-in/Outport',[blkname,'/output1']);
  end; % if in(5)
  set_param(blkname,'Location',[340,458,1222,836])
  %order of each integer: oei
  oei = in(6)/in(5);
  % highest order resettable hor
  hor = (in(4) - 1)/in(5) - 1;
  for i=1:in(5)
    for j=1:oei
      int_blk = [blkname '/int' num2str(i) num2str(j)];
      if j <= hor
        %resettable integer
        if cod(1) == 0
          add_block(adbr,int_blk);
	  set_param(int_blk, 'Initial','ic',...
		'Mask Display',did,...
		'Mask Type','Reset Integrator',...
		'Mask Dialogue','Integrates first input. If input 2 is not zero then state is set to input 3.|Initial value:',...
		'Mask Help','RESET The reset integrator may be used to reset the value of an integrator state while the simulation is running. If input two is nonzero then the integrator state (also the output value) is set to the value of input three at that time.',...
		'Mask Translate','ic=@1;');
          if gb ~= 0
            set_param(int_blk,'Mask Entries',...
		[num2str(fl((j-1)*in(5)+i)) '\/']);
          else % if gb
            set_param(int_blk,'Mask Entries','0\/');
          end; % if gb
          set_param([blkname '/input' num2str(j*in(5)+i)],...
		'Port',num2str(j*in(5)+i),...
		'position',...
		[150*(oei+1.1-j),i*100-40,150*(oei+1.1-j)+20,i*100-20]);
	  set_param(int_blk,'position',...
		[150*(oei+1.5-j),i*100-51,150*(oei+1.5-j)+40,i*100-9]);
          autoline(blkname,['input' num2str(j*in(5)+i) '/1'],...
		['int' num2str(i) num2str(j) '/2']);
        else
          return; % this one is not available right now.
        end; 
      else % if j
        % non-resettable integer
        add_block(adb,int_blk);
        if cod(1) == 0
          if gb ~= 0
    	    set_param(int_blk,'Initial',num2str(fl((j-1)*in(5)+i)));
          else %if gb
    	    set_param(int_blk,'Initial','0');
          end; %if gb
        else %if cod
          if gb ~= 0
            set_param(int_blk,'X0',num2str(fl((j-1)*in(5)+i)),...
  	      'A','1','B',num2str(timer(1)),'C','1','D','0',...
	      'Sample time',num2str(timer(1)));
          else  % if gb
            set_param(int_blk,'X0','0',...
  	      'A','1','B',num2str(timer(1)),'C','1','D','0',...
	      'Sample time',num2str(timer(1)));
          end; % if gb
        end; % if cod
      end; % if j
      set_param(int_blk,'position',...
	[150*(oei+1.5-j),i*100-51,150*(oei+1.5-j)+40,i*100-9]);
    end; % for j
  end; % for i
  if oei > 0
    set_param([blkname '/input' num2str(in(4))],'Port',num2str(in(4)),...
	'position',[15,100,35,120]);
  end; % if oei
  for i=1:in(5)
    for j=1:oei
      if j==oei
        set_param([blkname '/input' num2str(i)],'Port',num2str(i),...
	  'position',[95,100*i-40,115,100*i-20]);
      end; % if j
      if j<= hor
        %connect the input line
        add_line(blkname,[150*(oei+0.5-j)+40,i*100-30;150*(oei+0.5-j)+55,i*100-30;...
	   150*(oei+0.5-j)+55,i*100-44;150*(oei+1.5-j),i*100-44]);
        %connect the reset line
        add_line(blkname,[35,110;50,110;50,i*100+10;150*(oei+1.4-j),i*100+10;...
	   150*(oei+1.4-j),i*100-15;150*(oei+1.5-j),i*100-15]);
      else % if j
        %connect the input line
        add_line(blkname,[150*(oei+0.5-j)+40,i*100-30;150*(oei+1.5-j),i*100-30]);
      end; % if j
    end; % for j
    set_param([blkname '/output' num2str(i)],'Port',num2str(i),...
      'position',[150*(oei+1.2),i*100-40,150*(oei+1.2)+20,i*100-20]);
    if fl(gb+i) ~= 1
      %add gain and connect output
      add_block('built-in/Gain',[blkname '/Gain' num2str(i)]);
      set_param([blkname '/Gain' num2str(i)],'position',...
         [150*(oei+.9),i*100-51,150*(oei+1.1),i*100-9],...
         'Gain',num2str(fl(gb+i)));
      autoline(blkname, ['int' num2str(i) '1/1'], ['Gain' num2str(i) '/1']);
      autoline(blkname, ['Gain' num2str(i) '/1'], ['output' num2str(i) '/1']);
    else %if fl
      %connext output only
      autoline(blkname, ['int' num2str(i) '1/1'], ['output' num2str(i) '/1']);
    end; % if fl
  end; % for i
%  if in(4) > in(5)
%    set_param(blkname,'Mask Display','Reset\nIntegrator')
%  else
    set_param(blkname,'Mask Display',did)
%  end;  
end; %if test

