Unit uFT800Native;

interface
// global part

{ $W+}                  // enable/disable warnings for this unit

uses ;

{--------------------------------------------------------------}
{ Const Declarations }
const

{$IDATA}
{--------------------------------------------------------------}
{ Type Declarations }
type

{--------------------------------------------------------------}
{ Var Declarations }
var
   {$IFDEF StangeV2}
   FT800ResetDir[@DDRB, 6]: Bit;
   FT800Reset   [@PortB,6]: Bit;
   FT800CSDir   [@DDRC, 4]: Bit;
   FT800CS      [@PortC,4]: Bit;
   {$ELSIFDEF MGALoggerV2 or MGALoggerV2_640480}
   FT800ResetDir[@DDRA, 4]: Bit;
   FT800Reset   [@PortA,4]: Bit;
   FT800CSDir   [@DDRA, 2]: Bit;
   FT800CS      [@PortA,2]: Bit;
   {$ELSIFDEF AKZOLogger}
   FT800ResetDir[@DDRC, 0]: Bit;
   FT800Reset   [@PortC,0]: Bit;
   FT800CSDir   [@DDRC, 4]: Bit;
   FT800CS      [@PortC,4]: Bit;
   {$ENDIF}

{--------------------------------------------------------------}
{ functions }
Procedure InitFT800Pins;
Function  FT800_ReadGPU8(CMD:LongWord):Byte;
Function  FT800_ReadGPU16(CMD:LongWord):Word;
Function  FT800_ReadGPU32(CMD:LongWord):LongWord;
Procedure FT800_HostCommand(CMD:Byte);

Procedure FT800_SPI_Slow;
Procedure FT800_SPI_Fast;

Procedure FT800_CS_Hi;
Procedure FT800_CS_Lo;
Procedure FT800_RES_Hi;
Procedure FT800_RES_Lo;

Procedure EVEoutAddr(Value:LongWord);
Procedure EVEoutLong(Data:LongWord);
Procedure EVEoutWord(Data:Word);
Procedure EVEoutByte(Data:Byte);

implementation
// local part

{--------------------------------------------------------------}
{ Type Declarations }
type

{--------------------------------------------------------------}
{ Const Declarations }
const

{--------------------------------------------------------------}
{ Var Declarations }
{$IDATA}

{--------------------------------------------------------------}
{ functions }
Procedure InitFT800Pins;
Begin
  FT800ResetDir:=True;
  FT800Reset   :=True;
  FT800CSDir   :=True;
  FT800CS      :=True;
end;

Procedure EVEoutLong(Data:LongWord);
Begin
  SPIOutLongC(Data);         //Ausgabe 1Byte und Input 4 Byte
end;

Procedure EVEoutWord(Data:Word);
Begin
  SPIOutWordC(Data);         //Ausgabe 1Byte und Input 4 Byte
end;

Procedure EVEoutByte(Data:Byte);
Begin
  SPIOutByteC(Data);         //Ausgabe 1Byte und Input 4 Byte
end;

Procedure EVEoutAddr(Value:LongWord);
var CMDArray:Array[0..2] of Byte;
Begin
  CMDArray[0]:=Lo(HiWord(Value)) or %10000000;
  CMDArray[1]:=Hi(LoWord(Value));
  CMDArray[2]:=Lo(LoWord(Value));
  SPIOutC(@CMDArray,3);         //Ausgabe 1Byte und Input 4 Byte
end;

Procedure FT800_SPI_Fast;
Begin

end;

Procedure FT800_SPI_Slow;
Begin
  SetSPIprescC(128);
end;

Procedure FT800_CS_Hi;
Begin
  FT800CS:=True;
end;

Procedure FT800_CS_Lo;
Begin
  FT800CS:=False;
end;

Procedure FT800_RES_Hi;
Begin
  FT800Reset:=True;
end;

Procedure FT800_RES_Lo;
Begin
  FT800Reset:=False;
end;

Procedure Ft800_HostCommand(CMD:Byte);
var CMDArray:Array[0..2] of Byte;
Begin
  CMDArray[0]:=CMD;// or %01000000;
  CMDArray[1]:=0;
  CMDArray[2]:=0;
  FT800CS:=False;
  SPIOutC(@CMDArray,3);
  FT800CS:=True;
end;

Function  FT800_ReadGPU8(CMD:LongWord):Byte;
var CMDArray:Array[0..3] of Byte;
    temp:Byte;
Begin
  CMDArray[0]:=Lo(HiWord(CMD));
  CMDArray[1]:=Hi(LoWord(CMD));
  CMDArray[2]:=Lo(LoWord(CMD));
  CMDArray[3]:=0;
  FT800CS:=False;
  SPIOutC(@CMDArray,4);         //Ausgabe 1Byte und Input 4 Byte
  Temp:=SPIInpByteC;            //Ausgabe 1Byte und Input 4 Byte
  FT800CS:=True;
  Return(Temp);
end;

Function  FT800_ReadGPU16(CMD:LongWord):Word;
var CMDArray:Array[0..3] of Byte;
    Temp:Word;
Begin
  CMDArray[0]:=Lo(HiWord(CMD));
  CMDArray[1]:=Hi(LoWord(CMD));
  CMDArray[2]:=Lo(LoWord(CMD));
  CMDArray[3]:=0;
  FT800CS:=False;
  SPIOutC(@CMDArray,4);         //Ausgabe 1Byte und Input 4 Byte
  Temp :=SPIInpWordC;           //Ausgabe 1Byte und Input 4 Byte
  FT800CS:=True;
  Return(Temp);
end;

Function  FT800_ReadGPU32(CMD:LongWord):LongWord;
var CMDArray:Array[0..3] of Byte;
    Temp:LongWord;
Begin
  CMDArray[0]:=Lo(HiWord(CMD));
  CMDArray[1]:=Hi(LoWord(CMD));
  CMDArray[2]:=Lo(LoWord(CMD));
  CMDArray[3]:=0;
  FT800CS:=False;
  SPIOutC(@CMDArray,4);         //Ausgabe 1Byte und Input 4 Byte
  Temp :=SPIInpLongC;           //Ausgabe 1Byte und Input 4 Byte
  FT800CS:=True;
  Return(Temp);
end;


initialization
// at StartUp

// finalization          // optional
// at System_ShutDown
end uFT800Native.

