/*
 *  Program that converts values from EGS-DisplayAdjust to the values
 *  needed by the grf_cl.c cirrus logic driver for NetBSD.  This is
 *  intended to get you into the ballpark, so to speak :-).  If the
 *  mode you want is wierd enough, you may have to tweak the values
 *  manually after getting the base values from this program.
 */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>

unsigned int
getfinput(char *s)
{
    int x;

    printf(s);
    fflush(stdout);
    scanf("%d", &x);
    return(x);
}

int
main(ac,av)
int ac;
char **av;
{
    unsigned long   pixfreq, linefreq, vfront, vsync, vback, hfront,
                    hsync, hback, width, height, depth, laced;
    unsigned long   FEQ, HBS, HBE, HSS, HSE, HT, VBS, VBE, VSS, VSE, VT,
                    WID, HI, DEP;

    printf("EGS Display Adjust to grfconfig converter.\n");

    /* get values */
    width =     getfinput("Width           :");
    height =    getfinput("Height          :");
    depth =     getfinput("Depth           :");
    laced =     getfinput("Laced? (0 or 1) :");
    printf("\nEnter values from EGS * 1000 (ignore units)\n\n");
    pixfreq =   getfinput("Pixel Frequency :");  /* to kHx */
    linefreq =  getfinput("Line Frequency  :");  /* to Hz */
    vfront =    getfinput("Vfront          :");  /* to us */
    vsync =     getfinput("Vsync           :");
    vback =     getfinput("Vback           :");
    hfront =    getfinput("Hfront          :");  /* to ns */
    hsync =     getfinput("Hsync           :");
    hback =     getfinput("Hback           :");


    /* convert horizontals into cycles */
    /* ((in khz) * 1000) * ((in ns) / 1000000000) = cycles */

    hfront = (pixfreq * hfront) / 8000000;
    hsync = (pixfreq * hsync) / 8000000;
    hback = (pixfreq * hback) / 8000000;

    /* (in hz) * ((in us) / 1000000)) = cycles */

    vfront = (linefreq * vfront) / 1000000;
    vsync = (linefreq * vsync) / 1000000;
    vback = (linefreq * vback) / 1000000;


    /* into grfcl values */
    FEQ = pixfreq * 1000;
    HT  = (width/8) + hfront + hback + hsync - 5;
    HBS = (width/8) -1;
    HBE = (width/8) + hfront + hback + hsync - 5;
    HSS = (width/8) + hfront + 1;
    HSE = (width/8) + hfront + hsync + 1;
    HI  = height;
    if (laced) 
        height /= 2;
    VT  = height + vfront + vback + vsync - 1;
    VSS = height + vfront - 1;
    VSE = height + vfront + vsync - 1;
    VBS = height - 1;
    VBE = height + vfront + vback + vsync - 1;

    WID = width;
    DEP = depth;

    if (HSE > HBE) 
        HT = HBE = HSE;

    /* print it out */
    printf("%d %d %d %d %d %d %d %d %d %d %d %d %d %d %d\n",
        1, FEQ, WID, HI, DEP,
        HBS, HSS, HSE, HBE, HT,
        VBS, VSS, VSE, VBE, VT
        );
        

                
    return(0);
}
