#!/usr/bin/ruby -w =begin * gtf.rb Copyright (c) 2008, Paul Lutus * Released under the GPL * This Ruby program is largely based on: * ------------------------------------------------------------- * gtf.c Generate mode timings using the GTF Timing Standard * * Copyright (c) 2001, Andy Ritger aritger@nvidia.com * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * * o Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * o Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer * in the documentation and/or other materials provided with the * distribution. * o Neither the name of NVIDIA nor the names of its contributors * may be used to endorse or promote products derived from this * software without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT * NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL * THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE * POSSIBILITY OF SUCH DAMAGE. =end class Mode attr_accessor :hr, :hss, :hse, :hbl, :hfl, :vr, :vbase, :vss, :vse, :vfl, :pclk, :h_freq, :v_freq, :interlace, :interlaced end class Options attr_accessor :x, :y,:v_freq,:xf86mode,:interlaced,:margins def initialize() @xf86mode = true @margins = false @interlaced = false end end class Gtf # constants from GTF specification MARGIN_PERCENT = 1.8 # % of active vertical image CELL_GRAN = 8.0 # assumed character cell granularity MIN_PORCH = 1 # minimum front porch V_SYNC_RQD = 3 # width of vsync in lines H_SYNC_PERCENT = 8.0 # width of hsync as % of total line MIN_VSYNC_PLUS_BP = 550.0 # min time of vsync + back porch (microsec) M = 600.0 # blanking formula gradient C = 40.0 # blanking formula offset K = 128.0 # blanking formula scaling factor J = 20.0 # blanking formula scaling factor def initialize() @verbose = false compute() end # imitate the effect of "rint()" from the math.c library def rint(v) return ((v+0.5).to_i).to_f end def print_verbose(n, name, val) if (@verbose) printf("%2d: %-27s: %15f\n", n, name, val); end end def print_xf86_mode (m) s_int1 = (m.interlaced)?"i":"" s_int2 = (m.interlaced)?" interlace":"" printf("\n # %dx%d @ %.2f Hz (GTF) hsync: %.2f kHz; pclk: %.2f MHz\n", m.hr, m.vbase, m.v_freq, m.h_freq, m.pclk) printf(" Modeline \"%dx%d_%.2f%s\" %.2f" + " %d %d %d %d" + " %d %d %d %d" + " -HSync +Vsync%s\n\n", m.hr, m.vbase, m.v_freq, s_int1, m.pclk, m.hr, m.hss, m.hse, m.hfl, m.vr, m.vss, m.vse, m.vfl,s_int2) end def print_fb_mode (m) printf("\nmode \"%dx%d %.2fHz 32bit (GTF)\"\n", m.hr, m.vbase, m.v_freq) printf(" # PCLK: %.2f MHz, H: %.2f kHz, V: %.2f Hz\n", m.pclk, m.h_freq, m.v_freq) printf(" geometry %d %d %d %d 32\n", m.hr, m.vbase, m.hr, m.vr) printf(" timings %d %d %d %d %d %d %d\n", rint(1000000.0/m.pclk), # pixclock in picoseconds m.hfl - m.hse, # left margin (in pixels) m.hss - m.hr, # right margin (in pixels) m.vfl - m.vse, # upper margin (in pixel lines) m.vss - m.vr, # lower margin (in pixel lines) m.hse - m.hss, # horizontal sync length (in pixels) m.vse - m.vss); # vert sync length (in pixel lines) printf(" hsync low\n") printf(" vsync high\n") printf(" laced true\n") if m.interlaced printf("endmode\n\n") end def comp_stage_1(options) =begin /* 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]) */ =end h_pixels_rnd = rint(options.x.to_f / CELL_GRAN) * CELL_GRAN print_verbose(1, "[H PIXELS RND]", h_pixels_rnd) =begin /* 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)) */ =end v_lines_rnd = (options.interlaced)? rint(options.y.to_f)/ 2.0 : rint(options.y.to_f); print_verbose(2, "[V LINES RND]", v_lines_rnd); =begin /* 3. Find the frame rate required: * * [V FIELD RATE RQD] = IF([INT RQD?]="y", [I/P FREQ RQD]*2, * [I/P FREQ RQD]) */ =end v_field_rate_rqd = (options.interlaced)? (options.v_freq * 2.0) : (options.v_freq); print_verbose(3, "[V FIELD RATE RQD]", v_field_rate_rqd); =begin /* 4. Find number of lines in Top margin: * * [TOP MARGIN (LINES)] = IF([MARGINS RQD?]="Y", * ROUND(([MARGIN%]/100*[V LINES RND]),0), * 0) */ =end top_margin = (options.margins)? rint(MARGIN_PERCENT / 100.0 * v_lines_rnd) : (0.0); print_verbose(4, "[TOP MARGIN (LINES)]", top_margin); =begin /* 5. Find number of lines in Bottom margin: * * [BOT MARGIN (LINES)] = IF([MARGINS RQD?]="Y", * ROUND(([MARGIN%]/100*[V LINES RND]),0), * 0) */ =end bottom_margin = (options.margins)? rint(MARGIN_PERCENT/100.0 * v_lines_rnd) : (0.0) print_verbose(5, "[BOT MARGIN (LINES)]", bottom_margin); =begin /* 6. If interlace is required, then set variable [INTERLACE]=0.5: * * [INTERLACE]=(IF([INT RQD?]="y",0.5,0)) */ =end interlace = (options.interlaced)? 0.5 : 0.0; print_verbose(6, "[INTERLACE]", interlace); =begin /* 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 */ =end h_period_est = (((1.0/v_field_rate_rqd) - (MIN_VSYNC_PLUS_BP/1000000.0)) / (v_lines_rnd + (2*top_margin) + MIN_PORCH + interlace) * 1000000.0) print_verbose(7, "[H PERIOD EST]", h_period_est); =begin /* 8. Find the number of lines in V sync + back porch: * * [V SYNC+BP] = ROUND(([MIN VSYNC+BP]/[H PERIOD EST]),0) */ =end vsync_plus_bp = rint(MIN_VSYNC_PLUS_BP/h_period_est); print_verbose(8, "[V SYNC+BP]", vsync_plus_bp); =begin /* 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]? */ =end v_back_porch = vsync_plus_bp - V_SYNC_RQD; print_verbose(9, "[V BACK PORCH]", v_back_porch); =begin /* 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] */ =end total_v_lines = v_lines_rnd + top_margin + bottom_margin + vsync_plus_bp + interlace + MIN_PORCH; print_verbose(10, "[TOTAL V LINES]", total_v_lines); =begin /* 11. Estimate the Vertical field frequency: * * [V FIELD RATE EST] = 1 / [H PERIOD EST] / [TOTAL V LINES] * 1000000 */ =end v_field_rate_est = 1.0 / h_period_est / total_v_lines * 1000000.0; print_verbose(11, "[V FIELD RATE EST]", v_field_rate_est); =begin /* 12. Find the actual horizontal period: * * [H PERIOD] = [H PERIOD EST] / ([V FIELD RATE RQD] / [V FIELD RATE EST]) */ =end h_period = h_period_est / (v_field_rate_rqd / v_field_rate_est); print_verbose(12, "[H PERIOD]", h_period); =begin /* 13. Find the actual Vertical field frequency: * * [V FIELD RATE] = 1 / [H PERIOD] / [TOTAL V LINES] * 1000000 */ =end v_field_rate = 1.0 / h_period / total_v_lines * 1000000.0; print_verbose(13, "[V FIELD RATE]", v_field_rate); =begin /* 14. Find the Vertical frame frequency: * * [V FRAME RATE] = (IF([INT RQD?]="y", [V FIELD RATE]/2, [V FIELD RATE])) */ =end v_frame_rate = (options.interlaced)? v_field_rate / 2.0 : v_field_rate; print_verbose(14, "[V FRAME RATE]", v_frame_rate); =begin /* 15. Find number of pixels in left margin: * * [LEFT MARGIN (PIXELS)] = (IF( [MARGINS RQD?]="Y", * (ROUND( ([H PIXELS RND] * [MARGIN%] / 100 / * [CELL GRAN RND]),0)) * [CELL GRAN RND], * 0)) */ =end left_margin = (options.margins)? rint(h_pixels_rnd * MARGIN_PERCENT / 100.0 / CELL_GRAN) * CELL_GRAN : 0.0; print_verbose(15, "[LEFT MARGIN (PIXELS)]", left_margin); =begin /* 16. Find number of pixels in right margin: * * [RIGHT MARGIN (PIXELS)] = (IF( [MARGINS RQD?]="Y", * (ROUND( ([H PIXELS RND] * [MARGIN%] / 100 / * [CELL GRAN RND]),0)) * [CELL GRAN RND], * 0)) */ =end right_margin = (options.margins)? rint(h_pixels_rnd * MARGIN_PERCENT / 100.0 / CELL_GRAN) * CELL_GRAN : 0.0 print_verbose(16, "[RIGHT MARGIN (PIXELS)]", right_margin); =begin /* 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)] */ =end total_active_pixels = h_pixels_rnd + left_margin + right_margin; print_verbose(17, "[TOTAL ACTIVE PIXELS]", total_active_pixels); =begin /* 18. Find the ideal blanking duty cycle from the blanking duty cycle * equation: * * [IDEAL DUTY CYCLE] = [C'] - ([M']*[H PERIOD]/1000) */ =end ideal_duty_cycle = (((C - J) * K/256.0) + J) - ((K/256.0 * M) * h_period / 1000.0); print_verbose(18, "[IDEAL DUTY CYCLE]", ideal_duty_cycle); =begin /* 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]) */ =end h_blank = rint(total_active_pixels * ideal_duty_cycle / (100.0 - ideal_duty_cycle) / (2.0 * CELL_GRAN)) * (2.0 * CELL_GRAN); print_verbose(19, "[H BLANK (PIXELS)]", h_blank); =begin /* 20. Find total number of pixels: * * [TOTAL PIXELS] = [TOTAL ACTIVE PIXELS] + [H BLANK (PIXELS)] */ =end total_pixels = total_active_pixels + h_blank; print_verbose(20, "[TOTAL PIXELS]", total_pixels); =begin /* 21. Find pixel clock frequency: * * [PIXEL FREQ] = [TOTAL PIXELS] / [H PERIOD] */ =end pixel_freq = total_pixels / h_period; print_verbose(21, "[PIXEL FREQ]", pixel_freq); =begin /* 22. Find horizontal frequency: * * [H FREQ] = 1000 / [H PERIOD] */ =end h_freq = 1000.0 / h_period; print_verbose(22, "[H FREQ]", h_freq); m = Mode.new m.hr = (h_pixels_rnd).to_i m.hbl = h_blank.to_i m.hfl = (total_pixels).to_i m.vbase = options.y # non-interlaced vertical line count m.vr = (v_lines_rnd).to_i m.vfl = (total_v_lines).to_i m.pclk = pixel_freq m.h_freq = h_freq m.v_freq = options.v_freq m.interlace = interlace # the value m.interlaced = options.interlaced # the flag return(m) end # comp_stage_1() def comp_stage_2(m) =begin /* 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] */ =end h_sync = rint(H_SYNC_PERCENT/100.0 * m.hfl / CELL_GRAN) * CELL_GRAN; print_verbose(17, "[H SYNC (PIXELS)]", h_sync); =begin /* 18. Find the number of pixels in the horizontal front porch period: * * [H FRONT PORCH (PIXELS)] = ([H BLANK (PIXELS)]/2)-[H SYNC (PIXELS)] */ =end h_front_porch = (m.hbl / 2.0) - h_sync; print_verbose(18, "[H FRONT PORCH (PIXELS)]", h_front_porch); =begin /* 36. Find the number of lines in the odd front porch period: * * [V ODD FRONT PORCH(LINES)]=([MIN PORCH RND]+[INTERLACE]) */ =end v_odd_front_porch_lines = MIN_PORCH + m.interlace; print_verbose(36, "[V ODD FRONT PORCH(LINES)]", v_odd_front_porch_lines) m.hss = (m.hr + h_front_porch).to_i m.hse = (m.hr + h_front_porch + h_sync).to_i m.vss = (m.vr + v_odd_front_porch_lines).to_i m.vse = (m.vr + v_odd_front_porch_lines + V_SYNC_RQD).to_i if(m.interlaced) m.vr *= 2; m.vss *= 2; m.vse *= 2; m.vfl *= 2; end return(m) end # comp_stage_2() def usage() printf("\nusage: %s x y refresh [options]\n\n", __FILE__) puts("Required arguments:\n") puts(" x : the desired horizontal resolution, pixels (example 640)\n") puts(" y : the desired vertical resolution, pixels (example 480)\n") puts(" refresh : the desired refresh rate, Hz (example 60)\n") puts("Options:\n") puts(" -m|--margins : include standard image margins (#{MARGIN_PERCENT}%)\n") puts(" -i|--interlaced : interlaced video mode\n") puts(" -v|--verbose : print all intermediate values\n") puts(" -x|--xf86mode : output an XFree86-style mode description (default)\n") puts(" -f|--fbmode : output an fbset(8)-style mode description\n\n") end def parse_command_line() if ARGV.size < 3 # not enough args usage() return false else # ARGV count valid options = Options.new options.x = ARGV.shift.to_i options.y = ARGV.shift.to_i options.v_freq = ARGV.shift.to_f if(options.x == 0 || options.y == 0 || options.v_freq == 0) usage() return false end ARGV.each do |arg| case arg when "-v","--verbose" @verbose = true; when "-f","--fbmode" options.xf86mode = false; when "-x","--xf86mode" options.xf86mode = true; when "-i","--interlaced" options.interlaced = true; when "-m","--margins" options.margins = true; else # option error usage() return false end # case end # ARGV.each do end # ARGV count valid return options end # parse_command_line() def compute() if(options = parse_command_line()) m = comp_stage_1(options) m = comp_stage_2(m) if (options.xf86mode) print_xf86_mode(m) else print_fb_mode(m) end end # if options valid end # main() end # class Gtf gtf = Gtf.new