/** ###################################################################
**     THIS COMPONENT MODULE IS GENERATED BY THE TOOL. DO NOT MODIFY IT.
**     Filename  : Cpu.C
**     Project   : vga2scart
**     Processor : MC9S08QD4CPC
**     Component : MC9S08QD4_8
**     Version   : Component 01.037, Driver 01.35, CPU db: 3.00.002
**     Datasheet : MC9S08QD4 Rev. 4 9/2008
**     Compiler  : CodeWarrior HCS08 C Compiler
**     Date/Time : 7.7.2010, 1:29
**     Abstract  :
**         This bean "MC9S08QD4_8" contains initialization of the
**         CPU and provides basic methods and events for CPU core
**         settings.
**     Settings  :
**
**     Contents  :
**         EnableInt  - void Cpu_EnableInt(void);
**         DisableInt - void Cpu_DisableInt(void);
**
**     Copyright : 1997 - 2009 Freescale Semiconductor, Inc. All Rights Reserved.
**     
**     http      : www.freescale.com
**     mail      : support@freescale.com
** ###################################################################*/

/* MODULE Cpu. */

#pragma MESSAGE DISABLE C4002 /* WARNING C4002: Result not used is ignored */

#include "HSync.h"
#include "HSyncTimeout.h"
#include "CSyncEn.h"
#include "CSyncInv.h"
#include "VSync.h"
#include "RgbStatus.h"
#include "PE_Types.h"
#include "PE_Error.h"
#include "PE_Const.h"
#include "IO_Map.h"
#include "Events.h"
#include "Cpu.h"


/* Global variables */
volatile byte CCR_reg;                 /* Current CCR register */

/*Definition of global shadow variables*/
byte Shadow_PTA;


/*
** ===================================================================
**     Method      :  Cpu_Interrupt (component MC9S08QD4_8)
**
**     Description :
**         The method services unhandled interrupt vectors.
**         This method is internal. It is used by Processor Expert only.
** ===================================================================
*/
ISR(Cpu_Interrupt)
{
  /* This code can be changed using the CPU bean property "Build Options / Unhandled int code" */
  asm(BGND);
}


/*
** ===================================================================
**     Method      :  Cpu_DisableInt (component MC9S08QD4_8)
**
**     Description :
**         Disables maskable interrupts
**     Parameters  : None
**     Returns     : Nothing
** ===================================================================
*/
/*
void Cpu_DisableInt(void)

**      This method is implemented as macro in the header module. **
*/

/*
** ===================================================================
**     Method      :  Cpu_EnableInt (component MC9S08QD4_8)
**
**     Description :
**         Enables maskable interrupts
**     Parameters  : None
**     Returns     : Nothing
** ===================================================================
*/
/*
void Cpu_EnableInt(void)

**      This method is implemented as macro in the header module. **
*/

/*
** ===================================================================
**     Method      :  _EntryPoint (component MC9S08QD4_8)
**
**     Description :
**         Initializes the whole system like timing and so on. At the end 
**         of this function, the C startup is invoked to initialize stack,
**         memory areas and so on.
**         This method is internal. It is used by Processor Expert only.
** ===================================================================
*/
extern void _Startup(void);            /* Forward declaration of external startup function declared in file Start12.c */

#pragma NO_FRAME
#pragma NO_EXIT
void _EntryPoint(void)
{
  /* ### MC9S08QD4_8 "Cpu" init code ... */
  /*  PE initialization code after reset */

  /* Common initialization of the write once registers */
  /* SOPT1: COPE=0,COPT=1,STOPE=0,??=1,??=0,??=0,BKGDPE=0,RSTPE=0 */
  setReg8(SOPT1, 0x50);                 
  /* SPMSC1: LVDF=0,LVDACK=0,LVDIE=0,LVDRE=1,LVDSE=1,LVDE=1,??=0,BGBE=0 */
  setReg8(SPMSC1, 0x1C);                
  /* SPMSC2: LVWF=0,LVWACK=0,LVDV=0,LVWV=0,PPDF=0,PPDACK=0,??=0,PPDC=0 */
  setReg8(SPMSC2, 0x00);                
  /*  System clock initialization */
  if (*(unsigned char*)0xFFAF != 0xFF) { /* Test if the device trim value is stored on the specified address */
    ICSTRM = *(unsigned char*)0xFFAF;  /* Initialize ICSTRM register from a non volatile memory */
    ICSSC = *(unsigned char*)0xFFAE;   /* Initialize ICSSC register from a non volatile memory */
  }
  /* ICSC1: ??=0,CLKS=0,??=0,??=0,??=0,??=1,??=1,IREFSTEN=0 */
  setReg8(ICSC1, 0x06);                /* Initialization of the ICS control register 1 */ 
  /* ICSC2: BDIV=0,??=0,??=0,LP=0,??=0,??=0,??=0 */
  setReg8(ICSC2, 0x00);                /* Initialization of the ICS control register 2 */ 
  /*** End of PE initialization code after reset ***/
  __asm   jmp _Startup ;               /* Jump to C startup code */

}

/*
** ===================================================================
**     Method      :  PE_low_level_init (component MC9S08QD4_8)
**
**     Description :
**         Initializes beans and provides common register initialization. 
**         The method is called automatically as a part of the 
**         application initialization code.
**         This method is internal. It is used by Processor Expert only.
** ===================================================================
*/
void PE_low_level_init(void)
{
  /* Common initialization of the CPU registers */
  /* PTAPE: PTAPE3=0,PTAPE2=0,PTAPE1=1,PTAPE0=1 */
  clrSetReg8Bits(PTAPE, 0x0C, 0x03);    
  /* PTAD: PTAD4=0,PTAD3=0,PTAD2=1 */
  clrSetReg8Bits(PTAD, 0x18, 0x04);     
  /* PTADD: PTADD3=0,PTADD2=1,PTADD1=0,PTADD0=0 */
  clrSetReg8Bits(PTADD, 0x0B, 0x04);    
  /* PTASE: PTASE4=0,PTASE3=0,PTASE2=0,PTASE1=0,PTASE0=0 */
  clrReg8Bits(PTASE, 0x1F);             
  /* PTADS: ??=0,??=0,PTADS5=0,PTADS4=1,PTADS3=1,PTADS2=1,PTADS1=1,PTADS0=1 */
  setReg8(PTADS, 0x1F);                 
  /* ### Shared modules init code ... */
  /* ### Timer capture encapsulation "HSync" init code ... */
  HSync_Init();
  /* ### FreeCntr "HSyncTimeout" init code ... */
  HSyncTimeout_Init();
  /* ### BitIO "CSyncEn" init code ... */
  Shadow_PTA &= (byte)~0x08;           /* Initialize pin shadow variable bit */
  /* ### BitIO "CSyncInv" init code ... */
  /* ### Timer capture encapsulation "VSync" init code ... */
  VSync_Init();
  /* ### BitIO "RgbStatus" init code ... */
  /* Common peripheral initialization - ENABLE */
  /* TPMSC: CLKSB=0,CLKSA=1 */
  clrSetReg8Bits(TPMSC, 0x10, 0x08);    
  __EI();                              /* Enable interrupts */
}

/* Initialization of the CPU registers in FLASH */

/* NVPROT: FPS=0x7F,FPDIS=1 */
const unsigned char NVPROT_INIT @0x0000FFBD = 0xFF;

/* NVOPT: KEYEN=0,FNORED=1,??=1,??=1,??=1,??=1,SEC01=1,SEC00=0 */
const unsigned char NVOPT_INIT @0x0000FFBF = 0x7E;

/* END Cpu. */

/*
** ###################################################################
**
**     This file was created by Processor Expert 3.07 [04.34]
**     for the Freescale HCS08 series of microcontrollers.
**
** ###################################################################
*/
