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.
63 lines
1.6 KiB
63 lines
1.6 KiB
4 years ago
|
use std::thread::JoinHandle;
|
||
4 years ago
|
use std::time::Duration;
|
||
4 years ago
|
|
||
4 years ago
|
use crate::asm::data::literal::Addr;
|
||
|
use crate::runtime::exec::EvalRes;
|
||
|
use crate::runtime::frame::{CallStack, StackFrame};
|
||
|
use crate::runtime::program::Program;
|
||
4 years ago
|
|
||
4 years ago
|
const CYCLE_TIME: Duration = Duration::from_millis(0);
|
||
4 years ago
|
//const CYCLE_TIME : Duration = Duration::from_millis(100);
|
||
4 years ago
|
|
||
|
#[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
|
||
4 years ago
|
pub call_stack: CallStack,
|
||
4 years ago
|
/// 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));
|
||
4 years ago
|
debug!("Step {}; Status = {}", advance, self.frame.status);
|
||
4 years ago
|
self.frame.pc.advance(advance);
|
||
|
}
|
||
|
Err(e) => {
|
||
|
error!("Fault: {:?}", e);
|
||
|
break 'run;
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|