/*********************************************************************************
Module# (init.c)+13,16

        Title: イニシャライズモジュール

        File : INIT.C  Ver.1.0  written on Jan.19.1996 by M.M.

**********************************************************************************/
#include "m9401.h"
#define PG_DUM  5

extern float Ers_v,Ers_th;
extern float Xc,Yc;
extern float V,Th,Om;
extern int Z_ref;
extern float Z;
extern float Th0;
extern int flag;
extern int Seg[];
extern int Led1;
extern int Led2;

extern unsigned char outportu();/*  */
extern int inportb();/*  */
extern int outportb();/*  */
extern int disp();
extern int Act;

void rtc_clr();

int init()
{
        int ch,i;

        rtc_clr();

     /*  MMI PIT0 */
        outportb(PGCR0 ,0x00);  /*      clear PGCR0     */
        outportb(PADDR0,0xff);  /*      all output      */
        outportb(PBDDR0,0x10);  /*      RRRWRRRR        */
        outportb(PACR0 ,0x80);
        outportb(PBCR0 ,0x80);
        outportb(PGCR0 ,0x02);  /*      mode 0          */

        outportb(TCR0,0x00);    /*      clear TCR0      */
        outportb(CPRH0,0x00);
        outportb(CPRM0,0x00);
        outportb(CPRL0,0x3f);   /*      TOUT 2kHz       */
        outportb(TCR0,0x41);    /*      square wave     */
      /*  PWM PIT1 */
        outportb(PGCR1 ,0x00);  /*      clear PGCR1     */
        outportb(PADDR1,0xff);  /*      all output      */
        outportb(PBDDR1,0xff);  /*      all output      */
        outportb(PACR1 ,0x80);  /*      submode 1x      */
        outportb(PBCR1 ,0x80);  /*      submode 1x      */
        outportb(PGCR1 ,0x00);  /*  mode0 & H1,H2,H3,H4  disable*/

        outportb(TCR1  ,0x00);  
        outportb(CPRH1  ,0x00);
        outportb(CPRM1  ,0x00);
        outportb(CPRL1  ,0x01); /* Toutの出力は125[kHz] */      
        outportb(TCR1  ,0x41);  /*      square wave     */

                  /* SSS & PHOTO PIT2 */
        outportb(PGCR2 ,0x00);  /*      clear PGCR2     */
        outportb(PSRR2, 0x1c); 
        outportb(PACR2 ,0x20);
        outportb(PADDR2,0x03);
        outportb(PBCR2, 0xA0);   
        outportb(PBDDR2,0x00);
        outportb(PGCR2 ,0x18);  /*mode0 &  H3,H4  disable*/

        outportb(TCR2  ,0x00);
        outportb(CPRH2 ,0x00);
        outportb(CPRM2 ,0x14);
        outportb(CPRL2 ,0x4c);  /* アンダーフローまで 16[msec] としている */
        outportb(TCR2  ,0xba);

        outportb(TIVR2,72+0);    /*      sss interrupt causes by tout*/ 

       outportb(MSR,0x0c);
       inportb(0xfdfe07);
       outportb(MSR,0x4c);
       outportb(RTMR,0x08);
       outportb(PFIC,0x10);
       outportb(MSR,0x0c);
 



/*      RE/TS   */
        outportb(TSIE,0x00);    /*      Touch Sensor Interrupt Enable   */
        outportb(CRST,0x03);    /*      counter reset   */      

        outportb(TCR2  ,0xbb); 
         /* outportb(RTMR ,0x08);*/

        outportu(PADR1,0);
        outportu(PBDR1,0);

                /* Dummy Read of Encoders */
        outportb(CRST,0x03);
        for (ch=0;ch< PG_DUM;ch++)

        {
                inportb(EDRH);
                inportb(EDRL);
                inportb(EDLH);
                inportb(EDLL);
        }
        outportb(CRST,0x03);
        /* Initial State */
        V = Om = 0.;

        Th = Th0 = PIH;

        Xc = X0;

        Yc = Y0;

        Z = 0;

        Z_ref=0;

        for(i=0;i<=4;i++)

                Seg[i]=10;

        Led1=0;

        Led2=0;

        disp();

        Ers_th = Ers_v = 0.; /* 目標値との偏差の累積 */

    flag=ON;

    Act=0;

        
        return(ON);

}

void rtc_clr()
{
int     rtc_base;
        int     i;
 
        rtc_base=0xfdfe00;

 
        outportb(MSR,0x44);
        for ( i=3 ; rtc_base+ i<0xfdfe40 ; i+=2 ) outportb(rtc_base+i,0x00);
 
        outportb(MSR,0x04);
        for ( i=3 ; rtc_base+ i<0xfdfe0a ; i+=2 ) outportb(rtc_base+i,0x00);
 
}