xref: /haiku/src/tools/cpuidtool.c (revision 54066ddbb70fa4b8f2344300f91dd1c69a4e1d99)
1 /*
2  * Copyright 2012 Haiku, Inc. All rights reserved.
3  * Distributed under the terms of the MIT License.
4  *
5  * Authors:
6  *		Alexander von Gluck, kallisti5@unixzen.com
7  */
8 
9 /*
10  * Pass a standard CPUID in hex, and get out a CPUID for cpu_type.h
11  */
12 
13 
14 #include <stdio.h>
15 #include <stdlib.h>
16 #include <string.h>
17 
18 
19 #define EXT_FAMILY_MASK 0xF00000
20 #define EXT_MODEL_MASK	0x0F0000
21 #define FAMILY_MASK		0x000F00
22 #define MODEL_MASK		0x0000F0
23 #define STEPPING_MASK	0x00000F
24 
25 
26 // Converts a hexadecimal string to integer
27 static int
xtoi(const char * xs,unsigned int * result)28 xtoi(const char* xs, unsigned int* result)
29 {
30 	size_t szlen = strlen(xs);
31 	int i;
32 	int xv;
33 	int fact;
34 
35 	if (szlen > 0) {
36 		// Converting more than 32bit hexadecimal value?
37 		if (szlen > 8)
38 			return 2;
39 
40 		// Begin conversion here
41 		*result = 0;
42 		fact = 1;
43 
44 		// Run until no more character to convert
45 		for (i = szlen - 1; i>=0; i--) {
46 			if (isxdigit(*(xs + i))) {
47 				if (*(xs + i) >= 97)
48 					xv = (*(xs + i) - 97) + 10;
49 				else if (*(xs + i) >= 65)
50 					xv = (*(xs + i) - 65) + 10;
51 				else
52 					xv = *(xs + i) - 48;
53 
54 				*result += (xv * fact);
55 				fact *= 16;
56 			} else {
57 				// Conversion was abnormally terminated
58 				// by non hexadecimal digit, hence
59 				// returning only the converted with
60 				// an error value 4 (illegal hex character)
61 				return 4;
62 			}
63 		}
64 	}
65 
66 	// Nothing to convert
67 	return 1;
68 }
69 
70 
71 int
main(int argc,char * argv[])72 main(int argc, char *argv[])
73 {
74 	if (argc != 2) {
75 		printf("Provide the cpuid in hex, and you will get how we id it\n");
76 		printf("usage: cpuidhaiku <cpuid_hex>\n");
77 		return 1;
78 	}
79 
80 	unsigned int cpuid = 0;
81 	xtoi(argv[1], &cpuid);
82 
83 	printf("cpuid: 0x%X\n", cpuid);
84 
85     int family = ((cpuid >> 8) & 0xf) | ((cpuid >> 16) & 0xff0);
86     int model = ((cpuid >> 4) & 0xf) | ((cpuid >> 12) & 0xf0);
87     int stepping = cpuid & 0xf;
88 
89 	printf("Haiku CPUID: Family: 0x%x, Model: 0x%x, Stepping: 0x%x\n", family,
90 		model, stepping);
91 
92 	return 0;
93 }
94