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