FreeBSD/sys/dev/videomode/vesagtf.c
/*
 * vert_refresh() - as defined by the GTF Timing Standard, compute the
 * Stage 1 Parameters using the vertical refresh frequency.  In other
 * words: input a desired resolution and desired refresh rate, and
 * output the GTF mode timings.
 *
 * XXX All the code is in place to compute interlaced modes, but I don't
 * feel like testing it right now.
 *
 * XXX margin computations are implemented but not tested (nor used by
 * XFree86 of fbset mode descriptions, from what I can tell).
 */
as defined by the GTF Timing Standard, compute the
Stage 1 Parameters using the vertical refresh frequency.  In other
words: input a desired resolution and desired refresh rate, and
output the GTF mode timings.

XXX All the code is in place to compute interlaced modes, but I don't
feel like testing it right now.

XXX margin computations are implemented but not tested (nor used by
XFree86 of fbset mode descriptions, from what I can tell).
as defined by the GTF Timing Standard compute the Stage 1 Parameters using the vertical refresh frequency . In other words : input a desired resolution and desired refresh rate and output the GTF mode timings . XXX All the code is in place to compute interlaced modes but I don ' t feel like testing it right now . XXX margin computations are implemented but not tested ( nor used by XFree86 of fbset mode descriptions from what I can tell ) .
void
vesagtf_mode_params(unsigned h_pixels, unsigned v_lines, unsigned freq,
    struct vesagtf_params *params, int flags, struct videomode *vmp)
{
    unsigned v_field_rqd;
    unsigned top_margin;
    unsigned bottom_margin;
    unsigned interlace;
    uint64_t h_period_est;
    unsigned vsync_plus_bp;
    unsigned v_back_porch __unused;
    unsigned total_v_lines;
    uint64_t v_field_est;
    uint64_t h_period;
    unsigned v_field_rate;
    unsigned v_frame_rate __unused;
    unsigned left_margin;
    unsigned right_margin;
    unsigned total_active_pixels;
    uint64_t ideal_duty_cycle;
    unsigned h_blank;
    unsigned total_pixels;
    unsigned pixel_freq;
    unsigned h_sync;
    unsigned h_front_porch;
    unsigned v_odd_front_porch_lines;
#ifdef	GTFDEBUG
    unsigned h_freq;
#endif
    h_pixels = DIVIDE(h_pixels, CELL_GRAN) * CELL_GRAN;
    print_value(1, "[H PIXELS RND]", h_pixels);
    v_lines = (flags & VESAGTF_FLAG_ILACE) ? DIVIDE(v_lines, 2) : v_lines;
    print_value(2, "[V LINES RND]", v_lines);
    v_field_rqd = (flags & VESAGTF_FLAG_ILACE) ? (freq * 2) : (freq);
    print_value(3, "[V FIELD RATE RQD]", v_field_rqd);
    top_margin = bottom_margin = (flags & VESAGTF_FLAG_MARGINS) ?
	DIVIDE(v_lines * params->margin_ppt, 1000) : 0;
    print_value(4, "[TOP MARGIN (LINES)]", top_margin);
    print_value(5, "[BOT MARGIN (LINES)]", bottom_margin);
    interlace = (flags & VESAGTF_FLAG_ILACE) ? 1 : 0;
    print_value(6, "[2*INTERLACE]", interlace);
    h_period_est = DIVIDE(((DIVIDE(2000000000000ULL, v_field_rqd)) -
			      (2000000 * params->min_vsbp)),
	((2 * (v_lines + (2 * top_margin) + params->min_porch)) + interlace));
    print_value(7, "[H PERIOD EST (ps)]", h_period_est);
    vsync_plus_bp = DIVIDE(params->min_vsbp * 1000000, h_period_est);
    print_value(8, "[V SYNC+BP]", vsync_plus_bp);
    v_back_porch = vsync_plus_bp - params->vsync_rqd;
    print_value(9, "[V BACK PORCH]", v_back_porch);
    total_v_lines = v_lines + top_margin + bottom_margin + vsync_plus_bp +
        interlace + params->min_porch;
    print_value(10, "[TOTAL V LINES]", total_v_lines);
    v_field_est = DIVIDE(DIVIDE(1000000000000000ULL, h_period_est),
	total_v_lines);
    print_value(11, "[V FIELD RATE EST(uHz)]", v_field_est);
    h_period = DIVIDE(h_period_est * v_field_est, v_field_rqd * 1000);
    print_value(12, "[H PERIOD(ps)]", h_period);
    v_field_rate = DIVIDE(1000000000000ULL, h_period * total_v_lines);
    print_value(13, "[V FIELD RATE]", v_field_rate);
    v_frame_rate = (flags & VESAGTF_FLAG_ILACE) ?
	v_field_rate / 2 : v_field_rate;
    print_value(14, "[V FRAME RATE]", v_frame_rate);
    left_margin = right_margin = (flags & VESAGTF_FLAG_MARGINS) ?
	DIVIDE(DIVIDE(h_pixels * params->margin_ppt, 1000),
	    CELL_GRAN) * CELL_GRAN : 0;
    print_value(15, "[LEFT MARGIN (PIXELS)]", left_margin);
    print_value(16, "[RIGHT MARGIN (PIXELS)]", right_margin);
    total_active_pixels = h_pixels + left_margin + right_margin;
    print_value(17, "[TOTAL ACTIVE PIXELS]", total_active_pixels);
    ideal_duty_cycle =
	((C_PRIME256(params) * 1000) -
	    (M_PRIME256(params) * h_period / 1000000));
    print_value(18, "[IDEAL DUTY CYCLE]", ideal_duty_cycle);
    h_blank = DIVIDE(DIVIDE(total_active_pixels * ideal_duty_cycle,
			 (256000 * 100ULL) - ideal_duty_cycle),
	2 * CELL_GRAN) * (2 * CELL_GRAN);
    print_value(19, "[H BLANK (PIXELS)]", h_blank);
    total_pixels = total_active_pixels + h_blank;
    print_value(20, "[TOTAL PIXELS]", total_pixels);
    pixel_freq = DIVIDE(total_pixels * 1000000, DIVIDE(h_period, 1000));
    print_value(21, "[PIXEL FREQ]", pixel_freq);
#ifdef	GTFDEBUG
    h_freq = 1000000000 / h_period;
    print_value(22, "[H FREQ]", h_freq);
#endif
    h_sync = DIVIDE(((params->hsync_pct * total_pixels) / 100), CELL_GRAN) *
	CELL_GRAN;
    print_value(17, "[H SYNC (PIXELS)]", h_sync);
    h_front_porch = (h_blank / 2) - h_sync;
    print_value(18, "[H FRONT PORCH (PIXELS)]", h_front_porch);
    v_odd_front_porch_lines = ((2 * params->min_porch) + interlace) / 2;
    print_value(36, "[V ODD FRONT PORCH(LINES)]", v_odd_front_porch_lines);
    vmp->hsync_start = h_pixels + h_front_porch;
    vmp->hsync_end = vmp->hsync_start + h_sync;
    vmp->htotal = total_pixels;
    vmp->hdisplay = h_pixels;
    vmp->vsync_start = v_lines + v_odd_front_porch_lines;
    vmp->vsync_end = vmp->vsync_start + params->vsync_rqd;
    vmp->vtotal = total_v_lines;
    vmp->vdisplay = v_lines;
    vmp->dot_clock = pixel_freq;
}
void vesagtf_mode_params ( unsigned h_pixels , unsigned v_lines , unsigned freq , struct vesagtf_params * params , int flags , struct videomode * vmp ) { unsigned v_field_rqd ; unsigned top_margin ; unsigned bottom_margin ; unsigned interlace ; uint64_t h_period_est ; unsigned vsync_plus_bp ; unsigned v_back_porch __unused ; unsigned total_v_lines ; uint64_t v_field_est ; uint64_t h_period ; unsigned v_field_rate ; unsigned v_frame_rate __unused ; unsigned left_margin ; unsigned right_margin ; unsigned total_active_pixels ; uint64_t ideal_duty_cycle ; unsigned h_blank ; unsigned total_pixels ; unsigned pixel_freq ; unsigned h_sync ; unsigned h_front_porch ; unsigned v_odd_front_porch_lines ; #ifdef GTFDEBUG unsigned h_freq ; #endif h_pixels = DIVIDE ( h_pixels , CELL_GRAN ) * CELL_GRAN ; print_value ( 1 , " " , h_pixels ) ; v_lines = ( flags & VESAGTF_FLAG_ILACE ) ? DIVIDE ( v_lines , 2 ) : v_lines ; print_value ( 2 , " " , v_lines ) ; v_field_rqd = ( flags & VESAGTF_FLAG_ILACE ) ? ( freq * 2 ) : ( freq ) ; print_value ( 3 , " " , v_field_rqd ) ; top_margin = bottom_margin = ( flags & VESAGTF_FLAG_MARGINS ) ? DIVIDE ( v_lines * params -> margin_ppt , 1000 ) : 0 ; print_value ( 4 , " " , top_margin ) ; print_value ( 5 , " " , bottom_margin ) ; interlace = ( flags & VESAGTF_FLAG_ILACE ) ? 1 : 0 ; print_value ( 6 , " " , interlace ) ; h_period_est = DIVIDE ( ( ( DIVIDE ( 2000000000000ULL , v_field_rqd ) ) - ( 2000000 * params -> min_vsbp ) ) , ( ( 2 * ( v_lines + ( 2 * top_margin ) + params -> min_porch ) ) + interlace ) ) ; print_value ( 7 , " " , h_period_est ) ; vsync_plus_bp = DIVIDE ( params -> min_vsbp * 1000000 , h_period_est ) ; print_value ( 8 , " " , vsync_plus_bp ) ; v_back_porch = vsync_plus_bp - params -> vsync_rqd ; print_value ( 9 , " " , v_back_porch ) ; total_v_lines = v_lines + top_margin + bottom_margin + vsync_plus_bp + interlace + params -> min_porch ; print_value ( 10 , " " , total_v_lines ) ; v_field_est = DIVIDE ( DIVIDE ( 1000000000000000ULL , h_period_est ) , total_v_lines ) ; print_value ( 11 , " " , v_field_est ) ; h_period = DIVIDE ( h_period_est * v_field_est , v_field_rqd * 1000 ) ; print_value ( 12 , " " , h_period ) ; v_field_rate = DIVIDE ( 1000000000000ULL , h_period * total_v_lines ) ; print_value ( 13 , " " , v_field_rate ) ; v_frame_rate = ( flags & VESAGTF_FLAG_ILACE ) ? v_field_rate / 2 : v_field_rate ; print_value ( 14 , " " , v_frame_rate ) ; left_margin = right_margin = ( flags & VESAGTF_FLAG_MARGINS ) ? DIVIDE ( DIVIDE ( h_pixels * params -> margin_ppt , 1000 ) , CELL_GRAN ) * CELL_GRAN : 0 ; print_value ( 15 , " " , left_margin ) ; print_value ( 16 , " " , right_margin ) ; total_active_pixels = h_pixels + left_margin + right_margin ; print_value ( 17 , " " , total_active_pixels ) ; ideal_duty_cycle = ( ( C_PRIME256 ( params ) * 1000 ) - ( M_PRIME256 ( params ) * h_period / 1000000 ) ) ; print_value ( 18 , " " , ideal_duty_cycle ) ; h_blank = DIVIDE ( DIVIDE ( total_active_pixels * ideal_duty_cycle , ( 256000 * 100ULL ) - ideal_duty_cycle ) , 2 * CELL_GRAN ) * ( 2 * CELL_GRAN ) ; print_value ( 19 , " " , h_blank ) ; total_pixels = total_active_pixels + h_blank ; print_value ( 20 , " " , total_pixels ) ; pixel_freq = DIVIDE ( total_pixels * 1000000 , DIVIDE ( h_period , 1000 ) ) ; print_value ( 21 , " " , pixel_freq ) ; #ifdef GTFDEBUG h_freq = 1000000000 / h_period ; print_value ( 22 , " " , h_freq ) ; #endif h_sync = DIVIDE ( ( ( params -> hsync_pct * total_pixels ) / 100 ) , CELL_GRAN ) * CELL_GRAN ; print_value ( 17 , " " , h_sync ) ; h_front_porch = ( h_blank / 2 ) - h_sync ; print_value ( 18 , " " , h_front_porch ) ; v_odd_front_porch_lines = ( ( 2 * params -> min_porch ) + interlace ) / 2 ; print_value ( 36 , " " , v_odd_front_porch_lines ) ; vmp -> hsync_start = h_pixels + h_front_porch ; vmp -> hsync_end = vmp -> hsync_start + h_sync ; vmp -> htotal = total_pixels ; vmp -> hdisplay = h_pixels ; vmp -> vsync_start = v_lines + v_odd_front_porch_lines ; vmp -> vsync_end = vmp -> vsync_start + params -> vsync_rqd ; vmp -> vtotal = total_v_lines ; vmp -> vdisplay = v_lines ; vmp -> dot_clock = pixel_freq ; }
vert_refresh() - as defined by the GTF Timing Standard, compute the
 Stage 1 Parameters using the vertical refresh frequency.
vert_refresh () - as defined by the GTF Timing Standard compute the Stage 1 Parameters using the vertical refresh frequency .
/*  1. In order to give correct results, the number of horizontal
     *  pixels requested is first processed to ensure that it is divisible
     *  by the character size, by rounding it to the nearest character
     *  cell boundary:
     *
     *  [H PIXELS RND] = ((ROUND([H PIXELS]/[CELL GRAN RND],0))*[CELLGRAN RND])
     */ /*  2. If interlace is requested, the number of vertical lines assumed
     *  by the calculation must be halved, as the computation calculates
     *  the number of vertical lines per field. In either case, the
     *  number of lines is rounded to the nearest integer.
     *   
     *  [V LINES RND] = IF([INT RQD?]="y", ROUND([V LINES]/2,0),
     *                                     ROUND([V LINES],0))
     */ /*  3. Find the frame rate required:
     *
     *  [V FIELD RATE RQD] = IF([INT RQD?]="y", [I/P FREQ RQD]*2,
     *                                          [I/P FREQ RQD])
     */ /*  4. Find number of lines in Top margin:
     *  5. Find number of lines in Bottom margin:
     *
     *  [TOP MARGIN (LINES)] = IF([MARGINS RQD?]="Y",
     *          ROUND(([MARGIN%]/100*[V LINES RND]),0),
     *          0)
     *
     *  Ditto for bottom margin.  Note that instead of %, we use PPT, which
     *  is parts per thousand.  This helps us with integer math.
     */ /*  6. If interlace is required, then set variable [INTERLACE]=0.5:
     *   
     *  [INTERLACE]=(IF([INT RQD?]="y",0.5,0))
     *
     *  To make this integer friendly, we use some special hacks in step
     *  7 below.  Please read those comments to understand why I am using
     *  a whole number of 1.0 instead of 0.5 here.
     */ /*  7. Estimate the Horizontal period
     *
     *  [H PERIOD EST] = ((1/[V FIELD RATE RQD]) - [MIN VSYNC+BP]/1000000) /
     *                    ([V LINES RND] + (2*[TOP MARGIN (LINES)]) +
     *                     [MIN PORCH RND]+[INTERLACE]) * 1000000
     *
     *  To make it integer friendly, we pre-multiply the 1000000 to get to
     *  usec.  This gives us:
     *
     *  [H PERIOD EST] = ((1000000/[V FIELD RATE RQD]) - [MIN VSYNC+BP]) /
     *			([V LINES RND] + (2 * [TOP MARGIN (LINES)]) +
     *			 [MIN PORCH RND]+[INTERLACE])
     *
     *  The other problem is that the interlace value is wrong.  To get
     *  the interlace to a whole number, we multiply both the numerator and
     *  divisor by 2, so we can use a value of either 1 or 0 for the interlace
     *  factor.
     *
     * This gives us:
     *
     * [H PERIOD EST] = ((2*((1000000/[V FIELD RATE RQD]) - [MIN VSYNC+BP])) /
     *			 (2*([V LINES RND] + (2*[TOP MARGIN (LINES)]) +
     *			  [MIN PORCH RND]) + [2*INTERLACE]))
     *
     * Finally we multiply by another 1000, to get value in picosec.
     * Why picosec?  To minimize rounding errors.  Gotta love integer
     * math and error propagation.
     */ /*  8. Find the number of lines in V sync + back porch:
     *
     *  [V SYNC+BP] = ROUND(([MIN VSYNC+BP]/[H PERIOD EST]),0)
     *
     *  But recall that h_period_est is in psec. So multiply by 1000000.
     */ /*  9. Find the number of lines in V back porch alone:
     *
     *  [V BACK PORCH] = [V SYNC+BP] - [V SYNC RND]
     *
     *  XXX is "[V SYNC RND]" a typo? should be [V SYNC RQD]?
     */ /*  10. Find the total number of lines in Vertical field period:
     *
     *  [TOTAL V LINES] = [V LINES RND] + [TOP MARGIN (LINES)] +
     *                    [BOT MARGIN (LINES)] + [V SYNC+BP] + [INTERLACE] +
     *                    [MIN PORCH RND]
     */ /*  11. Estimate the Vertical field frequency:
     *
     *  [V FIELD RATE EST] = 1 / [H PERIOD EST] / [TOTAL V LINES] * 1000000
     *
     *  Again, we want to pre multiply by 10^9 to convert for nsec, thereby
     *  making it usable in integer math.
     *
     *  So we get:
     *
     *  [V FIELD RATE EST] = 1000000000 / [H PERIOD EST] / [TOTAL V LINES]
     *
     *  This is all scaled to get the result in uHz.  Again, we're trying to
     *  minimize error propagation.
     */ /*  12. Find the actual horizontal period:
     *
     *  [H PERIOD] = [H PERIOD EST] / ([V FIELD RATE RQD] / [V FIELD RATE EST])
     */ /*  13. Find the actual Vertical field frequency:
     *
     *  [V FIELD RATE] = 1 / [H PERIOD] / [TOTAL V LINES] * 1000000
     *
     *  And again, we convert to nsec ahead of time, giving us:
     *
     *  [V FIELD RATE] = 1000000 / [H PERIOD] / [TOTAL V LINES]
     *
     *  And another rescaling back to mHz.  Gotta love it.
     */ /*  14. Find the Vertical frame frequency:
     *
     *  [V FRAME RATE] = (IF([INT RQD?]="y", [V FIELD RATE]/2, [V FIELD RATE]))
     *
     *  N.B. that the result here is in mHz.
     */ /*  15. Find number of pixels in left margin:
     *  16. Find number of pixels in right margin:
     *
     *  [LEFT MARGIN (PIXELS)] = (IF( [MARGINS RQD?]="Y",
     *          (ROUND( ([H PIXELS RND] * [MARGIN%] / 100 /
     *                   [CELL GRAN RND]),0)) * [CELL GRAN RND],
     *          0))
     *
     *  Again, we deal with margin percentages as PPT (parts per thousand).
     *  And the calculations for left and right are the same.
     */ /*  17. Find total number of active pixels in image and left and right
     *  margins:
     *
     *  [TOTAL ACTIVE PIXELS] = [H PIXELS RND] + [LEFT MARGIN (PIXELS)] +
     *                          [RIGHT MARGIN (PIXELS)]
     */ /*  18. Find the ideal blanking duty cycle from the blanking duty cycle
     *  equation:
     *
     *  [IDEAL DUTY CYCLE] = [C'] - ([M']*[H PERIOD]/1000)
     *
     *  However, we have modified values for [C'] as [256*C'] and
     *  [M'] as [256*M'].  Again the idea here is to get good scaling.
     *  We use 256 as the factor to make the math fast.
     *
     *  Note that this means that we have to scale it appropriately in
     *  later calculations.
     *
     *  The ending result is that our ideal_duty_cycle is 256000x larger
     *  than the duty cycle used by VESA.  But again, this reduces error
     *  propagation.
     */ /*  19. Find the number of pixels in the blanking time to the nearest
     *  double character cell:
     *
     *  [H BLANK (PIXELS)] = (ROUND(([TOTAL ACTIVE PIXELS] *
     *                               [IDEAL DUTY CYCLE] /
     *                               (100-[IDEAL DUTY CYCLE]) /
     *                               (2*[CELL GRAN RND])), 0))
     *                       * (2*[CELL GRAN RND])
     *
     *  Of course, we adjust to make this rounding work in integer math.
     */ /*  20. Find total number of pixels:
     *
     *  [TOTAL PIXELS] = [TOTAL ACTIVE PIXELS] + [H BLANK (PIXELS)]
     */ /*  21. Find pixel clock frequency:
     *
     *  [PIXEL FREQ] = [TOTAL PIXELS] / [H PERIOD]
     *
     *  We calculate this in Hz rather than MHz, to get a value that
     *  is usable with integer math.  Recall that the [H PERIOD] is in
     *  nsec.
     */ /*  22. Find horizontal frequency:
     *
     *  [H FREQ] = 1000 / [H PERIOD]
     *
     *  I've ifdef'd this out, because we don't need it for any of
     *  our calculations.
     *  We calculate this in Hz rather than kHz, to avoid rounding
     *  errors.  Recall that the [H PERIOD] is in usec.
     */ /* Stage 1 computations are now complete; I should really pass
       the results to another function and do the Stage 2
       computations, but I only need a few more values so I'll just
       append the computations here for now */ /*  17. Find the number of pixels in the horizontal sync period:
     *
     *  [H SYNC (PIXELS)] =(ROUND(([H SYNC%] / 100 * [TOTAL PIXELS] /
     *                             [CELL GRAN RND]),0))*[CELL GRAN RND]
     *
     *  Rewriting for integer math:
     *
     *  [H SYNC (PIXELS)]=(ROUND((H SYNC%] * [TOTAL PIXELS] / 100 /
     *				   [CELL GRAN RND),0))*[CELL GRAN RND]
     */ /*  18. Find the number of pixels in the horizontal front porch period:
     *
     *  [H FRONT PORCH (PIXELS)] = ([H BLANK (PIXELS)]/2)-[H SYNC (PIXELS)]
     *
     *  Note that h_blank is always an even number of characters (i.e.
     *  h_blank % (CELL_GRAN * 2) == 0)
     */ /*  36. Find the number of lines in the odd front porch period:
     *
     *  [V ODD FRONT PORCH(LINES)]=([MIN PORCH RND]+[INTERLACE])
     *
     *  Adjusting for the fact that the interlace is scaled:
     *
     *  [V ODD FRONT PORCH(LINES)]=(([MIN PORCH RND] * 2) + [2*INTERLACE]) / 2
     */ /* finally, pack the results in the mode struct */