• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

Delphi编程--使用CPUID指令获取CPU信息

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

unit CPU;

interface

uses Types;

const

  //Version Information bitmask
  BITMASK_ExtendedFamilyID=$0FF00000;
  BITMASK_ExtendedModelID= $000F0000;
  BITMASK_ProcessorType=   $00003000;
  BITMASK_FamilyID=        $00000F00;
  BITMASK_ModelID=         $000000F0;
  BITMASK_SteppingID=      $0000000F;

  //Processor Type
  ProcessorType_OriginalOEMProcessor=    $00000000;
  ProcessorType_IntelOverDriveProcessor= $00001000;
  ProcessorType_DualProcessor=           $00002000;
  ProcessorType_IntelReserved=           $00003000;

type

  //register used in cpuid instruction
  TCPUIDRegister=packed record
    EAX:DWORD;
    case Integer of
    0://register
    (
      EBX:DWORD;
      ECX:DWORD;
      EDX:DWORD;
    );
    1://version information string
    (
      VIS1:array[1..4] of Char;
      VIS2:array[1..4] of Char;
      VIS3:array[1..4] of Char;
    );
  end;

  PCPUIDRegister=^TCPUIDRegister;

  //basic cupid information
  TCPUIDBasicInformation=packed record
    //EAX=0
    HighestBasicProcessorInformationValue:Cardinal;
    VendorIdentificationString:string[12];
    //EAX=1
    ExtendedFamileyID,
    ExtendedModelID,
    ProcessorType,
    FamilyID,
    ModelID,
    SteppingID:DWORD;
  end;

  PCPUIDBasicInformation=^TCPUIDBasicInformation;

  TCPUInfo=class
  private
    fSupportForCPUID:Boolean;
  private
    function CPUID(Value:DWORD):TCPUIDRegister;
    function Get_CPUIDBasicInformation:TCPUIDBasicInformation;
  public
    constructor Create;
  public
    property SupportForCPUID:Boolean read fSupportForCPUID;
    property CPUIDBasicInformation:TCPUIDBasicInformation read Get_CPUIDBasicInformation;
  end;

implementation

uses Dialogs,SysUtils;

{ TCPUInfo }

function TCPUInfo.CPUID(Value:DWORD): TCPUIDRegister;
var
  VEAX,VEBX,VECX,VEDX:DWORD;
begin
  asm
    pushad
    mov eax,Value
    cpuid
    mov VEAX,eax
    mov VEBX,ebx
    mov VECX,ecx
    mov VEDX,edx
    popad
  end;
  with Result do
  begin
    EAX:=VEAX;
    EBX:=VEBX;
    ECX:=VECX;
    EDX:=VEDX;
  end;
end;

constructor TCPUInfo.Create;
const
  EF_ID:DWORD=$200000;
var
  EFID:DWORD;
begin
  fSupportForCPUID:=False;
  asm
    //save current register
    pushad
    //move old eflags to ebx
    pushfd
    pop eax
    //move old eflags to ebx
    mov ebx,eax
    //revert EF_ID
    xor eax,EF_ID
    push eax
    popfd
    //move new eflags to eax
    pushfd
    pop eax
    //test EF_ID
    xor eax,ebx
    mov EFID,eax
    //restore register
    popad
  end;
  if (EFID xor EF_ID)=0 then fSupportForCPUID:=True;
end;

function TCPUInfo.Get_CPUIDBasicInformation: TCPUIDBasicInformation;
var
  CPUIDRegister:TCPUIDRegister;
begin
  CPUIDRegister:=CPUID(0);
  with CPUIDRegister,Result do
  begin
    HighestBasicProcessorInformationValue:=EAX;
    VendorIdentificationString:=VIS1+VIS3+VIS2;
  end;
  CPUIDRegister:=CPUID(1);
  with CPUIDRegister,Result do
  begin
    ExtendedFamileyID:=EAX and BITMASK_ExtendedFamilyID;
    ExtendedModelID:=EAX and BITMASK_ExtendedModelID;
    ProcessorType:=EAX and BITMASK_ProcessorType;
    FamilyID:=EAX and BITMASK_FamilyID;
    ModelID:=EAX and BITMASK_ModelID;
    SteppingID:=EAX and BITMASK_SteppingID;
  end;
end;

end.


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
matlab矩阵的表示和简单操作发布时间:2022-07-18
下一篇:
Delphi中window消息截获的实现方式(1)发布时间:2022-07-18
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap