function sb2sllti(blkname,in,fl,cod,timer)
%SB2SLLTI converts limited interpolation blocks to SIMULINK.
%	SB2SLLTI(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 11/17/92
% Copyright (c) 1990-93 by The MathWorks, Inc.
% $Revision: 1.10 $  $Date: 1993/06/09 22:09:40 $

% Subsystem 'limited int'
% read in the information in 
temp = 0;
% initial condition
if in(in(17)-1) == 0
  ini = zeros(1,in(5));
else
  ini = fl(temp+1:temp+in(5))';
  temp = in(5);
end;
%upper bound
UB = fl(temp+1:temp+in(5))';
%lower bound
LB = fl(temp+1+in(5):temp+in(5)*2)';
%gain
G = fl(temp+1+in(5)*2:temp+in(5)*3)';
%sampling time, in the case of discrete time
T = ones(1,in(5))*timer(1);

%begin convert
set_param(blkname,'Location',[0,102,477,316])

if cod(1) == 0
  for i=1:in(5)
    q= (i - 1) * 140;
    add_block('built-in/Integrator',[blkname '/Integrator' num2str(i)])
    set_param([blkname '/Integrator' num2str(i)],...
	'Initial',['X0(' num2str(i) ')'],...
	'position',[225,58+q,250,92+q])
    add_block('built-in/Mux',[blkname '/Mux' num2str(i)])
    set_param([blkname '/Mux' num2str(i)],...
	'orientation',2,...
	'inputs','2',...
	'position',[265,111+q,295,144+q])
    add_block('built-in/Fcn',[blkname '/Fcn' num2str(i)])
    set_param([blkname '/Fcn' num2str(i)],...
	'orientation',2,...
	'Expr',['(1-(u[1]<=LB(' num2str(i) ') && u[2] <0 || u[1]>=UB(' num2str(i) ') && u[2]>0)) * u[2]'],...
	'position',[155,116+q,235,144+q])
    add_block('built-in/Gain',[blkname '/Gain' num2str(i)])
    set_param([blkname '/Gain' num2str(i)],...
	'Gain',['K(' num2str(i) ')'],...
	'position',[370,65+q,390,85+q])
    add_block('built-in/Sum',[blkname '/Sum' num2str(i)])
    set_param([blkname '/Sum' num2str(i)],...
	'inputs','+-',...
	'position',[125,65+q,145,85+q])
    set_param([blkname '/output' num2str(i)],...
	'position',[420,65+q,440,85+q])
    set_param([blkname '/input' num2str(i)],...
	'position',[30,60+q,50,80+q])
    autoline(blkname,['input' num2str(i) '/1'],['Sum' num2str(i) '/1']);
    autoline(blkname,['input' num2str(i) '/1'],['Mux' num2str(i) '/1'],[],[0,2]);
    autoline(blkname,['Sum' num2str(i) '/1'],['Integrator' num2str(i) '/1']);
    autoline(blkname,['Integrator' num2str(i) '/1'],['Gain' num2str(i) '/1']);
    autoline(blkname,['Integrator' num2str(i) '/1'],['Mux' num2str(i) '/2'],[],[0,2]);
    autoline(blkname,['Mux' num2str(i) '/1'],['Fcn' num2str(i) '/1'],[],[2,2]);
    autoline(blkname,['Fcn' num2str(i) '/1'],['Sum' num2str(i) '/2'],[],[2,0]);
    autoline(blkname,['Gain' num2str(i) '/1'],['output' num2str(i) '/1']);
  end
  set_param(blkname,...
	'Mask Display','plot(-1,-0.2,3.5,1.2,[0.05,1,2,2.9],[0,0,1,1]); 1/s    ',...
	'Mask Type','Limited integrator.',...
	'Mask Dialogue','Limited integrator|Lower bound:|Upper bound:|Initial condition:|Gain')
  set_param(blkname,...
	'Mask Translate','LB=@1;UB=@2;X0=@3;K=@4',...
	'Mask Help','LIMITED INTEGRATOR Implements a limited integrator with an expression of the form:\nif(x<=lb & u<0) or (x>=ub & u>0)\n  dx = 0\nelse\n   dx=u.')
  set_param(blkname,'Mask Entries',...
	[mat2str(LB) '\/' mat2str(UB) '\/' mat2str(ini) '\/' mat2str(G) '\/'])
else
  for i=1:in(5)
    q= (i - 1) * 140;
    add_block('built-in/Sum',[blkname '/Sum1' num2str(i)])
    set_param([blkname '/Sum1' num2str(i)],...
	'position',[240,60+q,260,80+q])
    set_param([blkname '/input' num2str(i)],...
	'position',[30,50+q,50,70+q])
    add_block('built-in/Sum',[blkname '/Sum' num2str(i)])
    set_param([blkname '/Sum' num2str(i)],...
	'inputs','+-',...
	'position',[125,55+q,145,75+q])
    add_block('built-in/Gain',[blkname '/Gain1' num2str(i)])
    set_param([blkname '/Gain1' num2str(i)],...
	'Gain',['T(' num2str(i) ')'],...
	'position',[180,55+q,200,75+q])
    add_block('built-in/Gain',[blkname '/Gain' num2str(i)])
    set_param([blkname '/Gain' num2str(i)],...
	'Gain',['K(' num2str(i) ')'],...
	'position',[400,60+q,420,80+q])
    set_param([blkname '/output' num2str(i)],...
	'position',[450,60+q,470,80+q])
    add_block('built-in/Unit Delay',[blkname '/Unit Delay' num2str(i)])
    set_param([blkname '/Unit Delay' num2str(i)],...
	'orientation',2,...
	'x0',['X0(' num2str(i) ')'],...
	'Sample time',['T(' num2str(i) ')'],...
	'position',[285,95+q,335,115+q])
    add_block('built-in/Transport Delay',[blkname '/Unit Delay1' num2str(i)])
    set_param([blkname '/Unit Delay1' num2str(i)],...
	'orientation',2,...
	'position',[295,135+q,345,155+q])
    add_block('built-in/Fcn',[blkname '/Fcn' num2str(i)])
    set_param([blkname '/Fcn' num2str(i)],...
	'orientation',2,'Expr',...
	['(((u[1]<=LB(' num2str(i) ')) && (u[2] <0)) || ((u[1]>=UB(' num2str(i) ')) && (u[2]>0))) * u[2]'],...
	'position',[125,141+q,205,169+q])
    add_block('built-in/Mux',[blkname '/Mux' num2str(i)])
    set_param([blkname '/Mux' num2str(i)],...
	'orientation',2,...
	'inputs','2',...
	'position',[225,136+q,255,169+q])
    autoline(blkname,['input' num2str(i) '/1'],['Sum' num2str(i) '/1']);
    autoline(blkname,['input' num2str(i) '/1'],['Mux' num2str(i) '/1'],[],[0,2]);
    autoline(blkname,['Sum' num2str(i) '/1'],['Gain1' num2str(i) '/1']);
    autoline(blkname,['Gain1' num2str(i) '/1'],['Sum1' num2str(i) '/1']);
    autoline(blkname,['Sum1' num2str(i) '/1'],['Gain' num2str(i) '/1']);
    autoline(blkname,['Gain' num2str(i) '/1'],['output' num2str(i) '/1']);
    autoline(blkname,['Sum1' num2str(i) '/1'],['Unit Delay' num2str(i) '/1'],[],[0 2]);
    autoline(blkname,['Sum1' num2str(i) '/1'],['Unit Delay1' num2str(i) '/1'],[],[0 2]);
    autoline(blkname,['Unit Delay1' num2str(i) '/1'],['Mux' num2str(i) '/2'],[],[2 2]);
    autoline(blkname,['Unit Delay' num2str(i) '/1'],['Sum1' num2str(i) '/2'],[],[2 0]);
    autoline(blkname,['Mux' num2str(i) '/1'],['Fcn' num2str(i) '/1'],[],[2 2]);
    autoline(blkname,['Fcn' num2str(i) '/1'],['Sum' num2str(i) '/2'],[],[2 2]);
  end;
  set_param(blkname,...
	'Mask Display','plot(-1,-0.2,3.5,1.2,[0,1,2,2.9],[0,0,1,1],[0,1],[0.5,0.5],[0.5,0.5],[0.8,0.6],[0.15,0.15],[0.45,0.2],[0.4,0.6],[0.3,0.3],[0.7,0.9,0.7,0.9],[0.45,0.45,0.2,0.2]);',...
	'Mask Type','Discrete-time limited integrator.')
  set_param(blkname,...
	'Mask Dialogue','Discrete-time limited integrator|Lower bound:|Upper bound:|Initial condition:|Gain:|Sampling Time:',...
	'Mask Translate','LB=@1;UB=@2;X0=@3;K=@4;T=@5;')
  set_param(blkname,...
	'Mask Help','Discrete-time limited integrator. Unmask to see the detail.',...
	'Mask Entries',[mat2str(LB) '\/' mat2str(UB) '\/' mat2str(ini) '\/' mat2str(G) '\/' mat2str(T) '\/'])
end;

