xref: /freebsd/sys/dev/videomode/vesagtf.c (revision 0b3105a37d7adcadcb720112fed4dc4e8040be99)
1 /* $NetBSD: vesagtf.c,v 1.2 2013/09/15 15:56:07 martin Exp $ */
2 /* $FreeBSD$ */
3 
4 /*-
5  * Copyright (c) 2006 Itronix Inc.
6  * All rights reserved.
7  *
8  * Written by Garrett D'Amore for Itronix Inc.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  * 3. The name of Itronix Inc. may not be used to endorse
19  *    or promote products derived from this software without specific
20  *    prior written permission.
21  *
22  * THIS SOFTWARE IS PROVIDED BY ITRONIX INC. ``AS IS'' AND ANY EXPRESS
23  * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
24  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25  * ARE DISCLAIMED.  IN NO EVENT SHALL ITRONIX INC. BE LIABLE FOR ANY
26  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
28  * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
29  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
30  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
31  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
32  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33  */
34 
35 /*
36  * This was derived from a userland GTF program supplied by NVIDIA.
37  * NVIDIA's original boilerplate follows.
38  *
39  * Note that I have heavily modified the program for use in the EDID
40  * kernel code for NetBSD, including removing the use of floating
41  * point operations and making significant adjustments to minimize
42  * error propagation while operating with integer only math.
43  *
44  * This has required the use of 64-bit integers in a few places, but
45  * the upshot is that for a calculation of 1920x1200x85 (as an
46  * example), the error deviates by only ~.004% relative to the
47  * floating point version.  This error is *well* within VESA
48  * tolerances.
49  */
50 
51 /*
52  * Copyright (c) 2001, Andy Ritger  aritger@nvidia.com
53  * All rights reserved.
54  *
55  * Redistribution and use in source and binary forms, with or without
56  * modification, are permitted provided that the following conditions
57  * are met:
58  *
59  * o Redistributions of source code must retain the above copyright
60  *   notice, this list of conditions and the following disclaimer.
61  * o Redistributions in binary form must reproduce the above copyright
62  *   notice, this list of conditions and the following disclaimer
63  *   in the documentation and/or other materials provided with the
64  *   distribution.
65  * o Neither the name of NVIDIA nor the names of its contributors
66  *   may be used to endorse or promote products derived from this
67  *   software without specific prior written permission.
68  *
69  *
70  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
71  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT
72  * NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
73  * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
74  * THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
75  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
76  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
77  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
78  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
79  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
80  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
81  * POSSIBILITY OF SUCH DAMAGE.
82  *
83  *
84  *
85  * This program is based on the Generalized Timing Formula(GTF TM)
86  * Standard Version: 1.0, Revision: 1.0
87  *
88  * The GTF Document contains the following Copyright information:
89  *
90  * Copyright (c) 1994, 1995, 1996 - Video Electronics Standards
91  * Association. Duplication of this document within VESA member
92  * companies for review purposes is permitted. All other rights
93  * reserved.
94  *
95  * While every precaution has been taken in the preparation
96  * of this standard, the Video Electronics Standards Association and
97  * its contributors assume no responsibility for errors or omissions,
98  * and make no warranties, expressed or implied, of functionality
99  * of suitability for any purpose. The sample code contained within
100  * this standard may be used without restriction.
101  *
102  *
103  *
104  * The GTF EXCEL(TM) SPREADSHEET, a sample (and the definitive)
105  * implementation of the GTF Timing Standard, is available at:
106  *
107  * ftp://ftp.vesa.org/pub/GTF/GTF_V1R1.xls
108  *
109  *
110  *
111  * This program takes a desired resolution and vertical refresh rate,
112  * and computes mode timings according to the GTF Timing Standard.
113  * These mode timings can then be formatted as an XFree86 modeline
114  * or a mode description for use by fbset(8).
115  *
116  *
117  *
118  * NOTES:
119  *
120  * The GTF allows for computation of "margins" (the visible border
121  * surrounding the addressable video); on most non-overscan type
122  * systems, the margin period is zero.  I've implemented the margin
123  * computations but not enabled it because 1) I don't really have
124  * any experience with this, and 2) neither XFree86 modelines nor
125  * fbset fb.modes provide an obvious way for margin timings to be
126  * included in their mode descriptions (needs more investigation).
127  *
128  * The GTF provides for computation of interlaced mode timings;
129  * I've implemented the computations but not enabled them, yet.
130  * I should probably enable and test this at some point.
131  *
132  *
133  *
134  * TODO:
135  *
136  * o Add support for interlaced modes.
137  *
138  * o Implement the other portions of the GTF: compute mode timings
139  *   given either the desired pixel clock or the desired horizontal
140  *   frequency.
141  *
142  * o It would be nice if this were more general purpose to do things
143  *   outside the scope of the GTF: like generate double scan mode
144  *   timings, for example.
145  *
146  * o Printing digits to the right of the decimal point when the
147  *   digits are 0 annoys me.
148  *
149  * o Error checking.
150  *
151  */
152 
153 
154 #ifdef	_KERNEL
155 #include <sys/cdefs.h>
156 
157 __FBSDID("$FreeBSD$");
158 #include <sys/types.h>
159 #include <sys/param.h>
160 #include <sys/systm.h>
161 #include <dev/videomode/videomode.h>
162 #include <dev/videomode/vesagtf.h>
163 #else
164 #include <stdio.h>
165 #include <stdlib.h>
166 #include <sys/types.h>
167 #include "videomode.h"
168 #include "vesagtf.h"
169 
170 void print_xf86_mode(struct videomode *m);
171 #endif
172 
173 #define CELL_GRAN         8     /* assumed character cell granularity        */
174 
175 /* C' and M' are part of the Blanking Duty Cycle computation */
176 /*
177  * #define C_PRIME           (((C - J) * K/256.0) + J)
178  * #define M_PRIME           (K/256.0 * M)
179  */
180 
181 /*
182  * C' and M' multiplied by 256 to give integer math.  Make sure to
183  * scale results using these back down, appropriately.
184  */
185 #define	C_PRIME256(p)	  (((p->C - p->J) * p->K) + (p->J * 256))
186 #define	M_PRIME256(p)	  (p->K * p->M)
187 
188 #define	DIVIDE(x,y)	(((x) + ((y) / 2)) / (y))
189 
190 /*
191  * print_value() - print the result of the named computation; this is
192  * useful when comparing against the GTF EXCEL spreadsheet.
193  */
194 
195 #ifdef GTFDEBUG
196 
197 static void
198 print_value(int n, const char *name, unsigned val)
199 {
200         printf("%2d: %-27s: %u\n", n, name, val);
201 }
202 #else
203 #define	print_value(n, name, val)
204 #endif
205 
206 
207 /*
208  * vert_refresh() - as defined by the GTF Timing Standard, compute the
209  * Stage 1 Parameters using the vertical refresh frequency.  In other
210  * words: input a desired resolution and desired refresh rate, and
211  * output the GTF mode timings.
212  *
213  * XXX All the code is in place to compute interlaced modes, but I don't
214  * feel like testing it right now.
215  *
216  * XXX margin computations are implemented but not tested (nor used by
217  * XFree86 of fbset mode descriptions, from what I can tell).
218  */
219 
220 void
221 vesagtf_mode_params(unsigned h_pixels, unsigned v_lines, unsigned freq,
222     struct vesagtf_params *params, int flags, struct videomode *vmp)
223 {
224     unsigned v_field_rqd;
225     unsigned top_margin;
226     unsigned bottom_margin;
227     unsigned interlace;
228     uint64_t h_period_est;
229     unsigned vsync_plus_bp;
230     unsigned v_back_porch __unused;
231     unsigned total_v_lines;
232     uint64_t v_field_est;
233     uint64_t h_period;
234     unsigned v_field_rate;
235     unsigned v_frame_rate __unused;
236     unsigned left_margin;
237     unsigned right_margin;
238     unsigned total_active_pixels;
239     uint64_t ideal_duty_cycle;
240     unsigned h_blank;
241     unsigned total_pixels;
242     unsigned pixel_freq;
243 
244     unsigned h_sync;
245     unsigned h_front_porch;
246     unsigned v_odd_front_porch_lines;
247 
248 #ifdef	GTFDEBUG
249     unsigned h_freq;
250 #endif
251 
252     /*  1. In order to give correct results, the number of horizontal
253      *  pixels requested is first processed to ensure that it is divisible
254      *  by the character size, by rounding it to the nearest character
255      *  cell boundary:
256      *
257      *  [H PIXELS RND] = ((ROUND([H PIXELS]/[CELL GRAN RND],0))*[CELLGRAN RND])
258      */
259 
260     h_pixels = DIVIDE(h_pixels, CELL_GRAN) * CELL_GRAN;
261 
262     print_value(1, "[H PIXELS RND]", h_pixels);
263 
264 
265     /*  2. If interlace is requested, the number of vertical lines assumed
266      *  by the calculation must be halved, as the computation calculates
267      *  the number of vertical lines per field. In either case, the
268      *  number of lines is rounded to the nearest integer.
269      *
270      *  [V LINES RND] = IF([INT RQD?]="y", ROUND([V LINES]/2,0),
271      *                                     ROUND([V LINES],0))
272      */
273 
274     v_lines = (flags & VESAGTF_FLAG_ILACE) ? DIVIDE(v_lines, 2) : v_lines;
275 
276     print_value(2, "[V LINES RND]", v_lines);
277 
278 
279     /*  3. Find the frame rate required:
280      *
281      *  [V FIELD RATE RQD] = IF([INT RQD?]="y", [I/P FREQ RQD]*2,
282      *                                          [I/P FREQ RQD])
283      */
284 
285     v_field_rqd = (flags & VESAGTF_FLAG_ILACE) ? (freq * 2) : (freq);
286 
287     print_value(3, "[V FIELD RATE RQD]", v_field_rqd);
288 
289 
290     /*  4. Find number of lines in Top margin:
291      *  5. Find number of lines in Bottom margin:
292      *
293      *  [TOP MARGIN (LINES)] = IF([MARGINS RQD?]="Y",
294      *          ROUND(([MARGIN%]/100*[V LINES RND]),0),
295      *          0)
296      *
297      *  Ditto for bottom margin.  Note that instead of %, we use PPT, which
298      *  is parts per thousand.  This helps us with integer math.
299      */
300 
301     top_margin = bottom_margin = (flags & VESAGTF_FLAG_MARGINS) ?
302 	DIVIDE(v_lines * params->margin_ppt, 1000) : 0;
303 
304     print_value(4, "[TOP MARGIN (LINES)]", top_margin);
305     print_value(5, "[BOT MARGIN (LINES)]", bottom_margin);
306 
307 
308     /*  6. If interlace is required, then set variable [INTERLACE]=0.5:
309      *
310      *  [INTERLACE]=(IF([INT RQD?]="y",0.5,0))
311      *
312      *  To make this integer friendly, we use some special hacks in step
313      *  7 below.  Please read those comments to understand why I am using
314      *  a whole number of 1.0 instead of 0.5 here.
315      */
316     interlace = (flags & VESAGTF_FLAG_ILACE) ? 1 : 0;
317 
318     print_value(6, "[2*INTERLACE]", interlace);
319 
320 
321     /*  7. Estimate the Horizontal period
322      *
323      *  [H PERIOD EST] = ((1/[V FIELD RATE RQD]) - [MIN VSYNC+BP]/1000000) /
324      *                    ([V LINES RND] + (2*[TOP MARGIN (LINES)]) +
325      *                     [MIN PORCH RND]+[INTERLACE]) * 1000000
326      *
327      *  To make it integer friendly, we pre-multiply the 1000000 to get to
328      *  usec.  This gives us:
329      *
330      *  [H PERIOD EST] = ((1000000/[V FIELD RATE RQD]) - [MIN VSYNC+BP]) /
331      *			([V LINES RND] + (2 * [TOP MARGIN (LINES)]) +
332      *			 [MIN PORCH RND]+[INTERLACE])
333      *
334      *  The other problem is that the interlace value is wrong.  To get
335      *  the interlace to a whole number, we multiply both the numerator and
336      *  divisor by 2, so we can use a value of either 1 or 0 for the interlace
337      *  factor.
338      *
339      * This gives us:
340      *
341      * [H PERIOD EST] = ((2*((1000000/[V FIELD RATE RQD]) - [MIN VSYNC+BP])) /
342      *			 (2*([V LINES RND] + (2*[TOP MARGIN (LINES)]) +
343      *			  [MIN PORCH RND]) + [2*INTERLACE]))
344      *
345      * Finally we multiply by another 1000, to get value in picosec.
346      * Why picosec?  To minimize rounding errors.  Gotta love integer
347      * math and error propagation.
348      */
349 
350     h_period_est = DIVIDE(((DIVIDE(2000000000000ULL, v_field_rqd)) -
351 			      (2000000 * params->min_vsbp)),
352 	((2 * (v_lines + (2 * top_margin) + params->min_porch)) + interlace));
353 
354     print_value(7, "[H PERIOD EST (ps)]", h_period_est);
355 
356 
357     /*  8. Find the number of lines in V sync + back porch:
358      *
359      *  [V SYNC+BP] = ROUND(([MIN VSYNC+BP]/[H PERIOD EST]),0)
360      *
361      *  But recall that h_period_est is in psec. So multiply by 1000000.
362      */
363 
364     vsync_plus_bp = DIVIDE(params->min_vsbp * 1000000, h_period_est);
365 
366     print_value(8, "[V SYNC+BP]", vsync_plus_bp);
367 
368 
369     /*  9. Find the number of lines in V back porch alone:
370      *
371      *  [V BACK PORCH] = [V SYNC+BP] - [V SYNC RND]
372      *
373      *  XXX is "[V SYNC RND]" a typo? should be [V SYNC RQD]?
374      */
375 
376     v_back_porch = vsync_plus_bp - params->vsync_rqd;
377 
378     print_value(9, "[V BACK PORCH]", v_back_porch);
379 
380 
381     /*  10. Find the total number of lines in Vertical field period:
382      *
383      *  [TOTAL V LINES] = [V LINES RND] + [TOP MARGIN (LINES)] +
384      *                    [BOT MARGIN (LINES)] + [V SYNC+BP] + [INTERLACE] +
385      *                    [MIN PORCH RND]
386      */
387 
388     total_v_lines = v_lines + top_margin + bottom_margin + vsync_plus_bp +
389         interlace + params->min_porch;
390 
391     print_value(10, "[TOTAL V LINES]", total_v_lines);
392 
393 
394     /*  11. Estimate the Vertical field frequency:
395      *
396      *  [V FIELD RATE EST] = 1 / [H PERIOD EST] / [TOTAL V LINES] * 1000000
397      *
398      *  Again, we want to pre multiply by 10^9 to convert for nsec, thereby
399      *  making it usable in integer math.
400      *
401      *  So we get:
402      *
403      *  [V FIELD RATE EST] = 1000000000 / [H PERIOD EST] / [TOTAL V LINES]
404      *
405      *  This is all scaled to get the result in uHz.  Again, we're trying to
406      *  minimize error propagation.
407      */
408     v_field_est = DIVIDE(DIVIDE(1000000000000000ULL, h_period_est),
409 	total_v_lines);
410 
411     print_value(11, "[V FIELD RATE EST(uHz)]", v_field_est);
412 
413 
414     /*  12. Find the actual horizontal period:
415      *
416      *  [H PERIOD] = [H PERIOD EST] / ([V FIELD RATE RQD] / [V FIELD RATE EST])
417      */
418 
419     h_period = DIVIDE(h_period_est * v_field_est, v_field_rqd * 1000);
420 
421     print_value(12, "[H PERIOD(ps)]", h_period);
422 
423 
424     /*  13. Find the actual Vertical field frequency:
425      *
426      *  [V FIELD RATE] = 1 / [H PERIOD] / [TOTAL V LINES] * 1000000
427      *
428      *  And again, we convert to nsec ahead of time, giving us:
429      *
430      *  [V FIELD RATE] = 1000000 / [H PERIOD] / [TOTAL V LINES]
431      *
432      *  And another rescaling back to mHz.  Gotta love it.
433      */
434 
435     v_field_rate = DIVIDE(1000000000000ULL, h_period * total_v_lines);
436 
437     print_value(13, "[V FIELD RATE]", v_field_rate);
438 
439 
440     /*  14. Find the Vertical frame frequency:
441      *
442      *  [V FRAME RATE] = (IF([INT RQD?]="y", [V FIELD RATE]/2, [V FIELD RATE]))
443      *
444      *  N.B. that the result here is in mHz.
445      */
446 
447     v_frame_rate = (flags & VESAGTF_FLAG_ILACE) ?
448 	v_field_rate / 2 : v_field_rate;
449 
450     print_value(14, "[V FRAME RATE]", v_frame_rate);
451 
452 
453     /*  15. Find number of pixels in left margin:
454      *  16. Find number of pixels in right margin:
455      *
456      *  [LEFT MARGIN (PIXELS)] = (IF( [MARGINS RQD?]="Y",
457      *          (ROUND( ([H PIXELS RND] * [MARGIN%] / 100 /
458      *                   [CELL GRAN RND]),0)) * [CELL GRAN RND],
459      *          0))
460      *
461      *  Again, we deal with margin percentages as PPT (parts per thousand).
462      *  And the calculations for left and right are the same.
463      */
464 
465     left_margin = right_margin = (flags & VESAGTF_FLAG_MARGINS) ?
466 	DIVIDE(DIVIDE(h_pixels * params->margin_ppt, 1000),
467 	    CELL_GRAN) * CELL_GRAN : 0;
468 
469     print_value(15, "[LEFT MARGIN (PIXELS)]", left_margin);
470     print_value(16, "[RIGHT MARGIN (PIXELS)]", right_margin);
471 
472 
473     /*  17. Find total number of active pixels in image and left and right
474      *  margins:
475      *
476      *  [TOTAL ACTIVE PIXELS] = [H PIXELS RND] + [LEFT MARGIN (PIXELS)] +
477      *                          [RIGHT MARGIN (PIXELS)]
478      */
479 
480     total_active_pixels = h_pixels + left_margin + right_margin;
481 
482     print_value(17, "[TOTAL ACTIVE PIXELS]", total_active_pixels);
483 
484 
485     /*  18. Find the ideal blanking duty cycle from the blanking duty cycle
486      *  equation:
487      *
488      *  [IDEAL DUTY CYCLE] = [C'] - ([M']*[H PERIOD]/1000)
489      *
490      *  However, we have modified values for [C'] as [256*C'] and
491      *  [M'] as [256*M'].  Again the idea here is to get good scaling.
492      *  We use 256 as the factor to make the math fast.
493      *
494      *  Note that this means that we have to scale it appropriately in
495      *  later calculations.
496      *
497      *  The ending result is that our ideal_duty_cycle is 256000x larger
498      *  than the duty cycle used by VESA.  But again, this reduces error
499      *  propagation.
500      */
501 
502     ideal_duty_cycle =
503 	((C_PRIME256(params) * 1000) -
504 	    (M_PRIME256(params) * h_period / 1000000));
505 
506     print_value(18, "[IDEAL DUTY CYCLE]", ideal_duty_cycle);
507 
508 
509     /*  19. Find the number of pixels in the blanking time to the nearest
510      *  double character cell:
511      *
512      *  [H BLANK (PIXELS)] = (ROUND(([TOTAL ACTIVE PIXELS] *
513      *                               [IDEAL DUTY CYCLE] /
514      *                               (100-[IDEAL DUTY CYCLE]) /
515      *                               (2*[CELL GRAN RND])), 0))
516      *                       * (2*[CELL GRAN RND])
517      *
518      *  Of course, we adjust to make this rounding work in integer math.
519      */
520 
521     h_blank = DIVIDE(DIVIDE(total_active_pixels * ideal_duty_cycle,
522 			 (256000 * 100ULL) - ideal_duty_cycle),
523 	2 * CELL_GRAN) * (2 * CELL_GRAN);
524 
525     print_value(19, "[H BLANK (PIXELS)]", h_blank);
526 
527 
528     /*  20. Find total number of pixels:
529      *
530      *  [TOTAL PIXELS] = [TOTAL ACTIVE PIXELS] + [H BLANK (PIXELS)]
531      */
532 
533     total_pixels = total_active_pixels + h_blank;
534 
535     print_value(20, "[TOTAL PIXELS]", total_pixels);
536 
537 
538     /*  21. Find pixel clock frequency:
539      *
540      *  [PIXEL FREQ] = [TOTAL PIXELS] / [H PERIOD]
541      *
542      *  We calculate this in Hz rather than MHz, to get a value that
543      *  is usable with integer math.  Recall that the [H PERIOD] is in
544      *  nsec.
545      */
546 
547     pixel_freq = DIVIDE(total_pixels * 1000000, DIVIDE(h_period, 1000));
548 
549     print_value(21, "[PIXEL FREQ]", pixel_freq);
550 
551 
552     /*  22. Find horizontal frequency:
553      *
554      *  [H FREQ] = 1000 / [H PERIOD]
555      *
556      *  I've ifdef'd this out, because we don't need it for any of
557      *  our calculations.
558      *  We calculate this in Hz rather than kHz, to avoid rounding
559      *  errors.  Recall that the [H PERIOD] is in usec.
560      */
561 
562 #ifdef	GTFDEBUG
563     h_freq = 1000000000 / h_period;
564 
565     print_value(22, "[H FREQ]", h_freq);
566 #endif
567 
568 
569 
570     /* Stage 1 computations are now complete; I should really pass
571        the results to another function and do the Stage 2
572        computations, but I only need a few more values so I'll just
573        append the computations here for now */
574 
575 
576 
577     /*  17. Find the number of pixels in the horizontal sync period:
578      *
579      *  [H SYNC (PIXELS)] =(ROUND(([H SYNC%] / 100 * [TOTAL PIXELS] /
580      *                             [CELL GRAN RND]),0))*[CELL GRAN RND]
581      *
582      *  Rewriting for integer math:
583      *
584      *  [H SYNC (PIXELS)]=(ROUND((H SYNC%] * [TOTAL PIXELS] / 100 /
585      *				   [CELL GRAN RND),0))*[CELL GRAN RND]
586      */
587 
588     h_sync = DIVIDE(((params->hsync_pct * total_pixels) / 100), CELL_GRAN) *
589 	CELL_GRAN;
590 
591     print_value(17, "[H SYNC (PIXELS)]", h_sync);
592 
593 
594     /*  18. Find the number of pixels in the horizontal front porch period:
595      *
596      *  [H FRONT PORCH (PIXELS)] = ([H BLANK (PIXELS)]/2)-[H SYNC (PIXELS)]
597      *
598      *  Note that h_blank is always an even number of characters (i.e.
599      *  h_blank % (CELL_GRAN * 2) == 0)
600      */
601 
602     h_front_porch = (h_blank / 2) - h_sync;
603 
604     print_value(18, "[H FRONT PORCH (PIXELS)]", h_front_porch);
605 
606 
607     /*  36. Find the number of lines in the odd front porch period:
608      *
609      *  [V ODD FRONT PORCH(LINES)]=([MIN PORCH RND]+[INTERLACE])
610      *
611      *  Adjusting for the fact that the interlace is scaled:
612      *
613      *  [V ODD FRONT PORCH(LINES)]=(([MIN PORCH RND] * 2) + [2*INTERLACE]) / 2
614      */
615 
616     v_odd_front_porch_lines = ((2 * params->min_porch) + interlace) / 2;
617 
618     print_value(36, "[V ODD FRONT PORCH(LINES)]", v_odd_front_porch_lines);
619 
620 
621     /* finally, pack the results in the mode struct */
622 
623     vmp->hsync_start = h_pixels + h_front_porch;
624     vmp->hsync_end = vmp->hsync_start + h_sync;
625     vmp->htotal = total_pixels;
626     vmp->hdisplay = h_pixels;
627 
628     vmp->vsync_start = v_lines + v_odd_front_porch_lines;
629     vmp->vsync_end = vmp->vsync_start + params->vsync_rqd;
630     vmp->vtotal = total_v_lines;
631     vmp->vdisplay = v_lines;
632 
633     vmp->dot_clock = pixel_freq;
634 
635 }
636 
637 void
638 vesagtf_mode(unsigned x, unsigned y, unsigned refresh, struct videomode *vmp)
639 {
640 	struct vesagtf_params	params;
641 
642 	params.margin_ppt = VESAGTF_MARGIN_PPT;
643 	params.min_porch = VESAGTF_MIN_PORCH;
644 	params.vsync_rqd = VESAGTF_VSYNC_RQD;
645 	params.hsync_pct = VESAGTF_HSYNC_PCT;
646 	params.min_vsbp = VESAGTF_MIN_VSBP;
647 	params.M = VESAGTF_M;
648 	params.C = VESAGTF_C;
649 	params.K = VESAGTF_K;
650 	params.J = VESAGTF_J;
651 
652 	vesagtf_mode_params(x, y, refresh, &params, 0, vmp);
653 }
654 
655 /*
656  * The tidbit here is so that you can compile this file as a
657  * standalone user program to generate X11 modelines using VESA GTF.
658  * This also allows for testing of the code itself, without
659  * necessitating a full kernel recompile.
660  */
661 
662 /* print_xf86_mode() - print the XFree86 modeline, given mode timings. */
663 
664 #ifndef _KERNEL
665 void
666 print_xf86_mode (struct videomode *vmp)
667 {
668 	float	vf, hf;
669 
670 	hf = 1000.0 * vmp->dot_clock / vmp->htotal;
671 	vf = 1.0 * hf / vmp->vtotal;
672 
673     printf("\n");
674     printf("  # %dx%d @ %.2f Hz (GTF) hsync: %.2f kHz; pclk: %.2f MHz\n",
675 	vmp->hdisplay, vmp->vdisplay, vf, hf, vmp->dot_clock / 1000.0);
676 
677     printf("  Modeline \"%dx%d_%.2f\"  %.2f"
678 	"  %d %d %d %d"
679 	"  %d %d %d %d"
680 	"  -HSync +Vsync\n\n",
681 	vmp->hdisplay, vmp->vdisplay, vf, (vmp->dot_clock / 1000.0),
682 	vmp->hdisplay, vmp->hsync_start, vmp->hsync_end, vmp->htotal,
683 	vmp->vdisplay, vmp->vsync_start, vmp->vsync_end, vmp->vtotal);
684 }
685 
686 int
687 main (int argc, char *argv[])
688 {
689 	struct videomode m;
690 
691 	if (argc != 4) {
692 		printf("usage: %s x y refresh\n", argv[0]);
693 		exit(1);
694 	}
695 
696 	vesagtf_mode(atoi(argv[1]), atoi(argv[2]), atoi(argv[3]), &m);
697 
698         print_xf86_mode(&m);
699 
700 	return 0;
701 
702 }
703 #endif
704