#include "urv_asm.h"
#include "hydra_supervisor_regs.h"

        .section    .boot, "ax", @progbits

        .global _start
_start:
        j       _entry
        nop

_exception_entry:
        /* Fix all registers */
	fix_ecc x0,x0,x0
	fix_ecc x1,x1,x1
	fix_ecc x2,x2,x2
	fix_ecc x3,x3,x3
	fix_ecc x4,x4,x4
	fix_ecc x5,x5,x5
	fix_ecc x6,x6,x6
	fix_ecc x7,x7,x7
	fix_ecc x8,x8,x8
	fix_ecc x9,x9,x9
	fix_ecc x10,x10,x10
	fix_ecc x11,x11,x11
	fix_ecc x12,x12,x12
	fix_ecc x13,x13,x13
	fix_ecc x14,x14,x14
	fix_ecc x15,x15,x15
	fix_ecc x16,x16,x16
	fix_ecc x17,x17,x17
	fix_ecc x18,x18,x18
	fix_ecc x19,x19,x19
	fix_ecc x20,x20,x20
	fix_ecc x21,x21,x21
	fix_ecc x22,x22,x22
	fix_ecc x23,x23,x23
	fix_ecc x24,x24,x24
	fix_ecc x25,x25,x25
	fix_ecc x26,x26,x26
	fix_ecc x27,x27,x27
	fix_ecc x28,x28,x28
	fix_ecc x29,x29,x29
	fix_ecc x30,x30,x30
	fix_ecc x31,x31,x31

        /* Stats.  */
        addi    sp,sp,-8
        sw      t1,0(sp)
        la      t1,0x80000  /* SUPERVISOR_BASE */
        sw      zero, HYDRA_SUPERVISOR_REGS_CPU_FAULT_COUNT(t1)
        lw      t1,0(sp)
        addi    sp,sp,8

        mret

_entry:
        /* Clear all the registers so that ECC is correct */
        lui     ra, 0        /* ra */
	la      sp, _fstack
        la      gp, _gp         /* Initialize global pointer */
        lui     x4, 0        /* tp */
        mv      x5, zero
        mv      x6, zero
        mv      x7, zero
        mv      x8, zero
        mv      x9, zero
        mv      x10, zero
        mv      x11, zero
        mv      x12, zero
        mv      x13, zero
        mv      x14, zero
        mv      x15, zero
        mv      x16, zero
        mv      x17, zero
        mv      x18, zero
        mv      x19, zero
        mv      x20, zero
        mv      x21, zero
        mv      x22, zero
        mv      x23, zero
        mv      x24, zero
        mv      x25, zero
        mv      x26, zero
        mv      x27, zero
        mv      x28, zero
        mv      x29, zero
        mv      x30, zero
        mv      x31, zero

/*
#        # Watchdog
#        lui	a5,0xc0424
#        lui	a4,0x80
#        addi	a5,a5,-1079 # c0423bc9 <_fstack+0xc04137c9>
#        sw	a5,20(a4) # 80014 <_fstack+0x6fc14>
*/
        call    init


1:
        /* Resync regs (just clear them) */
1:
        mv      x1, zero        /* ra */
	la      sp, _fstack
        la      gp, _gp         /* Initialize global pointer */
        lui     x4, 0           /* tp */
        lui     x5, 0
        lui     x6, 0
        lui     x7, 0
        lui     x8, 0
        lui     x9, 0
        lui     x10, 0
        lui     x11, 0
        lui     x12, 0
        lui     x13, 0
        lui     x14, 0
        lui     x15, 0
        lui     x16, 0
        lui     x17, 0
        lui     x18, 0
        lui     x19, 0
        lui     x20, 0
        lui     x21, 0
        lui     x22, 0
        lui     x23, 0
        lui     x24, 0
        lui     x25, 0
        lui     x26, 0
        lui     x27, 0
        lui     x28, 0
        lui     x29, 0
        lui     x30, 0
        lui     x31, 0
        call    main
        j       1b
