fixed logging and more
[retroray] / src / cpuid.c
1 #include <stdio.h>
2 #include <string.h>
3 #include "cpuid.h"
4 #include "logger.h"
5
6 static const char *cpuname(struct cpuid_info *cpu);
7 static const char *cpuvendor(struct cpuid_info *cpu);
8
9 struct cpuid_info cpuid;
10
11 void print_cpuid(struct cpuid_info *cpu)
12 {
13         int i, col, len;
14         static const char *featstr[32] = {
15                 "fpu", "vme", "dbgext", "pse", "tsc", "msr", "pae", "mce",
16                 "cx8", "apic", "?", "sep", "mtrr", "pge", "mca", "cmov",
17                 "pat", "pse36", "psn", "clf", "?", "dtes", "acpi", "mmx",
18                 "fxsr", "sse", "sse2", "ss", "htt", "tm1", "ia64", "pbe"};
19         static const char *feat2str[32] = {
20                 "sse3", "pclmul", "dtes64", "monitor", "ds-cpl", "vmx", "smx", "est",
21                 "tm2", "ssse3", "cid", "sdbg", "fma", "cx16", "etprd", "pdcm",
22                 "?", "pcid", "dca", "sse41", "sse42", "x2apic", "movbe", "popcnt",
23                 "?", "aes", "xsave", "osxsave", "avx", "f16c", "rdrand", "?"};
24
25         infomsg("CPU: %s - %s\n", cpuvendor(cpu), cpuname(cpu));
26         infomsg("features:\n   ");
27         col = 3;
28         for(i=0; i<32; i++) {
29                 if(cpu->feat & (1 << i)) {
30                         len = strlen(featstr[i]) + 1;
31                         if(col + len >= 80) {
32                                 infomsg("\n   ");
33                                 col = 3;
34                         }
35                         infomsg(" %s", featstr[i]);
36                         col += strlen(featstr[i]) + 1;
37                 }
38         }
39         for(i=0; i<32; i++) {
40                 if(cpu->feat2 & (1 << i)) {
41                         len = strlen(feat2str[i]) + 1;
42                         if(col + len >= 80) {
43                                 infomsg("\n   ");
44                                 col = 3;
45                         }
46                         infomsg(" %s", feat2str[i]);
47                         col += strlen(feat2str[i]) + 1;
48                 }
49         }
50         infomsg("\n");
51 }
52
53 static const char *fam4_models[16] = {
54         "486 DX 25/33", "486 DX 50", "486 SX", "486 DX/2", "486 SL", "486 SX/2",
55         0, "486 DX/2-WB", "486 DX/4", "486 DX/4-WB"
56 };
57 static const char *fam5_models[16] = {
58         "Pentium 60/66", "Pentium 60/66", "Pentium 75-200", "OverDrive", "Pentium MMX",
59         0, 0, "Mobile Pentium 75-200", "Mobile Pentium MMX", "Quark"
60 };
61 static const char *fam6_models[16] = {
62         "Pentium Pro", "Pentium Pro", 0, "Pentium 2", "Pentium 2", "Pentium 2",
63         "Mobile Pentium 2", "Pentium 3", "Pentium 3", 0, "Pentium 3", "Pentium 3"
64 };
65
66
67 static const char *cpuname(struct cpuid_info *cpu)
68 {
69         int model, family;
70         char *rd, *wr;
71
72         if(*cpu->brandstr) {
73                 /* unwank the string */
74                 rd = wr = cpu->brandstr;
75                 while(*rd) {
76                         if(rd[0] == '(' && rd[1] == 'T' && rd[2] == 'M' && rd[3] == ')')
77                                 rd += 4;
78                         else if(rd[0] == '(' && rd[1] == 'R' && rd[2] == ')')
79                                 rd += 3;
80                         if(rd != wr) *wr = *rd;
81                         wr++;
82                         rd++;
83                 }
84                 *wr = 0;
85                 return cpu->brandstr;
86         }
87
88         if(CPUID_EXTMODEL(cpu->id)) {
89                 /* processors new enough to have an extended model, should also provide
90                  * a brand string. If we end up here, we don't know what it is
91                  */
92                 return "unknown";
93         }
94
95         model = CPUID_MODEL(cpu->id);
96         family = CPUID_FAMILY(cpu->id) | (CPUID_EXTFAMILY(cpu->id) << 4);
97
98         switch(family) {
99         case 3: return "386";
100         case 4: return fam4_models[model] ? fam4_models[model] : "486";
101         case 5: return fam5_models[model] ? fam5_models[model] : "Pentium";
102         case 6: return fam6_models[model] ? fam6_models[model] : "unknown";
103         case 15: return "Pentium 4";
104         default:
105                 break;
106         }
107         return "unknown";
108 }
109
110 static const char *cpuvendor(struct cpuid_info *cpu)
111 {
112         static char other[16];
113         static const struct { const char *wank, *vendor; } unwanktab[] = {
114                 {"GenuineIntel", "intel"},
115                 {"AuthenticAMD", "AMD"},
116                 {"AMDisbetter!", "AMD"},
117                 {"CentaurHauls", "IDT"},
118                 {"CyrixInstead", "Cyrix"},
119                 {"TransmetaCPU", "Transmeta"},
120                 {"GenuineTMx86", "Transmeta"},
121                 {"Geode by NSC", "NatSemi"},
122                 {"NexGenDriven", "NexGen"},
123                 {"RiseRiseRise", "Rise"},
124                 {"SiS SiS SiS ", "SiS"},
125                 {"UMC UMC UMC ", "UMC"},
126                 {"VIA VIA VIA ", "VIA"},
127                 {"Vortex86 SoC", "DM&P"},
128                 {"  Shanghai  ", "Zhaoxin"},
129                 {"HygonGenuine", "Hygon"},
130                 {"E2K MACHINE", "MCST Elbrus"},
131                 {"MiSTer A0486", "ao486"},
132                 {"bhyve bhyve ", "bhyve"},
133                 {" KVMKVMKVM  ", "KVM"},
134                 {"TCGTCGTCGTCG", "qemu"},
135                 {"Microsoft Hv", "MS Hyper-V"},
136                 {" lrpepyh  vr", "Parallels"},
137                 {"VMwareVMware", "VMware"},
138                 {"XenVMMXenVMM", "Xen"},
139                 {"ACRNACRNACRN", "ACRN"},
140                 {" QNXQVMBSQG ", "QNX Hypervisor"},
141                 {0, 0}
142         };
143
144         int i;
145         for(i=0; unwanktab[i].wank; i++) {
146                 if(memcmp(cpu->vendor, unwanktab[i].wank, 12) == 0) {
147                         return unwanktab[i].vendor;
148                 }
149         }
150
151         memcpy(other, cpu->vendor, 12);
152         other[12] = 0;
153         return other;
154 }