! DATCOMP:
! Compare two BEPI .DAT files to ensure they describe the same
! circuit, although the description may be permuted both within
! and between nets.
! RWT Aug 86

%include "inc:util.imp"
%begin
%string(255)file1,file2
%integer rows=99,cols=99,nets=999
  define param("File1",file1,pamnodefault)
  define param("File2",file2,pamnodefault)
  define intparam("Rowmax",rows,0)
  define intparam("Colmax",cols,0)
  define intparam("Netmax",nets,0)
  process parameters(cliparam)
%begin
%shortarray p1,p2(1:rows,1:cols)
%shortarray n1,n2(1:nets)
%short pp1,pp2
%integer r,c,i,net

%routine readin(%string(255)%name s,%shortarray(2)%name p)
%short nn,r,c,k
%shortname pp
  %on 9 %start
    closeinput; %return
  %finish
  openinput(1,s.".dat"); selectinput(1)
  readsymbol(k) %and printsymbol(k) %until k=nl
  %cycle
    read(nn;c;r); readsymbol(k) %until k=nl
    %signal 9 %if nn!c!r=0
    nn = |nn|
    pp == p(r,c)
    %if pp=0 %start
      pp = nn
    %else
      printstring("Duplicate nets")
      write(pp,3); printstring(" and"); write(nn,3)
      printstring(" at row"); write(r,2)
      printstring(" column"); write(c,2)
      newline
    %finish
  %repeat
%end

  %for c = 1,1,cols %cycle
    %for r = 1,1,rows %cycle
      p1(r,c) = 0; p2(r,c) = 0
    %repeat
  %repeat
  %for i = 1,1,nets %cycle
    n1(i) = 0; n2(i) = 0
  %repeat
  readin(file1,p1)
  readin(file2,p2)
  printstring("Checking"); newline
  %for c = 1,1,cols %cycle
    %for r = 1,1,rows %cycle
      pp1 = p1(r,c); pp2 = p2(r,c)
      %if pp1#pp2 %start
        %if pp1=0 %or pp2=0 %start
          printstring("Row"); write(r,2)
          printstring(" column"); write(c,2)
          printstring(" missed in ")
          %if pp1=0 %then printstring(file1) %else printstring(file2); newline
        %finish
      %finish
      %if pp1#0 %and pp2#0 %start
        %if n1(pp1)=0 %start
          n1(pp1) = pp2
        %elseunless n1(pp1)=pp2
          printstring("Bad mapping 1->2 at row"); write(r,2)
          printstring(" column"); write(c,2)
          printstring(" nets "); write(pp1,3); write(pp2,3); newline
        %finish
        %if n2(pp2)=0 %start
          n2(pp2) = pp1
        %elseunless n2(pp2)=pp1
          printstring("Bad mapping 2->1 at row"); write(r,2)
          printstring(" column"); write(c,2)
          printstring(" nets "); write(pp1,3); write(pp2,3); newline
        %finish
      %finish
    %repeat
  %repeat
  %for i = 1,1,nets %cycle
    %unless n1(i)=0 %and n2(i)=0 %start
      %unless n1(n2(i))=i %and n2(n1(i))=i %start
        printstring("Bad net mapping")
        write(i,3; n1(i),3; n2(n1(i)),3; n2(i),3; n1(n2(i)),3); newline
      %finish
    %finish
  %repeat
%end
%end
