/*
    TMError.c, Copyright (c) by Silvio Dragone & David Reist,
    Swiss Federal Institute of Technology,
    Computer Engineering and Networks Laboratory.
 
    TOPSY -- A Teachable Operating System.
             Implementation of a tiny and simple
             micro kernel for teaching purposes.
 
    For further information, please visit http://www.tik.ee.ethz.ch/~topsy
 
    This software is provided under the terms of the GNU General Public Licence.
 
    A full copy of the GNU GPL is provided in the file COPYING found in the 
    development root of Topsy.
*/
/*
        
    File:                  $Source: /home/ruf/cvs_Topsy/cvs/Topsy/Threads/sa1100/TMError.c,v $
    Affiliation:           ETH Zuerich, TIK
    Version:               $Revision: 2922 $
    Last Date of Change:   $Date: 2007-03-19 17:48:52 +0100 (Mon, 19 Mär 2007) $  by: $Author: baderma $
 
*/

#include "TMError.h"
#include "Threads.h"
#include "Messages.h"
#include "TMHal.h"
#include "TMScheduler.h"
#include "Exception.h"
#include "TMClock.h"
#include "TMIPC.h"


static void tmUndefinedInstructionError(ThreadId currentThread);

void tmInstallErrorHandlers(void)
{
  /* Vital for correct operations */
  tmSetInterruptHandler(PCI_I, (InterruptHandler)tmPCIIntHandler, NULL);
  //[SD]
  tmSetInterruptHandler(MBZ_I, (InterruptHandler)dummyMBZ, NULL);
  tmSetInterruptHandler(SDRAM_I, (InterruptHandler)dummySDRAM, NULL);
  tmSetInterruptHandler(RTC_I, (InterruptHandler)dummyRTC, NULL);
  tmSetInterruptHandler(SRAM_I, (InterruptHandler)dummySRAM, NULL);
  tmSetInterruptHandler(UENG_I, (InterruptHandler)dummyUENG, NULL);
  tmSetInterruptHandler(CINT_I, (InterruptHandler)dummyCINT, NULL);
  //tmSetInterruptHandler(PCI_I, (InterruptHandler)dummyPCI, NULL);
  //[SD]

  tmSetExceptionHandler(UNDEF_INSTR,
                        (ExceptionHandler)tmUndefinedInstructionError);
  tmSetExceptionHandler(SOFT_INTR, (ExceptionHandler)msgDispatcher);
  //-CJ	tmSetExceptionHandler(ABORT_PREFETCH, (ExceptionHandler)APF_exception);
  //-CJ	tmSetExceptionHandler(ABORT_DATA, (ExceptionHandler)ADA_exception);
}


static void tmError(ThreadId currentThread, char* errorString)
{
  Message msg;

  msg.id = TM_KILL;
  msg.from = TMTHREADID;
  msg.msg.tmKill.id = currentThread;

  ERROR(errorString);
  printRegisters();

  /* immediate in-kernel delivery without syscall
   * kSend does a schedule after the message arrived
   */
  kSend(TMTHREADID, &msg);
}

static void tmUndefinedInstructionError(ThreadId currentThread)
{
  tmError(currentThread, "reserved instruction");
}

