2 * gtf.c Generate mode timings using the GTF Timing Standard
4 * gcc gtf.c -o gtf -lm -Wall
6 * Copyright (c) 2001, Andy Ritger aritger@nvidia.com
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
13 * o Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * o Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer
17 * in the documentation and/or other materials provided with the
19 * o Neither the name of NVIDIA nor the names of its contributors
20 * may be used to endorse or promote products derived from this
21 * 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.
36 * This program is based on the Generalized Timing Formula(GTF TM)
37 * Standard Version: 1.0, Revision: 1.0
39 * The GTF Document contains the following Copyright information:
41 * Copyright (c) 1994, 1995, 1996 - Video Electronics Standards
42 * Association. Duplication of this document within VESA member
43 * companies for review purposes is permitted. All other rights
46 * While every precaution has been taken in the preparation
47 * of this standard, the Video Electronics Standards Association and
48 * its contributors assume no responsibility for errors or omissions,
49 * and make no warranties, expressed or implied, of functionality
50 * of suitability for any purpose. The sample code contained within
51 * this standard may be used without restriction.
55 * The GTF EXCEL(TM) SPREADSHEET, a sample (and the definitive)
56 * implementation of the GTF Timing Standard, is available at:
58 * ftp://ftp.vesa.org/pub/GTF/GTF_V1R1.xls
61 /* Ruthlessly converted to server code by Adam Jackson <ajax@redhat.com> */
63 #ifdef HAVE_XORG_CONFIG_H
64 #include <xorg-config.h>
72 #include "xf86Modes.h"
75 #define MARGIN_PERCENT 1.8 /* % of active vertical image */
76 #define CELL_GRAN 8.0 /* assumed character cell granularity */
77 #define MIN_PORCH 1 /* minimum front porch */
78 #define V_SYNC_RQD 3 /* width of vsync in lines */
79 #define H_SYNC_PERCENT 8.0 /* width of hsync as % of total line */
80 #define MIN_VSYNC_PLUS_BP 550.0 /* min time of vsync + back porch (microsec) */
81 #define M 600.0 /* blanking formula gradient */
82 #define C 40.0 /* blanking formula offset */
83 #define K 128.0 /* blanking formula scaling factor */
84 #define J 20.0 /* blanking formula scaling factor */
86 /* C' and M' are part of the Blanking Duty Cycle computation */
88 #define C_PRIME (((C - J) * K/256.0) + J)
89 #define M_PRIME (K/256.0 * M)
92 * xf86GTFMode() - as defined by the GTF Timing Standard, compute the
93 * Stage 1 Parameters using the vertical refresh frequency. In other
94 * words: input a desired resolution and desired refresh rate, and
95 * output the GTF mode timings.
97 * XXX All the code is in place to compute interlaced modes, but I don't
98 * feel like testing it right now.
100 * XXX margin computations are implemented but not tested (nor used by
101 * XServer of fbset mode descriptions, from what I can tell).
105 xf86GTFMode(int h_pixels
, int v_lines
, float freq
, int interlaced
, int margins
)
107 DisplayModeRec
*mode
= xnfcalloc(1, sizeof(DisplayModeRec
));
111 float v_field_rate_rqd
;
119 float v_field_rate_est
;
125 float total_active_pixels
;
126 float ideal_duty_cycle
;
134 float v_odd_front_porch_lines
;
136 /* 1. In order to give correct results, the number of horizontal
137 * pixels requested is first processed to ensure that it is divisible
138 * by the character size, by rounding it to the nearest character
141 * [H PIXELS RND] = ((ROUND([H PIXELS]/[CELL GRAN RND],0))*[CELLGRAN RND])
144 h_pixels_rnd
= rint((float) h_pixels
/ CELL_GRAN
) * CELL_GRAN
;
146 /* 2. If interlace is requested, the number of vertical lines assumed
147 * by the calculation must be halved, as the computation calculates
148 * the number of vertical lines per field. In either case, the
149 * number of lines is rounded to the nearest integer.
151 * [V LINES RND] = IF([INT RQD?]="y", ROUND([V LINES]/2,0),
152 * ROUND([V LINES],0))
155 v_lines_rnd
= interlaced
?
156 rint((float) v_lines
) / 2.0 : rint((float) v_lines
);
158 /* 3. Find the frame rate required:
160 * [V FIELD RATE RQD] = IF([INT RQD?]="y", [I/P FREQ RQD]*2,
164 v_field_rate_rqd
= interlaced
? (freq
* 2.0) : (freq
);
166 /* 4. Find number of lines in Top margin:
168 * [TOP MARGIN (LINES)] = IF([MARGINS RQD?]="Y",
169 * ROUND(([MARGIN%]/100*[V LINES RND]),0),
173 top_margin
= margins
? rint(MARGIN_PERCENT
/ 100.0 * v_lines_rnd
) : (0.0);
175 /* 5. Find number of lines in Bottom margin:
177 * [BOT MARGIN (LINES)] = IF([MARGINS RQD?]="Y",
178 * ROUND(([MARGIN%]/100*[V LINES RND]),0),
183 margins
? rint(MARGIN_PERCENT
/ 100.0 * v_lines_rnd
) : (0.0);
185 /* 6. If interlace is required, then set variable [INTERLACE]=0.5:
187 * [INTERLACE]=(IF([INT RQD?]="y",0.5,0))
190 interlace
= interlaced
? 0.5 : 0.0;
192 /* 7. Estimate the Horizontal period
194 * [H PERIOD EST] = ((1/[V FIELD RATE RQD]) - [MIN VSYNC+BP]/1000000) /
195 * ([V LINES RND] + (2*[TOP MARGIN (LINES)]) +
196 * [MIN PORCH RND]+[INTERLACE]) * 1000000
199 h_period_est
= (((1.0 / v_field_rate_rqd
) - (MIN_VSYNC_PLUS_BP
/ 1000000.0))
200 / (v_lines_rnd
+ (2 * top_margin
) + MIN_PORCH
+ interlace
)
203 /* 8. Find the number of lines in V sync + back porch:
205 * [V SYNC+BP] = ROUND(([MIN VSYNC+BP]/[H PERIOD EST]),0)
208 vsync_plus_bp
= rint(MIN_VSYNC_PLUS_BP
/ h_period_est
);
210 /* 9. Find the number of lines in V back porch alone:
212 * [V BACK PORCH] = [V SYNC+BP] - [V SYNC RND]
214 * XXX is "[V SYNC RND]" a typo? should be [V SYNC RQD]?
217 v_back_porch
= vsync_plus_bp
- V_SYNC_RQD
;
219 /* 10. Find the total number of lines in Vertical field period:
221 * [TOTAL V LINES] = [V LINES RND] + [TOP MARGIN (LINES)] +
222 * [BOT MARGIN (LINES)] + [V SYNC+BP] + [INTERLACE] +
226 total_v_lines
= v_lines_rnd
+ top_margin
+ bottom_margin
+ vsync_plus_bp
+
227 interlace
+ MIN_PORCH
;
229 /* 11. Estimate the Vertical field frequency:
231 * [V FIELD RATE EST] = 1 / [H PERIOD EST] / [TOTAL V LINES] * 1000000
234 v_field_rate_est
= 1.0 / h_period_est
/ total_v_lines
* 1000000.0;
236 /* 12. Find the actual horizontal period:
238 * [H PERIOD] = [H PERIOD EST] / ([V FIELD RATE RQD] / [V FIELD RATE EST])
241 h_period
= h_period_est
/ (v_field_rate_rqd
/ v_field_rate_est
);
243 /* 13. Find the actual Vertical field frequency:
245 * [V FIELD RATE] = 1 / [H PERIOD] / [TOTAL V LINES] * 1000000
248 v_field_rate
= 1.0 / h_period
/ total_v_lines
* 1000000.0;
250 /* 14. Find the Vertical frame frequency:
252 * [V FRAME RATE] = (IF([INT RQD?]="y", [V FIELD RATE]/2, [V FIELD RATE]))
255 v_frame_rate
= interlaced
? v_field_rate
/ 2.0 : v_field_rate
;
257 /* 15. Find number of pixels in left margin:
259 * [LEFT MARGIN (PIXELS)] = (IF( [MARGINS RQD?]="Y",
260 * (ROUND( ([H PIXELS RND] * [MARGIN%] / 100 /
261 * [CELL GRAN RND]),0)) * [CELL GRAN RND],
265 left_margin
= margins
?
266 rint(h_pixels_rnd
* MARGIN_PERCENT
/ 100.0 / CELL_GRAN
) * CELL_GRAN
:
269 /* 16. Find number of pixels in right margin:
271 * [RIGHT MARGIN (PIXELS)] = (IF( [MARGINS RQD?]="Y",
272 * (ROUND( ([H PIXELS RND] * [MARGIN%] / 100 /
273 * [CELL GRAN RND]),0)) * [CELL GRAN RND],
277 right_margin
= margins
?
278 rint(h_pixels_rnd
* MARGIN_PERCENT
/ 100.0 / CELL_GRAN
) * CELL_GRAN
:
281 /* 17. Find total number of active pixels in image and left and right
284 * [TOTAL ACTIVE PIXELS] = [H PIXELS RND] + [LEFT MARGIN (PIXELS)] +
285 * [RIGHT MARGIN (PIXELS)]
288 total_active_pixels
= h_pixels_rnd
+ left_margin
+ right_margin
;
290 /* 18. Find the ideal blanking duty cycle from the blanking duty cycle
293 * [IDEAL DUTY CYCLE] = [C'] - ([M']*[H PERIOD]/1000)
296 ideal_duty_cycle
= C_PRIME
- (M_PRIME
* h_period
/ 1000.0);
298 /* 19. Find the number of pixels in the blanking time to the nearest
299 * double character cell:
301 * [H BLANK (PIXELS)] = (ROUND(([TOTAL ACTIVE PIXELS] *
302 * [IDEAL DUTY CYCLE] /
303 * (100-[IDEAL DUTY CYCLE]) /
304 * (2*[CELL GRAN RND])), 0))
305 * * (2*[CELL GRAN RND])
308 h_blank
= rint(total_active_pixels
*
310 (100.0 - ideal_duty_cycle
) /
311 (2.0 * CELL_GRAN
)) * (2.0 * CELL_GRAN
);
313 /* 20. Find total number of pixels:
315 * [TOTAL PIXELS] = [TOTAL ACTIVE PIXELS] + [H BLANK (PIXELS)]
318 total_pixels
= total_active_pixels
+ h_blank
;
320 /* 21. Find pixel clock frequency:
322 * [PIXEL FREQ] = [TOTAL PIXELS] / [H PERIOD]
325 pixel_freq
= total_pixels
/ h_period
;
327 /* 22. Find horizontal frequency:
329 * [H FREQ] = 1000 / [H PERIOD]
332 h_freq
= 1000.0 / h_period
;
334 /* Stage 1 computations are now complete; I should really pass
335 the results to another function and do the Stage 2
336 computations, but I only need a few more values so I'll just
337 append the computations here for now */
339 /* 17. Find the number of pixels in the horizontal sync period:
341 * [H SYNC (PIXELS)] =(ROUND(([H SYNC%] / 100 * [TOTAL PIXELS] /
342 * [CELL GRAN RND]),0))*[CELL GRAN RND]
346 rint(H_SYNC_PERCENT
/ 100.0 * total_pixels
/ CELL_GRAN
) * CELL_GRAN
;
348 /* 18. Find the number of pixels in the horizontal front porch period:
350 * [H FRONT PORCH (PIXELS)] = ([H BLANK (PIXELS)]/2)-[H SYNC (PIXELS)]
353 h_front_porch
= (h_blank
/ 2.0) - h_sync
;
355 /* 36. Find the number of lines in the odd front porch period:
357 * [V ODD FRONT PORCH(LINES)]=([MIN PORCH RND]+[INTERLACE])
360 v_odd_front_porch_lines
= MIN_PORCH
+ interlace
;
362 /* finally, pack the results in the mode struct */
364 mode
->HDisplay
= (int) (h_pixels_rnd
);
365 mode
->HSyncStart
= (int) (h_pixels_rnd
+ h_front_porch
);
366 mode
->HSyncEnd
= (int) (h_pixels_rnd
+ h_front_porch
+ h_sync
);
367 mode
->HTotal
= (int) (total_pixels
);
368 mode
->VDisplay
= (int) (v_lines_rnd
);
369 mode
->VSyncStart
= (int) (v_lines_rnd
+ v_odd_front_porch_lines
);
370 mode
->VSyncEnd
= (int) (v_lines_rnd
+ v_odd_front_porch_lines
+ V_SYNC_RQD
);
371 mode
->VTotal
= (int) (total_v_lines
);
373 mode
->Clock
= (int) (pixel_freq
* 1000.0);
374 mode
->HSync
= h_freq
;
375 mode
->VRefresh
= freq
;
377 xf86SetModeDefaultName(mode
);
379 mode
->Flags
= V_NHSYNC
| V_PVSYNC
;
382 mode
->Flags
|= V_INTERLACE
;