/*
 * common.h
 *
 *  Created on: May 22, 2013
 *      Author: dkempert
 */

#ifndef COMMON_H_
#define COMMON_H_
#include <alt_types.h>
#include <io.h>
#include "system.h"

#define FALSE   (alt_u8)0
#define TRUE    (alt_u8)1

// peripheral I/O
#define PI0_DATA_REG_0FT 0		// data register addr offset
#define PI0_DIRT_REG_0FT 1		// direction reg addr offset
#define PI0_INTM_REG_0FT 2		// interrupt mask reg addr offset
#define PI0_EDGE_REG_0FT 3		// edge capture reg addr offset

#define pio_read(base)			IORD(base,PI0_DATA_REG_0FT)
#define pio_write(base, data)	IOWR(base,PI0_DATA_REG_0FT,data)
#define btn_read(base)			IORD(base, PI0_EDGE_REG_0FT)
#define btn_clear(base)			IOWR(base, PI0_EDGE_REG_0FT, 0xf)



// JTAG UART
#define JUART_DATA_REG_OFT 0	// data register addr offset
#define JUART_CTRL_REG_OFT 1	// control register addr offset

/* check # slots available in FIFO buffer */
#define jtaguart_rd_wspace(base)	((IORD(base, JUART_CTRL_REG_OFT) & 0xffff0000) >> 16)
/* write an 8-bit char */
#define jtaguart_wr_ch(base, data)	IOWR(base, JUART_DATA_REG_OFT, data & 0x000000ff)

// Timers
#define TIMER_STAT_REG_OFT 0	// status register addr offset
#define TIMER_CTRL_REG_OFT 1	// control reg addr offset
#define TIMER_PRDL_REG_OFT 2	// period low reg addr offset
#define TIMER_PRDH_REG_OFT 3	// period high reg addr offset

#define timer_read_tick(base)	(IORD(base, TIMER_STAT_REG_OFT) & 0x01)
#define timer_clear_tick(base)	IOWR(base, TIMER_STAT_REG_OFT, 0)



#endif /* COMMON_H_ */
