⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 common.cpp

📁 配合KEIL C的AGSI调试I2C的工具
💻 CPP
字号:

// This module contains functions that are needed for almost every AGSI DLL

#include "stdafx.h"
#include "Agsi.h"
#include "common.h"

AGSICONFIG  AgsiConfig;
struct AGSIFUNCS   Agsi;

DWORD GetFunctionPointers(void) {           // get all function pointers

  Agsi.DefineSFR            = (tAgsiDefineSFR)             GetProcAddress(AgsiConfig.m_hInstance, "AgsiDefineSFR");
  Agsi.DefineVTR            = (tAgsiDefineVTR)             GetProcAddress(AgsiConfig.m_hInstance, "AgsiDefineVTR");
  Agsi.DeclareInterrupt     = (tAgsiDeclareInterrupt)      GetProcAddress(AgsiConfig.m_hInstance, "AgsiDeclareInterrupt");
  Agsi.SetWatchOnSFR        = (tAgsiSetWatchOnSFR)         GetProcAddress(AgsiConfig.m_hInstance, "AgsiSetWatchOnSFR");
  Agsi.SetWatchOnVTR        = (tAgsiSetWatchOnVTR)         GetProcAddress(AgsiConfig.m_hInstance, "AgsiSetWatchOnVTR");
  Agsi.SetWatchOnMemory     = (tAgsiSetWatchOnMemory)      GetProcAddress(AgsiConfig.m_hInstance, "AgsiSetWatchOnMemory");
  Agsi.CreateTimer          = (tAgsiCreateTimer)           GetProcAddress(AgsiConfig.m_hInstance, "AgsiCreateTimer");
  Agsi.DefineMenuItem       = (tAgsiDefineMenuItem)        GetProcAddress(AgsiConfig.m_hInstance, "AgsiDefineMenuItem");

  Agsi.WriteSFR             = (tAgsiWriteSFR)              GetProcAddress(AgsiConfig.m_hInstance, "AgsiWriteSFR");
  Agsi.ReadSFR              = (tAgsiReadSFR)               GetProcAddress(AgsiConfig.m_hInstance, "AgsiReadSFR");
  Agsi.WriteVTR             = (tAgsiWriteVTR)              GetProcAddress(AgsiConfig.m_hInstance, "AgsiWriteVTR");
  Agsi.ReadVTR              = (tAgsiReadVTR)               GetProcAddress(AgsiConfig.m_hInstance, "AgsiReadVTR");
  Agsi.SetSFRReadValue      = (tAgsiSetSFRReadValue)       GetProcAddress(AgsiConfig.m_hInstance, "AgsiSetSFRReadValue");
  Agsi.ReadMemory           = (tAgsiReadMemory)            GetProcAddress(AgsiConfig.m_hInstance, "AgsiReadMemory");
  Agsi.WriteMemory          = (tAgsiWriteMemory)           GetProcAddress(AgsiConfig.m_hInstance, "AgsiWriteMemory");
  Agsi.GetLastMemoryAddress = (tAgsiGetLastMemoryAddress)  GetProcAddress(AgsiConfig.m_hInstance, "AgsiGetLastMemoryAddress");
  Agsi.IsSimulatorAccess    = (tAgsiIsSimulatorAccess)     GetProcAddress(AgsiConfig.m_hInstance, "AgsiIsSimulatorAccess");
  Agsi.SetTimer             = (tAgsiSetTimer)              GetProcAddress(AgsiConfig.m_hInstance, "AgsiSetTimer");
  Agsi.GetStates            = (tAgsiGetStates)             GetProcAddress(AgsiConfig.m_hInstance, "AgsiGetStates");
  Agsi.GetProgramCounter    = (tAgsiGetProgramCounter)     GetProcAddress(AgsiConfig.m_hInstance, "AgsiGetProgramCounter");
  Agsi.IsInInterrupt        = (tAgsiIsInInterrupt)         GetProcAddress(AgsiConfig.m_hInstance, "AgsiIsInInterrupt");
  Agsi.IsSleeping           = (tAgsiIsSleeping)            GetProcAddress(AgsiConfig.m_hInstance, "AgsiIsSleeping");
  Agsi.StopSimulator        = (tAgsiStopSimulator)         GetProcAddress(AgsiConfig.m_hInstance, "AgsiStopSimulator");
  Agsi.TriggerReset         = (tAgsiTriggerReset)          GetProcAddress(AgsiConfig.m_hInstance, "AgsiTriggerReset");
  Agsi.UpdateWindows        = (tAgsiUpdateWindows)         GetProcAddress(AgsiConfig.m_hInstance, "AgsiUpdateWindows");
  Agsi.HandleFocus          = (tAgsiHandleFocus)           GetProcAddress(AgsiConfig.m_hInstance, "AgsiHandleFocus");
  Agsi.GetExternalClockRate = (tAgsiGetExternalClockRate)  GetProcAddress(AgsiConfig.m_hInstance, "AgsiGetExternalClockRate");
  Agsi.GetInternalClockRate = (tAgsiGetInternalClockRate)  GetProcAddress(AgsiConfig.m_hInstance, "AgsiGetInternalClockRate");
  Agsi.GetClockFactor       = (tAgsiGetClockFactor)        GetProcAddress(AgsiConfig.m_hInstance, "AgsiGetClockFactor");
  Agsi.Message              = (tAgsiMessage)               GetProcAddress(AgsiConfig.m_hInstance, "AgsiMessage");
  Agsi.GetTargetKey         = (tAgsiGetTargetKey)          GetProcAddress(AgsiConfig.m_hInstance, "AgsiGetTargetKey");
  Agsi.SetTargetKey         = (tAgsiSetTargetKey)          GetProcAddress(AgsiConfig.m_hInstance, "AgsiSetTargetKey");
  Agsi.GetSymbolByName      = (tAgsiGetSymbolByName)       GetProcAddress(AgsiConfig.m_hInstance, "AgsiGetSymbolByName");
  Agsi.GetSymbolByValue     = (tAgsiGetSymbolByValue)      GetProcAddress(AgsiConfig.m_hInstance, "AgsiGetSymbolByValue");

  if ((Agsi.DefineSFR            == NULL) ||
      (Agsi.DefineVTR            == NULL) ||
      (Agsi.DeclareInterrupt     == NULL) ||
      (Agsi.SetWatchOnSFR        == NULL) ||
      (Agsi.SetWatchOnVTR        == NULL) ||
      (Agsi.SetWatchOnMemory     == NULL) ||
      (Agsi.CreateTimer          == NULL) ||
      (Agsi.DefineMenuItem       == NULL) ||

      (Agsi.WriteSFR             == NULL) ||
      (Agsi.ReadSFR              == NULL) ||
      (Agsi.WriteVTR             == NULL) ||
      (Agsi.ReadVTR              == NULL) ||
      (Agsi.SetSFRReadValue      == NULL) ||
      (Agsi.ReadMemory           == NULL) ||
      (Agsi.WriteMemory          == NULL) ||
      (Agsi.GetLastMemoryAddress == NULL) ||
      (Agsi.IsSimulatorAccess    == NULL) ||
      (Agsi.SetTimer             == NULL) ||
      (Agsi.GetStates            == NULL) ||
      (Agsi.GetProgramCounter    == NULL) ||
      (Agsi.IsInInterrupt        == NULL) ||
      (Agsi.IsSleeping           == NULL) ||
      (Agsi.StopSimulator        == NULL) ||
      (Agsi.TriggerReset         == NULL) ||
      (Agsi.UpdateWindows        == NULL) ||
      (Agsi.HandleFocus          == NULL) ||
      (Agsi.GetExternalClockRate == NULL) ||
      (Agsi.GetInternalClockRate == NULL) ||
      (Agsi.GetClockFactor       == NULL) ||
      (Agsi.Message              == NULL) ||
      (Agsi.GetTargetKey         == NULL) ||
      (Agsi.SetTargetKey         == NULL) ||
      (Agsi.GetSymbolByName      == NULL) ||
      (Agsi.GetSymbolByValue     == NULL)) {

    return(FALSE);
  }

  return(1);
}

//********************************************************************************************
// The following functions help you to input and output strings and (float) numbers in dialogs
// Please review them before you try to write your own function

// Output 2-digit Hexnumber to Text-Control of given Dialog
void StringHex2 (CWnd * pCWnd, DWORD val)  {
  char locbuffer[20];
  sprintf (locbuffer, "0x%02X", val);
  pCWnd->SetWindowText (locbuffer);
}

// Output 4-digit Hexnumber to Text-Control of given Dialog
void StringHex4 (CWnd * pCWnd, DWORD val)  {
  char locbuffer[20];
  sprintf (locbuffer, "0x%04X", val);
  pCWnd->SetWindowText (locbuffer);
}

// Output 6-digit Hexnumber to Text-Control of given Dialog
void StringHex6 (CWnd * pCWnd, DWORD val)  {
  char locbuffer[20];
  sprintf (locbuffer, "0x%06X", val);
  pCWnd->SetWindowText (locbuffer);
}

// Output Float number to Text-Control of given Dialog
void StringFloat (CWnd * pCWnd, float val)  {
  char locbuffer[20];
  sprintf (locbuffer, "%1.4f", val);
  pCWnd->SetWindowText (locbuffer);
}

static const char INPUT_ERR_TITLE [] = "Invalid number";
static const char INPUT_ERRMSG[]     = "You have entered an invalid number!\n"
                                       "The previous value will be restored.\n"
                                       "Examples: 0x12\n";

static const char INPUT_OVR_TITLE [] = "Out of range";
static const char INPUT_OVRMSG[]     = "You have entered a number that is out of range!\n";

long GetDlg8BNumber (CWnd * pCWnd, DWORD oldval) {
  DWORD temp;
  WORD n;
  char lbuf[100];
  
  n = pCWnd->GetWindowText (lbuf, 100);
  lbuf[n] = '\0';  /* terminate string */
  n = sscanf(lbuf, "%x", &temp);
  if (n != 1) {
    MessageBeep(MB_ICONEXCLAMATION);
    pCWnd->MessageBox(&INPUT_ERRMSG[0], &INPUT_ERR_TITLE[0], MB_OK|MB_ICONSTOP); 
    StringHex2 (pCWnd, oldval);
    return(-1);
  }
  if (temp > 0xFF) {
    MessageBeep(MB_ICONEXCLAMATION);
    pCWnd->MessageBox(INPUT_OVRMSG, INPUT_OVR_TITLE, MB_OK | MB_ICONSTOP); 
    StringHex2 (pCWnd, oldval);
    return(-1);
  }
  StringHex2 (pCWnd, temp);
  return(temp);
}

long GetDlg16BNumber (CWnd *pCWnd, DWORD oldval) {
  DWORD temp;
  WORD n;
  char lbuf[100];
  
  n = pCWnd->GetWindowText (lbuf, 100);
  lbuf[n] = '\0';  /* terminate string */
  n = sscanf(lbuf, "%x", &temp);
  if (n != 1) {
    MessageBeep(MB_ICONEXCLAMATION);
    pCWnd->MessageBox(&INPUT_ERRMSG[0],&INPUT_ERR_TITLE[0],MB_OK|MB_ICONSTOP); 
    StringHex4 (pCWnd, oldval);
    return(-1);
  }
  if (temp > 0xFFFF) {
    MessageBeep(MB_ICONEXCLAMATION);
    pCWnd->MessageBox(INPUT_OVRMSG, INPUT_OVR_TITLE, MB_OK | MB_ICONSTOP); 
    StringHex4 (pCWnd, oldval);
    return(-1);
  }
  StringHex4 (pCWnd, temp);
  return(temp);
}

long GetDlg24BNumber (CWnd *pCWnd, DWORD oldval) {
  DWORD temp;
  WORD n;
  char lbuf[100];
  
  n = pCWnd->GetWindowText (lbuf, 100);
  lbuf[n] = '\0';  /* terminate string */
  n = sscanf(lbuf, "%x", &temp);
  if (n != 1) {
    MessageBeep(MB_ICONEXCLAMATION);
    pCWnd->MessageBox(INPUT_ERRMSG, INPUT_ERR_TITLE, MB_OK | MB_ICONSTOP); 
    StringHex6 (pCWnd, oldval);
    return(-1);
  }
  if (temp > 0xFFFFFF) {
    MessageBeep(MB_ICONEXCLAMATION);
    pCWnd->MessageBox(INPUT_OVRMSG, INPUT_OVR_TITLE, MB_OK | MB_ICONSTOP); 
    StringHex6 (pCWnd, oldval);
    return(-1);
  }
  StringHex6 (pCWnd, temp);
  return(temp);
}


static const char INPUT_F_ERR_TITLE [] = "Invalid float number";
static const char INPUT_F_ERRMSG[]     = "You have entered an invalid float number!\n"
                                         "The previous value will be restored.\n"
                                         "Example: 1.234\n";

float GetDlgFloat (CWnd * pCWnd, float oldval) {
  WORD n;
  float f;
  char lbuf[100];
  
  n = pCWnd->GetWindowText (lbuf, 100);
  lbuf[n] = '\0';  /* terminate string */
  n = sscanf(lbuf, "%f", &f);
  if (n != 1) {
    MessageBeep(MB_ICONEXCLAMATION);
    pCWnd->MessageBox(INPUT_F_ERRMSG, INPUT_F_ERR_TITLE, MB_OK | MB_ICONSTOP); 
    sprintf (lbuf, "%1.4f", oldval);
    pCWnd->SetWindowText (lbuf);
    return((float)-1.9876e-36);
  }
  sprintf (lbuf, "%1.4f", f);
  pCWnd->SetWindowText (lbuf);
  return(f);
}


// This function handles a byte input in a dialog
void HandleByteInput(CWnd * pCWnd, AGSIADDR sfr) {
  DWORD oldval, oldvalp, tmp;

  Agsi.ReadSFR(sfr, &oldval, &oldvalp, 0xFF);
  tmp = GetDlg8BNumber (pCWnd, oldval);
  if (tmp != -1) {
    Agsi.WriteSFR(sfr, tmp, 0xFF);
	Agsi.UpdateWindows();
  }
}

// This function handles a word input in a dialog
void HandleWordInput(CWnd * pCWnd, AGSIADDR sfrl, AGSIADDR sfrh) {
  DWORD oldval, tmp, tmpp;

  Agsi.ReadSFR(sfrl, &tmp, &tmpp, 0xFF);
  oldval = tmp;
  Agsi.ReadSFR(sfrh, &tmp, &tmpp, 0xFF);
  oldval += tmp << 8;
  tmp = GetDlg16BNumber (pCWnd, oldval);
  if (tmp != -1) {
    Agsi.WriteSFR(sfrl, tmp, 0xFF);
    Agsi.WriteSFR(sfrh, tmp >> 8, 0xFF);
	Agsi.UpdateWindows();
  }
}

// This function handles a 3 byte input in a dialog
void Handle3ByteInput(CWnd * pCWnd, AGSIADDR sfrl, AGSIADDR sfrh, AGSIADDR sfrp) {
  DWORD oldval, tmp, tmpp;

  Agsi.ReadSFR(sfrl, &tmp, &tmpp, 0xFF);
  oldval = tmp;
  Agsi.ReadSFR(sfrh, &tmp, &tmpp, 0xFF);
  oldval += tmp << 8;
  Agsi.ReadSFR(sfrp, &tmp, &tmpp, 0xFF);
  oldval += tmp << 16;
  tmp = GetDlg24BNumber (pCWnd, oldval);
  if (tmp != -1) {
    Agsi.WriteSFR(sfrl, tmp, 0xFF);
    Agsi.WriteSFR(sfrh, tmp >> 8, 0xFF);
    Agsi.WriteSFR(sfrp, tmp >> 16, 0xFF);
	Agsi.UpdateWindows();
  }
}

// This function handles a float number input in a dialog
void HandleFloatInput(CWnd * pCWnd, AGSIVTR vtr) {
  union fv tmp;
  float result;

  Agsi.ReadVTR(vtr, &tmp.DW);
  result = GetDlgFloat (pCWnd, tmp.f);
  if (result != -1.9876e-36) {
	tmp.f = result;
    Agsi.WriteVTR(vtr, tmp.DW);
  }
}

// This function is intended to be called when a checkbox is clicked (SFR bit)
void WriteBit(AGSIADDR sfr, DWORD mask, DWORD set) {
  DWORD value;

  value = (set == 0) ? 0 : 0xFF;
  Agsi.WriteSFR(sfr, value, mask);
  Agsi.UpdateWindows();
}

// This function is intended to be called when a checkbox is clicked (VTR bit)
void WriteBitVTR(AGSIVTR vtr, DWORD mask, DWORD set) {
  DWORD value;	

  Agsi.ReadVTR(vtr, &value);
  if (set) value |= mask;
  else value &= ~mask;
  Agsi.WriteVTR(vtr, value);
  Agsi.UpdateWindows();
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -