/* $XFree86: xc/programs/Xserver/hw/kdrive/savage/s3.nick,v 1.1 1999/11/19 13:53:55 hohndel Exp $ */

global f_ref = 14318000;

function s3_clock (m, n, r)
{
    return f_ref * (m + 2) / ((n + 2) * (2 ^ r));
}

function s3_near (f1, f2)
{
    return abs (f1 - f2) < f1 / 10;
}

function s3_clocks (f)
{
    auto    m, n, r, ft;
    auto    dist, min_dist;
    auto    min_m, min_n, min_r;

    min_dist = f / 5;
    for (r = 0; r <= 3; r++)
	for (n = 0; n <= 31; n++)
		for (m = 0; m <= 127; m++)
		{
		    ft = s3_clock (m, n, r);
		    if (s3_near (ft, f))
			printf ("m %d n %d r %d = %d\n",
				m, n, r, ft);
		    dist = abs (f - ft);
		    if (dist < min_dist)
		    {
			min_dist = dist;
			min_m = m;
			min_n = n;
			min_r = r;
		    }
		}
    printf ("m %d n %d r %d f %d dist %d\n",
	    min_m, min_n, min_r, s3_clock(min_m, min_n, min_r), min_dist);
}
