//these routines are usesd to communicate with the CPLD.
//initpldport configures the IO pins on the MSP for
//appropriate control of the pld.  This should be done
// as soon as possible to avoid any problems in start up.

#include "include\MSP430x14x.h"
#include "include\PLDdriver.h"

#define AS 0x01
#define WR 0x04
#define RD 0x10

//static unsigned char gMISCout = 0x1f;

void initpldport(void)
{
 //set port1 as input
 P1DIR = 0x0;
 //make sure port functions are low first
 P3OUT = 0x0;
 //set port 3 functions as outputs 
 P3DIR |= 0x15;
 
 writereg(MISCout, gMISCout);
 return;
}
//this routine is used to read a register from the cpld
unsigned char readreg(unsigned char address)
{
unsigned char regvalue;
_DINT();
  //set up data to be output
  P1OUT = address;
  //then setup port1 as output
  P1DIR = 0xff;
  //now strobe AS
  P3OUT |= AS;
  P3OUT &= ~AS;
  //now set p1 input
  P1DIR = 0x00;
  //Raise RD line
  P3OUT |= RD;
  //READ THE VALUE
  regvalue = P1IN;
  //lower RD
  P3OUT &= (unsigned char)(~(RD));
  _EINT();
  return (regvalue);
}
//this routine is used to write a byte to the cpld
void writereg(unsigned char address, unsigned char data)
{
_DINT();
  //set up data to be output
  P1OUT = address;
  //then setup port1 as output
  P1DIR = 0xff;
  //now strobe AS
  P3OUT |= AS;
  P3OUT &= (unsigned char)(~(AS));
  //now the data
  P1OUT = data;
  //Raise WR line
  P3OUT |= WR;
  //lower WR
  P3OUT &= (unsigned char)(~(WR));
  //put back to input
  P1DIR = 0x0;
   _EINT();
  return;
}
