#include "xparameters.h"

unsigned int *dft_command_reg =
              (unsigned int *) XPAR_MICRO_ASSIST_DFT_0_BASEADDR; 
unsigned int *dft_index_reg =
              (unsigned int *) XPAR_MICRO_ASSIST_DFT_0_BASEADDR + 1;
unsigned int *dft_value_reg =
              (unsigned int *) XPAR_MICRO_ASSIST_DFT_0_BASEADDR + 2;

int main (void) 
   {
   volatile unsigned int command_data, index_data, value_data;
   unsigned int old_command_data;
   int vals[10];

   vals[0] = 0x12;
   vals[1] = 0x20;
   vals[2] = 0x40;

// Clear the screen
   xil_printf("%c[2J",27);

   xil_printf("Command/index/value reg addresses: 0x%X 0x%X 0x%X\r\n", 
      dft_command_reg, dft_index_reg, dft_value_reg);

   xil_printf("Vals array: 0x%X 0x%X 0x%X\r\n", vals[0], vals[1], vals[2]);

// Give the 'go' command to the hardware DFT.
   *dft_command_reg = (unsigned int) 0x00000001;

// Read it back to make sure it got set.
   command_data = *dft_command_reg;
   xil_printf("Command reg should be 'go' (1): 0x%X\r\n", command_data);
   old_command_data = (unsigned int) 0xFFFFFFFF;

// Busy wait loop, serving the commands issued by the hardware DFT core.
   while(1)
      {

// Keep reading the command register, checking to see if a command has 
// been issued by the hardware DFT.
      command_data = *dft_command_reg;

      if (command_data != old_command_data)
         {
         xil_printf("Command register value: 0x%X\r\n", command_data);

// If dft engine wants to read a value, get the index and return the value.
         if ( command_data == 3 )
            {
            index_data = *dft_index_reg;
            xil_printf("DFT index for DFT engine READ request: 0x%X\r\n", index_data);
            *dft_value_reg = vals[index_data];
            xil_printf("Vals array value sent to DFT engine: 0x%X\r\n", vals[index_data]);
	    }

// If dft engine wants to write a value, get the index and update the vals array.
         if ( command_data == 5 )
            {
            index_data = *dft_index_reg;
            xil_printf("DFT index for DFT engine WRITE request: 0x%X\r\n", index_data);
            vals[index_data] = *dft_value_reg; 
            xil_printf("New vals array value received from DFT engine: 0x%X\r\n", 
               vals[index_data]);
	    }

// If command is to exit, exit.
         if ( command_data == 9 )
            {
            break;
	    }

// Save in old command data and wait.
         old_command_data = command_data;
         }
      }

   xil_printf("Vals array: 0x%X 0x%X 0x%X\r\n", vals[0], vals[1], vals[2]);

   }
