#include "Vectors.h"


#define CHKSUM 2
#define IN_ROM 1
#define CLEAR  0 

/*   Local Function ProtoTypes */

void Vectors_Display( void );
void Intrupt_Vect( APTR , char *,int );
void Exec_Vect( APTR , char *,int );
void Except_Vect( APTR, char *, int);
void Other_Vect( APTR , char *,int );
BOOL Check_ROM_Location( APTR ,int );
void Handle_Vector_Input(int);


/* End Of Local Function ProtoTypes */



void Vectors_Display( void )
{
    char *Scrap_Text;
    Scrap_Text = AllocMem(200,MEMF_ANY);   
    if(!Scrap_Text)
        {
	  // Some Smart FATAL Error Routine
        }       
       
    Exec_Vect( &SysBase->ColdCapture, "ColdCapture       ",CLEAR); 
    Exec_Vect( &SysBase->CoolCapture, "CoolCapture       ",CLEAR);
    Exec_Vect( &SysBase->WarmCapture, "WarmCapture       ",CLEAR);
    Exec_Vect( &SysBase->DebugEntry,  "DebugEntry        ",IN_ROM); 
    Exec_Vect( &SysBase->DebugData,   "DebugData         ",CLEAR);
    Exec_Vect( &SysBase->AlertData,   "AlertData         ",CLEAR);
    Exec_Vect( &SysBase->KickMemPtr,  "KickMemPtr        ",CLEAR);
    Exec_Vect( &SysBase->KickTagPtr,  "KickTagPtr        ",CLEAR);
    Exec_Vect( &SysBase->ChkSum,      "ExecBase Checksum ",CHKSUM);  // Can Calculate This!
    Exec_Vect( &SysBase->KickCheckSum,"KickStart CheckSum",CLEAR);
    
    Except_Vect( (*(( int **)0x08)),"Bus Error                  $000008 ",IN_ROM);
    Except_Vect( (*(( int **)0x0C)),"Address Error              $00000C ",IN_ROM);
    Except_Vect( (*(( int **)0x10)),"Illegal Instruction        $000010 ",IN_ROM);
    Except_Vect( (*(( int **)0x14)),"Division By Zero           $000014 ",IN_ROM);
    Except_Vect( (*(( int **)0x18)),"CHK Instruction            $000018 ",IN_ROM);
    Except_Vect( (*(( int **)0x1C)),"TRAPV Instruction          $00001C ",IN_ROM);
    Except_Vect( (*(( int **)0x20)),"Privilege Violation        $000020 ",IN_ROM);
    Except_Vect( (*(( int **)0x24)),"Trace                      $000024 ",IN_ROM);
    Except_Vect( (*(( int **)0x28)),"Axxx-Instruction Emulation $000028 ",IN_ROM);
    Except_Vect( (*(( int **)0x2C)),"Fxxx-Instruction Emulation $00002C ",IN_ROM);
    
    Intrupt_Vect( (*(( int **)0x64)),"$000064 Level 1 Interrupt",IN_ROM);
    Intrupt_Vect( (*(( int **)0x68)),"$000068 Level 2 Interrupt",IN_ROM);
    Intrupt_Vect( (*(( int **)0x6C)),"$00006C Level 3 Interrupt",IN_ROM);
    Intrupt_Vect( (*(( int **)0x70)),"$000070 Level 4 Interrupt",IN_ROM);
    Intrupt_Vect( (*(( int **)0x74)),"$000074 Level 5 Interrupt",IN_ROM);
    Intrupt_Vect( (*(( int **)0x78)),"$000078 Level 6 Interrupt",IN_ROM);
    Intrupt_Vect( (*(( int **)0x7C)),"$00007C Level 7 Interrupt",IN_ROM);
    Other_Vect( (*(( int **)0x80)),"$000080 Trap 0 ",IN_ROM);
    Other_Vect( (*(( int **)0x84)),"$000084 Trap 1 ",IN_ROM);
    Other_Vect( (*(( int **)0x88)),"$000088 Trap 2 ",IN_ROM);
    Other_Vect( (*(( int **)0x8C)),"$00008C Trap 3 ",IN_ROM);
    Other_Vect( (*(( int **)0x90)),"$000090 Trap 4 ",IN_ROM);
    Other_Vect( (*(( int **)0x94)),"$000094 Trap 5 ",IN_ROM);
    Other_Vect( (*(( int **)0x98)),"$000098 Trap 6 ",IN_ROM);
    Other_Vect( (*(( int **)0x9C)),"$00009C Trap 7 ",IN_ROM);
    Other_Vect( (*(( int **)0xA0)),"$0000A0 Trap 8 ",IN_ROM);
    Other_Vect( (*(( int **)0xA4)),"$0000A4 Trap 9 ",IN_ROM);
    Other_Vect( (*(( int **)0xA8)),"$0000A8 Trap 10",IN_ROM);
    Other_Vect( (*(( int **)0xAC)),"$0000AC Trap 11",IN_ROM);
    Other_Vect( (*(( int **)0xB0)),"$0000B0 Trap 12",IN_ROM);
    Other_Vect( (*(( int **)0xB4)),"$0000B4 Trap 13",IN_ROM);
    Other_Vect( (*(( int **)0xB8)),"$0000B8 Trap 14",IN_ROM);
    Other_Vect( (*(( int **)0xBC)),"$0000BC Trap 15",IN_ROM);
        
    FreeMem(Scrap_Text,200);
   
    
}

void Exec_Vect( APTR Location, char *Vector_Name,int Check_Type)
{
    
    char *Scrap_Text;
    Scrap_Text = AllocMem(200,MEMF_ANY);
    if(!Scrap_Text) Problem("Un-Able To Allocate Enough Memory For A Scrap Text Buffer",0);
    if(Check_ROM_Location((*((APTR **)Location)),Check_Type)) 
       {
        sprintf(Scrap_Text,"$%p  %s  $%p",Location,Vector_Name,(*((APTR **)Location)));
         
            Print_Routine(Scrap_Text,Vector1View_Address);
       }
        else 
         {
            sprintf(Scrap_Text,"$%p  "MUIX_U"%s"MUIX_N"  "MUIX_B"$%p"MUIX_N"",Location,Vector_Name,(*((APTR **)Location)));
    
             Print_Routine(Scrap_Text,Vector1View_Address);
        }

        FreeMem(Scrap_Text,200);
   
}

void Intrupt_Vect( APTR Location, char *Vector_Name,int Check_Type)
{
    char *Scrap_Text;
    Scrap_Text = AllocMem(200,MEMF_CLEAR);
    if(!Scrap_Text) Problem("Un-Able To Allocate Enough Memory For A Scrap Text Buffer",0);
    sprintf(Scrap_Text,"%s  $%p ",Vector_Name,Location);
    if(Check_ROM_Location(Location,Check_Type))
      {
        sprintf(Scrap_Text,"%s  $%p",Vector_Name,Location);
         Print_Routine(Scrap_Text,Vector2View_Address);
      }
      else
      {
        sprintf(Scrap_Text,""MUIX_B"%s  $%p",Vector_Name,Location);
         Print_Routine(Scrap_Text,Vector2View_Address);
    }
    FreeMem(Scrap_Text,200);
    
}

void Except_Vect( APTR Location, char *Vector_Name,int Check_Type)
{
    char *Scrap_Text;
    Scrap_Text = AllocMem(200,MEMF_CLEAR);
    if(!Scrap_Text) Problem("Un-Able To Allocate Enough Memory For A Scrap Text Buffer",0);
    sprintf(Scrap_Text,"%s  $%p ",Vector_Name,Location);
    if(Check_ROM_Location(Location,Check_Type))
      {
        sprintf(Scrap_Text,"%s  $%p",Vector_Name,Location);
         Print_Routine(Scrap_Text,Vector3View_Address);
      }
      else
      {
        sprintf(Scrap_Text,""MUIX_B"%s  $%p",Vector_Name,Location);
         Print_Routine(Scrap_Text,Vector3View_Address);
    }
    FreeMem(Scrap_Text,200);
    
}

void Other_Vect( APTR Location, char *Vector_Name,int Check_Type)
{
    char *Scrap_Text;
    Scrap_Text = AllocMem(200,MEMF_CLEAR);
    if(!Scrap_Text) Problem("Un-Able To Allocate Enough Memory For A Scrap Text Buffer",0);
    sprintf(Scrap_Text,"%s  $%p ",Vector_Name,Location);
    if(Check_ROM_Location(Location,Check_Type))
      {
        sprintf(Scrap_Text,"%s  $%p",Vector_Name,Location);
         Print_Routine(Scrap_Text,Vector4View_Address);
      }
      else
      {
        sprintf(Scrap_Text,""MUIX_B"%s  $%p",Vector_Name,Location);
         Print_Routine(Scrap_Text,Vector4View_Address);
    }
    FreeMem(Scrap_Text,200);
    
}

BOOL Check_ROM_Location( APTR Address ,int Check_Type )
{
 char *Scrap_Text;
 WORD x;
 struct ExecBase *SysBase = (*((struct ExecBase **)4));
 Scrap_Text = AllocMem(200,MEMF_ANY);   
 sprintf(Scrap_Text,"%p",Address);
  if(Check_Type==IN_ROM)
   {
    if(Scrap_Text[2]=='F'||Scrap_Text[2]=='f')
       {
         FreeMem(Scrap_Text,200);
         return(TRUE);
       }
     else
       {
         FreeMem(Scrap_Text,200);
         return(FALSE);
       }
   }
   
 if(Check_Type==CLEAR)
   {
    if(atoi(Scrap_Text)==0)
       {
         FreeMem(Scrap_Text,200);
         return(TRUE);
       }
     else
       {
         FreeMem(Scrap_Text,200);
         return(FALSE);
       }
   }
   
 if(Check_Type==CHKSUM)
   {
    x  = SysBase->SoftVer;
    x += SysBase->LowMemChkSum;
    x += SysBase->ChkBase;
    x += (int)SysBase->ColdCapture;
    x += (int)SysBase->CoolCapture;
    x += (int)SysBase->WarmCapture;
    x =  !!x;  /* Problem Here To Be Fixed L8rs */
 // printf("I Work Out The CheckSum To Be %x\n",x);
    if(x!=SysBase->ChkSum)
       {
        FreeMem(Scrap_Text,200);
        return(FALSE);
      }
      else
      {
        FreeMem(Scrap_Text,200);
        return(TRUE);
      }
    } 
    
    
 if(Check_Type!=CLEAR||Check_Type!=IN_ROM||Check_Type!=CHKSUM)
    {
        FreeMem(Scrap_Text,200);
        Problem("Check Type In Check_ROM_Location() Does No Exist.",1);
        return(TRUE);
    }
    
 
}

void Handle_Vector_Input(int Index)
{
    struct Node *Quick_Node;
    char   Error[]="System Standing By.......";
  if(Index>=1)
   {  
    Quick_Node = List_View_Location(Index);
       }
   else
   {
    
    }

}










/* I Wonder if I Can Now Make This System Crash!!! 
   I Really Hope That I Cannot Make The System Crash
   Cos That Would Mean That I Would Have Worked Out
   What the Hell was Wrong. There Are A Number of Things
   That It Could Have Been But I Think I have got them 
   sorted out just now! :)
   However I Could Be Very Wrong. Please Kill This
   Message if the System Does Not Rocover!! */


/*      The System Vectors That Are Going To Be Monitored By This 
        Program Are The Following:
        
        ColdCapture Vector
        CoolCapture Vector
        WarmCapture Vector
        Debug Entry Data
        Debug Entry Code
        Alert Data
        The Chksum
        
        Pointer To The Resident Modules
        Show The Software Interupts
        Kickmemptr
        KickTagPtr
        Kickchecksum
        Thats it for this page
        
*/
