|
|
@ -12,6 +12,7 @@ use crsn::utils::A; |
|
|
|
|
|
|
|
|
|
|
|
use crate::defs::ArithOp; |
|
|
|
use crate::defs::ArithOp; |
|
|
|
use crsn::asm::instr::cond::Flag; |
|
|
|
use crsn::asm::instr::cond::Flag; |
|
|
|
|
|
|
|
use rand::Rng; |
|
|
|
|
|
|
|
|
|
|
|
impl OpTrait for ArithOp { |
|
|
|
impl OpTrait for ArithOp { |
|
|
|
fn execute(&self, _ti: &ThreadInfo, state: &mut RunState) -> Result<EvalRes, Fault> { |
|
|
|
fn execute(&self, _ti: &ThreadInfo, state: &mut RunState) -> Result<EvalRes, Fault> { |
|
|
@ -44,6 +45,29 @@ impl OpTrait for ArithOp { |
|
|
|
state.set_flag(Flag::Greater, val > b); |
|
|
|
state.set_flag(Flag::Greater, val > b); |
|
|
|
state.update_status(val); |
|
|
|
state.update_status(val); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
ArithOp::Rng { dst, min, max } => { |
|
|
|
|
|
|
|
state.clear_status(); |
|
|
|
|
|
|
|
let min = state.read(min)?; |
|
|
|
|
|
|
|
let mut max = state.read(max)?; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
let val = if min == max { |
|
|
|
|
|
|
|
min |
|
|
|
|
|
|
|
} else if min > max { |
|
|
|
|
|
|
|
state.set_flag(Flag::Invalid, true); |
|
|
|
|
|
|
|
min |
|
|
|
|
|
|
|
} else { |
|
|
|
|
|
|
|
let offset = min; |
|
|
|
|
|
|
|
max -= min; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
offset + if max == u64::MAX { |
|
|
|
|
|
|
|
rand::thread_rng().gen() |
|
|
|
|
|
|
|
} else { |
|
|
|
|
|
|
|
rand::thread_rng().gen_range(0, max) |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
}; |
|
|
|
|
|
|
|
state.write(dst, val)?; |
|
|
|
|
|
|
|
state.update_status(val); |
|
|
|
|
|
|
|
} |
|
|
|
ArithOp::Add { dst, a, b } => { |
|
|
|
ArithOp::Add { dst, a, b } => { |
|
|
|
state.clear_status(); |
|
|
|
state.clear_status(); |
|
|
|
let x = state.read(a)?; |
|
|
|
let x = state.read(a)?; |
|
|
@ -201,6 +225,87 @@ impl OpTrait for ArithOp { |
|
|
|
state.write(dst, res)?; |
|
|
|
state.write(dst, res)?; |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
ArithOp::Sw32 { dst, src } => { |
|
|
|
|
|
|
|
let val = state.read(src)?; |
|
|
|
|
|
|
|
let res = ((val & 0xFFFF_FFFF) << 32) | ((val & !0xFFFF_FFFF) >> 32); |
|
|
|
|
|
|
|
state.write(dst, res)?; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
ArithOp::Sw16 { dst, src } => { |
|
|
|
|
|
|
|
let val = state.read(src)?; |
|
|
|
|
|
|
|
let res = ((val & 0x0000_FFFF_0000_FFFF) << 16) | ((val & 0xFFFF_0000_FFFF_0000) >> 16); |
|
|
|
|
|
|
|
state.write(dst, res)?; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
ArithOp::Sw8 { dst, src } => { |
|
|
|
|
|
|
|
let val = state.read(src)?; |
|
|
|
|
|
|
|
let res = ((val & 0x00FF_00FF_00FF_00FF) << 8) | ((val & 0xFF00_FF00_FF00_FF00) >> 8); |
|
|
|
|
|
|
|
state.write(dst, res)?; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
ArithOp::Rev { dst, src } => { |
|
|
|
|
|
|
|
let val = state.read(src)?; |
|
|
|
|
|
|
|
let res = val.swap_bytes(); |
|
|
|
|
|
|
|
state.write(dst, res)?; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
ArithOp::Rbit { dst, src } => { |
|
|
|
|
|
|
|
let val = state.read(src)?; |
|
|
|
|
|
|
|
let res = val.reverse_bits(); |
|
|
|
|
|
|
|
state.write(dst, res)?; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
ArithOp::Clz { dst, src } => { |
|
|
|
|
|
|
|
state.clear_status(); |
|
|
|
|
|
|
|
let val = state.read(src)?; |
|
|
|
|
|
|
|
let res = val.leading_zeros() as u64; |
|
|
|
|
|
|
|
state.update_status(res); |
|
|
|
|
|
|
|
state.write(dst, res)?; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
ArithOp::Clz32 { dst, src } => { |
|
|
|
|
|
|
|
state.clear_status(); |
|
|
|
|
|
|
|
let val = state.read(src)?; |
|
|
|
|
|
|
|
let res = (((val & 0xFFFF_FFFF) << 32) | 0xFFFF_FFFF).leading_zeros() as u64; |
|
|
|
|
|
|
|
state.update_status(res); |
|
|
|
|
|
|
|
state.write(dst, res)?; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
ArithOp::Clz16 { dst, src } => { |
|
|
|
|
|
|
|
state.clear_status(); |
|
|
|
|
|
|
|
let val = state.read(src)?; |
|
|
|
|
|
|
|
let res = (((val & 0xFFFF) << 48) | 0xFFFF_FFFF_FFFF).leading_zeros() as u64; |
|
|
|
|
|
|
|
state.update_status(res); |
|
|
|
|
|
|
|
state.write(dst, res)?; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
ArithOp::Clz8 { dst, src } => { |
|
|
|
|
|
|
|
state.clear_status(); |
|
|
|
|
|
|
|
let val = state.read(src)?; |
|
|
|
|
|
|
|
let res = (((val & 0xFF) << 56) | 0x00FF_FFFF_FFFF_FFFF).leading_zeros() as u64; |
|
|
|
|
|
|
|
state.update_status(res); |
|
|
|
|
|
|
|
state.write(dst, res)?; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
ArithOp::Clo { dst, src } => { |
|
|
|
|
|
|
|
state.clear_status(); |
|
|
|
|
|
|
|
let val = state.read(src)?; |
|
|
|
|
|
|
|
let res = val.leading_ones() as u64; |
|
|
|
|
|
|
|
state.update_status(res); |
|
|
|
|
|
|
|
state.write(dst, res)?; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
ArithOp::Clo32 { dst, src } => { |
|
|
|
|
|
|
|
state.clear_status(); |
|
|
|
|
|
|
|
let val = state.read(src)?; |
|
|
|
|
|
|
|
let res = ((val & 0xFFFF_FFFF) << 32).leading_ones() as u64; |
|
|
|
|
|
|
|
state.update_status(res); |
|
|
|
|
|
|
|
state.write(dst, res)?; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
ArithOp::Clo16 { dst, src } => { |
|
|
|
|
|
|
|
state.clear_status(); |
|
|
|
|
|
|
|
let val = state.read(src)?; |
|
|
|
|
|
|
|
let res = ((val & 0xFFFF) << 48).leading_ones() as u64; |
|
|
|
|
|
|
|
state.update_status(res); |
|
|
|
|
|
|
|
state.write(dst, res)?; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
ArithOp::Clo8 { dst, src } => { |
|
|
|
|
|
|
|
state.clear_status(); |
|
|
|
|
|
|
|
let val = state.read(src)?; |
|
|
|
|
|
|
|
let res = ((val & 0xFF) << 56).leading_ones() as u64; |
|
|
|
|
|
|
|
state.update_status(res); |
|
|
|
|
|
|
|
state.write(dst, res)?; |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
Ok(eres) |
|
|
|
Ok(eres) |
|
|
@ -212,15 +317,20 @@ impl OpTrait for ArithOp { |
|
|
|
ArithOp::Test { a } => sexp::list(&[A("test"), A(a)]), |
|
|
|
ArithOp::Test { a } => sexp::list(&[A("test"), A(a)]), |
|
|
|
ArithOp::Compare { a, b } => sexp::list(&[A("cmp"), A(a), A(b)]), |
|
|
|
ArithOp::Compare { a, b } => sexp::list(&[A("cmp"), A(a), A(b)]), |
|
|
|
ArithOp::RangeTest { val, a: start, b: end } => sexp::list(&[A("rcmp"), A(val), A(start), A(end)]), |
|
|
|
ArithOp::RangeTest { val, a: start, b: end } => sexp::list(&[A("rcmp"), A(val), A(start), A(end)]), |
|
|
|
|
|
|
|
ArithOp::Rng { dst, min, max } => { |
|
|
|
|
|
|
|
if min.is_immediate_equal(0) && max.is_immediate_equal(u64::MAX) { |
|
|
|
|
|
|
|
sexp::list(&[A("rng"), A(dst)]) |
|
|
|
|
|
|
|
} else if min.is_immediate_equal(0) { |
|
|
|
|
|
|
|
sexp::list(&[A("rng"), A(dst), A(max)]) |
|
|
|
|
|
|
|
} else { |
|
|
|
|
|
|
|
sexp::list(&[A("rng"), A(dst), A(min), A(max)]) |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
ArithOp::Sub { dst, a, b } => to_sexp_2_or_3("sub", dst, a, b), |
|
|
|
ArithOp::Sub { dst, a, b } => to_sexp_2_or_3("sub", dst, a, b), |
|
|
|
ArithOp::Mul { dst, a, b } => to_sexp_2_or_3("mul", dst, a, b), |
|
|
|
ArithOp::Mul { dst, a, b } => to_sexp_2_or_3("mul", dst, a, b), |
|
|
|
ArithOp::Div { dst, rem, a, div } => { |
|
|
|
ArithOp::Div { dst, rem, a, div } => { |
|
|
|
if rem.is_discard() { |
|
|
|
if rem.is_discard() { |
|
|
|
if &dst.as_rd() == a { |
|
|
|
to_sexp_2_or_3("div", dst, a, div) |
|
|
|
sexp::list(&[A("div"), A(dst), A(div)]) |
|
|
|
|
|
|
|
} else { |
|
|
|
|
|
|
|
sexp::list(&[A("div"), A(dst), A(a), A(div)]) |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} else { |
|
|
|
} else { |
|
|
|
if &dst.as_rd() == a { |
|
|
|
if &dst.as_rd() == a { |
|
|
|
sexp::list(&[A("divr"), A(dst), A(rem), A(div)]) |
|
|
|
sexp::list(&[A("divr"), A(dst), A(rem), A(div)]) |
|
|
@ -233,18 +343,25 @@ impl OpTrait for ArithOp { |
|
|
|
ArithOp::And { dst, a, b } => to_sexp_2_or_3("and", dst, a, b), |
|
|
|
ArithOp::And { dst, a, b } => to_sexp_2_or_3("and", dst, a, b), |
|
|
|
ArithOp::Or { dst, a, b } => to_sexp_2_or_3("or", dst, a, b), |
|
|
|
ArithOp::Or { dst, a, b } => to_sexp_2_or_3("or", dst, a, b), |
|
|
|
ArithOp::Xor { dst, a, b } => to_sexp_2_or_3("xor", dst, a, b), |
|
|
|
ArithOp::Xor { dst, a, b } => to_sexp_2_or_3("xor", dst, a, b), |
|
|
|
ArithOp::Cpl { dst, a } => { |
|
|
|
ArithOp::Cpl { dst, a } => to_sexp_1_or_2("cpl", dst, a), |
|
|
|
if &dst.as_rd() == a { |
|
|
|
|
|
|
|
sexp::list(&[A("cpl"), A(dst)]) |
|
|
|
|
|
|
|
} else { |
|
|
|
|
|
|
|
sexp::list(&[A("cpl"), A(dst), A(a)]) |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
ArithOp::Rol { dst, a, n } => to_sexp_2_or_3("rol", dst, a, n), |
|
|
|
ArithOp::Rol { dst, a, n } => to_sexp_2_or_3("rol", dst, a, n), |
|
|
|
ArithOp::Ror { dst, a, n } => to_sexp_2_or_3("ror", dst, a, n), |
|
|
|
ArithOp::Ror { dst, a, n } => to_sexp_2_or_3("ror", dst, a, n), |
|
|
|
ArithOp::Lsl { dst, a, n } => to_sexp_2_or_3("lsl", dst, a, n), |
|
|
|
ArithOp::Lsl { dst, a, n } => to_sexp_2_or_3("lsl", dst, a, n), |
|
|
|
ArithOp::Lsr { dst, a, n } => to_sexp_2_or_3("lsr", dst, a, n), |
|
|
|
ArithOp::Lsr { dst, a, n } => to_sexp_2_or_3("lsr", dst, a, n), |
|
|
|
ArithOp::Asr { dst, a, n } => to_sexp_2_or_3("asr", dst, a, n), |
|
|
|
ArithOp::Asr { dst, a, n } => to_sexp_2_or_3("asr", dst, a, n), |
|
|
|
|
|
|
|
ArithOp::Sw32 { dst, src } => to_sexp_1_or_2("sw32", dst, src), |
|
|
|
|
|
|
|
ArithOp::Sw16 { dst, src } => to_sexp_1_or_2("sw16", dst, src), |
|
|
|
|
|
|
|
ArithOp::Sw8 { dst, src } => to_sexp_1_or_2("sw8", dst, src), |
|
|
|
|
|
|
|
ArithOp::Rev { dst, src } => to_sexp_1_or_2("rev", dst, src), |
|
|
|
|
|
|
|
ArithOp::Rbit { dst, src } => to_sexp_1_or_2("rbit", dst, src), |
|
|
|
|
|
|
|
ArithOp::Clz { dst, src } => to_sexp_1_or_2("clz", dst, src), |
|
|
|
|
|
|
|
ArithOp::Clz32 { dst, src } => to_sexp_1_or_2("clz32", dst, src), |
|
|
|
|
|
|
|
ArithOp::Clz16 { dst, src } => to_sexp_1_or_2("clz16", dst, src), |
|
|
|
|
|
|
|
ArithOp::Clz8 { dst, src } => to_sexp_1_or_2("clz8", dst, src), |
|
|
|
|
|
|
|
ArithOp::Clo { dst, src } => to_sexp_1_or_2("clo", dst, src), |
|
|
|
|
|
|
|
ArithOp::Clo32 { dst, src } => to_sexp_1_or_2("clo32", dst, src), |
|
|
|
|
|
|
|
ArithOp::Clo16 { dst, src } => to_sexp_1_or_2("clo16", dst, src), |
|
|
|
|
|
|
|
ArithOp::Clo8 { dst, src } => to_sexp_1_or_2("clo8", dst, src), |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
@ -256,3 +373,11 @@ fn to_sexp_2_or_3(name: &str, dst: &Wr, a: &Rd, b: &Rd) -> Sexp { |
|
|
|
sexp::list(&[A(name), A(dst), A(a), A(b)]) |
|
|
|
sexp::list(&[A(name), A(dst), A(a), A(b)]) |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
fn to_sexp_1_or_2(name: &str, dst: &Wr, src: &Rd) -> Sexp { |
|
|
|
|
|
|
|
if &dst.as_rd() == src { |
|
|
|
|
|
|
|
sexp::list(&[A(name), A(dst)]) |
|
|
|
|
|
|
|
} else { |
|
|
|
|
|
|
|
sexp::list(&[A(name), A(dst), A(src)]) |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|