xref: /OK3568_Linux_fs/external/xserver/hw/xfree86/modes/xf86gtf.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun /*
2*4882a593Smuzhiyun  * gtf.c  Generate mode timings using the GTF Timing Standard
3*4882a593Smuzhiyun  *
4*4882a593Smuzhiyun  * gcc gtf.c -o gtf -lm -Wall
5*4882a593Smuzhiyun  *
6*4882a593Smuzhiyun  * Copyright (c) 2001, Andy Ritger  aritger@nvidia.com
7*4882a593Smuzhiyun  * All rights reserved.
8*4882a593Smuzhiyun  *
9*4882a593Smuzhiyun  * Redistribution and use in source and binary forms, with or without
10*4882a593Smuzhiyun  * modification, are permitted provided that the following conditions
11*4882a593Smuzhiyun  * are met:
12*4882a593Smuzhiyun  *
13*4882a593Smuzhiyun  * o Redistributions of source code must retain the above copyright
14*4882a593Smuzhiyun  *   notice, this list of conditions and the following disclaimer.
15*4882a593Smuzhiyun  * o Redistributions in binary form must reproduce the above copyright
16*4882a593Smuzhiyun  *   notice, this list of conditions and the following disclaimer
17*4882a593Smuzhiyun  *   in the documentation and/or other materials provided with the
18*4882a593Smuzhiyun  *   distribution.
19*4882a593Smuzhiyun  * o Neither the name of NVIDIA nor the names of its contributors
20*4882a593Smuzhiyun  *   may be used to endorse or promote products derived from this
21*4882a593Smuzhiyun  *   software without specific prior written permission.
22*4882a593Smuzhiyun  *
23*4882a593Smuzhiyun  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
24*4882a593Smuzhiyun  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT
25*4882a593Smuzhiyun  * NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
26*4882a593Smuzhiyun  * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
27*4882a593Smuzhiyun  * THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
28*4882a593Smuzhiyun  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
29*4882a593Smuzhiyun  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
30*4882a593Smuzhiyun  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
31*4882a593Smuzhiyun  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32*4882a593Smuzhiyun  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
33*4882a593Smuzhiyun  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
34*4882a593Smuzhiyun  * POSSIBILITY OF SUCH DAMAGE.
35*4882a593Smuzhiyun  *
36*4882a593Smuzhiyun  * This program is based on the Generalized Timing Formula(GTF TM)
37*4882a593Smuzhiyun  * Standard Version: 1.0, Revision: 1.0
38*4882a593Smuzhiyun  *
39*4882a593Smuzhiyun  * The GTF Document contains the following Copyright information:
40*4882a593Smuzhiyun  *
41*4882a593Smuzhiyun  * Copyright (c) 1994, 1995, 1996 - Video Electronics Standards
42*4882a593Smuzhiyun  * Association. Duplication of this document within VESA member
43*4882a593Smuzhiyun  * companies for review purposes is permitted. All other rights
44*4882a593Smuzhiyun  * reserved.
45*4882a593Smuzhiyun  *
46*4882a593Smuzhiyun  * While every precaution has been taken in the preparation
47*4882a593Smuzhiyun  * of this standard, the Video Electronics Standards Association and
48*4882a593Smuzhiyun  * its contributors assume no responsibility for errors or omissions,
49*4882a593Smuzhiyun  * and make no warranties, expressed or implied, of functionality
50*4882a593Smuzhiyun  * of suitability for any purpose. The sample code contained within
51*4882a593Smuzhiyun  * this standard may be used without restriction.
52*4882a593Smuzhiyun  *
53*4882a593Smuzhiyun  *
54*4882a593Smuzhiyun  *
55*4882a593Smuzhiyun  * The GTF EXCEL(TM) SPREADSHEET, a sample (and the definitive)
56*4882a593Smuzhiyun  * implementation of the GTF Timing Standard, is available at:
57*4882a593Smuzhiyun  *
58*4882a593Smuzhiyun  * ftp://ftp.vesa.org/pub/GTF/GTF_V1R1.xls
59*4882a593Smuzhiyun  */
60*4882a593Smuzhiyun 
61*4882a593Smuzhiyun /* Ruthlessly converted to server code by Adam Jackson <ajax@redhat.com> */
62*4882a593Smuzhiyun 
63*4882a593Smuzhiyun #ifdef HAVE_XORG_CONFIG_H
64*4882a593Smuzhiyun #include <xorg-config.h>
65*4882a593Smuzhiyun #endif
66*4882a593Smuzhiyun 
67*4882a593Smuzhiyun #include "xf86.h"
68*4882a593Smuzhiyun #include "xf86Modes.h"
69*4882a593Smuzhiyun #include <string.h>
70*4882a593Smuzhiyun 
71*4882a593Smuzhiyun #define MARGIN_PERCENT    1.8   /* % of active vertical image                */
72*4882a593Smuzhiyun #define CELL_GRAN         8.0   /* assumed character cell granularity        */
73*4882a593Smuzhiyun #define MIN_PORCH         1     /* minimum front porch                       */
74*4882a593Smuzhiyun #define V_SYNC_RQD        3     /* width of vsync in lines                   */
75*4882a593Smuzhiyun #define H_SYNC_PERCENT    8.0   /* width of hsync as % of total line         */
76*4882a593Smuzhiyun #define MIN_VSYNC_PLUS_BP 550.0 /* min time of vsync + back porch (microsec) */
77*4882a593Smuzhiyun #define M                 600.0 /* blanking formula gradient                 */
78*4882a593Smuzhiyun #define C                 40.0  /* blanking formula offset                   */
79*4882a593Smuzhiyun #define K                 128.0 /* blanking formula scaling factor           */
80*4882a593Smuzhiyun #define J                 20.0  /* blanking formula scaling factor           */
81*4882a593Smuzhiyun 
82*4882a593Smuzhiyun /* C' and M' are part of the Blanking Duty Cycle computation */
83*4882a593Smuzhiyun 
84*4882a593Smuzhiyun #define C_PRIME           (((C - J) * K/256.0) + J)
85*4882a593Smuzhiyun #define M_PRIME           (K/256.0 * M)
86*4882a593Smuzhiyun 
87*4882a593Smuzhiyun /*
88*4882a593Smuzhiyun  * xf86GTFMode() - as defined by the GTF Timing Standard, compute the
89*4882a593Smuzhiyun  * Stage 1 Parameters using the vertical refresh frequency.  In other
90*4882a593Smuzhiyun  * words: input a desired resolution and desired refresh rate, and
91*4882a593Smuzhiyun  * output the GTF mode timings.
92*4882a593Smuzhiyun  *
93*4882a593Smuzhiyun  * XXX All the code is in place to compute interlaced modes, but I don't
94*4882a593Smuzhiyun  * feel like testing it right now.
95*4882a593Smuzhiyun  *
96*4882a593Smuzhiyun  * XXX margin computations are implemented but not tested (nor used by
97*4882a593Smuzhiyun  * XServer of fbset mode descriptions, from what I can tell).
98*4882a593Smuzhiyun  */
99*4882a593Smuzhiyun 
100*4882a593Smuzhiyun DisplayModePtr
xf86GTFMode(int h_pixels,int v_lines,float freq,int interlaced,int margins)101*4882a593Smuzhiyun xf86GTFMode(int h_pixels, int v_lines, float freq, int interlaced, int margins)
102*4882a593Smuzhiyun {
103*4882a593Smuzhiyun     DisplayModeRec *mode = xnfcalloc(1, sizeof(DisplayModeRec));
104*4882a593Smuzhiyun 
105*4882a593Smuzhiyun     float h_pixels_rnd;
106*4882a593Smuzhiyun     float v_lines_rnd;
107*4882a593Smuzhiyun     float v_field_rate_rqd;
108*4882a593Smuzhiyun     float top_margin;
109*4882a593Smuzhiyun     float bottom_margin;
110*4882a593Smuzhiyun     float interlace;
111*4882a593Smuzhiyun     float h_period_est;
112*4882a593Smuzhiyun     float vsync_plus_bp;
113*4882a593Smuzhiyun     float v_back_porch;
114*4882a593Smuzhiyun     float total_v_lines;
115*4882a593Smuzhiyun     float v_field_rate_est;
116*4882a593Smuzhiyun     float h_period;
117*4882a593Smuzhiyun     float v_field_rate;
118*4882a593Smuzhiyun     float v_frame_rate;
119*4882a593Smuzhiyun     float left_margin;
120*4882a593Smuzhiyun     float right_margin;
121*4882a593Smuzhiyun     float total_active_pixels;
122*4882a593Smuzhiyun     float ideal_duty_cycle;
123*4882a593Smuzhiyun     float h_blank;
124*4882a593Smuzhiyun     float total_pixels;
125*4882a593Smuzhiyun     float pixel_freq;
126*4882a593Smuzhiyun     float h_freq;
127*4882a593Smuzhiyun 
128*4882a593Smuzhiyun     float h_sync;
129*4882a593Smuzhiyun     float h_front_porch;
130*4882a593Smuzhiyun     float v_odd_front_porch_lines;
131*4882a593Smuzhiyun 
132*4882a593Smuzhiyun     /*  1. In order to give correct results, the number of horizontal
133*4882a593Smuzhiyun      *  pixels requested is first processed to ensure that it is divisible
134*4882a593Smuzhiyun      *  by the character size, by rounding it to the nearest character
135*4882a593Smuzhiyun      *  cell boundary:
136*4882a593Smuzhiyun      *
137*4882a593Smuzhiyun      *  [H PIXELS RND] = ((ROUND([H PIXELS]/[CELL GRAN RND],0))*[CELLGRAN RND])
138*4882a593Smuzhiyun      */
139*4882a593Smuzhiyun 
140*4882a593Smuzhiyun     h_pixels_rnd = rint((float) h_pixels / CELL_GRAN) * CELL_GRAN;
141*4882a593Smuzhiyun 
142*4882a593Smuzhiyun     /*  2. If interlace is requested, the number of vertical lines assumed
143*4882a593Smuzhiyun      *  by the calculation must be halved, as the computation calculates
144*4882a593Smuzhiyun      *  the number of vertical lines per field. In either case, the
145*4882a593Smuzhiyun      *  number of lines is rounded to the nearest integer.
146*4882a593Smuzhiyun      *
147*4882a593Smuzhiyun      *  [V LINES RND] = IF([INT RQD?]="y", ROUND([V LINES]/2,0),
148*4882a593Smuzhiyun      *                                     ROUND([V LINES],0))
149*4882a593Smuzhiyun      */
150*4882a593Smuzhiyun 
151*4882a593Smuzhiyun     v_lines_rnd = interlaced ?
152*4882a593Smuzhiyun         rint((float) v_lines) / 2.0 : rint((float) v_lines);
153*4882a593Smuzhiyun 
154*4882a593Smuzhiyun     /*  3. Find the frame rate required:
155*4882a593Smuzhiyun      *
156*4882a593Smuzhiyun      *  [V FIELD RATE RQD] = IF([INT RQD?]="y", [I/P FREQ RQD]*2,
157*4882a593Smuzhiyun      *                                          [I/P FREQ RQD])
158*4882a593Smuzhiyun      */
159*4882a593Smuzhiyun 
160*4882a593Smuzhiyun     v_field_rate_rqd = interlaced ? (freq * 2.0) : (freq);
161*4882a593Smuzhiyun 
162*4882a593Smuzhiyun     /*  4. Find number of lines in Top margin:
163*4882a593Smuzhiyun      *
164*4882a593Smuzhiyun      *  [TOP MARGIN (LINES)] = IF([MARGINS RQD?]="Y",
165*4882a593Smuzhiyun      *          ROUND(([MARGIN%]/100*[V LINES RND]),0),
166*4882a593Smuzhiyun      *          0)
167*4882a593Smuzhiyun      */
168*4882a593Smuzhiyun 
169*4882a593Smuzhiyun     top_margin = margins ? rint(MARGIN_PERCENT / 100.0 * v_lines_rnd) : (0.0);
170*4882a593Smuzhiyun 
171*4882a593Smuzhiyun     /*  5. Find number of lines in Bottom margin:
172*4882a593Smuzhiyun      *
173*4882a593Smuzhiyun      *  [BOT MARGIN (LINES)] = IF([MARGINS RQD?]="Y",
174*4882a593Smuzhiyun      *          ROUND(([MARGIN%]/100*[V LINES RND]),0),
175*4882a593Smuzhiyun      *          0)
176*4882a593Smuzhiyun      */
177*4882a593Smuzhiyun 
178*4882a593Smuzhiyun     bottom_margin =
179*4882a593Smuzhiyun         margins ? rint(MARGIN_PERCENT / 100.0 * v_lines_rnd) : (0.0);
180*4882a593Smuzhiyun 
181*4882a593Smuzhiyun     /*  6. If interlace is required, then set variable [INTERLACE]=0.5:
182*4882a593Smuzhiyun      *
183*4882a593Smuzhiyun      *  [INTERLACE]=(IF([INT RQD?]="y",0.5,0))
184*4882a593Smuzhiyun      */
185*4882a593Smuzhiyun 
186*4882a593Smuzhiyun     interlace = interlaced ? 0.5 : 0.0;
187*4882a593Smuzhiyun 
188*4882a593Smuzhiyun     /*  7. Estimate the Horizontal period
189*4882a593Smuzhiyun      *
190*4882a593Smuzhiyun      *  [H PERIOD EST] = ((1/[V FIELD RATE RQD]) - [MIN VSYNC+BP]/1000000) /
191*4882a593Smuzhiyun      *                    ([V LINES RND] + (2*[TOP MARGIN (LINES)]) +
192*4882a593Smuzhiyun      *                     [MIN PORCH RND]+[INTERLACE]) * 1000000
193*4882a593Smuzhiyun      */
194*4882a593Smuzhiyun 
195*4882a593Smuzhiyun     h_period_est = (((1.0 / v_field_rate_rqd) - (MIN_VSYNC_PLUS_BP / 1000000.0))
196*4882a593Smuzhiyun                     / (v_lines_rnd + (2 * top_margin) + MIN_PORCH + interlace)
197*4882a593Smuzhiyun                     * 1000000.0);
198*4882a593Smuzhiyun 
199*4882a593Smuzhiyun     /*  8. Find the number of lines in V sync + back porch:
200*4882a593Smuzhiyun      *
201*4882a593Smuzhiyun      *  [V SYNC+BP] = ROUND(([MIN VSYNC+BP]/[H PERIOD EST]),0)
202*4882a593Smuzhiyun      */
203*4882a593Smuzhiyun 
204*4882a593Smuzhiyun     vsync_plus_bp = rint(MIN_VSYNC_PLUS_BP / h_period_est);
205*4882a593Smuzhiyun 
206*4882a593Smuzhiyun     /*  9. Find the number of lines in V back porch alone:
207*4882a593Smuzhiyun      *
208*4882a593Smuzhiyun      *  [V BACK PORCH] = [V SYNC+BP] - [V SYNC RND]
209*4882a593Smuzhiyun      *
210*4882a593Smuzhiyun      *  XXX is "[V SYNC RND]" a typo? should be [V SYNC RQD]?
211*4882a593Smuzhiyun      */
212*4882a593Smuzhiyun 
213*4882a593Smuzhiyun     v_back_porch = vsync_plus_bp - V_SYNC_RQD;
214*4882a593Smuzhiyun     (void) v_back_porch;
215*4882a593Smuzhiyun 
216*4882a593Smuzhiyun     /*  10. Find the total number of lines in Vertical field period:
217*4882a593Smuzhiyun      *
218*4882a593Smuzhiyun      *  [TOTAL V LINES] = [V LINES RND] + [TOP MARGIN (LINES)] +
219*4882a593Smuzhiyun      *                    [BOT MARGIN (LINES)] + [V SYNC+BP] + [INTERLACE] +
220*4882a593Smuzhiyun      *                    [MIN PORCH RND]
221*4882a593Smuzhiyun      */
222*4882a593Smuzhiyun 
223*4882a593Smuzhiyun     total_v_lines = v_lines_rnd + top_margin + bottom_margin + vsync_plus_bp +
224*4882a593Smuzhiyun         interlace + MIN_PORCH;
225*4882a593Smuzhiyun 
226*4882a593Smuzhiyun     /*  11. Estimate the Vertical field frequency:
227*4882a593Smuzhiyun      *
228*4882a593Smuzhiyun      *  [V FIELD RATE EST] = 1 / [H PERIOD EST] / [TOTAL V LINES] * 1000000
229*4882a593Smuzhiyun      */
230*4882a593Smuzhiyun 
231*4882a593Smuzhiyun     v_field_rate_est = 1.0 / h_period_est / total_v_lines * 1000000.0;
232*4882a593Smuzhiyun 
233*4882a593Smuzhiyun     /*  12. Find the actual horizontal period:
234*4882a593Smuzhiyun      *
235*4882a593Smuzhiyun      *  [H PERIOD] = [H PERIOD EST] / ([V FIELD RATE RQD] / [V FIELD RATE EST])
236*4882a593Smuzhiyun      */
237*4882a593Smuzhiyun 
238*4882a593Smuzhiyun     h_period = h_period_est / (v_field_rate_rqd / v_field_rate_est);
239*4882a593Smuzhiyun 
240*4882a593Smuzhiyun     /*  13. Find the actual Vertical field frequency:
241*4882a593Smuzhiyun      *
242*4882a593Smuzhiyun      *  [V FIELD RATE] = 1 / [H PERIOD] / [TOTAL V LINES] * 1000000
243*4882a593Smuzhiyun      */
244*4882a593Smuzhiyun 
245*4882a593Smuzhiyun     v_field_rate = 1.0 / h_period / total_v_lines * 1000000.0;
246*4882a593Smuzhiyun 
247*4882a593Smuzhiyun     /*  14. Find the Vertical frame frequency:
248*4882a593Smuzhiyun      *
249*4882a593Smuzhiyun      *  [V FRAME RATE] = (IF([INT RQD?]="y", [V FIELD RATE]/2, [V FIELD RATE]))
250*4882a593Smuzhiyun      */
251*4882a593Smuzhiyun 
252*4882a593Smuzhiyun     v_frame_rate = interlaced ? v_field_rate / 2.0 : v_field_rate;
253*4882a593Smuzhiyun     (void) v_frame_rate;
254*4882a593Smuzhiyun 
255*4882a593Smuzhiyun     /*  15. Find number of pixels in left margin:
256*4882a593Smuzhiyun      *
257*4882a593Smuzhiyun      *  [LEFT MARGIN (PIXELS)] = (IF( [MARGINS RQD?]="Y",
258*4882a593Smuzhiyun      *          (ROUND( ([H PIXELS RND] * [MARGIN%] / 100 /
259*4882a593Smuzhiyun      *                   [CELL GRAN RND]),0)) * [CELL GRAN RND],
260*4882a593Smuzhiyun      *          0))
261*4882a593Smuzhiyun      */
262*4882a593Smuzhiyun 
263*4882a593Smuzhiyun     left_margin = margins ?
264*4882a593Smuzhiyun         rint(h_pixels_rnd * MARGIN_PERCENT / 100.0 / CELL_GRAN) * CELL_GRAN :
265*4882a593Smuzhiyun         0.0;
266*4882a593Smuzhiyun 
267*4882a593Smuzhiyun     /*  16. Find number of pixels in right margin:
268*4882a593Smuzhiyun      *
269*4882a593Smuzhiyun      *  [RIGHT MARGIN (PIXELS)] = (IF( [MARGINS RQD?]="Y",
270*4882a593Smuzhiyun      *          (ROUND( ([H PIXELS RND] * [MARGIN%] / 100 /
271*4882a593Smuzhiyun      *                   [CELL GRAN RND]),0)) * [CELL GRAN RND],
272*4882a593Smuzhiyun      *          0))
273*4882a593Smuzhiyun      */
274*4882a593Smuzhiyun 
275*4882a593Smuzhiyun     right_margin = margins ?
276*4882a593Smuzhiyun         rint(h_pixels_rnd * MARGIN_PERCENT / 100.0 / CELL_GRAN) * CELL_GRAN :
277*4882a593Smuzhiyun         0.0;
278*4882a593Smuzhiyun 
279*4882a593Smuzhiyun     /*  17. Find total number of active pixels in image and left and right
280*4882a593Smuzhiyun      *  margins:
281*4882a593Smuzhiyun      *
282*4882a593Smuzhiyun      *  [TOTAL ACTIVE PIXELS] = [H PIXELS RND] + [LEFT MARGIN (PIXELS)] +
283*4882a593Smuzhiyun      *                          [RIGHT MARGIN (PIXELS)]
284*4882a593Smuzhiyun      */
285*4882a593Smuzhiyun 
286*4882a593Smuzhiyun     total_active_pixels = h_pixels_rnd + left_margin + right_margin;
287*4882a593Smuzhiyun 
288*4882a593Smuzhiyun     /*  18. Find the ideal blanking duty cycle from the blanking duty cycle
289*4882a593Smuzhiyun      *  equation:
290*4882a593Smuzhiyun      *
291*4882a593Smuzhiyun      *  [IDEAL DUTY CYCLE] = [C'] - ([M']*[H PERIOD]/1000)
292*4882a593Smuzhiyun      */
293*4882a593Smuzhiyun 
294*4882a593Smuzhiyun     ideal_duty_cycle = C_PRIME - (M_PRIME * h_period / 1000.0);
295*4882a593Smuzhiyun 
296*4882a593Smuzhiyun     /*  19. Find the number of pixels in the blanking time to the nearest
297*4882a593Smuzhiyun      *  double character cell:
298*4882a593Smuzhiyun      *
299*4882a593Smuzhiyun      *  [H BLANK (PIXELS)] = (ROUND(([TOTAL ACTIVE PIXELS] *
300*4882a593Smuzhiyun      *                               [IDEAL DUTY CYCLE] /
301*4882a593Smuzhiyun      *                               (100-[IDEAL DUTY CYCLE]) /
302*4882a593Smuzhiyun      *                               (2*[CELL GRAN RND])), 0))
303*4882a593Smuzhiyun      *                       * (2*[CELL GRAN RND])
304*4882a593Smuzhiyun      */
305*4882a593Smuzhiyun 
306*4882a593Smuzhiyun     h_blank = rint(total_active_pixels *
307*4882a593Smuzhiyun                    ideal_duty_cycle /
308*4882a593Smuzhiyun                    (100.0 - ideal_duty_cycle) /
309*4882a593Smuzhiyun                    (2.0 * CELL_GRAN)) * (2.0 * CELL_GRAN);
310*4882a593Smuzhiyun 
311*4882a593Smuzhiyun     /*  20. Find total number of pixels:
312*4882a593Smuzhiyun      *
313*4882a593Smuzhiyun      *  [TOTAL PIXELS] = [TOTAL ACTIVE PIXELS] + [H BLANK (PIXELS)]
314*4882a593Smuzhiyun      */
315*4882a593Smuzhiyun 
316*4882a593Smuzhiyun     total_pixels = total_active_pixels + h_blank;
317*4882a593Smuzhiyun 
318*4882a593Smuzhiyun     /*  21. Find pixel clock frequency:
319*4882a593Smuzhiyun      *
320*4882a593Smuzhiyun      *  [PIXEL FREQ] = [TOTAL PIXELS] / [H PERIOD]
321*4882a593Smuzhiyun      */
322*4882a593Smuzhiyun 
323*4882a593Smuzhiyun     pixel_freq = total_pixels / h_period;
324*4882a593Smuzhiyun 
325*4882a593Smuzhiyun     /*  22. Find horizontal frequency:
326*4882a593Smuzhiyun      *
327*4882a593Smuzhiyun      *  [H FREQ] = 1000 / [H PERIOD]
328*4882a593Smuzhiyun      */
329*4882a593Smuzhiyun 
330*4882a593Smuzhiyun     h_freq = 1000.0 / h_period;
331*4882a593Smuzhiyun 
332*4882a593Smuzhiyun     /* Stage 1 computations are now complete; I should really pass
333*4882a593Smuzhiyun        the results to another function and do the Stage 2
334*4882a593Smuzhiyun        computations, but I only need a few more values so I'll just
335*4882a593Smuzhiyun        append the computations here for now */
336*4882a593Smuzhiyun 
337*4882a593Smuzhiyun     /*  17. Find the number of pixels in the horizontal sync period:
338*4882a593Smuzhiyun      *
339*4882a593Smuzhiyun      *  [H SYNC (PIXELS)] =(ROUND(([H SYNC%] / 100 * [TOTAL PIXELS] /
340*4882a593Smuzhiyun      *                             [CELL GRAN RND]),0))*[CELL GRAN RND]
341*4882a593Smuzhiyun      */
342*4882a593Smuzhiyun 
343*4882a593Smuzhiyun     h_sync =
344*4882a593Smuzhiyun         rint(H_SYNC_PERCENT / 100.0 * total_pixels / CELL_GRAN) * CELL_GRAN;
345*4882a593Smuzhiyun 
346*4882a593Smuzhiyun     /*  18. Find the number of pixels in the horizontal front porch period:
347*4882a593Smuzhiyun      *
348*4882a593Smuzhiyun      *  [H FRONT PORCH (PIXELS)] = ([H BLANK (PIXELS)]/2)-[H SYNC (PIXELS)]
349*4882a593Smuzhiyun      */
350*4882a593Smuzhiyun 
351*4882a593Smuzhiyun     h_front_porch = (h_blank / 2.0) - h_sync;
352*4882a593Smuzhiyun 
353*4882a593Smuzhiyun     /*  36. Find the number of lines in the odd front porch period:
354*4882a593Smuzhiyun      *
355*4882a593Smuzhiyun      *  [V ODD FRONT PORCH(LINES)]=([MIN PORCH RND]+[INTERLACE])
356*4882a593Smuzhiyun      */
357*4882a593Smuzhiyun 
358*4882a593Smuzhiyun     v_odd_front_porch_lines = MIN_PORCH + interlace;
359*4882a593Smuzhiyun 
360*4882a593Smuzhiyun     /* finally, pack the results in the mode struct */
361*4882a593Smuzhiyun 
362*4882a593Smuzhiyun     mode->HDisplay = (int) (h_pixels_rnd);
363*4882a593Smuzhiyun     mode->HSyncStart = (int) (h_pixels_rnd + h_front_porch);
364*4882a593Smuzhiyun     mode->HSyncEnd = (int) (h_pixels_rnd + h_front_porch + h_sync);
365*4882a593Smuzhiyun     mode->HTotal = (int) (total_pixels);
366*4882a593Smuzhiyun     mode->VDisplay = (int) (v_lines_rnd);
367*4882a593Smuzhiyun     mode->VSyncStart = (int) (v_lines_rnd + v_odd_front_porch_lines);
368*4882a593Smuzhiyun     mode->VSyncEnd = (int) (v_lines_rnd + v_odd_front_porch_lines + V_SYNC_RQD);
369*4882a593Smuzhiyun     mode->VTotal = (int) (total_v_lines);
370*4882a593Smuzhiyun 
371*4882a593Smuzhiyun     mode->Clock = (int) (pixel_freq * 1000.0);
372*4882a593Smuzhiyun     mode->HSync = h_freq;
373*4882a593Smuzhiyun     mode->VRefresh = freq;
374*4882a593Smuzhiyun 
375*4882a593Smuzhiyun     xf86SetModeDefaultName(mode);
376*4882a593Smuzhiyun 
377*4882a593Smuzhiyun     mode->Flags = V_NHSYNC | V_PVSYNC;
378*4882a593Smuzhiyun     if (interlaced) {
379*4882a593Smuzhiyun         mode->VTotal *= 2;
380*4882a593Smuzhiyun         mode->Flags |= V_INTERLACE;
381*4882a593Smuzhiyun     }
382*4882a593Smuzhiyun 
383*4882a593Smuzhiyun     return mode;
384*4882a593Smuzhiyun }
385