(*
*******************************************************************************
*         Copyright (C) SofCon 2003 All Rights Reserved                       *
*******************************************************************************
*
*    project:     Aplikace zjistujici HW konfiguraci KIT
*
*    title:       HwDetect
*
*******************************************************************************
*
*
*    description: cely program
*
* TODO: komunikacni rozhrani pomoci KernelMonitoru
*
*    version:     1.00
*    language:    Borland Pascal
*
*    author:      Jan Hvozdovic
*    history:     date      sign   remark
*                12.03      hv     zacatek vyvoje
*******************************************************************************
*)
{$define Kit386}
{ define KitV40}
{$define KERN_MON}
program HwDetect;
{$I sets.inc}

uses
   _1stUnit           { !! jednotka musi byt prvni }

  ,xDesc              { definice konstant, typu a funkci sdilenych
                        s uzivatelskou aplikaci }
  ,uxDesc             { uzivatelske nastaveni promennych }
  ,HwSyst
  ,NumToStr
  ,AtmFlash           { !@# - upravit AtmFlash pro cinnost v DOS prostredi
                        prace s pameti FLASH }
  ,DiskIO
  ,XtKing_1
{$ifdef KitV40}
  ,ChnV40
{$endif}
{$ifdef Kit386}
  ,ChnCOM
{$endif}
{$ifdef KERN_MON}
  ,RemoTerm
  ,KernMon
  ,Kernel
{$endif}
  ,Crt
  ;


const
  cSwId  = '123456';     { 6 znaku jednoznacne specifikujicich aplikaci }
  cVerNo = $0100;        { BCD format }
  cVer   = '01.00,21.01.2004';     { verze jednotky }
{$ifdef KERN_MON}
  SourceNode = '254';
  DestNode   = '255';
{$endif}


var
  b:Byte;
  FO:Text;
  SaveExit:Pointer;
  BiosIdStr:String;
  DiskIODesc:tDiskRecord;
  ManDev,SectSize:Word;



procedure ShowMemType(abMemType:Byte);
begin
  case abMemType of
   hwsysReserved: write(FO, 'RESERVED');
   hwsysRAM:      write(FO, 'RAM');
   hwsysFLASH:    write(FO, 'FLASH');
   hwsysROM:      write(FO, 'ROM');
  else
    write(FO, 'UNKNOWN - osetrit');
  end;
end;


{$S-}
procedure MyExit; far;
begin
{$ifdef VerMc}
  asm cli end;
{$endif}
  ExitProc:=SaveExit;
  if ExitCode<>0 then
  begin
    WriteLn('ExitCode:=',ExitCode,' ErrAddr:='+
            WordStrHex(seg(ErrorAddr^))+':'+WordStrHex(ofs(ErrorAddr^)));
  end;
 {$ifdef VerMC}
  repeat until FALSE;
 {$endif}
end;
{$S+}


{ hlavni telo programu pro zjisteni HW konfiguraci }
begin
  SaveExit:=ExitProc;
  ExitProc:=@MyExit;


 {$ifdef DOS}
  Assign(FO, 'Output.txt');
{
  ClrScr;
  AssignCrt(FO);
 }
 {$else}
  ClrScr;
  AssignCrt(FO);
 {$endif}

 {$ifdef KERN_MON}
  {prirazeni vystupu procesu do okna TerminalOut Retosdebuggeru}
  AssignRemote(FO);  {prirazeni RTD jako vzdaleny terminal output}

  { zapnuti cekani na dokonceni operace }
  {$ifdef MCP}
   RemoTerm.gfWaitForOutputRelease:=TRUE;
  {$endif}
 {$endif}

  Rewrite(FO);

 {$ifdef KERN_MON}
  TextMode(CO80+Font8x8);

  StartMain(250,254);
  InitInterruptStack(1, 1048);
  StartTimeSlicing($8);

  {$ifdef KitV40}
   KernelMonitor('NAM=PRT LSB=5000 NOD='+SourceNode+' DNO='+DestNode+'|'+
                 'NAM=V40 BD=4800 BIT=8 PAR=ODD STOP=2 LRB=1000',false);
  {$endif}
  {$ifdef Kit386}
   {$ifdef VerPC}
    KernelMonitor('NAM=PRT LSB=5000 NOD='+SourceNode+' DNO='+DestNode+'|'+
                  'NAM=COM COM=2 IRQ=3 BD=4800 BIT=8 PAR=ODD STOP=2 LRB=1000',false);
   {$else}
    KernelMonitor('NAM=PRT LSB=5000 NOD='+SourceNode+' DNO='+DestNode+'|'+
                  'NAM=COM COM=1 IRQ=4 BD=4800 BIT=8 PAR=ODD STOP=2 LRB=1000',false);
   {$endif}
  {$endif}
 {$endif}

  Writeln('Spusteni programu');

  writeln(FO, 'Spusteni programu pro detekci HW');
  writeln(FO, '********************************');

  { prizpusobeni aplikaci - sdilena funkce s LOADER, APP }
  AdaptToMyApp;

  wFlashLenSect:=512;   { 16b, AT29C040A }

 {$ifdef MCP} {!@#}
  { inicializace AtmFlash }
  if not AtmIdent(laFlashBase, ManDev, SectSize) then
  begin
    Writeln(FO, 'FLASH type info error - user setting');

    { nastaveni pameti FLASH - podle uzivatele }
    cLenSect:=wFlashLenSect;
    cManDev :=wFlashManDev;
  end;
 {$endif}

  writeln(FO);
  write  (FO, 'PROCESOR:= ');
  case Test8086 of
    0: writeln(FO, '8086');
    1: writeln(FO, '80286');
    2: writeln(FO, '80386 nebo vyssi');
  end;

  writeln(FO);
  writeln(FO, 'SystIdent            := ', LongIntStrHex(SystIdent));
  writeln(FO, 'ModuleTable BaseAddr := ', LongintStrHex(laModuleTable));
  writeln(FO, 'ModuleTable Size     := ', laLoaderStart-laModuleTable);
  writeln(FO, 'ModuleTable Residuum := ', WordStrHex(cLdrResidue));
  writeln(FO, 'FLASH sector size    := ', AtmFlash.cLenSect);

  GetBiosIdStr(BiosIdStr, sizeof(tBiosInfoStr));
  writeln(FO, 'GetBIOSId := ', BiosIdStr);

  writeln(FO);
  writeln(FO, 'LA RomBaseAddr (FLASH) := ', LongIntStrHex(GetBaseAddr));
  writeln(FO);
  { PAMETOVY PROSTOR KIT }
  writeln(FO, 'MEMORY SPACE');
  case SystIdent of
    SystKitV40_8,
    SystKitV40_16,
    SystJT283_8,
    SystTERM03_8,
    SystTERM03_16,
    SystTERM10_8,
    SystTERM10_16:
      begin
        write(FO, '--- TYPE, ');
              ShowMemType(GetCSType(1));
              writeln(FO);
        writeln(FO, '--- BASE, LA:$', LongIntStrHex(GetCSAddr(1)));
        writeln(FO, '--- SIZE: ', MemDiskKByteSize[GetCSSize(1)], 'kB');

        write(FO, '--- TYPE, ');
              ShowMemType(GetCSType(2));
              writeln(FO);
        writeln(FO, '--- BASE, LA:$', LongIntStrHex(GetCSAddr(2)));
        writeln(FO, '--- SIZE: ', MemDiskKByteSize[GetCSSize(2)], 'kB');

        writeln(FO, '--- TYPE, RESERVED (VGA RAM)');
        writeln(FO, '--- BASE, LA:$A0000');
        writeln(FO, '--- SIZE, $20000)');

        writeln(FO, '--- TYPE, RESERVED (VGA BIOS)');
        writeln(FO, '--- BASE, LA:$C0000');
        writeln(FO, '--- SIZE, $7FFF)');

        writeln(FO, '--- TYPE, RESERVED (BIOSmin)');
        writeln(FO, '--- BASE, LA:$', LongIntStrHex(cBIOSBeginAddr));
        writeln(FO, '--- SIZE, $', LongIntStrHex($FFFFF-cBIOSBeginAddr));
      end;
    SystKit386exR_25,
    SystKit386exR_33:
      begin
        for b:=1 to 4 do
        begin
          write(FO, '--- TYPE, ');
                ShowMemType(GetCSType(b));
                writeln(FO);
          writeln(FO, '--- BASE, LA:$', LongIntStrHex(GetCSAddr(b)));
          writeln(FO, '--- SIZE: ', MemDiskKByteSize[GetCSSize(b)], 'kB');
        end;
      end;
    else
      begin
        writeln(FO, 'DOS nebo neznamy board');
        for b:=1 to 4 do
        begin
          write(FO, '--- TYPE, ');
                ShowMemType(GetCSType(b));
                writeln(FO);
          writeln(FO, '--- BASE, LA:$', LongIntStrHex(GetCSAddr(b)));
          writeln(FO, '--- SIZE: ', MemDiskKByteSize[GetCSSize(b)], 'kB');
        end;
      end;
  end;
  writeln(FO);


  { Parametry osazenych pameti }
  writeln(FO, 'Parametry osazenych pameti');
  case SystIdent of
    SystKitV40_8,
    SystKitV40_16,
    SystJT283_8,
    SystTERM03_8,
    SystTERM03_16,
    SystTERM10_8,
    SystTERM10_16:
      begin
        writeln(FO, '--- RAM - dle BIOS - musi se dopocitat');
        writeln(FO, '--- ROM - dle BIOS - musi se dopocitat');
      end;
    SystKit386exR_25,
    SystKit386exR_33:
      begin
        for b:=1 to 4 do
        begin
          write(FO, '--- TYPE, ');
                ShowMemType(GetMemoryType(b));
                writeln(FO);
          writeln(FO, '--- Oper. Memory Size: ', MemSizeKB[GetOpMemory(b)], 'kB');
          writeln(FO, '--- Chip Size:         ', MemSizeKB[GetChipSize(b)], 'kB');
          writeln(FO, '--- AccessTime:        ', GetAccessTime(b), 'ns' );
        end;
      end;
    else
      begin
        writeln(FO, 'DOS nebo neznamy board');
      end;
  end;
  writeln(FO);


  { ROZSIRENE DISKY }
  if Test8086=2 then
  begin
    { inicializace rozsirenych disku }
    InitDiskArea;
    { zjisteni parametru DiskIO }
    GetDiskParam(@DiskIODesc);

    { nastaveni popisovacu knihoven DiskIO }
    writeln(FO, 'DiskIO.RAM ');
    with DiskIODesc.RAM do
    begin
      writeln(Fo, ' --- SectorCount := ', SectorCount);
      writeln(FO, ' --- MemType     := cMEMtype_RAM');
      writeln(FO, ' --- FirstSector := ', FirstSector);
      writeln(FO, ' --- LastSector  := ', FirstSector+SectorCount-1);
      writeln(FO, ' --- SectorCount := ', SectorCount);
      writeln(FO, ' --- SectorSize  := ', cSectorSize);
    end;

    writeln(FO, 'DiskIO.FLASH ');
    with DiskIODesc.FLASH do
    begin
      writeln(Fo, ' --- SectorCount := ', SectorCount);
      writeln(FO, ' --- MemType     := cMEMtype_FLASH');
      writeln(FO, ' --- FirstSector := ', FirstSector);
      writeln(FO, ' --- LastSector  := ', FirstSector+SectorCount-1);
      writeln(FO, ' --- SectorCount := ', SectorCount);
      writeln(FO, ' --- SectorSize  := ', cSectorSize);
    end;

    writeln(FO, 'DiskIO.ROM ');
    with DiskIODesc.ROM do
    begin
      writeln(Fo, ' --- SectorCount := ', SectorCount);
      writeln(FO, ' --- MemType     := cMEMtype_ROM');
      writeln(FO, ' --- FirstSector := ', FirstSector);
      writeln(FO, ' --- LastSector  := ', FirstSector+SectorCount-1);
      writeln(FO, ' --- SectorCount := ', SectorCount);
      writeln(FO, ' --- SectorSize  := ', cSectorSize);
    end;
  end;

  Writeln(FO, 'Ukonceni programu');
  Flush(FO);
  Close(FO);


  writeln('Konec programu');
 {$ifdef MCP}
  repeat
  until FALSE;
 {$else}
  repeat
    Wait(1);
  until not Keypressed;
 {$endif}
End.