//! Implementation of [`TaskContext`]
use crate::trap::trap_return;

#[repr(C)]
/// task context structure containing some registers
pub struct TaskContext {
    /// Ret position after task switching
    pub ra: usize,
    /// Stack pointer
    sp:     usize,
    /// s0-11 register, callee saved
    pub s:  [usize; 12],
}

impl TaskContext {
    /// Create a new empty task context
    pub fn zero_init() -> Self {
        Self {
            ra: 0,
            sp: 0,
            s:  [0; 12],
        }
    }
    /// Create a new task context with a trap return addr and a kernel stack pointer
    pub fn goto_trap_return(kstack_ptr: usize) -> Self {
        Self {
            ra: trap_return as usize,
            sp: kstack_ptr,
            s:  [0; 12],
        }
    }

    pub fn goto_initproc_entry(kstack_ptr: usize) -> Self {
        Self {
            ra: crate::trap::initproc_entry as usize,
            sp: kstack_ptr,
            s:  [0; 12],
        }
    }

    pub fn goto_user_entry(kstack_ptr: usize) -> Self {
        Self {
            ra: crate::trap::user_entry as usize,
            sp: kstack_ptr,
            s:  [0; 12],
        }
    }
}
