use crate::arch::cpu;
use crate::arch::interrupt::TrapFrame;
use crate::process::*;
use log::*;

pub static mut TICK: usize = 0;

pub fn uptime_msec() -> usize {
    unsafe { crate::trap::TICK / crate::consts::USEC_PER_TICK / 1000 }
}

pub fn timer() {
    if cpu::id() == 0 {
        unsafe {
            TICK += 1;
        }
    }
    processor().tick();
}

pub fn error(tf: &mut TrapFrame) -> ! {
    use crate::arch::syscall::*;
    error!("{:#x?}", tf);
    let tid = processor().tid();
    error!("On CPU{} Thread {}", cpu::id(), tid);

    crate::syscall::syscall(SYS_EXIT_GROUP, [0x100, 0, 0, 0, 0, 0], tf);
    unreachable!();
}

pub fn serial(c: char) {
    trace!("Serial input: {:X}", c as u8);
    if c == '\r' {
        // in linux, we use '\n' instead
        crate::fs::STDIN.push('\n');
    } else {
        crate::fs::STDIN.push(c);
    }
}
