Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix stp #42

Merged
merged 1 commit into from
Jan 12, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
70 changes: 57 additions & 13 deletions src/isa/exec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -469,14 +469,9 @@ impl InstructionSet for ArithmeticOp {
reg,
idx,
regs.get(reg, idx).and_then(|val| {
if step.as_i16() < 0 {
val.int_sub(Number::from(-step.as_i16()), IntFlags {
Copy link
Contributor Author

@6293 6293 Jan 12, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When applying Stp to A8 register, we are not sure that the step value (which is stored as i16) is within the scope of 8 bits. Maybe we should make Step i8, or add a method on Number that check if a number is representable in target bits (reshape method does not check that. it always returns false if a target bits are less than current bits)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Very good point! I think we need to reduce step down to u8 - but we need to make sure that the byte command is updated correspondingly. Let's do it as a separate PR (or pls open an issue for that just not to forget)

signed: false,
wrap: false,
})
} else {
val.int_add(Number::from(*step), IntFlags { signed: false, wrap: false })
}
let mut n = Number::from(*step);
n.reshape(val.layout());
val.int_add(n, IntFlags { signed: false, wrap: false })
}),
),
ArithmeticOp::Neg(reg, idx) => {
Expand Down Expand Up @@ -912,14 +907,63 @@ impl InstructionSet for ReservedOp {
#[cfg(test)]
mod tests {
use super::*;
use crate::data::Step;

#[test]
fn cmp_ne_test() {
let mut register = CoreRegs::default();
let lib_site = LibSite::default();
PutOp::PutA(RegA::A8, Reg32::Reg1, MaybeNumber::from(12).into())
.exec(&mut register, lib_site);
PutOp::PutA(RegA::A8, Reg32::Reg2, MaybeNumber::from(9).into())
.exec(&mut register, lib_site);
assert_eq!(true, register.st0);
CmpOp::EqA(NoneEqFlag::NonEqual, RegA::A8, Reg32::Reg1, Reg32::Reg2)
.exec(&mut register, lib_site);
assert_eq!(false, register.st0);
}

#[test]
fn cmp_eq_test() {
let mut register = CoreRegs::default();
let lib_site = LibSite::default();
PutOp::PutA(RegA::A8, Reg32::Reg1, MaybeNumber::from(9).into())
.exec(&mut register, lib_site);
PutOp::PutA(RegA::A8, Reg32::Reg2, MaybeNumber::from(9).into())
.exec(&mut register, lib_site);
assert_eq!(true, register.st0);
CmpOp::EqA(NoneEqFlag::NonEqual, RegA::A8, Reg32::Reg1, Reg32::Reg2)
.exec(&mut register, lib_site);
assert_eq!(true, register.st0);
}

#[test]
fn stp_add_test() {
let mut register = CoreRegs::default();
let lib_site = LibSite::default();
PutOp::PutA(RegA::A8, Reg32::Reg1, MaybeNumber::from(3).into())
.exec(&mut register, lib_site);
ArithmeticOp::Stp(RegA::A8, Reg32::Reg1, Step::with(4)).exec(&mut register, lib_site);
PutOp::PutA(RegA::A8, Reg32::Reg2, MaybeNumber::from(7).into())
.exec(&mut register, lib_site);
assert_eq!(true, register.st0);
CmpOp::EqA(NoneEqFlag::NonEqual, RegA::A8, Reg32::Reg1, Reg32::Reg2)
.exec(&mut register, lib_site);
assert_eq!(true, register.st0);
}

#[test]
fn put_test() {
fn stp_sub_test() {
let mut register = CoreRegs::default();
let lib_site = LibSite::default();
let instr = PutOp::PutA(RegA::A8, Reg32::Reg1, MaybeNumber::from(3).into());
instr.exec(&mut register, lib_site);
let number = register.get(RegA::A8, Reg32::Reg1).unwrap();
assert_eq!(Number::from([3u8; 1]), number);
PutOp::PutA(RegA::A8, Reg32::Reg1, MaybeNumber::from(3).into())
.exec(&mut register, lib_site);
ArithmeticOp::Stp(RegA::A8, Reg32::Reg1, Step::with(-4)).exec(&mut register, lib_site);
PutOp::PutA(RegA::A8, Reg32::Reg2, MaybeNumber::from(-1i8).into())
.exec(&mut register, lib_site);
assert_eq!(true, register.st0);
CmpOp::EqA(NoneEqFlag::NonEqual, RegA::A8, Reg32::Reg1, Reg32::Reg2)
.exec(&mut register, lib_site);
assert_eq!(true, register.st0);
}
}