/******************************************************************************
*     $Date: 2004/11/24 06:27:04 $
*     $RCSfile: ps2_scancodes_polled.c,v $
*     This program polls a PS/2 port and echoes input scancodes it detects, e.g.
*     from keystrokes on a PS/2 keyboard. 
*******************************************************************************

*******************************************************************************
*
*     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 <stdio.h>
#include "xbasic_types.h"
#include "xps2_l.h"
#include "xparameters.h"
#include "xuartns550_l.h"

/* uncomment the following #define to use the first PS/2 port */
//#define USE_PS2_0

#ifdef USE_PS2_0
#define PS2_BASEADDR  XPAR_OPB_PS2_DUAL_REF_0_BASEADDR_0
#else
#define PS2_BASEADDR  XPAR_OPB_PS2_DUAL_REF_0_BASEADDR_1
#endif

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

int main()
{
  Xuint8 x;

  XPs2_mReset(PS2_BASEADDR);

  XUartNs550_SetBaud(UART_BASEADDR, UART_CLOCK, UART_BAUDRATE);
  XUartNs550_mSetLineControlReg(UART_BASEADDR, XUN_LCR_8_DATA_BITS);
  printf("\n\nPS/2 Demo using polled mode\n\r");
  printf("Depending on which port you would like to test, \r\n");
  printf("Port 0: Uncomment \"#define USE_PS2_0\r\n");
  printf("Port 1: Comment   \"#define USE_PS2_0\"\r\n");
  printf("and recompile the source code.\r\n\r\n");
  printf("Echoing PS/2 scancodes from a PS/2 input device connected to ");
#ifdef USE_PS2_0
  printf("PS/2 port 0 (Mouse):\r\n");
#else
  printf("PS/2 port 1 (Keyboard):\r\n");
#endif

  while(1)
  {
    x = XPs2_RecvByte(PS2_BASEADDR);
    printf("%X \n", x);
  }

  return 0;
}

