xref: /OK3568_Linux_fs/external/xserver/hw/xfree86/utils/gtf/gtf.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun /* gtf.c  Generate mode timings using the GTF Timing Standard
2*4882a593Smuzhiyun  *
3*4882a593Smuzhiyun  * gcc gtf.c -o gtf -lm -Wall
4*4882a593Smuzhiyun  *
5*4882a593Smuzhiyun  * Copyright (c) 2001, Andy Ritger  aritger@nvidia.com
6*4882a593Smuzhiyun  * All rights reserved.
7*4882a593Smuzhiyun  *
8*4882a593Smuzhiyun  * Redistribution and use in source and binary forms, with or without
9*4882a593Smuzhiyun  * modification, are permitted provided that the following conditions
10*4882a593Smuzhiyun  * are met:
11*4882a593Smuzhiyun  *
12*4882a593Smuzhiyun  * o Redistributions of source code must retain the above copyright
13*4882a593Smuzhiyun  *   notice, this list of conditions and the following disclaimer.
14*4882a593Smuzhiyun  * o Redistributions in binary form must reproduce the above copyright
15*4882a593Smuzhiyun  *   notice, this list of conditions and the following disclaimer
16*4882a593Smuzhiyun  *   in the documentation and/or other materials provided with the
17*4882a593Smuzhiyun  *   distribution.
18*4882a593Smuzhiyun  * o Neither the name of NVIDIA nor the names of its contributors
19*4882a593Smuzhiyun  *   may be used to endorse or promote products derived from this
20*4882a593Smuzhiyun  *   software without specific prior written permission.
21*4882a593Smuzhiyun  *
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  *
37*4882a593Smuzhiyun  *
38*4882a593Smuzhiyun  * This program is based on the Generalized Timing Formula(GTF TM)
39*4882a593Smuzhiyun  * Standard Version: 1.0, Revision: 1.0
40*4882a593Smuzhiyun  *
41*4882a593Smuzhiyun  * The GTF Document contains the following Copyright information:
42*4882a593Smuzhiyun  *
43*4882a593Smuzhiyun  * Copyright (c) 1994, 1995, 1996 - Video Electronics Standards
44*4882a593Smuzhiyun  * Association. Duplication of this document within VESA member
45*4882a593Smuzhiyun  * companies for review purposes is permitted. All other rights
46*4882a593Smuzhiyun  * reserved.
47*4882a593Smuzhiyun  *
48*4882a593Smuzhiyun  * While every precaution has been taken in the preparation
49*4882a593Smuzhiyun  * of this standard, the Video Electronics Standards Association and
50*4882a593Smuzhiyun  * its contributors assume no responsibility for errors or omissions,
51*4882a593Smuzhiyun  * and make no warranties, expressed or implied, of functionality
52*4882a593Smuzhiyun  * of suitability for any purpose. The sample code contained within
53*4882a593Smuzhiyun  * this standard may be used without restriction.
54*4882a593Smuzhiyun  *
55*4882a593Smuzhiyun  *
56*4882a593Smuzhiyun  *
57*4882a593Smuzhiyun  * The GTF EXCEL(TM) SPREADSHEET, a sample (and the definitive)
58*4882a593Smuzhiyun  * implementation of the GTF Timing Standard, is available at:
59*4882a593Smuzhiyun  *
60*4882a593Smuzhiyun  * ftp://ftp.vesa.org/pub/GTF/GTF_V1R1.xls
61*4882a593Smuzhiyun  *
62*4882a593Smuzhiyun  *
63*4882a593Smuzhiyun  *
64*4882a593Smuzhiyun  * This program takes a desired resolution and vertical refresh rate,
65*4882a593Smuzhiyun  * and computes mode timings according to the GTF Timing Standard.
66*4882a593Smuzhiyun  * These mode timings can then be formatted as an XServer modeline
67*4882a593Smuzhiyun  * or a mode description for use by fbset(8).
68*4882a593Smuzhiyun  *
69*4882a593Smuzhiyun  *
70*4882a593Smuzhiyun  *
71*4882a593Smuzhiyun  * NOTES:
72*4882a593Smuzhiyun  *
73*4882a593Smuzhiyun  * The GTF allows for computation of "margins" (the visible border
74*4882a593Smuzhiyun  * surrounding the addressable video); on most non-overscan type
75*4882a593Smuzhiyun  * systems, the margin period is zero.  I've implemented the margin
76*4882a593Smuzhiyun  * computations but not enabled it because 1) I don't really have
77*4882a593Smuzhiyun  * any experience with this, and 2) neither XServer modelines nor
78*4882a593Smuzhiyun  * fbset fb.modes provide an obvious way for margin timings to be
79*4882a593Smuzhiyun  * included in their mode descriptions (needs more investigation).
80*4882a593Smuzhiyun  *
81*4882a593Smuzhiyun  * The GTF provides for computation of interlaced mode timings;
82*4882a593Smuzhiyun  * I've implemented the computations but not enabled them, yet.
83*4882a593Smuzhiyun  * I should probably enable and test this at some point.
84*4882a593Smuzhiyun  *
85*4882a593Smuzhiyun  *
86*4882a593Smuzhiyun  *
87*4882a593Smuzhiyun  * TODO:
88*4882a593Smuzhiyun  *
89*4882a593Smuzhiyun  * o Add support for interlaced modes.
90*4882a593Smuzhiyun  *
91*4882a593Smuzhiyun  * o Implement the other portions of the GTF: compute mode timings
92*4882a593Smuzhiyun  *   given either the desired pixel clock or the desired horizontal
93*4882a593Smuzhiyun  *   frequency.
94*4882a593Smuzhiyun  *
95*4882a593Smuzhiyun  * o It would be nice if this were more general purpose to do things
96*4882a593Smuzhiyun  *   outside the scope of the GTF: like generate double scan mode
97*4882a593Smuzhiyun  *   timings, for example.
98*4882a593Smuzhiyun  *
99*4882a593Smuzhiyun  * o Printing digits to the right of the decimal point when the
100*4882a593Smuzhiyun  *   digits are 0 annoys me.
101*4882a593Smuzhiyun  *
102*4882a593Smuzhiyun  * o Error checking.
103*4882a593Smuzhiyun  *
104*4882a593Smuzhiyun  */
105*4882a593Smuzhiyun 
106*4882a593Smuzhiyun #ifdef HAVE_XORG_CONFIG_H
107*4882a593Smuzhiyun #include <xorg-config.h>
108*4882a593Smuzhiyun #endif
109*4882a593Smuzhiyun 
110*4882a593Smuzhiyun #include <stdio.h>
111*4882a593Smuzhiyun #include <stdlib.h>
112*4882a593Smuzhiyun #include <string.h>
113*4882a593Smuzhiyun #include <math.h>
114*4882a593Smuzhiyun 
115*4882a593Smuzhiyun #define MARGIN_PERCENT    1.8   /* % of active vertical image                */
116*4882a593Smuzhiyun #define CELL_GRAN         8.0   /* assumed character cell granularity        */
117*4882a593Smuzhiyun #define MIN_PORCH         1     /* minimum front porch                       */
118*4882a593Smuzhiyun #define V_SYNC_RQD        3     /* width of vsync in lines                   */
119*4882a593Smuzhiyun #define H_SYNC_PERCENT    8.0   /* width of hsync as % of total line         */
120*4882a593Smuzhiyun #define MIN_VSYNC_PLUS_BP 550.0 /* min time of vsync + back porch (microsec) */
121*4882a593Smuzhiyun #define M                 600.0 /* blanking formula gradient                 */
122*4882a593Smuzhiyun #define C                 40.0  /* blanking formula offset                   */
123*4882a593Smuzhiyun #define K                 128.0 /* blanking formula scaling factor           */
124*4882a593Smuzhiyun #define J                 20.0  /* blanking formula scaling factor           */
125*4882a593Smuzhiyun 
126*4882a593Smuzhiyun /* C' and M' are part of the Blanking Duty Cycle computation */
127*4882a593Smuzhiyun 
128*4882a593Smuzhiyun #define C_PRIME           (((C - J) * K/256.0) + J)
129*4882a593Smuzhiyun #define M_PRIME           (K/256.0 * M)
130*4882a593Smuzhiyun 
131*4882a593Smuzhiyun /* struct definitions */
132*4882a593Smuzhiyun 
133*4882a593Smuzhiyun typedef struct __mode {
134*4882a593Smuzhiyun     int hr, hss, hse, hfl;
135*4882a593Smuzhiyun     int vr, vss, vse, vfl;
136*4882a593Smuzhiyun     float pclk, h_freq, v_freq;
137*4882a593Smuzhiyun } mode;
138*4882a593Smuzhiyun 
139*4882a593Smuzhiyun typedef struct __options {
140*4882a593Smuzhiyun     int x, y;
141*4882a593Smuzhiyun     int xorgmode, fbmode;
142*4882a593Smuzhiyun     float v_freq;
143*4882a593Smuzhiyun } options;
144*4882a593Smuzhiyun 
145*4882a593Smuzhiyun /* prototypes */
146*4882a593Smuzhiyun 
147*4882a593Smuzhiyun void print_value(int n, const char *name, float val);
148*4882a593Smuzhiyun void print_xf86_mode(mode * m);
149*4882a593Smuzhiyun void print_fb_mode(mode * m);
150*4882a593Smuzhiyun mode *vert_refresh(int h_pixels, int v_lines, float freq,
151*4882a593Smuzhiyun                    int interlaced, int margins);
152*4882a593Smuzhiyun options *parse_command_line(int argc, char *argv[]);
153*4882a593Smuzhiyun 
154*4882a593Smuzhiyun /*
155*4882a593Smuzhiyun  * print_value() - print the result of the named computation; this is
156*4882a593Smuzhiyun  * useful when comparing against the GTF EXCEL spreadsheet.
157*4882a593Smuzhiyun  */
158*4882a593Smuzhiyun 
159*4882a593Smuzhiyun int global_verbose = 0;
160*4882a593Smuzhiyun 
161*4882a593Smuzhiyun void
print_value(int n,const char * name,float val)162*4882a593Smuzhiyun print_value(int n, const char *name, float val)
163*4882a593Smuzhiyun {
164*4882a593Smuzhiyun     if (global_verbose) {
165*4882a593Smuzhiyun         printf("%2d: %-27s: %15f\n", n, name, val);
166*4882a593Smuzhiyun     }
167*4882a593Smuzhiyun }
168*4882a593Smuzhiyun 
169*4882a593Smuzhiyun /* print_xf86_mode() - print the XServer modeline, given mode timings. */
170*4882a593Smuzhiyun 
171*4882a593Smuzhiyun void
print_xf86_mode(mode * m)172*4882a593Smuzhiyun print_xf86_mode(mode * m)
173*4882a593Smuzhiyun {
174*4882a593Smuzhiyun     printf("\n");
175*4882a593Smuzhiyun     printf("  # %dx%d @ %.2f Hz (GTF) hsync: %.2f kHz; pclk: %.2f MHz\n",
176*4882a593Smuzhiyun            m->hr, m->vr, m->v_freq, m->h_freq, m->pclk);
177*4882a593Smuzhiyun 
178*4882a593Smuzhiyun     printf("  Modeline \"%dx%d_%.2f\"  %.2f"
179*4882a593Smuzhiyun            "  %d %d %d %d"
180*4882a593Smuzhiyun            "  %d %d %d %d"
181*4882a593Smuzhiyun            "  -HSync +Vsync\n\n",
182*4882a593Smuzhiyun            m->hr, m->vr, m->v_freq, m->pclk,
183*4882a593Smuzhiyun            m->hr, m->hss, m->hse, m->hfl, m->vr, m->vss, m->vse, m->vfl);
184*4882a593Smuzhiyun 
185*4882a593Smuzhiyun }
186*4882a593Smuzhiyun 
187*4882a593Smuzhiyun /*
188*4882a593Smuzhiyun  * print_fb_mode() - print a mode description in fbset(8) format;
189*4882a593Smuzhiyun  * see the fb.modes(8) manpage.  The timing description used in
190*4882a593Smuzhiyun  * this is rather odd; they use "left and right margin" to refer
191*4882a593Smuzhiyun  * to the portion of the hblank before and after the sync pulse
192*4882a593Smuzhiyun  * by conceptually wrapping the portion of the blank after the pulse
193*4882a593Smuzhiyun  * to infront of the visible region; ie:
194*4882a593Smuzhiyun  *
195*4882a593Smuzhiyun  *
196*4882a593Smuzhiyun  * Timing description I'm accustomed to:
197*4882a593Smuzhiyun  *
198*4882a593Smuzhiyun  *
199*4882a593Smuzhiyun  *
200*4882a593Smuzhiyun  *     <--------1--------> <--2--> <--3--> <--4-->
201*4882a593Smuzhiyun  *                                _________
202*4882a593Smuzhiyun  *    |-------------------|_______|       |_______
203*4882a593Smuzhiyun  *
204*4882a593Smuzhiyun  *                        R       SS      SE     FL
205*4882a593Smuzhiyun  *
206*4882a593Smuzhiyun  * 1: visible image
207*4882a593Smuzhiyun  * 2: blank before sync (aka front porch)
208*4882a593Smuzhiyun  * 3: sync pulse
209*4882a593Smuzhiyun  * 4: blank after sync (aka back porch)
210*4882a593Smuzhiyun  * R: Resolution
211*4882a593Smuzhiyun  * SS: Sync Start
212*4882a593Smuzhiyun  * SE: Sync End
213*4882a593Smuzhiyun  * FL: Frame Length
214*4882a593Smuzhiyun  *
215*4882a593Smuzhiyun  *
216*4882a593Smuzhiyun  * But the fb.modes format is:
217*4882a593Smuzhiyun  *
218*4882a593Smuzhiyun  *
219*4882a593Smuzhiyun  *    <--4--> <--------1--------> <--2--> <--3-->
220*4882a593Smuzhiyun  *                                       _________
221*4882a593Smuzhiyun  *    _______|-------------------|_______|       |
222*4882a593Smuzhiyun  *
223*4882a593Smuzhiyun  * The fb.modes(8) manpage refers to <4> and <2> as the left and
224*4882a593Smuzhiyun  * right "margin" (as well as upper and lower margin in the vertical
225*4882a593Smuzhiyun  * direction) -- note that this has nothing to do with the term
226*4882a593Smuzhiyun  * "margin" used in the GTF Timing Standard.
227*4882a593Smuzhiyun  *
228*4882a593Smuzhiyun  * XXX always prints the 32 bit mode -- should I provide a command
229*4882a593Smuzhiyun  * line option to specify the bpp?  It's simple enough for a user
230*4882a593Smuzhiyun  * to edit the mode description after it's generated.
231*4882a593Smuzhiyun  */
232*4882a593Smuzhiyun 
233*4882a593Smuzhiyun void
print_fb_mode(mode * m)234*4882a593Smuzhiyun print_fb_mode(mode * m)
235*4882a593Smuzhiyun {
236*4882a593Smuzhiyun     printf("\n");
237*4882a593Smuzhiyun     printf("mode \"%dx%d %.2fHz 32bit (GTF)\"\n", m->hr, m->vr, m->v_freq);
238*4882a593Smuzhiyun     printf("    # PCLK: %.2f MHz, H: %.2f kHz, V: %.2f Hz\n",
239*4882a593Smuzhiyun            m->pclk, m->h_freq, m->v_freq);
240*4882a593Smuzhiyun     printf("    geometry %d %d %d %d 32\n", m->hr, m->vr, m->hr, m->vr);
241*4882a593Smuzhiyun     printf("    timings %d %d %d %d %d %d %d\n", (int)lrint(1000000.0 / m->pclk),       /* pixclock in picoseconds */
242*4882a593Smuzhiyun            m->hfl - m->hse,     /* left margin (in pixels) */
243*4882a593Smuzhiyun            m->hss - m->hr,      /* right margin (in pixels) */
244*4882a593Smuzhiyun            m->vfl - m->vse,     /* upper margin (in pixel lines) */
245*4882a593Smuzhiyun            m->vss - m->vr,      /* lower margin (in pixel lines) */
246*4882a593Smuzhiyun            m->hse - m->hss,     /* horizontal sync length (pixels) */
247*4882a593Smuzhiyun            m->vse - m->vss);    /* vert sync length (pixel lines) */
248*4882a593Smuzhiyun     printf("    hsync low\n");
249*4882a593Smuzhiyun     printf("    vsync high\n");
250*4882a593Smuzhiyun     printf("endmode\n\n");
251*4882a593Smuzhiyun 
252*4882a593Smuzhiyun }
253*4882a593Smuzhiyun 
254*4882a593Smuzhiyun /*
255*4882a593Smuzhiyun  * vert_refresh() - as defined by the GTF Timing Standard, compute the
256*4882a593Smuzhiyun  * Stage 1 Parameters using the vertical refresh frequency.  In other
257*4882a593Smuzhiyun  * words: input a desired resolution and desired refresh rate, and
258*4882a593Smuzhiyun  * output the GTF mode timings.
259*4882a593Smuzhiyun  *
260*4882a593Smuzhiyun  * XXX All the code is in place to compute interlaced modes, but I don't
261*4882a593Smuzhiyun  * feel like testing it right now.
262*4882a593Smuzhiyun  *
263*4882a593Smuzhiyun  * XXX margin computations are implemented but not tested (nor used by
264*4882a593Smuzhiyun  * XServer of fbset mode descriptions, from what I can tell).
265*4882a593Smuzhiyun  */
266*4882a593Smuzhiyun 
267*4882a593Smuzhiyun mode *
vert_refresh(int h_pixels,int v_lines,float freq,int interlaced,int margins)268*4882a593Smuzhiyun vert_refresh(int h_pixels, int v_lines, float freq, int interlaced, int margins)
269*4882a593Smuzhiyun {
270*4882a593Smuzhiyun     float h_pixels_rnd;
271*4882a593Smuzhiyun     float v_lines_rnd;
272*4882a593Smuzhiyun     float v_field_rate_rqd;
273*4882a593Smuzhiyun     float top_margin;
274*4882a593Smuzhiyun     float bottom_margin;
275*4882a593Smuzhiyun     float interlace;
276*4882a593Smuzhiyun     float h_period_est;
277*4882a593Smuzhiyun     float vsync_plus_bp;
278*4882a593Smuzhiyun     float v_back_porch;
279*4882a593Smuzhiyun     float total_v_lines;
280*4882a593Smuzhiyun     float v_field_rate_est;
281*4882a593Smuzhiyun     float h_period;
282*4882a593Smuzhiyun     float v_field_rate;
283*4882a593Smuzhiyun     float v_frame_rate;
284*4882a593Smuzhiyun     float left_margin;
285*4882a593Smuzhiyun     float right_margin;
286*4882a593Smuzhiyun     float total_active_pixels;
287*4882a593Smuzhiyun     float ideal_duty_cycle;
288*4882a593Smuzhiyun     float h_blank;
289*4882a593Smuzhiyun     float total_pixels;
290*4882a593Smuzhiyun     float pixel_freq;
291*4882a593Smuzhiyun     float h_freq;
292*4882a593Smuzhiyun 
293*4882a593Smuzhiyun     float h_sync;
294*4882a593Smuzhiyun     float h_front_porch;
295*4882a593Smuzhiyun     float v_odd_front_porch_lines;
296*4882a593Smuzhiyun 
297*4882a593Smuzhiyun     mode *m = (mode *) malloc(sizeof(mode));
298*4882a593Smuzhiyun 
299*4882a593Smuzhiyun     /*  1. In order to give correct results, the number of horizontal
300*4882a593Smuzhiyun      *  pixels requested is first processed to ensure that it is divisible
301*4882a593Smuzhiyun      *  by the character size, by rounding it to the nearest character
302*4882a593Smuzhiyun      *  cell boundary:
303*4882a593Smuzhiyun      *
304*4882a593Smuzhiyun      *  [H PIXELS RND] = ((ROUND([H PIXELS]/[CELL GRAN RND],0))*[CELLGRAN RND])
305*4882a593Smuzhiyun      */
306*4882a593Smuzhiyun 
307*4882a593Smuzhiyun     h_pixels_rnd = rint((float) h_pixels / CELL_GRAN) * CELL_GRAN;
308*4882a593Smuzhiyun 
309*4882a593Smuzhiyun     print_value(1, "[H PIXELS RND]", h_pixels_rnd);
310*4882a593Smuzhiyun 
311*4882a593Smuzhiyun     /*  2. If interlace is requested, the number of vertical lines assumed
312*4882a593Smuzhiyun      *  by the calculation must be halved, as the computation calculates
313*4882a593Smuzhiyun      *  the number of vertical lines per field. In either case, the
314*4882a593Smuzhiyun      *  number of lines is rounded to the nearest integer.
315*4882a593Smuzhiyun      *
316*4882a593Smuzhiyun      *  [V LINES RND] = IF([INT RQD?]="y", ROUND([V LINES]/2,0),
317*4882a593Smuzhiyun      *                                     ROUND([V LINES],0))
318*4882a593Smuzhiyun      */
319*4882a593Smuzhiyun 
320*4882a593Smuzhiyun     v_lines_rnd = interlaced ?
321*4882a593Smuzhiyun         rint((float) v_lines) / 2.0 : rint((float) v_lines);
322*4882a593Smuzhiyun 
323*4882a593Smuzhiyun     print_value(2, "[V LINES RND]", v_lines_rnd);
324*4882a593Smuzhiyun 
325*4882a593Smuzhiyun     /*  3. Find the frame rate required:
326*4882a593Smuzhiyun      *
327*4882a593Smuzhiyun      *  [V FIELD RATE RQD] = IF([INT RQD?]="y", [I/P FREQ RQD]*2,
328*4882a593Smuzhiyun      *                                          [I/P FREQ RQD])
329*4882a593Smuzhiyun      */
330*4882a593Smuzhiyun 
331*4882a593Smuzhiyun     v_field_rate_rqd = interlaced ? (freq * 2.0) : (freq);
332*4882a593Smuzhiyun 
333*4882a593Smuzhiyun     print_value(3, "[V FIELD RATE RQD]", v_field_rate_rqd);
334*4882a593Smuzhiyun 
335*4882a593Smuzhiyun     /*  4. Find number of lines in Top margin:
336*4882a593Smuzhiyun      *
337*4882a593Smuzhiyun      *  [TOP MARGIN (LINES)] = IF([MARGINS RQD?]="Y",
338*4882a593Smuzhiyun      *          ROUND(([MARGIN%]/100*[V LINES RND]),0),
339*4882a593Smuzhiyun      *          0)
340*4882a593Smuzhiyun      */
341*4882a593Smuzhiyun 
342*4882a593Smuzhiyun     top_margin = margins ? rint(MARGIN_PERCENT / 100.0 * v_lines_rnd) : (0.0);
343*4882a593Smuzhiyun 
344*4882a593Smuzhiyun     print_value(4, "[TOP MARGIN (LINES)]", top_margin);
345*4882a593Smuzhiyun 
346*4882a593Smuzhiyun     /*  5. Find number of lines in Bottom margin:
347*4882a593Smuzhiyun      *
348*4882a593Smuzhiyun      *  [BOT MARGIN (LINES)] = IF([MARGINS RQD?]="Y",
349*4882a593Smuzhiyun      *          ROUND(([MARGIN%]/100*[V LINES RND]),0),
350*4882a593Smuzhiyun      *          0)
351*4882a593Smuzhiyun      */
352*4882a593Smuzhiyun 
353*4882a593Smuzhiyun     bottom_margin =
354*4882a593Smuzhiyun         margins ? rint(MARGIN_PERCENT / 100.0 * v_lines_rnd) : (0.0);
355*4882a593Smuzhiyun 
356*4882a593Smuzhiyun     print_value(5, "[BOT MARGIN (LINES)]", bottom_margin);
357*4882a593Smuzhiyun 
358*4882a593Smuzhiyun     /*  6. If interlace is required, then set variable [INTERLACE]=0.5:
359*4882a593Smuzhiyun      *
360*4882a593Smuzhiyun      *  [INTERLACE]=(IF([INT RQD?]="y",0.5,0))
361*4882a593Smuzhiyun      */
362*4882a593Smuzhiyun 
363*4882a593Smuzhiyun     interlace = interlaced ? 0.5 : 0.0;
364*4882a593Smuzhiyun 
365*4882a593Smuzhiyun     print_value(6, "[INTERLACE]", interlace);
366*4882a593Smuzhiyun 
367*4882a593Smuzhiyun     /*  7. Estimate the Horizontal period
368*4882a593Smuzhiyun      *
369*4882a593Smuzhiyun      *  [H PERIOD EST] = ((1/[V FIELD RATE RQD]) - [MIN VSYNC+BP]/1000000) /
370*4882a593Smuzhiyun      *                    ([V LINES RND] + (2*[TOP MARGIN (LINES)]) +
371*4882a593Smuzhiyun      *                     [MIN PORCH RND]+[INTERLACE]) * 1000000
372*4882a593Smuzhiyun      */
373*4882a593Smuzhiyun 
374*4882a593Smuzhiyun     h_period_est = (((1.0 / v_field_rate_rqd) - (MIN_VSYNC_PLUS_BP / 1000000.0))
375*4882a593Smuzhiyun                     / (v_lines_rnd + (2 * top_margin) + MIN_PORCH + interlace)
376*4882a593Smuzhiyun                     * 1000000.0);
377*4882a593Smuzhiyun 
378*4882a593Smuzhiyun     print_value(7, "[H PERIOD EST]", h_period_est);
379*4882a593Smuzhiyun 
380*4882a593Smuzhiyun     /*  8. Find the number of lines in V sync + back porch:
381*4882a593Smuzhiyun      *
382*4882a593Smuzhiyun      *  [V SYNC+BP] = ROUND(([MIN VSYNC+BP]/[H PERIOD EST]),0)
383*4882a593Smuzhiyun      */
384*4882a593Smuzhiyun 
385*4882a593Smuzhiyun     vsync_plus_bp = rint(MIN_VSYNC_PLUS_BP / h_period_est);
386*4882a593Smuzhiyun 
387*4882a593Smuzhiyun     print_value(8, "[V SYNC+BP]", vsync_plus_bp);
388*4882a593Smuzhiyun 
389*4882a593Smuzhiyun     /*  9. Find the number of lines in V back porch alone:
390*4882a593Smuzhiyun      *
391*4882a593Smuzhiyun      *  [V BACK PORCH] = [V SYNC+BP] - [V SYNC RND]
392*4882a593Smuzhiyun      *
393*4882a593Smuzhiyun      *  XXX is "[V SYNC RND]" a typo? should be [V SYNC RQD]?
394*4882a593Smuzhiyun      */
395*4882a593Smuzhiyun 
396*4882a593Smuzhiyun     v_back_porch = vsync_plus_bp - V_SYNC_RQD;
397*4882a593Smuzhiyun 
398*4882a593Smuzhiyun     print_value(9, "[V BACK PORCH]", v_back_porch);
399*4882a593Smuzhiyun 
400*4882a593Smuzhiyun     /*  10. Find the total number of lines in Vertical field period:
401*4882a593Smuzhiyun      *
402*4882a593Smuzhiyun      *  [TOTAL V LINES] = [V LINES RND] + [TOP MARGIN (LINES)] +
403*4882a593Smuzhiyun      *                    [BOT MARGIN (LINES)] + [V SYNC+BP] + [INTERLACE] +
404*4882a593Smuzhiyun      *                    [MIN PORCH RND]
405*4882a593Smuzhiyun      */
406*4882a593Smuzhiyun 
407*4882a593Smuzhiyun     total_v_lines = v_lines_rnd + top_margin + bottom_margin + vsync_plus_bp +
408*4882a593Smuzhiyun         interlace + MIN_PORCH;
409*4882a593Smuzhiyun 
410*4882a593Smuzhiyun     print_value(10, "[TOTAL V LINES]", total_v_lines);
411*4882a593Smuzhiyun 
412*4882a593Smuzhiyun     /*  11. Estimate the Vertical field frequency:
413*4882a593Smuzhiyun      *
414*4882a593Smuzhiyun      *  [V FIELD RATE EST] = 1 / [H PERIOD EST] / [TOTAL V LINES] * 1000000
415*4882a593Smuzhiyun      */
416*4882a593Smuzhiyun 
417*4882a593Smuzhiyun     v_field_rate_est = 1.0 / h_period_est / total_v_lines * 1000000.0;
418*4882a593Smuzhiyun 
419*4882a593Smuzhiyun     print_value(11, "[V FIELD RATE EST]", v_field_rate_est);
420*4882a593Smuzhiyun 
421*4882a593Smuzhiyun     /*  12. Find the actual horizontal period:
422*4882a593Smuzhiyun      *
423*4882a593Smuzhiyun      *  [H PERIOD] = [H PERIOD EST] / ([V FIELD RATE RQD] / [V FIELD RATE EST])
424*4882a593Smuzhiyun      */
425*4882a593Smuzhiyun 
426*4882a593Smuzhiyun     h_period = h_period_est / (v_field_rate_rqd / v_field_rate_est);
427*4882a593Smuzhiyun 
428*4882a593Smuzhiyun     print_value(12, "[H PERIOD]", h_period);
429*4882a593Smuzhiyun 
430*4882a593Smuzhiyun     /*  13. Find the actual Vertical field frequency:
431*4882a593Smuzhiyun      *
432*4882a593Smuzhiyun      *  [V FIELD RATE] = 1 / [H PERIOD] / [TOTAL V LINES] * 1000000
433*4882a593Smuzhiyun      */
434*4882a593Smuzhiyun 
435*4882a593Smuzhiyun     v_field_rate = 1.0 / h_period / total_v_lines * 1000000.0;
436*4882a593Smuzhiyun 
437*4882a593Smuzhiyun     print_value(13, "[V FIELD RATE]", v_field_rate);
438*4882a593Smuzhiyun 
439*4882a593Smuzhiyun     /*  14. Find the Vertical frame frequency:
440*4882a593Smuzhiyun      *
441*4882a593Smuzhiyun      *  [V FRAME RATE] = (IF([INT RQD?]="y", [V FIELD RATE]/2, [V FIELD RATE]))
442*4882a593Smuzhiyun      */
443*4882a593Smuzhiyun 
444*4882a593Smuzhiyun     v_frame_rate = interlaced ? v_field_rate / 2.0 : v_field_rate;
445*4882a593Smuzhiyun 
446*4882a593Smuzhiyun     print_value(14, "[V FRAME RATE]", v_frame_rate);
447*4882a593Smuzhiyun 
448*4882a593Smuzhiyun     /*  15. Find number of pixels in left margin:
449*4882a593Smuzhiyun      *
450*4882a593Smuzhiyun      *  [LEFT MARGIN (PIXELS)] = (IF( [MARGINS RQD?]="Y",
451*4882a593Smuzhiyun      *          (ROUND( ([H PIXELS RND] * [MARGIN%] / 100 /
452*4882a593Smuzhiyun      *                   [CELL GRAN RND]),0)) * [CELL GRAN RND],
453*4882a593Smuzhiyun      *          0))
454*4882a593Smuzhiyun      */
455*4882a593Smuzhiyun 
456*4882a593Smuzhiyun     left_margin = margins ?
457*4882a593Smuzhiyun         rint(h_pixels_rnd * MARGIN_PERCENT / 100.0 / CELL_GRAN) * CELL_GRAN :
458*4882a593Smuzhiyun         0.0;
459*4882a593Smuzhiyun 
460*4882a593Smuzhiyun     print_value(15, "[LEFT MARGIN (PIXELS)]", left_margin);
461*4882a593Smuzhiyun 
462*4882a593Smuzhiyun     /*  16. Find number of pixels in right margin:
463*4882a593Smuzhiyun      *
464*4882a593Smuzhiyun      *  [RIGHT MARGIN (PIXELS)] = (IF( [MARGINS RQD?]="Y",
465*4882a593Smuzhiyun      *          (ROUND( ([H PIXELS RND] * [MARGIN%] / 100 /
466*4882a593Smuzhiyun      *                   [CELL GRAN RND]),0)) * [CELL GRAN RND],
467*4882a593Smuzhiyun      *          0))
468*4882a593Smuzhiyun      */
469*4882a593Smuzhiyun 
470*4882a593Smuzhiyun     right_margin = margins ?
471*4882a593Smuzhiyun         rint(h_pixels_rnd * MARGIN_PERCENT / 100.0 / CELL_GRAN) * CELL_GRAN :
472*4882a593Smuzhiyun         0.0;
473*4882a593Smuzhiyun 
474*4882a593Smuzhiyun     print_value(16, "[RIGHT MARGIN (PIXELS)]", right_margin);
475*4882a593Smuzhiyun 
476*4882a593Smuzhiyun     /*  17. Find total number of active pixels in image and left and right
477*4882a593Smuzhiyun      *  margins:
478*4882a593Smuzhiyun      *
479*4882a593Smuzhiyun      *  [TOTAL ACTIVE PIXELS] = [H PIXELS RND] + [LEFT MARGIN (PIXELS)] +
480*4882a593Smuzhiyun      *                          [RIGHT MARGIN (PIXELS)]
481*4882a593Smuzhiyun      */
482*4882a593Smuzhiyun 
483*4882a593Smuzhiyun     total_active_pixels = h_pixels_rnd + left_margin + right_margin;
484*4882a593Smuzhiyun 
485*4882a593Smuzhiyun     print_value(17, "[TOTAL ACTIVE PIXELS]", total_active_pixels);
486*4882a593Smuzhiyun 
487*4882a593Smuzhiyun     /*  18. Find the ideal blanking duty cycle from the blanking duty cycle
488*4882a593Smuzhiyun      *  equation:
489*4882a593Smuzhiyun      *
490*4882a593Smuzhiyun      *  [IDEAL DUTY CYCLE] = [C'] - ([M']*[H PERIOD]/1000)
491*4882a593Smuzhiyun      */
492*4882a593Smuzhiyun 
493*4882a593Smuzhiyun     ideal_duty_cycle = C_PRIME - (M_PRIME * h_period / 1000.0);
494*4882a593Smuzhiyun 
495*4882a593Smuzhiyun     print_value(18, "[IDEAL DUTY CYCLE]", ideal_duty_cycle);
496*4882a593Smuzhiyun 
497*4882a593Smuzhiyun     /*  19. Find the number of pixels in the blanking time to the nearest
498*4882a593Smuzhiyun      *  double character cell:
499*4882a593Smuzhiyun      *
500*4882a593Smuzhiyun      *  [H BLANK (PIXELS)] = (ROUND(([TOTAL ACTIVE PIXELS] *
501*4882a593Smuzhiyun      *                               [IDEAL DUTY CYCLE] /
502*4882a593Smuzhiyun      *                               (100-[IDEAL DUTY CYCLE]) /
503*4882a593Smuzhiyun      *                               (2*[CELL GRAN RND])), 0))
504*4882a593Smuzhiyun      *                       * (2*[CELL GRAN RND])
505*4882a593Smuzhiyun      */
506*4882a593Smuzhiyun 
507*4882a593Smuzhiyun     h_blank = rint(total_active_pixels *
508*4882a593Smuzhiyun                    ideal_duty_cycle /
509*4882a593Smuzhiyun                    (100.0 - ideal_duty_cycle) /
510*4882a593Smuzhiyun                    (2.0 * CELL_GRAN)) * (2.0 * CELL_GRAN);
511*4882a593Smuzhiyun 
512*4882a593Smuzhiyun     print_value(19, "[H BLANK (PIXELS)]", h_blank);
513*4882a593Smuzhiyun 
514*4882a593Smuzhiyun     /*  20. Find total number of pixels:
515*4882a593Smuzhiyun      *
516*4882a593Smuzhiyun      *  [TOTAL PIXELS] = [TOTAL ACTIVE PIXELS] + [H BLANK (PIXELS)]
517*4882a593Smuzhiyun      */
518*4882a593Smuzhiyun 
519*4882a593Smuzhiyun     total_pixels = total_active_pixels + h_blank;
520*4882a593Smuzhiyun 
521*4882a593Smuzhiyun     print_value(20, "[TOTAL PIXELS]", total_pixels);
522*4882a593Smuzhiyun 
523*4882a593Smuzhiyun     /*  21. Find pixel clock frequency:
524*4882a593Smuzhiyun      *
525*4882a593Smuzhiyun      *  [PIXEL FREQ] = [TOTAL PIXELS] / [H PERIOD]
526*4882a593Smuzhiyun      */
527*4882a593Smuzhiyun 
528*4882a593Smuzhiyun     pixel_freq = total_pixels / h_period;
529*4882a593Smuzhiyun 
530*4882a593Smuzhiyun     print_value(21, "[PIXEL FREQ]", pixel_freq);
531*4882a593Smuzhiyun 
532*4882a593Smuzhiyun     /*  22. Find horizontal frequency:
533*4882a593Smuzhiyun      *
534*4882a593Smuzhiyun      *  [H FREQ] = 1000 / [H PERIOD]
535*4882a593Smuzhiyun      */
536*4882a593Smuzhiyun 
537*4882a593Smuzhiyun     h_freq = 1000.0 / h_period;
538*4882a593Smuzhiyun 
539*4882a593Smuzhiyun     print_value(22, "[H FREQ]", h_freq);
540*4882a593Smuzhiyun 
541*4882a593Smuzhiyun     /* Stage 1 computations are now complete; I should really pass
542*4882a593Smuzhiyun        the results to another function and do the Stage 2
543*4882a593Smuzhiyun        computations, but I only need a few more values so I'll just
544*4882a593Smuzhiyun        append the computations here for now */
545*4882a593Smuzhiyun 
546*4882a593Smuzhiyun     /*  17. Find the number of pixels in the horizontal sync period:
547*4882a593Smuzhiyun      *
548*4882a593Smuzhiyun      *  [H SYNC (PIXELS)] =(ROUND(([H SYNC%] / 100 * [TOTAL PIXELS] /
549*4882a593Smuzhiyun      *                             [CELL GRAN RND]),0))*[CELL GRAN RND]
550*4882a593Smuzhiyun      */
551*4882a593Smuzhiyun 
552*4882a593Smuzhiyun     h_sync =
553*4882a593Smuzhiyun         rint(H_SYNC_PERCENT / 100.0 * total_pixels / CELL_GRAN) * CELL_GRAN;
554*4882a593Smuzhiyun 
555*4882a593Smuzhiyun     print_value(17, "[H SYNC (PIXELS)]", h_sync);
556*4882a593Smuzhiyun 
557*4882a593Smuzhiyun     /*  18. Find the number of pixels in the horizontal front porch period:
558*4882a593Smuzhiyun      *
559*4882a593Smuzhiyun      *  [H FRONT PORCH (PIXELS)] = ([H BLANK (PIXELS)]/2)-[H SYNC (PIXELS)]
560*4882a593Smuzhiyun      */
561*4882a593Smuzhiyun 
562*4882a593Smuzhiyun     h_front_porch = (h_blank / 2.0) - h_sync;
563*4882a593Smuzhiyun 
564*4882a593Smuzhiyun     print_value(18, "[H FRONT PORCH (PIXELS)]", h_front_porch);
565*4882a593Smuzhiyun 
566*4882a593Smuzhiyun     /*  36. Find the number of lines in the odd front porch period:
567*4882a593Smuzhiyun      *
568*4882a593Smuzhiyun      *  [V ODD FRONT PORCH(LINES)]=([MIN PORCH RND]+[INTERLACE])
569*4882a593Smuzhiyun      */
570*4882a593Smuzhiyun 
571*4882a593Smuzhiyun     v_odd_front_porch_lines = MIN_PORCH + interlace;
572*4882a593Smuzhiyun 
573*4882a593Smuzhiyun     print_value(36, "[V ODD FRONT PORCH(LINES)]", v_odd_front_porch_lines);
574*4882a593Smuzhiyun 
575*4882a593Smuzhiyun     /* finally, pack the results in the mode struct */
576*4882a593Smuzhiyun 
577*4882a593Smuzhiyun     m->hr = (int) (h_pixels_rnd);
578*4882a593Smuzhiyun     m->hss = (int) (h_pixels_rnd + h_front_porch);
579*4882a593Smuzhiyun     m->hse = (int) (h_pixels_rnd + h_front_porch + h_sync);
580*4882a593Smuzhiyun     m->hfl = (int) (total_pixels);
581*4882a593Smuzhiyun 
582*4882a593Smuzhiyun     m->vr = (int) (v_lines_rnd);
583*4882a593Smuzhiyun     m->vss = (int) (v_lines_rnd + v_odd_front_porch_lines);
584*4882a593Smuzhiyun     m->vse = (int) (int) (v_lines_rnd + v_odd_front_porch_lines + V_SYNC_RQD);
585*4882a593Smuzhiyun     m->vfl = (int) (total_v_lines);
586*4882a593Smuzhiyun 
587*4882a593Smuzhiyun     m->pclk = pixel_freq;
588*4882a593Smuzhiyun     m->h_freq = h_freq;
589*4882a593Smuzhiyun     m->v_freq = freq;
590*4882a593Smuzhiyun 
591*4882a593Smuzhiyun     return m;
592*4882a593Smuzhiyun 
593*4882a593Smuzhiyun }
594*4882a593Smuzhiyun 
595*4882a593Smuzhiyun /*
596*4882a593Smuzhiyun  * parse_command_line() - parse the command line and return an
597*4882a593Smuzhiyun  * alloced structure containing the results.  On error print usage
598*4882a593Smuzhiyun  * and return NULL.
599*4882a593Smuzhiyun  */
600*4882a593Smuzhiyun 
601*4882a593Smuzhiyun options *
parse_command_line(int argc,char * argv[])602*4882a593Smuzhiyun parse_command_line(int argc, char *argv[])
603*4882a593Smuzhiyun {
604*4882a593Smuzhiyun     int n;
605*4882a593Smuzhiyun 
606*4882a593Smuzhiyun     options *o = (options *) calloc(1, sizeof(options));
607*4882a593Smuzhiyun 
608*4882a593Smuzhiyun     if (argc < 4)
609*4882a593Smuzhiyun         goto bad_option;
610*4882a593Smuzhiyun 
611*4882a593Smuzhiyun     o->x = atoi(argv[1]);
612*4882a593Smuzhiyun     o->y = atoi(argv[2]);
613*4882a593Smuzhiyun     o->v_freq = atof(argv[3]);
614*4882a593Smuzhiyun 
615*4882a593Smuzhiyun     /* XXX should check for errors in the above */
616*4882a593Smuzhiyun 
617*4882a593Smuzhiyun     n = 4;
618*4882a593Smuzhiyun 
619*4882a593Smuzhiyun     while (n < argc) {
620*4882a593Smuzhiyun         if ((strcmp(argv[n], "-v") == 0) || (strcmp(argv[n], "--verbose") == 0)) {
621*4882a593Smuzhiyun             global_verbose = 1;
622*4882a593Smuzhiyun         }
623*4882a593Smuzhiyun         else if ((strcmp(argv[n], "-f") == 0) ||
624*4882a593Smuzhiyun                  (strcmp(argv[n], "--fbmode") == 0)) {
625*4882a593Smuzhiyun             o->fbmode = 1;
626*4882a593Smuzhiyun         }
627*4882a593Smuzhiyun         else if ((strcmp(argv[n], "-x") == 0) ||
628*4882a593Smuzhiyun                  (strcmp(argv[n], "--xorgmode") == 0) ||
629*4882a593Smuzhiyun                  (strcmp(argv[n], "--xf86mode") == 0)) {
630*4882a593Smuzhiyun             o->xorgmode = 1;
631*4882a593Smuzhiyun         }
632*4882a593Smuzhiyun         else {
633*4882a593Smuzhiyun             goto bad_option;
634*4882a593Smuzhiyun         }
635*4882a593Smuzhiyun 
636*4882a593Smuzhiyun         n++;
637*4882a593Smuzhiyun     }
638*4882a593Smuzhiyun 
639*4882a593Smuzhiyun     /* if neither xorgmode nor fbmode were requested, default to
640*4882a593Smuzhiyun        xorgmode */
641*4882a593Smuzhiyun 
642*4882a593Smuzhiyun     if (!o->fbmode && !o->xorgmode)
643*4882a593Smuzhiyun         o->xorgmode = 1;
644*4882a593Smuzhiyun 
645*4882a593Smuzhiyun     return o;
646*4882a593Smuzhiyun 
647*4882a593Smuzhiyun  bad_option:
648*4882a593Smuzhiyun 
649*4882a593Smuzhiyun     fprintf(stderr, "\n");
650*4882a593Smuzhiyun     fprintf(stderr, "usage: %s x y refresh [-v|--verbose] "
651*4882a593Smuzhiyun             "[-f|--fbmode] [-x|--xorgmode]\n", argv[0]);
652*4882a593Smuzhiyun 
653*4882a593Smuzhiyun     fprintf(stderr, "\n");
654*4882a593Smuzhiyun 
655*4882a593Smuzhiyun     fprintf(stderr, "            x : the desired horizontal "
656*4882a593Smuzhiyun             "resolution (required)\n");
657*4882a593Smuzhiyun     fprintf(stderr, "            y : the desired vertical "
658*4882a593Smuzhiyun             "resolution (required)\n");
659*4882a593Smuzhiyun     fprintf(stderr, "      refresh : the desired refresh " "rate (required)\n");
660*4882a593Smuzhiyun     fprintf(stderr, " -v|--verbose : enable verbose printouts "
661*4882a593Smuzhiyun             "(traces each step of the computation)\n");
662*4882a593Smuzhiyun     fprintf(stderr, "  -f|--fbmode : output an fbset(8)-style mode "
663*4882a593Smuzhiyun             "description\n");
664*4882a593Smuzhiyun     fprintf(stderr, " -x|--xorgmode : output an " __XSERVERNAME__ "-style mode "
665*4882a593Smuzhiyun             "description (this is the default\n"
666*4882a593Smuzhiyun             "                if no mode description is requested)\n");
667*4882a593Smuzhiyun 
668*4882a593Smuzhiyun     fprintf(stderr, "\n");
669*4882a593Smuzhiyun 
670*4882a593Smuzhiyun     free(o);
671*4882a593Smuzhiyun     return NULL;
672*4882a593Smuzhiyun 
673*4882a593Smuzhiyun }
674*4882a593Smuzhiyun 
675*4882a593Smuzhiyun int
main(int argc,char * argv[])676*4882a593Smuzhiyun main(int argc, char *argv[])
677*4882a593Smuzhiyun {
678*4882a593Smuzhiyun     mode *m;
679*4882a593Smuzhiyun     options *o;
680*4882a593Smuzhiyun 
681*4882a593Smuzhiyun     o = parse_command_line(argc, argv);
682*4882a593Smuzhiyun     if (!o)
683*4882a593Smuzhiyun         exit(1);
684*4882a593Smuzhiyun 
685*4882a593Smuzhiyun     m = vert_refresh(o->x, o->y, o->v_freq, 0, 0);
686*4882a593Smuzhiyun     if (!m)
687*4882a593Smuzhiyun         exit(1);
688*4882a593Smuzhiyun 
689*4882a593Smuzhiyun     if (o->xorgmode)
690*4882a593Smuzhiyun         print_xf86_mode(m);
691*4882a593Smuzhiyun 
692*4882a593Smuzhiyun     if (o->fbmode)
693*4882a593Smuzhiyun         print_fb_mode(m);
694*4882a593Smuzhiyun 
695*4882a593Smuzhiyun     free(m);
696*4882a593Smuzhiyun 
697*4882a593Smuzhiyun     return 0;
698*4882a593Smuzhiyun 
699*4882a593Smuzhiyun }
700