/*
    Notch.c - Turn on or off the SG-2000s Notch Filter

    DESCRIPTION
        This example shows usage of the SGSetRegister() function to modify
        a bit in the SuperGen 2000s Control register.

        The current state of the Notch Filter bit is either set or cleared
        depending on the command line arguments.  The resulting state of
        the Notch Filter bit is reported to stdout.

    USAGE
        Notch [0|1]

        example:
            Notch 1     turns on notch filter
            Notch 0     turns off notch filter
            Notch       reports current notch filter settings

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

        Aztec C:    (v5.0)
            cc Notch
            ln Notch -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

BOOL isctrlvalid (void);
void setnotch (BOOL enable);
void reportnotch (void);

int main (int argc, char **argv)
{
  #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;
    }

    if (isctrlvalid()) {   /* see if we can access the Control register */
            /* if there's a command line argument, read it and send it to
               setnotch() */
        if (argc > 1) setnotch (atoi(argv[1]));
        reportnotch();      /* print the current Notch Filter status */
    }
    else printf ("Couldn't access Control register.\n");

clean:
    if (SuperGenBase) CloseLibrary (SuperGenBase);

    return 0;
}


BOOL isctrlvalid (void)
{
    /*
        Call SuperGenType and look in info->RegMask to see if the
        Control register is accessible.
    */

    register struct SuperGenInfo *info = SuperGenType();

    return (info->RegMask & SGRF_Control) != 0;
}


void setnotch (BOOL enable)
{
    /*
        When enable is non-zero, turn on the Notch Filter.  Otherwise
        turn it off.

        The value constants for each of the Control bits is defined in
        supergen/supergen.h (SGCR_...._On or Off).  It is wise to use
        these constants rather than 1 or 0 since some of the Control
        bits use negative logic.  The _On or _Off constants can be
        safely used for the Value and ORed together in any combination
        just like the Mask values.

        The Mask value, SGCRF_NotchFilter, indicates that only the Notch
        Filter bit is to be changed by this call.
    */

    SGSetRegister (enable ? SGCR_NotchFilter_On : SGCR_NotchFilter_Off, SGCRF_NotchFilter, SGR_Control);
}


void reportnotch (void)
{
    BOOL notch;

    /*
        Prints the current state of the Notch Filter bit.

        SGSetRegister() is called to get the current settings for all
        the Control bits.  The result is masked with SGCRF_NotchFilter
        and compared to SGCR_NotchFilter_On.
    */

    notch = (SGGetRegister (SGR_Control) & SGCRF_NotchFilter) == SGCR_NotchFilter_On;
    printf ("Notch Filter is %s\n", notch ? "ON" : "OFF");
}
