/******************************************************************************
*     $Date: 2004/11/19 20:15:52 $
*     $RCSfile: button_led_test.c,v $
*******************************************************************************

*******************************************************************************
*
*     XILINX IS PROVIDING THIS DESIGN, CODE, OR INFORMATION "AS IS"
*     SOLELY FOR USE IN DEVELOPING PROGRAMS AND SOLUTIONS FOR
*     XILINX DEVICES.  BY PROVIDING THIS DESIGN, CODE, OR INFORMATION
*     AS ONE POSSIBLE IMPLEMENTATION OF THIS FEATURE, APPLICATION
*     OR STANDARD, XILINX IS MAKING NO REPRESENTATION THAT THIS
*     IMPLEMENTATION IS FREE FROM ANY CLAIMS OF INFRINGEMENT,
*     AND YOU ARE RESPONSIBLE FOR OBTAINING ANY RIGHTS YOU MAY REQUIRE
*     FOR YOUR IMPLEMENTATION.  XILINX EXPRESSLY DISCLAIMS ANY
*     WARRANTY WHATSOEVER WITH RESPECT TO THE ADEQUACY OF THE
*     IMPLEMENTATION, INCLUDING BUT NOT LIMITED TO ANY WARRANTIES OR
*     REPRESENTATIONS THAT THIS IMPLEMENTATION IS FREE FROM CLAIMS OF
*     INFRINGEMENT, IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
*     FOR A PARTICULAR PURPOSE.
*     
*     (c) Copyright 2004 Xilinx, Inc.
*     All rights reserved.
*
******************************************************************************/
#include "xparameters.h"
#include "xuartns550_l.h"
#include "xgpio_l.h"

#define UART_BASEADDR XPAR_OPB_UART16550_0_BASEADDR
#define UART_CLOCK    XPAR_XUARTNS550_CLOCK_HZ
#define GPIO_0_BASEADDR XPAR_OPB_GPIO_0_BASEADDR


#if !SIM
#define UART_BAUDRATE 9600                      /* real hardware */
#else
#define UART_BAUDRATE (UART_CLOCK / 16 / 3)     /* simulation */
#endif

static void printf(char *s)
{
  while (*s) {
    XUartNs550_SendByte(UART_BASEADDR, *s);
    s++;
  }
}

int main()
{
  Xboolean button_n, button_e, button_s, button_w, button_c; /* GPIO[13:9] */
  Xboolean led_n, led_e, led_s, led_w, led_c; /* GPIO[8:4] */
  Xboolean gpio_led3, gpio_led2, gpio_led1, gpio_led0; /*  GPIO [3:0] */
  Xboolean dip_sw8, dip_sw7, dip_sw6, dip_sw5, dip_sw4, dip_sw3, dip_sw2, dip_sw1; /* GPIO [21:14] */
  unsigned int gpio_out;

  /* setup 16550 Uart */
  XUartNs550_SetBaud(UART_BASEADDR, UART_CLOCK, UART_BAUDRATE);
  XUartNs550_mSetLineControlReg(UART_BASEADDR, XUN_LCR_8_DATA_BITS);
  /* set gpio bits to all output */
  XGpio_mSetDataDirection(GPIO_0_BASEADDR, 1, 0xFFFFFE00);

  printf("Button and LED Test\r\n\r\n");
  while (1) {
    button_n = (XGpio_mGetDataReg(GPIO_0_BASEADDR, 1) >> 13) & (0x00000001);
    button_e = (XGpio_mGetDataReg(GPIO_0_BASEADDR, 1) >> 12) & (0x00000001);
    button_s = (XGpio_mGetDataReg(GPIO_0_BASEADDR, 1) >> 11) & (0x00000001);
    button_w = (XGpio_mGetDataReg(GPIO_0_BASEADDR, 1) >> 10) & (0x00000001);
    button_c = (XGpio_mGetDataReg(GPIO_0_BASEADDR, 1) >> 9)  & (0x00000001);
    dip_sw8  = (XGpio_mGetDataReg(GPIO_0_BASEADDR, 1) >> 21) & (0x00000001);
    dip_sw7  = (XGpio_mGetDataReg(GPIO_0_BASEADDR, 1) >> 20) & (0x00000001);
    dip_sw6  = (XGpio_mGetDataReg(GPIO_0_BASEADDR, 1) >> 19) & (0x00000001);
    dip_sw5  = (XGpio_mGetDataReg(GPIO_0_BASEADDR, 1) >> 18) & (0x00000001);
    dip_sw4  = (XGpio_mGetDataReg(GPIO_0_BASEADDR, 1) >> 17) & (0x00000001);
    dip_sw3  = (XGpio_mGetDataReg(GPIO_0_BASEADDR, 1) >> 16) & (0x00000001);
    dip_sw2  = (XGpio_mGetDataReg(GPIO_0_BASEADDR, 1) >> 15) & (0x00000001);
    dip_sw1  = (XGpio_mGetDataReg(GPIO_0_BASEADDR, 1) >> 14) & (0x00000001);
    led_n = button_n;
    led_e = button_e;
    led_s = button_s;
    led_w = button_w;
    led_c = button_c;
    gpio_led3 = dip_sw8 | dip_sw4;
    gpio_led2 = dip_sw7 | dip_sw3;
    gpio_led1 = dip_sw6 | dip_sw2;
    gpio_led0 = dip_sw5 | dip_sw1;
	gpio_out = ((unsigned int) (gpio_led0))
	         | ((unsigned int) (gpio_led1) << 1)
	         | ((unsigned int) (gpio_led2) << 2)
	         | ((unsigned int) (gpio_led3) << 3)
	         | ((unsigned int) (led_c) << 4)
	         | ((unsigned int) (led_w) << 5)
	         | ((unsigned int) (led_s) << 6)
	         | ((unsigned int) (led_e) << 7)
	         | ((unsigned int) (led_n) << 8);
    XGpio_mSetDataReg(GPIO_0_BASEADDR, 1, gpio_out);
  }

  return 0;

}
