Re: [AD] CPU detection for Macintosh

[ Thread Index | Date Index | More lists.liballeg.org/allegro-developers Archives ]


I've attached a slightly reworked version of Pete's original patch. Most of 
the changes are are in the documentation, although I took the liberty of 
setting the CPU_FPU flag in the cpu_capabilities.

The patch adds a bunch of processor family/model defines to system.h rather 
than forcing the user to look at the documentation for a hint at what a 
specific number might mean. That said, I'm well aware that the list is 
still incomplete, although I've brought it a bit up-to-date. I don't think 
this information is referenced as much as the cpu_capabilities though, so 
I don't think it matters too much.

The test programme now also displays the CPU vendor string.

Evert
? src/macosx/pcpu.m
Index: makefile.osx
===================================================================
RCS file: /cvsroot/alleg/allegro/makefile.osx,v
retrieving revision 1.44
diff -u -r1.44 makefile.osx
--- makefile.osx	11 Mar 2005 15:57:00 -0000	1.44
+++ makefile.osx	11 Mar 2005 21:38:05 -0000
@@ -147,7 +147,8 @@
 ALLEGRO_SRC_MACOSX_FILES += src/macosx/main.m
 endif
 
-OBJECT_LIST = $(COMMON_OBJECTS) $(C_OBJECTS) $(basename $(notdir $(ALLEGRO_SRC_MACOSX_FILES)))
+# PH: need to kill including ccpu.o and use pcpu.o instead 
+OBJECT_LIST = $(COMMON_OBJECTS) $(subst ccpu,pcpu,$(C_OBJECTS)) $(basename $(notdir $(ALLEGRO_SRC_MACOSX_FILES)))
 
 STATIC_LIBRARIES = -framework Cocoa -framework Carbon -framework IOKit -framework System -framework CoreAudio -framework AudioUnit -framework AudioToolbox -framework QuickTime
 ifdef STATICLINK
Index: docs/src/allegro._tx
===================================================================
RCS file: /cvsroot/alleg/allegro/docs/src/allegro._tx,v
retrieving revision 1.301
diff -u -r1.301 allegro._tx
--- docs/src/allegro._tx	26 Feb 2005 21:14:48 -0000	1.301
+++ docs/src/allegro._tx	11 Mar 2005 21:38:26 -0000
@@ -380,29 +380,50 @@
 @@extern char @cpu_vendor[];
 @xref check_cpu, cpu_family, cpu_model, cpu_capabilities, allegro_init
 @shortdesc Contains the CPU vendor name.
-   Contains the CPU vendor name, if known (empty string on non-Intel 
-   platforms). You can read this variable after you have called check_cpu()
+   Contains the CPU vendor name, if known. On Mac OSX systems this returns
+   the PPC subtype name. On other platforms, this may be an empty string.
+   You can read this variable after you have called check_cpu()
    (which is automatically called by allegro_init()).
 
 @@extern int @cpu_family;
 @xref check_cpu, cpu_vendor, cpu_model, cpu_capabilities, allegro_init
-@domain.hid Contains the Intel CPU type.
-   Contains the Intel CPU type, where applicable: 3=386, 4=486, 5=Pentium, 
-   6=PPro, etc. You can read this variable after you have called check_cpu()
+@shortdesc Contains the CPU type.
+   Contains the Intel type, where applicable. Allegro defines the following
+   CPU family types:
+<textblock>
+    CPU_FAMILY_UNKNOWN  - The type of processor is unknown
+    CPU_FAMILY_I386     - The processor is an Intel-compatible 386
+    CPU_FAMILY_I486     - The processor is an Intel-compatible 486
+    CPU_FAMILY_PENTIUM  - The processor is a Pentium or equivalent
+    CPU_FAMILY_PPRO     - The processor is a Pentium Pro, II, III or equivalent
+    CPU_FAMILY_ITANIUM  - The processor is an Itanium processor
+    CPU_FAMILY_POWERPC  - The processor is a PowerPC processor
+    CPU_FAMILY_EXTENDED - The processor type needs to be read from the cpu_model</endblock>
+   You can read this variable after you have called check_cpu()
    (which is automatically called by allegro_init()).
 
 @@extern int @cpu_model;
 @xref check_cpu, cpu_vendor, cpu_family, cpu_capabilities, allegro_init
 @shortdesc Contains the Intel CPU submodel.
-   Contains the Intel CPU submodel, where applicable. On a 486 
-   (cpu_family=4), zero or one indicates a DX chip, 2 an SX, 3 a 487 (SX) or 
-   486 DX, 4 an SL, 5 an SX2, 7 a DX2 write-back enhanced, 8 a DX4 or DX4 
-   overdrive, 14 a Cyrix, and 15 is unknown. On a Pentium chip 
-   (cpu_family=5), 1 indicates a Pentium (510\66, 567\66), 2 is a Pentium 
-   P54C, 3 is a Pentium overdrive processor, 5 is a Pentium overdrive for 
-   IntelDX4, 14 is a Cyrix, and 15 is unknown. You can read this variable
-   after you have called check_cpu() (which is automatically called by
-   allegro_init()).
+   Contains the CPU submodel, where applicable. Allegro defines at least the 
+   following CPU family types (see include/allegro/system.h for a more 
+   complete list):
+<textblock>   
+   CPU_FAMILY_PENTIUM:
+      CPU_MODEL_PENTIUM, CPU_MODEL_K5, CPU_MODEL_K6
+
+   CPU_FAMILY_PPRO:
+      CPU_MODEL_PENTIUMPRO, CPU_MODEL_PENTIUMII, CPU_MODEL_PENTIUMIIIKATMAI,
+      CPU_MODEL_PENTIUMIIICOPPERMINE, CPU_MODEL_ATHLON, CPU_MODEL_DURON
+
+   CPU_FAMILY_EXTENDED:
+      CPU_MODEL_PENTIUMIV, CPU_MODEL_XEON, CPU_MODEL_ATHLON64, CPU_MODEL_OPTERON
+
+   CPU_FAMILY_POWERPC:
+      CPU_MODEL_POWERPC_x, for x=601-604, 620, 750, 7400, 7450</endblock>
+   You can read this variable after you have called check_cpu() (which is 
+   automatically called by allegro_init()). Make sure you check teh cpu_family
+   and cpu_vendor so you know which models make sense to check.
 
 @@extern int @cpu_capabilities;
 @xref check_cpu, cpu_vendor, cpu_family, cpu_model, cpu_capabilities,
@@ -415,7 +436,7 @@
 		     available. If this is set, then all Allegro CPU
 		     variables are 100% reliable, otherwise there
 		     may be some mistakes.
-      CPU_FPU      - An x87 FPU is available.
+      CPU_FPU      - An FPU is available.
       CPU_IA64     - Running on Intel 64 bit CPU
       CPU_AMD64    - Running on AMD 64 bit CPU
       CPU_MMX      - Intel MMX  instruction set is available.
Index: include/allegro/system.h
===================================================================
RCS file: /cvsroot/alleg/allegro/include/allegro/system.h,v
retrieving revision 1.14
diff -u -r1.14 system.h
--- include/allegro/system.h	6 Jan 2005 12:10:35 -0000	1.14
+++ include/allegro/system.h	11 Mar 2005 21:38:27 -0000
@@ -98,7 +98,7 @@
 
 AL_FUNC(void, check_cpu, (void));
 
-/* CPU Capabilities flags - set to 0 on non x86 capable chips */
+/* CPU Capabilities flags for x86 capable chips */
 #define CPU_ID       0x0001
 #define CPU_FPU      0x0002
 #define CPU_MMX      0x0004
@@ -112,6 +112,79 @@
 #define CPU_IA64     0x0400
 #define CPU_SSE3     0x0800
 
+/* CPU families - PC */
+#define CPU_FAMILY_UNKNOWN  0
+#define CPU_FAMILY_I386     3
+#define CPU_FAMILY_I486     4
+#define CPU_FAMILY_PENTIUM  5
+#define CPU_FAMILY_PPRO     6
+#define CPU_FAMILY_ITANIUM  7
+#define CPU_FAMILY_EXTENDED 15
+/* CPUID only returns 15 bits, we need extra information from the CPU */
+/*  model to identify Pentium IV, Xeon and Athlon 64 processors. */
+
+/* CPU families - Power PC */
+#define CPU_FAMILY_POWERPC  18
+
+/* CPU models - PC */
+/* 486 */
+#define CPU_MODEL_I486DX    0
+#define CPU_MODEL_I486DX50  1
+#define CPU_MODEL_I486SX    2
+#define CPU_MODEL_I487SX    3
+#define CPU_MODEL_I486SL    4
+#define CPU_MODEL_I486SX2   5
+#define CPU_MODEL_I486DX2   7
+#define CPU_MODEL_I486DX4   8
+
+/* Pentium/586 */
+#define CPU_MODEL_PENTIUM              1
+#define CPU_MODEL_PENTIUMP54C          2
+#define CPU_MODEL_PENTIUMOVERDRIVE     3
+#define CPU_MODEL_PENTIUMOVERDRIVEDX4  4
+#define CPU_MODEL_CYRIX                14
+#define CPU_MODEL_UNKNOWN              15
+
+/* AMD/586 */
+#define CPU_MODEL_K5                   0
+#define CPU_MODEL_K6                   6
+
+/* Pentium Pro/Pentium II/Pentium III/686 */
+#define CPU_MODEL_PENTIUMPROA          0
+#define CPU_MODEL_PENTIUMPRO           1
+#define CPU_MODEL_PENTIUMIIKLAMATH     3
+#define CPU_MODEL_PENTIUMII            5
+#define CPU_MODEL_CELERON              6
+#define CPU_MODEL_PENTIUMIIIKATMAI     7
+#define CPU_MODEL_PENTIUMIIICOPPERMINE 8
+#define CPU_MODEL_PENTIUMIIIMOBILE     9
+
+/* AMD/686 */
+#define CPU_MODEL_ATHLON               2
+#define CPU_MODEL_DURON                3
+
+
+/* Information when CPU_FAMILY is 15 */
+#define CPU_MODEL_PENTIUMIV            0
+#define CPU_MODEL_XEON                 2
+
+#define CPU_MODEL_ATHLON64             4
+#define CPU_MODEL_OPTERON              5
+
+/* Information for Power PC processors */
+/* these defines are taken from <mach-o/machine.h> */
+#define CPU_MODEL_POWERPC_601	       1
+#define CPU_MODEL_POWERPC_602	       2
+#define CPU_MODEL_POWERPC_603	       3
+#define CPU_MODEL_POWERPC_603e	       4
+#define CPU_MODEL_POWERPC_603ev	       5
+#define CPU_MODEL_POWERPC_604	       6
+#define CPU_MODEL_POWERPC_604e	       7
+#define CPU_MODEL_POWERPC_620	       8
+#define CPU_MODEL_POWERPC_750	       9
+#define CPU_MODEL_POWERPC_7400	       10
+#define CPU_MODEL_POWERPC_7450	       11
+
 AL_ARRAY(char, cpu_vendor);
 AL_VAR(int, cpu_family);
 AL_VAR(int, cpu_model);
Index: src/i386/icpu.c
===================================================================
RCS file: /cvsroot/alleg/allegro/src/i386/icpu.c,v
retrieving revision 1.8
diff -u -r1.8 icpu.c
--- src/i386/icpu.c	10 Mar 2005 18:32:55 -0000	1.8
+++ src/i386/icpu.c	11 Mar 2005 21:38:28 -0000
@@ -114,7 +114,8 @@
 	 _i_get_cpuid_info(1, reg);
 
 	 cpu_family = (reg[0] & 0xF00) >> 8;
-	 cpu_model = (reg[0] & 0xF0) >> 4;  //Note: Pentium 4 = 0xF -> needs changing.
+	 cpu_model = (reg[0] & 0xF0) >> 4;
+         /* Note: cpu_family = 15 can mean a Pentium IV, Xeon, AMD64, Opteron... */
 
 	 cpu_capabilities |= (reg[3] & 1 ? CPU_FPU : 0);
 	 cpu_capabilities |= (reg[3] & 0x800000 ? CPU_MMX : 0);
Index: tests/test.c
===================================================================
RCS file: /cvsroot/alleg/allegro/tests/test.c,v
retrieving revision 1.34
diff -u -r1.34 test.c
--- tests/test.c	10 Mar 2005 18:32:55 -0000	1.34
+++ tests/test.c	11 Mar 2005 21:38:31 -0000
@@ -4446,46 +4446,51 @@
    #if defined ALLEGRO_I386 || defined ALLEGRO_AMD64
 
       #ifdef ALLEGRO_I386
-         sprintf(cpu_specs, "CPU family: %d86", cpu_family);
+         sprintf(cpu_specs, "CPU family: %d86 (%s)", cpu_family, cpu_vendor?cpu_vendor:"unknown");
       #else
-         sprintf(cpu_specs, "CPU family: AMD64");
+         sprintf(cpu_specs, "CPU family: AMD64 (%s)", cpu_vendor);
       #endif
 
-      if (cpu_capabilities & CPU_ID)
-	 strcat(cpu_specs, " / cpuid");
+   #else
+      #if defined ALLEGRO_MACOSX
+         sprintf(cpu_specs, "CPU family: %s (%s)", 
+                 cpu_family==CPU_FAMILY_POWERPC ? "PowerPC" : "Other", 
+                 cpu_vendor);
+      #else
+         strcpy(cpu_specs, "Non-x86 CPU (very cool)");
+      #endif
+   #endif
 
-      if (cpu_capabilities & CPU_FPU)
-	 strcat(cpu_specs, " / FPU");
+   if (cpu_capabilities & CPU_ID)
+      strcat(cpu_specs, " / cpuid");
 
-      if (cpu_capabilities & CPU_CMOV)
-	 strcat(cpu_specs, " / cmov");
+   if (cpu_capabilities & CPU_FPU)
+      strcat(cpu_specs, " / FPU");
 
-      if (cpu_capabilities & CPU_SSE)
-	 strcat(cpu_specs, " / SSE");
-	 
-      if (cpu_capabilities & CPU_SSE2)
-	 strcat(cpu_specs, " / SSE2");
-	 
-      if (cpu_capabilities & CPU_SSE3)
-	 strcat(cpu_specs, " / SSE3");
-	 
-      if (cpu_capabilities & CPU_MMX)
-	 strcat(cpu_specs, " / MMX");
+   if (cpu_capabilities & CPU_CMOV)
+      strcat(cpu_specs, " / cmov");
 
-      if (cpu_capabilities & CPU_MMXPLUS)
-	 strcat(cpu_specs, " / MMX+");
+   if (cpu_capabilities & CPU_SSE)
+      strcat(cpu_specs, " / SSE");
 
-      if (cpu_capabilities & CPU_3DNOW)
-	 strcat(cpu_specs, " / 3DNow!");
+   if (cpu_capabilities & CPU_SSE2)
+      strcat(cpu_specs, " / SSE2");
 
-      if (cpu_capabilities & CPU_ENH3DNOW)
-	 strcat(cpu_specs, " / Enh 3DNow!");
+   if (cpu_capabilities & CPU_SSE3)
+      strcat(cpu_specs, " / SSE3");
 
-   #else
+   if (cpu_capabilities & CPU_MMX)
+      strcat(cpu_specs, " / MMX");
 
-      strcpy(cpu_specs, "Non-x86 CPU (very cool)");
+   if (cpu_capabilities & CPU_MMXPLUS)
+      strcat(cpu_specs, " / MMX+");
+
+   if (cpu_capabilities & CPU_3DNOW)
+      strcat(cpu_specs, " / 3DNow!");
+
+   if (cpu_capabilities & CPU_ENH3DNOW)
+      strcat(cpu_specs, " / Enh 3DNow!");
 
-   #endif
 
    cpu_has_capabilities = cpu_capabilities;
 
/*         ______   ___    ___ 
 *        /\  _  \ /\_ \  /\_ \ 
 *        \ \ \L\ \\//\ \ \//\ \      __     __   _ __   ___ 
 *         \ \  __ \ \ \ \  \ \ \   /'__`\ /'_ `\/\`'__\/ __`\
 *          \ \ \/\ \ \_\ \_ \_\ \_/\  __//\ \L\ \ \ \//\ \L\ \
 *           \ \_\ \_\/\____\/\____\ \____\ \____ \ \_\\ \____/
 *            \/_/\/_/\/____/\/____/\/____/\/___L\ \/_/ \/___/
 *                                           /\____/
 *                                           \_/__/
 *
 *      PowerPC CPU detection routines, by Peter Hull.
 *
 *      See readme.txt for copyright information.
 */
#include <allegro.h>
#include <allegro/internal/aintern.h>
#include <mach-o/arch.h>

void check_cpu() {
  const NXArchInfo* info=NXGetLocalArchInfo();
  cpu_family=info->cputype;
  cpu_model=info->cpusubtype;
  cpu_capabilities=CPU_ID|CPU_FPU; /* confident that this info is correct */
  do_uconvert(info->name, U_ASCII, cpu_vendor, U_CURRENT,
	      _AL_CPU_VENDOR_SIZE);
}


Mail converted by MHonArc 2.6.19+ http://listengine.tuxfamily.org/