]> git.saurik.com Git - apple/boot.git/blob - i386/libsaio/vbe.c
boot-80.1.tar.gz
[apple/boot.git] / i386 / libsaio / vbe.c
1 /*
2 * Copyright (c) 1999 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
6 * Portions Copyright (c) 1999 Apple Computer, Inc. All Rights
7 * Reserved. This file contains Original Code and/or Modifications of
8 * Original Code as defined in and that are subject to the Apple Public
9 * Source License Version 1.1 (the "License"). You may not use this file
10 * except in compliance with the License. Please obtain a copy of the
11 * License at http://www.apple.com/publicsource and read it before using
12 * this file.
13 *
14 * The Original Code and all software distributed under the License are
15 * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER
16 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
17 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE OR NON- INFRINGEMENT. Please see the
19 * License for the specific language governing rights and limitations
20 * under the License.
21 *
22 * @APPLE_LICENSE_HEADER_END@
23 */
24 /*
25 * Copyright 1993 NeXT, Inc.
26 * All rights reserved.
27 */
28 #include "io_inline.h"
29 #include "libsaio.h"
30 #include "vga.h"
31 #include "vbe.h"
32 #include "kernBootStruct.h"
33 #include "appleClut8.h"
34
35 /*
36 * Graphics mode settings.
37 */
38 BOOL in_linear_mode;
39 unsigned char * frame_buffer;
40 unsigned short screen_width;
41 unsigned short screen_height;
42 unsigned char bits_per_pixel;
43 unsigned short screen_rowbytes;
44
45 /*
46 * Various inline routines for video I/O
47 */
48 static inline void
49 outi (int port, int index, int val)
50 {
51 outw (port, (val << 8) | index);
52 }
53
54 static inline void
55 outib (int port, int index, int val)
56 {
57 outb (port, index);
58 outb (port + 1, val);
59 }
60
61 static inline int
62 ini (int port, int index)
63 {
64 outb (port, index);
65 return inb (port + 1);
66 }
67
68 static inline void
69 rmwi (int port, int index, int clear, int set)
70 {
71 outb (port, index);
72 outb (port + 1, (inb (port + 1) & ~clear) | set);
73 }
74
75 /*
76 * Local Prototypes
77 */
78 void setupPalette(VBEPalette * p, const unsigned char * g);
79
80 /*
81 * Globals
82 */
83 static biosBuf_t bb;
84
85 #if 0
86 static char *models[] = { "Text",
87 "CGA",
88 "Hercules",
89 "Planar",
90 "Packed Pixel",
91 "Non-Chain 4",
92 "Direct Color",
93 "YUV" };
94 #endif
95
96 int
97 set_linear_video_mode(unsigned short mode)
98 {
99 VBEInfoBlock vinfo;
100 VBEModeInfoBlock minfo;
101 int err;
102 VBEPalette palette;
103
104 do {
105 /*
106 * See if VESA is around.
107 */
108 err = getVBEInfo(&vinfo);
109 if (err != errSuccess)
110 {
111 printf("VESA not available.\n");
112 break;
113 }
114
115 #if 0
116 /*
117 * See if this mode is supported.
118 */
119 err = getVBEModeInfo(mode, &minfo);
120 if ( !((err == errSuccess) &&
121 (minfo.ModeAttributes & maModeIsSupportedBit) &&
122 (minfo.ModeAttributes & maGraphicsModeBit) /* &&
123 (minfo.ModeAttributes & maLinearFrameBufferAvailBit)*/) )
124 {
125 printf("Mode %d is not supported.\n", mode);
126 err = errFuncNotSupported;
127 break;
128 }
129 #endif
130
131 /*
132 * Set the mode.
133 */
134 err = setVBEMode(mode | kLinearFrameBufferBit );
135 if ( err != errSuccess )
136 {
137 if (vinfo.VESAVersion < MIN_VESA_VERSION)
138 {
139 printf("Video Card is VESA %d.%d. It must be at least VESA %d.%d\n",
140 vinfo.VESAVersion >> 8,
141 vinfo.VESAVersion & 0xff,
142 MIN_VESA_VERSION >> 8,
143 MIN_VESA_VERSION & 0xff);
144 }
145 else
146 {
147 printf("Error #%d in set video mode.\n", err);
148 }
149 break;
150 }
151
152 /*
153 * Get mode info.
154 */
155 err = getVBEModeInfo(mode, &minfo);
156 if ( err != errSuccess )
157 {
158 printf("Error #%d in get mode info.\n", err);
159 break;
160 }
161
162 /*
163 * Set the palette.
164 */
165 if (( vinfo.VESAVersion >= MIN_VESA_VERSION ) &&
166 ( minfo.BitsPerPixel == 8 ))
167 {
168 setupPalette(&palette, appleClut8);
169 if ((err = setVBEPalette(palette)) != errSuccess)
170 {
171 printf("Error #%d in setting palette.\n", err);
172 break;
173 }
174 }
175
176 err = errSuccess;
177 in_linear_mode = YES;
178 screen_width = minfo.XResolution;
179 screen_height = minfo.YResolution;
180 bits_per_pixel = minfo.BitsPerPixel;
181 screen_rowbytes = minfo.BytesPerScanline;
182
183 /* The S3 video card reports 15 bits... the video console driver
184 * Can't deal.. set it to 16.
185 */
186 if (bits_per_pixel > 8 && bits_per_pixel < 16)
187 bits_per_pixel = 16;
188
189 frame_buffer = (unsigned char *) ADDRESS(minfo.PhysBasePtr_low,
190 minfo.PhysBasePtr_1,
191 minfo.PhysBasePtr_2,
192 minfo.PhysBasePtr_high);
193 }
194 while ( 0 );
195
196 return err;
197 }
198
199 void setupPalette(VBEPalette * p, const unsigned char * g)
200 {
201 int i;
202 unsigned char * source = (unsigned char *) g;
203
204 for (i = 0; i < 256; i++)
205 {
206 (*p)[i] = 0;
207 (*p)[i] |= ((unsigned long)((*source++) >> 2)) << 16; // Red
208 (*p)[i] |= ((unsigned long)((*source++) >> 2)) << 8; // Green
209 (*p)[i] |= ((unsigned long)((*source++) >> 2)); // Blue
210 }
211 }
212
213 int getVBEInfo(void *vinfo_p)
214 {
215 bb.intno = 0x10;
216 bb.eax.rr = funcGetControllerInfo;
217 bb.es = SEG(vinfo_p);
218 bb.edi.rr = OFF(vinfo_p);
219 bios(&bb);
220 return(bb.eax.r.h);
221 }
222
223 int getVBEModeInfo(int mode, void *minfo_p)
224 {
225 bb.intno = 0x10;
226 bb.eax.rr = funcGetModeInfo;
227 bb.ecx.rr = mode;
228 bb.es = SEG(minfo_p);
229 bb.edi.rr = OFF(minfo_p);
230 bios(&bb);
231 return(bb.eax.r.h);
232 }
233
234 int getVBEDACFormat(unsigned char *format)
235 {
236 bb.intno = 0x10;
237 bb.eax.rr = funcGetSetPaletteFormat;
238 bb.ebx.r.l = subfuncGet;
239 bios(&bb);
240 *format = bb.ebx.r.h;
241 return(bb.eax.r.h);
242 }
243
244 int setVBEDACFormat(unsigned char format)
245 {
246 bb.intno = 0x10;
247 bb.eax.rr = funcGetSetPaletteFormat;
248 bb.ebx.r.l = subfuncSet;
249 bb.ebx.r.h = format;
250 bios(&bb);
251 return(bb.eax.r.h);
252 }
253
254 int setVBEMode(unsigned short mode)
255 {
256 bb.intno = 0x10;
257 bb.eax.rr = funcSetMode;
258 bb.ebx.rr = mode;
259 bios(&bb);
260 return(bb.eax.r.h);
261 }
262
263 int setVBEPalette(void *palette)
264 {
265 bb.intno = 0x10;
266 bb.eax.rr = funcGetSetPaletteData;
267 bb.ebx.r.l = subfuncSet;
268 bb.ecx.rr = 256;
269 bb.edx.rr = 0;
270 bb.es = SEG(palette);
271 bb.edi.rr = OFF(palette);
272 bios(&bb);
273 return(bb.eax.r.h);
274 }
275
276 int getVBEPalette(void *palette)
277 {
278 bb.intno = 0x10;
279 bb.eax.rr = funcGetSetPaletteData;
280 bb.ebx.r.l = subfuncGet;
281 bb.ecx.rr = 256;
282 bb.edx.rr = 0;
283 bb.es = SEG(palette);
284 bb.edi.rr = OFF(palette);
285 bios(&bb);
286 return(bb.eax.r.h);
287 }
288
289 int getVBECurrentMode(unsigned short *mode)
290 {
291 bb.intno = 0x10;
292 bb.eax.rr = funcGetCurrentMode;
293 bios(&bb);
294 *mode = bb.ebx.rr;
295 return(bb.eax.r.h);
296 }