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