function [stat,post] = blklogic(path,prop,bstr,bint,bflt,parent,name)
%BLKLOGIC This block describes any of a set of logical functions.
%
% See also BLKRELOP.

% Initial coding by A S Bozin, 6-12-97
% Copyright 1997-2000 The MathWorks, Inc.
% $Revision: 1.6 $ $Date: 2000/06/14 16:09:55 $

   stat = 0;			   		   % Error flag
   nInputs = bint(5);
   nOutputs = bint(6);
   %
   % Build mux and demux if necessary
   %
   nChannels = nInputs/nOutputs;
   channels = zeros(1,nChannels)+nOutputs;
   [post,rect] = blkio('port',channels,nOutputs,prop,path);
   %
   % Get logical operator
   %
   indInt = bint(18)+1;
   indInt = bint(indInt)+indInt;		   % Skip icon data
   operCode = bint(indInt);
   %
   % Logical operator block
   %
   switch (operCode)
   case {1,2,7}
      bh = add_block(cat(2,'libsb2sl/LOG/',mapcode(operCode)),path,...
		     'ForegroundColor',prop.fgColor,...
		     'BackgroundColor',prop.bgColor,...
		     'Orientation',prop.orientation,...
		     'DropShadow',prop.border,...
		     'Position',rect);
   case {3,4,5,6}
      op = mapcode(operCode);
      bh = add_block('built-in/SubSystem',path,...
		     'DropShadow',prop.border,...
		     'ForegroundColor',prop.fgColor,...
		     'BackgroundColor',prop.bgColor,...
		     'Orientation',prop.orientation,...
		     'Position',rect,...
		     'ShowPortLabels','off',...
		     'MaskType','LOG-Palette',...
		     'MaskDescription',sprintf('Logical operator %s.',op),...
		     'MaskInitialization',sprintf('[x,y]=sblibico(''logOperator'',''%s'');',op),...
		     'MaskDisplay','plot(0,0,1,1,x,y)');
      bldlogop(path,nChannels,op);
   otherwise
   end
   %
   % Connect the current block with the mux and demux blocks (if any)
   %
   blkio('line',channels,nOutputs,post,parent,name);
   return					   % Last card of BLKLOGIC

function opstr = mapcode(opnum)
%MAPCODE
%
%

   str = {'EQV';'NEQV';'OR';'NOR';'AND';'NAND';'NOT'};
   opstr = str{opnum};
   return					   % Last card of MAPCODE

function bldlogop(path,nch,op)
%BLDLOGOP
%
%

   vskip = 30;
   %
   % Create input ports
   %
   rect = [20 0 40 10];
   outport = zeros(nch,2);
   for (k = 1:nch)
      rect([2 4]) = rect([2 4])+vskip;
      bh = add_block('built-in/Inport',sprintf('%s/u%d',path,k),...
		     'Position',rect,...
		     'FontSize',10);
      outport(k,:) = get_param(bh,'OutputPorts');
   end
   %
   % Add a treshold
   %
   rect([2 4]) = rect([2 4])+vskip;
   bh = add_block('built-in/Constant',cat(2,path,'/zero'),...
		  'Position',rect,...
		  'ShowName','off',...
		  'Value','0',...
		  'FontSize',10);
   points = zeros(4,2)+70;
   points(1,:) = get_param(bh,'OutputPorts');
   points(2,2) = points(1,2);
   %
   % Create comparators
   %
   rect = [100 0 120 24];
   inpport = zeros(2,2);
   for (k = 1:nch)
      rect([2 4]) = rect([2 4])+vskip;
      bh = add_block('built-in/RelationalOperator',sprintf('%s/bool%d',path,k),...
		     'Position',rect,...
		     'ShowName','off',...
		     'Operator','>',...
		     'FontSize',10);
      inpport(:) = get_param(bh,'InputPorts');
      add_line(path,[outport(k,:);inpport(1,:)]);
      points(4,:) = inpport(2,:);
      points(3,2) = points(4,2);
      add_line(path,points);
      outport(k,:) = get_param(bh,'OutputPorts');
   end
   %
   % Logic
   %
   rect = [150 vskip 180 nch*vskip+24];
   bh = add_block('built-in/Logic',cat(2,path,'/logic'),...
		  'Position',rect,...
		  'Inputs',sprintf('%d',nch),...
		  'Operator',op,...
		  'FontSize',10);
   inpport = get_param(bh,'InputPorts');
   for (k = 1:nch)
      add_line(path,[outport(k,:);inpport(k,:)]);
   end
   outport = get_param(bh,'OutputPorts');
   rect = [210 0 230 10];
   rect([2 4]) = rect([2 4])+(0.5*(nch+1)*vskip+7);
   bh = add_block('built-in/Outport',cat(2,path,'/y'),...
		  'Position',rect,...
		  'FontSize',10);
   inpport = get_param(bh,'InputPorts');
   add_line(path,[outport;inpport]);
   %
   % Set the location
   %
   rect = [0 0 250 (nch+3)*vskip];
   rect = min(rect,[0 0 600 800])+80;
   set_param(path,'Location',rect);
   return					   % Last card of BLDLOGOP
