#include "emifa.h"

#include <cslr_device.h>
#include <cslr_emif16.h>

#include <stdio.h>
#include <assert.h>



// CE DSP Base Adresses
#define CE2_ADDRESS  0x70000000
#define CE3_ADDRESS  0x74000000
#define CE4_ADDRESS  0x78000000
#define CE5_ADDRESS  0x7C000000

typedef struct {
	Uint32 baseAddress;
} EmifaChip;

// **************************************************************************
// * File static variables
// **************************************************************************

	#define NUMBER_OF_CHIPS (3)
	EmifaChip CE[NUMBER_OF_CHIPS] = {CE2_ADDRESS, CE3_ADDRESS, CE4_ADDRESS};

// **************************************************************************
// * Interface
// **************************************************************************
void EMIFA_init(void) {
	volatile CSL_Emif16Regs* EMIF16_REGS = (CSL_Emif16Regs*)CSL_EMIF16_REGS;

	EMIF16_REGS->RCSR  = 0x00000205;
	EMIF16_REGS->AWCCR = 0xF0040080;
	EMIF16_REGS->A0CR  = 0x00000002;
	EMIF16_REGS->A1CR  = 0x00000002;
	EMIF16_REGS->A2CR  = 0x00000002;
	EMIF16_REGS->A3CR  = 0x00000002;

	return;
}

void EMIFA_write(unsigned chipID, Uint32 internalAddress, Uint16 value) { assert(chipID<NUMBER_OF_CHIPS); {
	Uint16* pValue = (Uint16*)(CE[chipID].baseAddress + (2*internalAddress));
	*pValue = value;
	*pValue = value+1;


	return;
}}

Uint16 EMIFA_read(unsigned chipID, Uint32 internalAddress) { assert(chipID<NUMBER_OF_CHIPS); {
	Uint32 i;
	for(i=internalAddress;i<5;i++)
	{
	Uint16* pValue = (Uint16*)(CE[chipID].baseAddress + (2*i));
	Uint16 value = *pValue;
}

}}

Uint8 EMIFA_runTest(void) {
	unsigned chipID=0;
	Uint16 value[2] = {0xABCD, 0x4567};
	Uint16 readBack = {0};
	Uint32 internalAddress = 0;

	EMIFA_write(chipID, internalAddress, value[0]);
	readBack = EMIFA_read(chipID, internalAddress);
	return 0x00;
}
