/******************************************************************************
 * @file     excep_test.c
 * @brief    Test exceptions
 */

#include "c4_privilege_modes.h"

#include "excep_test_configuration.h"      // Header file for test case configuration
#include "fsl_debug_console.h"
//#include <stdio.h>
#include <stddef.h>

extern void undef(void);                 // Assembly function for the undefined
                                        // instruction test
typedef uint32_t pointer;

volatile int i, j = 0;                  // Variables for the division-by-zero

// Function written in assembly language, calling an invalid instruction
__asm__ (".globl undef\n"
     "undef:\n\t"
//     "DCI  0xF123\n\t "                     // DCI is not an ARM assmebly instruction
//     "DCI  0x4567\n\t"
     "BX   LR");


// main function containing all the tests
int excep_test (void) {
  SCB->CCR = 0x210;                     // enable div-by-zero trap in processor core
 
#if TESTCASE == 0
	undef();                              // Call undef() function that contains an
	                                      // invalid instruction
 
#elif TESTCASE == 1
	typedef void (*t_funcPtr)(void);
  t_funcPtr MyFunc = (t_funcPtr)(0x0001000 | 0x0);  // INVSTATE because jumping
	                                                  // to valid address but
	                                                  // thumb bit not set
	MyFunc();
 
#elif TESTCASE == 2
  i =i/j;                               // i and j are 0 initialized -> Div/0
#endif
 
  for (;;) {}
}

//*****************************************************************************
// Default core exception handlers. Override the ones here by defining your own
// handler routines in your application code.
//*****************************************************************************
void NMI_Handler(void)
{ while(1) {}
}

void DS_PendSV_Handler(void)
{ while(1) {}
}

