/*************************************************************************/
/*
 * File name: DetectFpgaConfig.cpp
 * Location: See Matrox Example Launcher in the MIL Control Center
 * 
 *
 * Synopsis:  This example shows how to use MIL and the Mfpga API to detect 
 *            the currently loaded processing fpga configuration and 
 *            enumerate all the included Processing Unit (PU).
 *
 * Copyright (C) Matrox Electronic Systems Ltd., 1992-2020.
 * All Rights Reserved
 */

/* Headers. */
#include <mil.h>
#include <milfpga.h>

/* Main function. */
int MosMain(void)
   {
   MIL_ID         MilApplication;
   MIL_ID         MilSystem     ;
   MIL_TEXT_PTR   ConfigurationString = NULL;
   MIL_TEXT_PTR   FpgaPackageString = NULL;
   MIL_INT*       ProcessingUnitList = NULL;
   MIL_INT        BoardType = 0;
   MIL_INT        i = 0;
   MIL_INT        Count = 0;

   /* Allocate defaults. */
   MappAllocDefault(M_DEFAULT, &MilApplication, &MilSystem, M_NULL,
      M_NULL, M_NULL);

   /* Check if the processing FPGA is present. */
   MsysInquire(MilSystem, M_BOARD_TYPE, &BoardType);
   if(!(BoardType & M_PF))
      {
      MosPrintf(MIL_TEXT("Error, this example needs a processing FPGA installed on\n"));
      MosPrintf(MIL_TEXT("your board to continue.\n"));
      MosPrintf(MIL_TEXT("Press <Enter> to quit.\n"));
      MosGetch();
      MsysFree(MilSystem);
      MappFree(MilApplication);
      return 0;
      }

   MosPrintf(MIL_TEXT("Detecting processing FPGA configuration\n"));

   /* Detect the processing FPGA package name. */
   MfpgaInquire(MilSystem, M_DEV0, M_FPGA_PACKAGE_NAME_LENGTH, &Count);
   FpgaPackageString = new MIL_TEXT_CHAR[Count+1];
   MfpgaInquire(MilSystem, M_DEV0, M_FPGA_PACKAGE_NAME, FpgaPackageString);
   MosPrintf(MIL_TEXT("FPGA package: %s\n"), FpgaPackageString);

   /* Detect the currently loaded processing FPGA configuration file. */
   MfpgaInquire(MilSystem, M_DEV0, M_FPGA_CONFIGURATION_FILENAME_LENGTH, &Count);
   ConfigurationString = new MIL_TEXT_CHAR[Count+1];
   MfpgaInquire(MilSystem, M_DEV0, M_FPGA_CONFIGURATION_FILENAME, ConfigurationString);
   MosPrintf(MIL_TEXT("FPGA config file: %s\n"), ConfigurationString);

   /* Inquire the number of processing units present in the currently loaded
      firmware and list them.
   */
   MfpgaInquire(MilSystem, M_DEV0, M_NUMBER_OF_PU, &Count);
   ProcessingUnitList = new MIL_INT[Count];
   MfpgaInquire(MilSystem, M_DEV0, M_PU_LIST, ProcessingUnitList);
   MosPrintf(MIL_TEXT("\nListing FunctionId's of all detected units\n"));
   MosPrintf(MIL_TEXT("Detected %d Processing units:\n"), (int)Count);
   for(i=0; i<Count; i++)
      MosPrintf(MIL_TEXT("0x%.4X\t"), ProcessingUnitList[i]);
   MosPrintf(MIL_TEXT("\n\nPress a key to quit.\n"));
   MosGetch();

   /* Free the allocations. */
   delete [] FpgaPackageString;
   delete [] ConfigurationString;
   delete [] ProcessingUnitList;
   MappFreeDefault(MilApplication, MilSystem, M_NULL, M_NULL, M_NULL);
   return 0;
   }