fs.h: A slighly more useful default PATH
[syslinux.git] / core / graphics.c
blob080efa11a558d4609d8df9aef09ed99a84b7de3f
1 /*
2 * -----------------------------------------------------------------------
4 * Copyright 1994-2008 H. Peter Anvin - All Rights Reserved
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, Inc., 53 Temple Place Ste 330,
9 * Boston MA 02111-1307, USA; either version 2 of the License, or
10 * (at your option) any later version; incorporated herein by reference.
12 * -----------------------------------------------------------------------
14 * -----------------------------------------------------------------------
15 * VGA splash screen code
16 * -----------------------------------------------------------------------
19 #include <stddef.h>
20 #include "core.h"
21 #include <sys/io.h>
22 #include <hw/vga.h>
23 #include "fs.h"
24 #include "bios.h"
26 uint8_t UsingVGA = 0;
27 uint16_t VGAPos; /* Pointer into VGA memory */
28 uint16_t *VGAFilePtr; /* Pointer into VGAFileBuf */
30 char VGAFileBuf[VGA_FILE_BUF_SIZE]; /* Unmangled VGA image name */
31 char VGAFileMBuf[FILENAME_MAX]; /* Mangled VGA image name */
33 static uint8_t VGARowBuffer[640 + 80]; /* Decompression buffer */
34 static uint8_t VGAPlaneBuffer[(640/8) * 4]; /* Plane buffers */
36 extern uint16_t GXPixCols;
37 extern uint16_t GXPixRows;
39 /* Maps colors to consecutive DAC registers */
40 static uint8_t linear_color[] = { 0, 1, 2, 3, 4, 5, 6, 7, 8,
41 9, 10, 11, 12, 13, 14, 15, 0 };
43 static FILE *fd;
45 typedef struct {
46 uint32_t LSSMagic; /* Magic number */
47 uint16_t GraphXSize; /* Width of splash screen file */
48 uint16_t GraphYSize; /* Height of splash screen file */
49 uint8_t GraphColorMap[3*16];
50 } lssheader_t;
52 static lssheader_t LSSHeader;
54 #define LSSMagic LSSHeader.LSSMagic
55 #define GraphXSize LSSHeader.GraphXSize
56 #define GraphYSize LSSHeader.GraphYSize
59 * Enable VGA graphics, if possible. Return 0 on success.
61 static int vgasetmode(void)
63 com32sys_t ireg, oreg;
65 if (UsingVGA)
66 return 0; /* Nothing to do... */
68 memset(&ireg, 0, sizeof(ireg));
69 memset(&oreg, 0, sizeof(oreg));
71 if (UsingVGA & 0x4) {
73 * We're in VESA mode, which means VGA; use VESA call
74 * to revert the mode, and then call the conventional
75 * mode-setting for good measure...
77 ireg.eax.w[0] = 0x4F02;
78 ireg.ebx.w[0] = 0x0012;
79 __intcall(0x10, &ireg, &oreg);
80 } else {
81 /* Get video card and monitor */
82 ireg.eax.w[0] = 0x1A00;
83 __intcall(0x10, &ireg, &oreg);
84 oreg.ebx.b[0] -= 7; /* BL=07h and BL=08h OK */
86 if (oreg.ebx.b[0] > 1)
87 return -1;
91 * Set mode.
93 ireg.eax.w[0] = 0x0012; /* Set mode = 640x480 VGA 16 colors */
94 __intcall(0x10, &ireg, &oreg);
96 ireg.edx.w[0] = (uint32_t)linear_color;
97 ireg.eax.w[0] = 0x1002; /* Write color registers */
98 __intcall(0x10, &ireg, &oreg);
100 UsingVGA = 1;
102 /* Set GXPixCols and GXPixRows */
103 GXPixCols = 640;
104 GXPixRows = 480;
106 use_font();
107 ScrollAttribute = 0;
109 return 0;
112 static inline char getnybble(void)
114 char data = getc(fd);
116 if (data & 0x10) {
117 data &= 0x0F;
118 return data;
121 data = getc(fd);
122 return (data & 0x0F);
126 * rledecode:
127 * Decode a pixel row in RLE16 format.
129 * 'in': input (RLE16 encoded) buffer
130 * 'out': output (decoded) buffer
131 * 'count': pixel count
133 static void rledecode(uint8_t *out, size_t count)
135 uint8_t prev_pixel = 0;
136 size_t size = count;
137 uint8_t data;
138 int i;
140 again:
141 for (i = 0; i < size; i++) {
143 data = getnybble();
144 if (data == prev_pixel)
145 break;
147 *out++ = data;
148 prev_pixel = data;
151 size -= i;
152 if (!size)
153 return;
155 /* Start of run sequence */
156 data = getnybble();
157 if (data == 0) {
158 /* long run */
159 uint8_t hi;
161 data = getnybble();
162 hi = getnybble();
163 hi <<= 4;
164 data |= hi;
165 data += 16;
168 /* dorun */
169 for (i = 0; i < data; i++)
170 *out++ = prev_pixel;
172 size -= i;
173 if (size)
174 goto again;
178 * packedpixel2vga:
179 * Convert packed-pixel to VGA bitplanes
181 * 'in': packed pixel string (640 pixels)
182 * 'out': output (four planes @ 640/8 = 80 bytes)
183 * 'count': pixel count (multiple of 8)
185 static void packedpixel2vga(const uint8_t *in, uint8_t *out)
187 int i, j, k;
189 for (i = 0; i < 4; i++) {
190 const uint8_t *ip = in;
192 for (j = 0; j < 640/8; j++) {
193 uint8_t ob = 0;
195 for (k = 0; k < 8; k++) {
196 uint8_t px = *ip++;
197 ob = (ob << 1) | ((px >> i) & 1);
200 *out++ = ob;
206 * outputvga:
207 * Output four subsequent lines of VGA data
209 * 'in': four planes @ 640/8=80 bytes
210 * 'out': pointer into VGA memory
212 static void outputvga(const void *in, void *out)
214 int i;
216 /* Select the sequencer mask */
217 outb(VGA_SEQ_IX_MAP_MASK, VGA_SEQ_ADDR);
219 for (i = 1; i <= 8; i <<= 1) {
220 /* Select the bit plane to write */
221 outb(i, VGA_SEQ_DATA);
222 memcpy(out, in, 640/8);
223 in = (const char *)in + 640/8;
228 * Display a graphical splash screen.
230 void vgadisplayfile(FILE *_fd)
232 char *p;
233 int size;
235 fd = _fd;
238 * This is a cheap and easy way to make sure the screen is
239 * cleared in case we were in graphics mode aready.
241 vgaclearmode();
242 vgasetmode();
244 size = 4+2*2+16*3;
245 p = (char *)&LSSHeader;
247 /* Load the header */
248 while (size--)
249 *p = getc(fd);
251 if (*p != EOF) {
252 com32sys_t ireg, oreg;
253 uint16_t rows;
254 int i;
256 /* The header WILL be in the first chunk. */
257 if (LSSMagic != 0x1413f33d)
258 return;
260 memset(&ireg, 0, sizeof(ireg));
262 /* Color map offset */
263 ireg.edx.w[0] = offsetof(lssheader_t, GraphColorMap);
265 ireg.eax.w[0] = 0x1012; /* Set RGB registers */
266 ireg.ebx.w[0] = 0; /* First register number */
267 ireg.ecx.w[0] = 16; /* 16 registers */
268 __intcall(0x10, &ireg, &oreg);
270 /* Number of pixel rows */
271 rows = (GraphYSize + VGAFontSize) - 1;
272 rows = rows / VGAFontSize;
273 if (rows >= VidRows)
274 rows = VidRows - 1;
276 memset(&ireg, 0, sizeof(ireg));
278 ireg.edx.b[1] = rows;
279 ireg.eax.b[1] = 2;
280 ireg.ebx.w[0] = 0;
282 /* Set cursor below image */
283 __intcall(0x10, &ireg, &oreg);
285 rows = GraphYSize; /* Number of graphics rows */
286 VGAPos = 0;
288 for (i = 0; i < rows; i++) {
289 /* Pre-clear the row buffer */
290 memset(VGARowBuffer, 0, 640);
292 /* Decode one row */
293 rledecode(VGARowBuffer, GraphXSize);
295 packedpixel2vga(VGARowBuffer, VGAPlaneBuffer);
296 outputvga(VGAPlaneBuffer, MK_PTR(0xA000, VGAPos));
297 VGAPos += 640/8;
303 * Disable VGA graphics.
305 void vgaclearmode(void)
307 com32sys_t ireg, oreg;
309 /* Already in text mode? */
310 if (!UsingVGA)
311 return;
313 if (UsingVGA & 0x4) {
314 /* VESA return to normal video mode */
315 memset(&ireg, 0, sizeof(ireg));
317 ireg.eax.w[0] = 0x4F02; /* Set SuperVGA video mode */
318 ireg.ebx.w[0] = 0x0003;
319 __intcall(0x10, &ireg, &oreg);
322 /* Return to normal video mode */
323 memset(&ireg, 0, sizeof(ireg));
324 ireg.eax.w[0] = 0x0003;
325 __intcall(0x10, &ireg, &oreg);
327 UsingVGA = 0;
329 ScrollAttribute = 0x7;
330 /* Restore text font/data */
331 use_font();
334 static void vgacursorcommon(char data)
336 if (UsingVGA) {
337 com32sys_t ireg;
339 ireg.eax.b[0] = data;
340 ireg.eax.b[1] = 0x09;
341 ireg.ebx.w[0] = 0x0007;
342 ireg.ecx.w[0] = 1;
343 __intcall(0x10, &ireg, NULL);
347 void vgahidecursor(void)
349 vgacursorcommon(' ');
352 void vgashowcursor(void)
354 vgacursorcommon('_');
357 void using_vga(uint8_t vga, uint16_t pix_cols, uint16_t pix_rows)
359 UsingVGA = vga;
360 GXPixCols = pix_cols;
361 GXPixRows = pix_rows;
363 if (!(UsingVGA & 0x08))
364 adjust_screen();
367 void pm_using_vga(com32sys_t *regs)
369 using_vga(regs->eax.b[0], regs->ecx.w[0], regs->edx.w[0]);