#!/bin/bash
# #############################################################################
# Copyright (c) 2013 Benjamin Krill <benjamin@krll.de>
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.
# #############################################################################

if [ $# != 1 ]; then
	echo "$0 <input-file>"
	exit -1;
fi

ifile=$1

c=0;
for i in `cat $ifile`; do
	if [ $[ c % 16 ] -eq 0 ]; then
		echo
		printf "%04x: " $c
	fi
	printf " %02x" 0x$i;
	d[$c]=$i;
	c=$[ $c + 1 ];
done;
echo; echo

echo "Pixel Clock Freq: $[ 0x${d[0x37]}${d[0x36]} * 10000 ] Hz"
tmp=$[ (0x${d[0x3a]} & 0xf0) << 4 | 0x${d[0x38]}]
printf "Horizontal Active: %d | x\"%x\"\n" $tmp $tmp
tmp=$[ (0x${d[0x3a]} & 0x0f) << 8 | 0x${d[0x39]}]
printf "Horizontal Blanking: %d | x\"%x\"\n" $tmp $tmp
tmp=$[ (0x${d[0x3d]} & 0xf0) << 4 | 0x${d[0x3b]}]
printf "Vertical Active: %d | x\"%x\"\n" $tmp $tmp
tmp=$[ (0x${d[0x3d]} & 0x0f) << 8 | 0x${d[0x3c]}]
printf "Vertical Blanking: %d | x\"%x\"\n" $tmp $tmp

tmp=$[ (0x${d[0x41]} & 0xc0) << 2 | 0x${d[0x3e]}]
printf "Horizontal Sync. Offset: %d | x\"%x\"\n" $tmp $tmp
tmp=$[ (0x${d[0x41]} & 0x30) << 4 | 0x${d[0x3f]}]
printf "Horizontal Sync. Pulse Width: %d | x\"%x\"\n" $tmp $tmp

tmp=$[ (0x${d[0x41]} & 0x0c) << 2 | (0x${d[0x40]} >> 4) ]
printf "Vertical Sync. Offset: %d | x\"%x\"\n" $tmp $tmp
tmp=$[ (0x${d[0x41]} & 0x03)      | (0x${d[0x40]} & 0x0f) ]
printf "Vertical Sync. Pulse Width: %d | x\"%x\"\n" $tmp $tmp
