|
|
@ -3,6 +3,8 @@ use asm::instr::{Op, Cond}; |
|
|
|
use crate::fault::Fault; |
|
|
|
use crate::fault::Fault; |
|
|
|
use crate::frame::StackFrame; |
|
|
|
use crate::frame::StackFrame; |
|
|
|
use asm::data::literal::{Value, is_positive, is_negative, Addr}; |
|
|
|
use asm::data::literal::{Value, is_positive, is_negative, Addr}; |
|
|
|
|
|
|
|
use std::ops::{Rem, BitXor, Shl}; |
|
|
|
|
|
|
|
use num_traits::PrimInt; |
|
|
|
|
|
|
|
|
|
|
|
pub type CyclesSpent = usize; |
|
|
|
pub type CyclesSpent = usize; |
|
|
|
|
|
|
|
|
|
|
@ -12,6 +14,7 @@ pub struct EvalRes { |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
impl RunThread { |
|
|
|
impl RunThread { |
|
|
|
|
|
|
|
// TODO unit tests
|
|
|
|
pub fn eval_op(&mut self) -> Result<EvalRes, Fault> { |
|
|
|
pub fn eval_op(&mut self) -> Result<EvalRes, Fault> { |
|
|
|
let mut cycles = 1; |
|
|
|
let mut cycles = 1; |
|
|
|
let mut advance = 1; |
|
|
|
let mut advance = 1; |
|
|
@ -19,6 +22,8 @@ impl RunThread { |
|
|
|
let mut frame = &mut self.frame; |
|
|
|
let mut frame = &mut self.frame; |
|
|
|
|
|
|
|
|
|
|
|
let op = self.program.read(frame.pc); |
|
|
|
let op = self.program.read(frame.pc); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
debug!("------------------------"); |
|
|
|
debug!("{} | {:?}", frame.pc, op); |
|
|
|
debug!("{} | {:?}", frame.pc, op); |
|
|
|
|
|
|
|
|
|
|
|
/* Operations can be given different execution times when run in slow mode. */ |
|
|
|
/* Operations can be given different execution times when run in slow mode. */ |
|
|
@ -144,72 +149,196 @@ impl RunThread { |
|
|
|
frame.status.clear(); |
|
|
|
frame.status.clear(); |
|
|
|
let mut a = frame.read(dst.as_rd())?; |
|
|
|
let mut a = frame.read(dst.as_rd())?; |
|
|
|
let mut b = frame.read(*src)?; |
|
|
|
let mut b = frame.read(*src)?; |
|
|
|
|
|
|
|
let (val, ov) = if let Some(v) = a.checked_add(b) { |
|
|
|
let val = a.wrapping_add(b); |
|
|
|
(v, false) |
|
|
|
|
|
|
|
} else { |
|
|
|
|
|
|
|
(a.wrapping_add(b), true) |
|
|
|
|
|
|
|
}; |
|
|
|
frame.status.zero = (val == 0); |
|
|
|
frame.status.zero = (val == 0); |
|
|
|
frame.status.positive = is_positive(val); |
|
|
|
frame.status.positive = is_positive(val); |
|
|
|
frame.status.negative = is_negative(val); |
|
|
|
frame.status.negative = is_negative(val); |
|
|
|
frame.status.overflow = b > Value::MAX - a; // TODO check
|
|
|
|
frame.status.overflow = ov; |
|
|
|
// TODO carry?
|
|
|
|
|
|
|
|
frame.write(*dst, val)?; |
|
|
|
frame.write(*dst, val)?; |
|
|
|
} |
|
|
|
} |
|
|
|
Op::Sub(dst, src) => { |
|
|
|
Op::Sub(dst, src) => { |
|
|
|
frame.status.clear(); |
|
|
|
frame.status.clear(); |
|
|
|
let mut a = frame.read(dst.as_rd())?; |
|
|
|
let mut a = frame.read(dst.as_rd())?; |
|
|
|
let mut b = frame.read(*src)?; |
|
|
|
let mut b = frame.read(*src)?; |
|
|
|
|
|
|
|
let (val, ov) = if let Some(v) = a.checked_sub(b) { |
|
|
|
let val = a.wrapping_sub(b); |
|
|
|
(v, false) |
|
|
|
|
|
|
|
} else { |
|
|
|
|
|
|
|
(a.wrapping_sub(b), true) |
|
|
|
|
|
|
|
}; |
|
|
|
frame.status.zero = (val == 0); |
|
|
|
frame.status.zero = (val == 0); |
|
|
|
frame.status.positive = is_positive(val); |
|
|
|
frame.status.positive = is_positive(val); |
|
|
|
frame.status.negative = is_negative(val); |
|
|
|
frame.status.negative = is_negative(val); |
|
|
|
frame.status.overflow = b > a; |
|
|
|
frame.status.overflow = ov; |
|
|
|
// TODO carry?
|
|
|
|
|
|
|
|
frame.write(*dst, val)?; |
|
|
|
frame.write(*dst, val)?; |
|
|
|
} |
|
|
|
} |
|
|
|
Op::Mul(dst, src) => { |
|
|
|
Op::Mul(dst, src) => { |
|
|
|
frame.status.clear(); |
|
|
|
frame.status.clear(); |
|
|
|
let mut a = frame.read(dst.as_rd())?; |
|
|
|
let mut a = frame.read(dst.as_rd())?; |
|
|
|
let mut b = frame.read(*src)?; |
|
|
|
let mut b = frame.read(*src)?; |
|
|
|
|
|
|
|
let (val, ov) = if let Some(v) = a.checked_mul(b) { |
|
|
|
let val = a.wrapping_mul(b); |
|
|
|
(v, false) |
|
|
|
|
|
|
|
} else { |
|
|
|
|
|
|
|
(a.wrapping_mul(b), true) |
|
|
|
|
|
|
|
}; |
|
|
|
frame.status.zero = (val == 0); |
|
|
|
frame.status.zero = (val == 0); |
|
|
|
frame.status.positive = is_positive(val); |
|
|
|
frame.status.positive = is_positive(val); |
|
|
|
frame.status.negative = is_negative(val); |
|
|
|
frame.status.negative = is_negative(val); |
|
|
|
//frame.status.overflow = b > a; // TODO detect overflow
|
|
|
|
frame.status.overflow = ov; |
|
|
|
frame.write(*dst, val)?; |
|
|
|
frame.write(*dst, val)?; |
|
|
|
} |
|
|
|
} |
|
|
|
Op::Div(_, _) => { |
|
|
|
Op::Div(dst, src) => { |
|
|
|
unimplemented!() |
|
|
|
frame.status.clear(); |
|
|
|
|
|
|
|
let mut a = frame.read(dst.as_rd())?; |
|
|
|
|
|
|
|
let mut b = frame.read(*src)?; |
|
|
|
|
|
|
|
if b == 0 { |
|
|
|
|
|
|
|
frame.status.overflow = true; |
|
|
|
|
|
|
|
// TODO ?
|
|
|
|
|
|
|
|
} else { |
|
|
|
|
|
|
|
let (val, ov) = if let Some(v) = a.checked_div(b) { |
|
|
|
|
|
|
|
(v, false) |
|
|
|
|
|
|
|
} else { |
|
|
|
|
|
|
|
(a.wrapping_div(b), true) |
|
|
|
|
|
|
|
}; |
|
|
|
|
|
|
|
frame.status.zero = (val == 0); |
|
|
|
|
|
|
|
frame.status.positive = is_positive(val); |
|
|
|
|
|
|
|
frame.status.negative = is_negative(val); |
|
|
|
|
|
|
|
frame.status.overflow = ov; |
|
|
|
|
|
|
|
frame.write(*dst, val)?; |
|
|
|
} |
|
|
|
} |
|
|
|
Op::Mod(_, _, _) => { |
|
|
|
|
|
|
|
unimplemented!() |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
Op::And(_, _) => { |
|
|
|
Op::Mod(dst, src) => { |
|
|
|
unimplemented!() |
|
|
|
frame.status.clear(); |
|
|
|
|
|
|
|
let mut a = frame.read(dst.as_rd())?; |
|
|
|
|
|
|
|
let mut b = frame.read(*src)?; |
|
|
|
|
|
|
|
if b == 0 { |
|
|
|
|
|
|
|
frame.status.overflow = true; |
|
|
|
|
|
|
|
// TODO ?
|
|
|
|
|
|
|
|
} else { |
|
|
|
|
|
|
|
let (val, ov) = if let Some(v) = a.checked_rem(b) { |
|
|
|
|
|
|
|
(v, false) |
|
|
|
|
|
|
|
} else { |
|
|
|
|
|
|
|
(a.wrapping_rem(b), true) |
|
|
|
|
|
|
|
}; |
|
|
|
|
|
|
|
frame.status.zero = (val == 0); |
|
|
|
|
|
|
|
frame.status.positive = is_positive(val); |
|
|
|
|
|
|
|
frame.status.negative = is_negative(val); |
|
|
|
|
|
|
|
frame.status.overflow = ov; |
|
|
|
|
|
|
|
frame.write(*dst, val)?; |
|
|
|
} |
|
|
|
} |
|
|
|
Op::Or(_, _) => { |
|
|
|
|
|
|
|
unimplemented!() |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
Op::Xor(_, _) => { |
|
|
|
Op::DivRem(dst, remptr, src) => { |
|
|
|
unimplemented!() |
|
|
|
frame.status.clear(); |
|
|
|
|
|
|
|
let mut a = frame.read(dst.as_rd())?; |
|
|
|
|
|
|
|
let mut b = frame.read(*src)?; |
|
|
|
|
|
|
|
if b == 0 { |
|
|
|
|
|
|
|
frame.status.overflow = true; |
|
|
|
|
|
|
|
// TODO ?
|
|
|
|
|
|
|
|
} else { |
|
|
|
|
|
|
|
let (val, rem, ov) = if let Some(v) = a.checked_div(b) { |
|
|
|
|
|
|
|
(v, a.rem(b), false) |
|
|
|
|
|
|
|
} else { |
|
|
|
|
|
|
|
(a.wrapping_div(b), a.wrapping_rem(b), true) |
|
|
|
|
|
|
|
}; |
|
|
|
|
|
|
|
frame.status.zero = (val == 0); |
|
|
|
|
|
|
|
frame.status.positive = is_positive(val); |
|
|
|
|
|
|
|
frame.status.negative = is_negative(val); |
|
|
|
|
|
|
|
frame.status.overflow = ov; |
|
|
|
|
|
|
|
frame.write(*dst, val)?; |
|
|
|
|
|
|
|
frame.write(*remptr, rem)?; |
|
|
|
} |
|
|
|
} |
|
|
|
Op::Cpl(_) => { |
|
|
|
|
|
|
|
unimplemented!() |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
Op::Rol(_, _) => { |
|
|
|
Op::And(dst, src) => { |
|
|
|
unimplemented!() |
|
|
|
frame.status.clear(); |
|
|
|
|
|
|
|
let mut val = frame.read(dst.as_rd())?; |
|
|
|
|
|
|
|
let mut b = frame.read(*src)?; |
|
|
|
|
|
|
|
val &= b; |
|
|
|
|
|
|
|
frame.status.zero = (val == 0); |
|
|
|
|
|
|
|
frame.status.positive = is_positive(val); |
|
|
|
|
|
|
|
frame.status.negative = is_negative(val); |
|
|
|
|
|
|
|
frame.write(*dst, val)?; |
|
|
|
} |
|
|
|
} |
|
|
|
Op::Ror(_, _) => { |
|
|
|
Op::Or(dst, src) => { |
|
|
|
unimplemented!() |
|
|
|
frame.status.clear(); |
|
|
|
|
|
|
|
let mut val = frame.read(dst.as_rd())?; |
|
|
|
|
|
|
|
let mut b = frame.read(*src)?; |
|
|
|
|
|
|
|
val |= b; |
|
|
|
|
|
|
|
frame.status.zero = (val == 0); |
|
|
|
|
|
|
|
frame.status.positive = is_positive(val); |
|
|
|
|
|
|
|
frame.status.negative = is_negative(val); |
|
|
|
|
|
|
|
frame.write(*dst, val)?; |
|
|
|
} |
|
|
|
} |
|
|
|
Op::Lsl(_, _) => { |
|
|
|
Op::Xor(dst, src) => { |
|
|
|
unimplemented!() |
|
|
|
frame.status.clear(); |
|
|
|
|
|
|
|
let mut val = frame.read(dst.as_rd())?; |
|
|
|
|
|
|
|
let mut b = frame.read(*src)?; |
|
|
|
|
|
|
|
val ^= b; |
|
|
|
|
|
|
|
frame.status.zero = (val == 0); |
|
|
|
|
|
|
|
frame.status.positive = is_positive(val); |
|
|
|
|
|
|
|
frame.status.negative = is_negative(val); |
|
|
|
|
|
|
|
frame.write(*dst, val)?; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
Op::Cpl(dst) => { |
|
|
|
|
|
|
|
frame.status.clear(); |
|
|
|
|
|
|
|
let mut val = frame.read(dst.as_rd())?; |
|
|
|
|
|
|
|
val = !val; |
|
|
|
|
|
|
|
frame.status.zero = (val == 0); |
|
|
|
|
|
|
|
frame.status.positive = is_positive(val); |
|
|
|
|
|
|
|
frame.status.negative = is_negative(val); |
|
|
|
|
|
|
|
frame.write(*dst, val)?; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
Op::Rol(dst, num) => { |
|
|
|
|
|
|
|
frame.status.clear(); |
|
|
|
|
|
|
|
let mut val = frame.read(dst.as_rd())?; |
|
|
|
|
|
|
|
let mut b = frame.read(*num)?; |
|
|
|
|
|
|
|
val = val.rotate_left(b as u32); // FIXME check overflow
|
|
|
|
|
|
|
|
frame.status.zero = (val == 0); |
|
|
|
|
|
|
|
frame.status.positive = is_positive(val); |
|
|
|
|
|
|
|
frame.status.negative = is_negative(val); |
|
|
|
|
|
|
|
frame.write(*dst, val)?; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
Op::Ror(dst, num) => { |
|
|
|
|
|
|
|
frame.status.clear(); |
|
|
|
|
|
|
|
let mut val = frame.read(dst.as_rd())?; |
|
|
|
|
|
|
|
let mut b = frame.read(*num)?; |
|
|
|
|
|
|
|
val = val.rotate_right(b as u32); // FIXME check overflow
|
|
|
|
|
|
|
|
frame.status.zero = (val == 0); |
|
|
|
|
|
|
|
frame.status.positive = is_positive(val); |
|
|
|
|
|
|
|
frame.status.negative = is_negative(val); |
|
|
|
|
|
|
|
frame.write(*dst, val)?; |
|
|
|
} |
|
|
|
} |
|
|
|
Op::Lsr(_, _) => { |
|
|
|
Op::Lsl(dst, num) => { |
|
|
|
unimplemented!() |
|
|
|
frame.status.clear(); |
|
|
|
|
|
|
|
let mut val = frame.read(dst.as_rd())?; |
|
|
|
|
|
|
|
let mut b = frame.read(*num)?; |
|
|
|
|
|
|
|
val = val << b; |
|
|
|
|
|
|
|
frame.status.zero = (val == 0); |
|
|
|
|
|
|
|
frame.status.positive = is_positive(val); |
|
|
|
|
|
|
|
frame.status.negative = is_negative(val); |
|
|
|
|
|
|
|
frame.write(*dst, val)?; |
|
|
|
} |
|
|
|
} |
|
|
|
Op::Asr(_, _) => { |
|
|
|
Op::Lsr(dst, num) => { |
|
|
|
unimplemented!() |
|
|
|
frame.status.clear(); |
|
|
|
|
|
|
|
let mut val = frame.read(dst.as_rd())?; |
|
|
|
|
|
|
|
let mut b = frame.read(*num)?; |
|
|
|
|
|
|
|
val = val >> b; |
|
|
|
|
|
|
|
frame.status.zero = (val == 0); |
|
|
|
|
|
|
|
frame.status.positive = is_positive(val); |
|
|
|
|
|
|
|
frame.status.negative = is_negative(val); |
|
|
|
|
|
|
|
frame.write(*dst, val)?; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
Op::Asr(dst, num) => { |
|
|
|
|
|
|
|
frame.status.clear(); |
|
|
|
|
|
|
|
let mut val = frame.read(dst.as_rd())?; |
|
|
|
|
|
|
|
let mut b = frame.read(*num)?; |
|
|
|
|
|
|
|
val = val.signed_shr(b as u32); // FIXME check overflow
|
|
|
|
|
|
|
|
frame.status.zero = (val == 0); |
|
|
|
|
|
|
|
frame.status.positive = is_positive(val); |
|
|
|
|
|
|
|
frame.status.negative = is_negative(val); |
|
|
|
|
|
|
|
frame.write(*dst, val)?; |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|