cmov
This commit is contained in:
parent
f74be7dbbf
commit
65598cf86e
63
src/cpu.rs
63
src/cpu.rs
@ -33,6 +33,14 @@ impl Cpu {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn gr(&self, r: u8) -> u32{
|
||||||
|
self.reg[r as usize]
|
||||||
|
}
|
||||||
|
|
||||||
|
fn sr(&mut self, r: u8, v:u32) {
|
||||||
|
self.reg[r as usize] = v
|
||||||
|
}
|
||||||
|
|
||||||
fn ins(&self) -> Result<Instruction,CpuError> {
|
fn ins(&self) -> Result<Instruction,CpuError> {
|
||||||
if self.finger as usize >= self.arr[&0].len() {
|
if self.finger as usize >= self.arr[&0].len() {
|
||||||
return Err(CpuError::ProgramEnd);
|
return Err(CpuError::ProgramEnd);
|
||||||
@ -75,14 +83,23 @@ impl Cpu {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn handle_ortho(&mut self, i:SpecialInstruction) -> Result<(), CpuError>{
|
fn handle_ortho(&mut self, i:SpecialInstruction) -> Result<(), CpuError>{
|
||||||
self.reg[i.a as usize] = i.v as u32;
|
self.sr(i.a, i.v);
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
fn handle_cmov(&mut self, i:NormalInstruction) -> Result<(), CpuError>{
|
||||||
|
if self.gr(i.c) > 0 {
|
||||||
|
let v = self.gr(i.b);
|
||||||
|
self.sr(i.a, v);
|
||||||
|
}
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
fn exec(&mut self, instruction: Instruction)-> Result<(), CpuError>{
|
fn exec(&mut self, instruction: Instruction)-> Result<(), CpuError>{
|
||||||
println!("{:?}", instruction);
|
//println!("{:?}", instruction);
|
||||||
match instruction {
|
match instruction {
|
||||||
Instruction::Ortho(i) => self.handle_ortho(i),
|
Instruction::Ortho(i) => self.handle_ortho(i),
|
||||||
|
Instruction::ConditionalMove(i) => self.handle_cmov(i),
|
||||||
_ => Err(CpuError::InvalidInstruction)
|
_ => Err(CpuError::InvalidInstruction)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -121,4 +138,46 @@ mod tests{
|
|||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_cmov() -> Result<(), String> {
|
||||||
|
let pgm = vec!(
|
||||||
|
ezop(13, 1, 65535, 0),
|
||||||
|
ezop(13, 2, 1337, 0),
|
||||||
|
ezop(13, 3, 1, 0),
|
||||||
|
ezop(0, 1, 2, 3),
|
||||||
|
);
|
||||||
|
let mut c = Cpu::new(pgm);
|
||||||
|
_ = c.step();
|
||||||
|
_ = c.step();
|
||||||
|
_ = c.step();
|
||||||
|
let result = c.step();
|
||||||
|
assert!(matches!(result, Ok(_)));
|
||||||
|
assert_eq!(c.reg[0], 0);
|
||||||
|
assert_eq!(c.reg[3], 1);
|
||||||
|
assert_eq!(c.reg[2], 1337);
|
||||||
|
assert_eq!(c.reg[1], 1337);
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_cmov_neg() -> Result<(), String> {
|
||||||
|
let pgm = vec!(
|
||||||
|
ezop(13, 1, 65535, 0),
|
||||||
|
ezop(13, 2, 1337, 0),
|
||||||
|
ezop(13, 3, 0, 0),
|
||||||
|
ezop(0, 1, 2, 3),
|
||||||
|
);
|
||||||
|
let mut c = Cpu::new(pgm);
|
||||||
|
_ = c.step();
|
||||||
|
_ = c.step();
|
||||||
|
_ = c.step();
|
||||||
|
let result = c.step();
|
||||||
|
assert!(matches!(result, Ok(_)));
|
||||||
|
assert_eq!(c.reg[0], 0);
|
||||||
|
assert_eq!(c.reg[3], 0);
|
||||||
|
assert_eq!(c.reg[2], 1337);
|
||||||
|
assert_eq!(c.reg[1], 65535);
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
Loading…
Reference in New Issue
Block a user