1 /* Generate the VGABIOS VBE Tables */
2 #include <stdlib.h>
3 #include <stdio.h>
4 
5 #define VBE_DISPI_TOTAL_VIDEO_MEMORY_MB 16
6 
7 typedef struct {
8     int width;
9     int height;
10     int depth;
11     int mode;
12 } ModeInfo;
13 
14 ModeInfo modes[] = {
15     /* standard VESA modes */
16 { 640, 400, 8                          , 0x100},
17 { 640, 480, 8                          , 0x101},
18 { 800, 600, 4                          , 0x102},
19 { 800, 600, 8                          , 0x103},
20 { 1024, 768, 4                         , 0x104},
21 { 1024, 768, 8                         , 0x105},
22 { 1280, 1024, 4                        , 0x106},
23 { 1280, 1024, 8                        , 0x107},
24 { 320, 200, 15                       , 0x10D},
25 { 320, 200, 16                        , 0x10E},
26 { 320, 200, 24                        , 0x10F},
27 { 640, 480, 15                       , 0x110},
28 { 640, 480, 16                        , 0x111},
29 { 640, 480, 24                        , 0x112},
30 { 800, 600, 15                       , 0x113},
31 { 800, 600, 16                        , 0x114},
32 { 800, 600, 24                        , 0x115},
33 { 1024, 768, 15                      , 0x116},
34 { 1024, 768, 16                       , 0x117},
35 { 1024, 768, 24                       , 0x118},
36 { 1280, 1024, 15                     , 0x119},
37 { 1280, 1024, 16                      , 0x11A},
38 { 1280, 1024, 24                      , 0x11B},
39 { 1600, 1200, 8                        , 0x11C},
40 { 1600, 1200, 15                     , 0x11D},
41 { 1600, 1200, 16                      , 0x11E},
42 { 1600, 1200, 24                      , 0x11F},
43 
44       /* BOCHS/PLE, 86 'own' mode numbers */
45 { 320, 200, 32                       , 0x140},
46 { 640, 400, 32                       , 0x141},
47 { 640, 480, 32                       , 0x142},
48 { 800, 600, 32                       , 0x143},
49 { 1024, 768, 32                      , 0x144},
50 { 1280, 1024, 32                     , 0x145},
51 { 320, 200, 8                        , 0x146},
52 { 1600, 1200, 32                     , 0x147},
53 { 1152, 864, 8                       , 0x148},
54 { 1152, 864, 15                      , 0x149},
55 { 1152, 864, 16                      , 0x14a},
56 { 1152, 864, 24                      , 0x14b},
57 { 1152, 864, 32                      , 0x14c},
58 { 1280, 800, 16                      , 0x178},
59 { 1280, 800, 24                      , 0x179},
60 { 1280, 800, 32                      , 0x17a},
61 { 1280, 960, 16                      , 0x17b},
62 { 1280, 960, 24                      , 0x17c},
63 { 1280, 960, 32                      , 0x17d},
64 { 1440, 900, 16                      , 0x17e},
65 { 1440, 900, 24                      , 0x17f},
66 { 1440, 900, 32                      , 0x180},
67 { 1400, 1050, 16                     , 0x181},
68 { 1400, 1050, 24                     , 0x182},
69 { 1400, 1050, 32                     , 0x183},
70 { 1680, 1050, 16                     , 0x184},
71 { 1680, 1050, 24                     , 0x185},
72 { 1680, 1050, 32                     , 0x186},
73 { 1920, 1200, 16                     , 0x187},
74 { 1920, 1200, 24                     , 0x188},
75 { 1920, 1200, 32                     , 0x189},
76 { 2560, 1600, 16                     , 0x18a},
77 { 2560, 1600, 24                     , 0x18b},
78 { 2560, 1600, 32                     , 0x18c},
79 { 0, },
80 };
81 
main(int argc,char ** argv)82 int main(int argc, char **argv)
83 {
84   const ModeInfo *pm;
85   int pages, pitch;
86   int r_size, r_pos, g_size, g_pos, b_size, b_pos, a_size, a_pos;
87   const char *str;
88   long vram_size = VBE_DISPI_TOTAL_VIDEO_MEMORY_MB * 1024 * 1024;
89 
90   printf("/* THIS FILE IS AUTOMATICALLY GENERATED - DO NOT EDIT */\n\n");
91   printf("#define VBE_DISPI_TOTAL_VIDEO_MEMORY_MB %d\n\n", VBE_DISPI_TOTAL_VIDEO_MEMORY_MB);
92   printf("static ModeInfoListItem mode_info_list[]=\n");
93   printf("{\n");
94   for (pm = modes; pm->mode != 0; pm++) {
95     if (pm->depth == 4)
96       pitch = (pm->width + 7) / 8;
97     else
98       pitch = pm->width * ((pm->depth + 7) / 8);
99     pages = vram_size / (pm->height * pitch);
100     if (pages > 0) {
101       printf("{ 0x%04x, /* %dx%dx%d */\n",
102              pm->mode, pm->width, pm->height, pm->depth);
103       if (pm->depth == 4)
104         printf("{ /*Bit16u ModeAttributes*/ %s,\n",
105                "VBE_MODE_ATTRIBUTE_SUPPORTED | "
106                "VBE_MODE_ATTRIBUTE_EXTENDED_INFORMATION_AVAILABLE | "
107                "VBE_MODE_ATTRIBUTE_COLOR_MODE | "
108                "VBE_MODE_ATTRIBUTE_TTY_BIOS_SUPPORT | "
109                "VBE_MODE_ATTRIBUTE_GRAPHICS_MODE");
110       else
111         printf("{ /*Bit16u ModeAttributes*/ %s,\n",
112                "VBE_MODE_ATTRIBUTE_SUPPORTED | "
113                "VBE_MODE_ATTRIBUTE_EXTENDED_INFORMATION_AVAILABLE | "
114                "VBE_MODE_ATTRIBUTE_COLOR_MODE | "
115                "VBE_MODE_ATTRIBUTE_LINEAR_FRAME_BUFFER_MODE | "
116                "VBE_MODE_ATTRIBUTE_GRAPHICS_MODE");
117       printf("/*Bit8u  WinAAttributes*/ %s,\n",
118              "VBE_WINDOW_ATTRIBUTE_RELOCATABLE | "
119              "VBE_WINDOW_ATTRIBUTE_READABLE | "
120              "VBE_WINDOW_ATTRIBUTE_WRITEABLE");
121 
122       printf("/*Bit8u  WinBAttributes*/ %d,\n", 0);
123 
124       printf("/*Bit16u WinGranularity*/ %s,\n", "VBE_DISPI_BANK_SIZE_KB");
125 
126       printf("/*Bit16u WinSize*/ %s,\n", "VBE_DISPI_BANK_SIZE_KB");
127 
128       printf("/*Bit16u WinASegment*/ %s,\n", "VGAMEM_GRAPH");
129 
130       printf("/*Bit16u WinBSegment*/ 0x%04x,\n", 0);
131 
132       printf("/*Bit32u WinFuncPtr*/ %d,\n", 0);
133 
134       printf("/*Bit16u BytesPerScanLine*/ %d,\n", pitch);
135 
136       // Mandatory information for VBE 1.2 and above
137       printf("/*Bit16u XResolution*/ %d,\n", pm->width);
138       printf("/*Bit16u YResolution*/ %d,\n", pm->height);
139       printf("/*Bit8u  XCharSize*/ %d,\n", 8);
140       printf("/*Bit8u  YCharSize*/ %d,\n", 16);
141       if (pm->depth == 4) {
142         printf("/*Bit8u  NumberOfPlanes*/ %d,\n", 4);
143       } else {
144         printf("/*Bit8u  NumberOfPlanes*/ %d,\n", 1);
145       }
146       printf("/*Bit8u  BitsPerPixel*/ %d,\n", pm->depth);
147       printf("/*Bit8u  NumberOfBanks*/ %d,\n",
148              (pm->height * pitch + 65535) / 65536);
149 
150       if (pm->depth == 4)
151         str = "VBE_MEMORYMODEL_PLANAR";
152       else if (pm->depth == 8)
153         str = "VBE_MEMORYMODEL_PACKED_PIXEL";
154       else
155         str = "VBE_MEMORYMODEL_DIRECT_COLOR";
156       printf("/*Bit8u  MemoryModel*/ %s,\n", str);
157       printf("/*Bit8u  BankSize*/ %d,\n", 0);
158       if (pm->depth == 4)
159         printf("/*Bit8u  NumberOfImagePages*/ %d,\n", (pages / 4) - 1);
160       else
161         printf("/*Bit8u  NumberOfImagePages*/ %d,\n", pages - 1);
162       printf("/*Bit8u  Reserved_page*/ %d,\n", 0);
163 
164       // Direct Color fields (required for direct/6 and YUV/7 memory models)
165       switch(pm->depth) {
166         case 15:
167           r_size = 5;
168           r_pos = 10;
169           g_size = 5;
170           g_pos = 5;
171           b_size = 5;
172           b_pos = 0;
173           a_size = 1;
174           a_pos = 15;
175           break;
176         case 16:
177           r_size = 5;
178           r_pos = 11;
179           g_size = 6;
180           g_pos = 5;
181           b_size = 5;
182           b_pos = 0;
183           a_size = 0;
184           a_pos = 0;
185           break;
186         case 24:
187           r_size = 8;
188           r_pos = 16;
189           g_size = 8;
190           g_pos = 8;
191           b_size = 8;
192           b_pos = 0;
193           a_size = 0;
194           a_pos = 0;
195           break;
196         case 32:
197           r_size = 8;
198           r_pos = 16;
199           g_size = 8;
200           g_pos = 8;
201           b_size = 8;
202           b_pos = 0;
203           a_size = 8;
204           a_pos = 24;
205           break;
206         default:
207           r_size = 0;
208           r_pos = 0;
209           g_size = 0;
210           g_pos = 0;
211           b_size = 0;
212           b_pos = 0;
213           a_size = 0;
214           a_pos = 0;
215           break;
216       }
217 
218       printf("/*Bit8u  RedMaskSize*/ %d,\n", r_size);
219       printf("/*Bit8u  RedFieldPosition*/ %d,\n", r_pos);
220       printf("/*Bit8u  GreenMaskSize*/ %d,\n", g_size);
221       printf("/*Bit8u  GreenFieldPosition*/ %d,\n", g_pos);
222       printf("/*Bit8u  BlueMaskSize*/ %d,\n", b_size);
223       printf("/*Bit8u  BlueFieldPosition*/ %d,\n", b_pos);
224       printf("/*Bit8u  RsvdMaskSize*/ %d,\n", a_size);
225       printf("/*Bit8u  RsvdFieldPosition*/ %d,\n", a_pos);
226       if (pm->depth == 32)
227         printf("/*Bit8u  DirectColorModeInfo*/ %s,\n",
228                "VBE_DIRECTCOLOR_RESERVED_BITS_AVAILABLE");
229       else
230         printf("/*Bit8u  DirectColorModeInfo*/ %s,\n", "0");
231 
232 // Mandatory information for VBE 2.0 and above
233       if (pm->depth > 4)
234         printf("/*Bit32u PhysBasePtr*/ %s,\n",
235                "VBE_DISPI_LFB_PHYSICAL_ADDRESS");
236       else
237         printf("/*Bit32u PhysBasePtr*/ %s,\n", "0");
238       printf("/*Bit32u OffScreenMemOffset*/ %d,\n", 0);
239       printf("/*Bit16u OffScreenMemSize*/ %d,\n", 0);
240       // Mandatory information for VBE 3.0 and above
241       printf("/*Bit16u LinBytesPerScanLine*/ %d,\n", pitch);
242       printf("/*Bit8u  BnkNumberOfPages*/ %d,\n", 0);
243       printf("/*Bit8u  LinNumberOfPages*/ %d,\n", 0);
244       printf("/*Bit8u  LinRedMaskSize*/ %d,\n", r_size);
245       printf("/*Bit8u  LinRedFieldPosition*/ %d,\n", r_pos);
246       printf("/*Bit8u  LinGreenMaskSize*/ %d,\n", g_size);
247       printf("/*Bit8u  LinGreenFieldPosition*/ %d,\n", g_pos);
248       printf("/*Bit8u  LinBlueMaskSize*/ %d,\n", b_size);
249       printf("/*Bit8u  LinBlueFieldPosition*/ %d,\n", b_pos);
250       printf("/*Bit8u  LinRsvdMaskSize*/ %d,\n", a_size);
251       printf("/*Bit8u  LinRsvdFieldPosition*/ %d,\n", a_pos);
252       printf("/*Bit32u MaxPixelClock*/ %d,\n", 0);
253       printf("} },\n");
254     }
255   }
256   printf("{ VBE_VESA_MODE_END_OF_LIST,\n");
257   printf("{ 0,\n");
258   printf("} },\n");
259   printf("};\n");
260   return 0;
261 }
262