(*
*******************************************************************************
*         Copyright (C) SofCon 2002 All Rights Reserved                       *
*******************************************************************************
*
*    project:     LOADER patch
*
*    title:       LdrPatch
*
*******************************************************************************
*
*    description: program pro nahrani nove verze LOADER
*
*    version:     1.05
*    language:    Borland Pascal
*
*    author:      Hvozdovic
*    history:     date      sign   remark
*                11.02      hv     zacatek vyvoje
*                11.02      hv     prvni release
*                12.02      hv     doplneni nastaveni pro LOADER s UDP
*		  3.03      hv     uprava konstanty cVer do stand. formatu
*				   pridani konstanty cVerNo obsahujici ciselne
*					verzi knihovny
*				   zruseni konstanty cName
*				   pridani konstanty cSwId
*                 4.03      hv     pridana direktiva pro LOADER bez VGA
*                 1.04      hv     pridano osetreni chyby identifikace ATMEL Flash
*****************************************************************************
*)
{$I sets.inc}

program LdrPatch;
uses
   AtmFlash           { prace s pameti FLASH }
 {$ifdef Deb_crt}
  ,Crt                { zobrazovaci jednotka }
 {$endif}
{ pokud pridate tuto jednotku, budete umet zachytavat chyby,
  ktere nastanou pri behu teto jednotky, ale musite vzdy
  nastavit veskere adresy dle pozadavku Vasi aplikace
  ,AppDesc            { jednotka se sdilenymi strukturami }
  ,HwSyst             { deklarace konstant ridiciho systemu KIT }
  ;

const
  cSwId  = '123456';     { 6 znaku jednoznacne specifikujicich aplikaci }
  cVerNo = $0105;        { BCD format }
  cVer   = '01.05,21.01.2004';     { verze jednotky }

const
  { +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ }
  { +++++++++++ konstanty musi odpovidat nastaveni v programu RTD +++++++++ }
  { +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ }
  { umisteni programu LdrPatch dle nastaveni programu RTD
    a souboru LdrFinal.mem }
  { pokud bude soubor umisten v rozsahu $E0000-$FD000 pripadne $E0000-$FE000,
    je nova verze LOADER do ridiciho systemu zapsana vzdy,
    bezohledu na vypadky systemu }
  { pripadne staci --LOADER-- jako to je v pripade UDP }
  laLdrPatch    = $E0000;
 {$ifdef UDP}
  { umisteni nove verze Loader dle nastaveni MEM }
  laLdrUnit     = $80000;
 {$else}
  { umisteni nove verze Loader dle nastaveni MEM }
  laLdrUnit     = $E3000;
 {$endif}
  { umisteni a velikost nove verze LOADER dle nastaveni programu RTD }
  { nastaveni pro Kit386EXR a LOADER bez VGA }
 {$ifdef UDP}
  laLdr         = $E3000;
  cLdrSize      = $1A000;
 {$else}
  {$ifdef LDR_DebCrt}
   laLdr         = $F0200;
   cLdrSize      = $0CE00;
  {$else}
   laLdr         = $F1200;
   cLdrSize      = $0BE00;
  {$endif}
 {$endif}
  wFlashManDev     : Word      = $A41F; { AT29C040A       }
  wFlashLenSect    : Word      = 512;   { 16b - AT29C040A }

var
  laFlashBase:LongInt;     { linearni bazova adresa umisteni pameti FLASH }
  ManDev, SectSize:Word;   { parametry pameti FLASH }
  SaveExit:Pointer;        { ukazatel na ExitProc }

{$S-}
{ osetreni chyby a ukonceni programu }
procedure MyExitProc; far;
begin
  ExitProc:=SaveExit;

 {$ifdef Deb_crt}
  if (ExitCode<>0) then
  begin
    writeln('LdrPatch - code: ', ExitCode, ' -- addr: ',
                   LongInt(ErrorAddr) shr 16, ':',
                   LongInt(ErrorAddr) and $FFFF);
    Delay(3000);
  end;
 {$endif}

{$ifdef MCP}
  asm cli end;
  repeat until false;
{$else}
  halt(ExitCode);
{$endif}
end;
{$S+}

{ hlavni telo programu LdrPatch }
Begin
  SaveExit:=ExitProc;
  ExitProc:=@MyExitProc;

 {$ifdef Deb_Crt}
  ClrScr;
  Writeln('Spusten program: ', cSwId, ' - ', cVer, '.');
 {$endif}

  { nastaveni bazove adresy }
  laFlashBase:=GetBaseAddr;
{ nebo lze adresu zadat absolutne
  laFlashBase:=$80000; }

  { kontrola FLASH - pametovy podsystem }
  if not AtmIdent(laFlashBase, ManDev, SectSize) then
  begin
   {$ifdef Deb_crt}
    Writeln('FLASH type info error - default');
   {$endif}

    cLenSect:=wFlashLenSect;
    cManDev :=wFlashManDev;
  end;

 {$ifdef Deb_crt}
  Write('FlashType: ');
  case Hi(ManDev) of
    $D5 : Writeln('AT29C010');
    $DA : Writeln('AT29C020');
    $5B : Writeln('AT29C040');
    $A4 : Writeln('AT29C040A');
    else
      Writeln('Unknown');
  end;
 {$endif} {Deb_crt}


  { zapis nove verze LOADER }
 {$ifdef Deb_crt}
  Write('Zapis do pameti Flash .....');
 {$endif}
  if not AtmWrite(laFlashBase, laLdrUnit, laLdr, cLdrSize) then
  begin
   {$ifdef Deb_crt}
    Writeln('AtmWrite error');
   {$endif}
   {$ifdef VerMC}
    RunError(255);
   {$endif}
  end;
 {$ifdef Deb_crt}
  Writeln('hotovo');
 {$endif}

  { poskozeni programu LdrPatch }
 {$ifdef Deb_crt}
  Write('Zruseni LdrPatch .....');
 {$endif}
  if not AtmTotalDestruct(laFlashBase, laLdrPatch) then
  begin
   {$ifdef Deb_crt}
    Writeln('LdrPatch destruct error');
   {$endif}
   {$ifdef VerMC}
    RunError(255);
   {$endif}
  end;
 {$ifdef Deb_crt}
  Writeln('hotovo');
 {$endif}

 {$ifdef Deb_crt}
  Writeln('Program ukoncen. Nasleduje RESET systemu - WD.');
 {$endif}
End.
