2008년 4월 11일 금요일

dpcutil for delphi [Source code included]

This program is pascal version for Nexys 2 FPGA Board


 


Memory Transfer [ PC <-> Memory, Flash ]


To use this program , You must download and install Digilent Acept Suite


 







  

  • http://www.digilentinc.com
  • Xilinx Spartan-3E FPGA, 500K or 1200K gate
  • USB2 port providing board power, device configuration, and high-speed data transfers
  • Works with ISE/Webpack and EDK
  • 16MB fast Micron PSDRAM
  • 16MB Intel StrataFlash Flash R
  • Xilinx Platform Flash ROM
  • High-efficiency switching power supplies (good for battery-powered applications
  • 50MHz oscillator, plus a socket for a second oscillator
  • 59 FPGA I/O’s routed to expansion connectors (one high-speed Hirose FX2 connector with 21 differential pairs and four 2x6-pin headers)
  • All I/O signals are ESD and short-circuit protected, ensuring a long operating life in any environment.
  • On-board I/O includes eight LEDs, four-digit seven-segment display, four pushbuttons, eight slide switches
  • Ships in a DVD case with a high-speed USB2 cable


 


dpcutil.pas







//------------------------------------------------------------------------------
//
// dpcutil.dll Interface Code
// ----------------------------------------------------------------------------
//
// Original Code : http://www.digilentinc.com/
//                 dpcdefs.h
//                 dpcutil.h
//                 dpcutil.lib
//
//                 dpcuitl.dll [Ver 1.9.0.0]
//
// Delphi Code   : Translated and Modified
//                 Simon, Choi
//                 http://www.kredix.com , http://www.maxpaper.com
//                 simonsayz@naver.com
//               
//
// History       : 2008.04.09 1st Translation [Ver 0.1]
//
//
// Known Bugs    : dpcutil.dll [Ver 1.7] : Error in Data Transfer
//
//------------------------------------------------------------------------------


Unit DpcUtil;


interface


Uses
 Windows;


Type
 TTrID    = Word;    // Transaction ID type
 PTrID    = ^TTrID;  //
 TErc     = Integer; // Error code type
 TTrt     = Integer; // Transaction type
 TSts     = Integer; // Transaction status type
 TIfd     = Integer; // Interface ID
 TIfp     = LongInt;
 TDvct    = Integer; // Interface device type


 // Transaction status structure,
 TTrs     = Packed Record  // typedef struct tagTRS {
             trt  : TTrt;  // transaction type
             TrID : TTrID; // transaction ID
             sts  : TSts;  // status of transaction
             erc  : TErc;  // error code for transaction
            end;
 TDpcMsg = (dmTrc,   // Transaction Type   Message
            dmDvct,  // Device Type        Message
            dmErc,   // Error Code         Message
            dmSts);  // Transaction Status Message


Const
  cchDevNameMax       = 32;
  cbBlockSize         = 1000;
  // Transaction type codes
  trtOpenJtag         : TTrt  = $01;
  trtCloseJtag        : TTrt  = $02;
  trtSetTmsTdiTck     : TTrt  = $03;
  trtPutTdiBits       : TTrt  = $04;
  trtPutTmsTdiBits    : TTrt  = $05;
  trtGetTdoBits       : TTrt  = $06;
  trtSetJtsel         : TTrt  = $07;
  trtOpenSpi          : TTrt  = $21;
  trtCloseSpi         : TTrt  = $22;
  trtEnableSpi        : TTrt  = $23;
  trtDisableSpi       : TTrt  = $24;
  trtSetSpiSelect     : TTrt  = $25;
  trtSetSpiMode       : TTrt  = $26;
  trtPutSpiByte       : TTrt  = $27;
  trtPutSpi           : TTrt  = $28;
  trtGetSpi           : TTrt  = $29;
  trtOpenData         : TTrt  = $81;
  trtCloseData        : TTrt  = $82;
  trtSetDataPins      : TTrt  = $83;
  trtGetDataPins      : TTrt  = $84;
  trtSendDataByte     : TTrt  = $85;
  trtGetDataByte      : TTrt  = $86;
  trtSendDataBytes    : TTrt  = $87;
  trtGetDataBytes     : TTrt  = $88;
  trtSendDataStream   : TTrt  = $89;
  trtGetDataStream    : TTrt  = $8a;
  trtSendGetDataBytes : TTrt  = $8b;
  trtGetVersion       : TTrt  = $8c;
  trtStreamWrite      : TTrt  = $8d;
  trtStreamRead       : TTrt  = $8e;


  dvctEthernet        : TDVCT = $00;
  dvctUSB             : TDVCT = $01;
  dvctSerial          : TDVCT = $02;
  dvctParallel        : TDVCT = $03;


  // Error codes
  ercNoError          : TErc = 0000;
  ercConnReject       : TErc = 3001;
  ercConnType         : TErc = 3002;
  ercConnNoMode       : TErc = 3003;
  ercInvParam         : TErc = 3004;
  ercInvCmd           : TErc = 3005;
  ercUnknown          : TErc = 3006;
  ercJtagConflict     : TErc = 3007;
  ercNotImp           : TErc = 3008;
  ercNoMem            : TErc = 3009;
  ercTimeout          : TErc = 3010;
  ercConflict         : TErc = 3011;
  ercBadPacket        : TErc = 3012;
  ercInvOption        : TErc = 3013;
  ercAlreadyCon       : TErc = 3014;
  ercConnected        : TErc = 3101;
  ercNotInit          : TErc = 3102;
  ercCantConnect      : TErc = 3103;
  ercAlreadyConnect   : TErc = 3104;
  ercSendError        : TErc = 3105;
  ercRcvError         : TErc = 3106;
  ercAbort            : TErc = 3107;
  ercTimeOutEx        : TErc = 3108; // ??
  ercOutOfOrder       : TErc = 3109;
  ercExtraData        : TErc = 3110;
  ercMissingData      : TErc = 3111;
  ercTrIDNotFound     : TErc = 3201;
  ercNotComplete      : TErc = 3202;
  ercNotConnected     : TErc = 3203;
  ercWrongMode        : TErc = 3204;
  ercWrongVersion     : TErc = 3205;
  ercDvctableDne      : TErc = 3301;
  ercDvctableCorrupt  : TErc = 3302;
  ercDvcDne           : TErc = 3303;
  ercDpcutilInitFail  : TErc = 3304;
  ercUnknownErr       : TErc = 3305;
  ercDvcTableOpen     : TErc = 3306;
  ercRegError         : TErc = 3307;
  ercNotifyRegFull    : TErc = 3308;
  ercNotifyNotFound   : TErc = 3309;
  ercOldDriverNewFw   : TErc = 3310;
  ercInvHandle        : TErc = 3311;


  // Transaction status codes
  stsNew              : TSts = 1;
  stsComplete         : TSts = 2;


Const
 DPCModule = 'dpcutil.dll';


Var
 gDPCLoaded  : Boolean;
 gDPCHandle  : THandle;                             // Global Variable
 gDPCDevName : Array[0..cchDevNameMax-1] of Char;   // Global Variable
 gDPCDrvVer  : Array[0..cchDevNameMax-1] of Char;   // Global Variable


 // Function Description for Dynamic Loading
 DpcInit          : Function (Var Erc : TErc) : Boolean; cdecl;
 DpcTerm          : Procedure ; cdecl;


 DpcGetDpcVersion : Function (szVersion : PChar; Var Erc : TErc) : Boolean; cdecl;
 DpcStartNotify   : Function (hwndTemp : THandle; idNotifyTemp : Word; Var Erc : TErc) : Boolean; cdecl;
 DpcEndNotify     : Function (hwndTemp : THandle; Var Erc : TErc) : Boolean; cdecl;


 DpcGetVersion    : Function (hif : THandle; rgbVersion : PByte; cbVersion : Integer;
                              Var Erc : TErc; Var TrID : TTrID) : Boolean; cdecl;


 DpcWaitForTransaction     : Function (hif : THandle; TrID_  : TTrID; Var Erc : TErc) : Boolean; cdecl;
 DpcPendingTransactions    : Function (hif : THandle; pctran : PInteger; Var Erc : TErc) : Boolean; cdecl;
 DpcQueryConfigStatus      : Function (hif : THandle; TrID_  : TTrID; Var Trs : TTrs ;  Var Erc : TErc) : Boolean; cdecl;
 DpcAbortConfigTransaction : Function (hif : THandle; TrID_  : TTrID; Var Erc : TErc) : Boolean; cdecl;
 DpcClearConfigStatus      : Function (hif : THandle; TrID_  : TTrID; Var Erc : TErc) : Boolean; cdecl;


 DpcGetFirstError          : Function (hif : THandle) : TErc; cdecl;


 DpcOpenJtag               : Function(Var phif: THandle; szdvc : PChar; Var Erc : TErc; Var TrID : TTrID)  : Boolean; cdecl;
 DpcCloseJtag              : Function(hif : THandle; Var Erc : TErc )  : Boolean; cdecl;
 DpcEnableJtag             : Function(hif : THandle; Var Erc : TErc; Var TrID : TTrID) : Boolean; cdecl;
 DpcDisableJtag            : Function(hif : THandle; Var Erc : TErc; Var TrID : TTrID) : Boolean; cdecl;


 DpcSetTmsTdiTck  : Function (hif : THANDLE; fTms,fTDi,fTck : Boolean; Var Erc: TErc; Var TrID : TTrID) : Boolean; cdecl;
 DpcPutTdiBits    : Function (hif : THANDLE; cbit : Integer; rgbSnd : PChar; bitTms : Boolean;
                              fReturnTdo : Boolean; rgbRcv : PChar; Var Erc : TErc; Var TrID : TTrID) : Boolean; cdecl;


 DpcPutTmsTdiBits : Function (hif : THandle; cbit : Integer; rgbSnd : PChar;
                              fReturnTdo : Boolean; rgbRcv : PChar; Var Erc : TErc; Var TrID : TTrID) : Boolean; cdecl;


 DpcGetTdoBits    : Function (hif : THandle; cbits : Integer; bitTdi : Boolean; bitTms : Boolean;
                              rgbRcv : PChar; Var Erc : TErc; Var TrID : TTrID) : Boolean; cdecl;
 DpcOpenData      : Function (Var phif : THandle; szdvc : PChar; Var Erc : TErc; pTrID_ : pTrID) : Boolean; cdecl;
 DpcCloseData     : Function (hif : THandle; Var Erc : TErc) : Boolean; cdecl;
 DpcPutReg        : Function (hif : THandle; bAddr : Byte;     Data : Byte; Var Erc : TErc; pTrID_ : pTrID) : Boolean; cdecl;
 DpcGetReg        : Function (hif : THandle; bAddr : Byte; Var Data : Byte; Var Erc : TErc; pTrID_ : PTrID) : Boolean; cdecl;
 DpcPutRegSet     : Function (hif : THandle; rgbAddr : PByte;  rgbData : PByte; cbData : Integer; Var Erc : TErc; Var TrID : TTrID) : Boolean; cdecl;
 DpcGetRegSet     : Function (hif : THandle; rgbAddr : PByte;  rgbData : PByte; cbData : Integer; Var Erc : TErc; Var TrID : TTrID) : Boolean; cdecl;
 DpcPutRegRepeat  : Function (hif : THandle; bAddr : Byte; rgbData : PByte; cbData : Integer; Var Erc : TErc; pTrID_ : pTrID) : Boolean; cdecl;
 DpcGetRegRepeat  : Function (hif : THandle; bAddr : Byte; rgbData : PByte; cbData : Integer; Var Erc : TErc; pTrID_ : pTrID) : Boolean; cdecl;
 DpcStreamWrite   : Function (hif : THandle; rgbData: PByte; cbData : Integer; Var Erc : TErc; Var TrID : TTrID): Boolean; cdecl;
 DpcStreamRead    : Function (hif : THandle; rgbData: PByte; cbData : Integer; Var Erc : TErc; Var TrID : TTrID): Boolean; cdecl;


 // Device Manager Functions.
 // The device manager is used to maintain the table of Digilent
 // interface modules installed in the system.


 DvmgStartConfigureDevices : Procedure (hWnd : THandle; Var Erc: TErc); cdecl;


 DvmgGetDevCount  : Function (Var Erc : TErc) : Integer; cdecl;
 DvmgGetDevName   : Function (idvc : Integer; szdvcTemp : PChar; Var Erc : TErc) : Boolean; cdecl;
 DvmgGetDevType   : Function (idvc : Integer; dvtp : PInteger; perc_ : TErc) : Boolean; cdecl;
 DvmgGetDefaultDev: Function (Var Erc : TErc) : Integer; cdecl;


 //
 Function DpcEx_Msg (DpcMsgType : TDpcMsg; MsgID : Integer ) : String;


implementation


//------------------------------------------------------------------------------
//
// For Convenience
// DpcEx
//
//------------------------------------------------------------------------------


// Result Code to Msg Translation Function
Function DpcEx_Msg( DpcMsgType : TDpcMsg; MsgID : Integer ) : String;
 begin
  Case DpcMsgType of
   dmTrc : // Transaction Type   Message
           Case MsgID of
            $01 : Result := 'OpenJtag';
            $02 : Result := 'CloseJtag';
            $03 : Result := 'SetTmsTdiTck';
            $04 : Result := 'PutTdiBits';
            $05 : Result := 'PutTmsTdiBits';
            $06 : Result := 'GetTdoBits';
            $07 : Result := 'SetJtsel';
            $21 : Result := 'OpenSpi';
            $22 : Result := 'CloseSpi';
            $23 : Result := 'EnableSpi';
            $24 : Result := 'DisableSpi';
            $25 : Result := 'SetSpiSelect';
            $26 : Result := 'SetSpiMode';
            $27 : Result := 'PutSpiByte';
            $28 : Result := 'PutSpi';
            $29 : Result := 'GetSpi';
            $81 : Result := 'OpenData';
            $82 : Result := 'CloseData';
            $83 : Result := 'SetDataPins';
            $84 : Result := 'GetDataPins';
            $85 : Result := 'SendDataByte';
            $86 : Result := 'GetDataByte';
            $87 : Result := 'SendDataBytes';
            $88 : Result := 'GetDataBytes';
            $89 : Result := 'SendDataStream';
            $8a : Result := 'GetDataStream';
            $8b : Result := 'SendGetDataBytes';
            $8c : Result := 'GetVersion';
            $8d : Result := 'StreamWrite';
            $8e : Result := 'StreamRead';
            else  Result := 'Unknown';
           end;
   dmDvct : // Device Type        Message
           Case MsgID of
            $00 : Result := 'Ethernet';
            $01 : Result := 'USB';
            $02 : Result := 'Serial';
            $03 : Result := 'Parallel';
            else  Result := 'Unknown';
           End;
   dmErc  : // Error Code         Message
           Case MsgID of
            0000 : Result := 'NoError';
            3001 : Result := 'ConnReject';
            3002 : Result := 'ConnType';
            3003 : Result := 'ConnNoMode';
            3004 : Result := 'InvParam';
            3005 : Result := 'InvCmd';
            3006 : Result := 'Unknown';
            3007 : Result := 'JtagConflict';
            3008 : Result := 'NotImp';
            3009 : Result := 'NoMem';
            3010 : Result := 'Timeout';
            3011 : Result := 'Conflict';
            3012 : Result := 'BadPacket';
            3013 : Result := 'InvOption';
            3014 : Result := 'AlreadyCon';
            3101 : Result := 'Connected';
            3102 : Result := 'NotInit';
            3103 : Result := 'CantConnect';
            3104 : Result := 'AlreadyConnect';
            3105 : Result := 'SendError';
            3106 : Result := 'RcvError';
            3107 : Result := 'Abort';
            3108 : Result := 'TimeOutEx';
            3109 : Result := 'OutOfOrder';
            3110 : Result := 'ExtraData';
            3111 : Result := 'MissingData';
            3201 : Result := 'TrIDNotFound';
            3202 : Result := 'NotComplete';
            3203 : Result := 'NotConnected';
            3204 : Result := 'WrongMode';
            3205 : Result := 'WrongVersion';
            3301 : Result := 'DvctableDne';
            3302 : Result := 'DvctableCorrupt';
            3303 : Result := 'DvcDne';
            3304 : Result := 'DpcutilInitFail';
            3305 : Result := 'UnknownErr';
            3306 : Result := 'DvcTableOpen';
            3307 : Result := 'RegError';
            3308 : Result := 'NotifyRegFull';
            3309 : Result := 'NotifyNotFound';
            3310 : Result := 'OldDriverNewFw';
            3311 : Result := 'InvHandle';
            else   Result := 'UnKnown';
           end;
   dmSts  : // Transaction Status Message
            Case MsgId of
               1 : Result := 'New';
               2 : Result := 'Complete';
            End;
  end;
 end;


//
Function DllInit : Boolean;
 Var
  ErcCode : TErc;
  DevID   : Integer;
 begin
  // Initialize
  If gDpcLoaded then
   begin
    Result := True;
    Exit;
   end;
  //
  gDPCHandle := LoadLibrary(DPCModule);
  Case (gDPCHandle <= 0) of
   True  : begin
            Result := False;
            Exit;
           end;
   False : begin
            Result := True;
            gDpcLoaded := True;
           end;
  End;
  //
  DpcInit                   := GetProcAddress(gDPCHandle,'DpcInit');
  DpcTerm                   := GetProcAddress(gDPCHandle,'DpcTerm');
  DpcGetDpcVersion          := GetProcAddress(gDPCHandle,'DpcGetDpcVersion');
  DpcStartNotify            := GetProcAddress(gDPCHandle,'DpcStartNotify');
  DpcEndNotify              := GetProcAddress(gDPCHandle,'DpcEndNotify');
  DpcGetVersion             := GetProcAddress(gDPCHandle,'DpcGetVersion');
  DpcWaitForTransaction     := GetProcAddress(gDPCHandle,'DpcWaitForTransaction');
  DpcPendingTransactions    := GetProcAddress(gDPCHandle,'DpcPendingTransactions');
  DpcQueryConfigStatus      := GetProcAddress(gDPCHandle,'DpcQueryConfigStatus');
  DpcAbortConfigTransaction := GetProcAddress(gDPCHandle,'DpcAbortConfigTransaction');
  DpcClearConfigStatus      := GetProcAddress(gDPCHandle,'DpcClearConfigStatus');
  DpcGetFirstError          := GetProcAddress(gDPCHandle,'DpcGetFirstError');
  DpcOpenJtag               := GetProcAddress(gDPCHandle,'DpcOpenJtag');
  DpcCloseJtag              := GetProcAddress(gDPCHandle,'DpcCloseJtag');
  DpcEnableJtag             := GetProcAddress(gDPCHandle,'DpcEnableJtag');
  DpcDisableJtag            := GetProcAddress(gDPCHandle,'DpcDisableJtag');
  DpcSetTmsTdiTck           := GetProcAddress(gDPCHandle,'DpcSetTmsTdiTck');
  DpcPutTdiBits             := GetProcAddress(gDPCHandle,'DpcPutTdiBits');
  DpcPutTmsTdiBits          := GetProcAddress(gDPCHandle,'DpcPutTmsTdiBits');
  DpcGetTdoBits             := GetProcAddress(gDPCHandle,'DpcGetTdoBits');
  DpcOpenData               := GetProcAddress(gDPCHandle,'DpcOpenData');
  DpcCloseData              := GetProcAddress(gDPCHandle,'DpcCloseData');
  DpcPutReg                 := GetProcAddress(gDPCHandle,'DpcPutReg');
  DpcGetReg                 := GetProcAddress(gDPCHandle,'DpcGetReg');
  DpcPutRegSet              := GetProcAddress(gDPCHandle,'DpcPutRegSet');
  DpcGetRegSet              := GetProcAddress(gDPCHandle,'DpcGetRegSet');
  DpcPutRegRepeat           := GetProcAddress(gDPCHandle,'DpcPutRegRepeat');
  DpcGetRegRepeat           := GetProcAddress(gDPCHandle,'DpcGetRegRepeat');
  DpcStreamWrite            := GetProcAddress(gDPCHandle,'DpcStreamWrite');
  DpcStreamRead             := GetProcAddress(gDPCHandle,'DpcStreamRead');
  DvmgStartConfigureDevices := GetProcAddress(gDPCHandle,'DvmgStartConfigureDevices');
  DvmgGetDevCount           := GetProcAddress(gDPCHandle,'DvmgGetDevCount');
  DvmgGetDevName            := GetProcAddress(gDPCHandle,'DvmgGetDevName');
  DvmgGetDevType            := GetProcAddress(gDPCHandle,'DvmgGetDevType');
  DvmgGetDefaultDev         := GetProcAddress(gDPCHandle,'DvmgGetDefaultDev');
  //
 end;


//
Initialization
 gDPCLoaded := False; // Driver Not Loaded
 gDPCHandle := 0;     // No Handle
 FillChar(gDPCDevName,SizeOf(gDPCDevName),#0);
 DllInit;


Finalization
 // Clean Up
 If (gDPCLoaded)and(gDPCHandle <> 0) then
  FreeLibrary(gDpcHandle);
end. 


 


 


dpcutilex.pas







//
//  Encapsulate DpcUtil
//
//
//  Ref. PhoenixOnBoardMemCtrl.vhd
//
//     -- EPP Register Interface
//     --  Register Function
//     --  -------- --------
//     --     0     Memory control register (read/write)
//     --     1     Memory address bits 0-7 (read/write)
//     --     2     Memory address bits 8-15 (read/write)
//     --     3     Memory address bits 16-18 (read/write)
//     --     4     Memory data write holding register (read/write) -see Note 1
//     --     5     Memory data read register (read) - see Note 2
//     -- Registers 6 and 7 are used for block transfers
//     --     6     RAM auto read/write register (read/write) - see Note 3
//     --     7     Flash auto read/write register (read/write) - see Note 4
//
//
unit DpcUtilex;


Interface


Uses
 Types,Windows,SysUtils,StdCtrls,Forms;


Const
 cMaxBuf          = 1024*1024*16; // 16MB
 cXferBlockMemory = 1024*8; // Default  1000;
 cXferBlockFlash  = 1024*8; // Default  1000;


Type
 TDpcXferType = (xtMemory,xtFlash);
 TDpcXferDir  = (xdPut,xdGet);
 TDpcBuf      = Array[0..cMaxBuf-1] of Byte;


Var
 gDevName  : String;
 gDrvVer   : String;
 gDpcBuf   : TDpcBuf;
 gRptTotal : Single; // mSec
 gRptSpeed : Single; // MBytes/Sec


Function  DpcUtil_Init : Boolean;
Procedure DpcUtil_ShowSetupDlg;
Function  DpcUtil_GetReg  (grAddress : Byte;  Var grData : Byte   ) : Boolean;
Function  DpcUtil_PutReg  (grAddress : Byte;      grData : Byte   ) : Boolean;
Function  DpcUtil_Xfer    (XferType  : TDpcXferType;
                           XferDir   : TDpcXferDir;
                           dpAddress : DWord;
                           Size      : DWord;
                           Var dpBuf : TDpcBuf;
                           InfoLabel : TLabel) : Boolean;
Function  DpcUtil_File2Buf(gfFileName : String; Var gfBuf : TDpcBuf; Var Size : DWord) : Boolean;
Function  DpcUtil_Buf2File(Var gfBuf : TDpcBuf;gfFileName : String; Var Size : DWord) : Boolean;



Implementation


Uses
 DpcUtil;


Type
 TStopWatchMode = (swStart,swStop);


Var
 FStopWatch : TDateTime;


// 0.1 Sec
Function Time_StopWatch(sw_mode : TStopWatchMode) : Integer;
 Var
  SWTime            : TDateTime;
  Hour,Min,Sec,MSec :Word;
  Total             : Integer;
 begin
  Case sw_mode of
   swStart : begin
              FStopWatch := Now;
              Time_StopWatch  := 0;
             end;
   swStop  : begin
              SWTime := FStopWatch-Now;
              DecodeTime(SWTime,Hour,Min,Sec,MSec);
              Total := Hour*1000*60*60+
                       Min *1000*60+
                       Sec *1000+
                       MSec;
              Time_StopWatch := Total;
             end;
  end;
 end;


Function DpcUtil_Init : Boolean;
 Var
  ErcCode : TErc;
  DevID   : Integer;
 begin
  Case DpcInit(ErcCode) of
   False : Begin
            Result := False;
            Exit;
           End;
   True  : Begin
            DevID := DvmgGetDefaultDev(ErcCode); // Get Default Device ID
            Case DvmgGetDevName(DevID,gDpcDevName,ErcCode) of
             True  : begin
                      gDevName := String(gDpcDevName);
                      DpcGetDpcVersion(gDpcDrvVer,ErcCode);
                      gDrvVer  := String(gDpcDrvVer );
                      Result := True;
                     end;
             False : Result := False;
            End;
           End;
  End;
 end;


//
Procedure DpcUtil_ShowSetupDlg;
 Var
  Erc : TErc;
 begin
  DvmgStartConfigureDevices(0,Erc);
 end;


//
Function  DpcUtil_GetReg(grAddress : Byte; Var grData : Byte ) : Boolean;
 Var
  Erc    : TErc;
  TrID   : TTrid;
  hif    : THandle;
 begin
  //
  If Not(DpcOpenData(hif,@gDpcDevName,Erc,nil)) then
   begin
    Result := False;
    Exit;
   end;
  //
  If Not(DpcGetReg(hif,grAddress,grData,Erc,nil)) then
   begin
    Result := False;
    Exit;
   end;
  //
  Erc := DpcGetFirstError(hif);
  Result := (erc = ercNoError);
  //
  DpcCloseData(hif,Erc);
 end;


//
Function  DpcUtil_PutReg(grAddress : Byte;     grData : Byte ) : Boolean;
 Var
  Erc    : TErc;
  TrID   : TTrid;
  hif    : THandle;
 begin
  //
  If Not(DpcOpenData(hif,@gDpcDevName,Erc,nil)) then
   begin
    Result := False;
    Exit;
   end;
  //
  If Not(DpcPutReg(hif,grAddress,grData,Erc,nil)) then
   begin
    Result := False;
    Exit;
   end;
  //
  Erc := DpcGetFirstError(hif);
  Result := (erc = ercNoError);
  //
  DpcCloseData(hif,Erc);
 end;


//
Function  DpcUtil_Xfer    (XferType  : TDpcXferType;
                           XferDir   : TDpcXferDir;
                           dpAddress : DWord;
                           Size      : DWord;
                           Var dpBuf : TDpcBuf;
                           InfoLabel : TLabel) : Boolean;
 Var
  Erc              : TErc;
  TrID             : TTrid;
  idReg            : Byte;
  cbGet,cbGetTotal : Integer;
  cb               : DWord;
  hif              : THandle;
  Lp               : Integer;
  RstStr           : String;
  MemPtr           : DWord;
  XferBlockSize    : DWord;
  AddrBuf          : Array[0..3] of Byte;
 begin
  //
  hif    := 0;
  Case XferType of
   xtMemory : begin
               idReg         := 6; // to Memory
               XferBlockSize := cXferBlockMemory;
              end;
   xtFlash  : begin
               idReg         := 7; // to Flash
               XferBlockSize := cXferBlockFlash;
              end;
  End;
  cb     := Size;        // Data Size
  MemPtr := 0;
  Move(dpAddress,AddrBuf,SizeOf(dpAddress));
  //
  Time_StopWatch(swStart);
  //
  If Not(DpcOpenData(hif,@gDpcDevName,Erc,nil)) then
   begin
    Result := False;
    Exit;
   end;
  // -------------------------------------------------------------------------
  DpcPutReg(hif,0,$FF,Erc,nil); // Init


  DpcPutReg(hif,1,AddrBuf[0],Erc,nil); // Address
  DpcPutReg(hif,2,AddrBuf[1],Erc,nil);
  DpcPutReg(hif,3,AddrBuf[2],Erc,nil);


  // -------------------------------------------------------------------------
  cbGet := 0;
  cbGetTotal := cb;
  While (cbGetTotal > 0) do
   begin
    Case ((cbGetTotal - XferBlockSize) <= 0) of
     True  : begin
              cbGet      := cbGetTotal;
              cbGetTotal := 0;
             end;
     False : begin
              cbGet      := XferBlockSize;
              Dec(cbGetTotal,XferBlockSize);
             end;
    end;
    //
    Case XferDir of
     xdGet : If Not(DpcGetRegRepeat(hif,idReg,@dpBuf[MemPtr],cbGet,Erc,nil)) then
                begin
                 DpcCloseData(hif,Erc);
                 Result := False;
                 Exit;
                end;
     xdPut : If Not(DpcPutRegRepeat(hif,idReg,@dpBuf[MemPtr],cbGet,Erc,nil)) then
                begin
                 DpcCloseData(hif,Erc);
                 Result := False;
                 Exit;
                end;
    End;
    //
    Inc(MemPtr,cbGet);
    //
    InfoLabel.Caption := IntToStr(MemPtr);
    Application.ProcessMessages;
   end;
  //
  DpcCloseData(hif,Erc);
  //
  gRptTotal := Time_StopWatch (swStop); // mSec
  If gRptTotal = 0 then gRptTotal := 1;
  gRptSpeed := Size / (gRptTotal/1000); // Byte/Sec
  //
  Result := True;
 end;


//
Function  DpcUtil_File2Buf(gfFileName : String; Var gfBuf : TDpcBuf; Var Size : DWord) : Boolean;
 Var
  Fp : File;
 begin
  //
  If Not(FileExists(gfFileName)) then
   begin
    Result := False;
    Exit;
   end;
  //
  AssignFile(Fp,gfFileName);
  Reset(Fp,1);
  Size := FileSize(Fp);
  If Size > cMaxBuf then Size := cMaxBuf;
  BlockRead(Fp,gfBuf,Size);
  CloseFile(Fp);
  //
 end;


//
Function  DpcUtil_Buf2File(Var gfBuf : TDpcBuf;gfFileName : String; Var Size : DWord) : Boolean;
 Var
  Fp : File;
 begin
  //
  AssignFile(Fp,gfFileName);
  Rewrite(Fp,1);
  If Size > cMaxBuf then Size := cMaxBuf;
  BlockWrite(Fp,gfBuf,Size);
  CloseFile(Fp);
  //
 end;


initialization


finalization


end.