// Select Device
//

// CryptoMemory Library Include Files
#include "CM_LIB.h"
#include "CM_I2C.h"
#include "CM_I2C_L.h"
#include "CM_GPA.h"

// -------------------------------------------------------------------------------------------------
// Data
// -------------------------------------------------------------------------------------------------

uint8_t ucCM_DevGpaRegisters[16][Gpa_Regs];
uint8_t ucCM_DevEncrypt[16];
uint8_t ucCM_DevAuthenticate[16];
uint8_t ucCM_DevUserZone[16];
uint8_t ucCM_DevAntiTearing[16];

// -------------------------------------------------------------------------------------------------
// Functions
// -------------------------------------------------------------------------------------------------

// Select Chip
uint8_t cm_SelectChip(uint8_t ucChipNew)
{
	uint8_t ucChip, i;
	  
    if (ucChipNew == 0xFF) {
    	// Clear All State
    	for (ucChip = 0; ucChip < 16; ++ucChip) {
    	    for (i = 0; i < 16; ++i) ucCM_DevGpaRegisters[ucChip][i] = 0;
            ucCM_DevEncrypt[ucChip] = 0;
            ucCM_DevAuthenticate[ucChip] = 0;
            ucCM_DevUserZone[ucChip] = 0;
            ucCM_DevAntiTearing[ucChip] = 0;
        }
    }
    else {
    	ucChip = CM_PORT_CFG.ucChipSelect;
    	if (ucChip != ucChipNew) {
		    // Set Chip Select
            CM_PORT_CFG.ucChipSelect = ucChipNew;
			
			// Shift Addresses
    	    ucChip = (ucChip>>4)&0xF0;
    	    ucChipNew = (ucChipNew>>4)&0xF0;
			
    	    // Swap GPA Registers
    	    for (i = 0; i < 16; ++i) {
			    ucCM_DevGpaRegisters[ucChip][i] = ucGpaRegisters[i];
				ucGpaRegisters[i] = ucCM_DevGpaRegisters[ucChipNew][i];
			}
			
			//Save State 
            ucCM_DevEncrypt[ucChip] = ucCM_Encrypt;
            ucCM_DevAuthenticate[ucChip] = ucCM_Authenticate;
            ucCM_DevUserZone[ucChip] = ucCM_UserZone;
            ucCM_DevAntiTearing[ucChip] = ucCM_AntiTearing;
			
            // Restore Saved State
    	    ucCM_Encrypt = ucCM_DevEncrypt[ucChipNew];
            ucCM_Authenticate = ucCM_DevAuthenticate[ucChipNew];
            ucCM_UserZone = ucCM_DevUserZone[ucChipNew];
            ucCM_AntiTearing = ucCM_DevAntiTearing[ucChipNew];
        }
    }
	
	return SUCCESS;
}    	
