/******************************************************************************
** Copyright 2015 MBARI - Monterey Bay Aquarium Research Institute
*******************************************************************************
*******************************************************************************
** Summary  : Routines to access memory-mapped registers
** Filename : io_regs.c
** Author   : Luke Coletti
** Project  :
** Version  : 1.0
** Compiler : MetroWerks Code Warrior v8.3
** Created  : 12/15/14
** Archived :
*******************************************************************************
** Modification History:
** 01/02/15 : CF2 Port, LJC
*******************************************************************************/

#include        <cfxpico.h>             // Persistor BIOS and I/O Definitions
#include        <io_regs.h>


void RegIO_Init(void)
{

	//74HC273 OUTPUT LATCH
	CS8Setup(CS8_BASE_ADDR, CS8_BLOCK_SIZE, true);
	CS8Options(false, true, true, 0);

	//74HC373 INPUT LATCH
	CS10Setup(CS10_BASE_ADDR, CS10_BLOCK_SIZE, true);
	CS10Options(true, false, true, 0);

	RegIO_WrDefaults();

}


ushort RegIO_RdWord(void)
{

	return (ushort) *((vushort *)CS10_BASE_ADDR);

}


void RegIO_WrWord(ushort WordVal)
{

	*((vushort *)CS8_BASE_ADDR) = WordVal;

}


void RegIO_SetBit(ushort bitvals)
{

	bitvals &= 0x00FF;      // no upper byte in hardware

	WrRegVal |= bitvals;
	RegIO_WrWord( WrRegVal );

}


void RegIO_ClrBit(ushort BitVals)
{
	register ushort OnesComp;

	OnesComp = ~BitVals;

	WrRegVal &= OnesComp;
	RegIO_WrWord( WrRegVal );

}


void RegIO_WrDefaults(void)
{

	WrRegVal = WR_REG_DEFAULTS;
	RegIO_WrWord( WrRegVal );

}