/*
** ###################################################################
**
**     Filename  : ICS_tests.h
** 
**     Version   : 0.0
**
**     Compiler  : Metrowerks HCS08 C Compiler
**
**     Date      : 08Mar05
**
** ###################################################################
*/

#ifndef _ICS_TESTS_H
#define _ICS_TESTS_H
#define _GB60_Debug     // set if debugging w/ GB60

typedef union {
  byte Byte;
  struct {
    byte BIT0    :1;
    byte BIT1    :1;
    byte BIT2    :1;
    byte BIT3    :1;
    byte BIT4    :1;
    byte BIT5    :1;
    byte BIT6    :1;
    byte BIT7    :1;
  } Bits;
  struct {
    byte BYTE    :8;
  } MergedBits;
} BYTESTR;

// Declare defines for ASM code
#define aErrorFlags 0x81
#define aFCMD_val 0x80

// Declare 'define' constants
//#define Enter_Stop3 asm(stop)
#define Enter_Stop3 asm(nop)    //disable stop while debugging
#define Enter_Wait asm(wait)
//#define Bus_Speed 8000000
#define RTI_Exp 0x4000
#define M_RTI_Exp 0x85
#define hi_baud 4800
#define lo_baud 110
#define HGO_off 0x00
#define HGO_on  0x80
#define zero  0x30
#define one   0x31
#define two   0x32
#define three 0x33
#define four  0x34
#define five  0x35
#define six   0x36
#define seven 0x37
#define eight 0x38
#define enter 0x0D
#define space 0x20
#define EXIT  0x45
#define exit  0x65
#define STOP  0x53
#define stop  0x73
#define fei1_spd 8000000/16/hi_baud
#define fei2_spd 4000000/16/hi_baud
#define fei4_spd 2000000/16/hi_baud
#define fei8_spd 1000000/16/hi_baud
#define fbi_spd 16384/16/lo_baud
#define bus 0
#define xclk 1
#define ICSSCval 0x01				// FTrim val for #35
#define TRIMval 0xA0				// Trim val for #35
#define SOPT1val 0xe2				// enable cop w/long timeout
#define SOPT2val 0x00				// int clk for cop
#define SPMSC1_LVD_ON 0x65;         // enable LVD as int, disabled in stop, enable bandgap 
#define SPMSC1_LVD_OFF 0x01;        // disable LVD, enable bandgap

#define acmp_rise 0xa2             
#define acmp_fall 0xa1             
#define acmp_both 0xa3             

// RAM variables
#pragma DATA_SEG _DATA_ZEROPAGE
static word RTI_act = RTI_Exp;
static byte M_RTI_act = M_RTI_Exp;
static char n_str[3];
static word ACMP_IC;
static char ADC_val_H;
static char ADC_val_L;

/*** flags - RAM byte for saving flags ***/
typedef union {
  byte Byte;
  struct {
    byte stop1      :1; 
    byte stop2      :1; 
    byte ACMP_flag  :1; 
    byte            :1; 
    byte            :1; 
    byte            :1; 
    byte            :1; 
    byte            :1; 
  } Bits;
} FLAGSTR;
volatile FLAGSTR _FLAGS;
#define flags _FLAGS.Byte
#define flag_stop1 _FLAGS.Bits.stop1
#define flag_stop2 _FLAGS.Bits.stop2
#define flag_acmp  _FLAGS.Bits.ACMP_flag

/*** RAM_SRS - System Reset Status ***/
typedef union {
  byte Byte;
  struct {
    byte         :1; 
    byte LVD     :1;                                       /* Low Voltage Detect */
    byte         :1; 
    byte ILAD    :1; 																			 /* Illegal Address */
    byte ILOP    :1;                                       /* Illegal Opcode */
    byte COP     :1;                                       /* Computer Operating Properly (COP) Watchdog */
    byte PIN     :1;                                       /* External Reset Pin */
    byte POR     :1;                                       /* Power-On Reset */
  } Bits;
} RAMSRSSTR;
volatile RAMSRSSTR _RAMSRS;
#define RAMSRS _RAMSRS.Byte
#define RAMSRS_LVD _RAMSRS.Bits.LVD
#define RAMSRS_ILAD _RAMSRS.Bits.ILAD
#define RAMSRS_ILOP _RAMSRS.Bits.ILOP
#define RAMSRS_COP _RAMSRS.Bits.COP
#define RAMSRS_PIN _RAMSRS.Bits.PIN
#define RAMSRS_POR _RAMSRS.Bits.POR

#pragma DATA_SEG ILLEGAL_RAM
volatile byte invalid_addr;

#pragma DATA_SEG DEFAULT

/*** NVICSTRM - Nonvolatile ICS Trim Register; 0x0000FFAF ***/
extern volatile byte _NVICSTRM @0x0000FFAF;
#define NVICSTRM _NVICSTRM

// Function declarations
void cop_init(void);

#endif  //end #ifndef _flash_as_eeprom_H
