Chameleon

Chameleon Svn Source Tree

Root/branches/andyvand/i386/libsaio/vbe.c

1/*
2 * Copyright (c) 1999-2003 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
6 * Portions Copyright (c) 1999-2003 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 2.0 (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
29#include "libsaio.h"
30#include "vbe.h"
31
32/*
33 * Various inline routines for video I/O
34 */
35static inline void
36outi (int port, int index, int val)
37{
38 outw (port, (val << 8) | index);
39}
40
41static inline void
42outib (int port, int index, int val)
43{
44 outb (port, index);
45 outb (port + 1, val);
46}
47
48static inline int
49ini (int port, int index)
50{
51 outb (port, index);
52 return inb (port + 1);
53}
54
55static inline void
56rmwi (int port, int index, int clear, int set)
57{
58 outb (port, index);
59 outb (port + 1, (inb (port + 1) & ~clear) | set);
60}
61
62/*
63 * Globals
64 */
65static biosBuf_t bb;
66
67int getEDID( void * edidBlock, UInt8 block)
68{
69bzero(&bb, sizeof(bb));
70 bb.intno = 0x10;
71 bb.eax.rr = funcGetEDID;
72bb.ebx.r.l= 0x01;
73bb.edx.rr = block;
74
75 bb.es = SEG( edidBlock );
76 bb.edi.rr = OFF( edidBlock );
77
78 bios( &bb );
79 return(bb.eax.r.h);
80}
81
82int getVBEInfo( void * infoBlock )
83{
84 bb.intno = 0x10;
85 bb.eax.rr = funcGetControllerInfo;
86 bb.es = SEG( infoBlock );
87 bb.edi.rr = OFF( infoBlock );
88 bios( &bb );
89 return(bb.eax.r.h);
90}
91
92int getVBEModeInfo( int mode, void * minfo_p )
93{
94 bb.intno = 0x10;
95 bb.eax.rr = funcGetModeInfo;
96 bb.ecx.rr = mode;
97 bb.es = SEG(minfo_p);
98 bb.edi.rr = OFF(minfo_p);
99 bios(&bb);
100 return(bb.eax.r.h);
101}
102
103int getVBEDACFormat(unsigned char *format)
104{
105 bb.intno = 0x10;
106 bb.eax.rr = funcGetSetPaletteFormat;
107 bb.ebx.r.l = subfuncGet;
108 bios(&bb);
109 *format = bb.ebx.r.h;
110 return(bb.eax.r.h);
111}
112
113int setVBEDACFormat(unsigned char format)
114{
115 bb.intno = 0x10;
116 bb.eax.rr = funcGetSetPaletteFormat;
117 bb.ebx.r.l = subfuncSet;
118 bb.ebx.r.h = format;
119 bios(&bb);
120 return(bb.eax.r.h);
121}
122
123/*
124 * Default GTF parameter values.
125 */
126#define kCellGranularity 8.0 // character cell granularity
127#define kMinVSyncPlusBP 550.0 // min VSync + BP interval (us)
128#define kMinFrontPorch 1.0 // minimum front porch in lines(V)/cells(H)
129#define kVSyncLines 3.0 // width of VSync in lines
130#define kHSyncWidth 8.0 // HSync as a percent of total line width
131#define kC 30.0 // C = (C'-J) * (K/256) + J
132#define kM 300.0 // M = K/256 * M'
133
134int Round(double f)
135{
136return (int)(f + 0.5);
137}
138
139/*
140 * from http://www.azillionmonkeys.com/qed/sqroot.html
141 */
142
143double Sqrt( double y )
144{
145double x, z, tempf;
146unsigned long *tfptr = ((unsigned long *)&tempf) + 1;
147
148tempf = y;
149*tfptr = (0xbfcdd90a - *tfptr)>>1; /* estimate of 1/sqrt(y) */
150x = tempf;
151z = y*0.5; /* hoist out the “/2” */
152x = (1.5*x) - (x*x)*(x*z); /* iteration formula */
153x = (1.5*x) - (x*x)*(x*z);
154x = (1.5*x) - (x*x)*(x*z);
155x = (1.5*x) - (x*x)*(x*z);
156x = (1.5*x) - (x*x)*(x*z);
157return x*y;
158}
159
160int generateCRTCTiming( unsigned short width,
161 unsigned short height,
162 unsigned long paramValue,
163 int paramType,
164 VBECRTCInfoBlock * timing )
165{
166 double h_period_est, h_freq, h_period, h_total_pixels, h_sync_pixels;
167 double h_active_pixels, h_ideal_duty_cycle, h_blank_pixels, pixel_freq = 0;
168 double v_sync_plus_bp = 0, v_total_lines = 0, v_field_rate_est, v_frame_rate = 0;
169 const double h_pixels = (double) width;
170 const double v_lines = (double) height;
171
172 enum {
173 left_margin_pixels = 0,
174 right_margin_pixels = 0,
175 top_margin_lines = 0,
176 bot_margin_lines = 0,
177 interlace = 0
178 };
179
180 // Total number of active pixels in image and both margins
181 h_active_pixels = h_pixels + left_margin_pixels + right_margin_pixels;
182
183 if (paramType == kCRTCParamPixelClock)
184 {
185 // Pixel clock provided in MHz
186 pixel_freq = (double) paramValue / 1000000;
187
188 // Ideal horizontal period from the blanking duty cycle equation
189 h_period = ((kC - 100) + (Sqrt(((100 - kC) * (100 - kC)) + (0.4 * kM *
190 (h_active_pixels + right_margin_pixels + left_margin_pixels) /
191 pixel_freq)))) / 2.0 / kM * 1000;
192 }
193 else /* kCRTCParamRefreshRate */
194 {
195 double v_field_rate_in = (double) paramValue;
196
197 // Estimate the horizontal period
198 h_period_est = ((1 / v_field_rate_in) - kMinVSyncPlusBP / 1000000) /
199 (v_lines + (2 * top_margin_lines) + kMinFrontPorch + interlace) *
200 1000000;
201
202 // Number of lines in Vsync + back porch
203 v_sync_plus_bp = Round(kMinVSyncPlusBP / h_period_est);
204
205 // Total number of lines in Vetical field period
206 v_total_lines = v_lines + top_margin_lines + bot_margin_lines +
207 v_sync_plus_bp + interlace + kMinFrontPorch;
208
209 // Estimate the vertical field frequency
210 v_field_rate_est = 1 / h_period_est / v_total_lines * 1000000;
211
212 // Find the actual horizontal period
213 h_period = h_period_est / (v_field_rate_in / v_field_rate_est);
214
215 // Find the vertical frame rate (no interlace)
216 v_frame_rate = 1 / h_period / v_total_lines * 1000000;
217 }
218
219 // Ideal blanking duty cycle from the blanking duty cycle equation
220 h_ideal_duty_cycle = kC - (kM * h_period / 1000);
221
222 // Number of pixels in the blanking time to the nearest double character cell
223 h_blank_pixels = Round(h_active_pixels * h_ideal_duty_cycle /
224 (100 - h_ideal_duty_cycle) / (2 * kCellGranularity)) *
225 (2 * kCellGranularity);
226
227 // Total number of horizontal pixels
228 h_total_pixels = h_active_pixels + h_blank_pixels;
229
230 if (paramType == kCRTCParamPixelClock)
231 {
232 // Horizontal frequency
233 h_freq = pixel_freq / h_total_pixels * 1000;
234
235 // Number of lines in V sync + back porch
236 v_sync_plus_bp = Round(kMinVSyncPlusBP * h_freq / 1000);
237
238 // Total number of lines in vertical field period
239 v_total_lines = v_lines + top_margin_lines + bot_margin_lines +
240 interlace + v_sync_plus_bp + kMinFrontPorch;
241
242 // Vertical frame frequency
243 v_frame_rate = Round(h_freq / v_total_lines * 1000);
244 }
245 else
246 {
247 // Find pixel clock frequency
248 pixel_freq = Round(h_total_pixels / h_period);
249 }
250
251 h_sync_pixels = Round(h_total_pixels * kHSyncWidth / 100 / kCellGranularity) *
252 kCellGranularity;
253
254 timing->HTotal = h_total_pixels;
255 timing->HSyncStart = h_active_pixels + (h_blank_pixels / 2) - h_sync_pixels;
256 timing->HSyncEnd = timing->HSyncStart + h_sync_pixels;
257 timing->VTotal = v_total_lines;
258 timing->VSyncStart = v_total_lines - v_sync_plus_bp;
259 timing->VSyncEnd = timing->VSyncStart + kVSyncLines;
260 timing->Flags = kCRTCNegativeHorizontalSync;
261 timing->PixelClock = pixel_freq * 1000000;
262 timing->RefreshRate = v_frame_rate * 100;
263
264 return 0;
265}
266
267int setVBEMode(unsigned short mode, const VBECRTCInfoBlock * timing)
268{
269 bb.intno = 0x10;
270 bb.eax.rr = funcSetMode;
271 bb.ebx.rr = mode;
272 if (timing) {
273 bb.es = SEG(timing);
274 bb.edi.rr = OFF(timing);
275 }
276 bios(&bb);
277 return(bb.eax.r.h);
278}
279
280int setVBEPalette(void *palette)
281{
282 bb.intno = 0x10;
283 bb.eax.rr = funcGetSetPaletteData;
284 bb.ebx.r.l = subfuncSet;
285 bb.ecx.rr = 256;
286 bb.edx.rr = 0;
287 bb.es = SEG(palette);
288 bb.edi.rr = OFF(palette);
289 bios(&bb);
290 return(bb.eax.r.h);
291}
292
293int getVBEPalette(void *palette)
294{
295 bb.intno = 0x10;
296 bb.eax.rr = funcGetSetPaletteData;
297 bb.ebx.r.l = subfuncGet;
298 bb.ecx.rr = 256;
299 bb.edx.rr = 0;
300 bb.es = SEG(palette);
301 bb.edi.rr = OFF(palette);
302 bios(&bb);
303 return(bb.eax.r.h);
304}
305
306int getVBECurrentMode(unsigned short *mode)
307{
308 bb.intno = 0x10;
309 bb.eax.rr = funcGetCurrentMode;
310 bios(&bb);
311 *mode = bb.ebx.rr;
312 return(bb.eax.r.h);
313}
314
315int getVBEPixelClock(unsigned short mode, unsigned long * pixelClock)
316{
317 bb.intno = 0x10;
318 bb.eax.rr = funcGetSetPixelClock;
319 bb.ebx.r.l = 0;
320 bb.ecx.rx = *pixelClock;
321 bb.edx.rr = mode;
322 bios(&bb);
323 *pixelClock = bb.ecx.rx;
324 return(bb.eax.r.h);
325}
326

Archive Download this file

Revision: 142