/************/
/*mirx68k1.c*/
/************/
#include "m9402.h"
#include "table.h"

int  inzdsp(),eichi;
void timdsp(),nrmdsp(),intdsp();
interrupt void reig1(),reig2(),reig3(),reig4(),reig5(),reig6(),reig7(),reig8();
interrupt void reig9(),reig10(),reig11(),reig12(),reig13(),reig14(),reig15();
interrupt void reig16(),l1(),l2(),l3(),l4(),l5(),RTCint(),l7(),unsi();
void fin(),e_fin(int e2,int e1);
void sys0(),sys2(),sys3(),sys4(),sys5(),sys8();
int  sys1(),sys9(),sys10(),sys11();

void (*pinz)(),(*pnrm)(),(*ptim)(),(*pint)(),(*(*vp))();

unsigned que[7][MAXTSK],lastq[7];
int      ncom[7][MAXTSK], tcom[MAXTSK], icom[MAXTSK];
int      nscom[7][MAXTSK],tscom[MAXTSK],iscom[MAXTSK];
int      code[2];

extern void rtc_clr();

unsigned long Sysclk=0;
int vmemask=0;


int inportb(port)
char *port;
{
   return((int)*port);
}

int outportb(port,data)
char *port;
int data;
{
   return(*port = data);
}

int outportl(port,data)
char *port;
int data;
{
   unsigned char tbyte,hbyte,mbyte,lbyte;
   
   tbyte = (unsigned char)((data&0xff000000)>>24);
   hbyte = (unsigned char)((data&0x00ff0000)>>16);
   mbyte = (unsigned char)((data&0x0000ff00)>>8);
   lbyte = (unsigned char)(data&0x000000ff);
   *port = tbyte;
   *(port + 0x02) = hbyte;
   *(port + 0x04) = mbyte;
   *(port + 0x06) = lbyte;
   return(data);
}

int absl(a)
int a;
{
   int ans;
   if(a<0) ans = 0 - a;
   else ans = a;
   return(ans);
}

/***********************/
/*mirx68k1 main program*/
/***********************/

void main()
{
   int usetsk;
   eichi = 30;
   
  rtc_clr(); /*mirs9402 1/31*/
   /*tm_init*/
/*   outportb(MSR,0x40);
   outportb(PFIC,0x10);
   outportb(TSIC,0x00);
   outportb(MSR,0x00);*/
 
       outportb(MSR,0x0c);
       inportb(0xfdfe07);
       outportb(MSR,0x4c);
       outportb(RTMR,0x08);
       outportb(PFIC,0x10);
       outportb(MSR,0x0c);
 
   usetsk=inzdsp();
   eichi = 70;
/*   asm("   MOVE.W  #$2000,SR   "); */  /* Interrupt Able */ 
     asm("\tMOVE\t#$2000,SR\t");     /*enable interrupt*/
 
   while( usetsk != 0)
   {
      timdsp();
      nrmdsp();
   }
}

int inzdsp()
{
   int ut,tn,py;
   ut=0;

   for (tn=0;tn 0x80) ut++;
   }
   return(ut);
}

void timdsp()
{
   int tn;

   for (tn=0;tn= table[tn].timset))
      {
         ptim=table[tn].timtsk;
         table[tn].flg |= 0x20;         /*   set busyflag */
         (*ptim)(tcom[tn],tscom[tn]);
         table[tn].flg &= 0xdf;         /* reset busyflag */
         table[tn].timer=0;
      }
}

void nrmdsp()
{
   int nb,tn,py,savesrn;

   nb=tn=py=0;
   for (py=0;py<7;py++) 
      if (lastq[py] != 0)
      {
         tn=que[py][0];
         if ((table[tn].flg & 0xe0) == 0xc0)
         {
            pnrm=table[tn].nrmtsk;
            table[tn].flg |= 0x20;      /*   set busyflag  */
            (*pnrm)(ncom[py][0],nscom[py][0]);
            table[tn].flg &= 0xdf;      /* reset busyflag  */
         }

         asm("   MOVE.W  SR,`savesrn` ");  /*Save SR*/
         asm("   MOVE.W  #$2700,SR   ");  /*Interrupt disable*/ 
         lastq[py]--;
         for (nb=0;nb<lastq[py];nb++)
         {
            que[py][nb]  =que[py][nb+1];
            ncom[py][nb] =ncom[py][nb+1];
            nscom[py][nb]=nscom[py][nb+1];
         }

         asm("   MOVE.W  `savesrn`,SR   ");  /*Restore SR*/
         /*break;*/
      }
};
/*
void intdsp(intnum)
int intnum;
{
   int inttask;

   inttask = inter[intnum].tskn;
   if ((table[inttask].flg & 0xb8) == 0x88)
   {
      pint=table[inttask].inttsk;
      table[inttask].flg |= 0x20;       *//*   set busyflag *//*
      (*pint)(icom[inttask],iscom[inttask]);
      table[inttask].flg &= 0xdf;       *//* reset busyflag *//*
   }
}
*/

void e_fin(e2,e1)
int e1,e2;
{
	code[1]=e2; code[0]=e1;
	fin();
}

void fin()                      
{
   asm("   MOVE.W  #$2700,SR   ");  /*Interrupt Enable*/
   for(;;);
}

interrupt void reig1()			/*reset ssp*/
{ e_fin(0,1);}

interrupt void reig2()			/*reset pc*/
{ e_fin(0,2);}

interrupt void reig3()			/*Bus error*/
{ e_fin(0,3);}

interrupt void reig4()			/*Address error*/
{ e_fin(0,4);}

interrupt void reig5()			/*illegal error*/
{ e_fin(0,5);}

interrupt void reig6()			/*zero divide*/
{ e_fin(0,6);}

interrupt void reig7()			/*CHK*/
{ e_fin(0,7);}

interrupt void reig8()			/*trapV*/
{ e_fin(0,8);}

interrupt void reig9()			/*privilege*/
{ e_fin(0,9);}

interrupt void reig10()			/*trace*/
{ e_fin(1,0);}

interrupt void reig11()			/*l.10100 emu.*/
{ e_fin(1,1);}

interrupt void reig12()			/*l.1111 emu.*/
{e_fin(1,2);}

interrupt void reig13()			/*format error*/
{ e_fin(1,3);}

interrupt void reig14()			/*uninitialized*/
{ e_fin(1,4);}

interrupt void reig15()			/*sprious*/
{ e_fin(1,5);}

interrupt void reig16()			/*trap*/
{ e_fin(1,6);}

interrupt void unsi()			/*unsigned, reserved*/
{ e_fin(2,0);}

interrupt void l1()
{ e_fin(2,1);}

interrupt void l2()
{ e_fin(2,2);}
	
interrupt void l3()
{ e_fin(2,3);}

interrupt void l4()
{ e_fin(2,4);}

interrupt void l5()
{ e_fin(2,5);}

interrupt void RTCint()	/*RTC interrupt*/
{
   int tn,count,i,j,o,p;
   o=0;p=0;
   code[1]=2;code[0]=6;
   o = inportb(MSR);
   p = inportb(PFIC);
	if (((o & 0x04)==0x04) && ((p & 0x10)==0x10))
	{
   		Sysclk+=10;				/*mintim=10msec*/
   
   		count = sys9(0,2);                   	/*  count up  */
   		count++;
   		sys8(0,2,count);

                count = sys9(0,3);      /* mirs9402 original */
                count+=10;              /* mirs9402 original */
                sys8(0,3,count);        /* mirs9402 original */
   	
  	 	for (tn=0;tn= 0x80)
		e_fin(2,7);
	else
		e_fin(2,8);
}

void sys0(tskn,flag)                /* system call 0:task initialize        */
int tskn,flag;
{
   int fg7,fg6,fg3,fg2;

   fg7=fg6=fg3=fg2=1;
   if ((flag&0x1000) == 0) fg7=0;
   if ((flag&0x100) == 0) fg6=0;
   if ((flag&0x10) == 0) fg2=0;
   if ((flag&0x1) == 0) fg3=0;
   fg7<<=7; fg6<<=6; fg3<<=3; fg2<<=2;
   table[tskn].flg=fg7+fg6+fg3+fg2;

}

int sys1(tn,py,cm,scm)              /* system call 1:set normal task    */
int tn,py,cm,scm;
{
   int n,i,savesr;


   asm("   MOVE.W  SR,`savesr` ");  /*Save SR*/
   asm("   MOVE.W  #$2700,SR   ");  /*Interrupt disable*/ 
   n=lastq[py]++;
   if (n>=MAXTSK) fin();
   que[py][n] =tn;
   ncom[py][n] =cm;
   nscom[py][n]=scm;
   if(eichi != 30)
   {
   asm("   MOVE.W  `savesr`,SR   ");  /*Restore SR*/
   }
   return(0);
}

void sys2(tn,st,cm,scm)             /* system call 2:set timer task   */
int tn,st,cm,scm;
{
   table[tn].timset = st;
   tcom[tn]=cm;
   tscom[tn]=scm;
   sys3(tn,0);
}

void sys3(tskn,reset)               /* system call 3:count start       */
int tskn,reset;
{
   table[tskn].flg |= 0x01;
   if (reset!=0) table[tskn].timer=0;
}

void sys4(tskn)                     /* system call 4:count stop       */
int tskn;
{
   table[tskn].flg &= 0xfe;
}

void sys5(tskn,aflg,lv,cm,scm)          /* system call 5:set interrupt task   */
int tskn,aflg,lv,cm,scm;
{
   if(aflg == 1) {

               table[tskn].flg &= 0xef;
               icom[tskn]=cm;
               iscom[tskn]=scm;

               vp=(void *)(0x60+4*lv);

               *vp=table[tskn].inttsk; 
   }
   if(aflg == 0) {

       table[tskn].flg &= 0xef;
       icom[tskn]=cm;
       iscom[tskn]=scm;

  	vp = (void *)(lv * 4); 
	*vp=table[tskn].inttsk; 		
   }
}


void sys8(tskn,bbn,dat)             /* system call 8:‚a‚a write           */
int tskn,bbn,dat;
{
   switch (bbn)
   {
      case 1:table[tskn].BB1 = dat;break;
      case 2:table[tskn].BB2 = dat;break;
      case 3:table[tskn].BB3 = dat;break;
      case 4:table[tskn].BB4 = dat;break;
      case 5:table[tskn].BB5 = dat;break;
   }
}

int  sys9(tskn,bbn)                 /* system call 9:‚a‚a read           */
int tskn,bbn;
{
   switch (bbn)
   {
      case 1:return(table[tskn].BB1);
      case 2:return(table[tskn].BB2);
      case 3:return(table[tskn].BB3);
      case 4:return(table[tskn].BB4);
      case 5:return(table[tskn].BB5);
   }
   return(0);
}



int sys10(irqn)    /* VME IRQx mask */

int irqn;

{

  int i;

  i=(1<<irqn)^0xff;

  vmemask&=i;

  outportb(VIMR,vmemask);	/*	VME IRQ   disable	*/

  return(vmemask);
}


int sys11(irqn)    /* VME IRQx unmask */

int irqn;

{

  int i;

  i=1<<irqn;

  vmemask|=i;

  outportb(VIMR,vmemask);	/*	VME IRQ1  enable	*/

  return(vmemask);
 }


ŠJ”­‹K–ñ‚É–ß‚é