home *** CD-ROM | disk | FTP | other *** search
/ Meeting Pearls 1 / Meeting Pearls Vol 1 (1994).iso / installed_progs / hard / galer / source / src.lha / Source / GALer / JEDEC.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-11-10  |  25.8 KB  |  1,219 lines

  1. /****************************************************************/
  2. /*                                */
  3. /* Jedec.c - this file includes routines to load and save    */
  4. /*         GAL datas in JEDEC format                */
  5. /*                                 */
  6. /*                                */
  7. /****************************************************************/
  8.  
  9.  
  10.  
  11. #include <exec/types.h>
  12. #include <exec/memory.h>
  13. #include <libraries/dos.h>
  14. #include <libraries/locale.h>
  15. #include <stdio.h>
  16. #include <ctype.h>
  17. #include <stdio.h>
  18.  
  19. #include <proto/locale.h>
  20. #include <proto/dos.h>
  21. #include <proto/intuition.h>
  22. #include <proto/exec.h>
  23.  
  24. #include "GALer.h"
  25. #include "Localize.h"
  26.  
  27.  
  28.  
  29.  
  30. extern    struct  AppString { LONG   as_ID;
  31.                  STRPTR as_Str;
  32.               };
  33.  
  34. extern    struct  AppString AppStrings[];
  35.  
  36. extern    int    MaxFuseAdr, RowSize, XORSize, GALType;
  37. extern    char    path;
  38.  
  39. extern    struct    Configuration    Config;
  40. extern    struct    Catalog        *catalog;
  41.  
  42. struct    JedecStruct    Jedec;
  43.  
  44.  
  45. int    linenum, JedecSecurity, JedecGALType;
  46. int    oldMaxFuseAdr, oldRowSize, oldXORSize;
  47.  
  48. UBYTE    *actptr, *buffend;
  49.  
  50.  
  51.  
  52.  
  53. /* PutJedec:
  54.    read GAL and generate JEDEC structure and write JEDEC file
  55.    Syntax: PutJedec();
  56. */
  57. void PutJedec(void)
  58. {
  59.  if (MyRequest(GALTYPE_REQ, AppStrings[MSG_READGAL_JED].as_Str)) {
  60.      if (ReadGALParameter(YES))            /* get parameter or GAL */
  61.        return;                    /* error -> return */
  62.  
  63.      PrintText(AppStrings[MSG_READING_GAL].as_Str, 1);
  64.  
  65.      ReadGALToJedec();                    /*read GAL*/
  66.  
  67.      PrintText(AppStrings[MSG_LOWER_OK].as_Str, 0);
  68.  
  69.      if (MyFileReq(AppStrings[MSG_WRITE_JED].as_Str, ".jed", YES, SAVE))
  70.        WriteJedecFile(GALType);
  71.  
  72.   }
  73. }
  74.  
  75.  
  76.  
  77.  
  78.  
  79.  
  80. /* FileChecksum: calculate JEDEC-File-Checksum
  81.          start and end must be marked by <STX> and <ETX>!
  82.    Syntax: checksum = FileChecksum(buff);
  83.        buff: ActBuffer structure of file buffer
  84.    Result: checksum: 16-Bit checksum of JEDEC structure
  85. */
  86. int FileChecksum(struct ActBuffer buff)
  87. {
  88. int checksum;
  89.  
  90.   checksum = 0;
  91.  
  92.   while (*buff.Entry != 0x2)            /* search for <STX> */
  93.     IncPointer(&buff);
  94.  
  95.   while (*buff.Entry != 0x3) {            /* search for <ETX> and*/
  96.     checksum += *buff.Entry;            /* add values */
  97.  
  98.     IncPointer(&buff);
  99.   }
  100.  
  101.   checksum += 0x3;                /* add <ETX> too*/
  102.  
  103.   return(checksum);                /* ready */
  104. }
  105.  
  106.  
  107.  
  108.  
  109. /* FuseChecksum: calculate Fuse-Checksum of the JEDEC structure 
  110.    Syntax: checksum = FuseChecksum(galtype);
  111.        int galtype: type of GAL
  112.    Result: checksum: 16 bit checksum of JEDEC structure
  113. */
  114. int FuseChecksum(int galtype)
  115. {
  116. int    checksum, byte, n;
  117. BYTE    *ptr, *ptrXOR, *ptrS1;
  118.  
  119.  
  120.  
  121.   ptr = &Jedec.GALLogic[0] - 1L;
  122.   ptrXOR = &Jedec.GALXOR[0];
  123.   ptrS1  = &Jedec.GALS1[0];
  124.  
  125.   n = checksum = byte = 0;
  126.  
  127.   for (;;) {
  128.  
  129.     if (galtype == GAL16V8) {
  130.       if (n == XOR16) {
  131.         ptr = &Jedec.GALXOR[0];
  132.       }
  133.       else {
  134.     if (n == XOR16+8) {
  135.       ptr = &Jedec.GALSig[0];
  136.     }
  137.     else {
  138.       if (n == NUMOFFUSES16)
  139.         break;
  140.       else
  141.         ptr++;
  142.     }
  143.       }
  144.     }
  145.  
  146.  
  147.     if (galtype == GAL20V8) {
  148.       if (n == XOR20) {
  149.         ptr = &Jedec.GALXOR[0];
  150.       }
  151.       else {
  152.     if (n == XOR20+8) {
  153.       ptr = &Jedec.GALSig[0];
  154.     }
  155.     else {
  156.       if (n == NUMOFFUSES20)
  157.         break;
  158.       else
  159.         ptr++;
  160.     }
  161.       }
  162.     }
  163.  
  164.  
  165.     if (galtype == GAL22V10) {
  166.       if ((n >= XOR22V10) && (n < XOR22V10 + 20)) {
  167.     if (!(n % 2))
  168.       ptr = ++ptrXOR;
  169.     else
  170.       ptr = ++ptrS1;
  171.       }
  172.       else {
  173.     if (n == SIG22V10)
  174.       ptr = &Jedec.GALSig[0] - 1L;
  175.  
  176.     if (n == SIG22V10 + SIG_SIZE)
  177.       break;
  178.     else
  179.       ptr++;
  180.       }
  181.     }
  182.  
  183.  
  184.     if (galtype == GAL20RA10) {
  185.       if (n == XOR20RA10) {
  186.         ptr = &Jedec.GALXOR[0];
  187.       }
  188.       else {
  189.     if (n == SIG20RA10 + SIG_SIZE)
  190.       break;
  191.     else
  192.       ptr++;
  193.       }
  194.     }
  195.  
  196.  
  197.     byte |= (*ptr << (n+8) % 8);
  198.  
  199.     if (!((n+9) % 8)) {
  200.       checksum += byte;
  201.       byte = 0;
  202.     }
  203.  
  204.     n++;
  205.   }
  206.  
  207.   checksum += byte;
  208.  
  209.   return(checksum); 
  210. }
  211.  
  212.  
  213.  
  214.  
  215.  
  216. /* MakeJedecBuff: generate the JEDEC file in a ram-buffer
  217.    Syntax:   result = MakeJedecBuff(buff);
  218.             buff:    ActBuffer structure for the ram-buffer
  219.    Result:  int result:  0: o.k.
  220.              -1: not enough free memory
  221. */
  222. int MakeJedecBuff(struct ActBuffer buff, int galtype)
  223. {
  224. UBYTE    mystrng[10];
  225. struct    ActBuffer buff2;
  226. int    n, m, bitnum, bitnum2, flag;
  227. int    MaxFuseAdr, RowSize, XORSize;
  228.  
  229.  
  230.   switch (galtype) {
  231.  
  232.     case  GAL16V8:
  233.     MaxFuseAdr = MAX_FUSE_ADR16;        /* This variables are defined  */
  234.     RowSize    = ROW_SIZE_16V8;        /* both globally AND locally!  */
  235.     XORSize    = 8;                /* All assignments concern to  */
  236.     break;                    /* the locale variables!!!!    */
  237.  
  238.     case GAL20V8:
  239.     MaxFuseAdr = MAX_FUSE_ADR20;
  240.     RowSize    = ROW_SIZE_20V8;
  241.     XORSize    = 8;
  242.     break;
  243.  
  244.     case GAL22V10:
  245.     MaxFuseAdr = MAX_FUSE_ADR22V10;
  246.     RowSize    = ROW_SIZE_22V10;
  247.     XORSize    = 10;
  248.     break;
  249.  
  250.  
  251.     case GAL20RA10:
  252.     MaxFuseAdr = MAX_FUSE_ADR20RA10;
  253.     RowSize    = ROW_SIZE_20RA10;
  254.     XORSize    = 10;
  255.     break;
  256.  
  257.   }
  258.  
  259.  
  260.  
  261.  
  262.   buff2 = buff;
  263.  
  264.   if (Config.JedecFileChk)
  265.     if (AddString(&buff, (UBYTE *)"\2\n"))            /*<STX>*/
  266.       return(-1);
  267.  
  268.                     /*** make header of JEDEC file ***/
  269.   if (AddString(&buff, (UBYTE *)"Used Program:   GALer V1.5\n"))
  270.     return(-1);
  271.  
  272.   if (AddString(&buff, (UBYTE *)"GAL-Assembler:  GALer V1.5\n"))
  273.     return(-1);
  274.  
  275.   if (galtype == GAL16V8) {
  276.     if (AddString(&buff, (UBYTE *)"Device:         GAL16V8\n\n"))
  277.       return(-1);
  278.   }
  279.   if (galtype == GAL20V8) {
  280.     if (AddString(&buff, (UBYTE *)"Device:         GAL20V8\n\n"))
  281.       return(-1);
  282.   }
  283.   if (galtype == GAL20RA10) {
  284.     if (AddString(&buff, (UBYTE *)"Device:         GAL20RA10\n\n"))
  285.       return(-1);
  286.   }
  287.   if (galtype == GAL22V10) {
  288.     if (AddString(&buff, (UBYTE *)"Device:         GAL22V10\n\n"))
  289.       return(-1);
  290.   }
  291.  
  292.  
  293.   if (AddString(&buff, (UBYTE *)"*F0\n"))    /* default value of fuses */
  294.     return(-1);
  295.  
  296.   if (Config.JedecSecBit) {            /* Security-Bit */
  297.     if (AddString(&buff, (UBYTE *)"*G1\n"))
  298.       return(-1);
  299.   }
  300.   else
  301.     if (AddString(&buff, (UBYTE *)"*G0\n"))
  302.       return(-1);
  303.  
  304.  
  305.   if (galtype == GAL16V8)            /* number of fuses */
  306.     if (AddString(&buff, (UBYTE *)"*QF2194\n"))
  307.       return(-1);
  308.  
  309.   if (galtype == GAL20V8)
  310.     if (AddString(&buff, (UBYTE *)"*QF2706\n"))
  311.       return(-1);
  312.       
  313.   if (galtype == GAL20RA10)
  314.     if (AddString(&buff, (UBYTE *)"*QF3274\n"))
  315.       return(-1);
  316.  
  317.   if (galtype == GAL22V10)
  318.     if (AddString(&buff, (UBYTE *)"*QF5892\n"))
  319.       return(-1);
  320.  
  321.                         /*** make fuse-matrix ***/
  322.  
  323.   bitnum = 0;
  324.  
  325.   for (m = 0; m < RowSize; m++) {
  326.     flag = 0;
  327.  
  328.     bitnum2 = bitnum;
  329.  
  330.     for (n = 0; n <= MaxFuseAdr; n++) {
  331.       if (Jedec.GALLogic[bitnum2]) {
  332.         flag = 1;
  333.         break;
  334.       }
  335.  
  336.       bitnum2++;
  337.     }
  338.  
  339.     if (flag) {
  340.       sprintf((char *)&mystrng[0], "*L%04d \0", bitnum);
  341.  
  342.       if (AddString(&buff, (UBYTE *)&mystrng[0]))
  343.         return(-1);
  344.  
  345.       for (n=0; n<=MaxFuseAdr; n++) {
  346.         if (AddByte(&buff, (UBYTE)(Jedec.GALLogic[bitnum] + '0')))
  347.           return(-1);
  348.         bitnum++;
  349.       }
  350.  
  351.       if (AddByte(&buff, (UBYTE)'\n'))
  352.         return(-1);
  353.  
  354.     }
  355.     else
  356.       bitnum = bitnum2;
  357.   }
  358.  
  359.   if (!flag)
  360.     bitnum = bitnum2;
  361.  
  362.  
  363.                         /*** XOR-Bits ***/
  364.   sprintf((char *)&mystrng[0], "*L%04d \0", bitnum);    /* add fuse adr. */
  365.   if (AddString(&buff, (UBYTE *)&mystrng[0]))
  366.     return(-1);
  367.  
  368.   for (n = 0; n < XORSize; n++) {
  369.     if (AddByte(&buff, (UBYTE)(Jedec.GALXOR[n] + '0')))
  370.       return(-1);
  371.     bitnum++;
  372.  
  373.     if (galtype == GAL22V10) {            /*** S1 of 22V10 ***/
  374.         if (AddByte(&buff, (UBYTE)(Jedec.GALS1[n] + '0')))
  375.           return(-1);
  376.         bitnum++;
  377.     }
  378.   }
  379.  
  380.   if (AddByte(&buff, (UBYTE)'\n'))
  381.     return(-1);
  382.  
  383.  
  384.                         /*** Signature ***/
  385.   sprintf((char *)&mystrng[0], "*L%04d \0", bitnum);
  386.  
  387.   if (AddString(&buff, (UBYTE *)&mystrng[0]))
  388.     return(-1);
  389.  
  390.   for (n = 0; n < SIG_SIZE; n++) {
  391.     if (AddByte(&buff, (UBYTE)(Jedec.GALSig[n] + '0')))
  392.       return(-1);
  393.     bitnum++;
  394.   }
  395.  
  396.   if (AddByte(&buff, (UBYTE)'\n'))
  397.     return(-1);
  398.  
  399.  
  400.  
  401.   if ((galtype == GAL16V8) || (GALType == GAL20V8)) {
  402.                             /*** AC1-Bits ***/
  403.     sprintf((char *)&mystrng[0], "*L%04d \0", bitnum);
  404.     if (AddString(&buff, (UBYTE *)&mystrng[0]))
  405.       return(-1);
  406.  
  407.     for (n = 0; n < AC1_SIZE; n++) {
  408.       if (AddByte(&buff, (UBYTE)(Jedec.GALAC1[n] + '0')))
  409.         return(-1);
  410.       bitnum++;
  411.     }
  412.  
  413.     if (AddByte(&buff, (UBYTE)'\n'))
  414.       return(-1);
  415.  
  416.  
  417.                             /*** PT-Bits ***/
  418.     sprintf((char *)&mystrng[0], "*L%04d \0", bitnum);
  419.     if (AddString(&buff, (UBYTE *)&mystrng[0]))
  420.       return(-1);
  421.  
  422.     for (n = 0; n < PT_SIZE; n++) {
  423.       if (AddByte(&buff, (UBYTE)(Jedec.GALPT[n] + '0')))
  424.         return(-1);
  425.       bitnum++;
  426.     }
  427.  
  428.     if (AddByte(&buff, (UBYTE)'\n'))
  429.       return(-1);
  430.  
  431.  
  432.                             /*** SYN-Bit ***/
  433.     sprintf((char *)&mystrng[0], "*L%04d \0", bitnum);
  434.  
  435.     if (AddString(&buff, (UBYTE *)&mystrng[0]))
  436.       return(-1);
  437.  
  438.     if (AddByte(&buff, (UBYTE)(Jedec.GALSYN + '0')))
  439.       return(-1);
  440.  
  441.     if (AddByte(&buff, (UBYTE)'\n'))
  442.       return(-1);
  443.  
  444.     bitnum++;
  445.  
  446.  
  447.                             /*** AC0-Bit ***/
  448.     sprintf((char *)&mystrng[0], "*L%04d \0", bitnum);
  449.  
  450.     if (AddString(&buff, (UBYTE *)&mystrng[0]))
  451.       return(-1);
  452.  
  453.     if (AddByte(&buff, (UBYTE)(Jedec.GALAC0 + '0')))
  454.       return(-1);
  455.  
  456.     if (AddByte(&buff, (UBYTE)'\n'))
  457.       return(-1);
  458.  
  459.   }
  460.  
  461.  
  462.  
  463.   if (Config.JedecFuseChk) {            /* add fuse-checksum */
  464.     sprintf((char *)&mystrng[0], "*C%04x\n\0", FuseChecksum(GALType));
  465.     if (AddString(&buff, (UBYTE *)&mystrng[0]))
  466.       return(-1);
  467.    }
  468.  
  469.  
  470.   if (AddString(&buff, (UBYTE *)"*\n"))            /* closing '*' */
  471.     return(-1);
  472.  
  473.  
  474.   if (Config.JedecFileChk) {
  475.     if (AddByte(&buff, (UBYTE)0x3))            /* <ETX> */
  476.       return(-1);
  477.     sprintf((char *)&mystrng[0], "%04x\n\0", FileChecksum(buff2));
  478.     if (AddString(&buff, (UBYTE *)&mystrng[0]))
  479.       return(-1);
  480.   }
  481.  
  482.  
  483.   return(0);
  484. }
  485.  
  486.  
  487.  
  488.  
  489. /* WriteJedecFile:   write JEDEC file
  490.    Syntax:  WriteJedecFile(galtype);
  491.           int galtype: type of GAL
  492.  
  493. */
  494. void WriteJedecFile(int galtype)
  495. {
  496. struct    ActBuffer    mybuff;
  497. struct    Buffer        *first_buff;
  498. BPTR    fh;
  499. extern    char    path;
  500. UBYTE    *filebuffer, *filebuffer2;
  501. long    result;
  502.  
  503.  
  504.  
  505.   if (!(first_buff = (struct Buffer *)AllocMem((long)sizeof(struct Buffer),MEMF_PUBLIC|MEMF_CLEAR))) {
  506.     ErrorReq(2);                /* out of memory? */
  507.     return;
  508.   }
  509.  
  510.   mybuff.ThisBuff = first_buff;
  511.   mybuff.Entry    = (UBYTE *)(&first_buff->Entries[0]);
  512.   mybuff.BuffEnd  = (UBYTE *)first_buff + (long)sizeof(struct Buffer);
  513.  
  514.  
  515.  
  516.   if (MakeJedecBuff(mybuff, galtype)) {        /* put JEDEC in ram-buffer */
  517.     FreeBuffer(first_buff);            /* error? */
  518.     ErrorReq(2);
  519.     return;
  520.   }
  521.  
  522.   PrintText(AppStrings[MSG_WRITING_JED].as_Str, 1);
  523.  
  524.   if ((fh = Open(&path, (long)MODE_NEWFILE))) {
  525.     for (;;) {
  526.       filebuffer = filebuffer2 = mybuff.Entry;
  527.  
  528.       while (filebuffer2 < mybuff.BuffEnd) {    /* get size of buffer */
  529.         if (!*filebuffer2) 
  530.       break;
  531.         filebuffer2++;
  532.       }
  533.                         /* save buffer */
  534.       result = Write(fh, (char *)filebuffer, (long)(filebuffer2-filebuffer));
  535.  
  536.       if (result == -1L) {            /* write error? */
  537.         Close(fh);
  538.         FreeBuffer(first_buff);
  539.         PrintText(AppStrings[MSG_ERROR].as_Str ,0);
  540.         ErrorReq(13);
  541.         return;
  542.       }
  543.  
  544.       if (!mybuff.ThisBuff->Next)        /* more buffers here? */
  545.         break;                    /* no, then cancel */
  546.  
  547.       mybuff.ThisBuff = mybuff.ThisBuff->Next;
  548.       mybuff.Entry    = (UBYTE *)(&mybuff.ThisBuff->Entries[0]);
  549.       mybuff.BuffEnd  = (UBYTE *)mybuff.ThisBuff + (long)sizeof(struct Buffer);
  550.     }
  551.  
  552.     Close(fh);
  553.   }
  554.   else {
  555.     FreeBuffer(first_buff);            /* error?, then cancel*/
  556.     PrintText(AppStrings[MSG_ERROR].as_Str ,0);
  557.     ErrorReq(13);                /* can't open file */
  558.     return;
  559.   }
  560.  
  561.   PrintText(AppStrings[MSG_LOWER_OK].as_Str, 0);
  562.   FreeBuffer(first_buff);
  563. }
  564.  
  565.  
  566.  
  567.  
  568.  
  569. /* SearchNextChar: increment pointer to next character which is no TAB,
  570.            SPACE, CR ist - start searching at actptr + 1
  571.    Syntax:   result = SearchNextChar();
  572.    Result:   result = 0: no character found
  573.             =-1: no more characters (end of file)
  574. */
  575. int SearchNextChar(void)
  576. {
  577.  
  578.   actptr++;
  579.   while (*actptr) {
  580.     switch (*actptr) {
  581.       case  ' ':
  582.  
  583.       case 0x09: break;
  584.  
  585.       case 0x0A: linenum++;
  586.          break;
  587.       default  : return(0);
  588.     }
  589.     actptr++;
  590.   } 
  591.   return(-1);
  592.  
  593. }
  594.  
  595.  
  596. /* SearchNextAsterix: search for '*' up to the end of the file - starts
  597.               search at actptr
  598.    Syntax:   result = SearchNextAsterix();
  599.    Result:   result = 0: found a '*'
  600.             =-1: no more character (end of file)
  601. */
  602. int SearchNextAsterix(void)
  603. {
  604.   while (*actptr) {
  605.     if (*actptr == '*')
  606.       return(0);
  607.     if (*actptr == 0x0A)
  608.       linenum++;
  609.     actptr++;
  610.    }
  611.   return(-1);
  612. }
  613.  
  614.  
  615.  
  616. /* GetJedec:
  617.    read JEDEC file, check it and generate Jedec-structure
  618.  
  619.    Syntax: error=GetJedec(jedecfile);
  620.        UBYTE* jedecfile  : pointer to name of file (".jed")
  621.    Result: error: >0 : error
  622.                    0 : no error 
  623. */
  624. int GetJedec(UBYTE *jedecfile)
  625. {
  626. extern    char    path;
  627. LONG    filesize;
  628. UBYTE    *filebuff, *ptr, *jedbuff, *jedptr;
  629. int    n;
  630. int    QFflag, QPflag, Fflag, Gflag, Cflag;
  631. int    Lflag, STXflag, ETXflag, FCHKflag;
  632. int    errornum, fuse_chk, file_chk;
  633. unsigned int    num, fuse;
  634. unsigned int    QF, QP, F;
  635.  
  636.  
  637.   JedecGALType = NOT_SPECIFIED;
  638.  
  639.   filesize = FileSize(jedecfile);
  640.   switch (filesize) {
  641.     case -1L:
  642.       ErrorReq(1);
  643.       return(-1);
  644.       break;
  645.  
  646.    case -2L:
  647.      ErrorReq(2);
  648.      return(-2);
  649.      break;
  650.  
  651.    case 0L:
  652.      ErrorReq(4);
  653.      return(-4);
  654.      break;
  655.   }
  656.  
  657.  
  658.  if ((filebuff = (UBYTE *)AllocMem(filesize+2L, MEMF_PUBLIC|MEMF_CLEAR))) {
  659.  
  660.    if ((ReadFile(jedecfile, filesize, filebuff))) {
  661.  
  662.      PrintText(AppStrings[MSG_FILE_LOADED].as_Str, 1);
  663.      actptr = filebuff;                /* initialize variables */
  664.      linenum  = 1;
  665.      errornum = 0;
  666.      fuse     = 0;
  667.      QFflag = QPflag = Fflag = Gflag = Cflag = Lflag = STXflag = ETXflag = 0;
  668.      FCHKflag = 0;
  669.  
  670.      if (!(jedbuff = (UBYTE *)AllocMem((long)sizeof(Jedec), MEMF_PUBLIC|MEMF_CLEAR))) {
  671.        FreeMem(filebuff, filesize);
  672.        ErrorReq(2);                /* no more free memory */
  673.        return(-2);
  674.      }
  675.      jedptr = jedbuff;
  676.  
  677.  
  678.      if (SearchNextAsterix()) {            /* point to first instruction */
  679.        FreeMem(jedbuff, (long)sizeof(Jedec));    /* buffer for Jedec-structure */
  680.        FreeMem(filebuff, filesize);
  681.        JedecError(1, YES);            /* unexpected end of file*/
  682.        return(1);
  683.      }
  684.  
  685.  
  686.      for (;;) {
  687.        if (SearchNextChar())            /*is there any character left?*/
  688.      break;                    /*no, then end this loop      */
  689.  
  690.        switch (*actptr) {
  691.      case 'Q':            /*instructions introduced by Q*/
  692.        actptr++;
  693.        if (*actptr == 'F') {    /*number of fuses in this file*/
  694.          if (QFflag) {            /*Befehl darf innerhalb des*/
  695.            errornum = 7;            /*Jedec-Files nur einmal*/
  696.            break;                /*vorkommen*/
  697.          }
  698.          actptr++;
  699.  
  700.          if (!isdigit(*actptr)) {        /*is there a number?*/
  701.            errornum = 4;            /*no, then error*/
  702.            break;
  703.          }
  704.  
  705.          if (!sscanf((char *)actptr, "%d", &QF)) {
  706.            errornum = 3;
  707.            break;
  708.          }
  709.  
  710.          while (isdigit(*actptr))        /* skip number */
  711.            actptr++;
  712.  
  713.          QFflag = 1;
  714.  
  715.          if ((QF != 2194) && (QF != 2706) && (QF != 3274) && (QF != 5892)) {
  716.            errornum = 22;            /* unknown QF, then error */
  717.            break;
  718.          }
  719.  
  720.          if (QF == 2194)            /* get out of *QF type of GAL */
  721.            JedecGALType = GAL16V8;
  722.  
  723.          if (QF == 2706)
  724.            JedecGALType = GAL20V8;
  725.  
  726.          if (QF == 5892)
  727.            JedecGALType = GAL22V10;
  728.  
  729.          if (QF == 3274)
  730.            JedecGALType = GAL20RA10;
  731.  
  732.          break;
  733.        }
  734.  
  735.        if (*actptr == 'P') {        /* number of pins */
  736.          if (QPflag) {            /*this instructions is allowed*/
  737.            errornum = 8;            /*just once*/
  738.            break;
  739.          }
  740.          actptr++;
  741.  
  742.          if (!isdigit(*actptr)) {        /*is there a number?*/
  743.            errornum = 4;            /*no, then error*/
  744.            break;
  745.          }
  746.  
  747.          if (!sscanf((char *)actptr, "%d", &QP)) {
  748.            errornum = 3;
  749.            break;
  750.          }
  751.  
  752.          while (isdigit(*actptr))        /*skip number*/
  753.            actptr++;
  754.  
  755.          QPflag = 1;
  756.          break;
  757.        }
  758.        errornum = 2;            /*no QF,QP, then error*/
  759.        break;
  760.  
  761.  
  762.      case 'F':            /*** Fuse-Default ***/
  763.        actptr++;
  764.        if (*actptr == '0')
  765.          F = 0;
  766.        else
  767.          if (*actptr == '1')
  768.            F = 1;
  769.          else {                /*no 0 or 1 after F*/
  770.            errornum = 5;            /*then error*/
  771.            break;
  772.          }
  773.  
  774.        for (n=fuse; n<sizeof(Jedec); n++)    /*default value*/
  775.          *(jedbuff + (long)n) = F;
  776.        Fflag = 1;
  777.        break;
  778.  
  779.  
  780.      case 'P':                /*pins for test vectors*/
  781.      case 'V':                /*test vector*/
  782.      case 'N':                /*comment*/
  783.        break;                /*do nothing*/
  784.  
  785.  
  786.      case 'C':            /*** Fuse-Checksum ***/
  787.        if (Cflag) {                /* only one fuse-chk. allowed*/
  788.          errornum = 24;
  789.          break;
  790.        }
  791.        actptr++;
  792.  
  793.        if (!isxdigit(*actptr)) {        /*is there a hex. number?*/
  794.          errornum = 4;            /*no, then error*/
  795.          break;
  796.        }
  797.  
  798.        if (!sscanf((char *)actptr, "%x", &fuse_chk)) {
  799.          errornum = 3;
  800.          break;
  801.        }
  802.  
  803.        while (isxdigit(*actptr))        /* skip hex. number*/
  804.          actptr++;
  805.        Cflag = 1;
  806.        break;
  807.  
  808.  
  809.      case 'L':            /*** Fuse-Adresse ***/
  810.        if (Cflag) {                /*after *C no *L allowed*/
  811.          errornum = 10;
  812.          break;
  813.        }
  814.        actptr++;
  815.  
  816.        if (!isdigit(*actptr)) {        /*is there a dez. number?*/
  817.          errornum = 4;            /*no, then error*/
  818.          break;
  819.         }
  820.  
  821.        if (!sscanf((char *)actptr, "%d", &num)) {
  822.          errornum = 3;
  823.          break;
  824.         }
  825.  
  826.        while (isdigit(*actptr))        /*skip dez. number*/
  827.          actptr++;
  828.  
  829.        if (num < fuse) {            /* is fuse adr. too low?*/
  830.          errornum = 17;
  831.          break;
  832.        }
  833.  
  834.        if ((num > fuse) && !Fflag) {    /* skip fuse address */
  835.          errornum = 18;            /* was there a *F? */
  836.          break;                /* no, then error  */
  837.        }
  838.  
  839.        if (SearchNextChar()) {        /*no more characters?*/
  840.          errornum = 1;            /*then unexpected end of file*/
  841.          break;
  842.        }
  843.  
  844.        fuse = num;
  845.        for (;;) {                /*examine fuses*/
  846.          if ((*actptr == '1') || (*actptr == '0')) {
  847.            *(jedbuff + (long)fuse) = *actptr - '0';
  848.          }
  849.          else {
  850.            if (*actptr == '*')        /*next instruction?*/
  851.          break;                /*yes, then ready*/
  852.            else {                /*no, then error '*' expected*/
  853.          errornum = 19;
  854.          break;
  855.            }
  856.          }
  857.  
  858.          if (SearchNextChar()) {        /*no more characters?*/
  859.            errornum = 1;            /*then unexpected end of file*/
  860.            break;
  861.          }
  862.          fuse++;
  863.  
  864.        }
  865.        Lflag = 1;
  866.        break;
  867.  
  868.  
  869.      case 'G':            /*** Security ***/
  870.        if (QPflag) {            /*this instruction my appear*/
  871.          errornum = 8;            /*just one time in the file */
  872.          break;
  873.        }
  874.        actptr++;
  875.  
  876.        if (*actptr == '0')
  877.          JedecSecurity = 0;
  878.        else
  879.          if (*actptr == '1')
  880.            JedecSecurity = 1;
  881.          else {                /*no 0 nor 1 after G?*/
  882.            errornum = 5;            /*then error*/
  883.            break;
  884.          }
  885.        Gflag = 1;
  886.        break;
  887.  
  888.  
  889.      case 0x02:            /*** <STX> ***/
  890.        break;
  891.  
  892.  
  893.      case 0x03:            /*** <ETX> then File-Checksum ***/
  894.        ptr = filebuff;
  895.        file_chk = 0;
  896.        while (*ptr) {
  897.          if (*ptr == 0x02) {            /* multiple <STX> */
  898.            if (STXflag) {
  899.          errornum = 12;
  900.          break;
  901.            }
  902.            if (ETXflag) {                /* STX before ETX? */
  903.          errornum = 14;
  904.          break;
  905.            }
  906.            STXflag = 1;
  907.           }
  908.  
  909.          if (*ptr == 0x03) {            /* <ETX> */
  910.            if (ETXflag) {                /* mulitple? */
  911.          errornum = 13;
  912.          break;
  913.            }
  914.            if (!STXflag) {                /* STX vor ETX? */
  915.          errornum = 14;
  916.          break;
  917.            }
  918.            ETXflag = 1;
  919.          }
  920.  
  921.          if (STXflag && !ETXflag)            /*add to checksum*/
  922.            file_chk += *ptr;
  923.          ptr++;
  924.        }
  925.        file_chk += 0x03;                /* add ETX */
  926.        actptr++;
  927.  
  928.        if (!isxdigit(*actptr)) {        /*is there a hex. number?*/
  929.          errornum = 4;            /*no, then error*/
  930.          break;
  931.        }
  932.  
  933.        if (!sscanf((char *)actptr, "%x", &num)) {
  934.          errornum = 3;
  935.          break;
  936.        }
  937.  
  938.        while (isxdigit(*actptr))            /*skip hex. number*/
  939.          actptr++;
  940.        if (file_chk != num) {
  941.          if (!MyRequest(CONT_REQ, AppStrings[MSG_BAD_FILECHK].as_Str)) {
  942.            errornum = 16;
  943.            break;
  944.          }
  945.        }
  946.        FCHKflag = 1;
  947.        break;
  948.        }
  949.  
  950.  
  951.        if (errornum) {            /*was there an error? yes, then end*/
  952.          FreeMem(jedbuff, (long)sizeof(Jedec));    /*free buffer of JEDEC structure*/
  953.      FreeMem(filebuff,filesize);        /*free buffer of JEDEC file*/
  954.      JedecError(errornum,YES);
  955.      return(errornum);
  956.        }
  957.  
  958.        if (FCHKflag)                /*File-Checksum?*/
  959.      break;                    /*yes, then break loop*/
  960.  
  961.        if (SearchNextAsterix())         /*search for final '*'*/
  962.      errornum = 9;
  963.  
  964.      }
  965.  
  966.  
  967.      if (!Lflag && !Fflag) {            /*no fuses?*/
  968.        FreeMem(filebuff, filesize);        /*then error*/
  969.        FreeMem(jedbuff, (long)sizeof(Jedec));    /*free buffer of JEDEC structure*/
  970.        JedecError(21, NO);
  971.        return(21);
  972.      }
  973.  
  974.      if (QFflag) {                /*does *L.. fit to *QF?*/
  975.        if (((fuse != QF) && !Fflag) || (fuse > QF)) {
  976.      FreeMem(filebuff, filesize);        /*no, then error*/
  977.          FreeMem(jedbuff, (long)sizeof(Jedec));    /*free buffer of JEDEC structure*/
  978.      JedecError(20, NO);
  979.      return(20);
  980.        }
  981.      }
  982.      else {
  983.        if (Fflag) {                /* try to identify the type of */
  984.      if ((fuse > 3274) && (fuse <= 5892))   /* GAL for which this JEDEC file */
  985.        JedecGALType = GAL20RA10;        /* is */
  986.  
  987.      if (fuse <= 2194)            /* less than 2194 fuses => 16V8 */
  988.        JedecGALType = GAL16V8;
  989.  
  990.      if (fuse > 5892) {            /* too many fuses for the known */
  991.            FreeMem(jedbuff, (long)sizeof(Jedec)); /* types of GALs => error */
  992.        FreeMem(filebuff, filesize);
  993.        JedecError(6, NO);
  994.        return(23);
  995.      }
  996.        }
  997.        else {
  998.          if ((fuse != 2194) && (fuse != 2706) && (fuse !=5892) && (fuse != 3274)) {
  999.            FreeMem(jedbuff, (long)sizeof(Jedec));
  1000.        FreeMem(filebuff, filesize);
  1001.        JedecError(6, NO);
  1002.        return(6);
  1003.      }
  1004.  
  1005.          if (fuse == 2194)            /* if there was no QF and no */
  1006.        JedecGALType = GAL16V8;        /* F instruction, identify type */
  1007.                         /* of GAL by the last fuse */
  1008.          if (fuse == 2706)            /* address */
  1009.        JedecGALType = GAL20V8;
  1010.  
  1011.      if (fuse == 5892)
  1012.        JedecGALType = GAL22V10;
  1013.  
  1014.      if (fuse == 3274)
  1015.        JedecGALType = GAL20RA10;
  1016.  
  1017.        }
  1018.      }
  1019.  
  1020.      if (FCHKflag)                /*file checksum?*/
  1021.        if (!SearchNextChar()) {            /*are there any more characters?*/
  1022.      FreeMem(filebuff, filesize);        /*yes, then error*/
  1023.          FreeMem(jedbuff, (long)sizeof(Jedec));    /*free buffer of JEDEC structure*/
  1024.      JedecError(15, YES);
  1025.      return(15);
  1026.        }
  1027.  
  1028.  
  1029.                     /*** initialize JEDEC structure ***/
  1030.      for (n = 0; n < sizeof(Jedec); n++) {
  1031.  
  1032.        if (JedecGALType == GAL16V8) {        /*** GAL16V8 ***/
  1033.  
  1034.      if ((n < 2048) || (n > 5807 && n < 5816) || (n > 5817 && n < 5956)) {
  1035.         Jedec.GALLogic[n] = *jedptr;        /* get matrix, XOR, signature*/
  1036.        jedptr++;                /* AC1, PT, SYN and AC0 */
  1037.      }
  1038.      else
  1039.        Jedec.GALLogic[n] = 0;
  1040.  
  1041.        }
  1042.  
  1043.        if (JedecGALType == GAL20V8) {        /*** GAL20V8 ***/
  1044.  
  1045.      if ((n < 2560) || (n > 5807 && n < 5816) || (n > 5817 && n < 5956)) {
  1046.         Jedec.GALLogic[n] = *jedptr;        /* get matrix, XOR, signature */
  1047.        jedptr++;                /* AC1, PT, SYN and AC0 */
  1048.      }
  1049.      else
  1050.        Jedec.GALLogic[n] = 0;
  1051.  
  1052.        }
  1053.  
  1054.  
  1055.        if (JedecGALType == GAL22V10) {        /*** GAL22V10 ***/
  1056.  
  1057.      if ((n < 5808) || (n > 5817 && n < 5882)) {
  1058.         Jedec.GALLogic[n] = *jedptr;        /* get matrix, signature */
  1059.        jedptr++;
  1060.      }
  1061.      else {
  1062.        if (n > 5807 && n < 5818) {
  1063.           Jedec.GALXOR[n - 5808] = *jedptr;        /* get XORs */
  1064.          jedptr++;
  1065.  
  1066.           Jedec.GALS1[n - 5808] = *jedptr;        /* get S1 */
  1067.          jedptr++;
  1068.        }
  1069.        else
  1070.          if (n < 5956 || n > 5965)            /* don't erase S1 */
  1071.            Jedec.GALLogic[n] = 0;            /* it's set when n>5807 */
  1072.      }
  1073.        }
  1074.  
  1075.  
  1076.        if (JedecGALType == GAL20RA10) {        /*** GAL20RA10 ***/
  1077.  
  1078.      if ((n < 3200) || (n > 5807 && n < 5882)) {    /* get matrix, XOR */
  1079.         Jedec.GALLogic[n] = *jedptr;            /* and signature  */
  1080.        jedptr++;
  1081.      }
  1082.      else
  1083.        Jedec.GALLogic[n] = 0;
  1084.  
  1085.        }
  1086.  
  1087.  
  1088.      }
  1089.  
  1090.  
  1091.      if (Cflag) {            /* test whether checksum is o.k. or not */
  1092.        if (fuse_chk != FuseChecksum(JedecGALType)) {
  1093.          if (!MyRequest(CONT_REQ, AppStrings[MSG_BAD_FUSECHK].as_Str)) {
  1094.        FreeMem(filebuff, filesize);
  1095.            FreeMem(jedbuff, (long)sizeof(Jedec));
  1096.        JedecError(11, NO);            /* bad fuse checksum */
  1097.        return(11);
  1098.      }
  1099.        }
  1100.      }
  1101.  
  1102.      FreeMem(jedbuff, (long)sizeof(Jedec));    /*free buffer of JEDEC structure*/
  1103.      FreeMem(filebuff, filesize);        /*free buffer of JEDEC file*/
  1104.      return(0);                    /*no error occured*/
  1105.  
  1106.    }
  1107.    else {
  1108.      ErrorReq(3);                /*read error*/
  1109.      FreeMem(filebuff, filesize);
  1110.      return(-2);
  1111.    }
  1112.  
  1113.  }
  1114.  else {
  1115.    ErrorReq(2);                    /*not enough free momory*/
  1116.    return(-2);
  1117.  }
  1118.  
  1119. }
  1120.  
  1121.  
  1122.  
  1123.  
  1124.  
  1125.  
  1126.  
  1127. /*print error messages
  1128.   flag signs whether there should be printed a line number too or not
  1129. */
  1130. void JedecError(int errornum, int flag)
  1131. {
  1132.  if (flag)
  1133.    PrintErrorLine(linenum);
  1134.  
  1135.  switch (errornum) {
  1136.    case  1: { PrintText(AppStrings[MSG_ERR_JED1].as_Str, 1);
  1137.           break;
  1138.         }
  1139.    case  2: { PrintText(AppStrings[MSG_ERR_JED2].as_Str, 1);
  1140.           break;
  1141.         }
  1142.    case  3: { PrintText(AppStrings[MSG_ERR_JED3].as_Str, 1);
  1143.           break;
  1144.         }
  1145.    case  4: { PrintText(AppStrings[MSG_ERR_JED4].as_Str, 1);
  1146.           break;
  1147.         }
  1148.    case  5: { PrintText(AppStrings[MSG_ERR_JED5].as_Str, 1);
  1149.           break;
  1150.         }
  1151.    case  6: { PrintText(AppStrings[MSG_ERR_JED6].as_Str, 1);
  1152.           break;
  1153.         }
  1154.    case  7: { PrintText(AppStrings[MSG_ERR_JED7].as_Str, 1);
  1155.           break;
  1156.         }
  1157.    case  8: { PrintText(AppStrings[MSG_ERR_JED8].as_Str, 1);
  1158.           break;
  1159.         }
  1160.    case  9: { PrintText(AppStrings[MSG_ERR_JED9].as_Str, 1);
  1161.           break;
  1162.         }
  1163.    case 10: { PrintText(AppStrings[MSG_ERR_JED10].as_Str, 1);
  1164.           break;
  1165.         }
  1166.    case 11: { PrintText(AppStrings[MSG_ERR_JED11].as_Str, 1);
  1167.           break;
  1168.         }
  1169.    case 12: { PrintText(AppStrings[MSG_ERR_JED12].as_Str, 1);
  1170.           break;
  1171.         }
  1172.    case 13: { PrintText(AppStrings[MSG_ERR_JED13].as_Str, 1);
  1173.           break;
  1174.         }
  1175.    case 14: { PrintText(AppStrings[MSG_ERR_JED14].as_Str, 1);
  1176.           break;
  1177.         }
  1178.    case 15: { PrintText(AppStrings[MSG_ERR_JED15].as_Str, 1);
  1179.           break;
  1180.         }
  1181.    case 16: { PrintText(AppStrings[MSG_ERR_JED16].as_Str, 1);
  1182.           break;
  1183.         }
  1184.    case 17: { PrintText(AppStrings[MSG_ERR_JED17].as_Str, 1);
  1185.           break;
  1186.         }
  1187.    case 18: { PrintText(AppStrings[MSG_ERR_JED18].as_Str, 1);
  1188.           break;
  1189.         }
  1190.    case 19: { PrintText(AppStrings[MSG_ERR_JED19].as_Str, 1);
  1191.           break;
  1192.         }
  1193.    case 20: { PrintText(AppStrings[MSG_ERR_JED20].as_Str, 1);
  1194.           break;
  1195.         }
  1196.    case 21: { PrintText(AppStrings[MSG_ERR_JED21].as_Str, 1);
  1197.           break;
  1198.         }
  1199.    case 22: { PrintText(AppStrings[MSG_ERR_JED22].as_Str, 1);
  1200.           break;
  1201.         }
  1202.    case 23: { PrintText(AppStrings[MSG_ERR_JED23].as_Str, 1);
  1203.           break;
  1204.         }
  1205.    case 24: { PrintText(AppStrings[MSG_ERR_JED24].as_Str, 1);
  1206.           break;
  1207.         }
  1208.    case 25: { PrintText(AppStrings[MSG_ERR_JED25].as_Str, 1);
  1209.           break;
  1210.         }
  1211.   }
  1212.  ErrorReq(5);
  1213.  
  1214. }
  1215.  
  1216.  
  1217.  
  1218.  
  1219.