program TestExtendedDisk;
{ define TestMem}        { nesmi mizet pamet }
{$define TestStruc}      { test na ExtSect -- lokalni promenna - RunTime, Error }
{$define TestCopy}       { test na kopirovani pameti -- nesmi byt ohlasena
                                   chyba pri READ }

uses
 NumToStr,
 Crt,
 DiskIO,
 Uexit
 ;

const
 cSectTst = 100;
 cSectFirstWr1  = 0;
 cSectFirstWr2  = 200;
 cSectLastWr1 = cSectFirstWr1+cSectTst;
 cSectLastWr2 = cSectFirstWr2+cSectTst;

const
 ErrString : array [1..11] of String[39] =
  ('Not enough memory',
   'A20 is set to HIGH',
   'Only for 386EX and higher CPU',
   'Sector number doesn''t exist',
   'Sector can''t be written',
   'Sector can''t be read',
   'Sector can''t be copied',
   'Unknown Flash manufactor',
   'Unknown Flash type',
   'Unknown disk type',
   'Invalid disk structure');

var
 aPoint:word;
 Pole:array [0..255] of Word;       { oblast hodnot, ktera je prenasena
                                      do virtualnich disku  - zapis}
 Pole1:array [0..255] of Word;      { oblast hodnot, ktera je prenasena
                                      z virtualnich disku   - cteni }
 aSect:LongInt;
 aSub:Word;
 aDisk:tDiskRecord;


begin
 ClrScr;

{$ifdef TestMem}
 repeat
  Writeln('Volna pamet ',MemAvail);
  DestroyDiskArea;
  InitDiskArea;
  Delay(500);
 until false;
{$endif}

{$ifdef Ver7}
 Writeln('Detekce procesoru ',Test8086);
{$endif}
 Writeln('Zacatek programu ');
 InitDiskArea;
{$ifdef Ver7}
 Writeln('Detekce procesoru ',Test8086);
{$endif}
 Writeln('Zacatek programu ');

{$ifdef TestStruc}
 Writeln('Test Disk Info');
 PrintDiskInfo;
 Writeln('Test is OK');
 repeat until false;
{$endif}


 GetDiskParam(@aDisk);
 if IOResultDiskIO<>DiskIO_Ok then
  begin
   Writeln('Chyba pri ziskavani parametru :  ',ErrString[IOResultDiskIO]);
   repeat until false;
  end;

 if IOResultDiskIO=DiskIO_OK then
  begin
   with aDisk.RAM do
    begin
     Writeln('RAM:  ');
     Writeln('........ First sector : ',FirstSector);
     Writeln('........ Sector count : ',SectorCount);
    end;
   with aDisk.ROM do
    begin
     Writeln('ROM:  ');
     Writeln('........ First sector : ',FirstSector);
     Writeln('........ Sector count : ',SectorCount);
    end;
   with aDisk.FLASH do
    begin
     Writeln('FLASH:  ');
     Writeln('........ First sector : ',FirstSector);
     Writeln('........ Sector count : ',SectorCount);
    end;
{$ifdef DOS}
   Delay(1000);
{$endif}
{$ifdef MCP}
   Delay(5000);
{$endif}
  end;

 WriteSector(@Pole,-1);    { test meznich podminek }
 if IOResultDiskIO<>DiskIO_OK then
  begin
   Writeln('Ok: Chyba pri zapisu <test> :  ',ErrString[IOResultDiskIO]);
   Delay(1000);
  end;
 WriteSector(@Pole,10000); { test meznich podminek }
 if IOResultDiskIO<>DiskIO_OK then
  begin
   Writeln('Ok: Chyba pri zapisu <test> :  ',ErrString[IOResultDiskIO]);
   Delay(1000);
  end;

 Delay(1000);
 aSect:=cSectFirstWr1;

 while aSect<cSectLastWr1 do
  begin
   aPoint:=0;
   while aPoint<512/2 do
    begin
     aSub:=Word((aSect*3*aPoint+aPoint) and $FFFF);
     Pole [aPoint]:=aSub;
     Pole1[aPoint]:=0;
     inc(aPoint,1);
    end;

   WriteSector(@Pole,aSect);
   Writeln('Stav zapisu : ',IOResultDiskIO);
   if IOResultDiskIO<>DiskIO_OK then
    begin
     Writeln('Sektor ',aSect);
     Writeln('........ Chyba pri zapisu ',ErrString[IOResultDiskIO]);
     Writeln('........ Chyba pri zapisu ',ErrString[IOResultDiskIO]);
     Writeln('........ Chyba pri zapisu ',ErrString[IOResultDiskIO]);
     Writeln('........ Chyba pri zapisu ',ErrString[IOResultDiskIO]);
     Writeln('........ Chyba pri zapisu ',ErrString[IOResultDiskIO]);
{$ifdef MCP}
       repeat until false;
{$endif}
{$ifdef DOS}
       Exit;
{$endif}
    end;

   Inc(aSect);
  end;

 aSect:=cSectFirstWr2;

 while aSect<cSectLastWr2 do
  begin
   aPoint:=0;
   while aPoint<512/2 do
    begin
     aSub:=Word((aSect*3*aPoint+aPoint) and $FFFF);
     Pole [aPoint]:=aSub;
     Pole1[aPoint]:=0;
     inc(aPoint,1);
    end;

   WriteSector(@Pole,aSect);
   Writeln('Stav zapisu : ',IOResultDiskIO);
   if IOResultDiskIO<>DiskIO_OK then
    begin
     Writeln('Sektor ',aSect);
     Writeln('........ Chyba pri zapisu ',ErrString[IOResultDiskIO]);
     Writeln('........ Chyba pri zapisu ',ErrString[IOResultDiskIO]);
     Writeln('........ Chyba pri zapisu ',ErrString[IOResultDiskIO]);
     Writeln('........ Chyba pri zapisu ',ErrString[IOResultDiskIO]);
     Writeln('........ Chyba pri zapisu ',ErrString[IOResultDiskIO]);
{$ifdef MCP}
       repeat until false;
{$endif}
{$ifdef DOS}
       Exit;
{$endif}
    end;

   Inc(aSect);
  end;


{$ifdef TestCopy}
 CopySector(0,300);
 CopySector(1,301);
{$endif}
 CopySector(235,0);
 CopySector(200,1);
{$ifdef TestCopy}
 CopySector(300,0);
 CopySector(301,1);
{$endif}


 aSect:=cSectFirstWr1;

 while aSect<cSectLastWr1 do
  begin

   ReadSector(aSect,@Pole1);
   Writeln('Stav cteni : ',IOResultDiskIO);
   if IOResultDiskIO<>DiskIO_OK then
    begin
     Writeln('Sektor ',aSect);
     Writeln('........ Chyba pri cteni ',ErrString[IOResultDiskIO]);
     Writeln('........ Chyba pri cteni ',ErrString[IOResultDiskIO]);
     Writeln('........ Chyba pri cteni ',ErrString[IOResultDiskIO]);
     Writeln('........ Chyba pri cteni ',ErrString[IOResultDiskIO]);
     Writeln('........ Chyba pri cteni ',ErrString[IOResultDiskIO]);
     repeat until false;
    end;

   aPoint:=0;
   while aPoint<512/2 do
    begin
     aSub:=Word((aSect*3*aPoint+aPoint) and $FFFF);
     if Pole1[aPoint]<>aSub then
      begin
       Writeln('Sektor ',aSect);
       Writeln('Pole1   :  ',WordStrHex(Pole1[aPoint])  ,' x ',' Kontrola :  ',WordStrHex(aSub));
       Writeln('Pole1 +1:  ',WordStrHex(Pole1[aPoint+1]),' x ',' Kontrola :  ',WordStrHex(aSub+1));
       Writeln('Pole1 +2:  ',WordStrHex(Pole1[aPoint+2]),' x ',' Kontrola :  ',WordStrHex(aSub+2));
       Writeln('........ Chyba pri kontrole dat: ',aPoint);
       Writeln('........ Chyba pri kontrole dat: ',aPoint);
       Writeln('........ Chyba pri kontrole dat: ',aPoint);
       Writeln('........ Chyba pri kontrole dat: ',aPoint);
       Writeln('........ Chyba pri kontrole dat: ',aPoint);
{$ifdef MCP}
       repeat until false;
{$endif}
{$ifdef DOS}
       Exit;
{$endif}
      end;
     inc(aPoint,1);
    end;
   Inc(aSect);
  end;
 Writeln('Konec programu : ',IOResultDiskIO);
{$ifdef MCP}
 repeat
 until false;
{$endif}
end.