unit PluginIntf;

interface

///////////////////////////////////////////
/// Симулятор и модель микроконтроллера ///
///////////////////////////////////////////
type
  TMemorySegment = Byte;
const
  mcNone = 0;
  mcCode = 1;
  mcData = 2;
  mcXData = 3;
  mcIData = 4;
  mcBit = 5;
  mcEEData = 6;
  mcReg = 7;
type
  TMemoryAccessMode = (mamNormal, mamReadModifyWrite);
type
  PPorts = ^TPorts;
  TPorts = array [0..3] of Byte;

  PMemoryRAM = ^TMemoryRAM;
  TMemoryRAM = array [0..255] of Byte;
  PMemoryEEPROM = ^TMemoryEEPROM;
  TMemoryEEPROM = array [0..65535] of Byte;
  PMemoryAddRAM = ^TMemoryAddRAM;
  TMemoryAddRAM = array [128..255] of Byte;
  PMemoryLarge = ^TMemoryLarge;
  TMemoryLarge = array [0..65535] of Byte;
  TMemoryPointers = record
    RAM, RAMUsed : PMemoryRAM;
    AddRAM, AddRAMUsed : PMemoryAddRAM;
    FixedCode, Code, ICode, CodeUsed,
    XMem, XMemUsed : PMemoryLarge;
    FixedEEPROM, EEPROM, EEPROMUsed : PMemoryEEPROM;
    XRAM : PMemoryLarge;
    RAMSize, XRAMSize, EEPROMSize : Integer;
    UseInCode : Boolean;
    InPorts, PrevInPorts, OutPorts : PPorts;
  end;

  TPowerMode = (pmNormal, pmPowerDown, pmIdle);
  TSystemState = (ssNone, ssProject, ssSimulation);
  TProcessorInformation = record
    State : TSystemState;
    Cycle : Int64;
    RunTime : double;
    Freq : Integer;
    PC : Word;
    DPTRSelector : PByte;
    PowerMode : TPowerMode;
  end;

{*}  TGetMemoryData = function (Seg : Byte; Addr : Word; Mode : TMemoryAccessMode; Size : Integer; var Buf) : Boolean; stdcall;
{*}  TSetMemoryData = function (Seg : Byte; Addr : Word; Mode : TMemoryAccessMode; Size : Integer; const Buf) : Boolean; stdcall;
  TGetDPTR = function : Word; stdcall;
  TSetDPTR = procedure (Value : Word); stdcall;
  TGetProcessorInformation = function (var Info : TProcessorInformation) : Boolean; stdcall;
  TGetMemoryPointers = function (var Pointers : TMemoryPointers) : Boolean; stdcall;
  TUpdateInterruptPriors = procedure; stdcall;
  TRaiseSimulationErrorMessage = procedure (Message : PChar); stdcall;
  TRaiseSimulationError = procedure (ErrorPC : Word); stdcall;
  TSetForceStop = procedure; stdcall;
  TSetForceReset = procedure; stdcall;
  TSetPowerMode = procedure (PowerMode : TPowerMode); stdcall;
  TSimulationRun = procedure; stdcall;
  TSynchronizeCall = procedure (PluginID : Integer; Proc : Pointer); stdcall;
type
  TRegisterChipFeatureInfo = record
    GlobalFeatureID : Integer;

    PluginID : Integer;
    FeatureName,
    DisplayName : PChar;
    Flags : Word;
  end;
  TRegisterChipFeature = procedure (var Info : TRegisterChipFeatureInfo); stdcall;
  TFindFeatureInfo = record
    FeatureGID,
    FeatureID : Integer;
    FeatureDisplayName : PChar;
    PluginID : Integer;
    PluginHandle : THandle;
  end;
  TFindChipFeature = function (FeatureName : PChar; var Info : TFindFeatureInfo) : Boolean; stdcall;

const
  ifAutoClear = 1;
  ifExtraCond = 2;
type
  PInterruptBit = ^TInterruptBit;
  TInterruptBit = record
    Addr, Bit : Byte;
  end;
  TInterruptInfo = record
    InterruptID : Integer;

    Name, Info : PChar;

    FeatureID : Integer;
    Bits : PInterruptBit;
    BitCount : Byte;
    EnableBit : TInterruptBit;
    PriorityBit,
    PriorityHighBit : TInterruptBit;
    PredefPriority : Word;
    Vector : Word;
    Flags : Word;
  end;
  TRegisterInterrupt = procedure (var Info : TInterruptInfo); stdcall;
const
  ndRead = 1;
  ndWrite = 2;
  ndReadWrite = ndRead or ndWrite;
type
  TAssignSFRToMemory = procedure (FeatureID : Byte; Addr : Byte; Mask : Byte; Directions : Byte); stdcall;
  TAssignSFRToNotify = procedure (FeatureID : Byte; Addr : Byte; Mask : Byte; Directions : Byte; AsByte : Boolean); stdcall;
  TRegSFRCategory = (rtByte, rtBit);
  PRegSFRName = ^TRegSFRName;
  TRegSFRName = record
    Category : TRegSFRCategory;
    Addr : Byte;
    Name : PChar;
  end;
  TRegisterSFRName = procedure (FeatureID : Byte; Data : PRegSFRName; Count : Integer); stdcall;
  PRegFbdSFRBits = ^TRegFbdSFRBits;
  TRegFbdSFRBits = record
    Addr : Byte;
    Bit : Byte;
    Dirs : Byte;
    Name : PChar;
  end;
  TRegisterFbdSFRBits = procedure (FeatureID : Byte; Data : PRegFbdSFRBits; Count : Integer); stdcall;
//Resources tree
  TResTreeNodeCategory = (rncGroup, rncString, rncByte, rncBit, rncWord);
  TResTreeNodeType = (rtRAM, rtSFR, rtReg, rtDPTR, rt_xDPTR, rt_xReg, rtExtProg, rtExtMem, rtIPort, rtEEPROM);
  TResTreeNode = record
    NodeID : Integer;

    Name,
    Caption,
    Comment : PChar;
    ParentID : Integer;
    ImageIndex : Integer;
    Category : TResTreeNodeCategory;
    ReadOnly : Boolean;
    NumSys : ShortInt;
  case TResTreeNodeCategory of
    rncGroup: ();
    rncString: (ValueName : PChar);
    rncByte, rncBit: (ResType : TResTreeNodeType; Bit : Byte; Addr : Word);
    rncWord: (_ResType : TResTreeNodeType; AddrHi, AddrLo : Word);
  end;
  TCreateResTreeNode = procedure (FeatureID : Byte; var Info : TResTreeNode); stdcall;
  TFindResTreeNode = function (FeatureID : Byte; Name : PChar) : Integer; stdcall;
//export by plugin
const
  rtWarm = 0;
  rtCold = 1;
type
  TCreateFeature = procedure (GlobalFeatureID : Integer; FeatureID : Byte; Params : PChar); stdcall;
  TDestroyFeature = procedure (GlobalFeatureID : Integer; FeatureID : Byte); stdcall;

  TResTreeGetValue = procedure (ValueName : PChar; var Value : PChar); stdcall;
  TResTreeSetValue = procedure (ValueName : PChar); stdcall;

  TFeatureReadByte = function (FeatureID : Integer; Addr : Byte; Mode : TMemoryAccessMode) : Byte; stdcall;
  TFeatureReadBit = function (FeatureID : Integer; Addr, Bit : Byte; Mode : TMemoryAccessMode) : Boolean; stdcall;
  TFeatureWriteByte = procedure (FeatureID : Integer; Addr : Byte; Mode : TMemoryAccessMode; Val : Byte); stdcall;
  TFeatureWriteBit = procedure (FeatureID : Integer; Addr, Bit : Byte; Mode : TMemoryAccessMode; Val : Boolean); stdcall;
  TFeatureXMemRead = function (FeatureID : Integer; Addr : Word) : Byte; stdcall;
  TFeatureXMemWrite = procedure (FeatureID : Integer; Addr : Word; Val : Byte); stdcall;
  TFeatureEmulate = procedure (FeatureID : Integer; ProcessorInformation: TProcessorInformation; CyclesGone : Word; TimeGone : Double); stdcall;
  TFeatureReset = procedure (FeatureID : Integer; ResetType : Byte); stdcall;
  TFeatureLoad = procedure (FeatureID : Integer); stdcall;
  TFeatureSave = procedure (FeatureID : Integer); stdcall;
  TFeatureInterruptCheck = function (FeatureID, InterruptID : Integer) : Boolean; stdcall;

// EEPROM & XRAM
  TSetXMemoryHandler = procedure (FeatureID : Integer); stdcall;
  TRemoveXMemoryHandler = procedure (FeatureID : Integer); stdcall;

  TXMemExtraSpace = (xmsEEPROM, xmsXRAM);
  TSetXMemExtraSpaceSize = procedure (Space : TXMemExtraSpace; Size : Integer); stdcall;

  PEEPROMInfo = ^TEEPROMInfo;
  TEEPROMInfo = record
    Writing : Boolean;
    WriteAddr : Word;
    WriteValue : Byte;
  end;
  TGetEEPROMInfo = function: PEEPROMInfo; stdcall;
// UART procedures
type
  TUARTState = set of (usReceive, usTransmit);
  PUARTInfo = ^TUARTInfo;
  TUARTInfo = record
    UARTState : TUARTState;
    UARTMode : Byte;
    FastMode : Boolean;
    RCounter, TCounter : Integer;
    RDivider, TDivider, RDivValue, TDivValue : Integer;
    TransmitLastByte, ReceiveLastByte : Boolean;
    RSpeed, TSpeed : Integer;
    SyncRByte : Byte;
    SyncR9 : Boolean;
    NewRData : Boolean;
    WaitCounter : Integer;
    RData : Byte;
    RData9 : Boolean;
  end;
//mcstudio:
  TGetUARTInfo = function: PUARTInfo; stdcall;
  TUARTStartTransmitByte = procedure (B : Byte; a9 : Boolean); stdcall;
  TUARTTransmitByte = procedure (B : Byte; aStart, aStop, a9 : Boolean); stdcall;
  TUARTPrepareReceiveByte = procedure; stdcall;
  TUARTReceiveByteNotify = procedure; stdcall;
  TUARTAutoSendCheck = procedure; stdcall;
//uart plugins:
  TUARTReceiveTimer = procedure; stdcall;
  TUARTTransmitTimer = procedure; stdcall;

/////////////////////
// Общие процедуры //
/////////////////////

const
  pfProjectOptions = 1;
  pfSystemOptions = 2;
type
  TPluginInfo = record
    PluginID : Integer;
    Version,
    Desc,
    Author,
    Copyright,
    Links : PChar;
    Flags : LongWord;
  end;
  TInit = procedure (AppHandle, ExeHandle : THandle; var Info : TPluginInfo); stdcall;
  TFinalize = procedure; stdcall;

///////////////////////
/// Внешние форматы ///
///////////////////////
const
  fpfProgram = 1;
  fpfEEPROM = 2;
  fpfCreatePropPage = 4;
  fpfCallAny = 8;
  fpfNoFile = 16;
  fpfImportAsConst = 32;
type
  TRegProgFormatInfo = record
    FormatID : Integer;

    PluginID : Integer;
    SysName, FormatName, FileDesc : PChar;
    FileExt : PChar;
    Flags : LongWord;
  end;
  TRegProgFormat = procedure (var Info : TRegProgFormatInfo); stdcall;

  TProgFormatInfo = record
    FileName : PChar;
    Code, CodeUsed : PMemoryLarge;
    EEPROM, EEPROMUsed : PMemoryEEPROM;
    EEPROMSize : Integer;
    Create : Byte; //fpfProgram or fpfEEPROM
  end;
  TLoadProgFormat = function (FormatID : Integer; var Info : TProgFormatInfo) : Boolean; stdcall;
  TSaveProgFormat = function (FormatID : Integer; var Info : TProgFormatInfo) : Boolean; stdcall;
  TFormatPropCreate = procedure (FormatID : Integer; ParentWindow : THandle); stdcall;
  TFormatPropFree = procedure (FormatID : Integer); stdcall;
  TFormatPropCheck = function (FormatID : Integer) : Boolean; stdcall;
  TFormatPropSave = procedure (FormatID : Integer); stdcall;

/////////////////////////
/// Интерфейс системы ///
/////////////////////////

const
  afAutoCheck = 1;
  afEnProject = 2;
  afEnFile = 4;
  afEnDebug = 32;
  afToolbar = 64;
const
  asEnabled = 1;
  asVisible = 2;
  asChecked = 4;
const
  imgGlobal = 0;
  imgResources = 1;
type
  TAddImageIcon = function (PluginID : Integer; List : Byte; IconFile : PChar; IconIndex : Integer) : Integer; stdcall;
  TAddImageBitmap = function (PluginID : Integer; List : Byte; BitmapName : PChar) : Integer; stdcall;

  TActionInfo = record
    PluginID : Integer;
    ActionID : Integer;
    Caption : PChar;
    Hint : PChar;
    ImageIndex : Integer;
    ShortCut : PChar;
    GroupIndex : Integer;
    Flags : LongWord;
  end;

  TCreateAction = procedure (var Info : TActionInfo); stdcall;
  TFreeAction = procedure (ActionID : Integer); stdcall;
  TGetActionState = procedure (ActionID : Integer; var State : LongWord); stdcall;
  TSetActionState = procedure (ActionID : Integer; State : LongWord); stdcall;
type
  TMenuItemInfo = record
    ParentGroupName : PChar;
    Name : PChar;
    Caption : PChar;
    ActionID : Integer;
    AddType : (atAddChild, atInsertBefore);
  end;
  TCreateMenuItem = procedure (Info : TMenuItemInfo); stdcall;
  TFreeMenuItem = procedure (Name : PChar); stdcall;
type
  TLoadProjectOptions = procedure (Text : PChar); stdcall;
  TSaveProjectOptions = procedure (var Text : PChar); stdcall;
  TActionExecute = procedure (ActionID : Integer); stdcall;

/////////////////////////
/// Менеджер проектов ///
/////////////////////////
type
  TModelInfo = record
    Name, Group : PChar;
    InProgSize : Integer;
    RAM : Integer;
  end;
  TProjectInfo = record
    Loaded : Boolean;
    Path, Name : PChar;
    Model : TModelInfo;
  end;
  TGetProjectInfo = procedure (var Info : TProjectInfo); stdcall;
const
  lspSystem = 1;
  lspProject = 2;
  lspNotExpanded = 4;
  lspAll = lspSystem or lspProject;
type
  TGetLibSearchPath = procedure (SearchPathes : Byte; var S : PChar); stdcall;
  TGetMemory = function (NeedRebuild : Boolean; var Pointers: TMemoryPointers) : Boolean; stdcall;

////////////////////////////////////////
/// Текстовый и графический редактор ///
////////////////////////////////////////

{type
  TFileType = (ftAsm, ftC, ftFBD, ftFC, ftCBlock, ftProject, ftUnknown);
  TFileInfo = record
    FileType : TFileType;
    Path : PChar;
    Modified : Boolean;
  end;
  TGetCurrentFileInfo = function (var Info : TFileInfo) : Boolean; stdcall;
const
  timInsertLineBreaks = 1;
  timAutoIndent = 2;
type
  TTextInsertLines = function (Mode : Integer; Text : PChar) : Boolean; stdcall;}

//////////////////////////////////////////////////
// Объявления указателей на процедуры EXE-файла //
//////////////////////////////////////////////////
var
  GetProjectInfo : TGetProjectInfo;
  GetLibSearchPath : TGetLibSearchPath;
  GetMemory : TGetMemory;
  RegProgFormat : TRegProgFormat;
  AddImageIcon : TAddImageIcon;
  AddImageBitmap : TAddImageBitmap;
  CreateAction : TCreateAction;
  FreeAction : TFreeAction;
  GetActionState : TGetActionState;
  SetActionState : TSetActionState;
  CreateMenuItem : TCreateMenuItem;
  FreeMenuItem : TFreeMenuItem;

  GetMemoryData : TGetMemoryData;
  SetMemoryData : TSetMemoryData;
  GetDPTR : TGetDPTR;
  SetDPTR : TSetDPTR;
  GetProcessorInformation : TGetProcessorInformation;
  GetMemoryPointers : TGetMemoryPointers;
  RaiseSimulationErrorMessage : TRaiseSimulationErrorMessage;
  RaiseSimulationError : TRaiseSimulationError;
  SetForceStop : TSetForceStop;
  SetForceReset : TSetForceReset;
  SetPowerMode : TSetPowerMode;
  SimulationRun : TSimulationRun;
  SynchronizeCall : TSynchronizeCall;
  RegisterChipFeature : TRegisterChipFeature;
  RegisterInterrupt : TRegisterInterrupt;
  UpdateInterruptPriors : TUpdateInterruptPriors;
  FindChipFeature : TFindChipFeature;
  AssignSFRToMemory : TAssignSFRToMemory;
  AssignSFRToNotify : TAssignSFRToNotify;
  RegisterSFRName : TRegisterSFRName;
  RegisterFbdSFRBits : TRegisterFbdSFRBits;
  CreateResTreeNode : TCreateResTreeNode;
  FindResTreeNode : TFindResTreeNode;

  GetUARTInfo : TGetUARTInfo;
  UARTStartTransmitByte : TUARTStartTransmitByte;
  UARTTransmitByte : TUARTTransmitByte;
  UARTPrepareReceiveByte : TUARTPrepareReceiveByte;
  UARTReceiveByteNotify : TUARTReceiveByteNotify;
  UARTAutoSendCheck : TUARTAutoSendCheck;

  GetEEPROMInfo : TGetEEPROMInfo;
  SetXMemoryHandler : TSetXMemoryHandler;
  RemoveXMemoryHandler : TRemoveXMemoryHandler;
  SetXMemExtraSpaceSize : TSetXMemExtraSpaceSize;

//Заполняет эти указатели
procedure GetMCSProcAddresses(ExeHandle : THandle);

implementation

uses
  Windows;

procedure GetMCSProcAddresses(ExeHandle : THandle);
begin
  @GetProjectInfo := GetProcAddress(ExeHandle, 'GetProjectInfo');
  @GetLibSearchPath := GetProcAddress(ExeHandle, 'GetLibSearchPath');
  @GetMemory := GetProcAddress(ExeHandle, 'GetMemory');
  @RegProgFormat := GetProcAddress(ExeHandle, 'RegProgFormat');
  @AddImageIcon := GetProcAddress(ExeHandle, 'AddImageIcon');
  @AddImageBitmap := GetProcAddress(ExeHandle, 'AddImageBitmap');
  @CreateAction := GetProcAddress(ExeHandle, 'CreateAction');
  @FreeAction := GetProcAddress(ExeHandle, 'FreeAction');
  @GetActionState := GetProcAddress(ExeHandle, 'GetActionState');
  @SetActionState := GetProcAddress(ExeHandle, 'SetActionState');
  @CreateMenuItem := GetProcAddress(ExeHandle, 'CreateMenuItem');
  @FreeMenuItem := GetProcAddress(ExeHandle, 'FreeMenuItem');

  @GetMemoryData := GetProcAddress(ExeHandle, 'GetMemoryData');
  @SetMemoryData := GetProcAddress(ExeHandle, 'SetMemoryData');
  @GetDPTR := GetProcAddress(ExeHandle, 'GetDPTR');
  @SetDPTR := GetProcAddress(ExeHandle, 'SetDPTR');
  @GetProcessorInformation := GetProcAddress(ExeHandle, 'GetProcessorInformation');
  @GetMemoryPointers := GetProcAddress(ExeHandle, 'GetMemoryPointers');
  @UpdateInterruptPriors := GetProcAddress(ExeHandle, 'UpdateInterruptPriors');
  @RaiseSimulationErrorMessage := GetProcAddress(ExeHandle, 'RaiseSimulationErrorMessage');
  @RaiseSimulationError := GetProcAddress(ExeHandle, 'RaiseSimulationError');
  @SetForceStop := GetProcAddress(ExeHandle, 'SetForceStop');
  @SetForceReset := GetProcAddress(ExeHandle, 'SetForceReset');
  @SetPowerMode := GetProcAddress(ExeHandle, 'SetPowerMode');
  @SimulationRun := GetProcAddress(ExeHandle, 'SimulationRun');
  @SynchronizeCall := GetProcAddress(ExeHandle, 'SynchronizeCall');
  @RegisterChipFeature := GetProcAddress(ExeHandle, 'RegisterChipFeature');
  @RegisterInterrupt := GetProcAddress(ExeHandle, 'RegisterInterrupt');
  @FindChipFeature := GetProcAddress(ExeHandle, 'FindChipFeature');
  @AssignSFRToMemory := GetProcAddress(ExeHandle, 'AssignSFRToMemory');
  @AssignSFRToNotify := GetProcAddress(ExeHandle, 'AssignSFRToNotify');
  @RegisterSFRName := GetProcAddress(ExeHandle, 'RegisterSFRName');
  @RegisterFbdSFRBits := GetProcAddress(ExeHandle, 'RegisterFbdSFRBits');
  @CreateResTreeNode := GetProcAddress(ExeHandle, 'CreateResTreeNode');
  @FindResTreeNode := GetProcAddress(ExeHandle, 'FindResTreeNode');

  GetUARTInfo := GetProcAddress(ExeHandle, 'GetUARTInfo');
  UARTStartTransmitByte := GetProcAddress(ExeHandle, 'UARTStartTransmitByte');
  UARTTransmitByte := GetProcAddress(ExeHandle, 'UARTTransmitByte');
  UARTPrepareReceiveByte := GetProcAddress(ExeHandle, 'UARTPrepareReceiveByte');
  UARTReceiveByteNotify := GetProcAddress(ExeHandle, 'UARTReceiveByteNotify');
  UARTAutoSendCheck := GetProcAddress(ExeHandle, 'UARTAutoSendCheck');

  @GetEEPROMInfo := GetProcAddress(ExeHandle, 'GetEEPROMInfo');
  @SetXMemoryHandler := GetProcAddress(ExeHandle, 'SetXMemoryHandler');
  @RemoveXMemoryHandler := GetProcAddress(ExeHandle, 'RemoveXMemoryHandler');
  @SetXMemExtraSpaceSize := GetProcAddress(ExeHandle, 'SetXMemExtraSpaceSize');
end;

end.
