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.
196 lines
6.9 KiB
196 lines
6.9 KiB
5 years ago
|
use std::ops::Rem;
|
||
|
|
||
|
use num_traits::PrimInt;
|
||
|
|
||
5 years ago
|
use crsn::asm::instr::op::OpTrait;
|
||
|
use crsn::runtime::fault::Fault;
|
||
|
use crsn::runtime::frame::{CallStack, StackFrame};
|
||
|
use crsn::runtime::program::Program;
|
||
5 years ago
|
|
||
5 years ago
|
use crate::defs::ArithOp;
|
||
5 years ago
|
|
||
5 years ago
|
impl OpTrait for ArithOp {
|
||
|
fn execute(&self, _program: &Program, _call_stack: &mut CallStack, frame: &mut StackFrame) -> Result<(), Fault> {
|
||
|
match self {
|
||
|
ArithOp::Test { a } => {
|
||
5 years ago
|
frame.status.clear();
|
||
5 years ago
|
let res = frame.read(*a)?;
|
||
|
frame.status.update(res);
|
||
5 years ago
|
}
|
||
5 years ago
|
ArithOp::Compare { a, b } => {
|
||
5 years ago
|
frame.status.clear();
|
||
5 years ago
|
let x = frame.read(*a)?;
|
||
|
let y = frame.read(*b)?;
|
||
|
frame.status.equal = x == y;
|
||
|
frame.status.lower = x < y;
|
||
|
frame.status.greater = x > y;
|
||
|
// Test flags are set when both arguments have the property
|
||
|
if x == y {
|
||
|
frame.status.update(x);
|
||
|
}
|
||
5 years ago
|
}
|
||
5 years ago
|
ArithOp::Add { dst, a, b } => {
|
||
5 years ago
|
frame.status.clear();
|
||
5 years ago
|
let x = frame.read(*a)?;
|
||
|
let y = frame.read(*b)?;
|
||
|
let (res, ov) = if let Some(v) = x.checked_add(y) {
|
||
5 years ago
|
(v, false)
|
||
|
} else {
|
||
5 years ago
|
(x.wrapping_add(y), true)
|
||
5 years ago
|
};
|
||
5 years ago
|
frame.status.update(res);
|
||
5 years ago
|
frame.status.overflow = ov;
|
||
5 years ago
|
frame.write(*dst, res)?;
|
||
5 years ago
|
}
|
||
5 years ago
|
ArithOp::Sub { dst, a, b } => {
|
||
5 years ago
|
frame.status.clear();
|
||
5 years ago
|
let x = frame.read(*a)?;
|
||
|
let y = frame.read(*b)?;
|
||
|
let (res, ov) = if let Some(v) = x.checked_sub(y) {
|
||
5 years ago
|
(v, false)
|
||
|
} else {
|
||
5 years ago
|
(x.wrapping_sub(y), true)
|
||
5 years ago
|
};
|
||
5 years ago
|
frame.status.update(res);
|
||
5 years ago
|
frame.status.overflow = ov;
|
||
5 years ago
|
frame.write(*dst, res)?;
|
||
5 years ago
|
}
|
||
5 years ago
|
ArithOp::Mul { dst, a, b } => {
|
||
5 years ago
|
frame.status.clear();
|
||
5 years ago
|
let x = frame.read(*a)?;
|
||
|
let y = frame.read(*b)?;
|
||
|
let (res, ov) = if let Some(v) = x.checked_mul(y) {
|
||
5 years ago
|
(v, false)
|
||
|
} else {
|
||
5 years ago
|
(x.wrapping_mul(y), true)
|
||
5 years ago
|
};
|
||
5 years ago
|
frame.status.update(res);
|
||
5 years ago
|
frame.status.overflow = ov;
|
||
5 years ago
|
frame.write(*dst, res)?;
|
||
5 years ago
|
}
|
||
5 years ago
|
ArithOp::Div { dst, rem, a, div } => {
|
||
5 years ago
|
frame.status.clear();
|
||
5 years ago
|
let x = frame.read(*a)?;
|
||
|
let d = frame.read(*div)?;
|
||
|
if d == 0 {
|
||
|
frame.status.invalid = true;
|
||
5 years ago
|
} else {
|
||
5 years ago
|
let (res, remainder, ov) = if let Some(v) = x.checked_div(d) {
|
||
|
(v, x.rem(d), false)
|
||
5 years ago
|
} else {
|
||
5 years ago
|
(x.wrapping_div(d), x.wrapping_rem(d), true)
|
||
5 years ago
|
};
|
||
5 years ago
|
frame.status.update(res);
|
||
5 years ago
|
frame.status.overflow = ov;
|
||
5 years ago
|
frame.write(*dst, res)?;
|
||
|
frame.write(*rem, remainder)?;
|
||
5 years ago
|
}
|
||
5 years ago
|
}
|
||
5 years ago
|
ArithOp::Mod { dst, a, div } => {
|
||
5 years ago
|
frame.status.clear();
|
||
5 years ago
|
let x = frame.read(*a)?;
|
||
|
let d = frame.read(*div)?;
|
||
|
if d == 0 {
|
||
|
frame.status.invalid = true;
|
||
5 years ago
|
} else {
|
||
5 years ago
|
let (remainder, ov) = if let Some(v) = x.checked_rem(d) {
|
||
5 years ago
|
(v, false)
|
||
|
} else {
|
||
5 years ago
|
(x.wrapping_rem(d), true)
|
||
5 years ago
|
};
|
||
5 years ago
|
frame.status.update(remainder);
|
||
5 years ago
|
frame.status.overflow = ov;
|
||
5 years ago
|
frame.write(*dst, remainder)?;
|
||
5 years ago
|
}
|
||
5 years ago
|
}
|
||
5 years ago
|
ArithOp::And { dst, a, b } => {
|
||
5 years ago
|
frame.status.clear();
|
||
5 years ago
|
let x = frame.read(*a)?;
|
||
|
let y = frame.read(*b)?;
|
||
|
let res = x & y;
|
||
|
frame.status.update(res);
|
||
|
frame.write(*dst, res)?;
|
||
5 years ago
|
}
|
||
5 years ago
|
ArithOp::Or { dst, a, b } => {
|
||
5 years ago
|
frame.status.clear();
|
||
5 years ago
|
let x = frame.read(*a)?;
|
||
|
let y = frame.read(*b)?;
|
||
|
let res = x | y;
|
||
|
frame.status.update(res);
|
||
|
frame.write(*dst, res)?;
|
||
5 years ago
|
}
|
||
5 years ago
|
ArithOp::Xor { dst, a, b } => {
|
||
5 years ago
|
frame.status.clear();
|
||
5 years ago
|
let x = frame.read(*a)?;
|
||
|
let y = frame.read(*b)?;
|
||
|
let res = x ^ y;
|
||
|
frame.status.update(res);
|
||
|
frame.write(*dst, res)?;
|
||
5 years ago
|
}
|
||
5 years ago
|
ArithOp::Cpl { dst, a } => {
|
||
5 years ago
|
frame.status.clear();
|
||
5 years ago
|
let x = frame.read(*a)?;
|
||
|
let res = !x;
|
||
|
frame.status.update(res);
|
||
|
frame.write(*dst, res)?;
|
||
5 years ago
|
}
|
||
5 years ago
|
ArithOp::Rol { dst, a, n } => {
|
||
5 years ago
|
frame.status.clear();
|
||
5 years ago
|
let x = frame.read(*a)?;
|
||
|
let y = frame.read(*n)?;
|
||
|
if y > u32::MAX as u64 {
|
||
|
frame.status.invalid = true;
|
||
|
} else {
|
||
|
let res = x.rotate_left(y as u32);
|
||
|
frame.status.update(res);
|
||
|
frame.write(*dst, res)?;
|
||
|
}
|
||
5 years ago
|
}
|
||
5 years ago
|
ArithOp::Ror { dst, a, n } => {
|
||
5 years ago
|
frame.status.clear();
|
||
5 years ago
|
let x = frame.read(*a)?;
|
||
|
let y = frame.read(*n)?;
|
||
|
if y > u32::MAX as u64 {
|
||
|
frame.status.invalid = true;
|
||
|
} else {
|
||
|
let res = x.rotate_right(y as u32);
|
||
|
frame.status.update(res);
|
||
|
frame.write(*dst, res)?;
|
||
|
}
|
||
5 years ago
|
}
|
||
5 years ago
|
ArithOp::Lsl { dst, a, n } => {
|
||
5 years ago
|
frame.status.clear();
|
||
5 years ago
|
let x = frame.read(*a)?;
|
||
|
let y = frame.read(*n)?;
|
||
|
let res = x << y;
|
||
|
frame.status.update(res);
|
||
|
frame.write(*dst, res)?;
|
||
5 years ago
|
}
|
||
5 years ago
|
ArithOp::Lsr { dst, a, n } => {
|
||
5 years ago
|
frame.status.clear();
|
||
5 years ago
|
let x = frame.read(*a)?;
|
||
|
let y = frame.read(*n)?;
|
||
|
let res = x >> y;
|
||
|
frame.status.update(res);
|
||
|
frame.write(*dst, res)?;
|
||
5 years ago
|
}
|
||
5 years ago
|
ArithOp::Asr { dst, a, n } => {
|
||
5 years ago
|
frame.status.clear();
|
||
5 years ago
|
let x = frame.read(*a)?;
|
||
|
let y = frame.read(*n)?;
|
||
|
if y > u32::MAX as u64 {
|
||
|
frame.status.invalid = true;
|
||
|
} else {
|
||
|
let res = x.signed_shr(y as u32);
|
||
|
frame.status.update(res);
|
||
|
frame.write(*dst, res)?;
|
||
|
}
|
||
5 years ago
|
}
|
||
5 years ago
|
}
|
||
|
|
||
5 years ago
|
Ok(())
|
||
5 years ago
|
}
|
||
5 years ago
|
//
|
||
5 years ago
|
}
|
||
5 years ago
|
|