/*
    SetCPhase.c - set Chroma Phase SG-2000s register.

    DESCRIPTION
        This example shows how to set the contents of one of the
        SuperGen 2000s registers using SGSetRegister().

        The Chroma Phase register (SGR_CPhase) is set to values from the
        command line.  The new values are reported to stdout.

    USAGE
        SetCPhase Coarse Fine

            Coarse - 0..7
            Fine - 0..255

    COMPILATION
        SAS/C:      (v5.1)
            lc -L SetCPhase

        Aztec C:    (v5.0)
            cc SetCPhase
            ln SetCPhase -lc

    Copyright (C) 1989-1992 Digital Creations, Inc.  All rights reserved.
*/


#include <supergen/supergen.h>
#include <stdio.h>
#include <stdlib.h>

#include <clib/exec_protos.h>
#include <clib/supergen_protos.h>
#include <pragmas/exec_pragmas.h>
#include <pragmas/supergen_pragmas.h>

struct Library *SuperGenBase;

#if AZTEC_C
  void _wb_parse (void) {}      /* stub out wb window parser (Aztec C) */
#endif

#if __SASC                      /* suppress normal Ctrl-C processing (SAS/C) */
  void __chkabort (void) {}     /* SAS/C ansi libraries */
  void chkabort (void) {}       /* SAS/C 5.0 pre-ansi libraries */
#endif

int main (int argc, char **argv)
{
    int coarse, fine;
    LONG val;

  #if AZTEC_C                   /* suppress normal Ctrl-C processing (Aztec C) */
    {
        extern int Enable_Abort;
        Enable_Abort = 0;
    }
  #endif

    if (argc < 3) {
        printf ("usage SetCPhase coarse fine\n");
        goto clean;
    }

    if (!(SuperGenBase = OpenLibrary ("supergen.library", SUPERGEN_Minimum))) {
        printf ("You need supergen.library v2.0 or higher.\n");
        goto clean;
    }

        /* read the command line arguments */

    coarse = atoi(argv[1]);
    fine = atoi(argv[2]);

    /*
        Set the C-Phase register.  We are affecting both the Coarse and
        Fine parameters in this register so the Mask we provide is the
        sum of the Coarse and Fine masks:

            SGCPH_Coarse_Mask | SGCPH_Fine_Mask

        Each of the components is shifted to the appropriate place using
        the corresponding Shift constant.  The resulting bit patterns
        are ORed together for the Value:

            coarse << SGCPH_Coarse_Shift | fine << SGCPH_Fine_Shift

        SGSetRegister() returns the new contents of the register which
        we can then examine.

        If SGSetRegister() returns -1, we attempted to access an
        invalid register for the current SuperGen configuration
        (e.g. an SG-10).
    */

    val = SGSetRegister (coarse << SGCPH_Coarse_Shift | fine << SGCPH_Fine_Shift,
                         SGCPH_Coarse_Mask | SGCPH_Fine_Mask, SGR_CPhase);

    if (val != -1)
        printf ("new C-Phase: Coarse=%d Fine=%d\n",
                        ((UWORD)val & SGCPH_Coarse_Mask) >> SGCPH_Coarse_Shift,
                        ((UWORD)val & SGCPH_Fine_Mask) >> SGCPH_Fine_Shift);
    else
        printf ("Couldn't set C-Phase register.\n");

clean:
    if (SuperGenBase) CloseLibrary (SuperGenBase);

    return 0;
}

