[haiku-commits] haiku: hrev44217 - src/tools

  • From: kallisti5@xxxxxxxxxxx
  • To: haiku-commits@xxxxxxxxxxxxx
  • Date: Fri, 1 Jun 2012 18:02:24 +0200 (CEST)

hrev44217 adds 1 changeset to branch 'master'
old head: c84fd0f0fecfe6a153d57e7e9aa67206712b1b81
new head: d995d3c634421f74061bcd6225e3ee9fb25f7194

----------------------------------------------------------------------------

d995d3c: intelcpuid: A small tool for OS.h
  
  * Takes an Intel CPUID and converts it to a number for OS.h

                          [ Alexander von Gluck IV <kallisti5@xxxxxxxxxxx> ]

----------------------------------------------------------------------------

Revision:    hrev44217
Commit:      d995d3c634421f74061bcd6225e3ee9fb25f7194
URL:         http://cgit.haiku-os.org/haiku/commit/?id=d995d3c
Author:      Alexander von Gluck IV <kallisti5@xxxxxxxxxxx>
Date:        Fri Jun  1 16:01:29 2012 UTC

----------------------------------------------------------------------------

1 file changed, 96 insertions(+)
src/tools/intelcpuid.c |   96 ++++++++++++++++++++++++++++++++++++++++++++

----------------------------------------------------------------------------

diff --git a/src/tools/intelcpuid.c b/src/tools/intelcpuid.c
new file mode 100644
index 0000000..44e3e4c
--- /dev/null
+++ b/src/tools/intelcpuid.c
@@ -0,0 +1,96 @@
+/*
+ * Copyright 2012 Haiku, Inc. All rights reserved.
+ * Distributed under the terms of the MIT License.
+ *
+ * Authors:
+ *             Alexander von Gluck, kallisti5@xxxxxxxxxxx
+ */
+
+/*
+ * Pass an Intel CPUID in hex, and get out a CPUID for OS.h
+ */
+
+
+#include <stdlib.h>
+#include <stdio.h>
+#include <string.h>
+
+
+#define INTEL_VENDOR 0x100000
+
+#define EXT_FAMILY_MASK 0xF00000
+#define EXT_MODEL_MASK 0x0F0000
+#define FAMILY_MASK            0x000F00
+#define MODEL_MASK             0x0000F0
+#define STEPPING_MASK  0x00000F
+
+
+// Converts a hexadecimal string to integer
+static int xtoi(const char* xs, unsigned int* result)
+{
+       size_t szlen = strlen(xs);
+       int i, xv, fact;
+
+       if (szlen > 0) {
+               // Converting more than 32bit hexadecimal value?
+               if (szlen>8) return 2; // exit
+
+               // Begin conversion here
+               *result = 0;
+               fact = 1;
+
+               // Run until no more character to convert
+               for (i = szlen - 1; i>=0 ;i--) {
+                       if (isxdigit(*(xs+i))) {
+                               if (*(xs+i)>=97) {
+                                       xv = ( *(xs+i) - 97) + 10;
+                               } else if ( *(xs+i) >= 65) {
+                                       xv = (*(xs+i) - 65) + 10;
+                               } else {
+                                       xv = *(xs+i) - 48;
+                               }
+                               *result += (xv * fact);
+                               fact *= 16;
+                       } else {
+                               // Conversion was abnormally terminated
+                               // by non hexadecimal digit, hence
+                               // returning only the converted with
+                               // an error value 4 (illegal hex character)
+                               return 4;
+                       }
+               }
+       }
+
+       // Nothing to convert
+       return 1;
+}
+
+
+int
+main(int argc, char *argv[])
+{
+       if (argc != 2) {
+               printf("Provide the Intel cpuid in hex, and you will get how we 
id it\n");
+               printf("usage: intelcpuid <cpuid_hex>\n");
+               return 1;
+       }
+
+       unsigned int cpuid;
+       xtoi(argv[1], &cpuid);
+
+       printf("cpuid: 0x%X\n", cpuid);
+
+       unsigned int extFam = (cpuid & EXT_FAMILY_MASK) >> 20;
+       unsigned int extMod = (cpuid & EXT_MODEL_MASK) >> 16;
+       unsigned int family = (cpuid & FAMILY_MASK) >> 8;
+       unsigned int model = (cpuid & MODEL_MASK) >> 4;
+       unsigned int stepping = (cpuid & STEPPING_MASK);
+
+       // Haiku INTEL cpuid format: VVEFEM
+       unsigned int intelHaiku = INTEL_VENDOR + ((extFam & 0xF) << 12)
+               + (family << 8) + (extMod << 4) + model;
+
+       printf("Haiku CPUID: 0x%lX\n", intelHaiku);
+
+       return 0;
+}


Other related posts: