use core::arch::asm;

use crate::config::*;
use crate::trap::TrapContext;

static KERNEL_STACK: [[u8; KERNEL_STACK_SIZE]; MAX_APP_NUM] = [[0; KERNEL_STACK_SIZE]; MAX_APP_NUM];  // 这一章, 每个用户应用对应一个内核栈 [每个应用自己的内核栈; 总应用数]
static USER_STACK: [[u8; USER_STACK_SIZE]; MAX_APP_NUM] = [[0; USER_STACK_SIZE]; MAX_APP_NUM];  // 这一行每个用户应用对应一个应用栈

extern "C" {
    fn _num_app();
}

// 得到用户app的数量
pub fn get_num_app() -> usize{
    unsafe{
        (_num_app as usize as *const usize).read_volatile()
    }
}

pub fn get_app_data(app_id: usize) -> &'static [u8] {
    extern "C" {
        fn _num_app();
    }
    let num_app_ptr = _num_app as usize as *const usize;
    let num_app = get_num_app();
    let app_start = unsafe { core::slice::from_raw_parts(num_app_ptr.add(1), num_app + 1) };
    assert!(app_id < num_app);
    unsafe {
        core::slice::from_raw_parts(
            app_start[app_id] as *const u8,
            app_start[app_id + 1] - app_start[app_id],
        )
    }
}