txt/Makefile: make directories
[syslinux.git] / core / graphics.c
blob834372ff5662a469155243784a43f57146e0688e
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"
25 #include "bios.h"
26 #include "graphics.h"
28 __export uint8_t UsingVGA = 0;
29 uint16_t VGAPos; /* Pointer into VGA memory */
30 __export uint16_t *VGAFilePtr; /* Pointer into VGAFileBuf */
31 __export uint16_t VGAFontSize = 16; /* Defaults to 16 byte font */
33 __export char VGAFileBuf[VGA_FILE_BUF_SIZE]; /* Unmangled VGA image name */
34 __export char VGAFileMBuf[FILENAME_MAX]; /* Mangled VGA image name */
36 static uint8_t VGARowBuffer[640 + 80]; /* Decompression buffer */
37 static uint8_t VGAPlaneBuffer[(640/8) * 4]; /* Plane buffers */
39 extern uint16_t GXPixCols;
40 extern uint16_t GXPixRows;
42 /* Maps colors to consecutive DAC registers */
43 static uint8_t linear_color[] = { 0, 1, 2, 3, 4, 5, 6, 7, 8,
44 9, 10, 11, 12, 13, 14, 15, 0 };
46 static FILE *fd;
48 typedef struct {
49 uint32_t LSSMagic; /* Magic number */
50 uint16_t GraphXSize; /* Width of splash screen file */
51 uint16_t GraphYSize; /* Height of splash screen file */
52 uint8_t GraphColorMap[3*16];
53 } lssheader_t;
55 static lssheader_t LSSHeader;
57 #define LSSMagic LSSHeader.LSSMagic
58 #define GraphXSize LSSHeader.GraphXSize
59 #define GraphYSize LSSHeader.GraphYSize
62 * Enable VGA graphics, if possible. Return 0 on success.
64 static int vgasetmode(void)
66 com32sys_t ireg, oreg;
68 if (UsingVGA)
69 return 0; /* Nothing to do... */
71 memset(&ireg, 0, sizeof(ireg));
72 memset(&oreg, 0, sizeof(oreg));
74 if (UsingVGA & 0x4) {
76 * We're in VESA mode, which means VGA; use VESA call
77 * to revert the mode, and then call the conventional
78 * mode-setting for good measure...
80 ireg.eax.w[0] = 0x4F02;
81 ireg.ebx.w[0] = 0x0012;
82 __intcall(0x10, &ireg, &oreg);
83 } else {
84 /* Get video card and monitor */
85 ireg.eax.w[0] = 0x1A00;
86 __intcall(0x10, &ireg, &oreg);
87 oreg.ebx.b[0] -= 7; /* BL=07h and BL=08h OK */
89 if (oreg.ebx.b[0] > 1)
90 return -1;
94 * Set mode.
96 ireg.eax.w[0] = 0x0012; /* Set mode = 640x480 VGA 16 colors */
97 __intcall(0x10, &ireg, &oreg);
99 ireg.edx.w[0] = (uint32_t)linear_color;
100 ireg.eax.w[0] = 0x1002; /* Write color registers */
101 __intcall(0x10, &ireg, &oreg);
103 UsingVGA = 1;
105 /* Set GXPixCols and GXPixRows */
106 GXPixCols = 640;
107 GXPixRows = 480;
109 use_font();
110 ScrollAttribute = 0;
112 return 0;
115 static inline char getnybble(void)
117 char data = getc(fd);
119 if (data & 0x10) {
120 data &= 0x0F;
121 return data;
124 data = getc(fd);
125 return (data & 0x0F);
129 * rledecode:
130 * Decode a pixel row in RLE16 format.
132 * 'in': input (RLE16 encoded) buffer
133 * 'out': output (decoded) buffer
134 * 'count': pixel count
136 static void rledecode(uint8_t *out, size_t count)
138 uint8_t prev_pixel = 0;
139 size_t size = count;
140 uint8_t data;
141 int i;
143 again:
144 for (i = 0; i < size; i++) {
146 data = getnybble();
147 if (data == prev_pixel)
148 break;
150 *out++ = data;
151 prev_pixel = data;
154 size -= i;
155 if (!size)
156 return;
158 /* Start of run sequence */
159 data = getnybble();
160 if (data == 0) {
161 /* long run */
162 uint8_t hi;
164 data = getnybble();
165 hi = getnybble();
166 hi <<= 4;
167 data |= hi;
168 data += 16;
171 /* dorun */
172 for (i = 0; i < data; i++)
173 *out++ = prev_pixel;
175 size -= i;
176 if (size)
177 goto again;
181 * packedpixel2vga:
182 * Convert packed-pixel to VGA bitplanes
184 * 'in': packed pixel string (640 pixels)
185 * 'out': output (four planes @ 640/8 = 80 bytes)
186 * 'count': pixel count (multiple of 8)
188 static void packedpixel2vga(const uint8_t *in, uint8_t *out)
190 int i, j, k;
192 for (i = 0; i < 4; i++) {
193 const uint8_t *ip = in;
195 for (j = 0; j < 640/8; j++) {
196 uint8_t ob = 0;
198 for (k = 0; k < 8; k++) {
199 uint8_t px = *ip++;
200 ob = (ob << 1) | ((px >> i) & 1);
203 *out++ = ob;
209 * outputvga:
210 * Output four subsequent lines of VGA data
212 * 'in': four planes @ 640/8=80 bytes
213 * 'out': pointer into VGA memory
215 static void outputvga(const void *in, void *out)
217 int i;
219 /* Select the sequencer mask */
220 outb(VGA_SEQ_IX_MAP_MASK, VGA_SEQ_ADDR);
222 for (i = 1; i <= 8; i <<= 1) {
223 /* Select the bit plane to write */
224 outb(i, VGA_SEQ_DATA);
225 memcpy(out, in, 640/8);
226 in = (const char *)in + 640/8;
231 * Display a graphical splash screen.
233 __export void vgadisplayfile(FILE *_fd)
235 char *p;
236 int size;
238 fd = _fd;
241 * This is a cheap and easy way to make sure the screen is
242 * cleared in case we were in graphics mode aready.
244 syslinux_force_text_mode();
245 vgasetmode();
247 size = 4+2*2+16*3;
248 p = (char *)&LSSHeader;
250 /* Load the header */
251 while (size--)
252 *p = getc(fd);
254 if (*p != EOF) {
255 com32sys_t ireg, oreg;
256 uint16_t rows;
257 int i;
259 /* The header WILL be in the first chunk. */
260 if (LSSMagic != 0x1413f33d)
261 return;
263 memset(&ireg, 0, sizeof(ireg));
265 /* Color map offset */
266 ireg.edx.w[0] = offsetof(lssheader_t, GraphColorMap);
268 ireg.eax.w[0] = 0x1012; /* Set RGB registers */
269 ireg.ebx.w[0] = 0; /* First register number */
270 ireg.ecx.w[0] = 16; /* 16 registers */
271 __intcall(0x10, &ireg, &oreg);
273 /* Number of pixel rows */
274 rows = (GraphYSize + VGAFontSize) - 1;
275 rows = rows / VGAFontSize;
276 if (rows >= VidRows)
277 rows = VidRows - 1;
279 memset(&ireg, 0, sizeof(ireg));
281 ireg.edx.b[1] = rows;
282 ireg.eax.b[1] = 2;
283 ireg.ebx.w[0] = 0;
285 /* Set cursor below image */
286 __intcall(0x10, &ireg, &oreg);
288 rows = GraphYSize; /* Number of graphics rows */
289 VGAPos = 0;
291 for (i = 0; i < rows; i++) {
292 /* Pre-clear the row buffer */
293 memset(VGARowBuffer, 0, 640);
295 /* Decode one row */
296 rledecode(VGARowBuffer, GraphXSize);
298 packedpixel2vga(VGARowBuffer, VGAPlaneBuffer);
299 outputvga(VGAPlaneBuffer, MK_PTR(0xA000, VGAPos));
300 VGAPos += 640/8;
306 * Disable VGA graphics.
308 __export void syslinux_force_text_mode(void)
310 com32sys_t ireg, oreg;
312 /* Already in text mode? */
313 if (!UsingVGA)
314 return;
316 if (UsingVGA & 0x4) {
317 /* VESA return to normal video mode */
318 memset(&ireg, 0, sizeof(ireg));
320 ireg.eax.w[0] = 0x4F02; /* Set SuperVGA video mode */
321 ireg.ebx.w[0] = 0x0003;
322 __intcall(0x10, &ireg, &oreg);
325 /* Return to normal video mode */
326 memset(&ireg, 0, sizeof(ireg));
327 ireg.eax.w[0] = 0x0003;
328 __intcall(0x10, &ireg, &oreg);
330 UsingVGA = 0;
332 ScrollAttribute = 0x7;
333 /* Restore text font/data */
334 use_font();
337 static void vgacursorcommon(char data)
339 if (UsingVGA) {
340 com32sys_t ireg;
342 ireg.eax.b[0] = data;
343 ireg.eax.b[1] = 0x09;
344 ireg.ebx.w[0] = 0x0007;
345 ireg.ecx.w[0] = 1;
346 __intcall(0x10, &ireg, NULL);
350 void vgahidecursor(void)
352 vgacursorcommon(' ');
355 void vgashowcursor(void)
357 vgacursorcommon('_');
360 __export void using_vga(uint8_t vga, uint16_t pix_cols, uint16_t pix_rows)
362 UsingVGA = vga;
363 GXPixCols = pix_cols;
364 GXPixRows = pix_rows;
366 if (!(UsingVGA & 0x08))
367 adjust_screen();
370 void pm_using_vga(com32sys_t *regs)
372 using_vga(regs->eax.b[0], regs->ecx.w[0], regs->edx.w[0]);