{$C-}

CONST
  SELDSK = 08; SETDMA = 11;
  SETTRK = 09; RDSK   = 12;
  SETSEC = 10; WDSK   = 13;

TYPE
  Str2   = STRING [02];
  Str255 = STRING [255];

VAR
  Dma   : ARRAY [1..128] OF Byte;
  Buf   : ARRAY [0..1,0..3,1..128] OF Byte;
  Ar    : STRING [128];
  B,D,T,
  Td,Sd,
  S,Dc  : Byte;
  Key   : Char;
  Ok,
  Same  : Boolean;

FUNCTION Hex(N : Byte) : Str2;
CONST
  LETRAS   : STRING [6] = 'ABCDEF';
VAR
  Lista    : ARRAY [1..2] OF Byte;
  Res      : ARRAY [1..2] OF Char;
  Cont     : Byte;
  Pr       : STRING [1];
BEGIN
  Cont:=0;
  FillChar(Lista,2,0);
  REPEAT
    Cont:=Succ(Cont);
    Lista [Cont]:=N Mod 16;
    N:=N Div 16
  UNTIL N=0;
  FOR Cont:=1 TO 2 DO
    IF Lista [Cont] IN [0..9] THEN
      BEGIN
        Str(Lista [Cont],Pr);
        Res [Cont]:=Pr
      END
    ELSE
      Res [Cont]:=Copy(LETRAS,Lista [Cont]-9,1);
  Hex:=Res [2]+Res [1]
END;

PROCEDURE Mostra_Dma;
VAR
  A,B,Li : Byte;
BEGIN
  Mem [Addr(Ar)]:=128;
  Li:=0;
  FOR A:=1 TO 128 DO
    IF (Dma [A] AND 127) In [0..31,128..255] THEN
      Ar [A]:='.'
    ELSE
      Ar [A]:=Chr(Dma [A] AND 127);
  FOR A:=0 TO 7 DO
    BEGIN
      Li:=Li+1;
      GotoXY(1,Li);
      FOR B:=A*16+1 TO A*16+16 DO
        Write(Hex(Dma [B]):3);
      GotoXY(60,Li);
      Write(Copy(Ar,A*16+1,16))
    END;
END;

BEGIN
  ClrScr;
  Dc:=Bdos($19); Ok:=TRUE; Same:=TRUE;
  GotoXY(02,10); Write('Drive:');
  GotoXY(13,10); Write('Trilha:');
  GotoXY(24,10); Write('Setor:');
  Bios(SETDMA,Addr(Dma));
  T:=1;
  REPEAT
    B:=0;
    GotoXY(21,10); Write(T);
    Bios(SETTRK,T);
    REPEAT
      FOR D:=0 TO 1 DO
        BEGIN
          GotoXY(09,10); Write(D);
          Bios(SELDSK,D);
          FOR S:=B TO B+3 DO
            BEGIN
              GotoXY(31,10); Write(S:2);
              Bios(SETSEC,S);
              Bios(RDSK);
              Move(Dma,Buf[D,S-B],128)
            END
        END;
      FOR S:=B TO B+3 DO
        BEGIN
          D:=1;
          REPEAT
            IF Buf[0,S-B,D]<>Buf[1,S-B,D] THEN
              BEGIN
                Same:=FALSE; Ok:=FALSE; Sd:=S
              END;
            D:=D+1
          UNTIL (D=129) OR (NOT Ok)
        END;
      IF NOT Ok THEN
        BEGIN
          GotoXY(2,11);
          Write('Diskettes contem dados diferentes na trilha ',
                 T,' setor ',Sd);
          FOR D:=0 TO 1 DO
            BEGIN
              Bios(SELDSK,D); Bios(SETSEC,Sd);
              Bios(RDSK); Mostra_Dma
            END;
          GotoXY(2,12); Write('Continua comparacao (S/N) ?');
          REPEAT
            Read(Kbd,Key);
            Key:=UpCase(Key)
          UNTIL Key IN ['S','N'];
          GotoXY(2,11); Write(#27#126#107);
          Ok:=Key='S'
        END;
      B:=B+4
    UNTIL (B=40) OR (NOT Ok);
    T:=T+1
  UNTIL (T=3) OR (NOT Ok);
  IF Same THEN
    BEGIN
      GotoXY(2,11);
      Write('Diskettes contem os mesmos dados.')
    END;
  Bios(SELDSK,Dc)
END.