added read_cpuid and MTRR support checking before trying to set them
[retrobench] / src / rbench.c
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <math.h>
4 #include "rbench.h"
5 #include "treestor.h"
6 #include "util.h"
7
8 #define DEF_WIDTH       640
9 #define DEF_HEIGHT      480
10 #define DEF_BPP         24
11
12 struct options opt = {
13         DEF_WIDTH, DEF_HEIGHT, DEF_BPP
14 };
15
16 int fb_width, fb_height, fb_bpp, fb_pitch;
17 int fb_rshift, fb_gshift, fb_bshift;
18 unsigned int fb_rmask, fb_gmask, fb_bmask;
19 void *framebuf;
20 unsigned int time_msec;
21
22 static const char *cpufeat[] = {
23         "fpu", "vme", "dbgext", "pse", "tsc", "msr", "pae", "mce", "cx8", "apic", 0,
24         "sep", "mtrr", "pge", "mca", "cmov", "pat", "pse36", "psn", "clf", 0,
25         "dtes", "acpi", "mmx", "fxsr", "sse", "sse2", "ss", "htt", "tm1", "ia64", "pbe"
26 };
27
28 static const char *cpufeat2[] = {
29         "sse3", "pclmul", "dtes64", "monitor", "dscpl", "vmx", "smx", "est", "tm2",
30         "ssse3", "cid", 0, "fma", "cx16", "etprd", "pdcm", 0, "pcide", "dca", "sse4.1",
31         "sse4.2", "x2apic", "movbe", "popcnt", 0, "aes", "xsave", "osxsave", "avx"
32 };
33
34 int init(void)
35 {
36         int i;
37         struct cpuid_info cpu;
38
39         if(read_cpuid(&cpu) != -1) {
40                 printf("CPUID information:\n");
41                 printf("  cpuid blocks: %d\n", (int)cpu.maxidx);
42                 printf("  CPU vendor: ");
43                 for(i=0; i<12; i++) {
44                         putchar(cpu.vendor[i]);
45                 }
46                 putchar('\n');
47                 printf("  stepping: %u, model: %u, family: %u\n", CPUID_STEPPING(cpu.id),
48                                 CPUID_MODEL(cpu.id), CPUID_FAMILY(cpu.id));
49                 printf("  features:");
50                 for(i=0; i<sizeof cpufeat / sizeof *cpufeat; i++) {
51                         if(cpufeat[i] && (cpu.feat & (1 << i))) {
52                                 printf(" %s", cpufeat[i]);
53                         }
54                 }
55                 for(i=0; i<sizeof cpufeat2 / sizeof *cpufeat2; i++) {
56                         if(cpufeat2[i] && (cpu.feat2 & (1 << i))) {
57                                 printf(" %s", cpufeat2[i]);
58                         }
59                 }
60                 putchar('\n');
61         }
62
63         printf("initialized graphics %dx%d %dbpp\n", fb_width, fb_height, fb_bpp);
64         printf("  rgb mask: %x %x %x\n", fb_rmask, fb_gmask, fb_bmask);
65         printf("  rgb shift: %d %d %d\n", fb_rshift, fb_gshift, fb_bshift);
66         return 0;
67 }
68
69 void cleanup(void)
70 {
71 }
72
73 #define XORRGB(x, y, dx, dy, zoom, r, g, b) \
74         do { \
75                 int xor = ((((x) - fb_width/2) * (zoom) >> 16) + (dx)) ^ ((((y) - fb_height/2) * (zoom) >> 16) + (dy)); \
76                 (r) = xor >> 2; \
77                 (g) = xor >> 1; \
78                 (b) = xor; \
79         } while(0)
80
81 void redraw(void)
82 {
83         int i, j, r, g, b, xoffs, yoffs, zoom;
84         unsigned char *fbptr;
85         uint16_t *fbptr16;
86         uint32_t *fbptr32;
87
88         xoffs = COS(time_msec >> 2) * fb_width >> 14;
89         yoffs = SIN(time_msec >> 1) * fb_height >> 15;
90         zoom = (SIN(time_msec >> 3) << 1) + 0x18000;
91
92         switch(fb_bpp) {
93         case 15:
94         case 16:
95                 fbptr16 = framebuf;
96                 for(i=0; i<fb_height; i++) {
97                         for(j=0; j<fb_width; j++) {
98                                 XORRGB(j, i, xoffs, yoffs, zoom, r, g, b);
99                                 *fbptr16++ = (((r >> 3) << fb_rshift) & fb_rmask) |
100                                         (((g >> 2) << fb_gshift) & fb_gmask) |
101                                         (((b >> 3) << fb_bshift) & fb_bmask);
102                         }
103                         fbptr16 += (fb_pitch >> 1) - fb_width;
104                 }
105                 break;
106
107         case 24:
108                 fbptr = framebuf;
109                 for(i=0; i<fb_height; i++) {
110                         for(j=0; j<fb_width; j++) {
111                                 XORRGB(j, i, xoffs, yoffs, zoom, r, g, b);
112                                 *fbptr++ = r;
113                                 *fbptr++ = g;
114                                 *fbptr++ = b;
115                         }
116                         fbptr += fb_pitch - fb_width * 3;
117                 }
118                 break;
119
120         case 32:
121                 fbptr32 = framebuf;
122                 for(i=0; i<fb_height; i++) {
123                         for(j=0; j<fb_width; j++) {
124                                 XORRGB(j, i, xoffs, yoffs, zoom, r, g, b);
125                                 *fbptr32++ = (((r) << fb_rshift) & fb_rmask) |
126                                         (((g) << fb_gshift) & fb_gmask) |
127                                         (((b) << fb_bshift) & fb_bmask);
128                         }
129                         fbptr32 += (fb_pitch >> 2) - fb_width;
130                 }
131                 break;
132         }
133 }
134
135 void key_event(int key, int press)
136 {
137 }
138
139 int read_config(const char *fname)
140 {
141         FILE *fp;
142         struct ts_node *ts;
143
144         if(!(fp = fopen(fname, "rb"))) {
145                 return -1;
146         }
147         fclose(fp);
148
149         if(!(ts = ts_load(fname))) {
150                 return -1;
151         }
152
153         opt.width = ts_lookup_int(ts, "rbench.width", DEF_WIDTH);
154         opt.height = ts_lookup_int(ts, "rbench.height", DEF_HEIGHT);
155         opt.bpp = ts_lookup_int(ts, "rbench.bpp", DEF_BPP);
156
157         ts_free_tree(ts);
158         return 0;
159 }