#include "stddef.h"
#include "head.h"
#include "rtthread.h"
#include "rt_hw_stack_frame.h"

static const char *g_fault_cause[]={
  "Instruction address misaligned",// 0
  "Instruction access fault",// 1
  "Illegal instruction",// 2
  "Breakpoint",// 3
  "Load address misaligned",// 4
  "Load access fault",// 5
  "Store address misaligned",// 6
  "Store access fault",// 7
  "Environment call from U-mode",// 8
  "Environment call from S-mode",// 9
  "",
  "Environment call from M-mode",// 11
  "Instruction page fault",// 12
  "Load page fault",// 13
  "",
  "Store page fault",// 15
};


static void trap_print(uint32_t irq_num, rt_hw_stack_frame_t *stack) {
  my_printf("fault reason is %s\n", g_fault_cause[irq_num]);
  my_printf("x1(ra):    %08x\n", stack->ra);
  my_printf("x3(gp):    %08x\n", stack->gp);
  my_printf("x4(tp):    %08x\n", stack->tp);
  my_printf("x5(t0):    %08x\n", stack->t0);
  my_printf("x6(t1):    %08x\n", stack->t1);
  my_printf("x7(t2):    %08x\n", stack->t2);
  my_printf("x8(s0|fp): %08x\n", stack->s0_fp);
  my_printf("x9(s1):    %08x\n", stack->s1);
  my_printf("x10(a0):   %08x\n", stack->a0);
  my_printf("x11(a1):   %08x\n", stack->a1);
  my_printf("x12(a2):   %08x\n", stack->a2);
  my_printf("x13(a3):   %08x\n", stack->a3);
  my_printf("x14(a4):   %08x\n", stack->a4);
  my_printf("x15(a5):   %08x\n", stack->a5);
  my_printf("x16(a6):   %08x\n", stack->a6);
  my_printf("x17(a7):   %08x\n", stack->a7);
  my_printf("x18(s2):   %08x\n", stack->s2);
  my_printf("x19(s3):   %08x\n", stack->s3);
  my_printf("x20(s4):   %08x\n", stack->s4);
  my_printf("x21(s5):   %08x\n", stack->s5);
  my_printf("x22(s6):   %08x\n", stack->s6);
  my_printf("x23(s7):   %08x\n", stack->s7);
  my_printf("x24(s8):   %08x\n", stack->s8);
  my_printf("x25(s9):   %08x\n", stack->s9);
  my_printf("x26(s10):  %08x\n", stack->s10);
  my_printf("x27(s11):  %08x\n", stack->s11);
  my_printf("x28(t3):   %08x\n", stack->t3);
  my_printf("x29(t4):   %08x\n", stack->t4);
  my_printf("x30(t5):   %08x\n", stack->t5);
  my_printf("x31(t6):   %08x\n", stack->t6);
  my_printf("mstatus:   %08x\n", stack->mstatus);
  my_printf("mepc:      %08x\n", stack->epc);
  // my_printf("mtval:     %08x\n", read_csr(mtval));

  // 函数调用错误，直接跳过这个函数
  if(irq_num==1){
    my_printf("skip the function call, return to %08x\n", stack->ra);
    stack->epc = stack->ra;
  }

  // write_csr(mscratch, 0xffffffff);
}



void handle_trap(size_t mcause, size_t mepc, size_t sp) {
  if (mcause & 0x80000000) {
    my_printf("interupt mcause=%08x, mepc=%08x, sp=%08x\n", mcause, mepc, sp);
  } else {
    trap_print(mcause, (rt_hw_stack_frame_t*)sp);
    while (1);
  }
}


static void tick_handler(int vec, void* par) {
  (void)vec;
  (void)par;
  rt_tick_increase();
  // my_printf("tick irq %d\n", vec);
}


static uint32_t g_mem_heap[200 * 1024 / 4];
void rt_hw_board_init(void) {
  set_csr(mie, (1 << 3) | (1 << 7));
  set_csr(mip, (1 << 3));
  rt_system_heap_init(g_mem_heap, g_mem_heap + sizeof(g_mem_heap) / 4);
  rt_hw_interrupt_init();
  rt_hw_interrupt_install(7, tick_handler, 0, "tick_handler");
}


