function sb2slsc(blkname,in,fl,cod,timer)
%SB2SLSC converts general signal source blocks to SIMULINK.
%	SB2SLSC(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/20/92 -- 10/22/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,....
% This function processes all of the blocks with code1 == 10
% code2 = 1: general waveform
% code2 = 2: pulse training
% code2 = 3: square waveform
% code2 = 4: ramp
% code2 = 5: step
% code2 = 6: sine wave
% code2 = 7: noise
% use "repeat sequence" for code2=1
% use "pulse generator" for code2=2
% use "square wave" for code2=3 when start_time=0 and 1/freq=2*width use Sig_Gen.
% use "ramp" for code2=4
% use "step Fcn" for code2=5
% use "sine wave" for code2 = 6
% use "Signal Gen" for code2 = 7 when it is a uniform noise
%     "White Noise" for code2=7 when it is a normal noise
%     forget the one with Ornstein-Unlenbeck distribution

if in(3) == 1
  % general waveform
  % It always uses the new system no matter how many outputs it has.
  x0=fl(1:in(5))';  % initial condition
  temp = in(in(17)+3);
  input = fl(in(5)+1:in(5)+temp)';
  for i =1:in(5)
    outpt=[outpt; fl(in(5)+i*temp+1:in(5)+(i+1)*temp)'];
  end;
  set_param(blkname,'Location',[264,406,623,721])
  add_block('built-in/Clock',[blkname '/Clock'])
  set_param([blkname '/Clock'],...
		'position',[45,65,65,85])
  for i=1:in(5) % 
    set_param([blkname '/output' num2str(i)],...
		'Port', num2str(i),...
		'position',[285,65*i,305,65*i+20])
    add_block('built-in/Look Up Table',...
		[blkname '/Lk_Up_T' num2str(i)]);
    set_param([blkname '/Lk_Up_T' num2str(i)],...
		'Input_Values','t',...
		'Output_Values',['y(' num2str(i) ',:)'],...
		'position',[210,65*i-2,250,65*i+22])
    add_block('built-in/Fcn',[blkname '/Fcn' num2str(i)])
    set_param([blkname '/Fcn' num2str(i)],...
		'Expr',['rem((u[1]-stt(' num2str(i) ')),period)'],...
		'position',[110,65*i,150,65*i+20])
    autoline(blkname,'Clock/1',['Fcn' num2str(i) '/1']);
    autoline(blkname,['Fcn' num2str(i) '/1'],['Lk_Up_T' num2str(i) '/1']);
    autoline(blkname,['Lk_Up_T' num2str(i) '/1'],['output' num2str(i) '/1']);
  end;
  set_param(blkname,'Mask Display',...
   'plot([0,st],[0,0],[t+st,t+period+st,t+st+2*period],[0 yy,yy,yy])',...
   'Mask Type','Repeating table')
   set_param(blkname,'Mask Dialogue',...
	'Repeating table. Enter the signal starting time. Enter Time values(relative to start time) and output  values for the first cycle.|Time values:|Output values:|Start time:',...
	'Mask Translate',...
	'stt=@3;period=max(@1)-min(@1);t=@1;y=@2;st=stt(1);yy=y(1,:);')
  set_param(blkname,'Mask Help',...
   'Repeats the cycle given in the table. The time values should be monotonically increase. The output signal is zero before the start time.',...
   'Mask Entries',[mat2str(input) '\/' mat2str(outpt) '\/' mat2str(x0) '\/']);
elseif in(3) == 2
  %pulse training
  x0 = fl(1:in(5))';
  mag = fl(in(5)+1:in(5)*2)';
  wid = fl(in(5)*2+1:in(5)*3)';
  per = 1 ./ fl(in(5)*3+1:in(5)*4)';
  set_param(blkname,'Location',[30,59,326,307])
  add_block('built-in/Clock',[blkname '/Clock'])
  set_param([blkname '/Clock'],'position',[55,65,75,85])
  for i=1:in(5),
    add_block('built-in/Fcn',[blkname '/Fcn' num2str(i)])
    tp=num2str(i);
    temp = ['ht(' tp ') * ((( rem((u[1]-stt(' tp ')),Ts(' tp ')) <= duration(' tp];
    temp = [temp ')) - (u[1] < stt(' tp '))) > 0)'];
    set_param([blkname '/Fcn' num2str(i)],'Expr',temp,...        
	'position',[130,65*i,170,65*i+20]);
    set_param([blkname '/output' tp],...
	'Port', tp,...
	'position',[210,65*i,230,65*i+20])
    autoline(blkname,'Clock/1',['Fcn' tp '/1']);
    autoline(blkname,['Fcn' tp '/1'],['output' tp '/1']);
    add_block('built-in/Discrete Transfer Fcn',[blkname '/rising edge sharper' tp])
    set_param([blkname '/rising edge sharper' tp],...
	'Denominator','[1]',...
	'Sample time',['[Ts(' tp '),stt(' tp ')]'],...
	'position',[325,65*i,370,65*i+30])
    add_block('built-in/Discrete Transfer Fcn',[blkname '/cutting edge sharper' tp])
    set_param([blkname '/cutting edge sharper' num2str(i)],...
	'Denominator','[1]',...
	'Sample time',['[Ts(' tp '),stt(' tp ')+duration(' tp ')]'],...
	'position',[385,65*i,430,65*i+30])
  end;
  set_param(blkname,'Mask Display',...
    'plot(0,0,100,100,[90,75,75,60,60,35,35,20,20,10],[20,20,80,80,19,20,80,80,20,20])',...
    'Mask Type','Pulse generator')
  set_param(blkname,'Mask Dialogue',...
    'Pulse generator.|Pulse period (secs):|Pulse width:|Pulse height:|Pulse start time:',...
    'Mask Translate','Ts = @1; duration = @2; ht = @3; stt = @4;')
  temp = 'Pulse generator which ensures that pulse\ntransitions are hit.';
  temp = [temp ' Uses clock, Fcn and ZOH block. Unmask to see how it works.'];
  set_param(blkname,'Mask Help',temp,...
    'Mask Entries',[mat2str(per) '\/' mat2str(wid) '\/' mat2str(mag) '\/' mat2str(x0) '\/'])
elseif in(3) == 3
  %square waveform
  x0 = fl(1:in(5))';
  mag = fl(in(5)+1:in(5)*2)';
  wid = fl(in(5)*2+1:in(5)*3)';
  per = 1 ./ fl(in(5)*3+1:in(5)*4)';
  if in(5) == 1 & x0 == 0 & per./wid == 2
    % we may use signal Gen here
    delete_block(blkname);
    add_block('built-in/Signal Generator',blkname);
    set_param(blkname, 'Peak',num2str(mag),...
        'Peak Range',num2str(mag*5),'Freq',num2str(1/per),...
	'Freq Range',num2str(5/per),'Wave','Sqr',...
	'Units','Rads');
  else
    % regular procedure
    set_param(blkname,'Location',[30,59,326,307])
    add_block('built-in/Clock',[blkname '/Clock'])
    set_param([blkname '/Clock'],'position',[55,65,75,85])
    for i=1:in(5),
      add_block('built-in/Fcn',[blkname '/Fcn' num2str(i)])
      tp=num2str(i);
      set_param([blkname '/Fcn' num2str(i)],'Expr',...
        ['2*ht(' tp ')*((rem((u[1]-stt(' tp ')),Ts(' tp '))<=duration(' tp '))-0.5)*(u[1]>=stt(' tp '))'],...
	'position',[130,65*i,170,65*i+20]);
      set_param([blkname '/output' tp],...
	'Port', tp,...
	'position',[210,65*i,230,65*i+20])
      autoline(blkname,'Clock/1',['Fcn' tp '/1']);
      autoline(blkname,['Fcn' tp '/1'],['output' tp '/1']);
      add_block('built-in/Discrete Transfer Fcn',[blkname '/rising edge sharper' tp])
      set_param([blkname '/rising edge sharper' tp],...
	'Denominator','[1]',...
	'Sample time',['[Ts(' tp '),stt(' tp ')]'],...
	'position',[325,65*i,370,65*i+30])
      add_block('built-in/Discrete Transfer Fcn',[blkname '/cutting edge sharper' tp])
      set_param([blkname '/cutting edge sharper' num2str(i)],...
	'Denominator','[1]',...
	'Sample time',['[Ts(' tp '),stt(' tp ')+duration(' tp ')]'],...
	'position',[385,65*i,430,65*i+30])
    end;
    temp = 'plot([0,2*t1+t3],[0 0],[0,t3,t3,t2+t3,t2+t3,t1+t3,t1+t3,t2+t1+t3,t2+t1+t3,2*t1+t3],[0 0 z z -z -z z z -z -z])';
    set_param(blkname,'Mask Display',temp,...
     'Mask Type','Pulse generator')
    set_param(blkname,'Mask Dialogue',...
     'Pulse generator.|Pulse period (secs):|Pulse width:|Pulse height:|Pulse start time:',...
     'Mask Translate',...
     'Ts=@1; duration=@2; ht=@3; stt=@4; t1=Ts(1); t2=duration(1); t3=stt(1);z=ht(1);')
    temp = 'Pulse generator which ensures pulse transitions are hit.';
    temp = [temp ' Uses clock, fcn and ZOH block. Unmask to see how it works.'];
    set_param(blkname,'Mask Help',temp,...
     'Mask Entries',[mat2str(per) '\/' mat2str(wid) '\/' mat2str(mag) '\/' mat2str(x0) '\/'])
  end
elseif in(3) == 4
  %ramp
  x0=fl(1:in(5))';
  sl=fl(in(5)+1:in(5)*2)';
  sa=fl(in(5)*2+1:in(5)*3)';
  set_param(blkname,'Location',[217,241,565,376])
  add_block('built-in/Clock',[blkname '/Clock'])
  set_param([blkname '/Clock'],...
		'position',[55,65,75,85])
  for i=1:in(5)
    tp = num2str(i);
    set_param([blkname,'/output' tp],...
	'Port',tp,'position',[295,65*i,315,65*i+20])
    add_block('built-in/Fcn',[blkname '/RMP' tp])
    set_param([blkname '/RMP' tp],...
	'Expr',['sl(' tp ')*(u[1]-st(' tp '))*(u[1]>=st(' tp '))'],...
	'position',[90,65*i,155,65*i+20])
    add_block('built-in/Fcn',[blkname,'/LMT' tp])
    set_param([blkname,'/LMT',tp],...
	'Expr',['u[1]*(u[1]<=sa(' tp '))+sa(' tp ')*(u[1]>sa(' tp '))'],...
	'position',[190,65*i,255,65*i+20])
    autoline(blkname,'Clock/1',['RMP' tp '/1']);
    autoline(blkname,['RMP' tp '/1'],['LMT' tp '/1']);
    autoline(blkname,['LMT' tp '/1'],['output' tp '/1']);
  end;
  set_param(blkname,...
    'Mask Display','plot([0,0],[-.3,1.2],[-.3,2.2],[0,0],[0,1,2],[0,1,1])',...
    'Mask Type','Ramp',...
    'Mask Dialogue','At the Start time, an output is produced which ramps at the Ramp slope ratio until it saturates at the Ramp saturation.|Start time:|Ramp slope ratio:|Ramp saturation:')
  set_param(blkname,...
    'Mask Translate','st=@1;sl=@2;sa=@3;',...
    'Mask Help','RAMP Produces a ramp signal, with slope equal to the Ramp slope ratio, starts at the Start time, and saturates at the Saturation value. The dimension of the output signal is fixed.',...
    'Mask Entries',[mat2str(x0) '\/' mat2str(sl) '\/' mat2str(sa) '\/'])
elseif in(3) == 5
  %step
  x0 = fl(1:in(5))';
  mag = fl(in(5)+1:in(5)*2);
  if in(5) == 1
    set_param(blkname,'Time',num2str(x0),...
	'After',num2str(mag));
  else
    set_param(blkname,'Location',[115,466,316,675])
    for i=1:in(5)
      tp = num2str(i);
      set_param([blkname '/output' tp],...
	'Port',tp,...
	'position',[145,50*i,165,50*i+20])
      add_block('built-in/Step Fcn',[blkname,'/Step Fcn' tp])
      set_param([blkname, '/Step Fcn' tp],...
		'Time',['st(' tp ')'],...
		'After',['mag(' tp ')'],...
		'position',[50,50*i,70,50*i+20]);
      autoline(blkname,['Step Fcn' tp '/1'],['output' tp '/1']);
    end;
    set_param(blkname,'Mask Display',...
	'plot([0 0],[-0.2,1.1],[-0.2,1.1],[0 0],[.3 .3 1],[0 1 1])',...
	'Mask Type','Step Fcn',...
	'Mask Dialogue','At t=Step time, output changes from zero to Final Value.|Start time:|Final value:')
    set_param(blkname,...
	'Mask Translate','st=@1;mag=@2;',...
	'Mask Help','STEP The step Fcn block provides a signal transition between two values occuring at a specified time. The dimention of the output signal is fixed.',...
	'Mask Entries',[mat2str(x0) '\/' mat2str(mag) '\/'])    
  end;
elseif in(3) == 6
  %sine wave
  x0=fl(1:in(5))';
  mag=fl(in(5)+1:in(5)*2)';
  ph=fl(in(5)*2+1:in(5)*3)';
  frq=fl(in(5)*3+1:in(5)*4)';
  if in(5) == 1 & x0 == 0
    set_param(blkname,'amplitude',num2str(mag),...
	'frequency',num2str(frq),...
	'phase',num2str(ph));
  else
    if in(5) == 1
      delete_block(blkname);
      new_system(blkname);
      add_block('built-in/Outport',[blkname,'/output1']);
    end;  
    add_block('built-in/Clock',[blkname '/Clock'])
    set_param([blkname '/Clock'],'position',[40,50,60,70])
    for i=1:in(5)
      tp = num2str(i);
      set_param([blkname '/output' num2str(i)],...
	  'Port',tp,'position',[320,50*i,340,50*i+20])
      add_block('built-in/Fcn',[blkname,'/Fcn',tp]);
      set_param([blkname '/Fcn' tp],'Expr',...
	  ['sin(((u[1]-tm(' tp '))*frq(' tp ')*2+ph(' tp ')/180)*3.141592635)*(u[1]>=tm(' tp '))*mag(' tp ')'],...
	  'position',[205,50*i,245,50*i+20])
      autoline(blkname,'Clock/1',['Fcn' tp '/1']);
      autoline(blkname,['Fcn' tp '/1'],['output' tp '/1']);      
    end;
    set_param(blkname,'Mask Display',...
	'plot([0:20],sin([0:20]*pi/10),[0,0],[-1,1],[-1,20],[0,0])',...
	'Mask Type','Sinusoid Signal')
    temp = 'Sine Wave\nMag*sin(Freq*(Time-Start)+Phase).|Start time:|';
    temp = [temp 'Magnitude:|Phase (rads):|Frequecy (Hz):'];
    set_param(blkname,'Mask Dialogue',temp,...
	'Mask Translate','tm=@1;mag=@2;ph=@3;frq=@4;')
    set_param(blkname,'Mask Help',...
	'Multi-Output Sinusoid Signal Generator','Mask Entries',...
	[mat2str(x0) '\/' mat2str(mag) '\/' mat2str(ph/180*pi) '\/' mat2str(frq) '\/']);
  end;
elseif in(3) == 7
  % noise
  % when it is uniform distribution use signal Gen. noise in(in(17)+3) == 1
  % when normal noise use built in noise in(in(17)+3) == 2
  % when it is Omstein-Unlenbeck distribution treat it same as uniform in(in(17)+3) == 3
  seed = in(in(17)+4:in(17)+3+in(5))';
  stt= fl(1:in(5))';
  mean = fl(in(5)+1:in(5)*2)';    %minimum number for uniform
  std  = fl(in(5)*2+1:in(5)*3)';  %maximum number for uniform
  std = std .* std;
  if cod(1) ~= 1
    smpt = fl(in(5)*3+1); 
    smpt = mat2str(smpt*ones(1,length(std)));
  else
    smpt = mat2str(timer(1)*ones(1,length(std)));
  end;
  done = 0;
  if in(5) == 1
    % in the case of directly one block can be used, use one block
    % otherwise, use group
    if in(in(17)+3) == 1 & stt == 0 & std+mean == 0 & cod(1) == 1
      set_param(blkname,'Peak',num2str(std),'Peak Range',num2str(std*5),...
	'Wave','Rnd');
      done = 1;
    elseif in(in(17)+3) == 1 & stt == 0 & std == 1 & cod(1) == 1
      delete_block(blkname);
      add_block('built-in/White Noise',blkname);
      set_param(blkname, 'Seed', num2str(seed));
      done = 1;
    else
      delete_block(blkname);
      new_system(blkname);
      add_block('built-in/Outport',[blkname '/output1']);
    end;
  end;
  if ~done
    set_param(blkname,'Location',[93,402,506,727])
    add_block('built-in/Clock',[blkname '/Clock'])
    set_param([blkname '/Clock'],'position',[20,15,40,35])
    for i=1:in(5)
      tp = num2str(i);
      im1=(i-1)*100;
      % add zero hold when it is a continuous time system
      add_block('built-in/Discrete Transfer Fcn',[blkname '/Zero Order Hold' tp])
      set_param([blkname '/Zero Order Hold' tp],...
		'Sample time',['Ts(' tp ')'],...
		'position',[100,im1+61,165,im1+99])
      % add function
      add_block('built-in/Fcn',[blkname '/Fcn' tp])
      set_param([blkname '/Fcn' tp],'position',...
		[260,59+im1,325,91+im1])
      % add noise generator
      if in(in(17)+3) == 1
        % uniform distribution
        add_block('built-in/Signal Generator',[blkname '/Noise Source' tp])
	set_param([blkname '/Noise Source' tp],...
		'Peak','1.000000',...
		'Peak Range','5.000000',...
		'Wave','Rnd',...
		'position',[15,im1+63,60,im1+97])
	set_param([blkname '/Fcn' tp],'Expr',...
	  ['((u[2]+1)*(maxx(' tp ')-minn(' tp '))/2+minn(' tp '))*(u[1]>=stt(' tp ')) '])
	set_param([blkname '/Zero Order Hold' tp],...
		'Numerator','1','Denominator','1')
      else
        % normal distribution
	add_block('built-in/White Noise',[blkname '/Noise Source' tp])
	set_param([blkname '/Noise Source' tp],...
		'Seed',['seed(' tp ')'],...
		'position',[35,im1+63,55,im1+97])
	set_param([blkname '/Fcn' tp],'Expr',...
		['(u[2]+mean(' tp '))*(u[1]>=stt(' tp '))'])
	set_param([blkname '/Zero Order Hold' tp],...
		'Numerator',['[std(' tp ')]'],...
		'Denominator',['[sqrt(Ts(' tp '))]'])
      end;
      % place mux here
      add_block('built-in/Mux',[blkname '/Mux' tp])
      set_param([blkname '/Mux' tp],'inputs','2',...
		'position',[200,im1+55,230,90+im1])
      % place outport
      set_param([blkname '/output' tp],...
		'Port', tp,...
		'position',[375,65+im1,395,85+im1])
      % add lines
      add_line(blkname,[65,80+im1;90,80+im1])
      add_line(blkname,[330,75+im1;365,75+im1])
      add_line(blkname,[170,80+im1;190,80+im1])
      add_line(blkname,[235,75+im1;250,75+im1])
      add_line(blkname,[45,25;180,25;180,65+im1;190,65+im1])
    end % for
    stt = mat2str(stt);
    mean  = mat2str(mean);
    std   = mat2str(std);
    seed  = mat2str(seed);
    if in(in(17)+3) == 1
	set_param(blkname,...
	   'Mask Display','plot([1:13],r)',...
	   'Mask Type','Uniform White Noise.','Mask Dialogue',...
	   'Uniform Band-limited White Noise.|Relative Start Time:|Minmum Value:|Maximum Value:|Sample Time:')
	set_param(blkname,...
	   'Mask Translate','stt = @1; minn = @2; maxx=@3; Ts = @4; r = rand(1:12);')
	temp = 'Implemented using white noise into Zero Order Hold block. For faster simulation set sample time ';
	temp = [temp 'to highest value possible but in accordance with the fastest dynamics of system.'];
	set_param(blkname,'Mask Help',temp);
	set_param(blkname,'Mask Entries',[stt '\/' mean '\/' std '\/' smpt '\/']);
    else
	set_param(blkname,...
	   'Mask Display','plot([1:13],r)',...
	   'Mask Type','Normal White Noise.','Mask Dialogue',...
	   'Normal Band-limited White Noise.|Relative Start Time:|Noise Mean:|Standard Deviation:|Sample Time:|Seed')
	set_param(blkname,...
	   'Mask Translate','stt = @1; mean = @2; std=@3; Ts = @4; seed = @5; r = rand(1:12);')
	temp = 'Implemented using white noise into Zero Order Hold block. For faster simulation set sample time to ';
	temp = [temp 'highest value possible but in accordance with the fastest dynamics of system.'];
	set_param(blkname,'Mask Help',temp);
	set_param(blkname,'Mask Entries',...
	   [stt '\/' mean '\/' std '\/' smpt '\/' seed '\/'])
    end;
  end; % if ~done
end;
