/******************************************************************************
** GALer.h
*******************************************************************************
**
** author:   Christian Habermann
** date:     07.02.1995
**
*******************************************************************************
**
** description:
** This file includes some definitions and structures used by GALer.
**
*******************************************************************************
**
** changes:
** date     |     author     |  comment
**          |                |
**          |                |
******************************************************************************/





/****************************** include files ********************************/

#ifndef  EXEC_TYPES_H
#include <exec/types.h>
#endif

#ifndef  __STDIO_H
#include <stdio.h>
#endif





/******************************* definitions *********************************/

#define         GAL16V8         1               /* GAL types */
#define         GAL20V8         2
#define         GAL22V10        3
#define         GAL20RA10       4
#define         UNKNOWN         5
#define         NOT_SPECIFIED   6


#define         HW_GALER_1_0    1               /* hardware versions */
#define         HW_GALER_1_2    2
#define         HW_GALER_1_3    3
#define         HW_ELEKTOR_II   10


#define         EDITOR_STR_SIZE 25              /* string size for editor */


#define         IC1              3              /* IC numbers */
#define         IC2             11              /* IC2 only for Elektor- and */
#define         IC3              0              /* Maxon-GAL-programmer      */
#define         IC4              1
#define         IC5              2
#define         IC6a             0
#define         IC6c             4
#define         IC7              1
#define         IC10            10


#define         ON              1               /* LED status */
#define         OFF             0

#define         YES             1
#define         NO              0

#define         PROG            1               /* GAL mode */
#define         VERIFY          0

#define         LOW             0
#define         HIGH            1
#define         INPUT_PIN       0
#define         OUTPUT_PIN      1

#define         CONT_REQ        0               /* types of requester */
#define         INFO_REQ        1
#define         ERR_REQ         2
#define         GALTYPE_REQ     3
#define         ERASE_REQ       5

#define         LOAD            0               /* types of file-requesters */
#define         SAVE            1

                                             /* duration of the STR strobe */
#define         _80MS           80000L       /* in micros for programming  */
#define         _10MS           10000L       /* and erasing                */
#define         _40MS           40000L
#define         _50MS           50000L
#define         _100MS          100000L
#define         VERIFY_TIME     5L            /* read */


                                              /* voltages */
#define         V12_00          4             /* 12.00V */
#define         V14_00          3             /* 14.00V */
#define         V14_50          2             /* 14.50V */
#define         V15_75          1             /* 15.75V */
#define         V16_50          0             /* 16.50V */


#define         COPYGAL         1       /* used by ProgJedecToGAL function */
#define         PROGGAL         2

                                        /* GAL16V8 */
#define LOGIC16         0               /* location of the fuses */
#define XOR16           2048            /* in the JEDEC file     */
#define SIG16           2056
#define AC116           2120
#define PT16            2128
#define SYN16           2192
#define AC016           2193
#define NUMOFFUSES16    2194
                                        /* GAL20V8 */
#define LOGIC20         0               /* location of the fuses */
#define XOR20           2560            /* in the JEDEC file     */
#define SIG20           2568
#define AC120           2632
#define PT20            2640
#define SYN20           2704
#define AC020           2705
#define NUMOFFUSES20    2706

                                        /* GAL22V10 */
#define NUMOFFUSES22V10 5892            /* location of the fuses */
#define XOR22V10        5808            /* in the JEDEC file     */
#define SYN22V10        5809
#define SIG22V10        5828

                                        /* GAL20RA10 */
#define NUMOFFUSES20RA10 3274           /* location of the fuses */
#define XOR20RA10        3200           /* in the JEDEC file     */
#define SIG20RA10        3210


#define LOGIC16_SIZE    2048            /* number of bits for XOR etc. */
#define LOGIC20_SIZE    2560
#define LOGIC22V10_SIZE 5808
#define LOGIC20RA10_SIZE 3200
#define ROW_SIZE_16V8   64
#define ROW_SIZE_20V8   64
#define ROW_SIZE_22V10  132
#define ROW_SIZE_20RA10 80
#define XOR_SIZE        8
#define SIG_SIZE        64
#define AC1_SIZE        8
#define PT_SIZE         64
#define SYN_SIZE        1
#define AC0_SIZE        1
#define ACW_SIZE        82              /* architecture control word (ACW) */

#define MAX_FUSE_ADR16          31      /* addresses of the GALs */
#define SIG_ADR16               32      /* (fuer Fan-Post :-))   */
#define MAX_FUSE_ADR20          39
#define SIG_ADR20               40
#define MAX_FUSE_ADR22V10       43
#define SIG_ADR22V10            44
#define MAX_FUSE_ADR20RA10      39
#define SIG_ADR20RA10           40
#define ACW_ADR                 60
#define SECURITY_ADR            61
#define ERASE_ADR               63


                            /* the following definitions are */
                            /* used by GALer's assembler     */

#define ASSEMBLER       0         /* assemble whole file -> JEDEC       */
#define OPTIMIZER       1         /* assemble up to the equationd, then */
                                  /* return (used by the optimizer )    */
 
                                  /* output's polarity: */
#define ACTIVE_LOW      0             /* pin is high-active */
#define ACTIVE_HIGH     1             /* pin is low-active  */

                                  /* type of the pin: */
#define NOTUSED         0             /* pin not used up to now */
#define NOTCON          0             /* pin not used           */
#define INPUT           2             /* input                  */
#define COMOUT          3             /* combinational output   */
#define TRIOUT          4             /* tristate output        */
#define REGOUT          5             /* register output        */
#define COM_TRI_OUT     6             /* either tristate or     */
                                      /* combinational output   */

                                  /* tristate control: */
#define NO_TRICON       0             /* no tristate control defined       */
#define TRICON          1             /* tristate control defined          */
#define TRI_VCC         2             /* permanent low  impedance          */
#define TRI_GND         3             /* permanent high impedance          */
#define TRI_PRO         4             /* tristate control via product term */


#define NC_PIN          30

#define MODE1           1               /* modes (SYN, AC0) */
#define MODE2           2
#define MODE3           3


#define MAX_SUFFIX_SIZE 6               /* max. string length of a legal */
                                        /* suffix */


                            /* the following definitions are */
                            /* used by GALer's optimizer     */

#define NEGATION        0x80            /* bit 7 = 1 if pinname is */
                                        /* introduced by a'/'      */
#define EQUASKIP        0xFE            /* skip equation   */
#define EQUAEND         0xFF            /* end of equation */
#define EQUASEND        0x00            /* end of equation part, the next */
                                        /* word should be DESCRIPTION     */

#define ENTRY_SIZE      256             /* number of entries per buffer */

#define SIZE_OF_EQUASTRING 80
 


#define FIRST_GAL_GADGET_ID 1           /* lowest ID for the GAL gadgets */
                                        /* (must be > 0)                 */


                                        /* menu IDs */

enum {  FIRST_MENU_ID = 100,

        MEN_PROJECT, MEN_ABOUT, MEN_HARDVER, MEN_VER11, MEN_VER12,
        MEN_VER13, MEN_VERELEKTOR, MEN_SAVECONFIG, MEN_QUIT,

        MEN_GALTYPE, MEN_16V8, MEN_20V8, MEN_22V10, MEN_20RA10, MEN_TYPEREQ,

        MEN_GAL, MEN_PROG, MEN_COPY, MEN_ERASE, MEN_CMP, MEN_CLEAR,
        MEN_SETSEC, MEN_TESTSEC, MEN_WRITE,

        MEN_GALASM, MEN_ASSEMBLE,

        MEN_GALDIS, MEN_READSIG, MEN_READACW, MEN_READINFO, MEN_READJED,
        MEN_READJEDPARA, MEN_REASM,

        MEN_TOOLS, MEN_SHOWPIN, MEN_CLEARPIN, MEN_CALLCHECKER, MEN_OPTIMIZER,
        MEN_EDIT, MEN_CALLEDIT, MEN_HELP,

        MEN_CHECKER, MEN_EXITCHECKER,

        LAST_MENU_ID
     };
 





/******************************** structures *********************************/

                                        /* this structure is used to store   */
                                        /* GALer's configuration             */
struct  Configuration { BYTE GALType;          /* type of GAL                */
                        BYTE GALTypeReq;       /* TRUE=Requester für GAL-Typ */
                        BYTE GenJedec;         /* generate JEDEC file?       */
                        BYTE GenFuse;          /* generate fuse file?        */
                        BYTE GenChip;          /* generate chip file?        */
                        BYTE GenPin;           /* generate pin file?         */
                        BYTE AutoSave;         /* save automatically?        */
                        BYTE AutoGAL;          /* get type of GAL automatic.?*/
                        BYTE ProgEmptyTest;    /* blank test before prog.?   */
                        BYTE ProgVerify;       /* verify after programming?  */
                        BYTE CopyEmptyTest;    /* blank test before copying? */
                        BYTE CopyVerify;       /* verify after copying?      */
                        BYTE EraseEmptyTest;   /* blank test after erasing?  */
                        BYTE JedecSecBit;      /* set security bit in JEDEC? */
                        BYTE JedecFuseChk;     /* calc. fuse checksum?       */
                        BYTE JedecFileChk;     /* calc. file checksum?       */
                        BYTE HardwareVersion;  /* used hardware version      */
                        BYTE EdWBToFront;      /* WB to front when start. ED */
                        char EditorStr[EDITOR_STR_SIZE];
                      };


                                        /* this structure is used to store */
                                        /* the fuses in a kind of JEDEC    */
                                        /* format                          */

struct  JedecStruct { BYTE GALLogic[5808];      /*max. size of fuse matrix */
                      BYTE GALXOR  [10];        /* XOR bits                */
                      BYTE GALSig  [64];        /* signature               */
                      BYTE GALAC1  [8];         /* AC1 bits                */
                      BYTE GALPT   [64];        /* product term disable    */
                      BYTE GALSYN;              /* SYN bit                 */
                      BYTE GALAC0;              /* AC0 bit                 */
                      BYTE GALS1   [10];        /* S1 bits for 22V10       */
                    };
                    

                                        /* used to store infos about a pin */
struct  Pin             { BYTE p_Neg;        /* polarity of pin */
                          BYTE p_Pin;        /* pin number      */
                        };

                                        /* used to store infos about an OLMC */

struct  GAL_OLMC        { BYTE Active;       /* output's polarity           */
                          BYTE PinType;      /* type of pin (input,...)     */
                          BYTE TriCon;       /* user def. tristate control? */
                          BYTE Clock;        /* user def. clock equation?   */
                          BYTE ARST;         /* user def. ARST equation?    */
                          BYTE APRST;        /* user def. APRST equation?   */
                          BYTE FeedBack; };  /* is there a feedback?        */


                                        /* this structure is used to store  */
                                        /* some datas in a chained list     */
                                        /* e.g. the coded equations for the */
                                        /* optimizer                        */

struct  Buffer  { struct Buffer *Next;
                  struct Buffer *Prev;
                  UBYTE  Entries[ENTRY_SIZE];   /* data area */
                };
                                                    
                                        /* used to store results and     */
                                        /* parameters of functions       */
                                        /* which deal with chained lists */

struct  ActBuffer  { struct Buffer *ThisBuff;  /* pointer to current buffer */
                     UBYTE       *Entry;       /* pointer to data area      */
                     UBYTE       *BuffEnd;     /* pointer to the end of the */
                   };                          /* buffer                    */






/*************************** function declartions ****************************/


/* Port.asm */
void    InitHardware(void);
void    EnableOutput(void);
void    DisableOutput(void);
void    InitParPort(void);
void    RestoreParPort(void);
void    WriteByte(int, int);
int     ReadByte(int);
void    VeditOn(void);
void    VeditOff(void);
void    LED(int);
void    EnableVcc(void);
void    DisableVcc(void);
void    EnableVEdit(void);
void    DisableVEdit(void);
void    SetRow(int);
void    SDIn(int);
int     SDOut(void);
void    Clock(void);
void    STRImpuls(long);
void    EditMode(int);
void    ExitEditMode(void);
void    SetPV(int);
void    SetVolt(int);
void    SetGAL(int);
void    SetPESSAVE(int);
void    SetERASE(int);
void    SetCLR(int);
void    SetBE(int);
void    SetARCH(int);
void    SetANDBE(int);


/* GAL.c */
void  ProgramGAL(void);
void  CopyGAL(void);
int   CheckGAL(void);
void  TestSecurity(void);
void  Compare(void);
void  PrintACW(void);
void  PrintSignature(void);
int   ProgJedecToGAL(int mode);
void  ReadGALToJedec(void);
void  SetSecurity(int flag);
void  Loeschen(void);
void  EraseIt(void);
void  Leertest(void);


/*GALasm .c */
int   AssembleInputFile(int OpMode);
void  SetAND(int row, int pinnum, int negation);
void  IsPinName(UBYTE *pinnames, int numofpins);
int   GetNextChar(void);
int   GetNextLine(void);
void  AsmError(int errornum, int pinnum);
void  WriteChipFile(void);
void  WritePinFile(void);
void  WriteFuseFile(void);
void  WriteSpaces(FILE *fp, int numof);
void  WriteRow(FILE *fp, int row);
int   IsOR(char);
int   IsAND(char);
int   IsNEG(char);
void  Is_AR_SP(UBYTE *ptr);


/* Optimizer.c */
int  Optimizer(void);
struct ActBuffer GetProdTermStart(struct ActBuffer pos);
void  KillProdTerm(struct ActBuffer term);
int   OptimizeEqua(struct ActBuffer buff);
int   Resolvente(struct ActBuffer optstart, struct ActBuffer optend, struct ActBuffer alteSchicht);
int   NumOfVar(struct ActBuffer term);
int   AddResolvente(struct ActBuffer pos1, struct ActBuffer pos2, struct ActBuffer equastart, struct ActBuffer *equaend);
int   SearchVar(struct ActBuffer *, struct ActBuffer, UBYTE);
int   WriteNewSource(struct ActBuffer buff, UBYTE *equastart, UBYTE *equaend);
int   CopyEqua(struct ActBuffer from_buff, struct ActBuffer to_buff);
int   PrintEqua(struct ActBuffer buff, struct ActBuffer *filebuff);
int   GetNext(struct ActBuffer *buff);
int   GetNextEqua(struct ActBuffer *buff);
void  PrintOptText(UBYTE *txt);
void  ScrollRequester(void);
int   TranslateEqua(struct ActBuffer *buff);


/* Pruefer.c */
void  Pruefer(struct Gadget *gad, USHORT gadID);
void  SetLevel(USHORT gadID, int level);
void  CheckOutput(void);
void  TogglePruefer(void);
void  SetSymbolGAL(int type);
void  PrintPinNames(void);
void  ClearPinNames(void);


/* GAL2.c */
UBYTE GetByte(UBYTE *ptr);
int   ReadGALParameter(int checkflag);
void  GALInfo(void);
int   GALAType(void);


/* Sub.c */
void  Schreibverhalten(void);
void  JedecRequester(void);
void  About(void);
int   mystrcmp(char *cptr1, char *cptr2);
int   MyFileReq(char *headtxt, char *showtxt, int showreq, int mode);
int   MyRequest(int mode, UBYTE *text);
int   AsmRequester(void);
long  FileSize(UBYTE *filename);
int   ReadFile(UBYTE *filename, LONG filesize, UBYTE *filebuff);
void  ErrorReq(int errornum);
void  PrintText(UBYTE *text, int newline);
void  PrintErrorLine(int line);
void  LoadConfig(void);
void  SaveConfig(void);
void  SetGALType(int galtype);
int   AddByte(struct ActBuffer *buff, UBYTE code);
int   AddString(struct ActBuffer *buff, UBYTE *strnptr);
void  IncPointer(struct ActBuffer *buff);
void  DecPointer(struct ActBuffer *buff);
void  FreeBuffer(struct Buffer *buff);
void  SelectEditor(void);
void  CallEditor(void);
void  WaitForTimer(ULONG micro);
 

/* Reasm.c */
int    CheckPinNames(void);
int    PinNameRequester(int galtype);
int    RowActive(int row, int galtype);
int    OLMCOutput(int olmc, int mode);
int    MakeEquation(struct ActBuffer *buff, int olmc, int colsize,
                    int mode, int rowoffset, int numofrows);
int    Reasm(struct ActBuffer buff);
void   Reassembler(void);
void   ReasmError(int errornum, int pinnum);
UBYTE *PinNamePtr(int pinnum, struct Gadget *gadget);



/* Jedec.c */
void  PutJedec(void);
int   FileChecksum(struct ActBuffer buff);
int   FuseChecksum(int galtype);
int   MakeJedecBuff(struct ActBuffer buff, int galtype);
void  WriteJedecFile(int galtype);
int   SearchNextChar(void);
int   SearchNextAsterix(void);
int   GetJedec(UBYTE *jedecfile);
void  JedecError(int errornum, int flag);


/* Localize.c */
void  LocalizeText(void);


/* Help.c */
void  HelpOnMainMenu(UWORD menuNumber);
void  HelpOnCheckerMenu(UWORD menuNumber);
void  Help(void);

