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 | // Various inline routines for video I/O␊ |
33 | ␊ |
34 | static biosBuf_t bb;␊ |
35 | ␊ |
36 | #if UNUSED␊ |
37 | ␊ |
38 | //==============================================================================␊ |
39 | #if UNUSED␊ |
40 | static inline void␊ |
41 | outi (int port, int index, int val)␊ |
42 | {␊ |
43 | outw (port, (val << 8) | index);␊ |
44 | }␊ |
45 | ␊ |
46 | //==============================================================================␊ |
47 | static inline void␊ |
48 | outib (int port, int index, int val)␊ |
49 | {␊ |
50 | outb (port, index);␊ |
51 | outb (port + 1, val);␊ |
52 | }␊ |
53 | ␊ |
54 | //==============================================================================␊ |
55 | ␊ |
56 | static inline int␊ |
57 | ini (int port, int index)␊ |
58 | {␊ |
59 | outb (port, index);␊ |
60 | return inb (port + 1);␊ |
61 | }␊ |
62 | ␊ |
63 | //==============================================================================␊ |
64 | ␊ |
65 | static inline void␊ |
66 | rmwi(int port, int index, int clear, int set)␊ |
67 | {␊ |
68 | outb (port, index);␊ |
69 | outb (port + 1, (inb (port + 1) & ~clear) | set);␊ |
70 | }␊ |
71 | #endif /* UNUSED */␊ |
72 | //==============================================================================␊ |
73 | ␊ |
74 | #endif /* UNUSED */␊ |
75 | ␊ |
76 | int getVBEInfo( void * infoBlock )␊ |
77 | {␊ |
78 | bb.intno = 0x10;␊ |
79 | bb.eax.rr = funcGetControllerInfo;␊ |
80 | bb.es = SEG( infoBlock );␊ |
81 | bb.edi.rr = OFF( infoBlock );␊ |
82 | bios( &bb );␊ |
83 | return(bb.eax.r.h);␊ |
84 | }␊ |
85 | ␊ |
86 | //==============================================================================␊ |
87 | ␊ |
88 | int getVBEModeInfo( int mode, void * minfo_p )␊ |
89 | {␊ |
90 | bb.intno = 0x10;␊ |
91 | bb.eax.rr = funcGetModeInfo;␊ |
92 | bb.ecx.rr = mode;␊ |
93 | bb.es = SEG(minfo_p);␊ |
94 | bb.edi.rr = OFF(minfo_p);␊ |
95 | bios(&bb);␊ |
96 | return(bb.eax.r.h);␊ |
97 | }␊ |
98 | ␊ |
99 | //==============================================================================␊ |
100 | ␊ |
101 | int getVBEDACFormat(unsigned char *format)␊ |
102 | {␊ |
103 | bb.intno = 0x10;␊ |
104 | bb.eax.rr = funcGetSetPaletteFormat;␊ |
105 | bb.ebx.r.l = subfuncGet;␊ |
106 | bios(&bb);␊ |
107 | *format = bb.ebx.r.h;␊ |
108 | return(bb.eax.r.h);␊ |
109 | }␊ |
110 | ␊ |
111 | //==============================================================================␊ |
112 | ␊ |
113 | int 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 | ␊ |
134 | int Round(double f)␊ |
135 | {␊ |
136 | ␉return (int)(f + 0.5);␊ |
137 | }␊ |
138 | ␊ |
139 | /*␊ |
140 | * from http://www.azillionmonkeys.com/qed/sqroot.html␊ |
141 | */␊ |
142 | ␊ |
143 | double Sqrt( double y )␊ |
144 | {␊ |
145 | ␉double x, z, tempf;␊ |
146 | ␉unsigned long *tfptr = ((unsigned long *)&tempf) + 1;␊ |
147 | ␉␊ |
148 | ␉tempf = y;␊ |
149 | ␉*tfptr = (0xbfcdd90a - *tfptr)>>1; /* estimate of 1/sqrt(y) */␊ |
150 | ␉x = tempf;␊ |
151 | ␉z = y*0.5; /* hoist out the “/2” */␊ |
152 | ␉x = (1.5*x) - (x*x)*(x*z); /* iteration formula */␊ |
153 | ␉x = (1.5*x) - (x*x)*(x*z);␊ |
154 | ␉x = (1.5*x) - (x*x)*(x*z);␊ |
155 | ␉x = (1.5*x) - (x*x)*(x*z);␊ |
156 | ␉x = (1.5*x) - (x*x)*(x*z);␊ |
157 | ␉return x*y;␊ |
158 | }␊ |
159 | ␊ |
160 | int generateCRTCTiming( unsigned short width,␊ |
161 | unsigned short height,␊ |
162 | unsigned long paramValue, ␊ |
163 | int paramType,␊ |
164 | VBECRTCInfoBlock * timing )␊ |
165 | {␊ |
166 | double 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_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 | double h_period_est = ((1 / v_field_rate_in) - kMinVSyncPlusBP / 1000000) /␊ |
199 | (v_lines + (2 * top_margin_lines) + kMinFrontPorch + interlace) * ␊ |
200 | 1000000;␊ |
201 | ␊ |
202 | // Estimate the vertical field frequency␊ |
203 | double v_field_rate_est = 1 / h_period_est / v_total_lines * 1000000;␊ |
204 | ␊ |
205 | // Number of lines in Vsync + back porch␊ |
206 | v_sync_plus_bp = Round(kMinVSyncPlusBP / h_period_est);␊ |
207 | ␊ |
208 | // Total number of lines in Vetical field period␊ |
209 | v_total_lines = v_lines + top_margin_lines + bot_margin_lines +␊ |
210 | v_sync_plus_bp + interlace + kMinFrontPorch;␊ |
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 | double 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 | ␊ |
267 | int 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 | ␊ |
280 | int 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 | ␊ |
293 | int 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 | ␊ |
306 | int 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 | ␊ |
315 | int 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 | |