#include "7604.h"

char  a[10];
int   b[10];
short c[10];
long  d[10];

void sci_set(void)
{
  SCI.SMR.BYTE = 0;
  SCI.SMR.BIT.CA   = 1;
  SCI.SMR.BIT.CHR  = 1;
  SCI.SMR.BIT.PE   = 1;
  SCI.SMR.BIT.OE   = 1;
  SCI.SMR.BIT.STOP = 1;
  SCI.SMR.BIT.MP   = 1;
  SCI.SMR.BIT.CKS  = 3;
  SCI.BRR = 0;
  SCI.SCR.BYTE = 0;
  SCI.SCR.BIT.TIE  = 1;
  SCI.SCR.BIT.RIE  = 1;
  SCI.SCR.BIT.TE   = 1;
  SCI.SCR.BIT.RE   = 1;
  SCI.SCR.BIT.MPIE = 1;
  SCI.SCR.BIT.TEIE = 1;
  SCI.SCR.BIT.CKE  = 3;
  SCI.TDR = 0;
  SCI.SSR.BYTE = 0;
  SCI.SSR.BIT.TDRE = 1;
  SCI.SSR.BIT.RDRF = 1;
  SCI.SSR.BIT.ORER = 1;
  SCI.SSR.BIT.FER  = 1;
  SCI.SSR.BIT.PER  = 1;
  SCI.SSR.BIT.TEND = 1;
  SCI.SSR.BIT.MPB  = 1;
  SCI.SSR.BIT.MPBT = 1;
  SCI.RDR = 0;
}

void frt_set(void)
{
  FRT.TIER.BYTE = 0;
  FRT.TIER.BIT.ICIE  = 1;
  FRT.TIER.BIT.OCIAE = 1;
  FRT.TIER.BIT.OCIBE = 1;
  FRT.TIER.BIT.OVIE  = 1;
  FRT.FTCSR.BYTE = 0;
  FRT.FTCSR.BIT.ICF  = 1;
  FRT.FTCSR.BIT.OCFA  = 1;
  FRT.FTCSR.BIT.OCFB  = 1;
  FRT.FTCSR.BIT.OVF   = 1;
  FRT.FTCSR.BIT.CCLRA = 1;
  FRT.FRC = 0;
  FRT.OCR = 0; /* if OCRS of TOCR is 0 then OCRA */
  FRT.OCR = 0; /* if OCRS of TOCR is 1 then OCRB */
  FRT.TCR.BYTE = 0;
  FRT.TCR.BIT.IEDGA = 1;
  FRT.TCR.BIT.CKS   = 3;
  FRT.TOCR.BYTE = 0;
  FRT.TOCR.BIT.OCRS  = 1;
  FRT.TOCR.BIT.OLVLA = 1;
  FRT.TOCR.BIT.OLVLB = 1;
  FRT.FICR = 0;
}

void intc_set(void)
{
  INTC.IPRA.WORD = 0;
  INTC.IPRA.BIT.UU = 15;  /* Set DIVU Interrupt Level */
  INTC.IPRA.BIT.UL = 15;  /* Set DMAC Interrupt Level */
  INTC.IPRA.BIT.LU = 15;  /* Set WDT  Interrupt Level */
  INTC.IPRB.WORD = 0;
  INTC.IPRB.BIT.UU = 15;  /* Set SCI Interrupt Level */
  INTC.IPRB.BIT.UL = 15;  /* Set FRT Interrupt Level */
  INTC.VCRA.WORD = 0;
  INTC.VCRA.BYTE.H = 127;   /* Set SER Vector */
  INTC.VCRA.BYTE.L = 127;   /* Set SRX Vector */
  INTC.VCRB.WORD = 0;
  INTC.VCRB.BYTE.H = 127;   /* Set STX Vector */
  INTC.VCRB.BYTE.L = 127;   /* Set STE Vector */
  INTC.VCRC.WORD = 0;
  INTC.VCRC.BYTE.H = 127;   /* Set FIC Vector */
  INTC.VCRC.BYTE.L = 127;   /* Set FOC Vector */
  INTC.VCRD.WORD = 0;
  INTC.VCRD.BYTE.H = 127;   /* Set FOV Vector */
  INTC.ICR.WORD = 0;
  INTC.ICR.BIT.NMIL = 1;
  INTC.ICR.BIT.NMIE = 1;
  INTC.ICR.BIT.VECMD = 1;
  INTC.VCRWDT.WORD = 0;
  INTC.VCRWDT.BYTE.H = 127;   /* Set WIT Vector */
  INTC.VCRWDT.BYTE.L = 127;   /* Set BCM Vector */
}

void dmac_set(void)
{
  DMAC.DMAOR.LONG = 0;
  DMAC.DMAOR.BIT.PR   = 1;
  DMAC.DMAOR.BIT.AE   = 1;
  DMAC.DMAOR.BIT.NMIF = 1;
  DMAC.DMAOR.BIT.DME  = 1;

  DMAC0.DRCR.BYTE = 0;
  DMAC0.DRCR.BIT.RS = 3;
  DMAC0.SAR = &a[0];
  DMAC0.DAR = &b[0];
  DMAC0.TCR = 0;
  DMAC0.CHCR.LONG = 0;
  DMAC0.CHCR.BIT.DM = 3;
  DMAC0.CHCR.BIT.SM = 3;
  DMAC0.CHCR.BIT.TS = 3;
  DMAC0.CHCR.BIT.AR = 1;
  DMAC0.CHCR.BIT.AM = 1;
  DMAC0.CHCR.BIT.AL = 1;
  DMAC0.CHCR.BIT.DS = 1;
  DMAC0.CHCR.BIT.DL = 1;
  DMAC0.CHCR.BIT.TB = 1;
  DMAC0.CHCR.BIT.TA = 1;
  DMAC0.CHCR.BIT.IE = 1;
  DMAC0.CHCR.BIT.TE = 1;
  DMAC0.CHCR.BIT.DE = 1;
  DMAC0.VCRDMA = 0;

  DMAC1.DRCR.BYTE = 0;
  DMAC1.DRCR.BIT.RS = 3;
  DMAC1.SAR = &c[0];
  DMAC1.DAR = &d[0];
  DMAC1.TCR = 0;
  DMAC1.CHCR.LONG = 0;
  DMAC1.CHCR.BIT.DM = 3;
  DMAC1.CHCR.BIT.SM = 3;
  DMAC1.CHCR.BIT.TS = 3;
  DMAC1.CHCR.BIT.AR = 1;
  DMAC1.CHCR.BIT.AM = 1;
  DMAC1.CHCR.BIT.AL = 1;
  DMAC1.CHCR.BIT.DS = 1;
  DMAC1.CHCR.BIT.DL = 1;
  DMAC1.CHCR.BIT.TB = 1;
  DMAC1.CHCR.BIT.TA = 1;
  DMAC1.CHCR.BIT.IE = 1;
  DMAC1.CHCR.BIT.TE = 1;
  DMAC1.CHCR.BIT.DE = 1;
  DMAC1.VCRDMA = 0;
}

void divu_set(void)
{
  DIVU.DVSR = 0;
  DIVU.DVDNT = 0;
  DIVU.DVCR.WORD = 0;
  DIVU.DVCR.BIT.OVFIE = 1;
  DIVU.DVCR.BIT.OVF   = 1;
  DIVU.VCRDIV = 0;
  DIVU.DVDNTH = 0;
  DIVU.DVDNTL = 0;
}

void ubc_set(void)
{
  UBC.BARA = ubc_set;
  UBC.BAMRA = 0;
  UBC.BBRA.WORD = 0;
  UBC.BBRA.BIT.CPA = 3;
  UBC.BBRA.BIT.IDA = 3;
  UBC.BBRA.BIT.RWA = 3;
  UBC.BBRA.BIT.SZA = 3;
  UBC.BARB = ubc_set;
  UBC.BAMRB = 0;
  UBC.BBRB.WORD = 0;
  UBC.BBRB.BIT.CPB = 3;
  UBC.BBRB.BIT.IDB = 3;
  UBC.BBRB.BIT.RWB = 3;
  UBC.BBRB.BIT.SZB = 3;
  UBC.BDRB = 0;
  UBC.BDMRB = 0;
  UBC.BRCR.WORD = 0;
  UBC.BRCR.BIT.CMFCA = 1;
  UBC.BRCR.BIT.CMFPA = 1;
  UBC.BRCR.BIT.EBBE  = 1;
  UBC.BRCR.BIT.PCBA  = 1;
  UBC.BRCR.BIT.CMFCB = 1;
  UBC.BRCR.BIT.CMFPB = 1;
  UBC.BRCR.BIT.SEQ   = 1;
  UBC.BRCR.BIT.DBEB  = 1;
  UBC.BRCR.BIT.PCBB  = 1;
}

void bsc_set(void)
{
  BSC.BCR1.LONG = 0;
  BSC.BCR1.BIT.MASTER = 1;
  BSC.BCR1.BIT.ENDIAN = 1;
  BSC.BCR1.BIT.BSTROM = 1;
  BSC.BCR1.BIT.PSHR   = 1;
  BSC.BCR1.BIT.AHLW   = 3;
  BSC.BCR1.BIT.A1LW   = 3;
  BSC.BCR1.BIT.A0LW   = 3;
  BSC.BCR1.BIT.DRAM   = 7;
  BSC.BCR2.LONG = 0;
  BSC.BCR2.BIT.A3SZ = 3;
  BSC.BCR2.BIT.A2SZ = 3;
  BSC.BCR2.BIT.A1SZ = 3;
  BSC.WCR.LONG = 0;
  BSC.WCR.BIT.IW3 = 3;
  BSC.WCR.BIT.IW2 = 3;
  BSC.WCR.BIT.IW1 = 3;
  BSC.WCR.BIT.IW0 = 3;
  BSC.WCR.BIT.W3  = 3;
  BSC.WCR.BIT.W2  = 3;
  BSC.WCR.BIT.W1  = 3;
  BSC.WCR.BIT.W0  = 3;
  BSC.MCR.LONG = 0;
  BSC.MCR.BIT.TRP  = 1;
  BSC.MCR.BIT.RCD  = 1;
  BSC.MCR.BIT.TRWL = 1;
  BSC.MCR.BIT.TRAS = 3;
  BSC.MCR.BIT.BE   = 1;
  BSC.MCR.BIT.RASD = 1;
  BSC.MCR.BIT.AMX2 = 1;
  BSC.MCR.BIT.SZ   = 1;
  BSC.MCR.BIT.AMX1 = 1;
  BSC.MCR.BIT.AMX0 = 1;
  BSC.MCR.BIT.RFSH = 1;
  BSC.MCR.BIT.RMD  = 1;
  BSC.RTCSR.LONG = 0;
  BSC.RTCSR.BIT.CMF  = 1;
  BSC.RTCSR.BIT.CMIE = 1;
  BSC.RTCSR.BIT.CKS  = 7;
  BSC.RTCNT = 0;
  BSC.RTCOR = 0;
}

void sbycr_set(void)
{
  SBYCR.BYTE = 0;
  SBYCR.BIT.SBY   = 1;
  SBYCR.BIT.HIZ   = 1;
  SBYCR.BIT.MSTP4 = 1;
  SBYCR.BIT.MSTP3 = 1;
  SBYCR.BIT.MSTP2 = 1;
  SBYCR.BIT.MSTP1 = 1;
  SBYCR.BIT.MSTP0 = 1;
}

void ccr_set(void)
{
  CCR.BYTE = 0;
  CCR.BIT.W  = 3;
  CCR.BIT.CP = 1;
  CCR.BIT.TW = 1;
  CCR.BIT.OC = 1;
  CCR.BIT.ID = 1;
  CCR.BIT.CE = 1;
}

void main(void)
{
  sci_set();
  frt_set();
  intc_set();
  dmac_set();
  divu_set();
  ubc_set();
  bsc_set();
  sbycr_set();
  ccr_set();
}
