function sb2slss(blkname,in,fl,cod,timer)
%SB2SLSS converts general algebraic blocks into SIMULINK.
%	SB2SLSS(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/10/92 -- 9/14/92
% Copyright (c) 1990-93 by The MathWorks, Inc.
% $Revision: 1.11 $  $Date: 1993/06/09 22:09:40 $

% in = [8, code1 code2, input, output, state,....
% in(x)   A matrix element number 
% in(x+1) B matrix element number
% in(x+2) C matrix element number
% in(x+3) D matrix element number
% in(x+4:x+3+in(x)) Row position of parameters in A
% in(x+4+in(x):x+3+2*in(x)) Column position of parameters in A
% in(x+4+2*in(x):x+3+2*in(x)+in(x+1)) 
%                               Row position of parameters in B
% in(x+4+2*in(x)+in(x+1):x+3+2*in(x)+2*in(x+1)) 
%                               Column position of parameters in B
% in(x+4+2*in(x)+2*in(x+1):x+3+2*in(x)+2*in(x+1)+in(x+2))
%				Row position of parameters in C
% in(x+4+2*in(x)+2*in(x+1)+in(x+2):x+3+2*in(x)+2*in(x+1)+2*in(x+2))
%				Column position of parameters in C
% in(x+4+2*in(x)+2*in(x+1)+2*in(x+2):x+3+2*in(x)+2*in(x+1)+2*in(x+2)+in(x+3))
%				Row position of parameters in D
% in(x+4+2*in(x)+2*in(x+1)+2*in(x+2)+in(x+3):x+3+2*in(x)+2*in(x+1)+2*in(x+2)+2*in(x+3))
%				Column position of parameters in D
% x == in(17)+3;
% x == in(4)*2 + in(8) + 17    %14
% this one works the same for continuous or discrete time systems
%
x = in(17) + 3;
ind=0;
a=zeros(in(6),in(6));b=zeros(in(6),in(4));
c=zeros(in(5),in(6));d=zeros(in(5),in(4));
x0=zeros(1,in(6));
%initial condition 
if in(18) ~= 0
  for i=1:in(6)
    ind=ind+1;
    x0(i) = fl(ind);
  end;
end;
% a matrix
ii = x+3;
jj = ii + in(x);
if in(x) ~= 0,
    for i=1:in(x)
      ind=ind+1;
      a(in(ii+i),in(jj+i)) = fl(ind);
    end;
end;
% b matrix
ii = ii+2*in(x);
jj = ii + in(x+1);
if in(x+1) ~= 0
    for i=1:in(x+1)
      ind=ind+1;
      b(in(ii+i),in(jj+i)) = fl(ind);
    end;
end;
% c matrix
ii = ii+2*in(x+1);
jj = ii + in(x+2);
if in(x+2) ~= 0
    for i=1:in(x+2)
      ind=ind+1;
      c(in(ii+i),in(jj+i)) = fl(ind);
    end;
end;
% d matrix
ii = ii+2*in(x+2);
jj = ii + in(x+3);
if in(x+3) ~= 0
    for i=1:in(x+3)
      ind=ind+1;
      d(in(ii+i),in(jj+i)) = fl(ind);
    end;
end;
xa=mat2str(a); xb=mat2str(b); xc=mat2str(c); xd=mat2str(d); xx0 = mat2str(x0);
if length(xa)>=255 | length(xb)>=255 | length(xc)>=255 | length(xd) >= 255
  load sb2sltmp
  app = sb2slbnm(blkname);
  xa = ['O' num2str(xn+1) app]; xb = ['O' num2str(xn+2) app]; 
  xc = ['O' num2str(xn+3) app]; xd = ['O' num2str(xn+4) app];
  xx0= ['O'  num2str(xn+5) app];
  eval([xa '= a;']);  appsave('sb2sltmp', xa, a);
  disp([xa ' is used as A matrix in system ' blkname])
  eval([xb '= b;']);  appsave('sb2sltmp', xb, b);
  disp([xb ' is used as B matrix in system ' blkname])
  eval([xc '= c;']);  appsave('sb2sltmp', xc, c);
  disp([xc ' is used as C matrix in system ' blkname])
  eval([xd '= d;']);  appsave('sb2sltmp', xd, d);
  disp([xd ' is used as D matrix in system ' blkname])
  eval([xx0 '= x0;']);  appsave('sb2sltmp', xx0, x0);
  disp([xx0 ' is used as initial condition in system ' blkname])
  % xs=[xs xa xb xc xd xx0];
  xn=xn+5;
  appsave('sb2sltmp', 'xn', xn);
  % eval(xs)
end;
if in(4) <= 1 & in(5) <=1
  set_param(blkname,'A',xa,'B',xb,'C',xc,'D',xd,'X0',xx0);
  if cod(1) == 1
    set_param(blkname,'Sample time',num2str(timer(1)));
  end;
else
  set_param(blkname,'Location',[329,696,801,874])
  if cod(1) == 0
    add_block('built-in/State-space',[blkname '/State-space'])
  elseif cod(1) == 1
    add_block('built-in/Discrete State-space',[blkname '/State-space'])
    set_param([blkname '/State-space'], 'Sample time',num2str(timer(1)));
  end;
  set_param([blkname '/State-space'],...
	'A',xa,...
	'B',xb,...
        'C',xc,...
        'D',xd,...
	'X0',xx0,...
	'position',[195,50,265,80]);
  for i=1:in(4), 
    set_param([blkname '/input' num2str(i)],...
	'position',[45-30*rem(i,2),15*i+20,65-30*rem(i,2),15*i+40],...
	'Port', num2str(i));
  end;
  for i=1:in(5),
    set_param([blkname '/output' num2str(i)],...
	'position',[375+30*rem(i,2),15*i+20,395+30*rem(i,2),15*i+40],...
	'Port', num2str(i));
  end;
  if in(4) > 1
    add_block('built-in/Mux',[blkname '/Mux'])
    set_param([blkname '/Mux'],...
		'inputs',num2str(in(4)),...
		'position',[135,40,165,60+15*in(4)])
    autoline(blkname, 'Mux/1', 'State-space/1');
    for i = 1:in(4)
      autoline(blkname, ['input' num2str(i) '/1'], ['Mux/' num2str(i)]);
    end;
  else
    autoline(blkname, ['input1/1'], 'State-space/1');
  end;
  if in(5) > 1
    add_block('built-in/Demux',[blkname '/Demux'])
    set_param([blkname '/Demux'],...
		'outputs',num2str(in(5)),...
		'position',[310,40,340,60+15*in(5)])
    drawnow
    ttmp = get_param([blkname '/Demux'],'position');
    set_param([blkname '/Demux'],'position',ttmp);
    drawnow
    autoline(blkname, 'State-space/1', 'Demux/1');
    for i = 1:in(5)
      autoline(blkname, ['Demux/' num2str(i)], ['output' num2str(i) '/1']);
    end;
  else
    autoline(blkname,'State-space/1','output1/1');
  end;
  if cod(1) == 0
    set_param(blkname,'Mask Display','x''=Ax+Bu\ny=Cx+Du');
  else 
    set_param(blkname,'Mask Display','x(k+1)''=Ax(k)+Bu(k)\ny(k)=Cx(k)+Du(k)');
  end;
end;
