#include "fpgaflashconfig.h"

/* flash for carrier 1 xcku040, index 0 (golden) and 1 (user)
 * flash for carrier 2 xcku040, index 2 (golden) and 3 (user)
 * flash for carrier 1 xcku060, index 4 (golden) and 5 (user)
 * flash for carrier 2 xcku060, index 6 (golden) and 7 (user)
 * flash for digital controller xc7a200, index 8 (golden) and index 9 (user)
 */
#include "mydebug.h"

template <class T>
T reverse_bits(T n) {
    short bits = sizeof(n) * 8;
    T mask = ~T(0); // equivalent to uint32_t mask = 0b11111111111111111111111111111111;

    while (bits >>= 1) {
        mask ^= mask << (bits); // will convert mask to 0b00000000000000001111111111111111;
        n = (n & ~mask) >> bits | (n & mask) << bits; // divide and conquer
    }

    return n;
}

/*
//***********************************************************************
// Add TGT to QList
// Input parameters:    appName
//                      slotAddress
//                      archName
//                      description
//                      tgtName
//                      partName
//                      matchStr
//                      qspiport
//                      type
//                      byteaddress
//                      firmware_area
//                      address_start_area
//                      address_stop_area
//                      ip
//                      port
//                      tgtSection
//
// Returns:
//***********************************************************************
TgtMatch* TgtMatchSet::add(
        const QString& aName,
        const QString& sName,
        const QString& tName,
        const QString& pName,
        const QString& namePrefix,
        const QString& descr,
        const QString& arch,
        const QString&                   _address_start_area,
        const QString&                   _address_stop_area,
        const flash_firmware_area_t&     _firmware_area,
        const flash_type_t&              _type,
        const reg_moder_byteaddress_t&   _byteaddress,
        const reg_moder_qspiport_t&      _spiPort,
        const QString&                   _ip,
        const unsigned int&              _port,
        const QString &                  _tgtSection)

{
    TgtMatch t ;
    t.appName               = aName;
    t.slotAddress           = sName;
    t.archName              = arch;
    t.description           = descr;
    t.tgtName               = tName;
    t.partName              = pName;
    t.matchStr              = namePrefix;
    t.address_start_area    = _address_start_area;
    t.address_stop_area     = _address_stop_area;
    t.firmware_area         = _firmware_area;
    t.type                  = _type;
    t.byteaddress           = _byteaddress;
    t.spiPort               = _spiPort;
    t.ip                    = _ip;
    t.port                  = _port;
    t.tgtSection            = _tgtSection;
    db.append(t);
    return (TgtMatch*)&db.at(db.size()-1);
}

//***********************************************************************
// Search target with matchStr = fname
// Input parameters:    fname = identifier to search for the matchStr target
// Returns:             tgt = in case a matching target is found
//                      0 = if not found a target
//***********************************************************************
const TgtMatch* TgtMatchSet::lookup(const QString& fname)
{
    for(int i=0; i<db.size(); ++i)
    {
        bool match=fname.startsWith(db[i].matchStr, Qt::CaseInsensitive); //db[i].rexp.exactMatch(fname);
        if (match)
        {
                return &db[i];
        }
    }
    return 0;
}

//***********************************************************************
// Search target with ArchName = arc
// Input parameters:    arc = identifier to search for the arc target
// Returns:             tgt = in case a matching target is found
//                      0 = if not found a target
//***********************************************************************
const TgtMatch* TgtMatchSet::lookupArch(const QString& arc)
{
    for(int i=0; i<db.size(); ++i)
    {
        bool match=db[i].archName==arc;
        if (match)
        {
            MyDebug.logMsg(type_log_info,
                             origin_msg_generic,
                             QString("match arch:").arg(arc));
            return &db[i];
        }
    }
    return 0;
}

//***********************************************************************
// Search target with matchStr = fname
// Input parameters:    tgtName = identifier to search for the matchStr target
//                      sAddr = address to verify that it corresponds with that of the target
// Returns:             tgt = in case a matching target is found
//                      0 = if not found a target
//***********************************************************************
TgtMatch* TgtMatchSet::lookupCSCI(const QString& fname, const QString sAddr)
{
    TgtMatch* t=0;
    for(int i=0; i<db.size(); ++i)
    {
        bool match=fname.startsWith(db[i].tgtName, Qt::CaseInsensitive); //db[i].rexp.exactMatch(fname);
        if (match)
        {
            if (db[i].slotAddress.isEmpty())
            {
                if (t==0)
                    t=&db[i];
            }
            match=db[i].slotAddress==sAddr;
            if (match)
                return &db[i];
        }
    }
    return t;
}

//***********************************************************************
// Search target with tgtSection = _tgtSection
// Input parameters:    _tgtSection = identifier to search for the targetSection
//
// Returns:             tgt = in case a matching target is found
//                      0 = if not found a target
//***********************************************************************
const TgtMatch* TgtMatchSet::lookupTgtSection(QString _tgtSection)
{
    for(int i=0; i<db.size(); ++i)
    {
        bool match=db[i].tgtSection==_tgtSection;
        if (match)
        {
            MyDebug.logMsg(type_log_info,
                             origin_msg_generic,
                             QString("match tgtSection: %1").arg(_tgtSection));
            return &db[i];
        }
    }
    return 0;
}

//***********************************************************************
// Search target with matchStr = fname
// Input parameters:    fname = identifier to search for the matchStr target
// Returns:             tgt = in case a matching target is found
//                      0 = if not found a target
//***********************************************************************
const TgtMatch* TgtMatchSet::lookupAppName(const QString& fname)
{
    for(int i=0; i<db.size(); ++i)
    {
        bool match=fname.startsWith(db[i].appName, Qt::CaseInsensitive); //db[i].rexp.exactMatch(fname);
        if (match)
        {
            return &db[i];
        }
    }
    return 0;
}

*/

