/*
    ShowReg.c - displays the contents of all SG-2000s registers.

    DESCRIPTION
        This example shows how to read and parse the contents of all 5
        SuperGen 2000s registers using SuperGenType(), SGGetRegister()
        and related constants.  A report containing the results is sent
        to stdout.

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

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

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


#include <supergen/supergen.h>
#include <stdio.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)
{
    register UWORD val;
    register struct SuperGenInfo *info;

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

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


    /*
        Call SuperGenType() to get the SuperGen model specific
        information.  We'll use this to print the SuperGen model name
        and to determine which registers are valid.
    */

    info = SuperGenType();

    printf ("       Name: %s\n", info->Name);

    /*
        Calling SGSetRegister() always returns the new state of the
        register regardless of the mask.  With a mask of zero you can read
        the contents of a register without changing it.  This is simplified
        by using the SGGetRegister() macro defined in supergen/supergen.h.

        Most of the registers contain more than one parameter.  You can
        easily parse the results using the Shift and Mask constants defined
        in supergen/supergen.h.

        The Dissolve register contains the values for the Foreground
        (graphics) and Background faders.
    */

    if (info->RegMask & SGRF_Dissolve) {
        val = SGGetRegister (SGR_Dissolve);
        printf ("   Dissolve: BG=%d FG=%d\n",
                        (val & SGDIS_BG_Mask) >> SGDIS_BG_Shift,
                        (val & SGDIS_FG_Mask) >> SGDIS_FG_Shift);
    }


    /*
        The Control register contains 12 control bits that enable or
        disable specific features of the SG-2000s.  This return value is
        not parsed by this example for the sake of brevity.  The specific
        bit usage is diagrammed in supergen/supergen.h.

        The Control register is a special case.  Since it contains 12
        single-bit flags instead of one or two values, the normal Mask &
        Shift pair of constants don't exist.  There is a Mask constant
        defined to extract the 12 bits from the SGGetRegister() return
        value (usually contains a 4 bit register signature in bits
        12-15).  See Notch.c for an example of modifying a single bit.
    */

    if (info->RegMask & SGRF_Control) {
        val = SGGetRegister (SGR_Control);
        printf ("    Control: $%03x\n", val & SGCR_Mask);
    }


    /*
        The SatVPhase register contains controls for the Saturation and
        Vertical Phase parameters.  These are broken out using the Shift
        and Mask constants as before.
    */

    if (info->RegMask & SGRF_SatVPhase) {
        val = SGGetRegister (SGR_SatVPhase);
        printf (" Saturation: %d\n", (val & SGSVP_Sat_Mask) >> SGSVP_Sat_Shift);
        printf ("    V-Phase: %d\n", (val & SGSVP_VPhase_Mask) >> SGSVP_VPhase_Shift);
    }


    /*
        The CPhase register controls Chroma Phase parameters.  There are
        two segments, Coarse and Fine, contained in this register.
    */

    if (info->RegMask & SGRF_CPhase) {
        val = SGGetRegister (SGR_CPhase);
        printf ("    C-Phase: Coarse=%d Fine=%d\n",
                        (val & SGCPH_Coarse_Mask) >> SGCPH_Coarse_Shift,
                        (val & SGCPH_Fine_Mask) >> SGCPH_Fine_Shift);
    }


    /*
        The HPhase register, Horizontal Phase, contains only one parameter.
        However, for consistency we include a Shift and Mask constant for
        it.
    */

    if (info->RegMask & SGRF_HPhase) {
        val = SGGetRegister (SGR_HPhase);
        printf ("    H-Phase: %d\n", (val & SGHPH_Mask) >> SGHPH_Shift);
    }

clean:
    if (SuperGenBase) CloseLibrary (SuperGenBase);

    return 0;
}
