removed clang-format and clang_complete files from the repo
[dosdemo] / src / cpuid.c
1 #include <stdio.h>
2 #include <string.h>
3 #include "cpuid.h"
4
5 static const char *cpuname(struct cpuid_info *cpu);
6 static const char *cpuvendor(struct cpuid_info *cpu);
7
8 struct cpuid_info cpuid;
9
10 void print_cpuid(struct cpuid_info *cpu)
11 {
12         int i, col, len;
13         char buf[64];
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         printf("CPU: %s - %s\n", cpuvendor(cpu), cpuname(cpu));
26         printf("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                                 fputs("\n   ", stdout);
33                                 col = 3;
34                         }
35                         col += printf(" %s", featstr[i]);
36                 }
37         }
38         for(i=0; i<32; i++) {
39                 if(cpu->feat2 & (1 << i)) {
40                         len = strlen(feat2str[i]) + 1;
41                         if(col + len >= 80) {
42                                 fputs("\n   ", stdout);
43                                 col = 3;
44                         }
45                         col += printf(" %s", feat2str[i]);
46                 }
47         }
48         putchar('\n');
49 }
50
51 static const char *fam4_models[16] = {
52         "486 DX 25/33", "486 DX 50", "486 SX", "486 DX/2", "486 SL", "486 SX/2",
53         0, "486 DX/2-WB", "486 DX/4", "486 DX/4-WB"
54 };
55 static const char *fam5_models[16] = {
56         "Pentium 60/66", "Pentium 60/66", "Pentium 75-200", "OverDrive", "Pentium MMX",
57         0, 0, "Mobile Pentium 75-200", "Mobile Pentium MMX", "Quark"
58 };
59 static const char *fam6_models[16] = {
60         "Pentium Pro", "Pentium Pro", 0, "Pentium 2", "Pentium 2", "Pentium 2",
61         "Mobile Pentium 2", "Pentium 3", "Pentium 3", 0, "Pentium 3", "Pentium 3"
62 };
63
64
65 static const char *cpuname(struct cpuid_info *cpu)
66 {
67         int model, family;
68         char *rd, *wr;
69
70         if(*cpu->brandstr) {
71                 /* unwank the string */
72                 rd = wr = cpu->brandstr;
73                 while(*rd) {
74                         if(rd[0] == '(' && rd[1] == 'T' && rd[2] == 'M' && rd[3] == ')')
75                                 rd += 4;
76                         else if(rd[0] == '(' && rd[1] == 'R' && rd[2] == ')')
77                                 rd += 3;
78                         if(rd != wr) *wr = *rd;
79                         wr++;
80                         rd++;
81                 }
82                 return cpu->brandstr;
83         }
84
85         if(CPUID_EXTMODEL(cpu->id)) {
86                 /* processors new enough to have an extended model, should also provide
87                  * a brand string. If we end up here, we don't know what it is
88                  */
89                 return "unknown";
90         }
91
92         model = CPUID_MODEL(cpu->id);
93         family = CPUID_FAMILY(cpu->id) | (CPUID_EXTFAMILY(cpu->id) << 4);
94
95         switch(family) {
96         case 3: return "386";
97         case 4: return fam4_models[model] ? fam4_models[model] : "486";
98         case 5: return fam5_models[model] ? fam5_models[model] : "Pentium";
99         case 6: return fam6_models[model] ? fam6_models[model] : "unknown";
100         case 15: return "Pentium 4";
101         default:
102                 break;
103         }
104         return "unknown";
105 }
106
107 static const char *cpuvendor(struct cpuid_info *cpu)
108 {
109         static char other[16];
110         static const struct { const char *wank, *vendor; } unwanktab[] = {
111                 {"GenuineIntel", "intel"},
112                 {"AuthenticAMD", "AMD"},
113                 {"AMDisbetter!", "AMD"},
114                 {"CentaurHauls", "IDT"},
115                 {"CyrixInstead", "Cyrix"},
116                 {"TransmetaCPU", "Transmeta"},
117                 {"GenuineTMx86", "Transmeta"},
118                 {"Geode by NSC", "NatSemi"},
119                 {"NexGenDriven", "NexGen"},
120                 {"RiseRiseRise", "Rise"},
121                 {"SiS SiS SiS ", "SiS"},
122                 {"UMC UMC UMC ", "UMC"},
123                 {"VIA VIA VIA ", "VIA"},
124                 {"Vortex86 SoC", "DM&P"},
125                 {"  Shanghai  ", "Zhaoxin"},
126                 {"HygonGenuine", "Hygon"},
127                 {"E2K MACHINE", "MCST Elbrus"},
128                 {"MiSTer A0486", "ao486"},
129                 {"bhyve bhyve ", "bhyve"},
130                 {" KVMKVMKVM  ", "KVM"},
131                 {"TCGTCGTCGTCG", "qemu"},
132                 {"Microsoft Hv", "MS Hyper-V"},
133                 {" lrpepyh  vr", "Parallels"},
134                 {"VMwareVMware", "VMware"},
135                 {"XenVMMXenVMM", "Xen"},
136                 {"ACRNACRNACRN", "ACRN"},
137                 {" QNXQVMBSQG ", "QNX Hypervisor"},
138                 {0, 0}
139         };
140
141         int i;
142         for(i=0; unwanktab[i].wank; i++) {
143                 if(memcmp(cpu->vendor, unwanktab[i].wank, 12) == 0) {
144                         return unwanktab[i].vendor;
145                 }
146         }
147
148         memcpy(other, cpu->vendor, 12);
149         other[12] = 0;
150         return other;
151 }