博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Delphi编程 -- 使用CPUID指令获取CPU信息
阅读量:5314 次
发布时间:2019-06-14

本文共 2802 字,大约阅读时间需要 9 分钟。

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.

转载于:https://www.cnblogs.com/MaxWoods/archive/2008/05/24/1206211.html

你可能感兴趣的文章
OpenGL一个简单的纹理贴图
查看>>
js如何查看元素类型
查看>>
mysql复习
查看>>
A标签跳转链接并修改样式
查看>>
深入Java集合学习系列:ConcurrentHashMap之实现细节
查看>>
Pycharm下将py文件打包成exe文件
查看>>
tomcat一闪而过
查看>>
本周ASP.NET英文技术文章推荐[03/25 - 03/31]
查看>>
本周ASP.NET英文技术文章推荐[09/30- 07/13]:.NET Framework、JSON、Google Analytics、文件上传、GridView、IIS 7、Web开发...
查看>>
详解go语言的array和slice 【二】
查看>>
父元素 高度固定,如何使其中的文字垂直居中?
查看>>
汉三水属国(北地属国、安定属国)
查看>>
JS设置文本框只能是数字和小数点。
查看>>
struts2 标签不能使用EL 表单式
查看>>
spark-2.1.0 集群安装
查看>>
idea-spark-sbt 打包jar
查看>>
hdu 4493 Tutor (水 精度)
查看>>
Linux0.11内核剖析--内核体系结构
查看>>
vi is failed with error "E382: Cannot write, 'buftype' option is set" in Linux
查看>>
pageadmin CMS网站建设教程:站点添加自定义字段
查看>>