main.c 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. // See LICENSE for license details.
  2. #include <stdio.h>
  3. #include <time.h>
  4. #include <stdlib.h>
  5. #include "nuclei_sdk_soc.h"
  6. static void mmode_exception_handler(unsigned long mcause, unsigned long sp)
  7. {
  8. EXC_Frame_Type *exc_frame = (EXC_Frame_Type *)sp;
  9. printf("\r\nmmode_exception_handler enters\r\n");
  10. switch (mcause & MCAUSE_CAUSE) {
  11. case IlleIns_EXCn:
  12. printf("Illegal instruction fault occurs, mcause: 0x%lx, mepc: 0x%lx\r\n", exc_frame->cause, exc_frame->epc, exc_frame->epc);
  13. break;
  14. case MmodeEcall_EXCn:
  15. printf("Environment call from M-mode, mcause: 0x%lx, mepc: 0x%lx\r\n", exc_frame->cause, exc_frame->epc);
  16. break;
  17. default: break;
  18. }
  19. #ifdef CFG_SIMULATION
  20. // directly exit if in nuclei internally simulation
  21. SIMULATION_EXIT(0);
  22. #endif
  23. exc_frame->epc += 4; // The illegal instruction and ecall takes 4 bytes
  24. }
  25. static void trigger_illegal_inst(void)
  26. {
  27. // The illegal instruction takes 4 bytes
  28. __ASM volatile(".word 0xffffffff");
  29. }
  30. static void trigger_ecall(void)
  31. {
  32. // The ecall takes 4 bytes
  33. __ASM volatile("ecall");
  34. }
  35. int main(void)
  36. {
  37. CSR_MCFGINFO_Type mcfg_info;
  38. mcfg_info.d = __RV_CSR_READ(CSR_MCFG_INFO);
  39. #if defined(__ECLIC_PRESENT) && (__ECLIC_PRESENT != 0)
  40. if (0 == mcfg_info.b.clic) {
  41. printf("You expect ECLIC present, but ECLIC is not present, will not run this example!\r\n");
  42. printf("You can rebuild and run this example with extra make option XLCFG_ECLIC=0 if ECLIC not present!\r\n");
  43. return 0;
  44. }
  45. #endif
  46. /* register corresponding exception */
  47. Exception_Register_EXC(IlleIns_EXCn, (unsigned long)mmode_exception_handler);
  48. Exception_Register_EXC(MmodeEcall_EXCn, (unsigned long)mmode_exception_handler);
  49. trigger_illegal_inst();
  50. trigger_ecall();
  51. printf("\r\nM mode exception test finish and pass\r\n");
  52. return 0;
  53. }