15503bb3534d59116f4467005010e48ee801480d
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 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
12 * o Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * o Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer
16 * in the documentation and/or other materials provided with the
18 * o Neither the name of NVIDIA nor the names of its contributors
19 * may be used to endorse or promote products derived from this
20 * software without specific prior written permission.
23 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
24 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT
25 * NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
26 * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
27 * THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
28 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
29 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
30 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
31 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
33 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
34 * POSSIBILITY OF SUCH DAMAGE.
38 * This program is based on the Generalized Timing Formula(GTF TM)
39 * Standard Version: 1.0, Revision: 1.0
41 * The GTF Document contains the following Copyright information:
43 * Copyright (c) 1994, 1995, 1996 - Video Electronics Standards
44 * Association. Duplication of this document within VESA member
45 * companies for review purposes is permitted. All other rights
48 * While every precaution has been taken in the preparation
49 * of this standard, the Video Electronics Standards Association and
50 * its contributors assume no responsibility for errors or omissions,
51 * and make no warranties, expressed or implied, of functionality
52 * of suitability for any purpose. The sample code contained within
53 * this standard may be used without restriction.
57 * The GTF EXCEL(TM) SPREADSHEET, a sample (and the definitive)
58 * implementation of the GTF Timing Standard, is available at:
60 * ftp://ftp.vesa.org/pub/GTF/GTF_V1R1.xls
64 * This program takes a desired resolution and vertical refresh rate,
65 * and computes mode timings according to the GTF Timing Standard.
66 * These mode timings can then be formatted as an XServer modeline
67 * or a mode description for use by fbset(8).
73 * The GTF allows for computation of "margins" (the visible border
74 * surrounding the addressable video); on most non-overscan type
75 * systems, the margin period is zero. I've implemented the margin
76 * computations but not enabled it because 1) I don't really have
77 * any experience with this, and 2) neither XServer modelines nor
78 * fbset fb.modes provide an obvious way for margin timings to be
79 * included in their mode descriptions (needs more investigation).
81 * The GTF provides for computation of interlaced mode timings;
82 * I've implemented the computations but not enabled them, yet.
83 * I should probably enable and test this at some point.
89 * o Add support for interlaced modes.
91 * o Implement the other portions of the GTF: compute mode timings
92 * given either the desired pixel clock or the desired horizontal
95 * o It would be nice if this were more general purpose to do things
96 * outside the scope of the GTF: like generate double scan mode
97 * timings, for example.
99 * o Printing digits to the right of the decimal point when the
100 * digits are 0 annoys me.
106 #ifdef HAVE_XORG_CONFIG_H
107 #include <xorg-config.h>
115 #define MARGIN_PERCENT 1.8 /* % of active vertical image */
116 #define CELL_GRAN 8.0 /* assumed character cell granularity */
117 #define MIN_PORCH 1 /* minimum front porch */
118 #define V_SYNC_RQD 3 /* width of vsync in lines */
119 #define H_SYNC_PERCENT 8.0 /* width of hsync as % of total line */
120 #define MIN_VSYNC_PLUS_BP 550.0 /* min time of vsync + back porch (microsec) */
121 #define M 600.0 /* blanking formula gradient */
122 #define C 40.0 /* blanking formula offset */
123 #define K 128.0 /* blanking formula scaling factor */
124 #define J 20.0 /* blanking formula scaling factor */
126 /* C' and M' are part of the Blanking Duty Cycle computation */
128 #define C_PRIME (((C - J) * K/256.0) + J)
129 #define M_PRIME (K/256.0 * M)
131 /* struct definitions */
133 typedef struct __mode
{
134 int hr
, hss
, hse
, hfl
;
135 int vr
, vss
, vse
, vfl
;
136 float pclk
, h_freq
, v_freq
;
139 typedef struct __options
{
141 int xorgmode
, fbmode
;
147 void print_value(int n
, const char *name
, float val
);
148 void print_xf86_mode(mode
* m
);
149 void print_fb_mode(mode
* m
);
150 mode
*vert_refresh(int h_pixels
, int v_lines
, float freq
,
151 int interlaced
, int margins
);
152 options
*parse_command_line(int argc
, char *argv
[]);
155 * print_value() - print the result of the named computation; this is
156 * useful when comparing against the GTF EXCEL spreadsheet.
159 int global_verbose
= 0;
162 print_value(int n
, const char *name
, float val
)
164 if (global_verbose
) {
165 printf("%2d: %-27s: %15f\n", n
, name
, val
);
169 /* print_xf86_mode() - print the XServer modeline, given mode timings. */
172 print_xf86_mode(mode
* m
)
175 printf(" # %dx%d @ %.2f Hz (GTF) hsync: %.2f kHz; pclk: %.2f MHz\n",
176 m
->hr
, m
->vr
, m
->v_freq
, m
->h_freq
, m
->pclk
);
178 printf(" Modeline \"%dx%d_%.2f\" %.2f"
181 " -HSync +Vsync\n\n",
182 m
->hr
, m
->vr
, m
->v_freq
, m
->pclk
,
183 m
->hr
, m
->hss
, m
->hse
, m
->hfl
, m
->vr
, m
->vss
, m
->vse
, m
->vfl
);
188 * print_fb_mode() - print a mode description in fbset(8) format;
189 * see the fb.modes(8) manpage. The timing description used in
190 * this is rather odd; they use "left and right margin" to refer
191 * to the portion of the hblank before and after the sync pulse
192 * by conceptually wrapping the portion of the blank after the pulse
193 * to infront of the visible region; ie:
196 * Timing description I'm accustomed to:
200 * <--------1--------> <--2--> <--3--> <--4-->
202 * |-------------------|_______| |_______
207 * 2: blank before sync (aka front porch)
209 * 4: blank after sync (aka back porch)
216 * But the fb.modes format is:
219 * <--4--> <--------1--------> <--2--> <--3-->
221 * _______|-------------------|_______| |
223 * The fb.modes(8) manpage refers to <4> and <2> as the left and
224 * right "margin" (as well as upper and lower margin in the vertical
225 * direction) -- note that this has nothing to do with the term
226 * "margin" used in the GTF Timing Standard.
228 * XXX always prints the 32 bit mode -- should I provide a command
229 * line option to specify the bpp? It's simple enough for a user
230 * to edit the mode description after it's generated.
234 print_fb_mode(mode
* m
)
237 printf("mode \"%dx%d %.2fHz 32bit (GTF)\"\n", m
->hr
, m
->vr
, m
->v_freq
);
238 printf(" # PCLK: %.2f MHz, H: %.2f kHz, V: %.2f Hz\n",
239 m
->pclk
, m
->h_freq
, m
->v_freq
);
240 printf(" geometry %d %d %d %d 32\n", m
->hr
, m
->vr
, m
->hr
, m
->vr
);
241 printf(" timings %d %d %d %d %d %d %d\n", (int) rint(1000000.0 / m
->pclk
), /* pixclock in picoseconds */
242 m
->hfl
- m
->hse
, /* left margin (in pixels) */
243 m
->hss
- m
->hr
, /* right margin (in pixels) */
244 m
->vfl
- m
->vse
, /* upper margin (in pixel lines) */
245 m
->vss
- m
->vr
, /* lower margin (in pixel lines) */
246 m
->hse
- m
->hss
, /* horizontal sync length (pixels) */
247 m
->vse
- m
->vss
); /* vert sync length (pixel lines) */
248 printf(" hsync low\n");
249 printf(" vsync high\n");
250 printf("endmode\n\n");
255 * vert_refresh() - as defined by the GTF Timing Standard, compute the
256 * Stage 1 Parameters using the vertical refresh frequency. In other
257 * words: input a desired resolution and desired refresh rate, and
258 * output the GTF mode timings.
260 * XXX All the code is in place to compute interlaced modes, but I don't
261 * feel like testing it right now.
263 * XXX margin computations are implemented but not tested (nor used by
264 * XServer of fbset mode descriptions, from what I can tell).
268 vert_refresh(int h_pixels
, int v_lines
, float freq
, int interlaced
, int margins
)
272 float v_field_rate_rqd
;
280 float v_field_rate_est
;
286 float total_active_pixels
;
287 float ideal_duty_cycle
;
295 float v_odd_front_porch_lines
;
297 mode
*m
= (mode
*) malloc(sizeof(mode
));
299 /* 1. In order to give correct results, the number of horizontal
300 * pixels requested is first processed to ensure that it is divisible
301 * by the character size, by rounding it to the nearest character
304 * [H PIXELS RND] = ((ROUND([H PIXELS]/[CELL GRAN RND],0))*[CELLGRAN RND])
307 h_pixels_rnd
= rint((float) h_pixels
/ CELL_GRAN
) * CELL_GRAN
;
309 print_value(1, "[H PIXELS RND]", h_pixels_rnd
);
311 /* 2. If interlace is requested, the number of vertical lines assumed
312 * by the calculation must be halved, as the computation calculates
313 * the number of vertical lines per field. In either case, the
314 * number of lines is rounded to the nearest integer.
316 * [V LINES RND] = IF([INT RQD?]="y", ROUND([V LINES]/2,0),
317 * ROUND([V LINES],0))
320 v_lines_rnd
= interlaced
?
321 rint((float) v_lines
) / 2.0 : rint((float) v_lines
);
323 print_value(2, "[V LINES RND]", v_lines_rnd
);
325 /* 3. Find the frame rate required:
327 * [V FIELD RATE RQD] = IF([INT RQD?]="y", [I/P FREQ RQD]*2,
331 v_field_rate_rqd
= interlaced
? (freq
* 2.0) : (freq
);
333 print_value(3, "[V FIELD RATE RQD]", v_field_rate_rqd
);
335 /* 4. Find number of lines in Top margin:
337 * [TOP MARGIN (LINES)] = IF([MARGINS RQD?]="Y",
338 * ROUND(([MARGIN%]/100*[V LINES RND]),0),
342 top_margin
= margins
? rint(MARGIN_PERCENT
/ 100.0 * v_lines_rnd
) : (0.0);
344 print_value(4, "[TOP MARGIN (LINES)]", top_margin
);
346 /* 5. Find number of lines in Bottom margin:
348 * [BOT MARGIN (LINES)] = IF([MARGINS RQD?]="Y",
349 * ROUND(([MARGIN%]/100*[V LINES RND]),0),
354 margins
? rint(MARGIN_PERCENT
/ 100.0 * v_lines_rnd
) : (0.0);
356 print_value(5, "[BOT MARGIN (LINES)]", bottom_margin
);
358 /* 6. If interlace is required, then set variable [INTERLACE]=0.5:
360 * [INTERLACE]=(IF([INT RQD?]="y",0.5,0))
363 interlace
= interlaced
? 0.5 : 0.0;
365 print_value(6, "[INTERLACE]", interlace
);
367 /* 7. Estimate the Horizontal period
369 * [H PERIOD EST] = ((1/[V FIELD RATE RQD]) - [MIN VSYNC+BP]/1000000) /
370 * ([V LINES RND] + (2*[TOP MARGIN (LINES)]) +
371 * [MIN PORCH RND]+[INTERLACE]) * 1000000
374 h_period_est
= (((1.0 / v_field_rate_rqd
) - (MIN_VSYNC_PLUS_BP
/ 1000000.0))
375 / (v_lines_rnd
+ (2 * top_margin
) + MIN_PORCH
+ interlace
)
378 print_value(7, "[H PERIOD EST]", h_period_est
);
380 /* 8. Find the number of lines in V sync + back porch:
382 * [V SYNC+BP] = ROUND(([MIN VSYNC+BP]/[H PERIOD EST]),0)
385 vsync_plus_bp
= rint(MIN_VSYNC_PLUS_BP
/ h_period_est
);
387 print_value(8, "[V SYNC+BP]", vsync_plus_bp
);
389 /* 9. Find the number of lines in V back porch alone:
391 * [V BACK PORCH] = [V SYNC+BP] - [V SYNC RND]
393 * XXX is "[V SYNC RND]" a typo? should be [V SYNC RQD]?
396 v_back_porch
= vsync_plus_bp
- V_SYNC_RQD
;
398 print_value(9, "[V BACK PORCH]", v_back_porch
);
400 /* 10. Find the total number of lines in Vertical field period:
402 * [TOTAL V LINES] = [V LINES RND] + [TOP MARGIN (LINES)] +
403 * [BOT MARGIN (LINES)] + [V SYNC+BP] + [INTERLACE] +
407 total_v_lines
= v_lines_rnd
+ top_margin
+ bottom_margin
+ vsync_plus_bp
+
408 interlace
+ MIN_PORCH
;
410 print_value(10, "[TOTAL V LINES]", total_v_lines
);
412 /* 11. Estimate the Vertical field frequency:
414 * [V FIELD RATE EST] = 1 / [H PERIOD EST] / [TOTAL V LINES] * 1000000
417 v_field_rate_est
= 1.0 / h_period_est
/ total_v_lines
* 1000000.0;
419 print_value(11, "[V FIELD RATE EST]", v_field_rate_est
);
421 /* 12. Find the actual horizontal period:
423 * [H PERIOD] = [H PERIOD EST] / ([V FIELD RATE RQD] / [V FIELD RATE EST])
426 h_period
= h_period_est
/ (v_field_rate_rqd
/ v_field_rate_est
);
428 print_value(12, "[H PERIOD]", h_period
);
430 /* 13. Find the actual Vertical field frequency:
432 * [V FIELD RATE] = 1 / [H PERIOD] / [TOTAL V LINES] * 1000000
435 v_field_rate
= 1.0 / h_period
/ total_v_lines
* 1000000.0;
437 print_value(13, "[V FIELD RATE]", v_field_rate
);
439 /* 14. Find the Vertical frame frequency:
441 * [V FRAME RATE] = (IF([INT RQD?]="y", [V FIELD RATE]/2, [V FIELD RATE]))
444 v_frame_rate
= interlaced
? v_field_rate
/ 2.0 : v_field_rate
;
446 print_value(14, "[V FRAME RATE]", v_frame_rate
);
448 /* 15. Find number of pixels in left margin:
450 * [LEFT MARGIN (PIXELS)] = (IF( [MARGINS RQD?]="Y",
451 * (ROUND( ([H PIXELS RND] * [MARGIN%] / 100 /
452 * [CELL GRAN RND]),0)) * [CELL GRAN RND],
456 left_margin
= margins
?
457 rint(h_pixels_rnd
* MARGIN_PERCENT
/ 100.0 / CELL_GRAN
) * CELL_GRAN
:
460 print_value(15, "[LEFT MARGIN (PIXELS)]", left_margin
);
462 /* 16. Find number of pixels in right margin:
464 * [RIGHT MARGIN (PIXELS)] = (IF( [MARGINS RQD?]="Y",
465 * (ROUND( ([H PIXELS RND] * [MARGIN%] / 100 /
466 * [CELL GRAN RND]),0)) * [CELL GRAN RND],
470 right_margin
= margins
?
471 rint(h_pixels_rnd
* MARGIN_PERCENT
/ 100.0 / CELL_GRAN
) * CELL_GRAN
:
474 print_value(16, "[RIGHT MARGIN (PIXELS)]", right_margin
);
476 /* 17. Find total number of active pixels in image and left and right
479 * [TOTAL ACTIVE PIXELS] = [H PIXELS RND] + [LEFT MARGIN (PIXELS)] +
480 * [RIGHT MARGIN (PIXELS)]
483 total_active_pixels
= h_pixels_rnd
+ left_margin
+ right_margin
;
485 print_value(17, "[TOTAL ACTIVE PIXELS]", total_active_pixels
);
487 /* 18. Find the ideal blanking duty cycle from the blanking duty cycle
490 * [IDEAL DUTY CYCLE] = [C'] - ([M']*[H PERIOD]/1000)
493 ideal_duty_cycle
= C_PRIME
- (M_PRIME
* h_period
/ 1000.0);
495 print_value(18, "[IDEAL DUTY CYCLE]", ideal_duty_cycle
);
497 /* 19. Find the number of pixels in the blanking time to the nearest
498 * double character cell:
500 * [H BLANK (PIXELS)] = (ROUND(([TOTAL ACTIVE PIXELS] *
501 * [IDEAL DUTY CYCLE] /
502 * (100-[IDEAL DUTY CYCLE]) /
503 * (2*[CELL GRAN RND])), 0))
504 * * (2*[CELL GRAN RND])
507 h_blank
= rint(total_active_pixels
*
509 (100.0 - ideal_duty_cycle
) /
510 (2.0 * CELL_GRAN
)) * (2.0 * CELL_GRAN
);
512 print_value(19, "[H BLANK (PIXELS)]", h_blank
);
514 /* 20. Find total number of pixels:
516 * [TOTAL PIXELS] = [TOTAL ACTIVE PIXELS] + [H BLANK (PIXELS)]
519 total_pixels
= total_active_pixels
+ h_blank
;
521 print_value(20, "[TOTAL PIXELS]", total_pixels
);
523 /* 21. Find pixel clock frequency:
525 * [PIXEL FREQ] = [TOTAL PIXELS] / [H PERIOD]
528 pixel_freq
= total_pixels
/ h_period
;
530 print_value(21, "[PIXEL FREQ]", pixel_freq
);
532 /* 22. Find horizontal frequency:
534 * [H FREQ] = 1000 / [H PERIOD]
537 h_freq
= 1000.0 / h_period
;
539 print_value(22, "[H FREQ]", h_freq
);
541 /* Stage 1 computations are now complete; I should really pass
542 the results to another function and do the Stage 2
543 computations, but I only need a few more values so I'll just
544 append the computations here for now */
546 /* 17. Find the number of pixels in the horizontal sync period:
548 * [H SYNC (PIXELS)] =(ROUND(([H SYNC%] / 100 * [TOTAL PIXELS] /
549 * [CELL GRAN RND]),0))*[CELL GRAN RND]
553 rint(H_SYNC_PERCENT
/ 100.0 * total_pixels
/ CELL_GRAN
) * CELL_GRAN
;
555 print_value(17, "[H SYNC (PIXELS)]", h_sync
);
557 /* 18. Find the number of pixels in the horizontal front porch period:
559 * [H FRONT PORCH (PIXELS)] = ([H BLANK (PIXELS)]/2)-[H SYNC (PIXELS)]
562 h_front_porch
= (h_blank
/ 2.0) - h_sync
;
564 print_value(18, "[H FRONT PORCH (PIXELS)]", h_front_porch
);
566 /* 36. Find the number of lines in the odd front porch period:
568 * [V ODD FRONT PORCH(LINES)]=([MIN PORCH RND]+[INTERLACE])
571 v_odd_front_porch_lines
= MIN_PORCH
+ interlace
;
573 print_value(36, "[V ODD FRONT PORCH(LINES)]", v_odd_front_porch_lines
);
575 /* finally, pack the results in the mode struct */
577 m
->hr
= (int) (h_pixels_rnd
);
578 m
->hss
= (int) (h_pixels_rnd
+ h_front_porch
);
579 m
->hse
= (int) (h_pixels_rnd
+ h_front_porch
+ h_sync
);
580 m
->hfl
= (int) (total_pixels
);
582 m
->vr
= (int) (v_lines_rnd
);
583 m
->vss
= (int) (v_lines_rnd
+ v_odd_front_porch_lines
);
584 m
->vse
= (int) (int) (v_lines_rnd
+ v_odd_front_porch_lines
+ V_SYNC_RQD
);
585 m
->vfl
= (int) (total_v_lines
);
587 m
->pclk
= pixel_freq
;
596 * parse_command_line() - parse the command line and return an
597 * alloced structure containing the results. On error print usage
602 parse_command_line(int argc
, char *argv
[])
606 options
*o
= (options
*) calloc(1, sizeof(options
));
611 o
->x
= atoi(argv
[1]);
612 o
->y
= atoi(argv
[2]);
613 o
->v_freq
= atof(argv
[3]);
615 /* XXX should check for errors in the above */
620 if ((strcmp(argv
[n
], "-v") == 0) || (strcmp(argv
[n
], "--verbose") == 0)) {
623 else if ((strcmp(argv
[n
], "-f") == 0) ||
624 (strcmp(argv
[n
], "--fbmode") == 0)) {
627 else if ((strcmp(argv
[n
], "-x") == 0) ||
628 (strcmp(argv
[n
], "--xorgmode") == 0) ||
629 (strcmp(argv
[n
], "--xf86mode") == 0)) {
639 /* if neither xorgmode nor fbmode were requested, default to
642 if (!o
->fbmode
&& !o
->xorgmode
)
649 fprintf(stderr
, "\n");
650 fprintf(stderr
, "usage: %s x y refresh [-v|--verbose] "
651 "[-f|--fbmode] [-x|--xorgmode]\n", argv
[0]);
653 fprintf(stderr
, "\n");
655 fprintf(stderr
, " x : the desired horizontal "
656 "resolution (required)\n");
657 fprintf(stderr
, " y : the desired vertical "
658 "resolution (required)\n");
659 fprintf(stderr
, " refresh : the desired refresh " "rate (required)\n");
660 fprintf(stderr
, " -v|--verbose : enable verbose printouts "
661 "(traces each step of the computation)\n");
662 fprintf(stderr
, " -f|--fbmode : output an fbset(8)-style mode "
664 fprintf(stderr
, " -x|--xorgmode : output an " __XSERVERNAME__
"-style mode "
665 "description (this is the default\n"
666 " if no mode description is requested)\n");
668 fprintf(stderr
, "\n");
676 main(int argc
, char *argv
[])
681 o
= parse_command_line(argc
, argv
);
685 m
= vert_refresh(o
->x
, o
->y
, o
->v_freq
, 0, 0);