! TPLA :  a routine to generate an ILAP PLA from a table.

%include "nmos.inc"
%include "plautils.inc"
!%external %integer %fn %spec IN STREAM
!%external %routine %spec CLOSE INPUT

{#########################################################################}
{#                                                                       #}
{#      This program is part of the ILAP library, and was written in     #}
{#   The Department of Computer Science at the University of Edinburgh   #}
{#       (James Clerk Maxwell Building, Kings Buildings, Edinburgh)      #}
{#                                                                       #}
{#   This software is available free to other educational establisments  #}
{#   but the University of Edinburgh, retains all commercial rights.     #}
{#   It is a condition of having this software is that the sources are   #}
{#   not passed on to any other site, and that Edinburgh University is   #}
{#   given credit in any re-implementations of any of the algorithms     #}
{#   used, or articles published which refer to the software.            #}
{#                                                                       #}
{#   There is no formal support for this software, but any bugs should   #}
{#   be reported to Gordon Hughes or David Rees at the above address,    #}
{#   and these are likely to be fixed in a future release.               #}
{#                                                                       #}
{#########################################################################}

%external %routine stpla %alias "ILAP_STPLA" ( %string (31) symbol name,
   %string (255) file, %integer mode,
   %integer %array %name ispec,%integer %array %name ospec,
   %integer %name phi1y, phi2y,
   %integer %array %name inx,%integer %array %name outx)
  
   %byte %array table ( 1:maxnames, 1 : max prods )
   %string(7)%array names (1:maxnames)
   %integer %array feed back (1:maxnames)
   %integer oldin, oldout
   %integer ins, outs, feeds, prods
   %string (255) fin, fout
   %integer inn, outn

   %on 9 %start
       disaster ("STPLA: unexpected end of input")
   %finish
  
   ins = 0;   outs = 0;   feeds = 0
   oldin = in stream
   parse filespec ( file, "tbl" , "", fin, fout, inn, outn )
   select input (inn)
   get ilap table ( table, names, feedback, ins, outs, feeds, prods, "TPLA" )
   order table ( table, names, feedback, ins, outs, feeds, prods )
   %begin
      %integer %array and  (1:prods*(ins+feeds))
      %integer %array or   (1:(feeds+outs)*prods)
      %integer i,j,k
      k = 0
      %for j = 1,1,prods %cycle
         %for i = 1,1,ins+feeds %cycle
            k = k + 1
            and(k) = table(i,j)-1
         %repeat
      %repeat
      k = 0
      %for i = ins+feeds+1,1,ins+feeds+feeds+outs %cycle
         %for j = 1,1,prods %cycle
            k = k + 1
            table(i,j) = table(i,j)-1 %if table(i,j)#0
            or(k)=table(i,j)
         %repeat
      %repeat

      %if diags=true %start
          print string("TPLA: Version 4.1");   newline
          print string("and array :=");   newline
          %for j = 1,1,prods %cycle
               write(table(i,j)-1,3) %for i = 1,1,ins+feeds
               newline
          %repeat
          print string("or array :=");   newline
          %for i = ins+feeds+1,1,ins+feeds+feeds+outs %cycle
               write(table(i,j),3) %for j = 1,1,prods
               newline
          %repeat
      %finish
  
      %if mode&(stretchinputs!stretchoutputs!stretch)=0 %c
        %then npla(symbolname,mode,ispec,ospec,ins,outs,
                    feeds,prods,and,or,phi1y,phi2y,inx,outx) %c
      %else %start
        select output(0)
        print string("Warning: Stretched PLAs no longer available.
")
{        PLA(symbolname,mode,ispec,ospec,ins,outs,feeds,prods,and,or,
{                 phi1y,phi2y,inx,outx)
      %finish
   %end
   close input
   select input( oldin )
%end

%external %routine tpla %alias "ILAP_TPLA" (%string (31) name, %string (255) file,
   %integer %name phi1y, phi2y,
   %integer %array %name inx,
   %integer %array %name outx)
   %integer %array %name none {(1:*)}

   stpla (name, file, 0, none, none, phi1y, phi2y, inx, outx)
%end

%end %of %file
