/******************************************************************************
** Checker.c
*******************************************************************************
**
** description:
**
** This file contains the GAL-Checker.
**
******************************************************************************/






/********************************* includes **********************************/

#include <libraries/mui.h>

#include <intuition/intuition.h>
#include <stdio.h>
#include <string.h>

#include <clib/alib_protos.h>
 
#include <proto/locale.h>
#include <proto/graphics.h>
#include <proto/intuition.h>

#include "GALer.h"
#include "Localize.h"





/********************************** defines **********************************/





/********************************* functions *********************************/
 
static void  SetLevel(int pinnum, int level);

static void  CheckOutput(void);

static int   GadID2Pin(int gadID);





/********************************* variables *********************************/
 

extern  struct  AppString { LONG   as_ID;
                            STRPTR as_Str;
                          };

extern  struct  AppString AppStrings[];

extern  int     checker_mode;
extern  int     gal_type;
extern  int     asmreadyflag;
extern  int     num_of_pins;

extern  UBYTE   PinNames[24][10];

extern  APTR    main_win;

extern  APTR    menustrip, menu_project, menu_galtype, menu_gal, menu_asm,
                menu_galdis, menu_tools, menu_checker;

extern  APTR    GAL_txt[], GAL_gads[];

 



int     GALType;
int     outIC1, outIC2, outIC3, outIC4, outIC5;



struct  in_con    { int IC;
                    int Bit;
                    int IO;
                    int Level;
                  };

struct  out_con   { int IC;
                    int Bit;
                  };




            /* 1. entry: IC which controls the pin                      */
            /* 2. entry: bit of this IC which controls the pin          */
            /* 3. entry: shows whether a pin is used as input or output */
            /* 4. entry: shows level of this pin                        */

struct in_con  in_bit[23] = {

                {IC4, 0x01, INPUT_PIN,  LOW},    /* pin  1 */
                {IC4, 0x02, INPUT_PIN,  LOW},    /* pin  2 */
                {IC4, 0x04, INPUT_PIN,  LOW},    /* pin  3 */
                {IC4, 0x08, INPUT_PIN,  LOW},    /* pin  4 */
                {IC4, 0x10, INPUT_PIN,  LOW},    /* pin  5 */
                {IC4, 0x20, INPUT_PIN,  LOW},    /* pin  6 */
                {IC4, 0x40, INPUT_PIN,  LOW},    /* pin  7 */
                {IC4, 0x80, INPUT_PIN,  LOW},    /* pin  8 */
                {IC5, 0x01, INPUT_PIN,  LOW},    /* pin  9 */
                {IC5, 0x02, INPUT_PIN,  LOW},    /* pin 10 */
                {IC5, 0x04, INPUT_PIN,  LOW},    /* pin 11 */
                { 0,   0,      0,        0 },    /*  GND   */
                {IC5, 0x08, INPUT_PIN,  LOW},    /* pin 13 */
                {IC5, 0x10, INPUT_PIN,  LOW},    /* pin 14 */
                {IC5, 0x20, OUTPUT_PIN, LOW},    /* pin 15 */
                {IC3, 0x01, OUTPUT_PIN, LOW},    /* pin 16 */
                {IC3, 0x02, OUTPUT_PIN, LOW},    /* pin 17 */
                {IC3, 0x04, OUTPUT_PIN, LOW},    /* pin 18 */
                {IC3, 0x08, OUTPUT_PIN, LOW},    /* pin 19 */
                {IC3, 0x10, OUTPUT_PIN, LOW},    /* pin 20 */
                {IC3, 0x20, OUTPUT_PIN, LOW},    /* pin 21 */
                {IC3, 0x40, OUTPUT_PIN, LOW},    /* pin 22 */
                {IC3, 0x80, INPUT_PIN,  LOW}     /* pin 23 */

                            };



                                /* 1. entry: IC which can read the pin */
                                /* 2. entry: bit of this IC            */

struct out_con out_bit[10] = { { IC7,  0x01 },
                               { IC7,  0x02 },
                               { IC7,  0x04 },
                               { IC7,  0x08 },
                               { IC7,  0x10 },
                               { IC7,  0x20 },
                               { IC7,  0x40 },
                               { IC7,  0x80 },
                               { IC6a, 0x01 },
                               { IC6c, 0x01 } };






    /* The following figure shows where the parts of the symbolic GAL are */
    /* stored. The used variables are GAL_gads[] and GAL_txt[].           */

    /*  input:    name:               name:   output:   input:    in/out: */
    /*                                                                    */
    /*                    ---- ----                                       */
    /* gads[ 0]  txt[ 0]  | 1   24|  txt[23]                              */
    /* gads[ 1]  txt[ 1]  | 2   23|  txt[22]  txt[33]  gads[21]  gads[31] */
    /* gads[ 2]  txt[ 2]  | 3   22|  txt[21]  txt[32]  gads[20]  gads[30] */
    /* gads[ 3]  txt[ 3]  | 4   21|  txt[20]  txt[31]  gads[19]  gads[29] */
    /* gads[ 4]  txt[ 4]  | 5   20|  txt[19]  txt[30]  gads[18]  gads[28] */
    /* gads[ 5]  txt[ 5]  | 6   19|  txt[18]  txt[29]  gads[17]  gads[27] */
    /* gads[ 6]  txt[ 6]  | 7   18|  txt[17]  txt[28]  gads[16]  gads[26] */
    /* gads[ 7]  txt[ 7]  | 8   17|  txt[16]  txt[27]  gads[15]  gads[25] */
    /* gads[ 8]  txt[ 8]  | 9   16|  txt[15]  txt[26]  gads[14]  gads[24] */
    /* gads[ 9]  txt[ 9]  |10   15|  txt[14]  txt[25]  gads[13]  gads[23] */
    /* gads[10]  txt[10]  |11   14|  txt[13]  txt[24]  gads[12]  gads[22] */
    /*           txt[11]  |12   13|  txt[12]           gads[11]           */
    /*                    ---------                                       */






/******************************************************************************
** Checker()
*******************************************************************************
** input:   gadID       ID of the selected gadget of the symbolic GAL
**
** output:  none
**
** remarks: sets gadget texts and gadgets of the symbolic GAL according to
**          the users actions
******************************************************************************/

void Checker(ULONG gadID)
{
    UBYTE   c, *txt;

                                    /* get character of the */
                                    /* selected gadget      */

    get(GAL_gads[gadID - 1], MUIA_Text_Contents, &txt);

    c = *txt;


                            /*** input gadgets ***/
    if (gadID >= 1 && gadID <= 22)
    {
        if (c == 'L')                   /* level of input pin was low */
        {                               /* set it to high             */

            set(GAL_gads[gadID - 1], MUIA_Text_Contents, "H");
 
            in_bit[GadID2Pin(gadID) - 1].Level = HIGH;

            SetLevel(GadID2Pin(gadID), HIGH);
        }
        else
        {                               /* level of input pin was high */
                                        /* set it to low               */

            set(GAL_gads[gadID - 1], MUIA_Text_Contents, "L");

            in_bit[GadID2Pin(gadID) - 1].Level = LOW;

            SetLevel(GadID2Pin(gadID), LOW);
        }
    }

                                        /*** input/output gadgets (I/O) ***/

    if (gadID >= 23 && gadID <= 32)
    {
        if (c == 'O')                       /* pin is set as an output  */
        {                                   /* so change it to an input */

            set(GAL_gads[gadID - 1], MUIA_Text_Contents, "I");

                                            /* enable input level gadget */

            set(GAL_gads[gadID - 11], MUIA_Disabled, FALSE);

                                            /* disable output level */

            set(GAL_txt[gadID + 1], MUIA_Disabled, TRUE);
 

            SetLevel(GadID2Pin(gadID), LOW);

            in_bit[GadID2Pin(gadID) - 1].IO = INPUT_PIN;
        }
        else
        {                                   /* pin is set as an input    */
                                            /* so change it to an output */

            set(GAL_gads[gadID - 1], MUIA_Text_Contents, "O");

                                            /* disable input level gadget */

            set(GAL_gads[gadID - 11], MUIA_Disabled, TRUE);

                                            /* enable output level */

            set(GAL_txt[gadID + 1], MUIA_Disabled, FALSE);


            in_bit[GadID2Pin(gadID) - 1].IO = OUTPUT_PIN;

            SetLevel(GadID2Pin(gadID), LOW);
        }

    }


    CheckOutput();
}





/******************************************************************************
** ToggleChecker()
*******************************************************************************
** input:   none
**
** output:  none
**
** remarks: switchs the GAL checker on/off
******************************************************************************/

void ToggleChecker(void)
{
    int     n;


    if (checker_mode)
    {                                           /* exit GAL checker */

                        /* remove the checker's menu and add the main menu */
 
        DoMethod(menustrip, MUIM_Family_Remove, menu_checker);
 
        DoMethod(menustrip, MUIM_Family_AddHead, menu_project);
        DoMethod(menustrip, MUIM_Family_AddTail, menu_galtype);
        DoMethod(menustrip, MUIM_Family_AddTail, menu_gal);
        DoMethod(menustrip, MUIM_Family_AddTail, menu_asm);
        DoMethod(menustrip, MUIM_Family_AddTail, menu_galdis);
        DoMethod(menustrip, MUIM_Family_AddTail, menu_tools);



        LED(OFF);

        WriteByte(0x00, IC3);                   /* set all pins to low */
        WriteByte(0x00, IC4);
        WriteByte(0x00, IC5);
 
        DisableVcc();



        checker_mode = OFF;
                                                /* reset gadget text */


        switch (GALType)            /* set text and de-/activate gadgets */
        {
            case GAL16V8:

                DoMethod(main_win, MUIM_MultiSet, MUIA_Disabled, FALSE,
                        GAL_gads[12], GAL_gads[13], GAL_gads[14], GAL_gads[15],
                        GAL_gads[16], GAL_gads[17], GAL_gads[18], GAL_gads[19],
                        GAL_txt[24],  GAL_txt[25],  GAL_txt[26],  GAL_txt[27],
                        GAL_txt[28],  GAL_txt[29],  GAL_txt[30],  GAL_txt[31],
                        NULL);

                DoMethod(main_win, MUIM_MultiSet, MUIA_Text_Contents, "L",
                        GAL_gads[ 2], GAL_gads[ 3], GAL_gads[ 4], GAL_gads[ 5],
                        GAL_gads[ 6], GAL_gads[ 7], GAL_gads[ 8], GAL_gads[ 9],
                        GAL_gads[10], GAL_gads[11], GAL_gads[12], GAL_gads[13],
                        GAL_gads[14], GAL_gads[15], GAL_gads[16], GAL_gads[17],
                        GAL_gads[18], GAL_gads[19], NULL);

                DoMethod(main_win, MUIM_MultiSet, MUIA_Text_Contents, "O",
                        GAL_gads[22], GAL_gads[23], GAL_gads[24], GAL_gads[25],
                        GAL_gads[26], GAL_gads[27], GAL_gads[28], GAL_gads[29],
                        NULL);

                DoMethod(main_win, MUIM_MultiSet, MUIA_Text_Contents, "Z",
                        GAL_txt[24], GAL_txt[25], GAL_txt[26], GAL_txt[27],
                        GAL_txt[28], GAL_txt[29], GAL_txt[30], GAL_txt[31],
                        NULL);
                break;


            case GAL20V8:

                DoMethod(main_win, MUIM_MultiSet, MUIA_Disabled, FALSE,
                        GAL_gads[13], GAL_gads[14], GAL_gads[15], GAL_gads[16],
                        GAL_gads[17], GAL_gads[18], GAL_gads[19], GAL_gads[20],
                        GAL_txt[25],  GAL_txt[26],  GAL_txt[27],  GAL_txt[28],
                        GAL_txt[29],  GAL_txt[30],  GAL_txt[31],  GAL_txt[32],
                        NULL);

                DoMethod(main_win, MUIM_MultiSet, MUIA_Text_Contents, "L",
                        GAL_gads[ 0], GAL_gads[ 1], GAL_gads[ 2], GAL_gads[ 3],
                        GAL_gads[ 4], GAL_gads[ 5], GAL_gads[ 6], GAL_gads[ 7],
                        GAL_gads[ 8], GAL_gads[ 9], GAL_gads[10], GAL_gads[11],
                        GAL_gads[12], GAL_gads[13], GAL_gads[14], GAL_gads[15],
                        GAL_gads[16], GAL_gads[17], GAL_gads[18], GAL_gads[19],
                        GAL_gads[20], GAL_gads[21], NULL);

                DoMethod(main_win, MUIM_MultiSet, MUIA_Text_Contents, "O",
                        GAL_gads[23], GAL_gads[24], GAL_gads[25], GAL_gads[26],
                        GAL_gads[27], GAL_gads[28], GAL_gads[29], GAL_gads[30],
                        NULL);

                DoMethod(main_win, MUIM_MultiSet, MUIA_Text_Contents, "Z",
                        GAL_txt[25], GAL_txt[26], GAL_txt[27], GAL_txt[28],
                        GAL_txt[29], GAL_txt[30], GAL_txt[31], GAL_txt[32],
                        NULL);
                break;


            case GAL22V10:
            case GAL20RA10:

                DoMethod(main_win, MUIM_MultiSet, MUIA_Disabled, FALSE,
                        GAL_gads[12], GAL_gads[13], GAL_gads[14], GAL_gads[15],
                        GAL_gads[16], GAL_gads[17], GAL_gads[18], GAL_gads[19],
                        GAL_gads[20], GAL_gads[21],
                        GAL_txt[24],  GAL_txt[25],  GAL_txt[26],  GAL_txt[27],
                        GAL_txt[28],  GAL_txt[29],  GAL_txt[30],  GAL_txt[31],
                        GAL_txt[32],  GAL_txt[33],  NULL);

                DoMethod(main_win, MUIM_MultiSet, MUIA_Text_Contents, "L",
                        GAL_gads[ 0], GAL_gads[ 1], GAL_gads[ 2], GAL_gads[ 3],
                        GAL_gads[ 4], GAL_gads[ 5], GAL_gads[ 6], GAL_gads[ 7],
                        GAL_gads[ 8], GAL_gads[ 9], GAL_gads[10], GAL_gads[11],
                        GAL_gads[12], GAL_gads[13], GAL_gads[14], GAL_gads[15],
                        GAL_gads[16], GAL_gads[17], GAL_gads[18], GAL_gads[19],
                        GAL_gads[20], GAL_gads[21], NULL);

                DoMethod(main_win, MUIM_MultiSet, MUIA_Text_Contents, "O",
                        GAL_gads[22], GAL_gads[23], GAL_gads[24], GAL_gads[25],
                        GAL_gads[26], GAL_gads[27], GAL_gads[28], GAL_gads[29],
                        GAL_gads[30], GAL_gads[31], NULL);

                DoMethod(main_win, MUIM_MultiSet, MUIA_Text_Contents, "Z",
                        GAL_txt[24], GAL_txt[25], GAL_txt[26], GAL_txt[27],
                        GAL_txt[28], GAL_txt[29], GAL_txt[30], GAL_txt[31],
                        GAL_txt[32], GAL_txt[33], NULL);
                break;
        }

    }
    else
    {
        if (GALTypeRequest(AppStrings[MSG_GAL_CHECKER].as_Str))
        {
                            /* remove main menu and add the checker's menu */

            DoMethod(menustrip, MUIM_Family_Remove, menu_project);
            DoMethod(menustrip, MUIM_Family_Remove, menu_galtype);
            DoMethod(menustrip, MUIM_Family_Remove, menu_gal);
            DoMethod(menustrip, MUIM_Family_Remove, menu_asm);
            DoMethod(menustrip, MUIM_Family_Remove, menu_galdis);
            DoMethod(menustrip, MUIM_Family_Remove, menu_tools);
 
            DoMethod(menustrip, MUIM_Family_AddHead, menu_checker);
 


            WriteByte(0x00, IC3);           /* set all pins to low */
            WriteByte(0x00, IC4);
            WriteByte(0x00, IC5);

                                            /* initialize in_bit array */
            for (n = 0; n < 23; n++)
            {
                in_bit[n].Level = LOW;
                in_bit[n].IO    = INPUT_PIN;
            }

            in_bit[14].IO = OUTPUT_PIN;       /* pins 15 to 21 can always */
            in_bit[15].IO = OUTPUT_PIN;       /* be outputs */
            in_bit[16].IO = OUTPUT_PIN;
            in_bit[17].IO = OUTPUT_PIN;
            in_bit[18].IO = OUTPUT_PIN;
            in_bit[19].IO = OUTPUT_PIN;
            in_bit[20].IO = OUTPUT_PIN;


            switch (GALType)                    /* de-/activate gadgets */
            {
                case GAL16V8:
                    in_bit[13].IO = OUTPUT_PIN;

                    DoMethod(main_win, MUIM_MultiSet, MUIA_Disabled, FALSE,
                            GAL_txt[24], GAL_txt[25], GAL_txt[26], GAL_txt[27],
                            GAL_txt[28], GAL_txt[29], GAL_txt[30], GAL_txt[31],
                            NULL);

                    DoMethod(main_win, MUIM_MultiSet, MUIA_Disabled, TRUE,
                            GAL_gads[12], GAL_gads[13], GAL_gads[14], GAL_gads[15],
                            GAL_gads[16], GAL_gads[17], GAL_gads[18], GAL_gads[19],
                            NULL);
                    break;


                case GAL20V8:
                    in_bit[21].IO = OUTPUT_PIN;

                    DoMethod(main_win, MUIM_MultiSet, MUIA_Disabled, FALSE,
                            GAL_txt[25], GAL_txt[26], GAL_txt[27], GAL_txt[28],
                            GAL_txt[29], GAL_txt[30], GAL_txt[31], GAL_txt[32],
                            NULL);

                    DoMethod(main_win, MUIM_MultiSet, MUIA_Disabled, TRUE,
                            GAL_gads[13], GAL_gads[14], GAL_gads[15], GAL_gads[16],
                            GAL_gads[17], GAL_gads[18], GAL_gads[19], GAL_gads[20],
                            NULL);
                    break;


                case GAL22V10:
                case GAL20RA10:
                    in_bit[13].IO = OUTPUT_PIN;
                    in_bit[21].IO = OUTPUT_PIN;
                    in_bit[22].IO = OUTPUT_PIN;

                    DoMethod(main_win, MUIM_MultiSet, MUIA_Disabled, FALSE,
                            GAL_txt[24], GAL_txt[25], GAL_txt[26], GAL_txt[27],
                            GAL_txt[28], GAL_txt[29], GAL_txt[30], GAL_txt[31],
                            GAL_txt[32], GAL_txt[33], NULL);

                    DoMethod(main_win, MUIM_MultiSet, MUIA_Disabled, TRUE,
                            GAL_gads[12], GAL_gads[13], GAL_gads[14], GAL_gads[15],
                            GAL_gads[16], GAL_gads[17], GAL_gads[18], GAL_gads[19],
                            GAL_gads[20], GAL_gads[21], NULL);
                    break;
            }



            LED(ON);

            EnableVcc();

            checker_mode = ON;                        /* init. checker */

            WaitForTimer(100000L);

            CheckOutput();                            /* test outputs */

        }
    }
}
 




/******************************************************************************
** SetLevel()
*******************************************************************************
** input:   pinnum      pin number
**          level       level of the pin
**
** output:  none
**
** remarks: This function sets the level (= H or L) of a pin of the ZIF
**          socket.
******************************************************************************/
 
static void SetLevel(int pinnum, int level)
{
    int     bits, ICnum, outIC;



    ICnum = in_bit[pinnum - 1].IC;              /* get IC number and output */
    bits  = in_bit[pinnum - 1].Bit;             /* bit of this IC           */


    if (ICnum == IC3)
        outIC = outIC3;

    if (ICnum == IC4)
        outIC = outIC4;

    if (ICnum == IC5)
        outIC = outIC5;

 
    bits ^= 0xFF;                         /* invert all bits */

    outIC &= bits;                        /* set the output bit of the IC */

    if (level == HIGH)
    {
        bits ^= 0xFF;
        outIC |= bits;
    }

    WriteByte(outIC, ICnum);              /* set level of pin */

}





/******************************************************************************
** CheckOutput()
*******************************************************************************
** input:   none
**
** output:  none
**
** remarks: This function checks whether an output is High, Low or Tristate.
******************************************************************************/

static void CheckOutput(void)
{
    int n, in_bits, out_bits, pin_level;
    int inICnum, outICnum, outIC, byte, first_out, last_out;



    switch (GALType)                    /* get possible output pins */
    {
        case GAL16V8:
            first_out = 14;
            last_out  = 21;
            break;

        case GAL20V8:
            first_out = 15;
            last_out  = 22;
            break;

        case GAL22V10:
        case GAL20RA10:
            first_out = 14;
            last_out  = 23;
            break;
    }


    for (n = first_out; n <= last_out; n++)
    {                                           /* check all outputs */

        if (in_bit[n - 1].IO == OUTPUT_PIN)
        {

            outICnum = in_bit[n - 1].IC;
            out_bits = in_bit[n - 1].Bit;

            inICnum  = out_bit[n - 14].IC;
            in_bits  = out_bit[n - 14].Bit;


            if (outICnum == IC3)
                outIC = outIC3;

            if (outICnum == IC4)
                outIC = outIC4;

            if (outICnum == IC5)
                outIC = outIC5;


            byte = ReadByte(inICnum);       /* get level of the output pins */

            if (byte & in_bits)
                pin_level = HIGH;           /* "calculate" level of outputs */
            else
                pin_level = LOW;


            outIC |= out_bits;              /* set output HIGH */
            WriteByte(outIC, outICnum);

            byte = ReadByte(inICnum);       /* get level of the output pins */
                                            /* again                        */


            if (pin_level != (1 && (byte & in_bits)))
            {                           /* the output can be set high and low */
                                        /* so its a high impedance output     */

                set(GAL_txt[n + 10], MUIA_Text_Contents, "Z");
            }
            else
            {
                if (pin_level == LOW)
                    set(GAL_txt[n + 10], MUIA_Text_Contents, "L");
                else
                    set(GAL_txt[n + 10], MUIA_Text_Contents, "H");
            }


            outIC &= (out_bits^0xFF);       /* set output LOW again */
            WriteByte(outIC, outICnum);
        }
    }
}





/******************************************************************************
** SetSymbolGAL()
*******************************************************************************
** input:   type   type of GAL
** output:  none
**
** remarks: draws symbolic GAL according to the type of GAL
******************************************************************************/

void SetSymbolGAL(int gal_type)
{

                    /* enable/disable gadgets and text fields */
                    /* and set pin numbers according to the   */
                    /* selected type of GAL                   */

    switch (gal_type)
    {
        case GAL16V8:
            DoMethod(main_win, MUIM_MultiSet, MUIA_Disabled, TRUE,
                GAL_gads[0],  GAL_gads[1], GAL_gads[20], GAL_gads[21],
                GAL_gads[30], GAL_gads[31],
                GAL_txt[0],   GAL_txt[1], GAL_txt[22], GAL_txt[23], NULL);

            DoMethod(main_win, MUIM_MultiSet, MUIA_Text_Contents, "",
                GAL_gads[0],  GAL_gads[1], GAL_gads[20], GAL_gads[21],
                GAL_gads[30], GAL_gads[31],
                GAL_txt[0],   GAL_txt[1],  GAL_txt[22], GAL_txt[23],
                GAL_txt[32],  GAL_txt[33], NULL);
 

            DoMethod(GAL_gads[22], MUIM_Set, MUIA_Disabled, FALSE);

            set(GAL_gads[22], MUIA_Text_Contents, "O");
            set(GAL_txt[24],  MUIA_Text_Contents, "Z");

            break;


        case GAL20V8:
            DoMethod(main_win, MUIM_MultiSet, MUIA_Disabled, FALSE,
                GAL_gads[0],  GAL_gads[1], GAL_gads[20], GAL_gads[21],
                GAL_gads[30],
                GAL_txt[0],   GAL_txt[1], GAL_txt[22], GAL_txt[23], NULL);


            DoMethod(main_win, MUIM_MultiSet, MUIA_Disabled, TRUE,
                GAL_gads[22], GAL_gads[31], NULL);

            DoMethod(main_win, MUIM_MultiSet, MUIA_Text_Contents, "",
                GAL_gads[22], GAL_gads[31],
                GAL_txt[24],  GAL_txt[33], NULL);


            DoMethod(main_win, MUIM_MultiSet, MUIA_Text_Contents, "L",
                GAL_gads[0], GAL_gads[1], GAL_gads[20], GAL_gads[21], NULL);
 
            set(GAL_gads[30], MUIA_Text_Contents, "O");
            set(GAL_txt[32],  MUIA_Text_Contents, "Z");

            break;


        case GAL22V10:
        case GAL20RA10:
            DoMethod(main_win, MUIM_MultiSet, MUIA_Disabled, FALSE,
                GAL_gads[0],  GAL_gads[1], GAL_gads[20], GAL_gads[21],
                GAL_gads[22], GAL_gads[30], GAL_gads[31],
                GAL_txt[0],   GAL_txt[1], GAL_txt[22], GAL_txt[23], NULL);

            DoMethod(main_win, MUIM_MultiSet, MUIA_Text_Contents, "L",
                GAL_gads[0], GAL_gads[1], GAL_gads[20], GAL_gads[21], NULL);
 
            DoMethod(main_win, MUIM_MultiSet, MUIA_Text_Contents, "O",
                GAL_gads[22], GAL_gads[30], GAL_gads[31], NULL);

            DoMethod(main_win, MUIM_MultiSet, MUIA_Text_Contents, "Z",
                GAL_txt[24], GAL_txt[32], GAL_txt[33], NULL);

            break;
    }


    ResetPinNames();                /* set pin names/numbers */
}





/******************************************************************************
** PrintPinNames()
*******************************************************************************
** input:   none
**
** output:  none
**
** remarks: This function does print the pin names at the symbolic GAL.
******************************************************************************/

void PrintPinNames(void)
{
    int     start, end, n;

    char    string[20];
    char    number[8];



    if (asmreadyflag)
    {                                   /* is there an assembled file?  */
        if (gal_type == GALType)
        {                               /* is the type of the GAL o.k.? */

            if (GALType == GAL16V8)     /* GAL16V8: */
            {
                start =  2;             /* first pin has index 2  */
                end   = 21;             /* last  pin has index 21 */
            }
            else
            {                           /* GAL20V8, 22V10, 20RA10 */
                start =  0;
                end   = 23;
            }


            for (n = start; n <= end; n++)
            {
                                /* make string containing the pin name */
                                /* and the according pin number        */

                if (n < 12)             /* left side of the symbolic GAL  */
                {
                                        /* first pin name then pin number */

                    strcpy(string, "\33r");

                    strcat(string, (const char *)&PinNames[n - start][0]);

                    if (n - start + 1 < 10)
                        strcat(string, "  \33b");
                    else
                        strcat(string, " \33b");

                    sprintf(number, "%d", n - start + 1);

                    strcat(string, (const char *)number);

                }
                else                    /* right side of the symbolic GAL */
                {
                                        /* first pin number then pin name */

                    sprintf(number, "\33b%d\33n ", n - start + 1);

                    strcpy(string, (const char *)number);

                    strcat(string, (const char *)&PinNames[n - start][0]);

                }

                set(GAL_txt[n], MUIA_Text_Contents, string);
            }

        }
        else
        {
            ErrorReq(9);                             /* wrong GAL selected */
        }
    }
    else
    {
        ErrorReq(7);                                 /* no names to print*/
    }

}





/******************************************************************************
** PrintPinNames()
*******************************************************************************
** input:   none
**
** output:  none
**
** remarks: This function does assign "noname" to the pins of the
**          symbolic GAL.
******************************************************************************/

void ResetPinNames(void)
{
    int n, start, end;

    char    string[20];
    char    number[8];
 


    if (GALType == GAL16V8)     /* GAL16V8: */
    {
        start =  2;             /* first pin at index 2  */
        end   = 21;             /* last  pin at index 21 */
    }
    else
    {                           /* GAL20V8, 22V10, 20RA10 */
        start =  0;
        end   = 23;
    }


    for (n = start; n <= end; n++)
    {
                        /* make string containing the pin name */
                        /* and the according pin number        */

        if (n < 12)             /* left side of the symbolic GAL  */
        {
                                /* first pin name then pin number */

            strcpy(string, "\33rnoname");

            if (n - start + 1 < 10)
                strcat(string, "  \33b");
            else
                strcat(string, " \33b");

            sprintf(number, "%d", n - start + 1);

            strcat(string, (const char *)number);

        }
        else                    /* right side of the symbolic GAL */
        {
                                /* first pin number then pin name */

            sprintf(number, "\33b%d\33n ", n - start + 1);

            strcpy(string, (const char *)number);

            strcat(string, "noname");

        }

        set(GAL_txt[n], MUIA_Text_Contents, string);
    }

}





/******************************************************************************
** GadID2Pin()
*******************************************************************************
** input:   gadID       gadget ID
**
** output:              pin number
**                      -1: can't connect the gadget ID with a pin
**
** remarks: This function transforms a gadget ID of the symbolic GAL to the
**          according pin number of the ZIF socket.
******************************************************************************/

static int GadID2Pin(int gadID)
{
    int pin;



    pin = -1;


    if (gadID >= 1 && gadID <= 11)              /* input gadgets left  */
        pin = gadID;
    else
        if (gadID >= 12 && gadID <= 22)         /* input gadgets right */
            pin = gadID + 1;
        else
            if (gadID >= 23 && gadID <= 32)     /* in/out selection    */
                pin = gadID - 9;


    return (pin);
}





/* EOF */
