/**
 * Scout - The Amiga System Monitor
 *
 *------------------------------------------------------------------
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 *
 * You must not use this source code to gain profit of any kind!
 *
 *------------------------------------------------------------------
 *
 * @author Andreas Gelhausen
 * @author Richard Körber <rkoerber@gmx.de>
 */



#include "system_headers.h"

void ClearResetVectors (void) {
   SysBase->ColdCapture = NULL;
   SysBase->CoolCapture = NULL;
   SysBase->WarmCapture = NULL;
   SysBase->KickMemPtr = NULL;
   SysBase->KickTagPtr = NULL;
   SysBase->KickCheckSum = NULL;
}

void PrintVectors (char *filename) {
   char  *VBR;
   BPTR  handle;

   VBR = (char *) GetVBR ();

   handle = HandlePrintStart (filename);
   if (handle) {
      if (clientstate) {
         char  tmpbuffer[256];

         if (SendDaemon ("Vectors")) {
            while ((sgets (client_socket, tmpbuffer, 256)) \
              && (strcmp (tmpbuffer, CMD_DONE)) \
              && (strcmp (tmpbuffer, CMD_ERROR))) {
               PrintOneLine (handle, tmpbuffer);
            }
         }
      } else {
         if (PrintOneLine (handle, "\nReset Vectors:\n\n")) {
            _sprintf (tmpstr2, " %18s $%08lx %18s $%08lx\n %18s $%08lx %18s $%08lx\n %18s $%08lx %18s $%08lx\n", "ColdCapture:",SysBase->ColdCapture,"KickMemPtr:",SysBase->KickMemPtr,"CoolCapture:",SysBase->CoolCapture,"KickTagPtr:",SysBase->KickTagPtr,"WarmCapture:",SysBase->WarmCapture,"KickCheckSum:",SysBase->KickCheckSum);
            PrintOneLine (handle, tmpstr2);

            PrintOneLine (handle, "\nAuto Vector Interrupts:\n\n");

            _sprintf (tmpstr2, " %18s $%08lx %18s $%08lx\n %18s $%08lx %18s $%08lx\n %18s $%08lx %18s $%08lx\n %18s $%08lx\n", "Level1 ($0064):",*((ULONG *) (VBR + 0x0064)),"Level5 ($0074):",*((ULONG *) (VBR + 0x0074)),"Level2 ($0068):",*((ULONG *) (VBR + 0x0068)),"Level6 ($0078):",*((ULONG *) (VBR + 0x0078)),"Level3 ($006c):",*((ULONG *) (VBR + 0x006c)),"Level7 ($007c):",*((ULONG *) (VBR + 0x007c)),"Level4 ($0070):",*((ULONG *) (VBR + 0x0070)));
            PrintOneLine (handle, tmpstr2);

            PrintOneLine (handle, "\nInterrupt Vectors:\n\n");

            _sprintf (tmpstr2, " %18s $%08lx %18s $%08lx\n %18s $%08lx %18s $%08lx\n %18s $%08lx %18s $%08lx\n %18s $%08lx %18s $%08lx\n", "TBE  0:",SysBase->IntVects[0].iv_Code,"AUDIO1  8:",SysBase->IntVects[8].iv_Code,"DSKBLK  1:",SysBase->IntVects[1].iv_Code,"AUDIO2  9:",SysBase->IntVects[9].iv_Code,"SOFTINT  2:",SysBase->IntVects[2].iv_Code,"AUDIO3 10:",SysBase->IntVects[10].iv_Code,"PORTS  3:",SysBase->IntVects[3].iv_Code,"RBF 11:",SysBase->IntVects[11].iv_Code);
            PrintOneLine (handle, tmpstr2);

            _sprintf (tmpstr2, " %18s $%08lx %18s $%08lx\n %18s $%08lx %18s $%08lx\n %18s $%08lx %18s $%08lx\n %18s $%08lx %18s $%08lx\n", "COPPER  4:",SysBase->IntVects[4].iv_Code,"DSKSYNC 12:",SysBase->IntVects[12].iv_Code,"VERTB  5:",SysBase->IntVects[5].iv_Code,"EXTER 13:",SysBase->IntVects[13].iv_Code,"BLITTER  6:",SysBase->IntVects[6].iv_Code,"INTEN 14:",SysBase->IntVects[14].iv_Code,"AUDIO0  7:",SysBase->IntVects[7].iv_Code,"NMI 15:",SysBase->IntVects[15].iv_Code);
            PrintOneLine (handle, tmpstr2);
         }
      }
   }
   HandlePrintStop();
}

void GetVectors (void) {
   char  *VBR;

   if (clientstate) {
      char  tmpbuffer[256];

      if (SendDaemon ("GetVectorList")) {
         if (sgets (client_socket, tmpbuffer, 256)) {
            MySetContents (vectext1, tmpbuffer);
            if (sgets (client_socket, tmpbuffer, 256)) {
               MySetContents (vectext2, tmpbuffer);
               if (sgets (client_socket, tmpbuffer, 256)) {
                  MySetContents (vectext3, tmpbuffer);
                  if (sgets (client_socket, tmpbuffer, 256)) {
                     MySetContents (vectext4, tmpbuffer);
                     if (sgets (client_socket, tmpbuffer, 256)) {
                        strcmp (tmpbuffer, CMD_DONE);
                     }
                  }
               }
            }
         }
      }
   } else {
      VBR = (char *) GetVBR ();

      MySetContents (vectext1, ESC "r$%08lx\n$%08lx\n$%08lx\n$%08lx\n$%08lx\n$%08lx",SysBase->ColdCapture,SysBase->CoolCapture,SysBase->WarmCapture,SysBase->KickMemPtr,SysBase->KickTagPtr,SysBase->KickCheckSum);
      MySetContents (vectext2, ESC "r$%08lx\n$%08lx\n$%08lx\n$%08lx\n$%08lx\n$%08lx\n$%08lx",*((ULONG *) (VBR + 0x0064)),*((ULONG *) (VBR + 0x0068)),*((ULONG *) (VBR + 0x006c)),*((ULONG *) (VBR + 0x0070)),*((ULONG *) (VBR + 0x0074)),*((ULONG *) (VBR + 0x0078)),*((ULONG *) (VBR + 0x007c)));
      MySetContents (vectext3, ESC "r$%08lx\n$%08lx\n$%08lx\n$%08lx\n$%08lx\n$%08lx\n$%08lx\n$%08lx",SysBase->IntVects[0].iv_Code,SysBase->IntVects[1].iv_Code,SysBase->IntVects[2].iv_Code,SysBase->IntVects[3].iv_Code,SysBase->IntVects[4].iv_Code,SysBase->IntVects[5].iv_Code,SysBase->IntVects[6].iv_Code,SysBase->IntVects[7].iv_Code);
      MySetContents (vectext4, ESC "r$%08lx\n$%08lx\n$%08lx\n$%08lx\n$%08lx\n$%08lx\n$%08lx\n$%08lx",SysBase->IntVects[8].iv_Code,SysBase->IntVects[9].iv_Code,SysBase->IntVects[10].iv_Code,SysBase->IntVects[11].iv_Code,SysBase->IntVects[12].iv_Code,SysBase->IntVects[13].iv_Code,SysBase->IntVects[14].iv_Code,SysBase->IntVects[15].iv_Code);
   }
}

void SendVectorList (void) {
   char  *VBR;

   VBR = (char *) GetVBR ();

   SendClient (ESC "r$%08lx\n$%08lx\n$%08lx\n$%08lx\n$%08lx\n$%08lx",SysBase->ColdCapture,SysBase->CoolCapture,SysBase->WarmCapture,SysBase->KickMemPtr,SysBase->KickTagPtr,SysBase->KickCheckSum);
   SendClient (ESC "r$%08lx\n$%08lx\n$%08lx\n$%08lx\n$%08lx\n$%08lx\n$%08lx",*((ULONG *) (VBR + 0x0064)),*((ULONG *) (VBR + 0x0068)),*((ULONG *) (VBR + 0x006c)),*((ULONG *) (VBR + 0x0070)),*((ULONG *) (VBR + 0x0074)),*((ULONG *) (VBR + 0x0078)),*((ULONG *) (VBR + 0x007c)));
   SendClient (ESC "r$%08lx\n$%08lx\n$%08lx\n$%08lx\n$%08lx\n$%08lx\n$%08lx\n$%08lx",SysBase->IntVects[0].iv_Code,SysBase->IntVects[1].iv_Code,SysBase->IntVects[2].iv_Code,SysBase->IntVects[3].iv_Code,SysBase->IntVects[4].iv_Code,SysBase->IntVects[5].iv_Code,SysBase->IntVects[6].iv_Code,SysBase->IntVects[7].iv_Code);
   SendClient (ESC "r$%08lx\n$%08lx\n$%08lx\n$%08lx\n$%08lx\n$%08lx\n$%08lx\n$%08lx",SysBase->IntVects[8].iv_Code,SysBase->IntVects[9].iv_Code,SysBase->IntVects[10].iv_Code,SysBase->IntVects[11].iv_Code,SysBase->IntVects[12].iv_Code,SysBase->IntVects[13].iv_Code,SysBase->IntVects[14].iv_Code,SysBase->IntVects[15].iv_Code);
}


char vectors_title[WINDOWTITLELEN];
static char *VectorPages[] = { "ResetVecs","AutoVecInts","IntVecs",NULL };

void VectorsWindow (BOOL state) {
   if (state) {
      if (WI_Vectors) {
         GetVectors();
      } else {
         WI_Vectors = WindowObject,
         MUIA_Window_Title, MyGetWindowTitle (vectors_title, "VECTORS"),
         MUIA_HelpNode, VectorsText,
         MUIA_Window_ID, MakeListID('V','E','C','T'),
         WindowContents, VGroup,

            Child, RegisterGroup(VectorPages),

               Child, HGroup,
                  GroupFrameT("Reset Vectors"),
                  Child, HGroup,
                     Child, MyLabel2 (ESC "rColdCapture:\nCoolCapture:\nWarmCapture:\nKickMemPtr:\nKickTagPtr:\nKickCheckSum:"),
                     Child, vectext1 = MyTextObject2(),
                  End,
               End,

               Child, HGroup,
                  GroupFrameT("Auto Vector Interrupts"),
                  Child, HGroup,
                     Child, MyLabel2 (ESC "rLevel1 ($0064):\nLevel2 ($0068):\nLevel3 ($006c):\nLevel4 ($0070):\nLevel5 ($0074):\nLevel6 ($0078):\nLevel7 ($007c):"),
                     Child, vectext2 = MyTextObject2(),
                  End,
               End,

               Child, HGroup,
                  GroupFrameT("Interrupt Vectors"),
                  Child, HGroup,
                     Child, MyLabel (ESC "rTBE\nDSKBLK\nSOFTINT\nPORTS\nCOPPER\nVERTB\nBLITTER\nAUDIO0"),
                     Child, MyLabel (ESC "r 0:\n1:\n2:\n3:\n4:\n5:\n6:\n7:"),
                     Child, vectext3 = MyTextObject2(),
                  End,
                  Child, HGroup,
                     Child, MyLabel (ESC "rAUDIO1\nAUDIO2\nAUDIO3\nRBF\nDSKSYNC\nEXTER\nINTEN\nNMI"),
                     Child, MyLabel (ESC "r8:\n9:\n10:\n11:\n12:\n13:\n14:\n15:"),
                     Child, vectext4 = MyTextObject2(),
                  End,
               End,
            End,
#ifndef WINDOW_PROBLEMS
            Child, MyVSpace(4),
#endif
            Child, HGroup, MUIA_Group_SameSize, TRUE,
               Child, BT_VecUpdate = KeyButtonA (UpdateText,ID_VECUPDATE),
               Child, BT_VecPrint  = KeyButtonA (PrintText ,ID_VECPRINT),
               Child, BT_VecExit   = KeyButtonA (ExitText  ,ID_VECEXIT),
            End,
         End, End;

         DoMethod (AP_Scout,OM_ADDMEMBER,WI_Vectors);
         DoMethod (WI_Vectors,MUIM_Window_SetCycleChain,BT_VecUpdate,BT_VecPrint,BT_VecExit,NULL);
         SetCloseRequest (WI_Vectors,ID_VECEXIT);

         GetVectors();

         SetWindowOpen (WI_Vectors,NULL,ID_VECEXIT);
      }
   } else if ((! state) && (WI_Vectors)) {
      SetWindowClose (WI_Vectors,TRUE);

//      FreeVectors();

      DoMethod (AP_Scout,OM_REMMEMBER,WI_Vectors);
      MUI_DisposeObject (WI_Vectors);
      WI_Vectors = NULL;
   }
}

