function [stat,post,dsblk,pinnum] = blksuper(bldopt,bh,path,catalog,code,ws,node,dsblk)
%BLKSUPER 
%

% Initial coding by A S Bozin, 5-20-97
% Copyright 1997-2000 The MathWorks, Inc.
% $Revision: 1.7 $ $Date: 2000/06/14 16:11:19 $

   nBlocks = catalog(node).nBlocks;
   sbkids = catalog(node).sbChild;
   partition = catalog(node).partition;
   rect = catalog(node).boundingBox.sb;		   % Super block bounding box
   wdth = rect(3);			   	   % Box width
   if (bldopt{1})
      zoomFactor = int2str(min(5*fix(20*min([800 600]./rect(3:4))),100));
      rect = [80 80 880 680];
   else
      zoomFactor = '100';
      rect = min(rect,[0 0 800 600])+80;	   % Clipping
   end
   nb = size(sbkids,2);				   % Number of blocks
   cb = 1;					   % Current super block
   stat = 0;					   % Error flag
   bstr = code(node).str.sb{1};
   bint = code(node).int.sb{1};
   ind = mfindstr(bstr,1,'|');
   str = bstr(1:ind-1);
   set_param(bh,'ScreenColor','white',...
	        'Location',rect);
   %
   % Inherit activation signal from the parent superblock
   %
   exec = 1;
   enblpin = 0;
   trigpin = 0;
   posting = 2;
   pinblks = {};
   sbType = catalog(node).type;
   sbSample = catalog(node).sampleTime;
   switch (sbType)
   case 0					   % Continuous superblock
      sampleTime = '0';
   case 1					   % Discrete superblock
      sampleTime = sprintf('[%g,%g]',sbSample(1),sbSample(2));
      mode = catalog(node).signal;
      parent = catalog(node).caller;
      if (mode > 0)
	 enblpin = mode;
      end
   case 129					   % Triggered superblock
      sampleTime = '-1';
      sbSample = -1;
      mode = catalog(node).signal(1);
      posting = catalog(node).signal(2);
      if (mode > 0)
	 trigpin = mode;
      else
	 parent = catalog(node).caller;
	 if ((catalog(parent).type ~= 129) | (catalog(parent).signal(1) <= 0))
	    exec = 0;				   % Idle superblock
	 end
      end
   otherwise					   % Procedure type superblocks
      sbSample = catalog(catalog(node).caller).sampleTime;
      if (all(sbSample == 0))
	 blkName = get_param(bh,'Name');
	 CR = char(10);
	 SP = char(32);
	 ind = find(blkName == CR);
	 if (~isempty(ind))
	    blkName(ind) = SP;
	 end
	 msg = {'Procedure superblock cannot be used in any continuous';
		'superblock. Procedure name is';
		blkName};
	 sbboxerror('write',msg);
	 sbSample = [0 0];
      end
      catalog(node).sampleTime = sbSample;	   % Inherit the sampling time
      sampleTime = sprintf('[%g,%g]',sbSample(1),sbSample(2));
   end
   %
   % Add output ports
   %
   vskip = 30;
   default = num2cell(zeros(nBlocks,1));
   ports = struct('sb',struct('input','',...
			      'output','',...
			      'inpLabel',''),...
		  'blk',struct('name','',...
			       'post',default,...
			       'outLabel',''));
   nOutputs = catalog(node).nOutputs;
   output = cell(nOutputs,1);
   rect = [wdth-40 0 wdth-20 10];
   for (k = 1:nOutputs)
      rect([2 4]) = rect([2 4])+vskip;
      blkName = sprintf('output-%d',k);
      output{k} = blkName;
      ph = add_block('built-in/Outport',sprintf('%s/%s',path,blkName),...
		     'Position',rect,...
		     'FontSize',10);
   end
   %
   % Add input ports
   %
   nInputs = catalog(node).nInputs;
   input = cell(nInputs,1);
   rect = [20 0 40 10];
   for (k = 1:nInputs)
      rect([2 4]) = rect([2 4])+vskip;
      blkName = sprintf('input-%d',k);
      input{k} = blkName;
      ph = add_block('built-in/Inport',sprintf('%s/%s',path,blkName),...
		     'SampleTime',sampleTime,...
		     'Position',rect,...
		     'FontSize',10);
   end
   %
   % Active superblock
   %
   if (exec | bldopt{5})
      if (catalog(node).caller)			   % Not the top level superblocks
	 %
	 % Enabled discrete superblock
	 %
	 if (enblpin)
	    rect([2 4]) = rect([2 4])+vskip;
	    eh = add_block('built-in/EnablePort',cat(2,path,'/Enable'),...
			   'ShowName','off',...
			   'Position',rect,...
			   'FontSize',10,...
			   'StatesWhenEnabling','held',...
			   'ShowOutputPort','off');
	 end
	 %
	 % Triggered superblock
	 %
	 if (trigpin)
	    rect([2 4]) = rect([2 4])+vskip;
	    eh = add_block('built-in/TriggerPort',cat(2,path,'/Trigger'),...
			   'ShowName','off',...
			   'Position',rect,...
			   'FontSize',10,...
			   'TriggerType','rising',...
			   'ShowOutputPort','off');
	 end
      end
      %
      % Loop through the list of blocks
      %
      ports.sb.input = input;
      ports.sb.output = output;
      ports.sb.inpLabel = getlabels(5,bint,bstr);
      bbox = catalog(node).boundingBox.blk;	   % Bounding boxes associated with the blocks
      blkOrientations = zeros(nBlocks,1);	   % Block orientations
      for (blk = 1:nBlocks)
	 bstr = code(node).str.blk{blk};	   % Strings associated with the current block
	 bint = code(node).int.blk{blk};	   % Integers
	 bflt = code(node).flt.blk{blk};	   % Floating point numbers
	 grp = bint(3);				   % Block group number
	 typ = bint(4);				   % Block type numbe
	 %
	 % Create a structure to hold block properties
	 %
	 prop = blkprop(bint(bint(12)+[1 6]),bbox(blk,:),bldopt{6});
	 if (strcmp(prop.orientation,'left'))
	    blkOrientations(blk) = 2;
	 end
	 blkId = bint(8);			   % Block ID
	 ind = mfindstr(bstr,1,'|');		   % Block name
	 str = bstr(1:ind-1);
	 blkName = sprintf('%s\n(%d)',fixstring(str),blkId);
	 ports.blk(blk).name = blkName;
	 ports.blk(blk).outLabel = getlabels(6,bint,bstr);
	 newpath = sprintf('%s/%s',path,blkName);
	 switch (grp)
	 case 1
	    switch (typ)
	    case 1
	       %
	       % Super block
	       %
	       if (blk == catalog(node).sbBlock(cb))
		  %
		  % Local superblock
		  %
		  newbh = add_block('built-in/SubSystem',newpath,...
				    'DropShadow',prop.border,...
				    'ForegroundColor',prop.fgColor,...
				    'BackgroundColor',prop.bgColor,...
				    'Orientation',prop.orientation,...
				    'Position',prop.position,...
				    'ShowPortLabels','off');
		  sbhndlmsgs('write',sprintf('Translating %s.',str));
		  indStr = bint(17)+1;
		  ind = mfindstr(bstr,indStr,'|');
		  newnode = sbkids(cb);
		  newpartition = bstr(indStr:ind-1);
		  if (isempty(newpartition))
		     catalog(newnode).partition = partition;
		  else
		     catalog(newnode).partition = newpartition;
		  end
		  catalog(newnode).caller = node;
		  [stat,post,dsblk(:),pinnum] = blksuper(bldopt,newbh,newpath,catalog,code,ws,newnode,dsblk);
		  if (pinnum)
		     pinblks(end+1,:) = {pinnum blk newpath};
		  end
		  cb = min(nb,cb+1);	   	   % Increment counter
		  %
		  % Check the superblock position
		  %
		  outPorts = get_param(newbh,'OutputPorts');
		  inpPorts = get_param(newbh,'InputPorts');
		  if (any(inpPorts(:,2) <= 0) | any(outPorts(:,2) <= 0))
		     rect = get_param(newbh,'Position');
		     rect(4) = rect(4)-min(min(inpPorts(:,2)),min(outPorts(:,2)))+5;
		     set_param(newbh,'Position',rect);
		  end
	       else
		  %
		  % External superblock (file)
		  %
		  [stat,post] = blkunknown(newpath,prop,bstr,bint,bflt,'File');
	       end
	    case 2
	       [stat,post,dsblk(:)] = blkcond(bldopt,newpath,prop,bstr,bint,bflt,catalog,code,ws,node,sbSample,blk,dsblk);
	    case 3			   	   % Text
	       [stat,post] = blktext(path,prop,bstr,bint,bflt);
	    case 5			   	   % BlockScript
	       [stat,post] = blkscript(newpath,prop,bstr,bint,bflt,path,blkName,catalog,ws,node);
	    case 6			   	   % MathScript
	       [stat,post] = blkunknown(newpath,prop,bstr,bint,bflt,'Math Script');
	    otherwise
	       [stat,post] = blkunknown(newpath,prop,bstr,bint,bflt,'Unknown');
	    end
	 case 2
	    switch (typ)
	    case 1			   	   % AlgebraicExpression
	       [stat,post] = blkalgexp(newpath,prop,bstr,bint,bflt,path,blkName,catalog,ws,node,sbSample);
	    case 2			   	   % Summer
	       [stat,post] = blksum(newpath,prop,bstr,bint,bflt,path,blkName);
	    case 3			   	   % Polynomial
	       [stat,post] = blkpoly(newpath,prop,bstr,bint,bflt,path,blkName,catalog,ws,node);
	    case 4			   	   % ElemByElemProd
	       [stat,post] = blkmul(newpath,prop,bstr,bint,bflt,path,blkName);
	    case 5			   	   % DotProduct
	       [stat,post] = blkdotprod(newpath,prop,bstr,bint,bflt,path,blkName);
	    case 6			   	   % CrossProduct
	       [stat,post] = blkcross(newpath,prop,bstr,bint,bflt,path,blkName);
	    case 7			   	   % ElemByElemDivide
	       [stat,post] = blkdiv(newpath,prop,bstr,bint,bflt,path,blkName);
	    case 8			   	   % TypeConversion
	       [stat,post] = blkround(newpath,prop,bstr,bint,bflt,path,blkName);
	    otherwise
	       [stat,post] = blkunknown(newpath,prop,bstr,bint,bflt,'Unknown');
	    end
	 case 3
	    switch (typ)
	    case 1			   	   % BreakPoints
	       [stat,post] = blkbreakpts(newpath,prop,bstr,bint,bflt,path,blkName,catalog,ws,node);
	    case 2			   	   % DeadBand
	       [stat,post] = blkdeadband(newpath,prop,bstr,bint,bflt,path,blkName,catalog,ws,node);
	    case 3			   	   % AbsoluteSaturation
	       [stat,post] = blksaturate(newpath,prop,bstr,bint,bflt,path,blkName,catalog,ws,node);
	    case 4			   	   % BoundedLimit
	       [stat,post] = blkbndlimit(newpath,prop,bstr,bint,bflt,path,blkName,catalog,ws,node);
	    case 5			   	   % AbsoluteValue
	       [stat,post] = blkabs(newpath,prop,bstr,bint,bflt,path,blkName);
	    case 6			   	   % LinearInterpTable
	       [stat,post] = blklintable(newpath,prop,bstr,bint,bflt,path,blkName,catalog,ws,node);
	    case 7			   	   % Quantization
	       [stat,post] = blkquant(newpath,prop,bstr,bint,bflt,path,blkName,catalog,ws,node);
	    case 8			   	   % PreloadFunction
	       [stat,post] = blkpreload(newpath,prop,bstr,bint,bflt,path,blkName,catalog,ws,node);
	    otherwise
	       [stat,post] = blkunknown(newpath,prop,bstr,bint,bflt,'Unknown');
	    end
	 case 4
	    switch (typ)
	    case 1			   	   % StateSpace
	       [stat,post] = blkss(newpath,prop,bstr,bint,bflt,path,blkName,catalog,ws,node,sbSample);
	    case 2			   	   % NumDenCoeffs
	       [stat,post] = blktf(newpath,prop,bstr,bint,bflt,path,blkName,catalog,ws,node,sbSample);
	    case 3			   	   % GainZerosPoles
	       [stat,post] = blkzp(newpath,prop,bstr,bint,bflt,path,blkName,catalog,ws,node,sbSample);
	    case 4			   	   % GainDampsFreqs
	       [stat,post] = blkdf(newpath,prop,bstr,bint,bflt,path,blkName,catalog,ws,node,sbSample);
	    case 5			   	   % Integrator
	       [stat,post] = blkinteg(newpath,prop,bstr,bint,bflt,path,blkName,catalog,ws,node,sbSample);
	    case 6			   	   % Hysteresis
	       [stat,post] = blkhyst(newpath,prop,bstr,bint,bflt,path,blkName,catalog,ws,node,sbSample);
	    case 7			   	   % TimeDelay
	       [stat,post] = blkdly(newpath,prop,bstr,bint,bflt,path,blkName,catalog,ws,node,sbSample);
	    case 8			   	   % LimitedIntegrator
	       [stat,post] = blklimint(newpath,prop,bstr,bint,bflt,path,blkName,catalog,ws,node,sbSample);
	    case 9			   	   % PIDController
	       [stat,post] = blkpidcon(newpath,prop,bstr,bint,bflt,path,blkName,catalog,ws,node,sbSample);
	    case 10			   	   % SpringMassDamper
	       [stat,post] = blk2ndord(newpath,prop,bstr,bint,bflt,path,blkName,catalog,ws,node,sbSample);
	    otherwise
	       [stat,post] = blkunknown(newpath,prop,bstr,bint,bflt,'Unknown');
	    end
	 case 5
	    switch (typ)
	    case 1			   	   % Sine
	       [stat,post] = blksin(newpath,prop,bstr,bint,bflt,path,blkName);
	    case 2			   	   % Cosine
	       [stat,post] = blkcos(newpath,prop,bstr,bint,bflt,path,blkName);
	    case 3			   	   % Arctangent2
	       [stat,post] = blkatan(newpath,prop,bstr,bint,bflt,path,blkName);
	    case 4			   	   % SinAtan2
	       [stat,post] = blksinatan(newpath,prop,bstr,bint,bflt,path,blkName);
	    case 5			   	   % CosAtan2
	       [stat,post] = blkcosatan(newpath,prop,bstr,bint,bflt,path,blkName);
	    case 6			   	   % ArcSine
	       [stat,post] = blkasin(newpath,prop,bstr,bint,bflt,path,blkName);
	    case 7			   	   % CosAsine
	       [stat,post] = blkcosasin(newpath,prop,bstr,bint,bflt,path,blkName);
	    case 8			   	   % ArcCosine
	       [stat,post] = blkacos(newpath,prop,bstr,bint,bflt,path,blkName);
	    otherwise
	       [stat,post] = blkunknown(newpath,prop,bstr,bint,bflt,'Unknown');
	    end
	 case 6
	    switch (typ)
	    case 1			   	   % UserCode
	       [stat,post] = blkunknown(newpath,prop,bstr,bint,bflt,'User Code Block');
	    case 2			   	   % ZeroCrossing
	       [stat,post] = blkzcross(newpath,prop,bstr,bint,bflt,path,blkName);
	    otherwise
	       [stat,post] = blkunknown(newpath,prop,bstr,bint,bflt,'Unknown');
	    end
	 case 7
	    switch (typ)
	    case 1			   	   % GainBlock
	       [stat,post] = blkgain(newpath,prop,bstr,bint,bflt,path,blkName,catalog,ws,node);
	    otherwise
	       [stat,post] = blkunknown(newpath,prop,bstr,bint,bflt,'Unknown');
	    end
	 case 8
	    switch (typ)
	    case 1			   	   % SquareRoot
	       [stat,post] = blksqrt(newpath,prop,bstr,bint,bflt,path,blkName);
	    case 2			   	   % Logarithm
	       [stat,post] = blklog(newpath,prop,bstr,bint,bflt,path,blkName);
	    case 3			   	   % Exponential
	       [stat,post] = blkexp(newpath,prop,bstr,bint,bflt,path,blkName);
	    case 4			   	   % u**Constant
	       [stat,post] = blkuconst(newpath,prop,bstr,bint,bflt,path,blkName,catalog,ws,node);
	    case 5			   	   % Constant**u
	       [stat,post] = blkconstu(newpath,prop,bstr,bint,bflt,path,blkName,catalog,ws,node);
	    case 6			   	   % SignedSquareRoot
	       [stat,post] = blksgnsqrt(newpath,prop,bstr,bint,bflt,path,blkName);
	    otherwise  
	       [stat,post] = blkunknown(newpath,prop,bstr,bint,bflt,'Unknown');
	    end
	 case 9
	    switch (typ)
	    case 1			   	   % Cartesian2Polar
	       [stat,post] = blkc2p(newpath,prop,bstr,bint,bflt,path,blkName);
	    case 2			   	   % Polar2Cartesian
	       [stat,post] = blkp2c(newpath,prop,bstr,bint,bflt,path,blkName);
	    case 3			   	   % Cartesian2Spherical
	       [stat,post] = blkc2s(newpath,prop,bstr,bint,bflt,path,blkName);
	    case 4			   	   % Spherical2Catesian
	       [stat,post] = blks2c(newpath,prop,bstr,bint,bflt,path,blkName);
	    case 5			   	   % 3AxisRotation
	       [stat,post] = blkdtarot(newpath,prop,bstr,bint,bflt,path,blkName);
	    case 6			   	   % 3AxisInverse
	       [stat,post] = blkitarot(newpath,prop,bstr,bint,bflt,path,blkName);
	    otherwise
	       [stat,post] = blkunknown(newpath,prop,bstr,bint,bflt,'Unknown');
	    end
	 case 10
	    switch (typ)
	    case 1			   	   % GeneralWaveform
	       [stat,post] = blkgenwave(newpath,prop,bstr,bint,bflt,path,blkName,catalog,ws,node);
	    case 2			   	   % PulseTrain
	       [stat,post] = blkpulse(newpath,prop,bstr,bint,bflt,path,blkName,catalog,ws,node);
	    case 3			   	   % SquareWave
	       [stat,post] = blksqwave(newpath,prop,bstr,bint,bflt,path,blkName,catalog,ws,node);
	    case 4			   	   % Ramp
	       [stat,post] = blklimramp(newpath,prop,bstr,bint,bflt,path,blkName,catalog,ws,node);
	    case 5			   	   % Step
	       [stat,post] = blkstep(newpath,prop,bstr,bint,bflt,path,blkName,catalog,ws,node,sbSample);
	    case 6			   	   % Sinusoid
	       [stat,post] = blksinegen(newpath,prop,bstr,bint,bflt,path,blkName,catalog,ws,node);
	    case 7			   	   % UniformRandNumb
	       [stat,post] = blkurand(newpath,prop,bstr,bint,bflt,path,blkName,catalog,ws,node,sbSample);
	    case 8			   	   % NormalRandNum
	       [stat,post] = blknormal(newpath,prop,bstr,bint,bflt,path,blkName,catalog,ws,node,sbSample);
	    otherwise
	       [stat,post] = blkunknown(newpath,prop,bstr,bint,bflt,'Unknown');
	    end
	 case 11
	    switch (typ)
	    case 1			   	   % LogicalExpression
	       [stat,post] = blklogexp(newpath,prop,bstr,bint,bflt,path,blkName,catalog,ws,node,sbSample);
	    case 2			   	   % ShiftRegister
	       [stat,post] = blkshiftreg(newpath,prop,bstr,bint,bflt,path,blkName,catalog,ws,node,sbSample);
	    case 3			   	   % Logical (AndOrNot)
	       [stat,post] = blklogic(newpath,prop,bstr,bint,bflt,path,blkName);
	    case 4			   	   % Relational
	       [stat,post] = blkrelop(newpath,prop,bstr,bint,bflt,path,blkName);
	    case 5			   	   % DataPathSwitch
	       [stat,post] = blkswitch(newpath,prop,bstr,bint,bflt,path,blkName);
	    case 6			   	   % GainScheduler
	       [stat,post] = blkgainlist(newpath,prop,bstr,bint,bflt,path,blkName);
	    case 7			   	   % StopSimulation
	       [stat,post] = blkstop(newpath,prop,bstr,bint,bflt,path,blkName);
	    case 12				   % Encoder
	       [stat,post] = blkencode(newpath,prop,bstr,bint,bflt,path,blkName);
	    case 13				   % Decoder
	       [stat,post] = blkdecode(newpath,prop,bstr,bint,bflt,path,blkName);
	    otherwise
	       [stat,post] = blkunknown(newpath,prop,bstr,bint,bflt,'Unknown');
	    end
	 case 13
	    switch (typ)
	    case 1			   	   % STDBlock
	       [stat,post] = blkunknown(newpath,prop,bstr,bint,bflt,'State Transition Diagram');
	    otherwise
	       [stat,post] = blkunknown(newpath,prop,bstr,bint,bflt,'Unknown');
	    end
	 case 14
	    switch (typ)
	    case 1			   	   % DataStore
	       links = [code(node).int.blk,code(node).int.sb(1)];
	       [stat,post] = blkdsmem(newpath,prop,bstr,bint,bflt,path,blkName,sbSample,blk,links);
	       dsblk(catalog(node).bkChild{find(catalog(node).bkBlock == blk)}) = 1;
	    otherwise
	       [stat,post] = blkunknown(newpath,prop,bstr,bint,bflt,'Unknown');
	    end
	 case 15
	    switch (typ)
	    case 1			   	   % Interactive Animation
	       [stat,post] = blkunknown(newpath,prop,bstr,bint,bflt,'Interactive Animation Icons');
	    case 2			   	   % Interactive Animation
	       [stat,post] = blkunknown(newpath,prop,bstr,bint,bflt,'Interactive Animation Icons');
	    case 3			   	   % Interactive Animation
	       [stat,post] = blkunknown(newpath,prop,bstr,bint,bflt,'Interactive Animation Icons');
	    case 4			   	   % Interactive Animation
	       [stat,post] = blkunknown(newpath,prop,bstr,bint,bflt,'Interactive Animation Icons');
	    otherwise
	       [stat,post] = blkunknown(newpath,prop,bstr,bint,bflt,'Unknown');
	    end
	 case 16
	    switch (typ)
	    case 1
	       [stat,post] = blkunknown(newpath,prop,bstr,bint,bflt,'Unknown');
	    otherwise
	       [stat,post] = blkunknown(newpath,prop,bstr,bint,bflt,'Unknown');
	    end
	 case 17
	    switch (typ)
	    case 1			   	   % WriteToVariable
	       [stat,post] = blkwvar(newpath,prop,bstr,bint,bflt,path,blkName);
	    case 2			   	   % ReadFromVariable
	       [stat,post] = blkrvar(newpath,prop,bstr,bint,bflt,path,blkName);
	    otherwise
	       [stat,post] = blkunknown(newpath,prop,bstr,bint,bflt,'Unknown');
	    end
	 case 18
	    switch (typ)
	    case 1			   	   % ConstantInterp
	       [stat,post] = blkc1interp(newpath,prop,bstr,bint,bflt,path,blkName,catalog,ws,node);
	    case 2			   	   % LinearInterp
	       [stat,post] = blkl1interp(newpath,prop,bstr,bint,bflt,path,blkName,catalog,ws,node);
	    case 3			   	   % CubicInterp
	       [stat,post] = blks1interp(newpath,prop,bstr,bint,bflt,path,blkName,catalog,ws,node);
	    case 4			   	   % BiLinearInterp
	       [stat,post] = blkl2interp(newpath,prop,bstr,bint,bflt,path,blkName,catalog,ws,node);
	    case 5		   	   	   % BiCubicInterp
	       [stat,post] = blkc2interp(newpath,prop,bstr,bint,bflt,path,blkName,catalog,ws,node);
	    case 6			    	   % MultiLinInterp
	       [stat,post] = blklninterp(newpath,prop,bstr,bint,bflt,path,blkName,catalog,ws,node);
	    otherwise
	       [stat,post] = blkunknown(newpath,prop,bstr,bint,bflt,'Unknown');
	    end
	 otherwise
	    [stat,post] = blkunknown(newpath,prop,bstr,bint,bflt,'Unknown');
	 end
	 sb2sl('update');			   % Update the waiting bar
	 ports.blk(blk).post = post;
      end
      %
      % Connect the blocks
      %
      post = blkio('init',ones(1,nInputs));
      bint = code(node).int.sb{1};
      bind = bint(9);
      pind = bind+nOutputs;
      %
      % Output posting
      %
      switch (posting)
      case 1					   % At timing requirement
	 dstports = ones(1,nOutputs);
	 indFlt = bint(16)+1;
	 timerq = mat2char(code(node).flt.sb{1}(indFlt));
	 if (catalog(node).caller)		   % Not the top level superblock
	    rect = get_param(bh,'Position');
	    posn = rect;
	    bmarg = 20;
	    if (strcmp(get_param(bh,'Orientation'),'right'))
	       orientation = 'right';
	       posn(3) = posn(3)-bmarg;
	       rect(1) = rect(3)-4;
	    else
	       orientation = 'left';
	       posn(1) = posn(1)+bmarg;
	       rect(3) = rect(1)+4;
	    end
	    set_param(bh,'Position',posn);
	    sbName = get_param(bh,'Name');
	    post.output = cat(2,char(10),'posting');
	    blkName = cat(2,sbName,post.output);
	    parpath = get_param(bh,'Parent');
	    if (nOutputs == 1)
	       ph = add_block('built-in/TransportDelay',cat(2,parpath,'/',blkName),...
			      'Position',rect,...
			      'Orientation',orientation,...
			      'FontSize',10,...
			      'ForegroundColor','black',...
			      'BackgroundColor','black',...
			      'DropShadow','off',...
			      'ShowName','off',...
			      'DelayTime',timerq,...
			      'InitialInput','0',...
			      'BufferSize','1024');
	       add_line(parpath,cat(2,sbName,'/1'),cat(2,blkName,'/1'));
	    else
	       newpath = cat(2,parpath,'/',blkName);
	       newbh = add_block('built-in/SubSystem',newpath,...
				 'Position',rect,...
				 'Orientation',orientation,...
				 'FontSize',10,...
				 'ForegroundColor','black',...
				 'BackgroundColor','black',...
				 'DropShadow','off',...
				 'ShowName','off');
	       rect = [0 0 200 70+vskip*(nOutputs-1)]+80;
	       set_param(newbh,'ScreenColor','white',...
			       'Location',rect);
	       %
	       % Create a memory block
	       %
	       rect = [80 30 120 40+vskip*(nOutputs-1)];
	       ph = add_block('built-in/TransportDelay',cat(2,newpath,'/Delay'),...
			      'Position',rect,...
			      'FontSize',10,...
			      'ForegroundColor','black',...
			      'BackgroundColor','white',...
			      'DropShadow','off',...
			      'ShowName','off',...
			      'DelayTime',timerq,...
			      'InitialInput',mat2char(zeros(1,nOutputs)),...
			      'BufferSize','1024');
	       rect([1 3]) = [60 64];
	       ph = add_block('built-in/Mux',cat(2,newpath,'/Mux'),...
			      'Position',rect,...
			      'ForegroundColor','black',...
			      'BackgroundColor','black',...
			      'DropShadow','off',...
			      'ShowName','off',...
			      'Inputs',sprintf('%d',nOutputs));
	       add_line(newpath,'Mux/1','Delay/1');
	       rect([1 3]) = [136 140];
	       ph = add_block('built-in/Demux',cat(2,newpath,'/Demux'),...
			      'Position',rect,...
			      'ForegroundColor','black',...
			      'BackgroundColor','black',...
			      'DropShadow','off',...
			      'ShowName','off',...
			      'Outputs',sprintf('%d',nOutputs));
	       add_line(newpath,'Delay/1','Demux/1');
	       rect = [20 0 40 10];
	       for (k = 1:nOutputs)
		  rect([2 4]) = rect([2 4])+vskip;
		  blkName = sprintf('input-%d',k);
		  ph = add_block('built-in/Inport',cat(2,newpath,'/',blkName),...
				 'Position',rect,...
				 'FontSize',10);
		  add_line(newpath,cat(2,blkName,'/1'),sprintf('Mux/%d',k));
	       end
	       rect = [160 0 180 10];
	       outport = get_param(bh,'OutputPorts');
	       inpport = get_param(newbh,'InputPorts');
	       for (k = 1:nOutputs)
		  rect([2 4]) = rect([2 4])+vskip;
		  blkName = sprintf('output-%d',k);
		  ph = add_block('built-in/Outport',cat(2,newpath,'/',blkName),...
				 'Position',rect,...
				 'FontSize',10);
		  add_line(newpath,sprintf('Demux/%d',k),cat(2,blkName,'/1'));
		  add_line(parpath,[outport(k,:);inpport(k,:)]);
	       end
	    end
	 end
      case 2					   % As soon as finished
	 dstports = ones(1,nOutputs);
      case 3					   % At next trigger
	 dstports = [1:nOutputs];
	 rect = [wdth-60 30 wdth-56 40+vskip*(nOutputs-1)];
	 if (nOutputs == 1)
	    ph = add_block('built-in/Memory',cat(2,path,'/posting'),...
			   'Position',rect,...
			   'FontSize',10,...
			   'ForegroundColor','black',...
			   'BackgroundColor','black',...
			   'DropShadow','off',...
			   'ShowName','off',...
			   'X0','0',...
			   'InheritSampleTime','on');
	    add_line(path,'posting/1','output-1/1');
	    ports.sb.output{1} = 'posting';
	 else
	    newpath = cat(2,path,'/posting');
	    newbh = add_block('built-in/SubSystem',newpath,...
			      'Position',rect,...
			      'FontSize',10,...
			      'ForegroundColor','black',...
			      'BackgroundColor','black',...
			      'DropShadow','off',...
			      'ShowName','off');
	    rect = [0 0 200 70+vskip*(nOutputs-1)]+80;
	    set_param(newbh,'ScreenColor','white',...
			    'Location',rect);
	    %
	    % Create a memory block
	    %
	    rect = [80 30 120 40+vskip*(nOutputs-1)];
	    ph = add_block('built-in/Memory',cat(2,newpath,'/Memory'),...
			   'Position',rect,...
			   'FontSize',10,...
			   'ForegroundColor','black',...
			   'BackgroundColor','white',...
			   'DropShadow','off',...
			   'ShowName','off',...
			   'X0',mat2char(zeros(1,nOutputs)),...
			   'InheritSampleTime','on');
	    rect([1 3]) = [60 64];
	    ph = add_block('built-in/Mux',cat(2,newpath,'/Mux'),...
			   'Position',rect,...
			   'ForegroundColor','black',...
			   'BackgroundColor','black',...
			   'DropShadow','off',...
			   'ShowName','off',...
			   'Inputs',sprintf('%d',nOutputs));
	    add_line(newpath,'Mux/1','Memory/1');
	    rect([1 3]) = [136 140];
	    ph = add_block('built-in/Demux',cat(2,newpath,'/Demux'),...
			   'Position',rect,...
			   'ForegroundColor','black',...
			   'BackgroundColor','black',...
			   'DropShadow','off',...
			   'ShowName','off',...
			   'Outputs',sprintf('%d',nOutputs));
	    add_line(newpath,'Memory/1','Demux/1');
	    rect = [20 0 40 10];
	    for (k = 1:nOutputs)
	       rect([2 4]) = rect([2 4])+vskip;
	       blkName = sprintf('input-%d',k);
	       ph = add_block('built-in/Inport',cat(2,newpath,'/',blkName),...
			      'Position',rect,...
			      'FontSize',10);
	       add_line(newpath,cat(2,blkName,'/1'),sprintf('Mux/%d',k));
	    end
	    rect = [160 0 180 10];
	    for (k = 1:nOutputs)
	       rect([2 4]) = rect([2 4])+vskip;
	       blkName = sprintf('output-%d',k);
	       ph = add_block('built-in/Outport',cat(2,newpath,'/',blkName),...
			      'Position',rect,...
			      'FontSize',10);
	       add_line(newpath,sprintf('Demux/%d',k),cat(2,blkName,'/1'));
	       add_line(path,sprintf('posting/%d',k),cat(2,ports.sb.output{k},'/1'));
	    end
	    [ports.sb.output{:}] = deal('posting');
	 end
      otherwise
      end
      %
      % Connect the output ports
      %
      for (k = 1:nOutputs)
	 borient = zeros(1,2);
	 iblk = bint(bind+k);
	 oblk = bint(pind+k);
	 dstBlock = ports.sb.output{k};
	 dstBlockPath = cat(2,path,'/',dstBlock);
	 dstPort = dstports(k);
	 if (iblk == 0)				   % From an input port
	    if (oblk == 0)			   % Not connected
	       srcPort = [];
	    else				   % Connected to the input port
	       srcBlock = ports.sb.input{oblk};
	       srcBlockPath = cat(2,path,'/',srcBlock);
	       srcPort = 1;
	       lineLabel = ports.sb.inpLabel{oblk};
	    end
	 else					   % From a block
	    borient(1) = blkOrientations(iblk);
	    srcBlock = cat(2,ports.blk(iblk).name,ports.blk(iblk).post.output);
	    srcBlockPath = cat(2,path,'/',srcBlock);
	    srcPort = oblk;
	    lineLabel = ports.blk(iblk).outLabel{oblk};
	 end
	 if (~isempty(srcPort))
	    if (bldopt{3})
	       lh = autoline(path,srcBlockPath,srcPort,dstBlockPath,dstPort,bbox,borient);
	    else
	       %lh = autoline(path,srcBlockPath,srcPort,dstBlockPath,dstPort);
	       lh = add_line(path,sprintf('%s/%d',srcBlock,srcPort),...
			          sprintf('%s/%d',dstBlock,dstPort));
	    end
	    if (~isempty(lineLabel))
	       if (~islabeled(srcBlockPath,srcPort))
		  set_param(lh,'Name',lineLabel);
	       end
	    end
	 end
      end
      %
      % Connect the block inputs
      %
      for (blk = 1:nBlocks)
	 borient = zeros(1,2);
	 borient(2) = blkOrientations(blk);
	 bint = code(node).int.blk{blk};
	 bind = bint(9);
	 nInputs = bint(5);
	 pind = bind+nInputs;
	 k = 0;					   % Input port/signal counter
	 channels = ports.blk(blk).post.channels;
	 nChannels = size(channels,2);
	 for (nch = 1:nChannels)
	    dstBlock = cat(2,ports.blk(blk).name,ports.blk(blk).post.input{nch});
	    dstBlockPath = cat(2,path,'/',dstBlock);
	    nSignals = channels(nch);
	    for (nsg = 1:nSignals)
	       k = k+1;				   % Increment input port counter
	       iblk = bint(bind+k);
	       oblk = bint(pind+k);
	       if (nSignals == 1)
		  dstPort = nch;
	       else
		  dstPort = nsg;
	       end
	       if (iblk == 0)
		  if (oblk == 0)
		     srcPort = [];
		  else
		     borient(1) = 0;
		     srcBlock = ports.sb.input{oblk};
		     srcBlockPath = cat(2,path,'/',srcBlock);
		     srcPort = 1;
		     lineLabel = ports.sb.inpLabel{oblk};
		  end
	       else
		  borient(1) = blkOrientations(iblk);
		  srcBlock = cat(2,ports.blk(iblk).name,ports.blk(iblk).post.output);
		  srcBlockPath = cat(2,path,'/',srcBlock);
		  srcPort = oblk;
		  lineLabel = ports.blk(iblk).outLabel{oblk};
	       end
	       if (~isempty(srcPort))
		  if (bldopt{3})
		     lh = autoline(path,srcBlockPath,srcPort,dstBlockPath,dstPort,bbox,borient);
		  else
		     %lh = autoline(path,srcBlockPath,srcPort,dstBlockPath,dstPort);
		     lh = add_line(path,sprintf('%s/%d',srcBlock,srcPort),...
				        sprintf('%s/%d',dstBlock,dstPort));
		  end
		  if (~isempty(lineLabel))
		     if (~islabeled(srcBlockPath,srcPort))
			set_param(lh,'Name',lineLabel);
		     end
		  end
	       end
	    end
	 end
      end
      %
      % Connect the enable and trigger ports
      %
      ennum = size(pinblks,1);
      for (cb = 1:ennum)
	 blk = pinblks{cb,2};			   % Block index
	 pinnum = pinblks{cb,1};		   % Pin number
	 borient = zeros(1,2);
	 borient(2) = blkOrientations(blk);
	 bint = code(node).int.blk{blk};
	 bind = bint(9);
	 nInputs = bint(5);
	 pind = bind+nInputs;
	 dstBlockPath = pinblks{cb,3};
	 iblk = bint(bind+pinnum);
	 oblk = bint(pind+pinnum);
	 dstPort = nInputs+1;
	 if (iblk == 0)
	    if (oblk == 0)
	       srcPort = [];
	    else
	       borient(1) = 0;
	       srcBlockPath = cat(2,path,'/',ports.sb.input{oblk});
	       srcPort = 1;
	       lineLabel = ports.sb.inpLabel{oblk};
	    end
	 else
	    borient(1) = blkOrientations(iblk);
	    srcBlockPath = cat(2,path,'/',ports.blk(iblk).name,ports.blk(iblk).post.output);
	    srcPort = oblk;
	    lineLabel = ports.blk(iblk).outLabel{oblk};
	 end
	 if (~isempty(srcPort))
	    if (bldopt{3})
	       lh = autoline(path,srcBlockPath,srcPort,dstBlockPath,dstPort,bbox,borient);
	    else
	       lh = autoline(path,srcBlockPath,srcPort,dstBlockPath,dstPort);
	    end
	    if (~isempty(lineLabel))
	       if (~islabeled(srcBlockPath,srcPort))
		  set_param(lh,'Name',lineLabel);
	       end
	    end
	 end
      end
      pinnum = enblpin+trigpin;
      %
      % Finally, set the zooming factor
      %
      if (bldopt{1})
	 set_param(path,'ZoomFactor',zoomFactor);
      end
   else
      %
      % Superblock never executed
      %
      set_param(bh,'Tag','Idle');
      post = blkio('init',ones(1,nInputs));
      pinnum = 0;
   end
   return					   % Last card of BLKSUPER

function block = blkprop(flags,bbox,optimize)
%BLKPROP Returns the block properties.
%
%    BLKPROP(FLAG) returns the block properties which are encoded in FLAGS.
%

   %
   % Default block structure
   %
   block = struct('optimize',optimize,...
		  'position',bbox,...
		  'fgColor','black',...
		  'bgColor','white',...
		  'border','off',...
		  'orientation','right',...
		  'inputPins',0,...
		  'outputPins',0,...
		  'labels',1,...
		  'iconType',0);
   %
   % Block orientation, icon type and other properties
   %
   [nibble2,nibble1,nibble0] = deal(flags(1));
   nibble0 = bitand(nibble0,15);
   nibble1 = bitshift(bitand(nibble1,240),-4);
   nibble2 = bitshift(bitand(nibble2,3840),-8);
   if (~bitand(nibble0,1))			   % Border on or off
      block.border = 'on';
   end
   if (bitand(nibble0,2))			   % Icon orientation normal of flipped
      block.orientation = 'left';
   end
   if (bitand(nibble1,1))			   % Input pins scalar or vecorized
      block.inputPins = 1;
   end
   if (bitand(nibble1,2))			   % Output pins scalar or vectorized
      block.outputPins = 1;
   end
   if (bitand(nibble1,4))			   % Labels on or off
      block.labels = 1;
   end
   block.iconType = bitand(nibble2,3);		   % Iconn type
   %
   % Block color property
   %
   cmap = {'red';
           'green';
	   'yellow';
	   'blue';
	   'magenta';
	   'cyan';
	   'orange';
	   'cyan';				   % Pink
	   'yellow';				   % Yellow-green
	   'darkGreen';				   % Blue-green
	   'lightBlue';
	   'blue';				   % Purple
	   'green';				   % Brown
	   'gray'};
   cind = rem(flags(2),15);			   % Color index
   if (cind ~= 0)
      if (cind > 0)				   % Set background color
	 block.bgColor = cmap{cind};
      else					   % Set foreground color
	 block.fgColor = cmap{-cind};
      end
   end
   return					   % Last card of BLKPROP

function labels = getlabels(plab,bint,bstr)
%GETLABELS Returns block labels.
%
%    GETLABELS(PLAB,BINT,BSTR) returns the labels associated with the block
%    specified by BINT and BSTR arguments. PLAB points to the labels
%    encoded in BSTR.

   nlab = bint(plab);
   labels = cell(nlab,1);		   	   % Alloacte memory
   bos = bint(11);
   eos = bint(14);
   if (bos ~= eos)
      str = bstr(bos+1:eos);
      %
      % Loop through the string and extract all port numbers and labels
      %
      bos = 1;					   % Initialization
      ind = 1;
      ind = mfindstr(str,bos,'|');
      while (ind)
	 lab = str(bos:ind-1);
	 [chbeg,count,errmsg,bos] = sscanf(lab,'%d',1);
	 if (lab(bos) == ':')
	    [chend,count,errmsg,off] = sscanf(lab(bos+1:end),'%d',1);
	    labels(chbeg:chend) = {lab(bos+off:end)};
	 else
	    labels{chbeg} = lab(bos:end);
	 end
	 bos = ind+1;
	 ind = mfindstr(str,bos,'|');
      end
   end
   return					   % Last card of GETLABELS

function labeled = islabeled(blk,port)
%ISLABELED
%
%

   bh = get_param(blk,'Handle');
   connections = get_param(bh,'PortConnectivity');
   numInp = size(get_param(bh,'InputPorts'),1);	   % Get the number of input ports
   labeled = length(connections(numInp+port).DstPort) > 1;
   return					   % Last card of ISLABELED
