Home | History | Annotate | Line # | Download | only in iomd
      1  1.7   martin #	$NetBSD: makemodes.awk,v 1.7 2008/04/28 20:23:14 martin Exp $
      2  1.1  reinoud 
      3  1.1  reinoud #
      4  1.1  reinoud # Copyright (c) 1998 The NetBSD Foundation, Inc.
      5  1.1  reinoud # All rights reserved.
      6  1.1  reinoud #
      7  1.1  reinoud # This code is derived from software contributed to The NetBSD Foundation
      8  1.1  reinoud # by Mark Brinicombe
      9  1.1  reinoud #
     10  1.1  reinoud # Redistribution and use in source and binary forms, with or without
     11  1.1  reinoud # modification, are permitted provided that the following conditions
     12  1.1  reinoud # are met:
     13  1.1  reinoud # 1. Redistributions of source code must retain the above copyright
     14  1.1  reinoud #    notice, this list of conditions and the following disclaimer.
     15  1.1  reinoud # 2. Redistributions in binary form must reproduce the above copyright
     16  1.1  reinoud #    notice, this list of conditions and the following disclaimer in the
     17  1.1  reinoud #    documentation and/or other materials provided with the distribution.
     18  1.1  reinoud #
     19  1.1  reinoud # THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     20  1.1  reinoud # ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21  1.1  reinoud # TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22  1.1  reinoud # PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     23  1.1  reinoud # BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24  1.1  reinoud # CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25  1.1  reinoud # SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26  1.1  reinoud # INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27  1.1  reinoud # CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28  1.1  reinoud # ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29  1.1  reinoud # POSSIBILITY OF SUCH DAMAGE.
     30  1.1  reinoud #
     31  1.1  reinoud 
     32  1.1  reinoud # This parses a Acorn monitor definition file and constructs an array of
     33  1.1  reinoud # parameters for each mode.
     34  1.1  reinoud # Once the file has been parsed the list of modes is examined to find modes
     35  1.1  reinoud # that match the mode specifications specified on the command line.
     36  1.1  reinoud # The matching mode definitions are written to stdout in the form of a C file.
     37  1.1  reinoud # Parsing information is written to stderr.
     38  1.1  reinoud #
     39  1.1  reinoud #
     40  1.1  reinoud # Syntax for using this program
     41  1.1  reinoud #
     42  1.1  reinoud # awk -f makemodes.awk <MDF file> <mode spec> [<mode spec> ...]
     43  1.1  reinoud #
     44  1.1  reinoud # where <mode spec> is
     45  1.1  reinoud #	<x>,<y>
     46  1.1  reinoud #	<x>,<y>,<f>
     47  1.1  reinoud #	<x>,<y>,<c>,<f>
     48  1.1  reinoud #
     49  1.1  reinoud # Note: Spaces are NOT allowed in a mode specifier
     50  1.1  reinoud #
     51  1.1  reinoud # where	x = x resolution
     52  1.1  reinoud #	y = y resolution
     53  1.1  reinoud #	f = frame rate
     54  1.1  reinoud #	c = colour depth (16, 256, 32768, 65536)
     55  1.1  reinoud #
     56  1.1  reinoud 
     57  1.1  reinoud BEGIN {
     58  1.1  reinoud 	# Number of modes parsed and valid in the modes array.
     59  1.1  reinoud 	mode = 0;
     60  1.1  reinoud 
     61  1.1  reinoud 	# MDF file globals
     62  1.1  reinoud 	monitor = "";
     63  1.1  reinoud 	dpms = 0;
     64  1.1  reinoud 
     65  1.1  reinoud 	# Non zero if we are translating a mode
     66  1.1  reinoud 	translate = 0;
     67  1.1  reinoud 
     68  1.1  reinoud 	# ':'  character is used to separate the tokens.
     69  1.1  reinoud 	FS=":";
     70  1.1  reinoud 
     71  1.1  reinoud 	# Note the real number of arguments and truncate ARGC so that only the first
     72  1.1  reinoud 	# argument is used as a filename.
     73  1.1  reinoud 	realargc = ARGC;
     74  1.1  reinoud 	ARGC=2;
     75  1.4    bjh21 
     76  1.4    bjh21 	# Translation of sync_pol to videomode.flags
     77  1.4    bjh21 	pol[0] = "HP|VP";
     78  1.4    bjh21 	pol[1] = "HN|VP";
     79  1.4    bjh21 	pol[2] = "HP|VN";
     80  1.4    bjh21 	pol[3] = "HN|VN";
     81  1.1  reinoud }
     82  1.1  reinoud 
     83  1.1  reinoud # MDF File format
     84  1.1  reinoud /^file_format/ {
     85  1.1  reinoud 	# Currently we only understand format 1 MDF files
     86  1.1  reinoud 	if ($2 != 1) {
     87  1.1  reinoud 		printf("Unrecognised MDF format (%d)\n", $2);
     88  1.1  reinoud 		exit;
     89  1.1  reinoud 	}
     90  1.1  reinoud }
     91  1.1  reinoud 
     92  1.1  reinoud # Monitor name
     93  1.1  reinoud /^monitor_title/ {
     94  1.1  reinoud 	monitor = $2;
     95  1.1  reinoud }
     96  1.1  reinoud 
     97  1.1  reinoud # Monitor DPMS state
     98  1.1  reinoud /^DPMS_state/ {
     99  1.1  reinoud 	dpms = $2;
    100  1.1  reinoud }
    101  1.1  reinoud 
    102  1.1  reinoud # Start of mode definition
    103  1.1  reinoud /^startmode/ {
    104  1.1  reinoud 	translate = 1;
    105  1.1  reinoud }
    106  1.1  reinoud 
    107  1.1  reinoud # End of mode definition
    108  1.1  reinoud /^endmode/ {
    109  1.1  reinoud 	translate = 0;
    110  1.1  reinoud 	mode = mode + 1;
    111  1.1  reinoud }
    112  1.1  reinoud 
    113  1.1  reinoud # The mode definition name (only valid within startmode/endmode section)
    114  1.1  reinoud /^mode_name:/ {
    115  1.1  reinoud 	if (!translate)
    116  1.1  reinoud 		next;
    117  1.1  reinoud 	modes[mode, 0] = $2;
    118  1.1  reinoud 	next;
    119  1.1  reinoud }
    120  1.1  reinoud 
    121  1.1  reinoud # The horizontal resolution (only valid within startmode/endmode section)
    122  1.1  reinoud /^x_res:/ {
    123  1.1  reinoud 	if (!translate)
    124  1.1  reinoud 		next;
    125  1.1  reinoud 	modes[mode, 1] = $2;
    126  1.1  reinoud 	next;
    127  1.1  reinoud }
    128  1.1  reinoud 
    129  1.1  reinoud # The vertical resolution (only valid within startmode/endmode section)
    130  1.1  reinoud /^y_res:/ {
    131  1.1  reinoud 	if (!translate)
    132  1.1  reinoud 		next;
    133  1.1  reinoud 	modes[mode, 2] = $2;
    134  1.1  reinoud 	next;
    135  1.1  reinoud }
    136  1.1  reinoud 
    137  1.1  reinoud # The pixel rate (only valid within startmode/endmode section)
    138  1.1  reinoud /^pixel_rate:/ {
    139  1.1  reinoud 	if (!translate)
    140  1.1  reinoud 		next;
    141  1.1  reinoud 	modes[mode, 3] = $2;
    142  1.1  reinoud 	next;
    143  1.1  reinoud }
    144  1.1  reinoud 
    145  1.1  reinoud # The horizontal timings (only valid within startmode/endmode section)
    146  1.1  reinoud /^h_timings:/ {
    147  1.1  reinoud 	if (!translate)
    148  1.1  reinoud 		next;
    149  1.1  reinoud 	modes[mode, 4] = $2;
    150  1.1  reinoud 	next;
    151  1.1  reinoud }
    152  1.1  reinoud 
    153  1.1  reinoud # The vertical timings (only valid within startmode/endmode section)
    154  1.1  reinoud /^v_timings:/ {
    155  1.1  reinoud 	if (!translate)
    156  1.1  reinoud 		next;
    157  1.1  reinoud 	modes[mode, 5] = $2;
    158  1.1  reinoud 	next;
    159  1.1  reinoud }
    160  1.1  reinoud 
    161  1.1  reinoud # The sync polarity (only valid within startmode/endmode section)
    162  1.1  reinoud /^sync_pol:/ {
    163  1.1  reinoud 	if (!translate)
    164  1.1  reinoud 		next;
    165  1.1  reinoud 	modes[mode, 6] = $2;
    166  1.1  reinoud 	next;
    167  1.1  reinoud }
    168  1.1  reinoud 
    169  1.1  reinoud END {
    170  1.1  reinoud 	#
    171  1.1  reinoud 	# Now generate the C file
    172  1.1  reinoud 	#
    173  1.1  reinoud 
    174  1.1  reinoud 	# Create the file header
    175  1.1  reinoud 	printf("/*\n");
    176  1.1  reinoud 	printf(" * MACHINE GENERATED: DO NOT EDIT\n");
    177  1.1  reinoud 	printf(" *\n");
    178  1.1  reinoud 	printf(" * Created from %s\n", FILENAME);
    179  1.1  reinoud 	printf(" */\n\n");
    180  1.1  reinoud 	printf("#include <sys/types.h>\n");
    181  1.1  reinoud 	printf("#include <arm/iomd/vidc.h>\n\n");
    182  1.6    bjh21 	printf("const char * const monitor = \"%s\";\n", monitor);
    183  1.1  reinoud 	printf("const int dpms = %d;\n", dpms);
    184  1.4    bjh21 	printf("#define HP VID_PHSYNC\n");
    185  1.4    bjh21 	printf("#define HN VID_NHSYNC\n");
    186  1.4    bjh21 	printf("#define VP VID_PVSYNC\n");
    187  1.4    bjh21 	printf("#define VN VID_NVSYNC\n");
    188  1.1  reinoud 	printf("\n");
    189  1.1  reinoud 
    190  1.1  reinoud 	# Now define the modes array
    191  1.6    bjh21 	printf("const struct videomode vidc_videomode_list[] = {\n");
    192  1.6    bjh21 	nmodes = 0
    193  1.1  reinoud 
    194  1.1  reinoud 	# Loop round all the modespecs on the command line
    195  1.1  reinoud 	for (res = 2; res < realargc; res = res + 1) {
    196  1.1  reinoud 		pos = -1;
    197  1.1  reinoud 		found = -1;
    198  1.1  reinoud 		closest = 200;
    199  1.1  reinoud 
    200  1.1  reinoud 		# Report the mode specifier being processed
    201  1.2  gdamore 		printf("%s ==> ", ARGV[res]) | "cat 1>&2";
    202  1.1  reinoud 
    203  1.1  reinoud 		# Pull apart the modespec
    204  1.1  reinoud 		args = split(ARGV[res], modespec, ",");
    205  1.1  reinoud 
    206  1.1  reinoud 		# We need at least 2 arguments
    207  1.1  reinoud 		if (args < 2) {
    208  1.2  gdamore 			printf("Invalid mode specifier\n") | "cat 1>&2";
    209  1.1  reinoud 			continue;
    210  1.1  reinoud 		}
    211  1.1  reinoud 
    212  1.1  reinoud 		# If we only have x,y default c and f
    213  1.1  reinoud 		if (args == 2) {
    214  1.1  reinoud 			modespec[3] = 256;
    215  1.1  reinoud 			modespec[4] = -1;
    216  1.1  reinoud 		}
    217  1.1  reinoud 		# If we have x,y,f default c and re-arrange.
    218  1.1  reinoud 		if (args == 3) {
    219  1.1  reinoud 			modespec[4] = modespec[3];
    220  1.1  reinoud 			modespec[3] = 256;
    221  1.1  reinoud 		}
    222  1.1  reinoud 
    223  1.1  reinoud 		# Report the full mode specifier
    224  1.1  reinoud 		printf("%d x %d x %d x %d : ", modespec[1], modespec[2],
    225  1.2  gdamore 		    modespec[3], modespec[4]) | "cat 1>&2";
    226  1.1  reinoud 
    227  1.1  reinoud 		# Now loop round all the modes we parsed and find the matches
    228  1.1  reinoud 		for (loop = 0; loop < mode; loop = loop + 1) {
    229  1.1  reinoud 			# Match X & Y
    230  1.1  reinoud 			if (modespec[1] != modes[loop, 1]) continue;
    231  1.1  reinoud 			if (modespec[2] != modes[loop, 2]) continue;
    232  1.1  reinoud 
    233  1.1  reinoud 			# Split the horizontal and vertical timings
    234  1.1  reinoud 			# This is needed for the frame rate calculation
    235  1.1  reinoud 			ht = split(modes[loop, 4], htimings, ",");
    236  1.1  reinoud 			if (ht != 6) continue;
    237  1.1  reinoud 			vt = split(modes[loop, 5], vtimings, ",");
    238  1.1  reinoud 			if (vt != 6) continue;
    239  1.1  reinoud 
    240  1.1  reinoud 			# Calculate the frame rate
    241  1.1  reinoud 			fr = modes[loop, 3] / (htimings[1] + htimings[2] + \
    242  1.1  reinoud 			    htimings[3] + htimings[4] + htimings[5] + \
    243  1.1  reinoud 			    htimings[6]) / ( vtimings[1] + vtimings[2] + \
    244  1.1  reinoud 			    vtimings[3] + vtimings[4] + vtimings[5] + \
    245  1.3    bjh21 			    vtimings[6]);
    246  1.1  reinoud 			fr = fr * 1000;
    247  1.1  reinoud 
    248  1.1  reinoud 			# Remember the frame rate
    249  1.1  reinoud 			modes[loop, 7] = int(fr + 0.5);
    250  1.1  reinoud 
    251  1.4    bjh21 			# Create the internal version of the timings
    252  1.4    bjh21 			modes[loop, "timings"] = \
    253  1.4    bjh21 			    sprintf( \
    254  1.4    bjh21 			    "{ %d, %d,%d,%d,%d, %d,%d,%d,%d, %s, \"%s\" }",\
    255  1.4    bjh21 			    modes[loop, 3], htimings[4], \
    256  1.4    bjh21 			    htimings[4] + htimings[5] + htimings[6], \
    257  1.4    bjh21 			    htimings[4] + htimings[5] + htimings[6] + \
    258  1.4    bjh21 			    htimings[1], \
    259  1.4    bjh21 			    htimings[4] + htimings[5] + htimings[6] + \
    260  1.4    bjh21 			    htimings[1] + htimings[2] + htimings[3], \
    261  1.4    bjh21 			    vtimings[4], \
    262  1.4    bjh21 			    vtimings[4] + vtimings[5] + vtimings[6], \
    263  1.4    bjh21 			    vtimings[4] + vtimings[5] + vtimings[6] + \
    264  1.4    bjh21 			    vtimings[1], \
    265  1.4    bjh21 			    vtimings[4] + vtimings[5] + vtimings[6] + \
    266  1.4    bjh21 			    vtimings[1] + vtimings[2] + vtimings[3], \
    267  1.4    bjh21 			    pol[modes[loop, 6]], modes[loop, 0]);
    268  1.4    bjh21 
    269  1.1  reinoud 			# Report the frame rate
    270  1.2  gdamore 			printf("%d ", modes[loop, 7]) | "cat 1>&2";
    271  1.1  reinoud 
    272  1.1  reinoud 			# Is this the closest
    273  1.1  reinoud 			if (closest > mod(modes[loop, 7] - modespec[4])) {
    274  1.1  reinoud 				closest = mod(modes[loop, 7] - modespec[4]);
    275  1.1  reinoud 				pos = loop;
    276  1.1  reinoud 			}
    277  1.1  reinoud 
    278  1.1  reinoud 			# Do we have an exact match ?
    279  1.1  reinoud 			if (modes[loop, 7] == modespec[4])
    280  1.1  reinoud 				found = pos;
    281  1.1  reinoud 		}
    282  1.1  reinoud 
    283  1.1  reinoud 		# If no exact match use the nearest
    284  1.1  reinoud 		if (found == -1)
    285  1.1  reinoud 			found = pos;
    286  1.1  reinoud 
    287  1.1  reinoud 		# Did we find any sort of match ?
    288  1.1  reinoud 		if (found == -1) {
    289  1.2  gdamore 			printf("Cannot find mode") | "cat 1>&2";
    290  1.1  reinoud 			continue;
    291  1.1  reinoud 		}
    292  1.1  reinoud 
    293  1.1  reinoud 		# Report the frame rate matched
    294  1.2  gdamore 		printf("- %d", modes[found, 7]) | "cat 1>&2";
    295  1.1  reinoud 
    296  1.1  reinoud 		# Output the mode as part of the mode definition array
    297  1.6    bjh21 		printf("\t%s,\n", modes[found, "timings"]);
    298  1.1  reinoud 
    299  1.2  gdamore 		printf("\n") | "cat 1>&2";
    300  1.6    bjh21 		nmodes++;
    301  1.1  reinoud 	}
    302  1.1  reinoud 
    303  1.6    bjh21 	# Close the array.
    304  1.6    bjh21 	printf("};\n\n");
    305  1.6    bjh21 	printf("const int vidc_videomode_count = %d;\n", nmodes);
    306  1.1  reinoud }
    307  1.1  reinoud 
    308  1.1  reinoud #
    309  1.1  reinoud # Simple mod() function
    310  1.1  reinoud #
    311  1.1  reinoud function mod(a) {
    312  1.1  reinoud 	if (a < 0)
    313  1.1  reinoud 		return -a;
    314  1.1  reinoud 	return a;
    315  1.1  reinoud }
    316