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

📄 cpuinfo.c

📁 DOS下读取CPU信息,编译环境Tubor C 3.0
💻 C
字号:
#include "stdio.h"
#include "stdlib.h"
#include "malloc.h"
#include "string.h"
#include "bios.h"
#include "graph.h"

#include "ptypes.h"				
#include "global.h"

#include "dbg.h"

void puthz(char *,int ,int ,int );
BOOL GetYes(void);
BOOL GetStrValue(char *,char *,char* );
long GetCPUFrequence(void);
#define ROW 1 //纵坐标放大倍数
#define COL 1 //横坐标放大倍数

BOOL GetFun(int , char* );
#define WRITE 6 
#define CMP   8 


typedef struct _CPU{
    long    Step : 4;
    long    Model : 4;
    long    Family : 4;
    long    Type : 2;
    long    reserved : 2;
    long    Model_ex : 4;
	long    Family_ex : 8;
	long    reserved1 : 4;
}CPU, *pCPU;

#define TESTNUMS 36
long GetCPUFrequence(void)
{
	int	i;
	long Total=0,Total1=0,tmp=0;
	long high=0,low=0;
	long high1=0,low1=0;
	long max=0,cpu[TESTNUMS]={0,};
	
	for(i=0;i<TESTNUMS;i++)
	{
retest:	
	_bios_timeofday(_TIME_GETCLOCK, &Total);
	for(;;)
	{
		_bios_timeofday(_TIME_GETCLOCK, &Total1);
		if(Total1 > Total)
			break;
	}
	_bios_timeofday(_TIME_GETCLOCK, &Total);
	_asm{
//		dw 310fh;
		RDTSC;
		mov low,eax;
		mov high,edx;
	}
	for(;;)
	{
		_bios_timeofday(_TIME_GETCLOCK, &Total1);
		if(Total1 > Total)
			break;
	}
	_asm{
//		dw 310fh;
		RDTSC;
		mov low1,eax;
		mov high1,edx;
	};
//	if(high != high1)
//		goto retest;
	tmp=(high1-high)/(0xffffffff/1000000);
	cpu[i]= low1-low;
//	cpu[i] = cpu[i]*18.2;
	cpu[i] = cpu[i]/1000000;
	cpu[i] += tmp;
	cpu[i] *= 18.2; 
//	if(cpu[i]>max)
//		max=cpu[i];
	}

	max=0;
	for(i=0;i<TESTNUMS;i++)
	{
		max+=cpu[i];
	}
	return max/TESTNUMS;
}

long GetCpuinfo(void)
{
	long	Value;
	int		command=1;
	char OEMString[13];
	_asm {
		mov eax,0;
		cpuid;
		mov DWORD PTR OEMString,ebx;
		mov DWORD PTR OEMString+4,edx;
		mov DWORD PTR OEMString+8,ecx;
		mov BYTE PTR OEMString+12,0;
	}
	if(memcmp(OEMString,"AuthenticAMD",13) == 0)
		command = 0x80000001;
	_asm {
		mov eax,1;
		cpuid;
		mov Value,eax;
	};
	return Value;
} 

BOOL GetCpuName(char *name)
{
	long	Value,Value_eax,Value_ebx,Value_ecx,Value_edx;
	int		command=1,i;
	char    tmp[49];
	
	_asm {
		mov eax,0x80000000;
		cpuid;
		mov Value,eax;
	};
	if(!Value&0x80000000)
		return FALSE;
	if(Value<0x80000004)
		return FALSE;
	_asm {
		mov eax,0x80000002;
		cpuid;
		mov Value_eax,eax;
		mov Value_ebx,ebx;
		mov Value_ecx,ecx;
		mov Value_edx,edx;
	};
        *((long *)(&(name[0]))) = Value_eax;
        *((long *)(&(name[4]))) = Value_ebx;
        *((long *)(&(name[8]))) = Value_ecx;
        *((long *)(&(name[12]))) = Value_edx;

	_asm {
		mov eax,0x80000003;
		cpuid;
		mov Value_eax,eax;
		mov Value_ebx,ebx;
		mov Value_ecx,ecx;
		mov Value_edx,edx;
	};
        *((long *)(&(name[16]))) = Value_eax;
        *((long *)(&(name[20]))) = Value_ebx;
        *((long *)(&(name[24]))) = Value_ecx;
        *((long *)(&(name[28]))) = Value_edx;
	_asm {
		mov eax,0x80000004;
		cpuid;
		mov Value_eax,eax;
		mov Value_ebx,ebx;
		mov Value_ecx,ecx;
		mov Value_edx,edx;
	};
        *((long *)(&(name[32]))) = Value_eax;
        *((long *)(&(name[36]))) = Value_ebx;
        *((long *)(&(name[40]))) = Value_ecx;
        *((long *)(&(name[44]))) = Value_edx;
		memset(tmp,0,49);
		for(i=0;i<48;i++)
		{
			if(name[i] != 0x20)
			{
				memcpy(tmp,&(name[i]),48-i);
				break;
			}
		}
		memset(name,0,48);
		memcpy(name,tmp,48);
	return TRUE;
} 
///////////////////////////////////////////////////////////////////////////
// Function main
// This function is the entry of whole utility.
///////////////////////////////////////////////////////////////////////////
int main(int argc, char *argv[])
{
	long cpu=0;
//	CPU		info;
	FILE	*stream;
	char    name[49];
	char	cpu_name[60];
	char	tmp[60];
	char	cpu_frequence[60];
	int		name_valie=0,frequence_valid=0,fun=0;
	BOOL	err=0,notest=0;
	
	fun = GetFun(argc, argv[1]);
	if(!fun)
		return FALSE;
	if(fun == WRITE)
	{
		if((stream = fopen(argv[2],"a")) == NULL)
			return FALSE;
		
		fprintf(stream,"[Processor Information]\n");
		fprintf(stream,"CpuFrequence=%lu\n",GetCPUFrequence());
		if(GetCpuName(name))
			fprintf(stream,"CpuName=%s\n",name);
		fclose(stream);
		return TRUE;
	}
	
	memset(name,0,sizeof(char)*49);
	memset(cpu_name,0,sizeof(char)*60);
	memset(cpu_frequence,0,sizeof(char)*60);
	name_valie = GetStrValue(argv[2],"CpuName\0",cpu_name);
//	printf("CpuName is %s\n",cpu_name);
	frequence_valid = GetStrValue(argv[2],"CpuFrequence\0",cpu_frequence);
//	printf("CpuFrequence is %ld\n",atoi(cpu_frequence));

	if(frequence_valid)
	{
		cpu = GetCPUFrequence();
		if(abs(atoi(cpu_frequence)-cpu)>30)
		{
			_setvideomode(_VRES16COLOR);
			puthz("CPU检测失败!",200,30,12);
			puthz("实测频率:",5,110,14);
			_settextposition(8,20);
			ltoa(cpu,tmp,10);
			_outtext(tmp);
			puthz("标配频率:",5,170,14);
			_settextposition(12,20);
			_outtext(cpu_frequence);
			puthz("按“Y”键允许通过,否则按电源键关机。",60,360,14);
			GetYes();
			_setvideomode(_DEFAULTMODE);
			err++;
		}
	}else{
		notest++;
	}
/*
	cpu = GetCpuinfo();
	*((long *)(&info)) = cpu;
	printf("Step is %u\n",info.Step  );
	printf("Model is %u\n",info.Model&0xf);
	printf("Family is %u\n",info.Family  );
	printf("Type is %u\n",info.Type  );
*/
	if(name_valie)
	{
		if(GetCpuName(name))
		{
			if(memcmp(cpu_name,name,strlen(name))!=0)
			{
				_setvideomode(_VRES16COLOR);
				puthz("CPU类型检测失败!",200,30,12);
				puthz("实测类型:",5,110,14);
				_settextposition(8,20);
				_outtext(name);
				puthz("标配类型:",5,170,14);
				_settextposition(12,20);
				_outtext(cpu_name);
				puthz("按“Y”键允许通过,否则按电源键关机。",60,360,14);
				GetYes();
				_setvideomode(_DEFAULTMODE);
				err++;
			}
		}else{
			notest++;
		}
	}else{
		notest++;
	}
	if(err)
		return 2;
	if(notest)
		return FALSE;
	return TRUE;
}

BOOL GetStrValue(char *filename,char *str,char* value)
{
	int			i,k,isok=0;
	FILE		*stream;
	char		*lpBuffer;
	LONG		len=0;

	stream = fopen(filename,"rb");
	if(stream == NULL)
	{
		printf("Can't open %s\n",filename);
		return FALSE;
	}
	len =  filelength(stream->_handle);
	lpBuffer = (BYTE *)malloc((size_t )len);
	fread(lpBuffer,sizeof(char), (size_t)len,stream);

	for(i=0;i<(len-strlen(str));i++)
	{
		if((memcmp((char *)lpBuffer+i,str,strlen(str))==0 )&&
			(
			(*((char *)lpBuffer+i-1) == 20)||
			(*((char *)lpBuffer+i-1) == 0xa)||
			(i == 0)
			)&&
			(
			(*((char *)lpBuffer+i+strlen(str)) == 20)||
			(*((char *)lpBuffer+i+strlen(str)) == 0x3d)
			)
			)
		{
			int m=0;
			for(k=0;k<(len-strlen(str)-i);k++)
			{
				int j=lpBuffer+i+strlen(str)+1+k;
				if(
					(*((char *)j) == 0x0d)&&
					(*((char *)j+1) == 0x0a)
					)
				{
					isok = 1;
					free(lpBuffer);
					fclose(stream);
					return	TRUE;
				}else{
					value[m]=*((char *)j);
					m++;
				}
			}
		}
	}
	if(isok)
	{
		free(lpBuffer);
		fclose(stream);
		return	TRUE;
	}else{
		free(lpBuffer);
		fclose(stream);
		return	FALSE;
	}
}


void puthz(char *s,int x,int y,int color)
{
	FILE	*hzk;
	char buffer[32]; //buffer用来存储一个汉字
	register m,n,i,j,k;
	unsigned char qh,wh;
	unsigned long offset;
	
	if ((hzk=fopen("hzk16","rb"))==NULL)
		return;

	while(*s)
	{ qh=*(s)-0xa0; //汉字区位码
	wh=*(s+1)-0xa0;
	offset=(94*(qh-1)+(wh-1))*32L;//计算该汉字在字库中偏移量
	fseek(hzk,offset,SEEK_SET);
	fread(buffer,32,1,hzk); //取出汉字32字节的点阵字模存入buffer中(一个汉字)
	_setcolor( color );
	for (i=0;i<16;i++)//将32位字节的点阵按位在屏幕上打印出来(1:打印,0:不打印),显示汉字
		for(n=0;n<ROW;n++)
			for(j=0;j<2;j++)
				for(k=0;k<8;k++)
					for(m=0;m<COL;m++)
						if (((buffer[i*2+j]>>(7-k))&0x1)!=NULL)
							_setpixel(x+8*j*COL+k*COL+m,y+i*ROW+n);
						s+=2; //因为一个汉字内码占用两个字节,所以s必须加2
						x+=30;
	}
	fclose(hzk);
	//getch();
	//closegraph();
}

/////////////////////////////////////////////////////////////////////////////////
// Function GetYesOrNo
// This function get user selection:Y/N
// GetYesOrNo returns TRUE if user press 'y'or'Y', or FALSE if user press 'n'or'N'
//////////////////////////////////////////////////////////////////////////////////
BOOL GetYes(void)
{
//	printf("%s",Message); 
	for(;;){
		fflush(stdin);
		switch(getch()){
		case 'y':
		case 'Y':
			return TRUE;
//		case 'n':
//		case 'N':
//			return FALSE;
		default:
//			printf("Invalid input,please input again.\n");
//			printf("%s",Message); 
			break;
		}
	}
}
	
	
BOOL GetFun(int argc, char* argv)
{
	if((argc!=3)||(strlen(argv)>2))
	{
verinfo:
	printf("赏屯屯屯屯屯屯屯屯屯屯屯屯屯屯屯屯籠n");
	printf("

⌨️ 快捷键说明

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