17368436913824b3ab500d680f009c3b29bb819b
[seabios.git] / vgasrc / clext.c
1 //  QEMU Cirrus CLGD 54xx VGABIOS Extension.
2 //
3 // Copyright (C) 2009  Kevin O'Connor <kevin@koconnor.net>
4 //  Copyright (c) 2004 Makoto Suzuki (suzu)
5 //
6 // This file may be distributed under the terms of the GNU LGPLv3 license.
7
8 #include "clext.h" // clext_init
9 #include "vgabios.h" // VBE_VENDOR_STRING
10 #include "biosvar.h" // GET_GLOBAL
11 #include "util.h" // dprintf
12 #include "bregs.h" // struct bregs
13 #include "vbe.h" // struct vbe_info
14 #include "stdvga.h" // VGAREG_SEQU_ADDRESS
15
16
17 /****************************************************************
18  * tables
19  ****************************************************************/
20
21 /* VGA */
22 static u16 cseq_vga[] VAR16 = {0x0007,0xffff};
23 static u16 cgraph_vga[] VAR16 = {0x0009,0x000a,0x000b,0xffff};
24 static u16 ccrtc_vga[] VAR16 = {0x001a,0x001b,0x001d,0xffff};
25
26 /* extensions */
27 static u16 cgraph_svgacolor[] VAR16 = {
28     0x0000,0x0001,0x0002,0x0003,0x0004,0x4005,0x0506,0x0f07,0xff08,
29     0x0009,0x000a,0x000b,
30     0xffff
31 };
32 /* 640x480x8 */
33 static u16 cseq_640x480x8[] VAR16 = {
34     0x0300,0x2101,0x0f02,0x0003,0x0e04,0x1107,
35     0x580b,0x580c,0x580d,0x580e,
36     0x0412,0x0013,0x2017,
37     0x331b,0x331c,0x331d,0x331e,
38     0xffff
39 };
40 static u16 ccrtc_640x480x8[] VAR16 = {
41     0x2c11,
42     0x5f00,0x4f01,0x4f02,0x8003,0x5204,0x1e05,0x0b06,0x3e07,
43     0x4009,0x000c,0x000d,
44     0xea10,0xdf12,0x5013,0x4014,0xdf15,0x0b16,0xc317,0xff18,
45     0x001a,0x221b,0x001d,
46     0xffff
47 };
48 /* 640x480x16 */
49 static u16 cseq_640x480x16[] VAR16 = {
50     0x0300,0x2101,0x0f02,0x0003,0x0e04,0x1707,
51     0x580b,0x580c,0x580d,0x580e,
52     0x0412,0x0013,0x2017,
53     0x331b,0x331c,0x331d,0x331e,
54     0xffff
55 };
56 static u16 ccrtc_640x480x16[] VAR16 = {
57     0x2c11,
58     0x5f00,0x4f01,0x4f02,0x8003,0x5204,0x1e05,0x0b06,0x3e07,
59     0x4009,0x000c,0x000d,
60     0xea10,0xdf12,0xa013,0x4014,0xdf15,0x0b16,0xc317,0xff18,
61     0x001a,0x221b,0x001d,
62     0xffff
63 };
64 /* 640x480x24 */
65 static u16 cseq_640x480x24[] VAR16 = {
66     0x0300,0x2101,0x0f02,0x0003,0x0e04,0x1507,
67     0x580b,0x580c,0x580d,0x580e,
68     0x0412,0x0013,0x2017,
69     0x331b,0x331c,0x331d,0x331e,
70     0xffff
71 };
72 static u16 ccrtc_640x480x24[] VAR16 = {
73     0x2c11,
74     0x5f00,0x4f01,0x4f02,0x8003,0x5204,0x1e05,0x0b06,0x3e07,
75     0x4009,0x000c,0x000d,
76     0xea10,0xdf12,0x0013,0x4014,0xdf15,0x0b16,0xc317,0xff18,
77     0x001a,0x321b,0x001d,
78     0xffff
79 };
80 /* 800x600x8 */
81 static u16 cseq_800x600x8[] VAR16 = {
82     0x0300,0x2101,0x0f02,0x0003,0x0e04,0x1107,
83     0x230b,0x230c,0x230d,0x230e,
84     0x0412,0x0013,0x2017,
85     0x141b,0x141c,0x141d,0x141e,
86     0xffff
87 };
88 static u16 ccrtc_800x600x8[] VAR16 = {
89     0x2311,0x7d00,0x6301,0x6302,0x8003,0x6b04,0x1a05,0x9806,0xf007,
90     0x6009,0x000c,0x000d,
91     0x7d10,0x5712,0x6413,0x4014,0x5715,0x9816,0xc317,0xff18,
92     0x001a,0x221b,0x001d,
93     0xffff
94 };
95 /* 800x600x16 */
96 static u16 cseq_800x600x16[] VAR16 = {
97     0x0300,0x2101,0x0f02,0x0003,0x0e04,0x1707,
98     0x230b,0x230c,0x230d,0x230e,
99     0x0412,0x0013,0x2017,
100     0x141b,0x141c,0x141d,0x141e,
101     0xffff
102 };
103 static u16 ccrtc_800x600x16[] VAR16 = {
104     0x2311,0x7d00,0x6301,0x6302,0x8003,0x6b04,0x1a05,0x9806,0xf007,
105     0x6009,0x000c,0x000d,
106     0x7d10,0x5712,0xc813,0x4014,0x5715,0x9816,0xc317,0xff18,
107     0x001a,0x221b,0x001d,
108     0xffff
109 };
110 /* 800x600x24 */
111 static u16 cseq_800x600x24[] VAR16 = {
112     0x0300,0x2101,0x0f02,0x0003,0x0e04,0x1507,
113     0x230b,0x230c,0x230d,0x230e,
114     0x0412,0x0013,0x2017,
115     0x141b,0x141c,0x141d,0x141e,
116     0xffff
117 };
118 static u16 ccrtc_800x600x24[] VAR16 = {
119     0x2311,0x7d00,0x6301,0x6302,0x8003,0x6b04,0x1a05,0x9806,0xf007,
120     0x6009,0x000c,0x000d,
121     0x7d10,0x5712,0x2c13,0x4014,0x5715,0x9816,0xc317,0xff18,
122     0x001a,0x321b,0x001d,
123     0xffff
124 };
125 /* 1024x768x8 */
126 static u16 cseq_1024x768x8[] VAR16 = {
127     0x0300,0x2101,0x0f02,0x0003,0x0e04,0x1107,
128     0x760b,0x760c,0x760d,0x760e,
129     0x0412,0x0013,0x2017,
130     0x341b,0x341c,0x341d,0x341e,
131     0xffff
132 };
133 static u16 ccrtc_1024x768x8[] VAR16 = {
134     0x2911,0xa300,0x7f01,0x7f02,0x8603,0x8304,0x9405,0x2406,0xf507,
135     0x6009,0x000c,0x000d,
136     0x0310,0xff12,0x8013,0x4014,0xff15,0x2416,0xc317,0xff18,
137     0x001a,0x221b,0x001d,
138     0xffff
139 };
140 /* 1024x768x16 */
141 static u16 cseq_1024x768x16[] VAR16 = {
142     0x0300,0x2101,0x0f02,0x0003,0x0e04,0x1707,
143     0x760b,0x760c,0x760d,0x760e,
144     0x0412,0x0013,0x2017,
145     0x341b,0x341c,0x341d,0x341e,
146     0xffff
147 };
148 static u16 ccrtc_1024x768x16[] VAR16 = {
149     0x2911,0xa300,0x7f01,0x7f02,0x8603,0x8304,0x9405,0x2406,0xf507,
150     0x6009,0x000c,0x000d,
151     0x0310,0xff12,0x0013,0x4014,0xff15,0x2416,0xc317,0xff18,
152     0x001a,0x321b,0x001d,
153     0xffff
154 };
155 /* 1024x768x24 */
156 static u16 cseq_1024x768x24[] VAR16 = {
157     0x0300,0x2101,0x0f02,0x0003,0x0e04,0x1507,
158     0x760b,0x760c,0x760d,0x760e,
159     0x0412,0x0013,0x2017,
160     0x341b,0x341c,0x341d,0x341e,
161     0xffff
162 };
163 static u16 ccrtc_1024x768x24[] VAR16 = {
164     0x2911,0xa300,0x7f01,0x7f02,0x8603,0x8304,0x9405,0x2406,0xf507,
165     0x6009,0x000c,0x000d,
166     0x0310,0xff12,0x8013,0x4014,0xff15,0x2416,0xc317,0xff18,
167     0x001a,0x321b,0x001d,
168     0xffff
169 };
170 /* 1280x1024x8 */
171 static u16 cseq_1280x1024x8[] VAR16 = {
172     0x0300,0x2101,0x0f02,0x0003,0x0e04,0x1107,
173     0x760b,0x760c,0x760d,0x760e,
174     0x0412,0x0013,0x2017,
175     0x341b,0x341c,0x341d,0x341e,
176     0xffff
177 };
178 static u16 ccrtc_1280x1024x8[] VAR16 = {
179     0x2911,0xc300,0x9f01,0x9f02,0x8603,0x8304,0x9405,0x2406,0xf707,
180     0x6009,0x000c,0x000d,
181     0x0310,0xff12,0xa013,0x4014,0xff15,0x2416,0xc317,0xff18,
182     0x001a,0x221b,0x001d,
183     0xffff
184 };
185 /* 1280x1024x16 */
186 static u16 cseq_1280x1024x16[] VAR16 = {
187     0x0300,0x2101,0x0f02,0x0003,0x0e04,0x1707,
188     0x760b,0x760c,0x760d,0x760e,
189     0x0412,0x0013,0x2017,
190     0x341b,0x341c,0x341d,0x341e,
191     0xffff
192 };
193 static u16 ccrtc_1280x1024x16[] VAR16 = {
194     0x2911,0xc300,0x9f01,0x9f02,0x8603,0x8304,0x9405,0x2406,0xf707,
195     0x6009,0x000c,0x000d,
196     0x0310,0xff12,0x4013,0x4014,0xff15,0x2416,0xc317,0xff18,
197     0x001a,0x321b,0x001d,
198     0xffff
199 };
200
201 /* 1600x1200x8 */
202 static u16 cseq_1600x1200x8[] VAR16 = {
203     0x0300,0x2101,0x0f02,0x0003,0x0e04,0x1107,
204     0x760b,0x760c,0x760d,0x760e,
205     0x0412,0x0013,0x2017,
206     0x341b,0x341c,0x341d,0x341e,
207     0xffff
208 };
209 static u16 ccrtc_1600x1200x8[] VAR16 = {
210     0x2911,0xc300,0x9f01,0x9f02,0x8603,0x8304,0x9405,0x2406,0xf707,
211     0x6009,0x000c,0x000d,
212     0x0310,0xff12,0xa013,0x4014,0xff15,0x2416,0xc317,0xff18,
213     0x001a,0x221b,0x001d,
214     0xffff
215 };
216
217 struct cirrus_mode_s {
218     u16 mode;
219     struct vgamode_s info;
220
221     u16 hidden_dac; /* 0x3c6 */
222     u16 *seq; /* 0x3c4 */
223     u16 *graph; /* 0x3ce */
224     u16 *crtc; /* 0x3d4 */
225 };
226
227 static struct cirrus_mode_s cirrus_modes[] VAR16 = {
228     {0x5f,{MM_PACKED,640,480,8},0x00,
229      cseq_640x480x8,cgraph_svgacolor,ccrtc_640x480x8},
230     {0x64,{MM_DIRECT,640,480,16},0xe1,
231      cseq_640x480x16,cgraph_svgacolor,ccrtc_640x480x16},
232     {0x66,{MM_DIRECT,640,480,15},0xf0,
233      cseq_640x480x16,cgraph_svgacolor,ccrtc_640x480x16},
234     {0x71,{MM_DIRECT,640,480,24},0xe5,
235      cseq_640x480x24,cgraph_svgacolor,ccrtc_640x480x24},
236
237     {0x5c,{MM_PACKED,800,600,8},0x00,
238      cseq_800x600x8,cgraph_svgacolor,ccrtc_800x600x8},
239     {0x65,{MM_DIRECT,800,600,16},0xe1,
240      cseq_800x600x16,cgraph_svgacolor,ccrtc_800x600x16},
241     {0x67,{MM_DIRECT,800,600,15},0xf0,
242      cseq_800x600x16,cgraph_svgacolor,ccrtc_800x600x16},
243
244     {0x60,{MM_PACKED,1024,768,8},0x00,
245      cseq_1024x768x8,cgraph_svgacolor,ccrtc_1024x768x8},
246     {0x74,{MM_DIRECT,1024,768,16},0xe1,
247      cseq_1024x768x16,cgraph_svgacolor,ccrtc_1024x768x16},
248     {0x68,{MM_DIRECT,1024,768,15},0xf0,
249      cseq_1024x768x16,cgraph_svgacolor,ccrtc_1024x768x16},
250
251     {0x78,{MM_DIRECT,800,600,24},0xe5,
252      cseq_800x600x24,cgraph_svgacolor,ccrtc_800x600x24},
253     {0x79,{MM_DIRECT,1024,768,24},0xe5,
254      cseq_1024x768x24,cgraph_svgacolor,ccrtc_1024x768x24},
255
256     {0x6d,{MM_PACKED,1280,1024,8},0x00,
257      cseq_1280x1024x8,cgraph_svgacolor,ccrtc_1280x1024x8},
258     {0x69,{MM_DIRECT,1280,1024,15},0xf0,
259      cseq_1280x1024x16,cgraph_svgacolor,ccrtc_1280x1024x16},
260     {0x75,{MM_DIRECT,1280,1024,16},0xe1,
261      cseq_1280x1024x16,cgraph_svgacolor,ccrtc_1280x1024x16},
262
263     {0x7b,{MM_PACKED,1600,1200,8},0x00,
264      cseq_1600x1200x8,cgraph_svgacolor,ccrtc_1600x1200x8},
265 };
266
267 static struct cirrus_mode_s mode_switchback VAR16 =
268     {0xfe,{0xff},0,cseq_vga,cgraph_vga,ccrtc_vga};
269
270 static struct {
271     u16 vesamode, mode;
272 } cirrus_vesa_modelist[] VAR16 = {
273     // 640x480x8
274     { 0x101, 0x5f },
275     // 640x480x15
276     { 0x110, 0x66 },
277     // 640x480x16
278     { 0x111, 0x64 },
279     // 640x480x24
280     { 0x112, 0x71 },
281     // 800x600x8
282     { 0x103, 0x5c },
283     // 800x600x15
284     { 0x113, 0x67 },
285     // 800x600x16
286     { 0x114, 0x65 },
287     // 800x600x24
288     { 0x115, 0x78 },
289     // 1024x768x8
290     { 0x105, 0x60 },
291     // 1024x768x15
292     { 0x116, 0x68 },
293     // 1024x768x16
294     { 0x117, 0x74 },
295     // 1024x768x24
296     { 0x118, 0x79 },
297     // 1280x1024x8
298     { 0x107, 0x6d },
299     // 1280x1024x15
300     { 0x119, 0x69 },
301     // 1280x1024x16
302     { 0x11a, 0x75 },
303 };
304
305
306 /****************************************************************
307  * helper functions
308  ****************************************************************/
309
310 static u16
311 cirrus_vesamode_to_mode(u16 vesamode)
312 {
313     int i;
314     for (i=0; i<ARRAY_SIZE(cirrus_vesa_modelist); i++)
315         if (GET_GLOBAL(cirrus_vesa_modelist[i].vesamode) == vesamode)
316             return GET_GLOBAL(cirrus_vesa_modelist[i].mode);
317     return 0;
318 }
319
320 static struct cirrus_mode_s *
321 cirrus_get_modeentry(int mode)
322 {
323     int transmode = cirrus_vesamode_to_mode(mode);
324     if (transmode)
325         mode = transmode;
326     struct cirrus_mode_s *table_g = cirrus_modes;
327     while (table_g < &cirrus_modes[ARRAY_SIZE(cirrus_modes)]) {
328         u16 tmode = GET_GLOBAL(table_g->mode);
329         if (tmode == mode)
330             return table_g;
331         table_g++;
332     }
333     return NULL;
334 }
335
336 struct vgamode_s *
337 clext_find_mode(int mode)
338 {
339     struct cirrus_mode_s *table_g = cirrus_get_modeentry(mode);
340     if (table_g)
341         return &table_g->info;
342     return stdvga_find_mode(mode);
343 }
344
345 static void
346 cirrus_switch_mode_setregs(u16 *data, u16 port)
347 {
348     for (;;) {
349         u16 val = GET_GLOBAL(*data);
350         if (val == 0xffff)
351             return;
352         outw(val, port);
353         data++;
354     }
355 }
356
357 static void
358 cirrus_switch_mode(struct cirrus_mode_s *table)
359 {
360     // Unlock cirrus special
361     outw(0x1206, VGAREG_SEQU_ADDRESS);
362     cirrus_switch_mode_setregs(GET_GLOBAL(table->seq), VGAREG_SEQU_ADDRESS);
363     cirrus_switch_mode_setregs(GET_GLOBAL(table->graph), VGAREG_GRDC_ADDRESS);
364     cirrus_switch_mode_setregs(GET_GLOBAL(table->crtc), stdvga_get_crtc());
365
366     outb(0x00, VGAREG_PEL_MASK);
367     inb(VGAREG_PEL_MASK);
368     inb(VGAREG_PEL_MASK);
369     inb(VGAREG_PEL_MASK);
370     inb(VGAREG_PEL_MASK);
371     outb(GET_GLOBAL(table->hidden_dac), VGAREG_PEL_MASK);
372     outb(0xff, VGAREG_PEL_MASK);
373
374     u8 memmodel = GET_GLOBAL(table->info.memmodel);
375     u8 v = stdvga_get_single_palette_reg(0x10) & 0xfe;
376     if (memmodel == MM_PLANAR)
377         v |= 0x41;
378     else if (memmodel != MM_TEXT)
379         v |= 0x01;
380     stdvga_set_single_palette_reg(0x10, v);
381 }
382
383 static u8
384 cirrus_get_memsize(void)
385 {
386     // get DRAM band width
387     outb(0x0f, VGAREG_SEQU_ADDRESS);
388     u8 v = inb(VGAREG_SEQU_DATA);
389     u8 x = (v >> 3) & 0x03;
390     if (x == 0x03) {
391         if (v & 0x80)
392             // 4MB
393             return 0x40;
394         // 2MB
395         return 0x20;
396     }
397     return 0x04 << x;
398 }
399
400 static void
401 cirrus_enable_16k_granularity(void)
402 {
403     outb(0x0b, VGAREG_GRDC_ADDRESS);
404     u8 v = inb(VGAREG_GRDC_DATA);
405     outb(v | 0x20, VGAREG_GRDC_DATA);
406 }
407
408 static void
409 cirrus_clear_vram(u16 param)
410 {
411     cirrus_enable_16k_granularity();
412     u8 count = cirrus_get_memsize() * 4;
413     u8 i;
414     for (i=0; i<count; i++) {
415         outw((i<<8) | 0x09, VGAREG_GRDC_ADDRESS);
416         memset16_far(SEG_GRAPH, 0, param, 16 * 1024);
417     }
418     outw(0x0009, VGAREG_GRDC_ADDRESS);
419 }
420
421 int
422 clext_set_mode(int mode, int flags)
423 {
424     dprintf(1, "cirrus mode %d\n", mode);
425     SET_BDA(vbe_mode, 0);
426     struct cirrus_mode_s *table_g = cirrus_get_modeentry(mode);
427     if (table_g) {
428         cirrus_switch_mode(table_g);
429         if (!(flags & MF_LINEARFB))
430             cirrus_enable_16k_granularity();
431         if (!(flags & MF_NOCLEARMEM))
432             cirrus_clear_vram(0);
433         SET_BDA(video_mode, mode);
434         SET_BDA(vbe_mode, mode | flags);
435         return 0;
436     }
437     cirrus_switch_mode(&mode_switchback);
438     dprintf(1, "cirrus mode switch regular\n");
439     return stdvga_set_mode(mode, flags);
440 }
441
442 static int
443 cirrus_check(void)
444 {
445     outw(0x9206, VGAREG_SEQU_ADDRESS);
446     return inb(VGAREG_SEQU_DATA) == 0x12;
447 }
448
449
450 /****************************************************************
451  * extbios
452  ****************************************************************/
453
454 static void
455 cirrus_extbios_80h(struct bregs *regs)
456 {
457     u16 crtc_addr = stdvga_get_crtc();
458     outb(0x27, crtc_addr);
459     u8 v = inb(crtc_addr + 1);
460     if (v == 0xa0)
461         // 5430
462         regs->ax = 0x0032;
463     else if (v == 0xb8)
464         // 5446
465         regs->ax = 0x0039;
466     else
467         regs->ax = 0x00ff;
468     regs->bx = 0x00;
469     return;
470 }
471
472 static void
473 cirrus_extbios_81h(struct bregs *regs)
474 {
475     // XXX
476     regs->ax = 0x0100;
477 }
478
479 static void
480 cirrus_extbios_82h(struct bregs *regs)
481 {
482     u16 crtc_addr = stdvga_get_crtc();
483     outb(0x27, crtc_addr);
484     regs->al = inb(crtc_addr + 1) & 0x03;
485     regs->ah = 0xAF;
486 }
487
488 static void
489 cirrus_extbios_85h(struct bregs *regs)
490 {
491     regs->al = cirrus_get_memsize();
492 }
493
494 static void
495 cirrus_extbios_9Ah(struct bregs *regs)
496 {
497     regs->ax = 0x4060;
498     regs->cx = 0x1132;
499 }
500
501 extern void a0h_callback(void);
502 ASM16(
503     // fatal: not implemented yet
504     "a0h_callback:"
505     "cli\n"
506     "hlt\n"
507     "retf");
508
509 static void
510 cirrus_extbios_A0h(struct bregs *regs)
511 {
512     struct cirrus_mode_s *table_g = cirrus_get_modeentry(regs->al & 0x7f);
513     regs->ah = (table_g ? 1 : 0);
514     regs->si = 0xffff;
515     regs->di = regs->ds = regs->es = regs->bx = (u32)a0h_callback;
516 }
517
518 static void
519 cirrus_extbios_A1h(struct bregs *regs)
520 {
521     regs->bx = 0x0e00; // IBM 8512/8513, color
522 }
523
524 static void
525 cirrus_extbios_A2h(struct bregs *regs)
526 {
527     regs->al = 0x07; // HSync 31.5 - 64.0 kHz
528 }
529
530 static void
531 cirrus_extbios_AEh(struct bregs *regs)
532 {
533     regs->al = 0x01; // High Refresh 75Hz
534 }
535
536 void
537 cirrus_extbios(struct bregs *regs)
538 {
539     // XXX - regs->bl < 0x80 or > 0xaf call regular handlers.
540     switch (regs->bl) {
541     case 0x80: cirrus_extbios_80h(regs); break;
542     case 0x81: cirrus_extbios_81h(regs); break;
543     case 0x82: cirrus_extbios_82h(regs); break;
544     case 0x85: cirrus_extbios_85h(regs); break;
545     case 0x9a: cirrus_extbios_9Ah(regs); break;
546     case 0xa0: cirrus_extbios_A0h(regs); break;
547     case 0xa1: cirrus_extbios_A1h(regs); break;
548     case 0xa2: cirrus_extbios_A2h(regs); break;
549     case 0xae: cirrus_extbios_AEh(regs); break;
550     default: break;
551     }
552 }
553
554
555 /****************************************************************
556  * vesa calls
557  ****************************************************************/
558
559 void
560 clext_list_modes(u16 seg, u16 *dest, u16 *last)
561 {
562     int i;
563     for (i=0; i<ARRAY_SIZE(cirrus_vesa_modelist) && dest<last; i++) {
564         SET_FARVAR(seg, *dest, GET_GLOBAL(cirrus_vesa_modelist[i].vesamode));
565         dest++;
566     }
567     stdvga_list_modes(seg, dest, last);
568 }
569
570 static u8
571 cirrus_get_bpp_bytes(void)
572 {
573     outb(0x07, VGAREG_SEQU_ADDRESS);
574     u8 v = inb(VGAREG_SEQU_DATA) & 0x0e;
575     if (v == 0x06)
576         v &= 0x02;
577     v >>= 1;
578     if (v != 0x04)
579         v++;
580     return v;
581 }
582
583 static void
584 cirrus_set_line_offset(u16 new_line_offset)
585 {
586     u16 crtc_addr = stdvga_get_crtc();
587     outb(0x13, crtc_addr);
588     outb(new_line_offset / 8, crtc_addr + 1);
589
590     outb(0x1b, crtc_addr);
591     u8 v = inb(crtc_addr + 1);
592     outb((((new_line_offset / 8) & 0x100) >> 4) | (v & 0xef), crtc_addr + 1);
593 }
594
595 static u16
596 cirrus_get_line_offset(void)
597 {
598     u16 crtc_addr = stdvga_get_crtc();
599     outb(0x13, crtc_addr);
600     u8 reg13 = inb(crtc_addr + 1);
601     outb(0x1b, crtc_addr);
602     u8 reg1b = inb(crtc_addr + 1);
603
604     return (((reg1b << 4) & 0x100) + reg13) * 8;
605 }
606
607 u16
608 cirrus_get_line_offset_entry(struct cirrus_mode_s *table_g)
609 {
610     u16 *crtc = GET_GLOBAL(table_g->crtc);
611
612     u16 *c = crtc;
613     u16 reg13;
614     for (;;) {
615         reg13 = GET_GLOBAL(*c);
616         if ((reg13 & 0xff) == 0x13)
617             break;
618         c++;
619     }
620     reg13 >>= 8;
621
622     c = crtc;
623     u16 reg1b;
624     for (;;) {
625         reg1b = GET_GLOBAL(*c);
626         if ((reg1b & 0xff) == 0x1b)
627             break;
628         c++;
629     }
630     reg1b >>= 8;
631
632     return (((reg1b << 4) & 0x100) + reg13) * 8;
633 }
634
635 static void
636 cirrus_set_start_addr(u32 addr)
637 {
638     u16 crtc_addr = stdvga_get_crtc();
639     outb(0x0d, crtc_addr);
640     outb(addr, crtc_addr + 1);
641
642     outb(0x0c, crtc_addr);
643     outb(addr>>8, crtc_addr + 1);
644
645     outb(0x1d, crtc_addr);
646     u8 v = inb(crtc_addr + 1);
647     outb(((addr & 0x0800) >> 4) | (v & 0x7f), crtc_addr + 1);
648
649     outb(0x1b, crtc_addr);
650     v = inb(crtc_addr + 1);
651     outb(((addr & 0x0100) >> 8) | ((addr & 0x0600) >> 7) | (v & 0xf2)
652          , crtc_addr + 1);
653 }
654
655 static u32
656 cirrus_get_start_addr(void)
657 {
658     u16 crtc_addr = stdvga_get_crtc();
659     outb(0x0c, crtc_addr);
660     u8 b2 = inb(crtc_addr + 1);
661
662     outb(0x0d, crtc_addr);
663     u8 b1 = inb(crtc_addr + 1);
664
665     outb(0x1b, crtc_addr);
666     u8 b3 = inb(crtc_addr + 1);
667
668     outb(0x1d, crtc_addr);
669     u8 b4 = inb(crtc_addr + 1);
670
671     return (b1 | (b2<<8) | ((b3 & 0x01) << 16) | ((b3 & 0x0c) << 15)
672             | ((b4 & 0x80) << 12));
673 }
674
675 static void
676 cirrus_vesa_05h(struct bregs *regs)
677 {
678     if (regs->bl > 1)
679         goto fail;
680     if (regs->bh == 0) {
681         // set mempage
682         if (regs->dx >= 0x100)
683             goto fail;
684         outw((regs->dx << 8) | (regs->bl + 9), VGAREG_GRDC_ADDRESS);
685     } else if (regs->bh == 1) {
686         // get mempage
687         outb(regs->bl + 9, VGAREG_GRDC_ADDRESS);
688         regs->dx = inb(VGAREG_GRDC_DATA);
689     } else
690         goto fail;
691
692     regs->ax = 0x004f;
693     return;
694 fail:
695     regs->ax = 0x014f;
696 }
697
698 static void
699 cirrus_vesa_06h(struct bregs *regs)
700 {
701     if (regs->bl > 2) {
702         regs->ax = 0x0100;
703         return;
704     }
705
706     if (regs->bl == 0x00) {
707         cirrus_set_line_offset(cirrus_get_bpp_bytes() * regs->cx);
708     } else if (regs->bl == 0x02) {
709         cirrus_set_line_offset(regs->cx);
710     }
711
712     u32 v = cirrus_get_line_offset();
713     regs->cx = v / cirrus_get_bpp_bytes();
714     regs->bx = v;
715     regs->dx = (cirrus_get_memsize() * 64 * 1024) / v;
716     regs->ax = 0x004f;
717 }
718
719 static void
720 cirrus_vesa_07h(struct bregs *regs)
721 {
722     if (regs->bl == 0x80 || regs->bl == 0x00) {
723         u32 addr = (cirrus_get_bpp_bytes() * regs->cx
724                     + cirrus_get_line_offset() * regs->dx);
725         cirrus_set_start_addr(addr / 4);
726     } else if (regs->bl == 0x01) {
727         u32 addr = cirrus_get_start_addr() * 4;
728         u32 linelength = cirrus_get_line_offset();
729         regs->dx = addr / linelength;
730         regs->cx = (addr % linelength) / cirrus_get_bpp_bytes();
731     } else {
732         regs->ax = 0x0100;
733         return;
734     }
735
736     regs->ax = 0x004f;
737 }
738
739 static void
740 cirrus_vesa_10h(struct bregs *regs)
741 {
742     if (regs->bl == 0x00) {
743         regs->bx = 0x0f30;
744         regs->ax = 0x004f;
745         return;
746     }
747     if (regs->bl == 0x01) {
748         SET_BDA(vbe_flag, regs->bh);
749         regs->ax = 0x004f;
750         return;
751     }
752     if (regs->bl == 0x02) {
753         regs->bh = GET_BDA(vbe_flag);
754         regs->ax = 0x004f;
755         return;
756     }
757     regs->ax = 0x014f;
758 }
759
760 static void
761 cirrus_vesa_not_handled(struct bregs *regs)
762 {
763     debug_stub(regs);
764     regs->ax = 0x014f;
765 }
766
767 void
768 cirrus_vesa(struct bregs *regs)
769 {
770     switch (regs->al) {
771     case 0x05: cirrus_vesa_05h(regs); break;
772     case 0x06: cirrus_vesa_06h(regs); break;
773     case 0x07: cirrus_vesa_07h(regs); break;
774     case 0x10: cirrus_vesa_10h(regs); break;
775     default:   cirrus_vesa_not_handled(regs); break;
776     }
777 }
778
779
780 /****************************************************************
781  * init
782  ****************************************************************/
783
784 int
785 clext_init(void)
786 {
787     int ret = stdvga_init();
788     if (ret)
789         return ret;
790
791     dprintf(1, "cirrus init\n");
792     if (! cirrus_check())
793         return -1;
794     dprintf(1, "cirrus init 2\n");
795
796     u16 totalmem = cirrus_get_memsize();
797     SET_VGA(VBE_total_memory, totalmem * 64 * 1024);
798     SET_VGA(VBE_win_granularity, 16);
799
800     // memory setup
801     outb(0x0f, VGAREG_SEQU_ADDRESS);
802     u8 v = inb(VGAREG_SEQU_DATA);
803     outb(((v & 0x18) << 8) | 0x0a, VGAREG_SEQU_ADDRESS);
804     // set vga mode
805     outw(0x0007, VGAREG_SEQU_ADDRESS);
806     // reset bitblt
807     outw(0x0431, VGAREG_GRDC_ADDRESS);
808     outw(0x0031, VGAREG_GRDC_ADDRESS);
809
810     return 0;
811 }