function sb2slga(blkname,in,fl)
%SB2SLGA converts general algebraic blocks to SIMULINK.
%	SB2SLGA(BLKNAME,IN,FL) 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.

% Wes Wang 9/10/92 -- 9/14/92
% Copyright (c) 1990-93 by The MathWorks, Inc.
% $Revision: 1.15 $  $Date: 1993/06/09 22:09:40 $

% in = [8, code1 code2, input, output, state,....
% in(x+1) stack size (sz)
% in(x+2) number of integers in this sequence
% in(x+1+i*2) i=1,..,sz;     2==>u; 4==>p; 7==>y
% in(x+2+i*2) i=1,..,sz;     order of u,p, or y
% in(x+3+sz*2)               relation between sz-1 and sz
%   14->+; 15->- 16->* 17->/ 18->** 19->**
%   20->eqv; 21->neqv; 22->or; 23->nor; 24->and 25->nand 26->not
%   27-><>; 28->=; 29-><; 30-><=; 31->>; 32->>=.
% in(y)==11                  ==>;
% in(y+1)                    end of in(y+1)th y calculation
% x == in(4)*2 + in(8) + 14

%length of blankt == 200
blankt='                                                                                                    ';
blankt = [blankt blankt];
pnt = 0;
wait = 0;  % wait for the order of variable.
%x = in(4)*2 + in(8) + 14;
x = in(17)+6;
%make space:
i = x+2; j = 1;
while (i<x+in(x+1)+2) & (j<=in(5))
  if in(i)<11 % assign a new variable to 
    pnt = pnt+1;
    od = in(i+1);
    if in(i) == 1
      tmp = 'time_clock';
    elseif in(i) == 2,
      tmp = ['u[' num2str(od) ']'];
    elseif in(i) == 4 | in(i) == 5,
      tmp = num2str(fl(od));
      id = find(tmp == 'e');
      if ~isempty(id)
        xy=abs(str2num(tmp(id+1:length(tmp))));
        temp = ['%.' num2str(xy+3) 'f'];
        tmp = sprintf(temp,real(fl(od)));
      end;
    elseif in(i) == 7,
      tmp = ['y[' num2str(od) ']'];
    end;
    z(pnt,:) = [tmp, blankt(length(tmp)+1:200)];
    m(pnt) = 20;   %minimum calculation
    if in(i) > 1
      i=i+1;
    end;
  elseif in(i) == 11
%    if pnt == 1
      tmp = [deblank(z(pnt,:)) ';'];
      y(j,:) = [tmp, blankt(length(tmp)+1:200)];
      pnt = 0;
      j = j+1;
      i = i+1;
      if pnt == 1
	fprintf('\nPlease check block');
	fprintf(blkname);
	fprintf('. Error may be occured in the translation.\n');
      end;
%    else
%      fprintf('error in sb2slga with ');
%      fprintf(blkname);
%      fprintf(' process.\n')
%    end;
  elseif in(i) >= 14 & in(i) ~= 26
    % add bracket if it is necessary
    if m(pnt) <= ceil(in(i)/2)*2 & in(i) <=17
    %if m(pnt) <= 7
      tmp = ['(' deblank(z(pnt,:)) ')'];
      z(pnt,:) = [tmp blankt(length(tmp)+1:200)];
    end;
    pnt = pnt - 1;
    if m(pnt) < ceil(in(i)/2)*2 & in(i) <= 17
    %if m(pnt) <= 7
      tmp = ['(' deblank(z(pnt,:)) ')'];
      z(pnt,:) = [tmp blankt(length(tmp)+1:200)];
    end;
    if in(i) == 14
      tmp = [deblank(z(pnt,:)) ' + ' deblank(z(pnt+1,:))];
    elseif in(i) == 15
      tmp = [deblank(z(pnt,:)) ' - ' deblank(z(pnt+1,:))];
    elseif in(i) == 16
      tmp = [deblank(z(pnt,:)) '*' deblank(z(pnt+1,:))];
    elseif in(i) == 17
      tmp = [deblank(z(pnt,:)) '/' deblank(z(pnt+1,:))];
    elseif in(i) == 18 | in(i) == 19
      tmp = ['power(' deblank(z(pnt,:)) ', ' deblank(z(pnt+1,:)) ')'];
    elseif in(i) == 20
      tmp = [deblank(z(pnt,:)) '==' deblank(z(pnt+1,:))];
    elseif in(i) == 21
      tmp = [deblank(z(pnt,:)) '!=' deblank(z(pnt+1,:))];
    elseif in(i) == 22
      tmp = [deblank(z(pnt,:)) ' || ' deblank(z(pnt+1,:))];
    elseif in(i) == 23
      tmp = ['!(' deblank(z(pnt,:)) '||' deblank(z(pnt+1,:)) ')'];
    elseif in(i) == 24
      tmp = [deblank(z(pnt,:)) ' && ' deblank(z(pnt+1,:))];
    elseif in(i) == 25
      tmp = ['!(' deblank(z(pnt,:)) '&&' deblank(z(pnt+1,:)) ')'];
    elseif in(i) == 27
      tmp = [deblank(z(pnt,:)) '!=' deblank(z(pnt+1,:))];
    elseif in(i) == 28
      tmp = [deblank(z(pnt,:)) '==' deblank(z(pnt+1,:))];
    elseif in(i) == 29
      tmp = [deblank(z(pnt,:)) '<' deblank(z(pnt+1,:))];
    elseif in(i) == 30
      tmp = [deblank(z(pnt,:)) '<=' deblank(z(pnt+1,:))];
    elseif in(i) == 31
      tmp = [deblank(z(pnt,:)) '>' deblank(z(pnt+1,:))];
    elseif in(i) == 32
      tmp = [deblank(z(pnt,:)) '>=' deblank(z(pnt+1,:))];
    end;
    if in(i) == 23 | in(i) == 25
      in(i) = 26;
    end;
    if in(i) >= 27
      in(i) = 20;  % to make the braket match
    end
    z(pnt,:) = [tmp blankt(length(tmp)+1:200)];
    m(pnt) = ceil(in(i)/2)*2;
  elseif in(i) == 26
    % add bracket if it is necessary
    if m(pnt) < ceil(in(i)/2)*2
      tmp = ['(' deblank(z(pnt,:)) ')'];
    else
      tmp = deblank(z(pnt,:));
    end;
    if tmp(1) == '!'
      tmp = tmp(2:length(tmp));
    else
      tmp = ['!' tmp];      
    end;
    z(pnt,:) = [tmp blankt(length(tmp)+1:200)];
    m(pnt) = ceil(in(i)/2)*2;
  end;
  i = i+1;
end;

%now, y(j), j=1:output, contains the string for the calculation.
%Need to replace y[x] to u[x+input]

%beginning of the 'construction work'
if (in(4) <= 1) & (in(5) <=1)
  tmp = deblank(y(1,:));
  set_param(blkname,'Expr',tmp,'Mask Display',tmp(1:length(tmp)-1));
else
  ind = find(y=='y');
  mx = ~isempty(ind);

  %place input port
  for i=1:in(4)
    inname = [blkname '/input' num2str(i)];
%    add_block('built-in/Inport',inname);
    set_param(inname,'Port',num2str(i),'position',[25+5*i,5+40*i,45+5*i,25+40*i]);
  end;
  %place mux port
  if in(4) >1,
    inname = [blkname '/Mux1'];
    add_block('built-in/Mux',inname);
    set_param(inname, 'inputs', num2str(in(4)),'position',[125,40,155,60+in(4)*15]);
    for i=1:in(4)
      autoline(blkname, ['input' num2str(i) '/1'], ['Mux1/' num2str(i)]);
    end;
  end;

  %place function block

  for i=1:in(5)
    infcn = inname;
    ind=find(inname=='/');
    tinfcn = [inname(max(ind)+1:length(inname)) '/1'];
    if mx & i ~= 1
      %if there is a 'y' in the expression, add a mux before the input
        infcn = [blkname '/Mux' num2str(i)];
      add_block('built-in/Mux',infcn);
      set_param(infcn,'inputs',['[' num2str(in(4)+i-2) ',1]'],...
            'position',[200,80*i-40,230,80*i+10]);
      autoline(blkname,tinfcn,...
          [infcn(max(ind)+1:length(infcn)) '/1']);
      autoline(blkname,['Fcn' num2str(i-1) '/1'],...
        [infcn(max(ind)+1:length(infcn)) '/2']);
      inname = infcn;
       tinfcn = ['Mux' num2str(i) '/1'];
    end;
    %direct add function blocks and outport block
    add_block('built-in/Fcn',[blkname '/Fcn' num2str(i)]);
    tmp = deblank(y(i,:));
    %convert y[i] to u[i+in(4)]
    ind=find(tmp == 'y');
    if ~isempty(ind)
      for j = length(ind):-1:1
        tmp(ind(j)) = 'u';
        x=str2num(tmp(ind(j)+2));
        x = num2str(x+in(4));
        if length(x) <= 1
          tmp(ind(j)+2)= x;
        else
          tmp(ind(j)+2+length(x):length(tmp)+length(x)-1) = ...
		tmp(ind(j)+3:length(tmp));
          tmp(ind(j)+2:ind(j)+1+length(x)) = x;
        end;
      end;  
    end;
    set_param([blkname '/Fcn' num2str(i)],...
         'position',[300,80*i-35,450,80*i+15],'Expr',tmp);
%    add_block('built-in/Outport',[blkname '/output' num2str(i)];
    set_param([blkname '/output' num2str(i)], 'Port', num2str(i),...
         'position',[510,80*i-20,530,80*i]);
    if in(4) >0
        autoline(blkname, tinfcn,...
            ['Fcn' num2str(i) '/1']);
    end;
    autoline(blkname, ['Fcn', num2str(i) '/1'], ['output' num2str(i) '/1']);
  end;
  z=deblank(y(1,:));
  if in(5) > 1
    for i=2:in(5)
      z = [z '\n' deblank(y(i,:))];
    end;
  end;
  ind = find(z=='[');
  ind=[ind, find(z==']')];
  ind=[ind, find(z==';')];
  ind=sort(ind);
  if ~isempty(ind)
    for i=length(ind):-1:1
       z(ind(i)) = '';
    end;
  end;
  set_param(blkname,'Mask Display',z);
end;
   
%end of sb2slga.m

