forked from MightyPork/crsn
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
66 lines
1.6 KiB
66 lines
1.6 KiB
5 years ago
|
use std::time::Duration;
|
||
|
use std::thread::JoinHandle;
|
||
|
|
||
|
use asm::data::literal::{Addr, Value};
|
||
|
use asm::instr::Op;
|
||
|
use crate::exec;
|
||
|
use asm::data::{Rd, SrcDisp, reg::Register, Wr, DstDisp};
|
||
|
use crate::fault::Fault;
|
||
|
use crate::frame::StackFrame;
|
||
|
use crate::program::Program;
|
||
|
use crate::exec::EvalRes;
|
||
|
|
||
|
const CYCLE_TIME : Duration = Duration::from_millis(100);
|
||
|
|
||
|
#[derive(Clone, Copy, Eq, PartialEq, Debug, Ord, PartialOrd)]
|
||
|
pub struct ThreadToken(pub u32);
|
||
|
|
||
|
pub struct RunThread {
|
||
|
/// Thread ID
|
||
|
pub id: ThreadToken,
|
||
|
/// Active stack frame
|
||
|
pub frame: StackFrame,
|
||
|
/// Call stack
|
||
|
pub call_stack: Vec<StackFrame>,
|
||
|
/// Program to run
|
||
|
pub program: Program,
|
||
|
}
|
||
|
|
||
|
impl RunThread {
|
||
|
pub fn new(id: ThreadToken, program: Program, pc: Addr, args: &[u64]) -> Self {
|
||
|
let sf = StackFrame::new(pc, args);
|
||
|
|
||
|
Self {
|
||
|
id,
|
||
|
frame: sf,
|
||
|
call_stack: vec![],
|
||
|
program,
|
||
|
}
|
||
|
}
|
||
|
|
||
|
pub fn start(self) -> JoinHandle<()> {
|
||
|
std::thread::spawn(move || {
|
||
|
self.run();
|
||
|
})
|
||
|
}
|
||
|
|
||
|
fn run(mut self) {
|
||
|
'run: loop {
|
||
|
match self.eval_op() {
|
||
|
Ok(EvalRes {
|
||
|
cycles, advance
|
||
|
}) => {
|
||
|
std::thread::sleep(CYCLE_TIME * (cycles as u32));
|
||
|
debug!("PC += {}", advance);
|
||
|
self.frame.pc.advance(advance);
|
||
|
}
|
||
|
Err(e) => {
|
||
|
error!("Fault: {:?}", e);
|
||
|
break 'run;
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|