text: // 82S100.cpp : Defines the entry point for the console application.
  // (c) 2015 Frank "androSID" Wolf, Tucherstr. 16, 77815 Buehl, GERMANY
  
  
  #include "stdafx.h"
  #include "82S100.h"
  
  /*
  // Array taken directly from JEDEC file; '1' means fuse is destroyed, '0' fuse is left intact
  PLA c64pla =
  {
  //  |----------------------------------------- min Terms -----------------------------------------| |----  max Terms  ----|
      1, 1, 1, 0, 1, 0, 1, 1, 1, 1, 1, 0, 0, 1, 1, 0, 1, 1, 1, 1, 0, 1, 1, 0, 1, 1, 1, 0, 1, 1, 1, 1, 0, 0, 1, 1, 1, 1, 1, 1,
      1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 0, 1, 0, 1, 0, 1, 1, 1, 1, 0, 1, 1, 0, 1, 1, 1, 0, 1, 1, 1, 1, 0, 1, 0, 1, 1, 1, 1, 1,
      1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 0, 1, 0, 1, 0, 1, 1, 1, 1, 0, 1, 1, 0, 0, 1, 0, 1, 1, 1, 1, 1, 0, 1, 0, 1, 1, 1, 1, 1,
      1, 1, 1, 1, 1, 0, 0, 1, 1, 1, 1, 0, 1, 0, 0, 1, 1, 0, 1, 1, 0, 1, 1, 0, 1, 1, 1, 0, 1, 1, 1, 1, 0, 1, 1, 0, 1, 1, 1, 1,
      1, 1, 1, 0, 1, 1, 0, 1, 1, 1, 1, 0, 1, 0, 0, 1, 1, 0, 1, 1, 0, 1, 1, 0, 1, 1, 1, 0, 1, 1, 1, 1, 0, 1, 1, 0, 1, 1, 1, 1,
      1, 1, 1, 1, 1, 0, 0, 1, 1, 1, 1, 0, 1, 0, 0, 1, 1, 0, 1, 1, 0, 1, 1, 0, 0, 1, 0, 1, 1, 1, 1, 1, 0, 1, 1, 0, 1, 1, 1, 1,
      1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 0, 0, 1, 1, 0, 0, 1, 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0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
      0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
      0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
      0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
      0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
      0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
      0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
      0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  //  |-- output polarity --|
      0, 1, 1, 1, 1, 1, 1, 1
  };
  */
  
  bool calcMinTerm(uint16_t inputLines, const LogicTerm& pLogicTerm)
  {
      // According to datasheet all input lines for logic AND gates float to high
      bool minTerm = true;
  
      uint8_t idx = 0;
  
      for (uint16_t mask = 0x0001; mask; mask <<= 1)
      {
          // Mask out one of the 16 input lines
          bool inputLevel = (inputLines & mask) ? true : false;
  
          // Only pull down signal if fuse is intact
          minTerm &= pLogicTerm.AND[idx][0] | !inputLevel;
          minTerm &= pLogicTerm.AND[idx][1] | inputLevel;
  
          idx++;
      }
  
      return minTerm;
  }
  
  bool calculateOutputLine(PLA *pla, uint8_t outputLineNumber, uint16_t inputLines)
  {
      // According to datasheet all input lines for logic OR gates float to low
      bool outputLine = false;
  
      // Evaluate all 48 product terms
      for (uint8_t termNumber = 0; termNumber < 48; termNumber++)
      {
          const LogicTerm& pLogicTerm = pla->fusesInPLA.minTerms[termNumber];
  
          // Only pull up signal if fuse is intact
          outputLine |= (!pLogicTerm.OR[outputLineNumber]) && calcMinTerm(inputLines, pLogicTerm);
      }
  
      // Negate Output?
      return (outputLine ^ pla->fusesInPLA.NEG[outputLineNumber]);
  }
  
  uint8_t calcPlaOutputLines(PLA *pla, uint16_t inputLines)
  {
      uint8_t outputLines = 0x00;
  
      uint8_t outputLine = 0;
  
      for (uint8_t mask = 0x01; mask; mask <<= 1)
      {
          outputLines |= calculateOutputLine(pla, outputLine++, inputLines) ? mask : 0;
      }
  
      return outputLines;
  }
  
  uint16_t mapEpromAddrLinesToPlaInput(uint16_t epromAddrLines)
  {
      uint16_t inputLinesToPla;
  
      inputLinesToPla  = (epromAddrLines & 0x0001) ? (1 << 15) : 0;
      inputLinesToPla |= (epromAddrLines & 0x0002) ? (1 <<  0) : 0;
      inputLinesToPla |= (epromAddrLines & 0x0004) ? (1 <<  1) : 0;
      inputLinesToPla |= (epromAddrLines & 0x0008) ? (1 <<  2) : 0;
      inputLinesToPla |= (epromAddrLines & 0x0010) ? (1 <<  3) : 0;
      inputLinesToPla |= (epromAddrLines & 0x0020) ? (1 <<  4) : 0;
      inputLinesToPla |= (epromAddrLines & 0x0040) ? (1 <<  5) : 0;
      inputLinesToPla |= (epromAddrLines & 0x0080) ? (1 <<  6) : 0;
      inputLinesToPla |= (epromAddrLines & 0x0100) ? (1 << 10) : 0;
      inputLinesToPla |= (epromAddrLines & 0x0200) ? (1 << 11) : 0;
      inputLinesToPla |= (epromAddrLines & 0x0400) ? (1 << 14) : 0;
      inputLinesToPla |= (epromAddrLines & 0x0800) ? (1 << 12) : 0;
      inputLinesToPla |= (epromAddrLines & 0x1000) ? (1 <<  7) : 0;
      inputLinesToPla |= (epromAddrLines & 0x2000) ? (1 <<  9) : 0;
      inputLinesToPla |= (epromAddrLines & 0x4000) ? (1 <<  8) : 0;
      inputLinesToPla |= (epromAddrLines & 0x8000) ? (1 << 13) : 0;
  
      return inputLinesToPla;
  }
  
  uint8_t mapPlaOutputLinesToEpromDataLines(uint8_t outputsFromPLA)
  {
      uint8_t dataLines;
  
      dataLines  = (outputsFromPLA & (1 << 0)) ? (1 << 6) : 0;
      dataLines |= (outputsFromPLA & (1 << 1)) ? (1 << 5) : 0;
      dataLines |= (outputsFromPLA & (1 << 2)) ? (1 << 4) : 0;
      dataLines |= (outputsFromPLA & (1 << 3)) ? (1 << 3) : 0;
      dataLines |= (outputsFromPLA & (1 << 4)) ? (1 << 2) : 0;
      dataLines |= (outputsFromPLA & (1 << 5)) ? (1 << 1) : 0;
      dataLines |= (outputsFromPLA & (1 << 6)) ? (1 << 0) : 0;
      dataLines |= (outputsFromPLA & (1 << 7)) ? (1 << 7) : 0;
  
      return dataLines;
  }
  
  
  int convert_82S100(PLA *pla)
  {
      printf("82S100 JEDEC file to Eprom converter\n(c) 2015 by Frank 'androSID' Wolf\n");
      printf("Contact: info@androSID.com\n");
  
      FILE* pFile = fopen("82S100_EPROM.bin", "wb");
  
      if (pFile != NULL)
      {
          uint16_t addr = 0x0000;
  
          do
          {
              // Remap address lines to PLA input lines
              uint16_t plaInputLines = mapEpromAddrLinesToPlaInput(addr);
  
              // Calculate PLA terms (actual simulation).
              uint8_t plaOutputLines = calcPlaOutputLines(pla, plaInputLines);
  
              // Remap PLA output lines to EPROM data lines
              uint8_t epromDataLines = mapPlaOutputLinesToEpromDataLines(plaOutputLines);
  
              fputc(epromDataLines, pFile);
          }
          while (++addr);
  
          fclose(pFile);
      }
  
      return 0;
  }
  /*
  int _tmain(int argc, _TCHAR* argv[])
  {
  	
  	printf("82S100 JEDEC file to Eprom converter\n(c) 2015 by Frank 'androSID' Wolf\n");
  	printf("Contact: info@androSID.com\n");
  
  	FILE* pFile = fopen("82S100_EPROM.bin", "wb");
  
  	if (pFile != NULL)
  	{
  		uint16_t addr = 0x0000;
  
  		do
  		{
  			// Remap address lines to PLA input lines
  			uint16_t plaInputLines = mapEpromAddrLinesToPlaInput(addr);
  
  			// Calculate PLA terms (actual simulation).
  			uint8_t plaOutputLines = calcPlaOutputLines(&c64pla, plaInputLines);
  
  			// Remap PLA output lines to EPROM data lines
  			uint8_t epromDataLines = mapPlaOutputLinesToEpromDataLines(plaOutputLines);
  
  			fputc(epromDataLines, pFile);
  		} while (++addr);
  
  		fclose(pFile);
  	}
  
  	return 0;
  }
  */