1 /* gtf.c Generate mode timings using the GTF Timing Standard
3 * gcc gtf.c -o gtf -lm -Wall
5 * Copyright (c) 2001, Andy Ritger aritger@nvidia.com
8 * Source http://osdn.dl.sourceforge.net/sourceforge/gtf/gtf.c
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
14 * o Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * o Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer
18 * in the documentation and/or other materials provided with the
20 * o Neither the name of NVIDIA nor the names of its contributors
21 * may be used to endorse or promote products derived from this
22 * software without specific prior written permission.
25 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
26 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT
27 * NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
28 * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
29 * THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
30 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
31 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
32 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
33 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
35 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36 * POSSIBILITY OF SUCH DAMAGE.
40 * This program is based on the Generalized Timing Formula(GTF TM)
41 * Standard Version: 1.0, Revision: 1.0
43 * The GTF Document contains the following Copyright information:
45 * Copyright (c) 1994, 1995, 1996 - Video Electronics Standards
46 * Association. Duplication of this document within VESA member
47 * companies for review purposes is permitted. All other rights
50 * While every precaution has been taken in the preparation
51 * of this standard, the Video Electronics Standards Association and
52 * its contributors assume no responsibility for errors or omissions,
53 * and make no warranties, expressed or implied, of functionality
54 * of suitability for any purpose. The sample code contained within
55 * this standard may be used without restriction.
59 * The GTF EXCEL(TM) SPREADSHEET, a sample (and the definitive)
60 * implementation of the GTF Timing Standard, is available at:
62 * ftp://ftp.vesa.org/pub/GTF/GTF_V1R1.xls
66 * This program takes a desired resolution and vertical refresh rate,
67 * and computes mode timings according to the GTF Timing Standard.
68 * These mode timings can then be formatted as an XFree86 modeline
69 * or a mode description for use by fbset(8).
75 * The GTF allows for computation of "margins" (the visible border
76 * surrounding the addressable video); on most non-overscan type
77 * systems, the margin period is zero. I've implemented the margin
78 * computations but not enabled it because 1) I don't really have
79 * any experience with this, and 2) neither XFree86 modelines nor
80 * fbset fb.modes provide an obvious way for margin timings to be
81 * included in their mode descriptions (needs more investigation).
83 * The GTF provides for computation of interlaced mode timings;
84 * I've implemented the computations but not enabled them, yet.
85 * I should probably enable and test this at some point.
91 * o Add support for interlaced modes.
93 * o Implement the other portions of the GTF: compute mode timings
94 * given either the desired pixel clock or the desired horizontal
97 * o It would be nice if this were more general purpose to do things
98 * outside the scope of the GTF: like generate double scan mode
99 * timings, for example.
101 * o Printing digits to the right of the decimal point when the
102 * digits are 0 annoys me.
116 #define MARGIN_PERCENT 1.8 /* % of active vertical image */
117 #define CELL_GRAN 8.0 /* assumed character cell granularity */
118 #define MIN_PORCH 1 /* minimum front porch */
119 #define V_SYNC_RQD 3 /* width of vsync in lines */
120 #define H_SYNC_PERCENT 8.0 /* width of hsync as % of total line */
121 #define MIN_VSYNC_PLUS_BP 550.0 /* min time of vsync + back porch (microsec) */
122 #define M 600.0 /* blanking formula gradient */
123 #define C 40.0 /* blanking formula offset */
124 #define K 128.0 /* blanking formula scaling factor */
125 #define J 20.0 /* blanking formula scaling factor */
127 /* C' and M' are part of the Blanking Duty Cycle computation */
129 #define C_PRIME (((C - J) * K/256.0) + J)
130 #define M_PRIME (K/256.0 * M)
133 /* struct definitions */
135 typedef struct __mode
137 int hr, hss, hse, hfl;
138 int vr, vss, vse, vfl;
139 float pclk, h_freq, v_freq;
143 typedef struct __options
146 int xf86mode, fbmode;
155 void print_value(int n, char *name, float val);
156 void print_xf86_mode (mode *m);
157 void print_fb_mode (mode *m);
158 mode *vert_refresh (int h_pixels, int v_lines, float freq,
159 int interlaced, int margins);
160 options *parse_command_line (int argc, char *argv[]);
166 * print_value() - print the result of the named computation; this is
167 * useful when comparing against the GTF EXCEL spreadsheet.
170 int global_verbose = 0;
172 void print_value(int n, char *name, float val)
174 if (global_verbose) {
175 printf("%2d: %-27s: %15f\n", n, name, val);
181 /* print_xf86_mode() - print the XFree86 modeline, given mode timings. */
183 void print_xf86_mode (mode *m)
186 printf (" # %dx%d @ %.2f Hz (GTF) hsync: %.2f kHz; pclk: %.2f MHz\n",
187 m->hr, m->vr, m->v_freq, m->h_freq, m->pclk);
189 printf (" Modeline \"%dx%d_%.2f\" %.2f"
192 " -HSync +Vsync\n\n",
193 m->hr, m->vr, m->v_freq, m->pclk,
194 m->hr, m->hss, m->hse, m->hfl,
195 m->vr, m->vss, m->vse, m->vfl);
197 } // print_xf86_mode()
202 * print_fb_mode() - print a mode description in fbset(8) format;
203 * see the fb.modes(8) manpage. The timing description used in
204 * this is rather odd; they use "left and right margin" to refer
205 * to the portion of the hblank before and after the sync pulse
206 * by conceptually wrapping the portion of the blank after the pulse
207 * to infront of the visible region; ie:
210 * Timing description I'm accustomed to:
214 * <--------1--------> <--2--> <--3--> <--4-->
216 * |-------------------|_______| |_______
221 * 2: blank before sync (aka front porch)
223 * 4: blank after sync (aka back porch)
230 * But the fb.modes format is:
233 * <--4--> <--------1--------> <--2--> <--3-->
235 * _______|-------------------|_______| |
237 * The fb.modes(8) manpage refers to <4> and <2> as the left and
238 * right "margin" (as well as upper and lower margin in the vertical
239 * direction) -- note that this has nothing to do with the term
240 * "margin" used in the GTF Timing Standard.
242 * XXX always prints the 32 bit mode -- should I provide a command
243 * line option to specify the bpp? It's simple enough for a user
244 * to edit the mode description after it's generated.
247 void print_fb_mode (mode *m)
250 printf ("mode \"%dx%d %.2fHz 32bit (GTF)\"\n",
251 m->hr, m->vr, m->v_freq);
252 printf (" # PCLK: %.2f MHz, H: %.2f kHz, V: %.2f Hz\n",
253 m->pclk, m->h_freq, m->v_freq);
254 printf (" geometry %d %d %d %d 32\n",
255 m->hr, m->vr, m->hr, m->vr);
256 printf (" timings %d %d %d %d %d %d %d\n",
257 (int) rint(1000000.0/m->pclk),// pixclock in picoseconds
258 m->hfl - m->hse, // left margin (in pixels)
259 m->hss - m->hr, // right margin (in pixels)
260 m->vfl - m->vse, // upper margin (in pixel lines)
261 m->vss - m->vr, // lower margin (in pixel lines)
262 m->hse - m->hss, // horizontal sync length (in pixels)
263 m->vse - m->vss); // vert sync length (in pixel lines)
264 printf (" hsync low\n");
265 printf (" vsync high\n");
266 printf ("endmode\n\n");
274 * vert_refresh() - as defined by the GTF Timing Standard, compute the
275 * Stage 1 Parameters using the vertical refresh frequency. In other
276 * words: input a desired resolution and desired refresh rate, and
277 * output the GTF mode timings.
279 * XXX All the code is in place to compute interlaced modes, but I don't
280 * feel like testing it right now.
282 * XXX margin computations are implemented but not tested (nor used by
283 * XFree86 of fbset mode descriptions, from what I can tell).
286 mode *vert_refresh (int h_pixels, int v_lines, float freq,
287 int interlaced, int margins)
291 float v_field_rate_rqd;
299 float v_field_rate_est;
305 float total_active_pixels;
306 float ideal_duty_cycle;
314 float v_odd_front_porch_lines;
316 mode *m = (mode*) malloc (sizeof (mode));
319 /* 1. In order to give correct results, the number of horizontal
320 * pixels requested is first processed to ensure that it is divisible
321 * by the character size, by rounding it to the nearest character
324 * [H PIXELS RND] = ((ROUND([H PIXELS]/[CELL GRAN RND],0))*[CELLGRAN RND])
327 h_pixels_rnd = rint((float) h_pixels / CELL_GRAN) * CELL_GRAN;
329 print_value(1, "[H PIXELS RND]", h_pixels_rnd);
332 /* 2. If interlace is requested, the number of vertical lines assumed
333 * by the calculation must be halved, as the computation calculates
334 * the number of vertical lines per field. In either case, the
335 * number of lines is rounded to the nearest integer.
337 * [V LINES RND] = IF([INT RQD?]="y", ROUND([V LINES]/2,0),
338 * ROUND([V LINES],0))
341 v_lines_rnd = interlaced ?
342 rint((float) v_lines) / 2.0 :
343 rint((float) v_lines);
345 print_value(2, "[V LINES RND]", v_lines_rnd);
348 /* 3. Find the frame rate required:
350 * [V FIELD RATE RQD] = IF([INT RQD?]="y", [I/P FREQ RQD]*2,
354 v_field_rate_rqd = interlaced ? (freq * 2.0) : (freq);
356 print_value(3, "[V FIELD RATE RQD]", v_field_rate_rqd);
359 /* 4. Find number of lines in Top margin:
361 * [TOP MARGIN (LINES)] = IF([MARGINS RQD?]="Y",
362 * ROUND(([MARGIN%]/100*[V LINES RND]),0),
366 top_margin = margins ? rint(MARGIN_PERCENT / 100.0 * v_lines_rnd) : (0.0);
368 print_value(4, "[TOP MARGIN (LINES)]", top_margin);
371 /* 5. Find number of lines in Bottom margin:
373 * [BOT MARGIN (LINES)] = IF([MARGINS RQD?]="Y",
374 * ROUND(([MARGIN%]/100*[V LINES RND]),0),
378 bottom_margin = margins ? rint(MARGIN_PERCENT/100.0 * v_lines_rnd) : (0.0);
380 print_value(5, "[BOT MARGIN (LINES)]", bottom_margin);
383 /* 6. If interlace is required, then set variable [INTERLACE]=0.5:
385 * [INTERLACE]=(IF([INT RQD?]="y",0.5,0))
388 interlace = interlaced ? 0.5 : 0.0;
390 print_value(6, "[INTERLACE]", interlace);
393 /* 7. Estimate the Horizontal period
395 * [H PERIOD EST] = ((1/[V FIELD RATE RQD]) - [MIN VSYNC+BP]/1000000) /
396 * ([V LINES RND] + (2*[TOP MARGIN (LINES)]) +
397 * [MIN PORCH RND]+[INTERLACE]) * 1000000
400 h_period_est = (((1.0/v_field_rate_rqd) - (MIN_VSYNC_PLUS_BP/1000000.0))
401 / (v_lines_rnd + (2*top_margin) + MIN_PORCH + interlace)
404 print_value(7, "[H PERIOD EST]", h_period_est);
407 /* 8. Find the number of lines in V sync + back porch:
409 * [V SYNC+BP] = ROUND(([MIN VSYNC+BP]/[H PERIOD EST]),0)
412 vsync_plus_bp = rint(MIN_VSYNC_PLUS_BP/h_period_est);
414 print_value(8, "[V SYNC+BP]", vsync_plus_bp);
417 /* 9. Find the number of lines in V back porch alone:
419 * [V BACK PORCH] = [V SYNC+BP] - [V SYNC RND]
421 * XXX is "[V SYNC RND]" a typo? should be [V SYNC RQD]?
424 v_back_porch = vsync_plus_bp - V_SYNC_RQD;
426 print_value(9, "[V BACK PORCH]", v_back_porch);
429 /* 10. Find the total number of lines in Vertical field period:
431 * [TOTAL V LINES] = [V LINES RND] + [TOP MARGIN (LINES)] +
432 * [BOT MARGIN (LINES)] + [V SYNC+BP] + [INTERLACE] +
436 total_v_lines = v_lines_rnd + top_margin + bottom_margin + vsync_plus_bp +
437 interlace + MIN_PORCH;
439 print_value(10, "[TOTAL V LINES]", total_v_lines);
442 /* 11. Estimate the Vertical field frequency:
444 * [V FIELD RATE EST] = 1 / [H PERIOD EST] / [TOTAL V LINES] * 1000000
447 v_field_rate_est = 1.0 / h_period_est / total_v_lines * 1000000.0;
449 print_value(11, "[V FIELD RATE EST]", v_field_rate_est);
452 /* 12. Find the actual horizontal period:
454 * [H PERIOD] = [H PERIOD EST] / ([V FIELD RATE RQD] / [V FIELD RATE EST])
457 h_period = h_period_est / (v_field_rate_rqd / v_field_rate_est);
459 print_value(12, "[H PERIOD]", h_period);
462 /* 13. Find the actual Vertical field frequency:
464 * [V FIELD RATE] = 1 / [H PERIOD] / [TOTAL V LINES] * 1000000
467 v_field_rate = 1.0 / h_period / total_v_lines * 1000000.0;
469 print_value(13, "[V FIELD RATE]", v_field_rate);
472 /* 14. Find the Vertical frame frequency:
474 * [V FRAME RATE] = (IF([INT RQD?]="y", [V FIELD RATE]/2, [V FIELD RATE]))
477 v_frame_rate = interlaced ? v_field_rate / 2.0 : v_field_rate;
479 print_value(14, "[V FRAME RATE]", v_frame_rate);
482 /* 15. Find number of pixels in left margin:
484 * [LEFT MARGIN (PIXELS)] = (IF( [MARGINS RQD?]="Y",
485 * (ROUND( ([H PIXELS RND] * [MARGIN%] / 100 /
486 * [CELL GRAN RND]),0)) * [CELL GRAN RND],
490 left_margin = margins ?
491 rint(h_pixels_rnd * MARGIN_PERCENT / 100.0 / CELL_GRAN) * CELL_GRAN :
494 print_value(15, "[LEFT MARGIN (PIXELS)]", left_margin);
497 /* 16. Find number of pixels in right margin:
499 * [RIGHT MARGIN (PIXELS)] = (IF( [MARGINS RQD?]="Y",
500 * (ROUND( ([H PIXELS RND] * [MARGIN%] / 100 /
501 * [CELL GRAN RND]),0)) * [CELL GRAN RND],
505 right_margin = margins ?
506 rint(h_pixels_rnd * MARGIN_PERCENT / 100.0 / CELL_GRAN) * CELL_GRAN :
509 print_value(16, "[RIGHT MARGIN (PIXELS)]", right_margin);
512 /* 17. Find total number of active pixels in image and left and right
515 * [TOTAL ACTIVE PIXELS] = [H PIXELS RND] + [LEFT MARGIN (PIXELS)] +
516 * [RIGHT MARGIN (PIXELS)]
519 total_active_pixels = h_pixels_rnd + left_margin + right_margin;
521 print_value(17, "[TOTAL ACTIVE PIXELS]", total_active_pixels);
524 /* 18. Find the ideal blanking duty cycle from the blanking duty cycle
527 * [IDEAL DUTY CYCLE] = [C'] - ([M']*[H PERIOD]/1000)
530 ideal_duty_cycle = C_PRIME - (M_PRIME * h_period / 1000.0);
532 print_value(18, "[IDEAL DUTY CYCLE]", ideal_duty_cycle);
535 /* 19. Find the number of pixels in the blanking time to the nearest
536 * double character cell:
538 * [H BLANK (PIXELS)] = (ROUND(([TOTAL ACTIVE PIXELS] *
539 * [IDEAL DUTY CYCLE] /
540 * (100-[IDEAL DUTY CYCLE]) /
541 * (2*[CELL GRAN RND])), 0))
542 * * (2*[CELL GRAN RND])
545 h_blank = rint(total_active_pixels *
547 (100.0 - ideal_duty_cycle) /
548 (2.0 * CELL_GRAN)) * (2.0 * CELL_GRAN);
550 print_value(19, "[H BLANK (PIXELS)]", h_blank);
553 /* 20. Find total number of pixels:
555 * [TOTAL PIXELS] = [TOTAL ACTIVE PIXELS] + [H BLANK (PIXELS)]
558 total_pixels = total_active_pixels + h_blank;
560 print_value(20, "[TOTAL PIXELS]", total_pixels);
563 /* 21. Find pixel clock frequency:
565 * [PIXEL FREQ] = [TOTAL PIXELS] / [H PERIOD]
568 pixel_freq = total_pixels / h_period;
570 print_value(21, "[PIXEL FREQ]", pixel_freq);
573 /* 22. Find horizontal frequency:
575 * [H FREQ] = 1000 / [H PERIOD]
578 h_freq = 1000.0 / h_period;
580 print_value(22, "[H FREQ]", h_freq);
584 /* Stage 1 computations are now complete; I should really pass
585 the results to another function and do the Stage 2
586 computations, but I only need a few more values so I'll just
587 append the computations here for now */
591 /* 17. Find the number of pixels in the horizontal sync period:
593 * [H SYNC (PIXELS)] =(ROUND(([H SYNC%] / 100 * [TOTAL PIXELS] /
594 * [CELL GRAN RND]),0))*[CELL GRAN RND]
597 h_sync = rint(H_SYNC_PERCENT/100.0 * total_pixels / CELL_GRAN) * CELL_GRAN;
599 print_value(17, "[H SYNC (PIXELS)]", h_sync);
602 /* 18. Find the number of pixels in the horizontal front porch period:
604 * [H FRONT PORCH (PIXELS)] = ([H BLANK (PIXELS)]/2)-[H SYNC (PIXELS)]
607 h_front_porch = (h_blank / 2.0) - h_sync;
609 print_value(18, "[H FRONT PORCH (PIXELS)]", h_front_porch);
612 /* 36. Find the number of lines in the odd front porch period:
614 * [V ODD FRONT PORCH(LINES)]=([MIN PORCH RND]+[INTERLACE])
617 v_odd_front_porch_lines = MIN_PORCH + interlace;
619 print_value(36, "[V ODD FRONT PORCH(LINES)]", v_odd_front_porch_lines);
622 /* finally, pack the results in the mode struct */
624 m->hr = (int) (h_pixels_rnd);
625 m->hss = (int) (h_pixels_rnd + h_front_porch);
626 m->hse = (int) (h_pixels_rnd + h_front_porch + h_sync);
627 m->hfl = (int) (total_pixels);
629 m->vr = (int) (v_lines_rnd);
630 m->vss = (int) (v_lines_rnd + v_odd_front_porch_lines);
631 m->vse = (int) (int) (v_lines_rnd + v_odd_front_porch_lines + V_SYNC_RQD);
632 m->vfl = (int) (total_v_lines);
634 m->pclk = pixel_freq;
646 * parse_command_line() - parse the command line and return an
647 * alloced structure containing the results. On error print usage
651 options *parse_command_line (int argc, char *argv[])
655 options *o = (options *) calloc (1, sizeof (options));
657 if (argc < 4) goto bad_option;
659 o->x = atoi (argv[1]);
660 o->y = atoi (argv[2]);
661 o->v_freq = atof (argv[3]);
663 /* XXX should check for errors in the above */
668 if ((strcmp (argv[n], "-v") == 0) ||
669 (strcmp (argv[n], "--verbose") == 0)) {
671 } else if ((strcmp (argv[n], "-f") == 0) ||
672 (strcmp (argv[n], "--fbmode") == 0)) {
674 } else if ((strcmp (argv[n], "-x") == 0) ||
675 (strcmp (argv[n], "--xf86mode") == 0)) {
684 /* if neither xf86mode nor fbmode were requested, default to
687 if (!o->fbmode && !o->xf86mode) o->xf86mode = 1;
693 fprintf (stderr, "\n");
694 fprintf (stderr, "usage: %s x y refresh [-v|--verbose] "
695 "[-f|--fbmode] [-x|-xf86mode]\n", argv[0]);
697 fprintf (stderr, "\n");
699 fprintf (stderr, " x : the desired horizontal "
700 "resolution (required)\n");
701 fprintf (stderr, " y : the desired vertical "
702 "resolution (required)\n");
703 fprintf (stderr, " refresh : the desired refresh "
704 "rate (required)\n");
705 fprintf (stderr, " -v|--verbose : enable verbose printouts "
706 "(traces each step of the computation)\n");
707 fprintf (stderr, " -f|--fbmode : output an fbset(8)-style mode "
709 fprintf (stderr, " -x|-xf86mode : output an XFree86-style mode "
710 "description (this is the default\n"
711 " if no mode description is requested)\n");
713 fprintf (stderr, "\n");
718 } // parse_command_line()
722 int main (int argc, char *argv[])
727 o = parse_command_line (argc, argv);
730 m = vert_refresh (o->x, o->y, o->v_freq, 0, 0);