├── README.md ├── cvt12.c └── makefile /README.md: -------------------------------------------------------------------------------- 1 | # cvt_modeline_calculator_12 2 | CVT (Coordinated Video Timings) modeline calculator with CVT v1.2 timings. 3 | 4 | This is a modified CVT modeline calculator based on [cvt](http://www.uruk.org/~erich/projects/cvt/) 5 | by erich@uruk.org, which is based on [GTF modeline calculator](http://gtf.sourceforge.net/) by Andy Ritger. 6 | 7 | This modified version adds support for CVT v1.2 (VESA-2013-3 v1.2). 8 | 9 | Changes: 10 | * Add support for CVT v1.2 reduced blanking timings. 11 | * Add support for 1000/1001 reducing of refresh rate for better movie support (ie 24hz * 1000 / 1001 = 23.976hz). 12 | * Add check for reduce blanking on CVT v1.1 for refresh rates not a multiple of 60hz. 13 | * Add checks for bad x y and refresh values. 14 | * Display 3 digits after decimal point on CLI output for the commented out part of the xf86 modeline. (ie 23.98hz -> 23.976hz) 15 | * Change xf86 modeline printout to be similar to [x.org](http://www.x.org/wiki/)'s cvt. 16 | * Fix some default timing values on v1.1 to be in line with the Vesa Public Standards CVT Generator. 17 | * Various minor changes. 18 | 19 | Download: 20 | 21 | `$ curl https://raw.githubusercontent.com/kevinlekiller/cvt_modeline_calculator_12/master/cvt12.c --output cvt12.c` 22 | 23 | Compilation: 24 | 25 | `$ gcc cvt12.c -O2 -o cvt12 -lm -Wall` 26 | 27 | License is in the source at the top. 28 | -------------------------------------------------------------------------------- /cvt12.c: -------------------------------------------------------------------------------- 1 | /* cvt12.c Generate mode timings using the CVT v1.2 or CVT v1.1 Timing Standards. 2 | * 3 | * gcc cvt12.c -O2 -o cvt12 -lm -Wall 4 | * 5 | * This is a modification of cvt.c on May 25, 2015 at https://github.com/kevinlekiller/cvt_timing_calculator_12 6 | * 7 | * Changes: 8 | * Add support for CVT v1.2 reduced blanking timings. 9 | * Add support for 1000/1001 reducing of refresh rate for better movie support (ie 24hz * 1000 / 1001 = 23.976hz). 10 | * Add check for reduce blanking on CVT v1.1 for refresh rates not a multiple of 60hz. 11 | * Add checks for bad x y and refresh values. 12 | * Display 3 digits after decimal point on CLI output for the commented out part of the xf86 modeline. (ie 23.98hz -> 23.976hz) 13 | * Fix some default timing values on v1.1 to be in line with the Vesa Public Standards CVT Generator. 14 | * Change xf86 modeline printout to be similar to [x.org](http://www.x.org/wiki/)'s cvt. 15 | * Various minor changes. 16 | * 17 | * For more information on CVT v1.2, please see: 18 | * http://www.vesa.org/vesa-standards/free-standards/ 19 | * 20 | * I've left some of the original cvt.c information: 21 | * 22 | *---------------------------------------------------------------- 23 | * cvt.c Generate mode timings using the CVT 1.1 Timing Standard 24 | * 25 | * http://www.uruk.org/~erich/projects/cvt/ 26 | * 27 | * Originally created from "gtf" written by Andy Ritger at NVidia. 28 | * Original block comment at the beginning of the program follows: 29 | * 30 | *--------------------------------------------------------------------- 31 | * gtf.c Generate mode timings using the GTF Timing Standard 32 | * 33 | * http://gtf.sourceforge.net/ 34 | * 35 | * Copyright (c) 2001, Andy Ritger aritger@nvidia.com 36 | * All rights reserved. 37 | * 38 | * Redistribution and use in source and binary forms, with or without 39 | * modification, are permitted provided that the following conditions 40 | * are met: 41 | * 42 | * o Redistributions of source code must retain the above copyright 43 | * notice, this list of conditions and the following disclaimer. 44 | * o Redistributions in binary form must reproduce the above copyright 45 | * notice, this list of conditions and the following disclaimer 46 | * in the documentation and/or other materials provided with the 47 | * distribution. 48 | * o Neither the name of NVIDIA nor the names of its contributors 49 | * may be used to endorse or promote products derived from this 50 | * software without specific prior written permission. 51 | * 52 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 53 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT 54 | * NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND 55 | * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL 56 | * THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 57 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 58 | * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 59 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 60 | * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 61 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN 62 | * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 63 | * POSSIBILITY OF SUCH DAMAGE. 64 | * 65 | * This program is based on the Generalized Timing Formula(GTF TM) 66 | * Standard Version: 1.0, Revision: 1.0 67 | * 68 | * The GTF Document contains the following Copyright information: 69 | * 70 | * Copyright (c) 1994, 1995, 1996 - Video Electronics Standards 71 | * Association. Duplication of this document within VESA member 72 | * companies for review purposes is permitted. All other rights 73 | * reserved. 74 | * 75 | * While every precaution has been taken in the preparation 76 | * of this standard, the Video Electronics Standards Association and 77 | * its contributors assume no responsibility for errors or omissions, 78 | * and make no warranties, expressed or implied, of functionality 79 | * of suitability for any purpose. The sample code contained within 80 | * this standard may be used without restriction. 81 | */ 82 | 83 | #include 84 | #include 85 | #include 86 | #include 87 | 88 | // These are CVT 1.1 default timings, the global variables are 89 | // changed below if we need to use CVT 1.2 timings. 90 | float CLOCK_STEP = 0.250; // Clock steps in MHz 91 | #define MARGIN_PERCENT 1.800 // % of active vertical image 92 | #define H_SYNC_PER 8.000 // sync % of horizontal image 93 | float CELL_GRAN_RND = 8.000; // assumed character cell granularity (round) 94 | float MIN_V_BPORCH = 6.000; // width of vsync in lines 95 | #define MIN_V_PORCH_RND 3.000 // width of vsync in lines 96 | #define M 600.000 // blanking formula gradient 97 | #define C 40.000 // blanking formula offset 98 | #define K 128.000 // blanking formula scaling factor 99 | #define J 20.000 // blanking formula scaling factor 100 | // Standard Timing Parameters 101 | #define MIN_VSYNC_BP 550.000 // min time of vsync + back porch (us) 102 | // Reduced Blanking defines 103 | #define RB_MIN_V_BPORCH 6.000 // lines 104 | float RB_V_FPORCH = 3.000; // lines 105 | #define RB_MIN_V_BLANK 460.000 // us 106 | #define RB_H_SYNC 32.000 // pixels 107 | float RB_H_BLANK = 160.000; // pixels 108 | // C' and M' are part of the Blanking Duty Cycle computation. 109 | #define C_PRIME (((C - J) * K/256.0) + J) 110 | #define M_PRIME (M * K / 256.0) 111 | 112 | // Struct definitions. 113 | typedef struct __mode { 114 | int hr, hss, hse, hfl; 115 | int vr, vss, vse, vfl; 116 | float pclk, h_freq, v_freq; 117 | float real_v_rate; 118 | int rb, in; 119 | } mode; 120 | 121 | typedef struct __options { 122 | int x, y; 123 | int reduced_blank_ver, film_optimized, interlaced, force_reduced_blank; 124 | int xf86mode, fbmode; 125 | float v_freq; 126 | } options; 127 | 128 | // Prototypes. 129 | void print_value(int n, char *name, float val); 130 | void print_xf86_mode (mode *m); 131 | void print_fb_mode (mode *m); 132 | mode *vert_refresh ( 133 | int h_pixels, int v_lines, float freq, 134 | int interlaced, int reduced_blank_ver, int margins, 135 | int film_optimized 136 | ); 137 | void set_global_timings_v1_2(); 138 | options *parse_command_line (int argc, char *argv[]); 139 | 140 | int global_verbose = 0; 141 | 142 | /** 143 | * Print the result of the named computation. 144 | * This is useful when comparing against the CVT EXCEL spreadsheet. 145 | */ 146 | void print_value(int n, char *name, float val) 147 | { 148 | if (global_verbose) { 149 | printf("%4d: %-25s: %15f\n", n, name, val); 150 | } 151 | } 152 | 153 | /** 154 | * Print the XFree86 modeline, given mode timings. 155 | */ 156 | void print_xf86_mode (mode *m) 157 | { 158 | printf ( 159 | "# %dx%d @ %.3f Hz %s%s(CVT)" 160 | " field rate %.3f Hz; hsync: %.3f kHz; pclk: %.2f MHz\n" 161 | "Modeline \"%dx%d_%.2f%s%s%.0d\" %.2f" 162 | " %d %d %d %d" 163 | " %d %d %d %d" 164 | " %s%chsync %cvsync\n", 165 | m->hr, m->vr, m->v_freq, (m->in ? "Interlaced " : ""), (m->rb ? "Reduced Blank " : ""), 166 | m->real_v_rate, m->h_freq, m->pclk, 167 | m->hr, m->vr, m->v_freq, (m->in ? "i" : ""), (m->rb ? "_rb" : ""), m->rb, m->pclk, 168 | m->hr, m->hss, m->hse, m->hfl, 169 | m->vr, m->vss, m->vse, m->vfl, 170 | (m->in?"Interlace ":""), (m->rb?'+':'-'), (m->rb?'-':'+') 171 | ); 172 | } 173 | 174 | /** 175 | * Print a mode description in fbset(8) format. 176 | * See the fb.modes(8) manpage. 177 | * The timing description used in this is rather odd, 178 | * they use "left and right margin" to refer 179 | * to the portion of the hblank before and after the sync pulse 180 | * by conceptually wrapping the portion of the blank after the pulse 181 | * to infront of the visible region; ie: 182 | * 183 | * Timing description I'm accustomed to: 184 | * 185 | * <--------1--------> <--2--> <--3--> <--4--> 186 | * _________ 187 | * |-------------------|_______| |_______ 188 | * 189 | * R SS SE FL 190 | * 191 | * 1: visible image 192 | * 2: blank before sync (aka front porch) 193 | * 3: sync pulse 194 | * 4: blank after sync (aka back porch) 195 | * R: Resolution 196 | * SS: Sync Start 197 | * SE: Sync End 198 | * FL: Frame Length 199 | * 200 | * But the fb.modes format is: 201 | * 202 | * <--4--> <--------1--------> <--2--> <--3--> 203 | * _________ 204 | * _______|-------------------|_______| | 205 | * 206 | * The fb.modes(8) manpage refers to <4> and <2> as the left and 207 | * right "margin" (as well as upper and lower margin in the vertical 208 | * direction) -- note that this has nothing to do with the term 209 | * "margin" used in the CVT Timing Standard. 210 | * 211 | * XXX always prints the 32 bit mode -- should I provide a command 212 | * line option to specify the bpp? It's simple enough for a user 213 | * to edit the mode description after it's generated. 214 | */ 215 | void print_fb_mode (mode *m) 216 | { 217 | printf ( 218 | "\nmode \"%dx%d %.2fHz %s%s%.0d%s32bit (CVT)\"\n" 219 | " # PCLK: %.2f MHz, H: %.2f kHz, V: %.2f Hz\n" 220 | " geometry %d %d %d %d 32\n", 221 | m->hr, m->vr, m->v_freq, (m->in ? "INT " : ""), (m->rb ? "RBlank" : ""), (m->rb), (m->rb ? " " : ""), 222 | m->pclk, m->h_freq, m->real_v_rate, 223 | m->hr, m->vr, m->hr, m->vr 224 | ); 225 | printf ( 226 | " timings %d %d %d %d %d %d %d\n", 227 | (int) rint(1000000.0/m->pclk), // pixclock in picoseconds 228 | m->hfl - m->hse, // left margin (in pixels) 229 | m->hss - m->hr, // right margin (in pixels) 230 | m->vfl - m->vse, // upper margin (in pixel lines) 231 | m->vss - m->vr, // lower margin (in pixel lines) 232 | m->hse - m->hss, // horizontal sync length (in pixels) 233 | m->vse - m->vss // vert sync length (in pixel lines) 234 | ); 235 | printf ( 236 | " hsync %s\n" 237 | " vsync %s\n", 238 | (m->rb ? "high" : "low"), 239 | (m->rb ? "low" : "high") 240 | ); 241 | if (m->in) { 242 | printf(" laced true\n"); 243 | } 244 | printf ("endmode\n\n"); 245 | } 246 | 247 | /* 248 | * As defined by the CVT Timing Standard, compute the Stage 1 Parameters 249 | * using the vertical refresh frequency. 250 | * In other words: input a desired resolution and desired refresh rate, and 251 | * output the CVT mode timings. 252 | * 253 | * XXX margin computations are implemented but not tested (nor used by 254 | * XFree86 of fbset mode descriptions, from what I can tell). 255 | */ 256 | mode *vert_refresh (int h_pixels, int v_lines, float freq, int interlaced, int reduced_blank_ver, int margins, int film_optimized) 257 | { 258 | float h_pixels_rnd, v_lines_rnd; 259 | float v_field_rate_rqd; 260 | float top_margin, bot_margin; 261 | float interlace; 262 | float h_period_est; 263 | float v_sync_bp; 264 | float left_margin, right_margin; 265 | float total_active_pixels; 266 | float ideal_duty_cycle, cur_duty_cycle; 267 | float v_sync, h_blank; 268 | float total_pixels, total_v_lines; 269 | float v_sync_rnd, h_sync_rnd; 270 | float h_back_porch, v_front_porch, h_front_porch; 271 | float act_vbi_lines = 0, vbi_lines, rb_min_vbi; 272 | float pixel_clock_factor; 273 | float act_pixel_freq, act_h_freq; 274 | float act_field_rate, act_frame_rate; 275 | char *aspect_ratio; 276 | int stage; 277 | 278 | mode *m = (mode*) malloc (sizeof (mode)); 279 | 280 | /* 1. Required Field Rate 281 | * 282 | * This is slightly different from the spreadsheet because we use 283 | * a different result for interlaced video modes. Simplifies this 284 | * to the input field rate. 285 | * 286 | * [V FIELD RATE RQD] = [I/P FREQ RQD] 287 | */ 288 | v_field_rate_rqd = freq; 289 | print_value(1, "[V FIELD RATE RQD]", v_field_rate_rqd); 290 | 291 | /* 2. Horizontal Pixels 292 | * 293 | * In order to give correct results, the number of horizontal 294 | * pixels requested is first processed to ensure that it is divisible 295 | * by the character size, by rounding it to the nearest character 296 | * cell boundary. 297 | * 298 | * [H PIXELS RND] = ((ROUNDDOWN([H PIXELS]/[CELL GRAN RND],0)) 299 | * *[CELLGRAN RND]) 300 | */ 301 | h_pixels_rnd = floor((float) h_pixels / CELL_GRAN_RND) * CELL_GRAN_RND; 302 | print_value(2, "[H PIXELS RND]", h_pixels_rnd); 303 | 304 | /* 2.5th Calculation, aspect_ratio & v_sync_rnd 305 | * 306 | * [ASPECT_RATIO] = IF(H_PIXELS_RND = CELL_GRAN_RND*ROUND((V_LINES* 307 | * 4.0/3.0)/CELL_GRAN_RND),"4:3") 308 | * etc... 309 | * [V_SYNC] = [value from table based on aspect ratio] 310 | * [V_SYNC_RND] = ROUND(V_SYNC,0) // Not needed in principle 311 | */ 312 | if (h_pixels_rnd == CELL_GRAN_RND * floor(((float)v_lines * 4.0 / 3.0) / CELL_GRAN_RND)) { 313 | aspect_ratio = "4:3"; 314 | v_sync = 4; 315 | } else if (h_pixels_rnd == CELL_GRAN_RND * floor(((float)v_lines * 16.0 / 9.0) / CELL_GRAN_RND)) { 316 | aspect_ratio = "16:9"; 317 | v_sync = 5; 318 | } else if (h_pixels_rnd == CELL_GRAN_RND * floor(((float)v_lines * 16.0 / 10.0) / CELL_GRAN_RND)) { 319 | aspect_ratio = "16:10"; 320 | v_sync = 6; 321 | } else if (h_pixels_rnd == CELL_GRAN_RND * floor(((float)v_lines * 5.0 / 4.0) / CELL_GRAN_RND)) { 322 | aspect_ratio = "5:4"; 323 | v_sync = 7; 324 | } else if (h_pixels_rnd == CELL_GRAN_RND * floor(((float)v_lines * 15.0 / 9.0) / CELL_GRAN_RND)) { 325 | aspect_ratio = "15:9"; 326 | v_sync = 7; 327 | } else { 328 | // Default case of unknown aspect ratio. 329 | aspect_ratio = "Custom"; 330 | v_sync = 10; 331 | } 332 | 333 | // CVT 1.2 Reduced Blanking v2 always uses a v_sync of 8 lines for all aspect ratios 334 | if (reduced_blank_ver == 2) { 335 | v_sync = 8; 336 | } 337 | 338 | v_sync_rnd = v_sync; 339 | 340 | if (global_verbose) { 341 | printf( 342 | " 2.5: [ASPECT_RATIO] : %10s\n" 343 | " 2.5: [V SYNC] : %15f\n", 344 | aspect_ratio, 345 | v_sync_rnd 346 | ); 347 | } 348 | 349 | /* 350 | * 3. Determine Left & Right Borders 351 | * 352 | * Calculate the margins on the left and right side. 353 | * 354 | * [LEFT MARGIN (PIXELS)] = (IF( [MARGINS RQD?]="Y", 355 | * (ROUNDDOWN( ([H PIXELS RND] * [MARGIN%] / 100 / 356 | * [CELL GRAN RND]),0)) * [CELL GRAN RND], 357 | * 0)) 358 | * [RIGHT MARGIN (PIXELS)] = (IF( [MARGINS RQD?]="Y", 359 | * (ROUNDDOWN( ([H PIXELS RND] * [MARGIN%] / 100 / 360 | * [CELL GRAN RND]),0)) * [CELL GRAN RND], 361 | * 0)) 362 | */ 363 | left_margin = margins ? floor(h_pixels_rnd * MARGIN_PERCENT / 100.0 / CELL_GRAN_RND) * CELL_GRAN_RND : 0.0; 364 | right_margin = left_margin; 365 | print_value(3, "[LEFT MARGIN (PIXELS)]", left_margin); 366 | print_value(3, "[RIGHT MARGIN (PIXELS)]", right_margin); 367 | 368 | /* 4. Find total active pixels. 369 | * 370 | * Find total number of active pixels in image and left and right 371 | * margins. 372 | * 373 | * [TOTAL ACTIVE PIXELS] = [H PIXELS RND] + [LEFT MARGIN (PIXELS)] + 374 | * [RIGHT MARGIN (PIXELS)] 375 | */ 376 | total_active_pixels = h_pixels_rnd + left_margin + right_margin; 377 | print_value(4, "[TOTAL ACTIVE PIXELS]", total_active_pixels); 378 | 379 | /* 5. Find number of lines per field. 380 | * 381 | * If interlace is requested, the number of vertical lines assumed 382 | * by the calculation must be halved, as the computation calculates 383 | * the number of vertical lines per field. In either case, the 384 | * number of lines is rounded to the nearest integer. 385 | * 386 | * [V LINES RND] = IF([INT RQD?]="y", ROUNDDOWN([V LINES]/2,0), 387 | * ROUNDDOWN([V LINES],0)) 388 | */ 389 | v_lines_rnd = interlaced ? floor((float) v_lines / 2.0) : floor((float) v_lines); 390 | print_value(5, "[V LINES RND]", v_lines_rnd); 391 | 392 | /* 6. Find Top and Bottom margins. 393 | * 394 | * [TOP MARGIN (LINES)] = IF([MARGINS RQD?]="Y", 395 | * ROUNDDOWN(([MARGIN%]/100*[V LINES RND]),0), 396 | * 0) 397 | * [BOT MARGIN (LINES)] = IF([MARGINS RQD?]="Y", 398 | * ROUNDDOWN(([MARGIN%]/100*[V LINES RND]),0), 399 | * 0) 400 | */ 401 | top_margin = margins ? floor(MARGIN_PERCENT / 100.0 * v_lines_rnd) : (0.0); 402 | bot_margin = top_margin; 403 | print_value(6, "[TOP MARGIN (LINES)]", top_margin); 404 | print_value(6, "[BOT MARGIN (LINES)]", bot_margin); 405 | 406 | /* 7. If interlace is required, then set variable [INTERLACE]=0.5: 407 | * 408 | * [INTERLACE]=(IF([INT RQD?]="y",0.5,0)) 409 | */ 410 | interlace = interlaced ? 0.5 : 0.0; 411 | print_value(7, "[INTERLACE]", interlace); 412 | 413 | /* 414 | * Here it diverges for "reduced blanking" or normal blanking modes. 415 | */ 416 | if (reduced_blank_ver != 0) { 417 | h_blank = RB_H_BLANK; 418 | 419 | /* 8. Estimate Horiz. Period (us). 420 | * 421 | * [H PERIOD EST] = ((1000000/V_FIELD_RATE_RQD)-RB_MIN_V_BLANK)/(V_LINES_RND+TOP_MARGIN+BOT_MARGIN) 422 | */ 423 | h_period_est = (1000000.0/v_field_rate_rqd - RB_MIN_V_BLANK) / (v_lines_rnd + top_margin + bot_margin); 424 | print_value(8, "[H PERIOD EST]", h_period_est); 425 | 426 | /* 9. Find number of lines in vertical blanking. 427 | * 428 | * [Actual VBI_LINES] = RB_MIN_V_BLANK/H_PERIOD_EST 429 | * [VBI_LINES] = ROUNDDOWN(RB_MIN_V_BLANK/H_PERIOD_EST,0) + 1 430 | */ 431 | vbi_lines = RB_MIN_V_BLANK/h_period_est; 432 | print_value(9, "[Actual VBI LINES]", vbi_lines); 433 | 434 | vbi_lines = floor(vbi_lines) + 1.0; 435 | print_value(9, "[VBI LINES]", vbi_lines); 436 | 437 | /* 10. Check Vertical Blanking is sufficient. 438 | * 439 | * [RB MIN VBI] = RB_V_FPORCH+V_SYNC_RND+RB_MIN_V_BPORCH 440 | * [ACT VBI LINES] = IF(VBI_LINEShr = (int) (h_pixels_rnd); 628 | m->hss = (int) (h_pixels_rnd + h_front_porch); 629 | m->hse = (int) (h_pixels_rnd + h_front_porch + h_sync_rnd); 630 | m->hfl = (int) (total_pixels); 631 | 632 | int real_v_lines = v_lines; 633 | m->vr = (real_v_lines); 634 | m->vss = (int) (real_v_lines + v_front_porch); 635 | m->vse = (int) (real_v_lines + v_front_porch + v_sync_rnd); 636 | m->vfl = (int) (total_v_lines - v_lines_rnd + real_v_lines); 637 | 638 | m->pclk = act_pixel_freq; 639 | m->h_freq = act_h_freq; 640 | m->v_freq = freq; 641 | m->real_v_rate = act_field_rate; 642 | 643 | m->in = interlaced; 644 | m->rb = reduced_blank_ver; 645 | 646 | return (m); 647 | } 648 | 649 | /** 650 | * Parse the command line and return an 651 | * alloced structure containing the results. On error print usage 652 | * and return NULL. 653 | */ 654 | options *parse_command_line (int argc, char *argv[]) 655 | { 656 | options *o = (options *) calloc (1, sizeof (options)); 657 | 658 | int n = 3; 659 | if (argc < n+1) { 660 | goto bad_option; 661 | } 662 | 663 | o->x = atoi (argv[1]); 664 | o->y = atoi (argv[2]); 665 | o->v_freq = atof (argv[3]); 666 | if (!o->x || o->x <= 0 || !o->y || o->y <= 0 || !o->v_freq || o->v_freq <= 0.0) { 667 | goto bad_option; 668 | } 669 | 670 | o->force_reduced_blank = 0; 671 | while (++n < argc) { 672 | if ((strcmp (argv[n], "-v") == 0) || (strcmp (argv[n], "--verbose") == 0)) { 673 | global_verbose = 1; 674 | } else if ((strcmp (argv[n], "-r") == 0) || (strcmp (argv[n], "--reduced-blank") == 0)) { 675 | o->reduced_blank_ver = 1; 676 | } else if ((strcmp (argv[n], "-i") == 0) || (strcmp (argv[n], "--interlaced") == 0)) { 677 | o->interlaced = 1; 678 | } else if ((strcmp (argv[n], "-f") == 0) || (strcmp (argv[n], "--fbmode") == 0)) { 679 | o->fbmode = 1; 680 | } else if ((strcmp (argv[n], "-x") == 0) || (strcmp (argv[n], "--xf86mode") == 0)) { 681 | o->xf86mode = 1; 682 | } else if ((strcmp (argv[n], "-b") == 0) || (strcmp (argv[n], "--rb-v2") == 0)) { 683 | o->reduced_blank_ver = 2; 684 | } else if ((strcmp (argv[n], "-o") == 0) || (strcmp (argv[n], "--film-optimized") == 0)) { 685 | o->film_optimized = 1; 686 | } else if ((strcmp (argv[n], "-c") == 0) || (strcmp (argv[n], "--force-rb") == 0)) { 687 | o->reduced_blank_ver = 1; 688 | o->force_reduced_blank = 1; 689 | } else { 690 | goto bad_option; 691 | } 692 | } 693 | 694 | // If CVT 1.1 and using reduced blanking, check if vertical frequency is multiple of 60hz. 695 | if (o->reduced_blank_ver == 1) { 696 | if (o->force_reduced_blank) { 697 | fprintf( 698 | stderr, 699 | "WARNING: Refresh rate must be multiple of 60hz according to CVT 1.1 specifications for reduced blanking.\n" 700 | "WARNING: Forcing calculations might give incorrect results. Use at your own risk.\n\n" 701 | ); 702 | } else if (o->v_freq > 60.0 && fmodf(o->v_freq, (float)60) != 0) { 703 | goto bad_vrefresh; 704 | } else if (o->v_freq < 60.0 && fmodf((float)60, o->v_freq) != 0) { 705 | goto bad_vrefresh; 706 | } 707 | } 708 | 709 | // If -b is not passed, disable -o 710 | if (o->reduced_blank_ver != 2) { 711 | o-> film_optimized = 0; 712 | } 713 | 714 | // If neither xf86mode nor fbmode were requested, default to xf86mode. 715 | if (!o->fbmode && !o->xf86mode) { 716 | o->xf86mode = 1; 717 | } 718 | 719 | // If neither of the reduced blanking versions were requested, disable it 720 | if (!(o->reduced_blank_ver == 1 || o->reduced_blank_ver == 2)) { 721 | o->reduced_blank_ver = 0; 722 | } 723 | 724 | return (o); 725 | 726 | bad_vrefresh: 727 | 728 | fprintf( 729 | stderr, 730 | "ERROR: Refresh rate must be multiple of 60hz according to CVT 1.1 specifications for reduced blanking.\n" 731 | ); 732 | free(o); 733 | return(NULL); 734 | 735 | bad_option: 736 | 737 | fprintf ( 738 | stderr, 739 | "Description: This program generates video timing descriptions using formulas\n" 740 | " from the VESA \"CVT\" (Coordinated Video Timing) v1.1 specification, based\n" 741 | " itself on the earlier VESA \"GTF\" (Generalized Timing Formula) v1.0\n specification.\n\n" 742 | "usage: %s x y refresh [-v|--verbose]\n" 743 | " [-r|--reduced-blank] [-i|--interlaced]\n" 744 | " [-f|--fbmode] [-x|-xf86mode]\n" 745 | " [-b|--rb-v2] [-o|--film-optimized]\n\n" 746 | " x : The desired horizontal resolution (required).\n" 747 | " y : The desired vertical resolution (required).\n" 748 | " refresh : The desired refresh rate (required).\n\n" 749 | " -v|--verbose : Enable verbose printouts (traces each step of the computation).\n" 750 | " -r|--reduced-blank : Use CVT 1.1 \"Reduced Blanking\" timings\n" 751 | " Only allows multiple of 60hz.\n" 752 | " -c|--force-rb : Force CVT 1.1 \"Reduced Blanking\" timings\n" 753 | " Do not check for multiple of 60hz.\n" 754 | " -b|--rb-v2 : Use CVT 1.2 \"Reduced Blanking\" timings, this is more precise.\n" 755 | " Allows any refresh rate.\n" 756 | " -o|--film-optimized : Change refresh rate for better video support. Requires -b\n" 757 | " (ie. converts 24hz to 23.976hz)\n" 758 | " This is the same as passing 23.976 without -o,\n" 759 | " -o does the calculation for you.\n" 760 | " -i|--interlaced : Generate an interlaced video mode.\n" 761 | " -f|--fbmode : Output an fbset(8)-style mode description.\n" 762 | " -x|-xf86mode : output an XFree86-style mode description (this is the default\n" 763 | " if no mode description is requested).\n", 764 | argv[0] 765 | ); 766 | 767 | free (o); 768 | return (NULL); 769 | } 770 | 771 | /** 772 | * Set global timings to CVT v1.2 773 | */ 774 | void set_global_timings_v1_2() 775 | { 776 | CLOCK_STEP = 0.001; // Clock steps in MHz 777 | CELL_GRAN_RND = 1.000; // assumed character cell granularity (round) 778 | MIN_V_BPORCH = 6.000; // width of vsync in lines 779 | RB_V_FPORCH = 1.000; // lines 780 | RB_H_BLANK = 80.000; // pixels 781 | } 782 | 783 | int main (int argc, char *argv[]) 784 | { 785 | mode *m; 786 | options *o; 787 | 788 | o = parse_command_line (argc, argv); 789 | if (!o) { 790 | exit (1); 791 | } 792 | 793 | if (o->reduced_blank_ver == 2) { 794 | set_global_timings_v1_2(); 795 | } 796 | 797 | m = vert_refresh (o->x, o->y, o->v_freq, o->interlaced, o->reduced_blank_ver, 0, o->film_optimized); 798 | if (!m) { 799 | exit (2); 800 | } 801 | 802 | if (o->xf86mode) { 803 | print_xf86_mode(m); 804 | } 805 | 806 | if (o->fbmode) { 807 | print_fb_mode(m); 808 | } 809 | 810 | return 0; 811 | } 812 | -------------------------------------------------------------------------------- /makefile: -------------------------------------------------------------------------------- 1 | CC=gcc 2 | CFLAGS=-O2 -Wall 3 | cvt12: cvt12.c 4 | $(CC) $(CFLAGS) cvt12.c -o cvt12 -lm 5 | --------------------------------------------------------------------------------